Move KeyboardBacklightListener to InputManagerGlobal

Move KeyboardBacklightListener, along with its associated
functions from InputManager to InputManagerGlobal.
Associated CLs for this refactoring:
- ag/21609952
- ag/21699437
- ag/21849880
- ag/21853350

Bug: b/267758905
Test: Pre-submit
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:8a7eb1b26875cd45a656bbb8c925adddc4345e39)
Merged-In: I8f189a3c4aa1746ce9c225a19925a78375ec88b2

Change-Id: I69c31a5e5a64a8b36a584892007c0f943af0e375
This commit is contained in:
Asmita Poddar
2023-03-08 16:27:31 +00:00
committed by Cherrypicker Worker
parent 24e694b29d
commit 99924e91f7
2 changed files with 126 additions and 106 deletions

View File

@@ -63,7 +63,6 @@ import android.view.WindowManager.LayoutParams;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodSubtype;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import java.lang.annotation.Retention;
@@ -104,12 +103,6 @@ public final class InputManager {
@Nullable
private Boolean mIsStylusPointerIconEnabled = null;
private final Object mKeyboardBacklightListenerLock = new Object();
@GuardedBy("mKeyboardBacklightListenerLock")
private ArrayList<KeyboardBacklightListenerDelegate> mKeyboardBacklightListeners;
@GuardedBy("mKeyboardBacklightListenerLock")
private IKeyboardBacklightListener mKeyboardBacklightListener;
private InputDeviceSensorManager mInputDeviceSensorManager;
/**
* Broadcast Action: Query available keyboard layouts.
@@ -1696,30 +1689,7 @@ public final class InputManager {
@RequiresPermission(Manifest.permission.MONITOR_KEYBOARD_BACKLIGHT)
public void registerKeyboardBacklightListener(@NonNull Executor executor,
@NonNull KeyboardBacklightListener listener) throws IllegalArgumentException {
Objects.requireNonNull(executor, "executor should not be null");
Objects.requireNonNull(listener, "listener should not be null");
synchronized (mKeyboardBacklightListenerLock) {
if (mKeyboardBacklightListener == null) {
mKeyboardBacklightListeners = new ArrayList<>();
mKeyboardBacklightListener = new LocalKeyboardBacklightListener();
try {
mIm.registerKeyboardBacklightListener(mKeyboardBacklightListener);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
final int numListeners = mKeyboardBacklightListeners.size();
for (int i = 0; i < numListeners; i++) {
if (mKeyboardBacklightListeners.get(i).mListener == listener) {
throw new IllegalArgumentException("Listener has already been registered!");
}
}
KeyboardBacklightListenerDelegate delegate =
new KeyboardBacklightListenerDelegate(listener, executor);
mKeyboardBacklightListeners.add(delegate);
}
mGlobal.registerKeyboardBacklightListener(executor, listener);
}
/**
@@ -1732,23 +1702,7 @@ public final class InputManager {
@RequiresPermission(Manifest.permission.MONITOR_KEYBOARD_BACKLIGHT)
public void unregisterKeyboardBacklightListener(
@NonNull KeyboardBacklightListener listener) {
Objects.requireNonNull(listener, "listener should not be null");
synchronized (mKeyboardBacklightListenerLock) {
if (mKeyboardBacklightListeners == null) {
return;
}
mKeyboardBacklightListeners.removeIf((delegate) -> delegate.mListener == listener);
if (mKeyboardBacklightListeners.isEmpty()) {
try {
mIm.unregisterKeyboardBacklightListener(mKeyboardBacklightListener);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
mKeyboardBacklightListeners = null;
mKeyboardBacklightListener = null;
}
}
mGlobal.unregisterKeyboardBacklightListener(listener);
}
/**
@@ -1833,61 +1787,4 @@ public final class InputManager {
void onKeyboardBacklightChanged(
int deviceId, @NonNull KeyboardBacklightState state, boolean isTriggeredByKeyPress);
}
// Implementation of the android.hardware.input.KeyboardBacklightState interface used to report
// the keyboard backlight state via the KeyboardBacklightListener interfaces.
private static final class LocalKeyboardBacklightState extends KeyboardBacklightState {
private final int mBrightnessLevel;
private final int mMaxBrightnessLevel;
LocalKeyboardBacklightState(int brightnessLevel, int maxBrightnessLevel) {
mBrightnessLevel = brightnessLevel;
mMaxBrightnessLevel = maxBrightnessLevel;
}
@Override
public int getBrightnessLevel() {
return mBrightnessLevel;
}
@Override
public int getMaxBrightnessLevel() {
return mMaxBrightnessLevel;
}
}
private static final class KeyboardBacklightListenerDelegate {
final KeyboardBacklightListener mListener;
final Executor mExecutor;
KeyboardBacklightListenerDelegate(KeyboardBacklightListener listener, Executor executor) {
mListener = listener;
mExecutor = executor;
}
void notifyKeyboardBacklightChange(int deviceId, IKeyboardBacklightState state,
boolean isTriggeredByKeyPress) {
mExecutor.execute(() ->
mListener.onKeyboardBacklightChanged(deviceId,
new LocalKeyboardBacklightState(state.brightnessLevel,
state.maxBrightnessLevel), isTriggeredByKeyPress));
}
}
private class LocalKeyboardBacklightListener extends IKeyboardBacklightListener.Stub {
@Override
public void onBrightnessChanged(int deviceId, IKeyboardBacklightState state,
boolean isTriggeredByKeyPress) {
synchronized (mKeyboardBacklightListenerLock) {
if (mKeyboardBacklightListeners == null) return;
final int numListeners = mKeyboardBacklightListeners.size();
for (int i = 0; i < numListeners; i++) {
mKeyboardBacklightListeners.get(i)
.notifyKeyboardBacklightChange(deviceId, state, isTriggeredByKeyPress);
}
}
}
}
}

View File

@@ -16,12 +16,15 @@
package android.hardware.input;
import android.Manifest;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.content.Context;
import android.hardware.BatteryState;
import android.hardware.input.InputManager.InputDeviceBatteryListener;
import android.hardware.input.InputManager.InputDeviceListener;
import android.hardware.input.InputManager.KeyboardBacklightListener;
import android.hardware.input.InputManager.OnTabletModeChangedListener;
import android.os.Handler;
import android.os.IBinder;
@@ -44,7 +47,7 @@ import java.util.concurrent.Executor;
/**
* Manages communication with the input manager service on behalf of
* an application process. You're probably looking for {@link InputManager}.
* an application process. You're probably looking for {@link InputManager}.
*
* @hide
*/
@@ -73,6 +76,12 @@ public final class InputManagerGlobal {
@GuardedBy("mBatteryListenersLock")
@Nullable private IInputDeviceBatteryListener mInputDeviceBatteryListener;
private final Object mKeyboardBacklightListenerLock = new Object();
@GuardedBy("mKeyboardBacklightListenerLock")
@Nullable private ArrayList<KeyboardBacklightListenerDelegate> mKeyboardBacklightListeners;
@GuardedBy("mKeyboardBacklightListenerLock")
@Nullable private IKeyboardBacklightListener mKeyboardBacklightListener;
private static InputManagerGlobal sInstance;
private final IInputManager mIm;
@@ -698,4 +707,118 @@ public final class InputManagerGlobal {
return mCapacity;
}
}
private static final class KeyboardBacklightListenerDelegate {
final InputManager.KeyboardBacklightListener mListener;
final Executor mExecutor;
KeyboardBacklightListenerDelegate(KeyboardBacklightListener listener, Executor executor) {
mListener = listener;
mExecutor = executor;
}
void notifyKeyboardBacklightChange(int deviceId, IKeyboardBacklightState state,
boolean isTriggeredByKeyPress) {
mExecutor.execute(() ->
mListener.onKeyboardBacklightChanged(deviceId,
new LocalKeyboardBacklightState(state.brightnessLevel,
state.maxBrightnessLevel), isTriggeredByKeyPress));
}
}
private class LocalKeyboardBacklightListener extends IKeyboardBacklightListener.Stub {
@Override
public void onBrightnessChanged(int deviceId, IKeyboardBacklightState state,
boolean isTriggeredByKeyPress) {
synchronized (mKeyboardBacklightListenerLock) {
if (mKeyboardBacklightListeners == null) return;
final int numListeners = mKeyboardBacklightListeners.size();
for (int i = 0; i < numListeners; i++) {
mKeyboardBacklightListeners.get(i)
.notifyKeyboardBacklightChange(deviceId, state, isTriggeredByKeyPress);
}
}
}
}
// Implementation of the android.hardware.input.KeyboardBacklightState interface used to report
// the keyboard backlight state via the KeyboardBacklightListener interfaces.
private static final class LocalKeyboardBacklightState extends KeyboardBacklightState {
private final int mBrightnessLevel;
private final int mMaxBrightnessLevel;
LocalKeyboardBacklightState(int brightnessLevel, int maxBrightnessLevel) {
mBrightnessLevel = brightnessLevel;
mMaxBrightnessLevel = maxBrightnessLevel;
}
@Override
public int getBrightnessLevel() {
return mBrightnessLevel;
}
@Override
public int getMaxBrightnessLevel() {
return mMaxBrightnessLevel;
}
}
/**
* @see InputManager#registerKeyboardBacklightListener(Executor, KeyboardBacklightListener)
*/
@RequiresPermission(Manifest.permission.MONITOR_KEYBOARD_BACKLIGHT)
void registerKeyboardBacklightListener(@NonNull Executor executor,
@NonNull KeyboardBacklightListener listener) throws IllegalArgumentException {
Objects.requireNonNull(executor, "executor should not be null");
Objects.requireNonNull(listener, "listener should not be null");
synchronized (mKeyboardBacklightListenerLock) {
if (mKeyboardBacklightListener == null) {
mKeyboardBacklightListeners = new ArrayList<>();
mKeyboardBacklightListener = new LocalKeyboardBacklightListener();
try {
mIm.registerKeyboardBacklightListener(mKeyboardBacklightListener);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
final int numListeners = mKeyboardBacklightListeners.size();
for (int i = 0; i < numListeners; i++) {
if (mKeyboardBacklightListeners.get(i).mListener == listener) {
throw new IllegalArgumentException("Listener has already been registered!");
}
}
KeyboardBacklightListenerDelegate delegate =
new KeyboardBacklightListenerDelegate(listener, executor);
mKeyboardBacklightListeners.add(delegate);
}
}
/**
* @see InputManager#unregisterKeyboardBacklightListener(KeyboardBacklightListener)
*/
@RequiresPermission(Manifest.permission.MONITOR_KEYBOARD_BACKLIGHT)
void unregisterKeyboardBacklightListener(
@NonNull KeyboardBacklightListener listener) {
Objects.requireNonNull(listener, "listener should not be null");
synchronized (mKeyboardBacklightListenerLock) {
if (mKeyboardBacklightListeners == null) {
return;
}
mKeyboardBacklightListeners.removeIf((delegate) -> delegate.mListener == listener);
if (mKeyboardBacklightListeners.isEmpty()) {
try {
mIm.unregisterKeyboardBacklightListener(mKeyboardBacklightListener);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
mKeyboardBacklightListeners = null;
mKeyboardBacklightListener = null;
}
}
}
}