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

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

Sync InputDeviceSensorManager device state with InputManagerGlobal

Previously, InputDeviceSensorManager created its own thread for two
purposes:
1. To receive input devices changed callbacks, and
2. To notify events for listeners that were registered without a handler
  on which they should be notified.

The fact that the sensor manager was getting devices changed callbacks
on a separate thread meant that the callbacks would race with app
callbaks, so it was possible for apps to be notified of devices changing
before the manager was. In that case, an app that queries a newly added
device's sensors will not get the sensor even if it exists, because the
sensor manager doesn't know about the device yet.

To fix this, we no longer receive device changes on the sensor thread.
Instead, we notify the sensor manager of device changes directly from
the binder thread to keep it in sync with InputManagerGlobal.

The sensor thread will now be lazy-initialized only for case 2., where a
listener is added with a null handler.

Bug: 290254916
Test: atest InputDeviceSensorManagerTest
Change-Id: I6877a353e8cddd85f0aed5f4aa51e0e16fc364e7
parent 0e51c4b2
Loading
Loading
Loading
Loading
+23 −16
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.hardware.input;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.hardware.HardwareBuffer;
import android.hardware.Sensor;
import android.hardware.SensorAdditionalInfo;
@@ -49,7 +50,7 @@ import java.util.Map;
 * sensors.
 * @hide
 */
public class InputDeviceSensorManager implements InputManager.InputDeviceListener {
public class InputDeviceSensorManager {
    private static final String TAG = "InputDeviceSensorManager";
    private static final boolean DEBUG = false;

@@ -67,18 +68,15 @@ public class InputDeviceSensorManager implements InputManager.InputDeviceListene
    @GuardedBy("mInputSensorLock")
    private final ArrayList<InputSensorEventListenerDelegate> mInputSensorEventListeners =
            new ArrayList<InputSensorEventListenerDelegate>();
    private final HandlerThread mSensorThread;
    private final Handler mSensorHandler;

    // The sensor thread is only initialized if there is a listener added without a handler.
    @GuardedBy("mInputSensorLock")
    @Nullable
    private HandlerThread mSensorThread;

    public InputDeviceSensorManager(InputManagerGlobal inputManagerGlobal) {
        mGlobal = inputManagerGlobal;

        mSensorThread = new HandlerThread("SensorThread");
        mSensorThread.start();
        mSensorHandler = new Handler(mSensorThread.getLooper());

        // Register the input device listener
        mGlobal.registerInputDeviceListener(this, mSensorHandler);
        // Initialize the sensor list
        initializeSensors();
    }
@@ -105,7 +103,6 @@ public class InputDeviceSensorManager implements InputManager.InputDeviceListene
        }
    }

    @Override
    public void onInputDeviceAdded(int deviceId) {
        synchronized (mInputSensorLock) {
            if (!mSensors.containsKey(deviceId)) {
@@ -117,14 +114,12 @@ public class InputDeviceSensorManager implements InputManager.InputDeviceListene
        }
    }

    @Override
    public void onInputDeviceRemoved(int deviceId) {
        synchronized (mInputSensorLock) {
            mSensors.remove(deviceId);
        }
    }

    @Override
    public void onInputDeviceChanged(int deviceId) {
        synchronized (mInputSensorLock) {
            mSensors.remove(deviceId);
@@ -261,8 +256,8 @@ public class InputDeviceSensorManager implements InputManager.InputDeviceListene
        private final SparseArray<SensorEvent> mSensorEvents = new SparseArray<SensorEvent>();

        InputSensorEventListenerDelegate(SensorEventListener listener, Sensor sensor,
                int delayUs, int maxBatchReportLatencyUs, Handler handler) {
            super(handler != null ? handler.getLooper() : Looper.myLooper());
                int delayUs, int maxBatchReportLatencyUs, Looper looper) {
            super(looper);
            mListener = listener;
            mDelayUs = delayUs;
            mMaxBatchReportLatencyUs = maxBatchReportLatencyUs;
@@ -477,8 +472,7 @@ public class InputDeviceSensorManager implements InputManager.InputDeviceListene
            if (idx < 0) {
                InputSensorEventListenerDelegate d =
                        new InputSensorEventListenerDelegate(listener, sensor, delayUs,
                                maxBatchReportLatencyUs,
                                handler == null ? mSensorHandler : handler);
                                maxBatchReportLatencyUs, getLooperForListenerLocked(handler));
                mInputSensorEventListeners.add(d);
            } else {
                // The listener is already registered, see if it wants to listen to more sensors.
@@ -489,6 +483,19 @@ public class InputDeviceSensorManager implements InputManager.InputDeviceListene
        return true;
    }

    @GuardedBy("mInputSensorLock")
    @NonNull
    private Looper getLooperForListenerLocked(@Nullable Handler requestedHandler) {
        if (requestedHandler != null) {
            return requestedHandler.getLooper();
        }
        if (mSensorThread == null) {
            mSensorThread = new HandlerThread("SensorThread");
            mSensorThread.start();
        }
        return mSensorThread.getLooper();
    }

    private void unregisterListenerInternal(SensorEventListener listener, Sensor sensor) {
        if (DEBUG) {
            Slog.d(TAG, "unregisterListenerImpl listener=" + listener + " sensor=" + sensor);
+17 −3
Original line number Diff line number Diff line
@@ -100,6 +100,9 @@ public final class InputManagerGlobal {
    @GuardedBy("mKeyboardBacklightListenerLock")
    @Nullable private IKeyboardBacklightListener mKeyboardBacklightListener;

    // InputDeviceSensorManager gets notified synchronously from the binder thread when input
    // devices change, so it must be synchronized with the input device listeners.
    @GuardedBy("mInputDeviceListeners")
    @Nullable private InputDeviceSensorManager mInputDeviceSensorManager;

    private static InputManagerGlobal sInstance;
@@ -250,6 +253,9 @@ public final class InputManagerGlobal {
                        Log.d(TAG, "Device removed: " + deviceId);
                    }
                    mInputDevices.removeAt(i);
                    if (mInputDeviceSensorManager != null) {
                        mInputDeviceSensorManager.onInputDeviceRemoved(deviceId);
                    }
                    sendMessageToInputDeviceListenersLocked(
                            InputDeviceListenerDelegate.MSG_DEVICE_REMOVED, deviceId);
                }
@@ -267,6 +273,9 @@ public final class InputManagerGlobal {
                                Log.d(TAG, "Device changed: " + deviceId);
                            }
                            mInputDevices.setValueAt(index, null);
                            if (mInputDeviceSensorManager != null) {
                                mInputDeviceSensorManager.onInputDeviceChanged(deviceId);
                            }
                            sendMessageToInputDeviceListenersLocked(
                                    InputDeviceListenerDelegate.MSG_DEVICE_CHANGED, deviceId);
                        }
@@ -276,6 +285,9 @@ public final class InputManagerGlobal {
                        Log.d(TAG, "Device added: " + deviceId);
                    }
                    mInputDevices.put(deviceId, null);
                    if (mInputDeviceSensorManager != null) {
                        mInputDeviceSensorManager.onInputDeviceAdded(deviceId);
                    }
                    sendMessageToInputDeviceListenersLocked(
                            InputDeviceListenerDelegate.MSG_DEVICE_ADDED, deviceId);
                }
@@ -930,11 +942,13 @@ public final class InputManagerGlobal {
     */
    @NonNull
    public SensorManager getInputDeviceSensorManager(int deviceId) {
        synchronized (mInputDeviceListeners) {
            if (mInputDeviceSensorManager == null) {
                mInputDeviceSensorManager = new InputDeviceSensorManager(this);
            }
            return mInputDeviceSensorManager.getSensorManager(deviceId);
        }
    }

    /**
     * @see InputManager#getSensorList(int)