Merge "Fix issue where sensors wouldn't be reregistered" into qt-r1-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
f61fdd8249
@@ -198,7 +198,10 @@ public class DozeSensors {
|
||||
updateListening();
|
||||
}
|
||||
|
||||
private void updateListening() {
|
||||
/**
|
||||
* Registers/unregisters sensors based on internal state.
|
||||
*/
|
||||
public void updateListening() {
|
||||
boolean anyListening = false;
|
||||
for (TriggerSensor s : mSensors) {
|
||||
// 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() {
|
||||
for (TriggerSensor s : mSensors) {
|
||||
s.updateListener();
|
||||
s.updateListening();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,7 +249,7 @@ public class DozeSensors {
|
||||
return;
|
||||
}
|
||||
for (TriggerSensor s : mSensors) {
|
||||
s.updateListener();
|
||||
s.updateListening();
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -409,22 +412,22 @@ public class DozeSensors {
|
||||
public void setListening(boolean listen) {
|
||||
if (mRequested == listen) return;
|
||||
mRequested = listen;
|
||||
updateListener();
|
||||
updateListening();
|
||||
}
|
||||
|
||||
public void setDisabled(boolean disabled) {
|
||||
if (mDisabled == disabled) return;
|
||||
mDisabled = disabled;
|
||||
updateListener();
|
||||
updateListening();
|
||||
}
|
||||
|
||||
public void ignoreSetting(boolean ignored) {
|
||||
if (mIgnoresSetting == ignored) return;
|
||||
mIgnoresSetting = ignored;
|
||||
updateListener();
|
||||
updateListening();
|
||||
}
|
||||
|
||||
public void updateListener() {
|
||||
public void updateListening() {
|
||||
if (!mConfigured || mSensor == null) return;
|
||||
if (mRequested && !mDisabled && (enabledBySetting() || mIgnoresSetting)
|
||||
&& !mRegistered) {
|
||||
@@ -480,7 +483,7 @@ public class DozeSensors {
|
||||
mCallback.onSensorPulse(mPulseReason, mSensorPerformsProxCheck, screenX, screenY,
|
||||
event.values);
|
||||
if (!mRegistered) {
|
||||
updateListener(); // reregister, this sensor only fires once
|
||||
updateListening(); // reregister, this sensor only fires once
|
||||
}
|
||||
}));
|
||||
}
|
||||
@@ -541,7 +544,7 @@ public class DozeSensors {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateListener() {
|
||||
public void updateListening() {
|
||||
if (!mConfigured) return;
|
||||
AsyncSensorManager asyncSensorManager = (AsyncSensorManager) mSensorManager;
|
||||
if (mRequested && !mDisabled && (enabledBySetting() || mIgnoresSetting)
|
||||
|
||||
@@ -314,6 +314,9 @@ public class DozeTriggers implements DozeMachine.Part {
|
||||
break;
|
||||
case DOZE_PULSE_DONE:
|
||||
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;
|
||||
case FINISH:
|
||||
mBroadcastReceiver.unregister(mContext);
|
||||
|
||||
@@ -24,6 +24,7 @@ import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.withSettings;
|
||||
|
||||
import com.android.systemui.statusbar.phone.DozeParameters;
|
||||
import com.android.systemui.utils.hardware.FakeSensorManager;
|
||||
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.MockSettings;
|
||||
@@ -39,6 +40,7 @@ public class DozeConfigurationUtil {
|
||||
when(params.getPickupPerformsProxCheck()).thenReturn(true);
|
||||
when(params.getPolicy()).thenReturn(mock(AlwaysOnDisplayPolicy.class));
|
||||
when(params.doubleTapReportsTouchCoordinates()).thenReturn(false);
|
||||
when(params.getDisplayNeedsBlanking()).thenReturn(false);
|
||||
|
||||
doneHolder[0] = true;
|
||||
return params;
|
||||
@@ -52,11 +54,17 @@ public class DozeConfigurationUtil {
|
||||
when(config.pickupGestureEnabled(anyInt())).thenReturn(false);
|
||||
when(config.pulseOnNotificationEnabled(anyInt())).thenReturn(true);
|
||||
when(config.alwaysOnEnabled(anyInt())).thenReturn(false);
|
||||
when(config.enabled(anyInt())).thenReturn(true);
|
||||
when(config.getWakeLockScreenDebounce()).thenReturn(0L);
|
||||
|
||||
when(config.doubleTapSensorType()).thenReturn(null);
|
||||
when(config.tapSensorType()).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.wakeScreenGestureAvailable()).thenReturn(false);
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ package com.android.systemui.doze;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.clearInvocations;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
@@ -27,18 +28,17 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.Instrumentation;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.display.AmbientDisplayConfiguration;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.testing.AndroidTestingRunner;
|
||||
import android.testing.TestableLooper.RunWithLooper;
|
||||
|
||||
import androidx.test.InstrumentationRegistry;
|
||||
import androidx.test.filters.SmallTest;
|
||||
|
||||
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.util.wakelock.WakeLock;
|
||||
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.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@SmallTest
|
||||
@Ignore("failing")
|
||||
@RunWith(AndroidTestingRunner.class)
|
||||
@RunWithLooper
|
||||
@RunWithLooper(setAsMainLooper = true)
|
||||
public class DozeTriggersTest extends SysuiTestCase {
|
||||
private DozeTriggers mTriggers;
|
||||
private DozeMachine mMachine;
|
||||
@@ -61,10 +59,10 @@ public class DozeTriggersTest extends SysuiTestCase {
|
||||
private AmbientDisplayConfiguration mConfig;
|
||||
private DozeParameters mParameters;
|
||||
private FakeSensorManager mSensors;
|
||||
private Sensor mTapSensor;
|
||||
private WakeLock mWakeLock;
|
||||
private Instrumentation mInstrumentation;
|
||||
private AlarmManager mAlarmManager;
|
||||
private DockManagerFake mDockManagerFake;
|
||||
private DockManager mDockManagerFake;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupSuite() {
|
||||
@@ -74,15 +72,15 @@ public class DozeTriggersTest extends SysuiTestCase {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
mInstrumentation = InstrumentationRegistry.getInstrumentation();
|
||||
mMachine = mock(DozeMachine.class);
|
||||
mAlarmManager = mock(AlarmManager.class);
|
||||
mHost = new DozeHostFake();
|
||||
mHost = spy(new DozeHostFake());
|
||||
mConfig = DozeConfigurationUtil.createMockConfig();
|
||||
mParameters = DozeConfigurationUtil.createMockParameters();
|
||||
mSensors = new FakeSensorManager(mContext);
|
||||
mSensors = spy(new FakeSensorManager(mContext));
|
||||
mTapSensor = mSensors.getFakeTapSensor().getSensor();
|
||||
mWakeLock = new WakeLockFake();
|
||||
mDockManagerFake = spy(new DockManagerFake());
|
||||
mDockManagerFake = mock(DockManager.class);
|
||||
|
||||
mTriggers = new DozeTriggers(mContext, mMachine, mHost, mAlarmManager, mConfig, mParameters,
|
||||
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.INITIALIZED, DozeMachine.State.DOZE);
|
||||
clearInvocations(mMachine);
|
||||
|
||||
mHost.callback.onNotificationAlerted();
|
||||
|
||||
mSensors.getMockProximitySensor().sendProximityResult(false); /* Near */
|
||||
|
||||
verify(mMachine, never()).requestState(any());
|
||||
verify(mMachine, never()).requestPulse(anyInt());
|
||||
|
||||
mHost.callback.onNotificationAlerted();
|
||||
|
||||
mSensors.getMockProximitySensor().sendProximityResult(true); /* Far */
|
||||
|
||||
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
|
||||
public void testDockEventListener_registerAndUnregister() {
|
||||
mTriggers.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
|
||||
|
||||
verify(mDockManagerFake).addListener(any());
|
||||
|
||||
mTriggers.transitionTo(DozeMachine.State.DOZE, DozeMachine.State.FINISH);
|
||||
|
||||
verify(mDockManagerFake).removeListener(any());
|
||||
}
|
||||
|
||||
@@ -128,7 +142,6 @@ public class DozeTriggersTest extends SysuiTestCase {
|
||||
mTriggers.onSensor(DozeLog.REASON_SENSOR_DOUBLE_TAP,
|
||||
false /* sensorPerformedProxCheck */, 50 /* screenX */, 50 /* screenY */,
|
||||
null /* rawValues */);
|
||||
|
||||
verify(mMachine, never()).wakeUp();
|
||||
}
|
||||
|
||||
@@ -142,7 +155,7 @@ public class DozeTriggersTest extends SysuiTestCase {
|
||||
false /* sensorPerformedProxCheck */, 50 /* screenX */, 50 /* screenY */,
|
||||
null /* rawValues */);
|
||||
|
||||
verify(mHost).setAodDimmingScrim(eq(255));
|
||||
verify(mHost).setAodDimmingScrim(eq(255f));
|
||||
verify(mMachine).wakeUp();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,18 +40,23 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* listener on the right thread.
|
||||
*/
|
||||
public class FakeSensorManager extends SensorManager {
|
||||
|
||||
public static final String TAP_SENSOR_TYPE = "tapSensorType";
|
||||
|
||||
private final MockProximitySensor mMockProximitySensor;
|
||||
private final FakeGenericSensor mFakeLightSensor;
|
||||
private final FakeGenericSensor mFakeTapSensor;
|
||||
private final FakeGenericSensor[] mSensors;
|
||||
|
||||
public FakeSensorManager(Context context) throws Exception {
|
||||
@@ -59,12 +64,13 @@ public class FakeSensorManager extends SensorManager {
|
||||
.getDefaultSensor(Sensor.TYPE_PROXIMITY);
|
||||
if (proxSensor == null) {
|
||||
// No prox? Let's create a fake one!
|
||||
proxSensor = createSensor(Sensor.TYPE_PROXIMITY);
|
||||
proxSensor = createSensor(Sensor.TYPE_PROXIMITY, null);
|
||||
}
|
||||
|
||||
mSensors = new FakeGenericSensor[]{
|
||||
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;
|
||||
}
|
||||
|
||||
public FakeGenericSensor getFakeTapSensor() {
|
||||
return mFakeTapSensor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sensor getDefaultSensor(int type) {
|
||||
Sensor s = super.getDefaultSensor(type);
|
||||
@@ -160,13 +170,13 @@ public class FakeSensorManager extends SensorManager {
|
||||
|
||||
@Override
|
||||
protected boolean requestTriggerSensorImpl(TriggerEventListener listener, Sensor sensor) {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean cancelTriggerSensorImpl(TriggerEventListener listener, Sensor sensor,
|
||||
boolean disable) {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -185,12 +195,15 @@ public class FakeSensorManager extends SensorManager {
|
||||
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();
|
||||
constr.setAccessible(true);
|
||||
Sensor sensor = constr.newInstance();
|
||||
|
||||
setSensorType(sensor, type);
|
||||
if (stringType != null) {
|
||||
setSensorField(sensor, "mStringType", stringType);
|
||||
}
|
||||
setSensorField(sensor, "mName", "Mock " + sensor.getStringType() + "/" + type);
|
||||
setSensorField(sensor, "mVendor", "Mock Vendor");
|
||||
setSensorField(sensor, "mVersion", 1);
|
||||
|
||||
Reference in New Issue
Block a user