Merge "Pause Primary Prox when Secondary Running." into sc-qpr1-dev

This commit is contained in:
Dave Mankoff
2021-08-17 19:39:20 +00:00
committed by Android (Google) Code Review
2 changed files with 92 additions and 44 deletions

View File

@@ -87,15 +87,23 @@ public class ProximitySensor implements ThresholdSensor {
&& (mLastPrimaryEvent == null && (mLastPrimaryEvent == null
|| !mLastPrimaryEvent.getBelow() || !mLastPrimaryEvent.getBelow()
|| !event.getBelow())) { || !event.getBelow())) {
mSecondaryThresholdSensor.pause(); chooseSensor();
if (mLastPrimaryEvent == null || !mLastPrimaryEvent.getBelow()) { if (mLastPrimaryEvent == null || !mLastPrimaryEvent.getBelow()) {
// Only check the secondary as long as the primary thinks we're near. // Only check the secondary as long as the primary thinks we're near.
mCancelSecondaryRunnable = null; if (mCancelSecondaryRunnable != null) {
mCancelSecondaryRunnable.run();
mCancelSecondaryRunnable = null;
}
return; return;
} else { } else {
// Check this sensor again in a moment. // Check this sensor again in a moment.
mCancelSecondaryRunnable = mDelayableExecutor.executeDelayed( mCancelSecondaryRunnable = mDelayableExecutor.executeDelayed(() -> {
mSecondaryThresholdSensor::resume, SECONDARY_PING_INTERVAL_MS); // This is safe because we know that mSecondaryThresholdSensor
// is loaded, otherwise we wouldn't be here.
mPrimaryThresholdSensor.pause();
mSecondaryThresholdSensor.resume();
},
SECONDARY_PING_INTERVAL_MS);
} }
} }
logDebug("Secondary sensor event: " + event.getBelow() + "."); logDebug("Secondary sensor event: " + event.getBelow() + ".");
@@ -159,12 +167,8 @@ public class ProximitySensor implements ThresholdSensor {
* of what is reported by the primary sensor. * of what is reported by the primary sensor.
*/ */
public void setSecondarySafe(boolean safe) { public void setSecondarySafe(boolean safe) {
mSecondarySafe = safe; mSecondarySafe = mSecondaryThresholdSensor.isLoaded() && safe;
if (!mSecondarySafe) { chooseSensor();
mSecondaryThresholdSensor.pause();
} else {
mSecondaryThresholdSensor.resume();
}
} }
/** /**
@@ -209,16 +213,30 @@ public class ProximitySensor implements ThresholdSensor {
return; return;
} }
if (!mInitializedListeners) { if (!mInitializedListeners) {
mPrimaryThresholdSensor.pause();
mSecondaryThresholdSensor.pause();
mPrimaryThresholdSensor.register(mPrimaryEventListener); mPrimaryThresholdSensor.register(mPrimaryEventListener);
if (!mSecondarySafe) {
mSecondaryThresholdSensor.pause();
}
mSecondaryThresholdSensor.register(mSecondaryEventListener); mSecondaryThresholdSensor.register(mSecondaryEventListener);
mInitializedListeners = true; mInitializedListeners = true;
} }
logDebug("Registering sensor listener"); logDebug("Registering sensor listener");
mPrimaryThresholdSensor.resume();
mRegistered = true; mRegistered = true;
chooseSensor();
}
private void chooseSensor() {
mExecution.assertIsMainThread();
if (!mRegistered || mPaused || mListeners.isEmpty()) {
return;
}
if (mSecondarySafe) {
mSecondaryThresholdSensor.resume();
mPrimaryThresholdSensor.pause();
} else {
mPrimaryThresholdSensor.resume();
mSecondaryThresholdSensor.pause();
}
} }
/** /**
@@ -312,7 +330,7 @@ public class ProximitySensor implements ThresholdSensor {
} }
if (!mSecondarySafe && !event.getBelow()) { if (!mSecondarySafe && !event.getBelow()) {
mSecondaryThresholdSensor.pause(); chooseSensor();
} }
mLastEvent = event; mLastEvent = event;

View File

@@ -16,6 +16,8 @@
package com.android.systemui.util.sensors; package com.android.systemui.util.sensors;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@@ -338,30 +340,25 @@ public class ProximitySensorDualTest extends SysuiTestCase {
@Test @Test
public void testSecondaryCancelsSecondary() { public void testSecondaryCancelsSecondary() {
TestableListener listener = new TestableListener(); TestableListener listener = new TestableListener();
ThresholdSensor.Listener cancelingListener = new ThresholdSensor.Listener() { ThresholdSensor.Listener cancelingListener = event -> mProximitySensor.pause();
@Override
public void onThresholdCrossed(ThresholdSensor.ThresholdSensorEvent event) {
mProximitySensor.pause();
}
};
mProximitySensor.register(listener); mProximitySensor.register(listener);
mProximitySensor.register(cancelingListener); mProximitySensor.register(cancelingListener);
assertNull(listener.mLastEvent); assertThat(listener.mLastEvent).isNull();
assertEquals(0, listener.mCallCount); assertThat(listener.mCallCount).isEqualTo(0);
mThresholdSensorPrimary.triggerEvent(true, 0); mThresholdSensorPrimary.triggerEvent(true, 0);
assertNull(listener.mLastEvent); assertThat(listener.mLastEvent).isNull();
assertEquals(0, listener.mCallCount); assertThat(listener.mCallCount).isEqualTo(0);
mThresholdSensorSecondary.triggerEvent(true, 0); mThresholdSensorSecondary.triggerEvent(true, 0);
assertTrue(listener.mLastEvent.getBelow()); assertThat(listener.mLastEvent.getBelow()).isTrue();
assertEquals(1, listener.mCallCount); assertThat(listener.mCallCount).isEqualTo(1);
// The proximity sensor should now be canceled. Advancing the clock should do nothing. // The proximity sensor should now be canceled. Advancing the clock should do nothing.
assertEquals(0, mFakeExecutor.numPending()); assertThat(mFakeExecutor.numPending()).isEqualTo(0);
mThresholdSensorSecondary.triggerEvent(false, 1); mThresholdSensorSecondary.triggerEvent(false, 1);
assertTrue(listener.mLastEvent.getBelow()); assertThat(listener.mLastEvent.getBelow()).isTrue();
assertEquals(1, listener.mCallCount); assertThat(listener.mCallCount).isEqualTo(1);
mProximitySensor.unregister(listener); mProximitySensor.unregister(listener);
} }
@@ -372,33 +369,66 @@ public class ProximitySensorDualTest extends SysuiTestCase {
TestableListener listener = new TestableListener(); TestableListener listener = new TestableListener();
// WE immediately register the secondary sensor. // We immediately register the secondary sensor.
mProximitySensor.register(listener); mProximitySensor.register(listener);
assertFalse(mThresholdSensorPrimary.isPaused()); assertThat(mThresholdSensorPrimary.isPaused()).isTrue();
assertFalse(mThresholdSensorSecondary.isPaused()); assertThat(mThresholdSensorSecondary.isPaused()).isFalse();
assertNull(listener.mLastEvent); assertThat(listener.mLastEvent).isNull();
assertEquals(0, listener.mCallCount); assertThat(listener.mCallCount).isEqualTo(0);
mThresholdSensorPrimary.triggerEvent(true, 0); mThresholdSensorPrimary.triggerEvent(true, 0);
assertNull(listener.mLastEvent); assertThat(listener.mLastEvent).isNull();
assertEquals(0, listener.mCallCount); assertThat(listener.mCallCount).isEqualTo(0);
mThresholdSensorSecondary.triggerEvent(true, 0); mThresholdSensorSecondary.triggerEvent(true, 0);
assertTrue(listener.mLastEvent.getBelow()); assertThat(listener.mLastEvent.getBelow()).isTrue();
assertEquals(1, listener.mCallCount); assertThat(listener.mCallCount).isEqualTo(1);
// The secondary sensor should now remain resumed indefinitely. // The secondary sensor should now remain resumed indefinitely.
assertFalse(mThresholdSensorSecondary.isPaused()); assertThat(mThresholdSensorSecondary.isPaused()).isFalse();
mThresholdSensorSecondary.triggerEvent(false, 1); mThresholdSensorSecondary.triggerEvent(false, 1);
assertFalse(listener.mLastEvent.getBelow()); assertThat(listener.mLastEvent.getBelow()).isFalse();
assertEquals(2, listener.mCallCount); assertThat(listener.mCallCount).isEqualTo(2);
// The secondary is still running, and not polling with the executor. // The secondary is still running, and not polling with the executor.
assertFalse(mThresholdSensorSecondary.isPaused()); assertThat(mThresholdSensorSecondary.isPaused()).isFalse();
assertEquals(0, mFakeExecutor.numPending()); assertThat(mFakeExecutor.numPending()).isEqualTo(0);
mProximitySensor.unregister(listener); mProximitySensor.unregister(listener);
} }
@Test
public void testSecondaryPausesPrimary() {
TestableListener listener = new TestableListener();
mProximitySensor.register(listener);
assertThat(mThresholdSensorPrimary.isPaused()).isFalse();
assertThat(mThresholdSensorSecondary.isPaused()).isTrue();
mProximitySensor.setSecondarySafe(true);
assertThat(mThresholdSensorPrimary.isPaused()).isTrue();
assertThat(mThresholdSensorSecondary.isPaused()).isFalse();
}
@Test
public void testSecondaryResumesPrimary() {
mProximitySensor.setSecondarySafe(true);
TestableListener listener = new TestableListener();
mProximitySensor.register(listener);
assertThat(mThresholdSensorPrimary.isPaused()).isTrue();
assertThat(mThresholdSensorSecondary.isPaused()).isFalse();
mProximitySensor.setSecondarySafe(false);
assertThat(mThresholdSensorPrimary.isPaused()).isFalse();
assertThat(mThresholdSensorSecondary.isPaused()).isTrue();
}
private static class TestableListener implements ThresholdSensor.Listener { private static class TestableListener implements ThresholdSensor.Listener {
ThresholdSensor.ThresholdSensorEvent mLastEvent; ThresholdSensor.ThresholdSensorEvent mLastEvent;
int mCallCount = 0; int mCallCount = 0;