diff options
6 files changed, 117 insertions, 24 deletions
diff --git a/core/java/android/hardware/display/AmbientDisplayConfiguration.java b/core/java/android/hardware/display/AmbientDisplayConfiguration.java index f2c294ad8391..518b22bd5e10 100644 --- a/core/java/android/hardware/display/AmbientDisplayConfiguration.java +++ b/core/java/android/hardware/display/AmbientDisplayConfiguration.java @@ -148,7 +148,7 @@ public class AmbientDisplayConfiguration { /** {@hide} */ public String tapSensorType(int posture) { - return getSensorTypeForPosture( + return getSensorFromPostureMapping( mContext.getResources().getStringArray(R.array.config_dozeTapSensorPostureMapping), tapSensorType(), posture @@ -252,17 +252,18 @@ public class AmbientDisplayConfiguration { return Settings.Secure.getIntForUser(mContext.getContentResolver(), name, def, user) != 0; } - private String getSensorTypeForPosture( + /** {@hide} */ + public static String getSensorFromPostureMapping( String[] postureMapping, - String defaultSensorType, + String defaultValue, int posture) { - String sensorType = defaultSensorType; + String sensorType = defaultValue; if (posture < postureMapping.length) { sensorType = postureMapping[posture]; } else { Log.e(TAG, "Unsupported doze posture " + posture); } - return TextUtils.isEmpty(sensorType) ? defaultSensorType : sensorType; + return TextUtils.isEmpty(sensorType) ? defaultValue : sensorType; } } diff --git a/packages/SystemUI/res/values/config.xml b/packages/SystemUI/res/values/config.xml index fd2b0e0845fb..79dece051df7 100644 --- a/packages/SystemUI/res/values/config.xml +++ b/packages/SystemUI/res/values/config.xml @@ -220,6 +220,16 @@ always-on display) --> <string name="doze_brightness_sensor_type" translatable="false"></string> + <!-- Name of a sensor per posture state that provides a low-power estimate of the desired + display brightness, suitable to listen to while the device is asleep (e.g. during + always-on display) --> + <string-array name="doze_brightness_sensor_name_posture_mapping" translatable="false"> + <item></item> <!-- UNKNOWN --> + <item></item> <!-- CLOSED --> + <item></item> <!-- HALF_OPENED --> + <item></item> <!-- OPENED --> + </string-array> + <!-- Override value to use for proximity sensor. --> <string name="proximity_sensor_type" translatable="false"></string> diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeSensors.java b/packages/SystemUI/src/com/android/systemui/doze/DozeSensors.java index 099252b609ac..3cefce83393a 100644 --- a/packages/SystemUI/src/com/android/systemui/doze/DozeSensors.java +++ b/packages/SystemUI/src/com/android/systemui/doze/DozeSensors.java @@ -148,7 +148,7 @@ public class DozeSensors { false /* requires prox */, dozeLog), new TriggerSensor( - findSensorWithType(config.doubleTapSensorType()), + findSensor(config.doubleTapSensorType()), Settings.Secure.DOZE_DOUBLE_TAP_GESTURE, true /* configured */, DozeLog.REASON_SENSOR_DOUBLE_TAP, @@ -156,7 +156,7 @@ public class DozeSensors { true /* touchscreen */, dozeLog), new TriggerSensor( - findSensorWithType(config.tapSensorType(mDevicePosture)), + findSensor(config.tapSensorType(mDevicePosture)), Settings.Secure.DOZE_TAP_SCREEN_GESTURE, true /* settingDef */, true /* configured */, @@ -167,7 +167,7 @@ public class DozeSensors { dozeParameters.singleTapUsesProx(mDevicePosture) /* requiresProx */, dozeLog), new TriggerSensor( - findSensorWithType(config.longPressSensorType()), + findSensor(config.longPressSensorType()), Settings.Secure.DOZE_PULSE_ON_LONG_PRESS, false /* settingDef */, true /* configured */, @@ -178,7 +178,7 @@ public class DozeSensors { dozeParameters.longPressUsesProx() /* requiresProx */, dozeLog), new TriggerSensor( - findSensorWithType(config.udfpsLongPressSensorType()), + findSensor(config.udfpsLongPressSensorType()), "doze_pulse_on_auth", true /* settingDef */, udfpsEnrolled && (alwaysOn || mScreenOffUdfpsEnabled), @@ -206,7 +206,7 @@ public class DozeSensors { mConfig.getWakeLockScreenDebounce(), dozeLog), new TriggerSensor( - findSensorWithType(config.quickPickupSensorType()), + findSensor(config.quickPickupSensorType()), Settings.Secure.DOZE_QUICK_PICKUP_GESTURE, true /* setting default */, config.quickPickupSensorEnabled(KeyguardUpdateMonitor.getCurrentUser()) @@ -244,21 +244,29 @@ public class DozeSensors { mDebounceFrom = SystemClock.uptimeMillis(); } - private Sensor findSensorWithType(String type) { - return findSensorWithType(mSensorManager, type); + private Sensor findSensor(String type) { + return findSensor(mSensorManager, type, null); } /** - * Utility method to find a {@link Sensor} for the supplied string type. + * Utility method to find a {@link Sensor} for the supplied string type and string name. + * + * Return the first sensor in the list that matches the specified inputs. Ignores type or name + * if the input is null or empty. + * + * @param type sensorType + * @parm name sensorName, to differentiate between sensors with the same type */ - public static Sensor findSensorWithType(SensorManager sensorManager, String type) { - if (TextUtils.isEmpty(type)) { - return null; - } - List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL); - for (Sensor s : sensorList) { - if (type.equals(s.getStringType())) { - return s; + public static Sensor findSensor(SensorManager sensorManager, String type, String name) { + final boolean isNameSpecified = !TextUtils.isEmpty(name); + final boolean isTypeSpecified = !TextUtils.isEmpty(type); + if (isNameSpecified || isTypeSpecified) { + final List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL); + for (Sensor sensor : sensors) { + if ((!isNameSpecified || name.equals(sensor.getName())) + && (!isTypeSpecified || type.equals(sensor.getStringType()))) { + return sensor; + } } } return null; diff --git a/packages/SystemUI/src/com/android/systemui/doze/dagger/DozeModule.java b/packages/SystemUI/src/com/android/systemui/doze/dagger/DozeModule.java index 9c6e02a7924e..571b666e4573 100644 --- a/packages/SystemUI/src/com/android/systemui/doze/dagger/DozeModule.java +++ b/packages/SystemUI/src/com/android/systemui/doze/dagger/DozeModule.java @@ -38,6 +38,7 @@ import com.android.systemui.doze.DozeTriggers; import com.android.systemui.doze.DozeUi; import com.android.systemui.doze.DozeWallpaperState; import com.android.systemui.statusbar.phone.DozeParameters; +import com.android.systemui.statusbar.policy.DevicePostureController; import com.android.systemui.util.sensors.AsyncSensorManager; import com.android.systemui.util.wakelock.DelayedWakeLock; import com.android.systemui.util.wakelock.WakeLock; @@ -94,8 +95,15 @@ public abstract class DozeModule { @Provides @BrightnessSensor static Optional<Sensor> providesBrightnessSensor( - AsyncSensorManager sensorManager, Context context) { - return Optional.ofNullable(DozeSensors.findSensorWithType(sensorManager, - context.getString(R.string.doze_brightness_sensor_type))); + AsyncSensorManager sensorManager, + Context context, + DozeParameters dozeParameters, + DevicePostureController devicePostureController) { + return Optional.ofNullable( + DozeSensors.findSensor( + sensorManager, + context.getString(R.string.doze_brightness_sensor_type), + dozeParameters.brightnessName(devicePostureController.getDevicePosture()) + )); } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeParameters.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeParameters.java index 611f273320b0..5bf982b908cd 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeParameters.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeParameters.java @@ -283,6 +283,17 @@ public class DozeParameters implements } /** + * Sensor to use for brightness changes. + */ + public String brightnessName(@DevicePostureController.DevicePostureInt int posture) { + return AmbientDisplayConfiguration.getSensorFromPostureMapping( + mResources.getStringArray(R.array.doze_brightness_sensor_name_posture_mapping), + null /* defaultValue */, + posture + ); + } + + /** * Callback to listen for DozeParameter changes. */ public void addCallback(Callback callback) { diff --git a/packages/SystemUI/tests/src/com/android/systemui/doze/DozeSensorsTest.java b/packages/SystemUI/tests/src/com/android/systemui/doze/DozeSensorsTest.java index 57ab40c2eab1..42e34c81a790 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/doze/DozeSensorsTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/doze/DozeSensorsTest.java @@ -19,6 +19,7 @@ package com.android.systemui.doze; import static com.android.systemui.doze.DozeLog.REASON_SENSOR_TAP; import static com.android.systemui.plugins.SensorManagerPlugin.Sensor.TYPE_WAKE_LOCK_SCREEN; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; @@ -59,6 +60,11 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; import java.util.function.Consumer; @RunWith(AndroidTestingRunner.class) @@ -274,6 +280,36 @@ public class DozeSensorsTest extends SysuiTestCase { verify(mAmbientDisplayConfiguration).tapSensorType(eq(mDevicePosture)); } + @Test + public void testFindSensor() throws Exception { + // GIVEN a prox sensor + List<Sensor> sensors = new ArrayList<>(); + Sensor proxSensor = + createSensor(Sensor.TYPE_PROXIMITY, Sensor.STRING_TYPE_PROXIMITY); + sensors.add(proxSensor); + + when(mSensorManager.getSensorList(anyInt())).thenReturn(sensors); + + // WHEN we try to find the prox sensor with the same type and name + // THEN we find the added sensor + assertEquals( + proxSensor, + DozeSensors.findSensor( + mSensorManager, + Sensor.STRING_TYPE_PROXIMITY, + proxSensor.getName())); + + // WHEN we try to find a prox sensor with a different name + // THEN no sensor is found + assertEquals( + null, + DozeSensors.findSensor( + mSensorManager, + Sensor.STRING_TYPE_PROXIMITY, + "some other name")); + } + + private class TestableDozeSensors extends DozeSensors { TestableDozeSensors() { @@ -305,4 +341,23 @@ public class DozeSensorsTest extends SysuiTestCase { mDozeLog); } } + + public static void setSensorType(Sensor sensor, int type, String strType) throws Exception { + Method setter = Sensor.class.getDeclaredMethod("setType", Integer.TYPE); + setter.setAccessible(true); + setter.invoke(sensor, type); + if (strType != null) { + Field f = sensor.getClass().getDeclaredField("mStringType"); + f.setAccessible(true); + f.set(sensor, strType); + } + } + + public static Sensor createSensor(int type, String strType) throws Exception { + Constructor<Sensor> constr = Sensor.class.getDeclaredConstructor(); + constr.setAccessible(true); + Sensor sensor = constr.newInstance(); + setSensorType(sensor, type, strType); + return sensor; + } } |