Merge "Use alternative prox sensor when specified." into rvc-dev am: 9cfce017d7
Change-Id: I0ad2dd143d76afe5a7b3ac7c5fb9be5131d8d50b
This commit is contained in:
@@ -201,6 +201,13 @@
|
||||
always-on display) -->
|
||||
<string name="doze_brightness_sensor_type" translatable="false"></string>
|
||||
|
||||
<!-- Override value to use for proximity sensor. -->
|
||||
<string name="proximity_sensor_type" translatable="false"></string>
|
||||
|
||||
<!-- If using proximity_sensor_type, specifies a threshold value to distinguish near and
|
||||
far break points. A sensor value less than this is considered "near". -->
|
||||
<item name="proximity_sensor_threshold" translatable="false" format="float" type="dimen"></item>
|
||||
|
||||
<!-- Doze: pulse parameter - how long does it take to fade in? -->
|
||||
<integer name="doze_pulse_duration_in">130</integer>
|
||||
|
||||
|
||||
@@ -44,8 +44,7 @@ public class ProximitySensor {
|
||||
|
||||
private final Sensor mSensor;
|
||||
private final AsyncSensorManager mSensorManager;
|
||||
private final boolean mUsingBrightnessSensor;
|
||||
private final float mMaxRange;
|
||||
private final float mThreshold;
|
||||
private List<ProximitySensorListener> mListeners = new ArrayList<>();
|
||||
private String mTag = null;
|
||||
@VisibleForTesting ProximityEvent mLastEvent;
|
||||
@@ -68,20 +67,27 @@ public class ProximitySensor {
|
||||
public ProximitySensor(@Main Resources resources,
|
||||
AsyncSensorManager sensorManager) {
|
||||
mSensorManager = sensorManager;
|
||||
Sensor sensor = findBrightnessSensor(resources);
|
||||
|
||||
Sensor sensor = findCustomProxSensor(resources);
|
||||
float threshold = 0;
|
||||
if (sensor != null) {
|
||||
try {
|
||||
threshold = getCustomProxThreshold(resources);
|
||||
} catch (IllegalStateException e) {
|
||||
Log.e(TAG, "Can not load custom proximity sensor.", e);
|
||||
sensor = null;
|
||||
}
|
||||
}
|
||||
if (sensor == null) {
|
||||
mUsingBrightnessSensor = false;
|
||||
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
|
||||
} else {
|
||||
mUsingBrightnessSensor = true;
|
||||
if (sensor != null) {
|
||||
threshold = sensor.getMaximumRange();
|
||||
}
|
||||
}
|
||||
|
||||
mThreshold = threshold;
|
||||
|
||||
mSensor = sensor;
|
||||
if (mSensor != null) {
|
||||
mMaxRange = mSensor.getMaximumRange();
|
||||
} else {
|
||||
mMaxRange = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void setTag(String tag) {
|
||||
@@ -107,9 +113,15 @@ public class ProximitySensor {
|
||||
mPaused = false;
|
||||
registerInternal();
|
||||
}
|
||||
/**
|
||||
* Returns a brightness sensor that can be used for proximity purposes.
|
||||
*/
|
||||
private Sensor findCustomProxSensor(Resources resources) {
|
||||
String sensorType = resources.getString(R.string.proximity_sensor_type);
|
||||
if (sensorType.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private Sensor findBrightnessSensor(Resources resources) {
|
||||
String sensorType = resources.getString(R.string.doze_brightness_sensor_type);
|
||||
List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
|
||||
Sensor sensor = null;
|
||||
for (Sensor s : sensorList) {
|
||||
@@ -122,6 +134,17 @@ public class ProximitySensor {
|
||||
return sensor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a threshold value that can be used along with {@link #findCustomProxSensor}
|
||||
*/
|
||||
private float getCustomProxThreshold(Resources resources) {
|
||||
try {
|
||||
return resources.getFloat(R.dimen.proximity_sensor_threshold);
|
||||
} catch (Resources.NotFoundException e) {
|
||||
throw new IllegalStateException("R.dimen.proximity_sensor_threshold must be set.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if we are registered with the SensorManager.
|
||||
*/
|
||||
@@ -157,7 +180,6 @@ public class ProximitySensor {
|
||||
if (mRegistered || mPaused || mListeners.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
logDebug("Using brightness sensor? " + mUsingBrightnessSensor);
|
||||
logDebug("Registering sensor listener");
|
||||
mRegistered = true;
|
||||
mSensorManager.registerListener(mSensorEventListener, mSensor, mSensorDelay);
|
||||
@@ -196,10 +218,7 @@ public class ProximitySensor {
|
||||
}
|
||||
|
||||
private void onSensorEvent(SensorEvent event) {
|
||||
boolean near = event.values[0] < mMaxRange;
|
||||
if (mUsingBrightnessSensor) {
|
||||
near = event.values[0] == 0;
|
||||
}
|
||||
boolean near = event.values[0] < mThreshold;
|
||||
mLastEvent = new ProximityEvent(near, event.timestamp);
|
||||
alertListeners();
|
||||
}
|
||||
|
||||
@@ -20,11 +20,9 @@ import android.content.res.Resources;
|
||||
|
||||
public class FakeProximitySensor extends ProximitySensor {
|
||||
private boolean mAvailable;
|
||||
private boolean mPaused;
|
||||
|
||||
public FakeProximitySensor(Resources resources, AsyncSensorManager sensorManager) {
|
||||
super(resources, sensorManager);
|
||||
|
||||
mAvailable = true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user