Merge "Merge "More defensive checks for ProximitySensor" into rvc-d1-dev am: 8ce8c004a4" into rvc-d1-dev-plus-aosp
This commit is contained in:
committed by
Android (Google) Code Review
commit
ac7516e1dc
@@ -86,9 +86,12 @@ public class ProximitySensor implements ThresholdSensor {
|
|||||||
public void onThresholdCrossed(ThresholdSensorEvent event) {
|
public void onThresholdCrossed(ThresholdSensorEvent event) {
|
||||||
// If we no longer have a "below" signal and the secondary sensor is not
|
// If we no longer have a "below" signal and the secondary sensor is not
|
||||||
// considered "safe", then we need to turn it off.
|
// considered "safe", then we need to turn it off.
|
||||||
if (!mSecondarySafe && (!mLastPrimaryEvent.getBelow() || !event.getBelow())) {
|
if (!mSecondarySafe
|
||||||
|
&& (mLastPrimaryEvent == null
|
||||||
|
|| !mLastPrimaryEvent.getBelow()
|
||||||
|
|| !event.getBelow())) {
|
||||||
mSecondaryThresholdSensor.pause();
|
mSecondaryThresholdSensor.pause();
|
||||||
if (!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;
|
mCancelSecondaryRunnable = null;
|
||||||
return;
|
return;
|
||||||
@@ -100,8 +103,10 @@ public class ProximitySensor implements ThresholdSensor {
|
|||||||
}
|
}
|
||||||
logDebug("Secondary sensor event: " + event.getBelow() + ".");
|
logDebug("Secondary sensor event: " + event.getBelow() + ".");
|
||||||
|
|
||||||
|
if (!mPaused) {
|
||||||
onSensorEvent(event);
|
onSensorEvent(event);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@@ -252,9 +257,10 @@ public class ProximitySensor implements ThresholdSensor {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (mLastEvent != null) {
|
if (mLastEvent != null) {
|
||||||
|
ThresholdSensorEvent lastEvent = mLastEvent; // Listeners can null out mLastEvent.
|
||||||
List<ThresholdSensor.Listener> listeners = new ArrayList<>(mListeners);
|
List<ThresholdSensor.Listener> listeners = new ArrayList<>(mListeners);
|
||||||
listeners.forEach(proximitySensorListener ->
|
listeners.forEach(proximitySensorListener ->
|
||||||
proximitySensorListener.onThresholdCrossed(mLastEvent));
|
proximitySensorListener.onThresholdCrossed(lastEvent));
|
||||||
}
|
}
|
||||||
|
|
||||||
mAlerting.set(false);
|
mAlerting.set(false);
|
||||||
|
|||||||
@@ -152,6 +152,32 @@ public class ProximitySensorDualTest extends SysuiTestCase {
|
|||||||
assertFalse(mProximitySensor.isRegistered());
|
assertFalse(mProximitySensor.isRegistered());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUnregisterDuringCallback() {
|
||||||
|
ThresholdSensor.Listener listenerA = event -> mProximitySensor.pause();
|
||||||
|
TestableListener listenerB = new TestableListener();
|
||||||
|
|
||||||
|
assertFalse(mProximitySensor.isRegistered());
|
||||||
|
mProximitySensor.register(listenerA);
|
||||||
|
mProximitySensor.register(listenerB);
|
||||||
|
assertTrue(mProximitySensor.isRegistered());
|
||||||
|
assertFalse(mThresholdSensorPrimary.isPaused());
|
||||||
|
assertTrue(mThresholdSensorSecondary.isPaused());
|
||||||
|
assertNull(listenerB.mLastEvent);
|
||||||
|
|
||||||
|
// listenerA will pause the proximity sensor, unregistering it.
|
||||||
|
mThresholdSensorPrimary.triggerEvent(true, 0);
|
||||||
|
mThresholdSensorSecondary.triggerEvent(true, 0);
|
||||||
|
assertTrue(listenerB.mLastEvent.getBelow());
|
||||||
|
assertEquals(1, listenerB.mCallCount);
|
||||||
|
|
||||||
|
|
||||||
|
// A second call to trigger it should be ignored.
|
||||||
|
mThresholdSensorSecondary.triggerEvent(false, 0);
|
||||||
|
assertTrue(listenerB.mLastEvent.getBelow());
|
||||||
|
assertEquals(1, listenerB.mCallCount);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPauseAndResume() {
|
public void testPauseAndResume() {
|
||||||
TestableListener listener = new TestableListener();
|
TestableListener listener = new TestableListener();
|
||||||
|
|||||||
Reference in New Issue
Block a user