Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bc3091c0 authored by Dave Mankoff's avatar Dave Mankoff
Browse files

Remove SensorRateLimiter, as it should be unnecessary.

Bug: 141122220
Test: atest SystemUITests
Change-Id: I52f9293fc190b58e720a87e3ef0ab2a234b62df1
parent 63a1282b
Loading
Loading
Loading
Loading
+0 −3
Original line number Original line Diff line number Diff line
@@ -48,7 +48,6 @@ import com.android.systemui.shared.plugins.PluginManager;
import com.android.systemui.statusbar.phone.DozeParameters;
import com.android.systemui.statusbar.phone.DozeParameters;
import com.android.systemui.util.sensors.AsyncSensorManager;
import com.android.systemui.util.sensors.AsyncSensorManager;
import com.android.systemui.util.sensors.ProximitySensor;
import com.android.systemui.util.sensors.ProximitySensor;
import com.android.systemui.util.sensors.SensorRateLimiter;
import com.android.systemui.util.wakelock.WakeLock;
import com.android.systemui.util.wakelock.WakeLock;


import java.io.PrintWriter;
import java.io.PrintWriter;
@@ -150,8 +149,6 @@ public class DozeSensors {


        mProximitySensor = new ProximitySensor(
        mProximitySensor = new ProximitySensor(
                context, sensorManager, Dependency.get(PluginManager.class));
                context, sensorManager, Dependency.get(PluginManager.class));
        new SensorRateLimiter(mProximitySensor, mAlarmManager, policy.proxCooldownTriggerMs,
                policy.proxCooldownPeriodMs, "prox_sensor");


        mProximitySensor.register(
        mProximitySensor.register(
                proximityEvent -> mProxCallback.accept(!proximityEvent.getNear()));
                proximityEvent -> mProxCallback.accept(!proximityEvent.getNear()));
+0 −32
Original line number Original line Diff line number Diff line
/*
 * 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);
}
+4 −30
Original line number Original line Diff line number Diff line
@@ -37,7 +37,7 @@ import javax.inject.Inject;
/**
/**
 * Simple wrapper around SensorManager customized for the Proximity sensor.
 * 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 String TAG = "ProxSensor";
    private static final boolean DEBUG = false;
    private static final boolean DEBUG = false;


@@ -51,8 +51,6 @@ public class ProximitySensor implements LimitableSensor {
    private int mSensorDelay = SensorManager.SENSOR_DELAY_NORMAL;
    private int mSensorDelay = SensorManager.SENSOR_DELAY_NORMAL;
    private boolean mPaused;
    private boolean mPaused;
    private boolean mRegistered;
    private boolean mRegistered;
    private SensorRateLimiter mRateLimiter;
    private boolean mRateLimited = false;


    private SensorEventListener mSensorEventListener = new SensorEventListener() {
    private SensorEventListener mSensorEventListener = new SensorEventListener() {
        @Override
        @Override
@@ -109,14 +107,6 @@ public class ProximitySensor implements LimitableSensor {
        registerInternal();
        registerInternal();
    }
    }


    /**
     * Supply a {@link SensorRateLimiter} to help limit power usage.
     */
    @Override
    public void setRateLimiter(SensorRateLimiter rateLimiter) {
        mRateLimiter = rateLimiter;
    }

    private Sensor findBrightnessSensor(Context context) {
    private Sensor findBrightnessSensor(Context context) {
        String sensorType = context.getString(R.string.doze_brightness_sensor_type);
        String sensorType = context.getString(R.string.doze_brightness_sensor_type);
        List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
        List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
@@ -131,19 +121,6 @@ public class ProximitySensor implements LimitableSensor {
        return sensor;
        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.
     * Returns true if we are registered with the SensorManager.
     */
     */
@@ -177,7 +154,7 @@ public class ProximitySensor implements LimitableSensor {
    }
    }


    private void registerInternal() {
    private void registerInternal() {
        if (mRegistered || mRateLimited || mPaused || mListeners.isEmpty()) {
        if (mRegistered || mPaused || mListeners.isEmpty()) {
            return;
            return;
        }
        }
        logDebug("Registering sensor listener");
        logDebug("Registering sensor listener");
@@ -224,15 +201,12 @@ public class ProximitySensor implements LimitableSensor {
        }
        }
        mLastEvent = new ProximityEvent(near, event.timestamp);
        mLastEvent = new ProximityEvent(near, event.timestamp);
        alertListeners();
        alertListeners();
        if (mRateLimiter != null) {
            mRateLimiter.onSensorEvent(mLastEvent.getTimestampMs());
        }
    }
    }


    @Override
    @Override
    public String toString() {
    public String toString() {
        return String.format("{registered=%s, paused=%s, near=%s, rateLimiter=%s, sensor=%s}",
        return String.format("{registered=%s, paused=%s, near=%s, sensor=%s}",
                isRegistered(), mPaused, isNear(), mRateLimiter, mSensor);
                isRegistered(), mPaused, isNear(), mSensor);
    }
    }


    /**
    /**
+0 −77
Original line number Original line Diff line number Diff line
/*
 * 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());
    }
}
+0 −43
Original line number Original line Diff line number Diff line
@@ -164,49 +164,6 @@ public class ProximitySensorTest extends SysuiTestCase {
        assertFalse(mProximitySensor.isRegistered());
        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
    @Test
    public void testAlertListeners() {
    public void testAlertListeners() {
        TestableListener listenerA = new TestableListener();
        TestableListener listenerA = new TestableListener();
Loading