Skip to content

Commit

Permalink
Bugfixes and attempts to use quaternions for the gyroscope.
Browse files Browse the repository at this point in the history
Fixed support for API18-22 (TIL that one should never write code at 2a.m and make a release).
Added a warning for the user if the magnetic sensor returns weird/wrong values.
Added a way to fill a little bit more the fields of the Sensor class.
Now using the calculations of the gravity sensor to calculate the gyroscope, this allows it to work in any orientation and return the right values.
  • Loading branch information
Frazew committed Jul 26, 2016
1 parent d6271ed commit 731e41a
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ public class SensorModel {
public int minDelay;
public float maxRange;
public boolean alreadyThere = false;
public String stringType;
public String permission;

public SensorModel(int sensorType, String name, int handle, float resolution, int minDelay, float maxRange) {
public SensorModel(int sensorType, String name, int handle, float resolution, int minDelay, float maxRange, String stringType, String permission) {
this.name = name;
this.handle = (handle == -1 ? sensorType : handle);
this.resolution = resolution;
this.minDelay = minDelay;
this.maxRange = maxRange;
this.stringType = stringType;
this.permission = permission;
}
}
18 changes: 8 additions & 10 deletions app/src/main/java/fr/frazew/virtualgyroscope/XposedMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.util.SparseArray;

import de.robv.android.xposed.XposedBridge;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.robv.android.xposed.IXposedHookLoadPackage;
Expand All @@ -26,11 +23,11 @@
public class XposedMod implements IXposedHookLoadPackage {

public static final SparseArray<SensorModel> sensorsToEmulate = new SparseArray<SensorModel>() {{
put(Sensor.TYPE_ROTATION_VECTOR, new SensorModel(Sensor.TYPE_ROTATION_VECTOR, "VirtualSensor RotationVector", -1, 0.01F, -1, -1));
put(Sensor.TYPE_GYROSCOPE, new SensorModel(Sensor.TYPE_GYROSCOPE, "VirtualSensor Gyroscope", -1, 0.01F, -1, (float)Math.PI));
put(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR, new SensorModel(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR, "VirtualSensor GeomagneticRotationVector", -1, 0.01F, -1, -1));
put(Sensor.TYPE_GRAVITY, new SensorModel(Sensor.TYPE_GRAVITY, "VirtualSensor Gravity", -1, 0.01F, -1, -1));
put(Sensor.TYPE_LINEAR_ACCELERATION, new SensorModel(Sensor.TYPE_LINEAR_ACCELERATION, "VirtualSensor LinearAcceleration", 4242, 0.01F, -1, -1)); // Had to use another handle as it broke the magnetic sensor's readings (?!)
put(Sensor.TYPE_ROTATION_VECTOR, new SensorModel(Sensor.TYPE_ROTATION_VECTOR, "VirtualSensor RotationVector", -1, 0.01F, -1, -1, Sensor.STRING_TYPE_ROTATION_VECTOR, "none"));
put(Sensor.TYPE_GYROSCOPE, new SensorModel(Sensor.TYPE_GYROSCOPE, "VirtualSensor Gyroscope", -1, 0.01F, -1, (float)Math.PI, Sensor.STRING_TYPE_GYROSCOPE, "android.hardware.sensor.gyroscope"));
put(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR, new SensorModel(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR, "VirtualSensor GeomagneticRotationVector", -1, 0.01F, -1, -1, Sensor.STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR, "none"));
put(Sensor.TYPE_GRAVITY, new SensorModel(Sensor.TYPE_GRAVITY, "VirtualSensor Gravity", -1, 0.01F, -1, -1, Sensor.STRING_TYPE_GRAVITY, "none"));
put(Sensor.TYPE_LINEAR_ACCELERATION, new SensorModel(Sensor.TYPE_LINEAR_ACCELERATION, "VirtualSensor LinearAcceleration", 4242, 0.01F, -1, -1, Sensor.STRING_TYPE_LINEAR_ACCELERATION, "none")); // Had to use another handle as it broke the magnetic sensor's readings (?!)
}};

@Override
Expand Down Expand Up @@ -101,7 +98,7 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
XposedBridge.hookAllMethods(pkgMgrSrv, "hasSystemFeature", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (!(boolean)param.getResult() && (String)param.args[0] == PackageManager.FEATURE_SENSOR_GYROSCOPE) {
if (!(boolean) param.getResult() && (String) param.args[0] == PackageManager.FEATURE_SENSOR_GYROSCOPE) {
Object mPackages = XposedHelpers.getObjectField(param.thisObject, "mPackages");
synchronized (mPackages) {
Map<String, FeatureInfo> mAvailableFeatures = (Map<String, FeatureInfo>) param.getResult();
Expand All @@ -121,7 +118,8 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {

private void hookSensorValues(final LoadPackageParam lpparam) {
if (Build.VERSION.SDK_INT >= 18) {
XposedHelpers.findAndHookMethod("android.hardware.SystemSensorManager$SensorEventQueue", lpparam.classLoader, "dispatchSensorEvent", int.class, float[].class, int.class, long.class, new SensorChangeHook.API18Plus(lpparam));
if (Build.VERSION.SDK_INT >= 23) XposedHelpers.findAndHookMethod("android.hardware.SystemSensorManager$SensorEventQueue", lpparam.classLoader, "dispatchSensorEvent", int.class, float[].class, int.class, long.class, new SensorChangeHook.API23Plus(lpparam));
else XposedHelpers.findAndHookMethod("android.hardware.SystemSensorManager$SensorEventQueue", lpparam.classLoader, "dispatchSensorEvent", int.class, float[].class, int.class, long.class, new SensorChangeHook.API18Plus(lpparam));
} else {
XposedHelpers.findAndHookMethod("android.hardware.SystemSensorManager$ListenerDelegate", lpparam.classLoader, "onSensorChangedLocked", Sensor.class, float[].class, long[].class, int.class, new SensorChangeHook.API1617(lpparam));
}
Expand Down
Loading

0 comments on commit 731e41a

Please sign in to comment.