diff options
6 files changed, 59 insertions, 50 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/util/sensors/ProximitySensor.java b/packages/SystemUI/src/com/android/systemui/util/sensors/ProximitySensor.java index 6a648bdf8cd4..19ed2848ca6f 100644 --- a/packages/SystemUI/src/com/android/systemui/util/sensors/ProximitySensor.java +++ b/packages/SystemUI/src/com/android/systemui/util/sensors/ProximitySensor.java @@ -21,8 +21,8 @@ import android.util.Log; import com.android.internal.annotations.VisibleForTesting; import com.android.systemui.dagger.qualifiers.Main; -import com.android.systemui.util.Assert; import com.android.systemui.util.concurrency.DelayableExecutor; +import com.android.systemui.util.concurrency.Execution; import java.util.ArrayList; import java.util.List; @@ -62,6 +62,7 @@ public class ProximitySensor implements ThresholdSensor { private final ThresholdSensor mPrimaryThresholdSensor; private final ThresholdSensor mSecondaryThresholdSensor; private final DelayableExecutor mDelayableExecutor; + private final Execution mExecution; private final List<ThresholdSensor.Listener> mListeners = new ArrayList<>(); private String mTag = null; @VisibleForTesting protected boolean mPaused; @@ -74,14 +75,10 @@ public class ProximitySensor implements ThresholdSensor { private boolean mInitializedListeners = false; private boolean mSecondarySafe = false; - private ThresholdSensor.Listener mPrimaryEventListener = new ThresholdSensor.Listener() { - @Override - public void onThresholdCrossed(ThresholdSensorEvent event) { - onPrimarySensorEvent(event); - } - }; + private final ThresholdSensor.Listener mPrimaryEventListener = this::onPrimarySensorEvent; - private ThresholdSensor.Listener mSecondaryEventListener = new ThresholdSensor.Listener() { + private final ThresholdSensor.Listener mSecondaryEventListener = + new ThresholdSensor.Listener() { @Override public void onThresholdCrossed(ThresholdSensorEvent event) { // If we no longer have a "below" signal and the secondary sensor is not @@ -110,12 +107,15 @@ public class ProximitySensor implements ThresholdSensor { }; @Inject - public ProximitySensor(@PrimaryProxSensor ThresholdSensor primary, + public ProximitySensor( + @PrimaryProxSensor ThresholdSensor primary, @SecondaryProxSensor ThresholdSensor secondary, - @Main DelayableExecutor delayableExecutor) { + @Main DelayableExecutor delayableExecutor, + Execution execution) { mPrimaryThresholdSensor = primary; mSecondaryThresholdSensor = secondary; mDelayableExecutor = delayableExecutor; + mExecution = execution; } @Override @@ -127,7 +127,7 @@ public class ProximitySensor implements ThresholdSensor { @Override public void setDelay(int delay) { - Assert.isMainThread(); + mExecution.assertIsMainThread(); mPrimaryThresholdSensor.setDelay(delay); mSecondaryThresholdSensor.setDelay(delay); } @@ -137,7 +137,7 @@ public class ProximitySensor implements ThresholdSensor { */ @Override public void pause() { - Assert.isMainThread(); + mExecution.assertIsMainThread(); mPaused = true; unregisterInternal(); } @@ -147,18 +147,23 @@ public class ProximitySensor implements ThresholdSensor { */ @Override public void resume() { - Assert.isMainThread(); + mExecution.assertIsMainThread(); mPaused = false; registerInternal(); } /** * Sets that it is safe to leave the secondary sensor on indefinitely. + * + * The secondary sensor will be turned on if there are any registered listeners, regardless + * of what is reported by the primary sensor. */ public void setSecondarySafe(boolean safe) { mSecondarySafe = safe; if (!mSecondarySafe) { mSecondaryThresholdSensor.pause(); + } else { + mSecondaryThresholdSensor.resume(); } } @@ -185,7 +190,7 @@ public class ProximitySensor implements ThresholdSensor { */ @Override public void register(ThresholdSensor.Listener listener) { - Assert.isMainThread(); + mExecution.assertIsMainThread(); if (!isLoaded()) { return; } @@ -199,13 +204,15 @@ public class ProximitySensor implements ThresholdSensor { } protected void registerInternal() { - Assert.isMainThread(); + mExecution.assertIsMainThread(); if (mRegistered || mPaused || mListeners.isEmpty()) { return; } if (!mInitializedListeners) { mPrimaryThresholdSensor.register(mPrimaryEventListener); - mSecondaryThresholdSensor.pause(); + if (!mSecondarySafe) { + mSecondaryThresholdSensor.pause(); + } mSecondaryThresholdSensor.register(mSecondaryEventListener); mInitializedListeners = true; } @@ -222,7 +229,7 @@ public class ProximitySensor implements ThresholdSensor { */ @Override public void unregister(ThresholdSensor.Listener listener) { - Assert.isMainThread(); + mExecution.assertIsMainThread(); mListeners.remove(listener); if (mListeners.size() == 0) { unregisterInternal(); @@ -230,7 +237,7 @@ public class ProximitySensor implements ThresholdSensor { } protected void unregisterInternal() { - Assert.isMainThread(); + mExecution.assertIsMainThread(); if (!mRegistered) { return; } @@ -252,7 +259,7 @@ public class ProximitySensor implements ThresholdSensor { /** Update all listeners with the last value this class received from the sensor. */ public void alertListeners() { - Assert.isMainThread(); + mExecution.assertIsMainThread(); if (mAlerting.getAndSet(true)) { return; } @@ -267,7 +274,7 @@ public class ProximitySensor implements ThresholdSensor { } private void onPrimarySensorEvent(ThresholdSensorEvent event) { - Assert.isMainThread(); + mExecution.assertIsMainThread(); if (mLastPrimaryEvent != null && event.getBelow() == mLastPrimaryEvent.getBelow()) { return; } @@ -290,7 +297,7 @@ public class ProximitySensor implements ThresholdSensor { } private void onSensorEvent(ThresholdSensorEvent event) { - Assert.isMainThread(); + mExecution.assertIsMainThread(); if (mLastEvent != null && event.getBelow() == mLastEvent.getBelow()) { return; } @@ -306,9 +313,9 @@ public class ProximitySensor implements ThresholdSensor { @Override public String toString() { return String.format("{registered=%s, paused=%s, near=%s, primarySensor=%s, " - + "secondarySensor=%s}", + + "secondarySensor=%s secondarySafe=%s}", isRegistered(), mPaused, isNear(), mPrimaryThresholdSensor, - mSecondaryThresholdSensor); + mSecondaryThresholdSensor, mSecondarySafe); } /** 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 71b255229c8f..31c307297066 100644 --- a/packages/SystemUI/src/com/android/systemui/util/sensors/ThresholdSensorImpl.java +++ b/packages/SystemUI/src/com/android/systemui/util/sensors/ThresholdSensorImpl.java @@ -25,7 +25,7 @@ import android.util.Log; import com.android.internal.annotations.VisibleForTesting; import com.android.systemui.dagger.qualifiers.Main; -import com.android.systemui.util.Assert; +import com.android.systemui.util.concurrency.Execution; import java.util.ArrayList; import java.util.List; @@ -37,6 +37,7 @@ class ThresholdSensorImpl implements ThresholdSensor { private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); private final AsyncSensorManager mSensorManager; + private final Execution mExecution; private final Sensor mSensor; private final float mThreshold; private boolean mRegistered; @@ -61,9 +62,10 @@ class ThresholdSensorImpl implements ThresholdSensor { } }; - private ThresholdSensorImpl(AsyncSensorManager sensorManager, - Sensor sensor, float threshold, float thresholdLatch, int sensorDelay) { + private ThresholdSensorImpl(AsyncSensorManager sensorManager, Sensor sensor, + Execution execution, float threshold, float thresholdLatch, int sensorDelay) { mSensorManager = sensorManager; + mExecution = execution; mSensor = sensor; mThreshold = threshold; mThresholdLatch = thresholdLatch; @@ -107,7 +109,7 @@ class ThresholdSensorImpl implements ThresholdSensor { */ @Override public void register(Listener listener) { - Assert.isMainThread(); + mExecution.assertIsMainThread(); if (!mListeners.contains(listener)) { mListeners.add(listener); } @@ -116,7 +118,7 @@ class ThresholdSensorImpl implements ThresholdSensor { @Override public void unregister(Listener listener) { - Assert.isMainThread(); + mExecution.assertIsMainThread(); mListeners.remove(listener); unregisterInternal(); } @@ -126,7 +128,7 @@ class ThresholdSensorImpl implements ThresholdSensor { */ @Override public void pause() { - Assert.isMainThread(); + mExecution.assertIsMainThread(); mPaused = true; unregisterInternal(); } @@ -136,7 +138,7 @@ class ThresholdSensorImpl implements ThresholdSensor { */ @Override public void resume() { - Assert.isMainThread(); + mExecution.assertIsMainThread(); mPaused = false; registerInternal(); } @@ -148,7 +150,7 @@ class ThresholdSensorImpl implements ThresholdSensor { } private void registerInternal() { - Assert.isMainThread(); + mExecution.assertIsMainThread(); if (mRegistered || mPaused || mListeners.isEmpty()) { return; } @@ -158,7 +160,7 @@ class ThresholdSensorImpl implements ThresholdSensor { } private void unregisterInternal() { - Assert.isMainThread(); + mExecution.assertIsMainThread(); if (!mRegistered) { return; } @@ -177,7 +179,7 @@ class ThresholdSensorImpl implements ThresholdSensor { * still appears entirely binary. */ private void onSensorEvent(boolean belowThreshold, boolean aboveThreshold, long timestampNs) { - Assert.isMainThread(); + mExecution.assertIsMainThread(); if (!mRegistered) { return; } @@ -212,6 +214,7 @@ class ThresholdSensorImpl implements ThresholdSensor { static class Builder { private final Resources mResources; private final AsyncSensorManager mSensorManager; + private final Execution mExecution; private int mSensorDelay = SensorManager.SENSOR_DELAY_NORMAL;; private float mThresholdValue; private float mThresholdLatchValue; @@ -221,9 +224,10 @@ class ThresholdSensorImpl implements ThresholdSensor { private boolean mThresholdLatchValueSet; @Inject - Builder(@Main Resources resources, AsyncSensorManager sensorManager) { + Builder(@Main Resources resources, AsyncSensorManager sensorManager, Execution execution) { mResources = resources; mSensorManager = sensorManager; + mExecution = execution; } @@ -302,7 +306,8 @@ class ThresholdSensorImpl implements ThresholdSensor { } return new ThresholdSensorImpl( - mSensorManager, mSensor, mThresholdValue, mThresholdLatchValue, mSensorDelay); + mSensorManager, mSensor, mExecution, + mThresholdValue, mThresholdLatchValue, mSensorDelay); } private Sensor findSensorByType(String sensorType) { diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/sensors/FakeProximitySensor.java b/packages/SystemUI/tests/src/com/android/systemui/util/sensors/FakeProximitySensor.java index 9bb4c4b08481..50947ab0ee86 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/util/sensors/FakeProximitySensor.java +++ b/packages/SystemUI/tests/src/com/android/systemui/util/sensors/FakeProximitySensor.java @@ -17,6 +17,7 @@ package com.android.systemui.util.sensors; import com.android.systemui.util.concurrency.DelayableExecutor; +import com.android.systemui.util.concurrency.FakeExecution; public class FakeProximitySensor extends ProximitySensor { private boolean mAvailable; @@ -25,7 +26,7 @@ public class FakeProximitySensor extends ProximitySensor { public FakeProximitySensor(ThresholdSensor primary, ThresholdSensor secondary, DelayableExecutor delayableExecutor) { super(primary, secondary == null ? new FakeThresholdSensor() : secondary, - delayableExecutor); + delayableExecutor, new FakeExecution()); mAvailable = true; } diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ProximitySensorDualTest.java b/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ProximitySensorDualTest.java index bae1d98aa310..8f0754592b83 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ProximitySensorDualTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ProximitySensorDualTest.java @@ -27,6 +27,7 @@ import android.testing.TestableLooper; import androidx.test.filters.SmallTest; import com.android.systemui.SysuiTestCase; +import com.android.systemui.util.concurrency.FakeExecution; import com.android.systemui.util.concurrency.FakeExecutor; import com.android.systemui.util.time.FakeSystemClock; @@ -54,7 +55,8 @@ public class ProximitySensorDualTest extends SysuiTestCase { mThresholdSensorSecondary.setLoaded(true); mProximitySensor = new ProximitySensor( - mThresholdSensorPrimary, mThresholdSensorSecondary, mFakeExecutor); + mThresholdSensorPrimary, mThresholdSensorSecondary, mFakeExecutor, + new FakeExecution()); } @Test @@ -324,9 +326,10 @@ public class ProximitySensorDualTest extends SysuiTestCase { TestableListener listener = new TestableListener(); + // WE immediately register the secondary sensor. mProximitySensor.register(listener); assertFalse(mThresholdSensorPrimary.isPaused()); - assertTrue(mThresholdSensorSecondary.isPaused()); + assertFalse(mThresholdSensorSecondary.isPaused()); assertNull(listener.mLastEvent); assertEquals(0, listener.mCallCount); diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ProximitySensorSingleTest.java b/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ProximitySensorSingleTest.java index f2d5284d4009..6c6d355d7866 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ProximitySensorSingleTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/util/sensors/ProximitySensorSingleTest.java @@ -27,6 +27,7 @@ import android.testing.TestableLooper; import androidx.test.filters.SmallTest; import com.android.systemui.SysuiTestCase; +import com.android.systemui.util.concurrency.FakeExecution; import com.android.systemui.util.concurrency.FakeExecutor; import com.android.systemui.util.time.FakeSystemClock; @@ -54,7 +55,7 @@ public class ProximitySensorSingleTest extends SysuiTestCase { mThresholdSensor.setLoaded(true); mProximitySensor = new ProximitySensor( - mThresholdSensor, new FakeThresholdSensor(), mFakeExecutor); + mThresholdSensor, new FakeThresholdSensor(), mFakeExecutor, new FakeExecution()); } @Test 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 d3a35a735f6d..12765679a7f3 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 @@ -24,7 +24,7 @@ import android.test.suitebuilder.annotation.SmallTest; import android.testing.AndroidTestingRunner; import com.android.systemui.SysuiTestCase; -import com.android.systemui.util.Assert; +import com.android.systemui.util.concurrency.FakeExecution; import com.android.systemui.util.concurrency.FakeExecutor; import com.android.systemui.util.concurrency.FakeThreadFactory; import com.android.systemui.util.time.FakeSystemClock; @@ -52,7 +52,7 @@ public class ThresholdSensorImplTest extends SysuiTestCase { mFakeProximitySensor = mSensorManager.getFakeProximitySensor(); ThresholdSensorImpl.Builder thresholdSensorBuilder = new ThresholdSensorImpl.Builder( - null, mAsyncSensorManager); + null, mAsyncSensorManager, new FakeExecution()); mThresholdSensor = (ThresholdSensorImpl) thresholdSensorBuilder .setSensor(mFakeProximitySensor.getSensor()) .setThresholdValue(mFakeProximitySensor.getSensor().getMaximumRange()) @@ -61,7 +61,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase { @Test public void testSingleListener() { - Assert.setTestThread(Thread.currentThread()); TestableListener listener = new TestableListener(); assertFalse(mThresholdSensor.isRegistered()); @@ -83,7 +82,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase { @Test public void testMultiListener() { - Assert.setTestThread(Thread.currentThread()); TestableListener listenerA = new TestableListener(); TestableListener listenerB = new TestableListener(); @@ -117,7 +115,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase { @Test public void testDuplicateListener() { - Assert.setTestThread(Thread.currentThread()); TestableListener listenerA = new TestableListener(); assertFalse(mThresholdSensor.isRegistered()); @@ -142,7 +139,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase { } @Test public void testUnregister() { - Assert.setTestThread(Thread.currentThread()); TestableListener listener = new TestableListener(); assertFalse(mThresholdSensor.isRegistered()); @@ -162,7 +158,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase { @Test public void testPauseAndResume() { - Assert.setTestThread(Thread.currentThread()); TestableListener listener = new TestableListener(); assertFalse(mThresholdSensor.isRegistered()); @@ -205,7 +200,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase { @Test public void testAlertListeners() { - Assert.setTestThread(Thread.currentThread()); TestableListener listenerA = new TestableListener(); TestableListener listenerB = new TestableListener(); @@ -237,12 +231,11 @@ public class ThresholdSensorImplTest extends SysuiTestCase { @Test public void testHysteresis() { - Assert.setTestThread(Thread.currentThread()); float lowValue = 10f; float highValue = 100f; FakeSensorManager.FakeGenericSensor sensor = mSensorManager.getFakeLightSensor(); ThresholdSensorImpl.Builder thresholdSensorBuilder = new ThresholdSensorImpl.Builder( - null, mAsyncSensorManager); + null, mAsyncSensorManager, new FakeExecution()); ThresholdSensorImpl thresholdSensor = (ThresholdSensorImpl) thresholdSensorBuilder .setSensor(sensor.getSensor()) .setThresholdValue(lowValue) @@ -286,7 +279,6 @@ public class ThresholdSensorImplTest extends SysuiTestCase { @Test public void testAlertAfterPause() { - Assert.setTestThread(Thread.currentThread()); TestableListener listener = new TestableListener(); mThresholdSensor.register(listener); |