diff options
| -rw-r--r-- | core/java/android/hardware/input/InputDeviceSensorManager.java | 39 | ||||
| -rw-r--r-- | core/java/android/hardware/input/InputManagerGlobal.java | 20 |
2 files changed, 40 insertions, 19 deletions
diff --git a/core/java/android/hardware/input/InputDeviceSensorManager.java b/core/java/android/hardware/input/InputDeviceSensorManager.java index 05024ea95eda..85e1ac014396 100644 --- a/core/java/android/hardware/input/InputDeviceSensorManager.java +++ b/core/java/android/hardware/input/InputDeviceSensorManager.java @@ -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); diff --git a/core/java/android/hardware/input/InputManagerGlobal.java b/core/java/android/hardware/input/InputManagerGlobal.java index 8c598aeae67c..fb174b8cd689 100644 --- a/core/java/android/hardware/input/InputManagerGlobal.java +++ b/core/java/android/hardware/input/InputManagerGlobal.java @@ -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,10 +942,12 @@ public final class InputManagerGlobal { */ @NonNull public SensorManager getInputDeviceSensorManager(int deviceId) { - if (mInputDeviceSensorManager == null) { - mInputDeviceSensorManager = new InputDeviceSensorManager(this); + synchronized (mInputDeviceListeners) { + if (mInputDeviceSensorManager == null) { + mInputDeviceSensorManager = new InputDeviceSensorManager(this); + } + return mInputDeviceSensorManager.getSensorManager(deviceId); } - return mInputDeviceSensorManager.getSensorManager(deviceId); } /** |