Remove SensorRateLimiter, as it should be unnecessary.
Bug: 141122220 Test: atest SystemUITests Change-Id: I52f9293fc190b58e720a87e3ef0ab2a234b62df1
This commit is contained in:
@@ -48,7 +48,6 @@ import com.android.systemui.shared.plugins.PluginManager;
|
||||
import com.android.systemui.statusbar.phone.DozeParameters;
|
||||
import com.android.systemui.util.sensors.AsyncSensorManager;
|
||||
import com.android.systemui.util.sensors.ProximitySensor;
|
||||
import com.android.systemui.util.sensors.SensorRateLimiter;
|
||||
import com.android.systemui.util.wakelock.WakeLock;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
@@ -150,8 +149,6 @@ public class DozeSensors {
|
||||
|
||||
mProximitySensor = new ProximitySensor(
|
||||
context, sensorManager, Dependency.get(PluginManager.class));
|
||||
new SensorRateLimiter(mProximitySensor, mAlarmManager, policy.proxCooldownTriggerMs,
|
||||
policy.proxCooldownPeriodMs, "prox_sensor");
|
||||
|
||||
mProximitySensor.register(
|
||||
proximityEvent -> mProxCallback.accept(!proximityEvent.getNear()));
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.systemui.util.sensors;
|
||||
|
||||
/**
|
||||
* Represents a sensor that can be throttled by a {@link SensorRateLimiter}.
|
||||
*/
|
||||
public interface LimitableSensor {
|
||||
/**
|
||||
* Allows a sensor to be temporarily throttled or disabled.
|
||||
*/
|
||||
void setRateLimited(boolean limited);
|
||||
|
||||
/**
|
||||
* Registers a {@link SensorRateLimiter} with the sensor.
|
||||
*/
|
||||
void setRateLimiter(SensorRateLimiter rateLimiter);
|
||||
}
|
||||
@@ -37,7 +37,7 @@ import javax.inject.Inject;
|
||||
/**
|
||||
* Simple wrapper around SensorManager customized for the Proximity sensor.
|
||||
*/
|
||||
public class ProximitySensor implements LimitableSensor {
|
||||
public class ProximitySensor {
|
||||
private static final String TAG = "ProxSensor";
|
||||
private static final boolean DEBUG = false;
|
||||
|
||||
@@ -51,8 +51,6 @@ public class ProximitySensor implements LimitableSensor {
|
||||
private int mSensorDelay = SensorManager.SENSOR_DELAY_NORMAL;
|
||||
private boolean mPaused;
|
||||
private boolean mRegistered;
|
||||
private SensorRateLimiter mRateLimiter;
|
||||
private boolean mRateLimited = false;
|
||||
|
||||
private SensorEventListener mSensorEventListener = new SensorEventListener() {
|
||||
@Override
|
||||
@@ -109,14 +107,6 @@ public class ProximitySensor implements LimitableSensor {
|
||||
registerInternal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Supply a {@link SensorRateLimiter} to help limit power usage.
|
||||
*/
|
||||
@Override
|
||||
public void setRateLimiter(SensorRateLimiter rateLimiter) {
|
||||
mRateLimiter = rateLimiter;
|
||||
}
|
||||
|
||||
private Sensor findBrightnessSensor(Context context) {
|
||||
String sensorType = context.getString(R.string.doze_brightness_sensor_type);
|
||||
List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
|
||||
@@ -131,19 +121,6 @@ public class ProximitySensor implements LimitableSensor {
|
||||
return sensor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers/unregisters the proximity sensor with the SensorManager.
|
||||
*/
|
||||
@Override
|
||||
public void setRateLimited(boolean limited) {
|
||||
mRateLimited = limited;
|
||||
if (mRateLimited) {
|
||||
unregisterInternal();
|
||||
} else {
|
||||
registerInternal();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if we are registered with the SensorManager.
|
||||
*/
|
||||
@@ -177,7 +154,7 @@ public class ProximitySensor implements LimitableSensor {
|
||||
}
|
||||
|
||||
private void registerInternal() {
|
||||
if (mRegistered || mRateLimited || mPaused || mListeners.isEmpty()) {
|
||||
if (mRegistered || mPaused || mListeners.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
logDebug("Registering sensor listener");
|
||||
@@ -224,15 +201,12 @@ public class ProximitySensor implements LimitableSensor {
|
||||
}
|
||||
mLastEvent = new ProximityEvent(near, event.timestamp);
|
||||
alertListeners();
|
||||
if (mRateLimiter != null) {
|
||||
mRateLimiter.onSensorEvent(mLastEvent.getTimestampMs());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("{registered=%s, paused=%s, near=%s, rateLimiter=%s, sensor=%s}",
|
||||
isRegistered(), mPaused, isNear(), mRateLimiter, mSensor);
|
||||
return String.format("{registered=%s, paused=%s, near=%s, sensor=%s}",
|
||||
isRegistered(), mPaused, isNear(), mSensor);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.systemui.util.sensors;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.os.Handler;
|
||||
|
||||
import com.android.systemui.util.AlarmTimeout;
|
||||
|
||||
/**
|
||||
* Monitors a sensor for over-activity and can throttle it down as needed.
|
||||
*
|
||||
* Currently, this class is designed with the {@link ProximitySensor} in mind.
|
||||
*/
|
||||
public class SensorRateLimiter {
|
||||
private final LimitableSensor mSensor;
|
||||
private final AlarmTimeout mCooldownTimer;
|
||||
private final long mCoolDownTriggerMs;
|
||||
private final long mCoolDownPeriodMs;
|
||||
private long mLastTimestampMs;
|
||||
|
||||
public SensorRateLimiter(
|
||||
LimitableSensor sensor,
|
||||
AlarmManager alarmManager,
|
||||
long coolDownTriggerMs,
|
||||
long coolDownPeriodMs,
|
||||
String alarmTag) {
|
||||
|
||||
mSensor = sensor;
|
||||
mCoolDownTriggerMs = coolDownTriggerMs;
|
||||
mCoolDownPeriodMs = coolDownPeriodMs;
|
||||
mLastTimestampMs = -1;
|
||||
|
||||
Handler handler = new Handler();
|
||||
mCooldownTimer = new AlarmTimeout(alarmManager, this::coolDownComplete, alarmTag, handler);
|
||||
|
||||
mSensor.setRateLimiter(this);
|
||||
}
|
||||
|
||||
|
||||
void onSensorEvent(long timestampMs) {
|
||||
if (mLastTimestampMs >= 0 && mCoolDownTriggerMs > 0 && mCoolDownPeriodMs > 0
|
||||
&& timestampMs - mLastTimestampMs < mCoolDownTriggerMs) {
|
||||
scheduleCoolDown();
|
||||
}
|
||||
|
||||
mLastTimestampMs = timestampMs;
|
||||
}
|
||||
|
||||
private void scheduleCoolDown() {
|
||||
mSensor.setRateLimited(true);
|
||||
mCooldownTimer.schedule(mCoolDownPeriodMs, AlarmTimeout.MODE_IGNORE_IF_SCHEDULED);
|
||||
}
|
||||
|
||||
private void coolDownComplete() {
|
||||
mSensor.setRateLimited(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("{coolingDown=%s}", mCooldownTimer.isScheduled());
|
||||
}
|
||||
}
|
||||
@@ -164,49 +164,6 @@ public class ProximitySensorTest extends SysuiTestCase {
|
||||
assertFalse(mProximitySensor.isRegistered());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRateLimit() {
|
||||
TestableListener listener = new TestableListener();
|
||||
|
||||
assertFalse(mProximitySensor.isRegistered());
|
||||
mProximitySensor.register(listener);
|
||||
waitForSensorManager();
|
||||
assertTrue(mProximitySensor.isRegistered());
|
||||
assertNull(listener.mLastEvent);
|
||||
|
||||
mFakeProximitySensor.sendProximityResult(true);
|
||||
assertFalse(listener.mLastEvent.getNear());
|
||||
assertEquals(listener.mCallCount, 1);
|
||||
|
||||
mProximitySensor.setRateLimited(true);
|
||||
waitForSensorManager();
|
||||
assertFalse(mProximitySensor.isRegistered());
|
||||
|
||||
// More events do nothing when rate limited.
|
||||
mFakeProximitySensor.sendProximityResult(true);
|
||||
assertFalse(listener.mLastEvent.getNear());
|
||||
assertEquals(listener.mCallCount, 1);
|
||||
mFakeProximitySensor.sendProximityResult(false);
|
||||
assertFalse(listener.mLastEvent.getNear());
|
||||
assertEquals(listener.mCallCount, 1);
|
||||
|
||||
mProximitySensor.setRateLimited(false);
|
||||
waitForSensorManager();
|
||||
assertTrue(mProximitySensor.isRegistered());
|
||||
// Still matches our previous call
|
||||
assertFalse(listener.mLastEvent.getNear());
|
||||
assertEquals(listener.mCallCount, 1);
|
||||
|
||||
mFakeProximitySensor.sendProximityResult(true);
|
||||
assertFalse(listener.mLastEvent.getNear());
|
||||
assertEquals(listener.mCallCount, 2);
|
||||
|
||||
mProximitySensor.unregister(listener);
|
||||
waitForSensorManager();
|
||||
assertFalse(mProximitySensor.isRegistered());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlertListeners() {
|
||||
TestableListener listenerA = new TestableListener();
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.systemui.util.sensors;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.os.Handler;
|
||||
import android.testing.AndroidTestingRunner;
|
||||
import android.testing.TestableLooper;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
|
||||
import com.android.systemui.SysuiTestCase;
|
||||
import com.android.systemui.util.AlarmTimeout;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
@SmallTest
|
||||
@RunWith(AndroidTestingRunner.class)
|
||||
@TestableLooper.RunWithLooper
|
||||
public class RateLimiterTest extends SysuiTestCase {
|
||||
|
||||
private static final long COOL_DOWN_TRIGGER_MS = 100;
|
||||
private static final long COOL_DOWN_PERIOD_MS = 200;
|
||||
private static final String ALARM_TAG = "rate_limiter_test";
|
||||
|
||||
@Mock
|
||||
LimitableSensor mSensor;
|
||||
@Mock
|
||||
private AlarmManager mAlarmManager;
|
||||
|
||||
private SensorRateLimiter mSensorRateLimiter;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mSensorRateLimiter = new SensorRateLimiter(mSensor, mAlarmManager, COOL_DOWN_TRIGGER_MS,
|
||||
COOL_DOWN_PERIOD_MS, ALARM_TAG);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInfrequentEvents() {
|
||||
mSensorRateLimiter.onSensorEvent(0);
|
||||
mSensorRateLimiter.onSensorEvent(COOL_DOWN_TRIGGER_MS);
|
||||
mSensorRateLimiter.onSensorEvent(COOL_DOWN_TRIGGER_MS * 2);
|
||||
mSensorRateLimiter.onSensorEvent(COOL_DOWN_TRIGGER_MS * 4);
|
||||
mSensorRateLimiter.onSensorEvent(COOL_DOWN_TRIGGER_MS * 8);
|
||||
|
||||
verify(mSensor, never()).setRateLimited(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRateLimit() {
|
||||
mSensorRateLimiter.onSensorEvent(0);
|
||||
mSensorRateLimiter.onSensorEvent(COOL_DOWN_TRIGGER_MS - 1);
|
||||
|
||||
verify(mSensor).setRateLimited(true);
|
||||
verify(mAlarmManager).setExact(
|
||||
anyInt(), anyLong(), eq(ALARM_TAG), any(AlarmTimeout.class), any(Handler.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSlowToOverTrigger() {
|
||||
mSensorRateLimiter.onSensorEvent(0);
|
||||
mSensorRateLimiter.onSensorEvent(COOL_DOWN_TRIGGER_MS);
|
||||
mSensorRateLimiter.onSensorEvent(COOL_DOWN_TRIGGER_MS * 2);
|
||||
mSensorRateLimiter.onSensorEvent(COOL_DOWN_TRIGGER_MS * 4);
|
||||
|
||||
verify(mSensor, never()).setRateLimited(true);
|
||||
|
||||
mSensorRateLimiter.onSensorEvent(COOL_DOWN_TRIGGER_MS * 5 - 1);
|
||||
|
||||
verify(mSensor).setRateLimited(true);
|
||||
verify(mAlarmManager).setExact(
|
||||
anyInt(), anyLong(), eq(ALARM_TAG), any(AlarmTimeout.class), any(Handler.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCoolDownComplete() {
|
||||
mSensorRateLimiter.onSensorEvent(0);
|
||||
mSensorRateLimiter.onSensorEvent(COOL_DOWN_TRIGGER_MS - 1);
|
||||
|
||||
ArgumentCaptor<AlarmManager.OnAlarmListener> listenerArgumentCaptor =
|
||||
ArgumentCaptor.forClass(AlarmManager.OnAlarmListener.class);
|
||||
|
||||
verify(mSensor).setRateLimited(true);
|
||||
verify(mAlarmManager).setExact(
|
||||
anyInt(), anyLong(), eq(ALARM_TAG), listenerArgumentCaptor.capture(),
|
||||
any(Handler.class));
|
||||
|
||||
listenerArgumentCaptor.getValue().onAlarm();
|
||||
verify(mSensor).setRateLimited(false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user