Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
Added restriction registerListener
Browse files Browse the repository at this point in the history
Fixes #1878
  • Loading branch information
M66B committed Aug 15, 2014
1 parent c827b78 commit 272bbb5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Version 2.99.x and version 3.x will be available with a [pro license](http://www

**Next release**

* Added restriction *registerListener* to the *Sensors* category, which will limit the rate of the gyroscope to 100 Hz to prevent eavesdropping ([issue](/../../issues/1878))
* Updated Slovenian translation

[Open issues](https://github.com/M66B/XPrivacy/issues?state=open)
Expand Down
1 change: 1 addition & 0 deletions res/values/functions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@
<!-- sensors -->
<string name="sensors_getDefaultSensor" translatable="false"><![CDATA[<a href="https://developer.android.com/reference/android/hardware/SensorManager.html#getDefaultSensor(int)">Google documentation</a>]]></string>
<string name="sensors_getSensorList" translatable="false"><![CDATA[<a href="https://developer.android.com/reference/android/hardware/SensorManager.html#getSensorList(int)">Google documentation</a>]]></string>
<string name="sensors_registerListener" translatable="false"><![CDATA[Will limit the rate of the gyroscope to 100 Hz to prevent eavesdropping<br /><br /><a href="http://developer.android.com/reference/android/hardware/SensorManager.html#registerListener(android.hardware.SensorEventListener,%20android.hardware.Sensor, int)">Google documentation</a>]]></string>
<string name="sensors_acceleration" translatable="false"><![CDATA[<a href="https://developer.android.com/reference/android/hardware/Sensor.html">Google documentation</a>]]></string>
<string name="sensors_gravity" translatable="false"><![CDATA[<a href="https://developer.android.com/reference/android/hardware/Sensor.html">Google documentation</a>]]></string>
<string name="sensors_humidity" translatable="false"><![CDATA[<a href="https://developer.android.com/reference/android/hardware/Sensor.html">Google documentation</a>]]></string>
Expand Down
1 change: 1 addition & 0 deletions src/biz/bokhorst/xprivacy/Meta.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ public static List<Hook> get() {

mListHook.add(new Hook("sensors", "getDefaultSensor", "", 3, null, null).unsafe().dangerous());
mListHook.add(new Hook("sensors", "getSensorList", "", 3, null, null).unsafe().dangerous());
mListHook.add(new Hook("sensors", "registerListener", "", 3, "2.99.27", null).unsafe());
mListHook.add(new Hook("sensors", "acceleration", "", 3, null, null).unsafe());
mListHook.add(new Hook("sensors", "gravity", "", 3, null, null).unsafe());
mListHook.add(new Hook("sensors", "humidity", "", 3, null, null).unsafe());
Expand Down
59 changes: 44 additions & 15 deletions src/biz/bokhorst/xprivacy/XSensorManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class XSensorManager extends XHook {
private String mClassName;
private static final String cClassName = "android.hardware.SensorManager";

private static final int cMaxRateUs = (int) (0.01 * 1000 * 1000); // 100 Hz

private XSensorManager(Methods method, String restrictionName, String className) {
super(restrictionName, method.name(), null);
mMethod = method;
Expand All @@ -25,14 +27,18 @@ public String getClassName() {

// public Sensor getDefaultSensor(int type)
// public List<Sensor> getSensorList(int type)
// boolean registerListener(SensorEventListener listener, Sensor sensor, int rateUs, int maxBatchReportLatencyUs)
// boolean registerListener(SensorEventListener listener, Sensor sensor, int rateUs, Handler handler)
// boolean registerListener(SensorEventListener listener, Sensor sensor, int rateUs, int maxBatchReportLatencyUs, Handler handler)
// boolean registerListener(SensorEventListener listener, Sensor sensor, int rateUs)
// frameworks/base/core/java/android/hardware/SensorManager.java
// http://developer.android.com/reference/android/hardware/SensorManager.html
// http://developer.android.com/reference/android/hardware/Sensor.html

// @formatter:on

private enum Methods {
getDefaultSensor, getSensorList
getDefaultSensor, getSensorList, registerListener
};

public static List<XHook> getInstances(String className) {
Expand All @@ -43,41 +49,64 @@ public static List<XHook> getInstances(String className) {

listHook.add(new XSensorManager(Methods.getDefaultSensor, PrivacyManager.cSensors, className));
listHook.add(new XSensorManager(Methods.getSensorList, PrivacyManager.cSensors, className));
listHook.add(new XSensorManager(Methods.registerListener, PrivacyManager.cSensors, className));
}
return listHook;
}

@Override
protected void before(XParam param) throws Throwable {
if (mMethod == Methods.getDefaultSensor) {
switch (mMethod) {
case getDefaultSensor:
if (isRestricted(param))
param.setResult(null);
else if (param.args.length > 0)
else if (param.args.length > 0 && param.args[0] instanceof Integer)
if (isRestricted(param, (Integer) param.args[0]))
param.setResult(null);
break;

} else if (mMethod == Methods.getSensorList) {
case getSensorList:
if (isRestricted(param))
param.setResult(new ArrayList<Sensor>());
else if (param.args.length > 0)
else if (param.args.length > 0 && param.args[0] instanceof Integer)
if (isRestricted(param, (Integer) param.args[0]))
param.setResult(new ArrayList<Sensor>());
break;

} else
Util.log(this, Log.WARN, "Unknown method=" + param.method.getName());
case registerListener:
if (param.args.length > 2 && param.args[1] instanceof Sensor && param.args[2] instanceof Integer) {
int type = ((Sensor) param.args[1]).getType();
if (type == Sensor.TYPE_GYROSCOPE || type == Sensor.TYPE_GYROSCOPE_UNCALIBRATED) {
int rateUs = (Integer) param.args[2];
if (rateUs < cMaxRateUs)
if (isRestricted(param))
param.args[2] = cMaxRateUs;
}
}
break;
}
}

@Override
@SuppressWarnings("unchecked")
protected void after(XParam param) throws Throwable {
if (mMethod == Methods.getSensorList)
if (param.getResult() != null && param.args.length > 0 && (Integer) param.args[0] == Sensor.TYPE_ALL) {
List<Sensor> listSensor = new ArrayList<Sensor>();
for (Sensor sensor : (List<Sensor>) param.getResult())
if (!isRestricted(param, sensor.getType()))
listSensor.add(sensor);
param.setResult(listSensor);
}
switch (mMethod) {
case getDefaultSensor:
case registerListener:
// Do nothing
break;

case getSensorList:
if (param.getResult() != null && param.args.length > 0 && param.args[0] instanceof Integer)
if ((Integer) param.args[0] == Sensor.TYPE_ALL) {
List<Sensor> listSensor = new ArrayList<Sensor>();
for (Sensor sensor : (List<Sensor>) param.getResult())
if (!isRestricted(param, sensor.getType()))
listSensor.add(sensor);
param.setResult(listSensor);
}
break;
}
}

@SuppressWarnings("deprecation")
Expand Down

0 comments on commit 272bbb5

Please sign in to comment.