Merge "Pause Primary Prox when Secondary Running." into sc-qpr1-dev am: 57f6f21248 am: 1f02f1381f am: 476df837fb

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15587573

Change-Id: I772b77a7babab6a7d734d762cc5f6cd32208745a
This commit is contained in:
Dave Mankoff
2021-08-17 20:15:08 +00:00
committed by Automerger Merge Worker
2 changed files with 92 additions and 44 deletions

View File

@@ -87,15 +87,23 @@ public class ProximitySensor implements ThresholdSensor {
&& (mLastPrimaryEvent == null
|| !mLastPrimaryEvent.getBelow()
|| !event.getBelow())) {
mSecondaryThresholdSensor.pause();
chooseSensor();
if (mLastPrimaryEvent == null || !mLastPrimaryEvent.getBelow()) {
// Only check the secondary as long as the primary thinks we're near.
mCancelSecondaryRunnable = null;
if (mCancelSecondaryRunnable != null) {
mCancelSecondaryRunnable.run();
mCancelSecondaryRunnable = null;
}
return;
} else {
// Check this sensor again in a moment.
mCancelSecondaryRunnable = mDelayableExecutor.executeDelayed(
mSecondaryThresholdSensor::resume, SECONDARY_PING_INTERVAL_MS);
mCancelSecondaryRunnable = mDelayableExecutor.executeDelayed(() -> {
// 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() + ".");
@@ -159,12 +167,8 @@ public class ProximitySensor implements ThresholdSensor {
* of what is reported by the primary sensor.
*/
public void setSecondarySafe(boolean safe) {
mSecondarySafe = safe;
if (!mSecondarySafe) {
mSecondaryThresholdSensor.pause();
} else {
mSecondaryThresholdSensor.resume();
}
mSecondarySafe = mSecondaryThresholdSensor.isLoaded() && safe;
chooseSensor();
}
/**
@@ -209,16 +213,30 @@ public class ProximitySensor implements ThresholdSensor {
return;
}
if (!mInitializedListeners) {
mPrimaryThresholdSensor.pause();
mSecondaryThresholdSensor.pause();
mPrimaryThresholdSensor.register(mPrimaryEventListener);
if (!mSecondarySafe) {
mSecondaryThresholdSensor.pause();
}
mSecondaryThresholdSensor.register(mSecondaryEventListener);
mInitializedListeners = true;
}
logDebug("Registering sensor listener");
mPrimaryThresholdSensor.resume();
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()) {
mSecondaryThresholdSensor.pause();
chooseSensor();
}
mLastEvent = event;

View File

@@ -16,6 +16,8 @@
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.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -338,30 +340,25 @@ public class ProximitySensorDualTest extends SysuiTestCase {
@Test
public void testSecondaryCancelsSecondary() {
TestableListener listener = new TestableListener();
ThresholdSensor.Listener cancelingListener = new ThresholdSensor.Listener() {
@Override
public void onThresholdCrossed(ThresholdSensor.ThresholdSensorEvent event) {
mProximitySensor.pause();
}
};
ThresholdSensor.Listener cancelingListener = event -> mProximitySensor.pause();
mProximitySensor.register(listener);
mProximitySensor.register(cancelingListener);
assertNull(listener.mLastEvent);
assertEquals(0, listener.mCallCount);
assertThat(listener.mLastEvent).isNull();
assertThat(listener.mCallCount).isEqualTo(0);
mThresholdSensorPrimary.triggerEvent(true, 0);
assertNull(listener.mLastEvent);
assertEquals(0, listener.mCallCount);
assertThat(listener.mLastEvent).isNull();
assertThat(listener.mCallCount).isEqualTo(0);
mThresholdSensorSecondary.triggerEvent(true, 0);
assertTrue(listener.mLastEvent.getBelow());
assertEquals(1, listener.mCallCount);
assertThat(listener.mLastEvent.getBelow()).isTrue();
assertThat(listener.mCallCount).isEqualTo(1);
// 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);
assertTrue(listener.mLastEvent.getBelow());
assertEquals(1, listener.mCallCount);
assertThat(listener.mLastEvent.getBelow()).isTrue();
assertThat(listener.mCallCount).isEqualTo(1);
mProximitySensor.unregister(listener);
}
@@ -372,33 +369,66 @@ public class ProximitySensorDualTest extends SysuiTestCase {
TestableListener listener = new TestableListener();
// WE immediately register the secondary sensor.
// We immediately register the secondary sensor.
mProximitySensor.register(listener);
assertFalse(mThresholdSensorPrimary.isPaused());
assertFalse(mThresholdSensorSecondary.isPaused());
assertNull(listener.mLastEvent);
assertEquals(0, listener.mCallCount);
assertThat(mThresholdSensorPrimary.isPaused()).isTrue();
assertThat(mThresholdSensorSecondary.isPaused()).isFalse();
assertThat(listener.mLastEvent).isNull();
assertThat(listener.mCallCount).isEqualTo(0);
mThresholdSensorPrimary.triggerEvent(true, 0);
assertNull(listener.mLastEvent);
assertEquals(0, listener.mCallCount);
assertThat(listener.mLastEvent).isNull();
assertThat(listener.mCallCount).isEqualTo(0);
mThresholdSensorSecondary.triggerEvent(true, 0);
assertTrue(listener.mLastEvent.getBelow());
assertEquals(1, listener.mCallCount);
assertThat(listener.mLastEvent.getBelow()).isTrue();
assertThat(listener.mCallCount).isEqualTo(1);
// The secondary sensor should now remain resumed indefinitely.
assertFalse(mThresholdSensorSecondary.isPaused());
assertThat(mThresholdSensorSecondary.isPaused()).isFalse();
mThresholdSensorSecondary.triggerEvent(false, 1);
assertFalse(listener.mLastEvent.getBelow());
assertEquals(2, listener.mCallCount);
assertThat(listener.mLastEvent.getBelow()).isFalse();
assertThat(listener.mCallCount).isEqualTo(2);
// The secondary is still running, and not polling with the executor.
assertFalse(mThresholdSensorSecondary.isPaused());
assertEquals(0, mFakeExecutor.numPending());
assertThat(mThresholdSensorSecondary.isPaused()).isFalse();
assertThat(mFakeExecutor.numPending()).isEqualTo(0);
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 {
ThresholdSensor.ThresholdSensorEvent mLastEvent;
int mCallCount = 0;