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

Commit a23c564a authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

Misc code cleanup in InputDeviceSensorManager

Clean up code in sensor manager by removing dead code,
ensuring proper synchronization, and some small style changes.

Notable changes:
- Sensor methods in InputManager are not being used, as
  InputDeviceSensorManager is the entry pointer for those APIs, and it
  uses InputManagerGlobal directly. So remove unnecessary sensor methods
  in InputManager.

Bug: 290254916
Test: atest InputDeviceSensorManagerTest
Change-Id: I41b8ee6e0067374950ad45dc66699be6fbabfc65
parent ff45ea84
Loading
Loading
Loading
Loading
+28 −28
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ import android.util.SparseArray;
import android.view.InputDevice;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.os.SomeArgs;

import java.util.ArrayList;
import java.util.HashMap;
@@ -57,7 +56,7 @@ public class InputDeviceSensorManager {
    private static final int MSG_SENSOR_ACCURACY_CHANGED = 1;
    private static final int MSG_SENSOR_CHANGED = 2;

    private InputManagerGlobal mGlobal;
    private final InputManagerGlobal mGlobal;

    // sensor map from device id to sensor list
    @GuardedBy("mInputSensorLock")
@@ -67,7 +66,7 @@ public class InputDeviceSensorManager {
    private InputSensorEventListener mInputServiceSensorListener;
    @GuardedBy("mInputSensorLock")
    private final ArrayList<InputSensorEventListenerDelegate> mInputSensorEventListeners =
            new ArrayList<InputSensorEventListenerDelegate>();
            new ArrayList<>();

    // The sensor thread is only initialized if there is a listener added without a handler.
    @GuardedBy("mInputSensorLock")
@@ -128,14 +127,11 @@ public class InputDeviceSensorManager {
    }

    private static boolean sensorEquals(@NonNull Sensor lhs, @NonNull Sensor rhs) {
        if (lhs.getType() == rhs.getType() && lhs.getId() == rhs.getId()) {
            return true;
        }
        return false;
        return lhs.getType() == rhs.getType() && lhs.getId() == rhs.getId();
    }

    private void populateSensorsForInputDeviceLocked(int deviceId, InputSensorInfo[] sensorInfos) {
        List<Sensor> sensors = new ArrayList<Sensor>();
        List<Sensor> sensors = new ArrayList<>();
        for (int i = 0; i < sensorInfos.length; i++) {
            Sensor sensor = new Sensor(sensorInfos[i]);
            if (DEBUG) {
@@ -192,6 +188,11 @@ public class InputDeviceSensorManager {
        }
        synchronized (mInputSensorLock) {
            Sensor sensor = getInputDeviceSensorLocked(deviceId, sensorType);
            if (sensor == null) {
                Slog.wtf(TAG, "onInputSensorChanged: Got sensor update for device " + deviceId
                        + " but the sensor was not found.");
                return;
            }
            for (int i = 0; i < mInputSensorEventListeners.size(); i++) {
                InputSensorEventListenerDelegate listener =
                        mInputSensorEventListeners.get(i);
@@ -247,20 +248,16 @@ public class InputDeviceSensorManager {

    private static final class InputSensorEventListenerDelegate extends Handler {
        private final SensorEventListener mListener;
        private final int mDelayUs;
        private final int mMaxBatchReportLatencyUs;
        // List of sensors being listened to
        private List<Sensor> mSensors = new ArrayList<Sensor>();
        private final List<Sensor> mSensors = new ArrayList<>();
        // Sensor event array by sensor type, preallocate sensor events for each sensor of listener
        // to avoid allocation and garbage collection for each listener callback.
        private final SparseArray<SensorEvent> mSensorEvents = new SparseArray<SensorEvent>();
        private final SparseArray<SensorEvent> mSensorEvents = new SparseArray<>();

        InputSensorEventListenerDelegate(SensorEventListener listener, Sensor sensor,
                int delayUs, int maxBatchReportLatencyUs, Looper looper) {
                Looper looper) {
            super(looper);
            mListener = listener;
            mDelayUs = delayUs;
            mMaxBatchReportLatencyUs = maxBatchReportLatencyUs;
            addSensor(sensor);
        }

@@ -275,12 +272,13 @@ public class InputDeviceSensorManager {
        /**
         * Remove sensor from sensor list for listener
         */
        public void removeSensor(Sensor sensor) {
        public void removeSensor(@Nullable Sensor sensor) {
            // If sensor is not specified the listener will be unregistered for all sensors
            // and the sensor list is cleared.
            if (sensor == null) {
                mSensors.clear();
                mSensorEvents.clear();
                return;
            }
            for (Sensor s : mSensors) {
                if (sensorEquals(s, sensor)) {
@@ -301,7 +299,7 @@ public class InputDeviceSensorManager {
                }
            }
            mSensors.add(sensor);
            final int vecLength = sensor.getMaxLengthValuesArray(sensor, Build.VERSION.SDK_INT);
            final int vecLength = Sensor.getMaxLengthValuesArray(sensor, Build.VERSION.SDK_INT);
            SensorEvent event = new SensorEvent(sensor, SensorManager.SENSOR_STATUS_NO_CONTACT,
                    0 /* timestamp */, new float[vecLength]);
            mSensorEvents.put(sensor.getType(), event);
@@ -340,7 +338,6 @@ public class InputDeviceSensorManager {
         * Send sensor changed message
         */
        public void sendSensorChanged(SensorEvent event) {
            SomeArgs args = SomeArgs.obtain();
            obtainMessage(MSG_SENSOR_CHANGED, event).sendToTarget();
        }

@@ -348,7 +345,6 @@ public class InputDeviceSensorManager {
         * Send sensor accuracy changed message
         */
        public void sendSensorAccuracyChanged(int deviceId, int sensorType, int accuracy) {
            SomeArgs args = SomeArgs.obtain();
            obtainMessage(MSG_SENSOR_ACCURACY_CHANGED, deviceId, sensorType, accuracy)
                    .sendToTarget();
        }
@@ -437,15 +433,21 @@ public class InputDeviceSensorManager {
            Slog.e(TAG, "Trigger Sensors should use the requestTriggerSensor.");
            return false;
        }

        if (maxBatchReportLatencyUs < 0 || delayUs < 0) {
            Slog.e(TAG, "maxBatchReportLatencyUs and delayUs should be non-negative");
            return false;
        }

        if (getSensorForInputDevice(sensor.getId(), sensor.getType()) != null) {
        synchronized (mInputSensorLock) {
            if (getSensorForInputDevice(sensor.getId(), sensor.getType()) != null) {
                final int deviceId = sensor.getId();
                InputDevice inputDevice = InputDevice.getDevice(deviceId);
                final InputDevice inputDevice = mGlobal.getInputDevice(deviceId);
                if (inputDevice == null) {
                    Slog.e(TAG, "input device not found for sensor " + sensor.getId());
                    return false;
                }

                if (!inputDevice.hasSensor()) {
                    Slog.e(TAG, "The device doesn't have the sensor:" + sensor);
                    return false;
@@ -456,9 +458,7 @@ public class InputDeviceSensorManager {
                    return false;
                }
            }
        }

        synchronized (mInputSensorLock) {
            // Register the InputManagerService sensor listener if not yet.
            if (mInputServiceSensorListener == null) {
                mInputServiceSensorListener = new InputSensorEventListener();
@@ -471,8 +471,8 @@ public class InputDeviceSensorManager {
            int idx = findSensorEventListenerLocked(listener);
            if (idx < 0) {
                InputSensorEventListenerDelegate d =
                        new InputSensorEventListenerDelegate(listener, sensor, delayUs,
                                maxBatchReportLatencyUs, getLooperForListenerLocked(handler));
                        new InputSensorEventListenerDelegate(listener, sensor,
                                getLooperForListenerLocked(handler));
                mInputSensorEventListeners.add(d);
            } else {
                // The listener is already registered, see if it wants to listen to more sensors.
@@ -510,7 +510,7 @@ public class InputDeviceSensorManager {
            if (idx >= 0) {
                InputSensorEventListenerDelegate delegate =
                        mInputSensorEventListeners.get(idx);
                sensorsRegistered = new ArrayList<Sensor>(delegate.getSensors());
                sensorsRegistered = new ArrayList<>(delegate.getSensors());
                // Get the sensor types the listener is listening to
                delegate.removeSensor(sensor);
                if (delegate.isEmpty()) {
@@ -522,7 +522,7 @@ public class InputDeviceSensorManager {
                return;
            }
            // If no delegation remains, unregister the listener to input service
            if (mInputServiceSensorListener != null && mInputSensorEventListeners.size() == 0) {
            if (mInputServiceSensorListener != null && mInputSensorEventListeners.isEmpty()) {
                mGlobal.unregisterSensorListener(mInputServiceSensorListener);
                mInputServiceSensorListener = null;
            }
+0 −56
Original line number Diff line number Diff line
@@ -1094,62 +1094,6 @@ public final class InputManager {
        return mGlobal.monitorGestureInput(name, displayId);
    }

    /**
     * Get sensors information as list.
     *
     * @hide
     */
    public InputSensorInfo[] getSensorList(int deviceId) {
        return mGlobal.getSensorList(deviceId);
    }

    /**
     * Enable input device sensor
     *
     * @hide
     */
    public boolean enableSensor(int deviceId, int sensorType, int samplingPeriodUs,
            int maxBatchReportLatencyUs) {
        return mGlobal.enableSensor(deviceId, sensorType, samplingPeriodUs,
                maxBatchReportLatencyUs);
    }

    /**
     * Enable input device sensor
     *
     * @hide
     */
    public void disableSensor(int deviceId, int sensorType) {
        mGlobal.disableSensor(deviceId, sensorType);
    }

    /**
     * Flush input device sensor
     *
     * @hide
     */
    public boolean flushSensor(int deviceId, int sensorType) {
        return mGlobal.flushSensor(deviceId, sensorType);
    }

    /**
     * Register input device sensor listener
     *
     * @hide
     */
    public boolean registerSensorListener(IInputSensorEventListener listener) {
        return mGlobal.registerSensorListener(listener);
    }

    /**
     * Unregister input device sensor listener
     *
     * @hide
     */
    public void unregisterSensorListener(IInputSensorEventListener listener) {
        mGlobal.unregisterSensorListener(listener);
    }

    /**
     * Add a runtime association between the input port and the display port. This overrides any
     * static associations.
+7 −6
Original line number Diff line number Diff line
@@ -951,7 +951,8 @@ public final class InputManagerGlobal {
    }

    /**
     * @see InputManager#getSensorList(int)
     * Get information about all of the sensors supported by an input device
     * @see InputDeviceSensorManager
     */
    InputSensorInfo[] getSensorList(int deviceId) {
        try {
@@ -962,7 +963,7 @@ public final class InputManagerGlobal {
    }

    /**
     * @see InputManager#enableSensor(int, int, int, int)
     * @see InputDeviceSensorManager
     */
    boolean enableSensor(int deviceId, int sensorType, int samplingPeriodUs,
            int maxBatchReportLatencyUs) {
@@ -975,7 +976,7 @@ public final class InputManagerGlobal {
    }

    /**
     * @see InputManager#disableSensor(int, int)
     * @see InputDeviceSensorManager
     */
    void disableSensor(int deviceId, int sensorType) {
        try {
@@ -986,7 +987,7 @@ public final class InputManagerGlobal {
    }

    /**
     * @see InputManager#flushSensor(int, int)
     * @see InputDeviceSensorManager
     */
    boolean flushSensor(int deviceId, int sensorType) {
        try {
@@ -997,7 +998,7 @@ public final class InputManagerGlobal {
    }

    /**
     * @see InputManager#registerSensorListener(IInputSensorEventListener)
     * @see InputDeviceSensorManager
     */
    boolean registerSensorListener(IInputSensorEventListener listener) {
        try {
@@ -1008,7 +1009,7 @@ public final class InputManagerGlobal {
    }

    /**
     * @see InputManager#unregisterSensorListener(IInputSensorEventListener)
     * @see InputDeviceSensorManager
     */
    void unregisterSensorListener(IInputSensorEventListener listener) {
        try {