Merge "Fix issue where sensors wouldn't be reregistered" into qt-r1-dev

This commit is contained in:
TreeHugger Robot
2019-07-16 18:53:45 +00:00
committed by Android (Google) Code Review
5 changed files with 73 additions and 33 deletions

View File

@@ -198,7 +198,10 @@ public class DozeSensors {
updateListening(); updateListening();
} }
private void updateListening() { /**
* Registers/unregisters sensors based on internal state.
*/
public void updateListening() {
boolean anyListening = false; boolean anyListening = false;
for (TriggerSensor s : mSensors) { for (TriggerSensor s : mSensors) {
// We don't want to be listening while we're PAUSED (prox sensor is covered) // We don't want to be listening while we're PAUSED (prox sensor is covered)
@@ -231,7 +234,7 @@ public class DozeSensors {
public void onUserSwitched() { public void onUserSwitched() {
for (TriggerSensor s : mSensors) { for (TriggerSensor s : mSensors) {
s.updateListener(); s.updateListening();
} }
} }
@@ -246,7 +249,7 @@ public class DozeSensors {
return; return;
} }
for (TriggerSensor s : mSensors) { for (TriggerSensor s : mSensors) {
s.updateListener(); s.updateListening();
} }
} }
}; };
@@ -409,22 +412,22 @@ public class DozeSensors {
public void setListening(boolean listen) { public void setListening(boolean listen) {
if (mRequested == listen) return; if (mRequested == listen) return;
mRequested = listen; mRequested = listen;
updateListener(); updateListening();
} }
public void setDisabled(boolean disabled) { public void setDisabled(boolean disabled) {
if (mDisabled == disabled) return; if (mDisabled == disabled) return;
mDisabled = disabled; mDisabled = disabled;
updateListener(); updateListening();
} }
public void ignoreSetting(boolean ignored) { public void ignoreSetting(boolean ignored) {
if (mIgnoresSetting == ignored) return; if (mIgnoresSetting == ignored) return;
mIgnoresSetting = ignored; mIgnoresSetting = ignored;
updateListener(); updateListening();
} }
public void updateListener() { public void updateListening() {
if (!mConfigured || mSensor == null) return; if (!mConfigured || mSensor == null) return;
if (mRequested && !mDisabled && (enabledBySetting() || mIgnoresSetting) if (mRequested && !mDisabled && (enabledBySetting() || mIgnoresSetting)
&& !mRegistered) { && !mRegistered) {
@@ -480,7 +483,7 @@ public class DozeSensors {
mCallback.onSensorPulse(mPulseReason, mSensorPerformsProxCheck, screenX, screenY, mCallback.onSensorPulse(mPulseReason, mSensorPerformsProxCheck, screenX, screenY,
event.values); event.values);
if (!mRegistered) { if (!mRegistered) {
updateListener(); // reregister, this sensor only fires once updateListening(); // reregister, this sensor only fires once
} }
})); }));
} }
@@ -541,7 +544,7 @@ public class DozeSensors {
} }
@Override @Override
public void updateListener() { public void updateListening() {
if (!mConfigured) return; if (!mConfigured) return;
AsyncSensorManager asyncSensorManager = (AsyncSensorManager) mSensorManager; AsyncSensorManager asyncSensorManager = (AsyncSensorManager) mSensorManager;
if (mRequested && !mDisabled && (enabledBySetting() || mIgnoresSetting) if (mRequested && !mDisabled && (enabledBySetting() || mIgnoresSetting)

View File

@@ -314,6 +314,9 @@ public class DozeTriggers implements DozeMachine.Part {
break; break;
case DOZE_PULSE_DONE: case DOZE_PULSE_DONE:
mDozeSensors.requestTemporaryDisable(); mDozeSensors.requestTemporaryDisable();
// A pulse will temporarily disable sensors that require a touch screen.
// Let's make sure that they are re-enabled when the pulse is over.
mDozeSensors.updateListening();
break; break;
case FINISH: case FINISH:
mBroadcastReceiver.unregister(mContext); mBroadcastReceiver.unregister(mContext);

View File

@@ -24,6 +24,7 @@ import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings; import static org.mockito.Mockito.withSettings;
import com.android.systemui.statusbar.phone.DozeParameters; import com.android.systemui.statusbar.phone.DozeParameters;
import com.android.systemui.utils.hardware.FakeSensorManager;
import org.mockito.Answers; import org.mockito.Answers;
import org.mockito.MockSettings; import org.mockito.MockSettings;
@@ -39,6 +40,7 @@ public class DozeConfigurationUtil {
when(params.getPickupPerformsProxCheck()).thenReturn(true); when(params.getPickupPerformsProxCheck()).thenReturn(true);
when(params.getPolicy()).thenReturn(mock(AlwaysOnDisplayPolicy.class)); when(params.getPolicy()).thenReturn(mock(AlwaysOnDisplayPolicy.class));
when(params.doubleTapReportsTouchCoordinates()).thenReturn(false); when(params.doubleTapReportsTouchCoordinates()).thenReturn(false);
when(params.getDisplayNeedsBlanking()).thenReturn(false);
doneHolder[0] = true; doneHolder[0] = true;
return params; return params;
@@ -52,11 +54,17 @@ public class DozeConfigurationUtil {
when(config.pickupGestureEnabled(anyInt())).thenReturn(false); when(config.pickupGestureEnabled(anyInt())).thenReturn(false);
when(config.pulseOnNotificationEnabled(anyInt())).thenReturn(true); when(config.pulseOnNotificationEnabled(anyInt())).thenReturn(true);
when(config.alwaysOnEnabled(anyInt())).thenReturn(false); when(config.alwaysOnEnabled(anyInt())).thenReturn(false);
when(config.enabled(anyInt())).thenReturn(true);
when(config.getWakeLockScreenDebounce()).thenReturn(0L);
when(config.doubleTapSensorType()).thenReturn(null); when(config.doubleTapSensorType()).thenReturn(null);
when(config.tapSensorType()).thenReturn(null); when(config.tapSensorType()).thenReturn(null);
when(config.longPressSensorType()).thenReturn(null); when(config.longPressSensorType()).thenReturn(null);
when(config.tapGestureEnabled(anyInt())).thenReturn(true);
when(config.tapSensorAvailable()).thenReturn(true);
when(config.tapSensorType()).thenReturn(FakeSensorManager.TAP_SENSOR_TYPE);
when(config.dozePickupSensorAvailable()).thenReturn(false); when(config.dozePickupSensorAvailable()).thenReturn(false);
when(config.wakeScreenGestureAvailable()).thenReturn(false); when(config.wakeScreenGestureAvailable()).thenReturn(false);

View File

@@ -19,6 +19,7 @@ package com.android.systemui.doze;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
@@ -27,18 +28,17 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.Instrumentation; import android.hardware.Sensor;
import android.hardware.display.AmbientDisplayConfiguration; import android.hardware.display.AmbientDisplayConfiguration;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.testing.AndroidTestingRunner; import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper.RunWithLooper; import android.testing.TestableLooper.RunWithLooper;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import com.android.systemui.SysuiTestCase; import com.android.systemui.SysuiTestCase;
import com.android.systemui.dock.DockManagerFake; import com.android.systemui.dock.DockManager;
import com.android.systemui.statusbar.phone.DozeParameters; import com.android.systemui.statusbar.phone.DozeParameters;
import com.android.systemui.util.wakelock.WakeLock; import com.android.systemui.util.wakelock.WakeLock;
import com.android.systemui.util.wakelock.WakeLockFake; import com.android.systemui.util.wakelock.WakeLockFake;
@@ -46,14 +46,12 @@ import com.android.systemui.utils.hardware.FakeSensorManager;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@SmallTest @SmallTest
@Ignore("failing")
@RunWith(AndroidTestingRunner.class) @RunWith(AndroidTestingRunner.class)
@RunWithLooper @RunWithLooper(setAsMainLooper = true)
public class DozeTriggersTest extends SysuiTestCase { public class DozeTriggersTest extends SysuiTestCase {
private DozeTriggers mTriggers; private DozeTriggers mTriggers;
private DozeMachine mMachine; private DozeMachine mMachine;
@@ -61,10 +59,10 @@ public class DozeTriggersTest extends SysuiTestCase {
private AmbientDisplayConfiguration mConfig; private AmbientDisplayConfiguration mConfig;
private DozeParameters mParameters; private DozeParameters mParameters;
private FakeSensorManager mSensors; private FakeSensorManager mSensors;
private Sensor mTapSensor;
private WakeLock mWakeLock; private WakeLock mWakeLock;
private Instrumentation mInstrumentation;
private AlarmManager mAlarmManager; private AlarmManager mAlarmManager;
private DockManagerFake mDockManagerFake; private DockManager mDockManagerFake;
@BeforeClass @BeforeClass
public static void setupSuite() { public static void setupSuite() {
@@ -74,15 +72,15 @@ public class DozeTriggersTest extends SysuiTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
mInstrumentation = InstrumentationRegistry.getInstrumentation();
mMachine = mock(DozeMachine.class); mMachine = mock(DozeMachine.class);
mAlarmManager = mock(AlarmManager.class); mAlarmManager = mock(AlarmManager.class);
mHost = new DozeHostFake(); mHost = spy(new DozeHostFake());
mConfig = DozeConfigurationUtil.createMockConfig(); mConfig = DozeConfigurationUtil.createMockConfig();
mParameters = DozeConfigurationUtil.createMockParameters(); mParameters = DozeConfigurationUtil.createMockParameters();
mSensors = new FakeSensorManager(mContext); mSensors = spy(new FakeSensorManager(mContext));
mTapSensor = mSensors.getFakeTapSensor().getSensor();
mWakeLock = new WakeLockFake(); mWakeLock = new WakeLockFake();
mDockManagerFake = spy(new DockManagerFake()); mDockManagerFake = mock(DockManager.class);
mTriggers = new DozeTriggers(mContext, mMachine, mHost, mAlarmManager, mConfig, mParameters, mTriggers = new DozeTriggers(mContext, mMachine, mHost, mAlarmManager, mConfig, mParameters,
mSensors, Handler.createAsync(Looper.myLooper()), mWakeLock, true, mSensors, Handler.createAsync(Looper.myLooper()), mWakeLock, true,
@@ -95,29 +93,45 @@ public class DozeTriggersTest extends SysuiTestCase {
mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
mTriggers.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE); mTriggers.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE);
clearInvocations(mMachine);
mHost.callback.onNotificationAlerted(); mHost.callback.onNotificationAlerted();
mSensors.getMockProximitySensor().sendProximityResult(false); /* Near */ mSensors.getMockProximitySensor().sendProximityResult(false); /* Near */
verify(mMachine, never()).requestState(any()); verify(mMachine, never()).requestState(any());
verify(mMachine, never()).requestPulse(anyInt()); verify(mMachine, never()).requestPulse(anyInt());
mHost.callback.onNotificationAlerted(); mHost.callback.onNotificationAlerted();
mSensors.getMockProximitySensor().sendProximityResult(true); /* Far */ mSensors.getMockProximitySensor().sendProximityResult(true); /* Far */
verify(mMachine).requestPulse(anyInt()); verify(mMachine).requestPulse(anyInt());
} }
@Test
public void testTransitionTo_disablesAndEnablesTouchSensors() {
when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
mTriggers.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE);
verify(mSensors).requestTriggerSensor(any(), eq(mTapSensor));
clearInvocations(mSensors);
mTriggers.transitionTo(DozeMachine.State.DOZE,
DozeMachine.State.DOZE_REQUEST_PULSE);
mTriggers.transitionTo(DozeMachine.State.DOZE_REQUEST_PULSE,
DozeMachine.State.DOZE_PULSING);
verify(mSensors).cancelTriggerSensor(any(), eq(mTapSensor));
clearInvocations(mSensors);
mTriggers.transitionTo(DozeMachine.State.DOZE_PULSING, DozeMachine.State.DOZE_PULSE_DONE);
verify(mSensors).requestTriggerSensor(any(), eq(mTapSensor));
}
@Test @Test
public void testDockEventListener_registerAndUnregister() { public void testDockEventListener_registerAndUnregister() {
mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED); mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
verify(mDockManagerFake).addListener(any()); verify(mDockManagerFake).addListener(any());
mTriggers.transitionTo(DozeMachine.State.DOZE, DozeMachine.State.FINISH); mTriggers.transitionTo(DozeMachine.State.DOZE, DozeMachine.State.FINISH);
verify(mDockManagerFake).removeListener(any()); verify(mDockManagerFake).removeListener(any());
} }
@@ -128,7 +142,6 @@ public class DozeTriggersTest extends SysuiTestCase {
mTriggers.onSensor(DozeLog.REASON_SENSOR_DOUBLE_TAP, mTriggers.onSensor(DozeLog.REASON_SENSOR_DOUBLE_TAP,
false /* sensorPerformedProxCheck */, 50 /* screenX */, 50 /* screenY */, false /* sensorPerformedProxCheck */, 50 /* screenX */, 50 /* screenY */,
null /* rawValues */); null /* rawValues */);
verify(mMachine, never()).wakeUp(); verify(mMachine, never()).wakeUp();
} }
@@ -142,7 +155,7 @@ public class DozeTriggersTest extends SysuiTestCase {
false /* sensorPerformedProxCheck */, 50 /* screenX */, 50 /* screenY */, false /* sensorPerformedProxCheck */, 50 /* screenX */, 50 /* screenY */,
null /* rawValues */); null /* rawValues */);
verify(mHost).setAodDimmingScrim(eq(255)); verify(mHost).setAodDimmingScrim(eq(255f));
verify(mMachine).wakeUp(); verify(mMachine).wakeUp();
} }
} }

View File

@@ -40,18 +40,23 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Nullable;
/** /**
* Rudimentary fake for SensorManager * Rudimentary fake for SensorManager
* *
* Currently only supports the proximity sensor. * Currently only supports proximity, light and tap sensors.
* *
* Note that this class ignores the "Handler" argument, so the test is responsible for calling the * Note that this class ignores the "Handler" argument, so the test is responsible for calling the
* listener on the right thread. * listener on the right thread.
*/ */
public class FakeSensorManager extends SensorManager { public class FakeSensorManager extends SensorManager {
public static final String TAP_SENSOR_TYPE = "tapSensorType";
private final MockProximitySensor mMockProximitySensor; private final MockProximitySensor mMockProximitySensor;
private final FakeGenericSensor mFakeLightSensor; private final FakeGenericSensor mFakeLightSensor;
private final FakeGenericSensor mFakeTapSensor;
private final FakeGenericSensor[] mSensors; private final FakeGenericSensor[] mSensors;
public FakeSensorManager(Context context) throws Exception { public FakeSensorManager(Context context) throws Exception {
@@ -59,12 +64,13 @@ public class FakeSensorManager extends SensorManager {
.getDefaultSensor(Sensor.TYPE_PROXIMITY); .getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (proxSensor == null) { if (proxSensor == null) {
// No prox? Let's create a fake one! // No prox? Let's create a fake one!
proxSensor = createSensor(Sensor.TYPE_PROXIMITY); proxSensor = createSensor(Sensor.TYPE_PROXIMITY, null);
} }
mSensors = new FakeGenericSensor[]{ mSensors = new FakeGenericSensor[]{
mMockProximitySensor = new MockProximitySensor(proxSensor), mMockProximitySensor = new MockProximitySensor(proxSensor),
mFakeLightSensor = new FakeGenericSensor(createSensor(Sensor.TYPE_LIGHT)), mFakeLightSensor = new FakeGenericSensor(createSensor(Sensor.TYPE_LIGHT, null)),
mFakeTapSensor = new FakeGenericSensor(createSensor(99, TAP_SENSOR_TYPE))
}; };
} }
@@ -76,6 +82,10 @@ public class FakeSensorManager extends SensorManager {
return mFakeLightSensor; return mFakeLightSensor;
} }
public FakeGenericSensor getFakeTapSensor() {
return mFakeTapSensor;
}
@Override @Override
public Sensor getDefaultSensor(int type) { public Sensor getDefaultSensor(int type) {
Sensor s = super.getDefaultSensor(type); Sensor s = super.getDefaultSensor(type);
@@ -160,13 +170,13 @@ public class FakeSensorManager extends SensorManager {
@Override @Override
protected boolean requestTriggerSensorImpl(TriggerEventListener listener, Sensor sensor) { protected boolean requestTriggerSensorImpl(TriggerEventListener listener, Sensor sensor) {
return false; return true;
} }
@Override @Override
protected boolean cancelTriggerSensorImpl(TriggerEventListener listener, Sensor sensor, protected boolean cancelTriggerSensorImpl(TriggerEventListener listener, Sensor sensor,
boolean disable) { boolean disable) {
return false; return true;
} }
@Override @Override
@@ -185,12 +195,15 @@ public class FakeSensorManager extends SensorManager {
return false; return false;
} }
private Sensor createSensor(int type) throws Exception { private Sensor createSensor(int type, @Nullable String stringType) throws Exception {
Constructor<Sensor> constr = Sensor.class.getDeclaredConstructor(); Constructor<Sensor> constr = Sensor.class.getDeclaredConstructor();
constr.setAccessible(true); constr.setAccessible(true);
Sensor sensor = constr.newInstance(); Sensor sensor = constr.newInstance();
setSensorType(sensor, type); setSensorType(sensor, type);
if (stringType != null) {
setSensorField(sensor, "mStringType", stringType);
}
setSensorField(sensor, "mName", "Mock " + sensor.getStringType() + "/" + type); setSensorField(sensor, "mName", "Mock " + sensor.getStringType() + "/" + type);
setSensorField(sensor, "mVendor", "Mock Vendor"); setSensorField(sensor, "mVendor", "Mock Vendor");
setSensorField(sensor, "mVersion", 1); setSensorField(sensor, "mVersion", 1);