From 8c5cce6328177e72d21143ef5d96d7e7d1311c2a Mon Sep 17 00:00:00 2001 From: Dave Mankoff Date: Tue, 28 Apr 2020 15:08:36 -0400 Subject: [PATCH] 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 --- .../util/sensors/ThresholdSensorImpl.java | 2 +- .../util/sensors/ThresholdSensorImplTest.java | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/SystemUI/src/com/android/systemui/util/sensors/ThresholdSensorImpl.java b/packages/SystemUI/src/com/android/systemui/util/sensors/ThresholdSensorImpl.java index 546333b3a073b..333e8da2a84f3 100644 --- a/packages/SystemUI/src/com/android/systemui/util/sensors/ThresholdSensorImpl.java +++ b/packages/SystemUI/src/com/android/systemui/util/sensors/ThresholdSensorImpl.java @@ -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; diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ThresholdSensorImplTest.java b/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ThresholdSensorImplTest.java index 09ec8e55efd62..70b92dc014472 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ThresholdSensorImplTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ThresholdSensorImplTest.java @@ -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;