Prevent paused ThresholdSensorImpl event delivery.

With this change, a paused ThresholdSensorImpl will no longer deliver
events. This can otherwise happen if a ThresholdSensorImpl attempts
to unregister itself, but an event comes before the de-registration
actually occurs.

This can occur because ThresholdSensorImpl relies on an
AsyncSensorManager.

Fixes: 155135632
Test: atest SystemUITests
Change-Id: I26f743016cba2383e853436ac41463a7677692a9
This commit is contained in:
Dave Mankoff
2020-04-28 15:08:36 -04:00
parent 5f991c6184
commit 8c5cce6328
2 changed files with 18 additions and 1 deletions

View File

@@ -167,7 +167,7 @@ class ThresholdSensorImpl implements ThresholdSensor {
private void onSensorEvent(boolean below, long timestampNs) {
Assert.isMainThread();
if (mLastBelow != null && mLastBelow == below) {
if (!mRegistered || mLastBelow != null && mLastBelow == below) {
return;
}
mLastBelow = below;

View File

@@ -226,6 +226,23 @@ public class ThresholdSensorImplTest extends SysuiTestCase {
waitForSensorManager();
}
@Test
public void testAlertAfterPause() {
TestableListener listener = new TestableListener();
mThresholdSensor.register(listener);
waitForSensorManager();
mFakeProximitySensor.sendProximityResult(false);
assertTrue(listener.mBelow);
assertEquals(1, listener.mCallCount);
mThresholdSensor.pause();
mFakeProximitySensor.sendProximityResult(false);
assertTrue(listener.mBelow);
assertEquals(1, listener.mCallCount);
}
static class TestableListener implements ThresholdSensor.Listener {
boolean mBelow;
long mTimestampNs;