Loading services/core/java/com/android/server/input/BatteryController.java +21 −36 Original line number Diff line number Diff line Loading @@ -46,7 +46,6 @@ import android.view.InputDevice; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; import com.android.server.input.BatteryController.UEventManager.UEventBatteryListener; import java.io.PrintWriter; import java.util.Arrays; Loading Loading @@ -102,8 +101,9 @@ final class BatteryController { @GuardedBy("mLock") private BluetoothBatteryManager.BluetoothBatteryListener mBluetoothBatteryListener; BatteryController(Context context, NativeInputManagerService nativeService, Looper looper) { this(context, nativeService, looper, new UEventManager() {}, BatteryController(Context context, NativeInputManagerService nativeService, Looper looper, UEventManager uEventManager) { this(context, nativeService, looper, uEventManager, new LocalBluetoothBatteryManager(context, looper)); } Loading Loading @@ -567,7 +567,7 @@ final class BatteryController { private BluetoothAdapter.OnMetadataChangedListener mBluetoothMetadataListener; @Nullable private UEventBatteryListener mUEventBatteryListener; private BatteryController.UEventBatteryListener mUEventBatteryListener; DeviceMonitor(int deviceId) { mState = new State(deviceId); Loading Loading @@ -630,7 +630,7 @@ final class BatteryController { return; } final int deviceId = mState.deviceId; mUEventBatteryListener = new UEventBatteryListener() { mUEventBatteryListener = new BatteryController.UEventBatteryListener() { @Override public void onBatteryUEvent(long eventTime) { handleUEventNotification(deviceId, eventTime); Loading Loading @@ -898,15 +898,10 @@ final class BatteryController { } } // An interface used to change the API of UEventObserver to a more test-friendly format. @VisibleForTesting interface UEventManager { @VisibleForTesting abstract class UEventBatteryListener { private final UEventObserver mObserver = new UEventObserver() { abstract static class UEventBatteryListener extends UEventManager.UEventListener { @Override public void onUEvent(UEvent event) { public void onUEvent(UEventObserver.UEvent event) { final long eventTime = SystemClock.uptimeMillis(); if (DEBUG) { Slog.d(TAG, Loading @@ -920,20 +915,10 @@ final class BatteryController { } UEventBatteryListener.this.onBatteryUEvent(eventTime); } }; public abstract void onBatteryUEvent(long eventTime); } default void addListener(UEventBatteryListener listener, String match) { listener.mObserver.startObserving(match); } default void removeListener(UEventBatteryListener listener) { listener.mObserver.stopObserving(); } } // An interface used to change the API of adding a bluetooth battery listener to a more // test-friendly format. @VisibleForTesting Loading services/core/java/com/android/server/input/InputManagerService.java +12 −4 Original line number Diff line number Diff line Loading @@ -393,10 +393,12 @@ public class InputManagerService extends IInputManager.Stub static class Injector { private final Context mContext; private final Looper mLooper; private final UEventManager mUEventManager; Injector(Context context, Looper looper) { Injector(Context context, Looper looper, UEventManager uEventManager) { mContext = context; mLooper = looper; mUEventManager = uEventManager; } Context getContext() { Loading @@ -407,6 +409,10 @@ public class InputManagerService extends IInputManager.Stub return mLooper; } UEventManager getUEventManager() { return mUEventManager; } NativeInputManagerService getNativeService(InputManagerService service) { return new NativeInputManagerService.NativeImpl(service, mLooper.getQueue()); } Loading @@ -417,7 +423,7 @@ public class InputManagerService extends IInputManager.Stub } public InputManagerService(Context context) { this(new Injector(context, DisplayThread.get().getLooper())); this(new Injector(context, DisplayThread.get().getLooper(), new UEventManager() {})); } @VisibleForTesting Loading @@ -432,10 +438,12 @@ public class InputManagerService extends IInputManager.Stub mSettingsObserver = new InputSettingsObserver(mContext, mHandler, this, mNative); mKeyboardLayoutManager = new KeyboardLayoutManager(mContext, mNative, mDataStore, injector.getLooper()); mBatteryController = new BatteryController(mContext, mNative, injector.getLooper()); mBatteryController = new BatteryController(mContext, mNative, injector.getLooper(), injector.getUEventManager()); mKeyboardBacklightController = InputFeatureFlagProvider.isKeyboardBacklightControlEnabled() ? new KeyboardBacklightController(mContext, mNative, mDataStore, injector.getLooper()) : new KeyboardBacklightControllerInterface() {}; injector.getLooper(), injector.getUEventManager()) : new KeyboardBacklightControllerInterface() {}; mKeyRemapper = new KeyRemapper(mContext, mNative, mDataStore, injector.getLooper()); mUseDevInputEventForAudioJack = Loading services/core/java/com/android/server/input/KeyboardBacklightController.java +9 −7 Original line number Diff line number Diff line Loading @@ -94,6 +94,7 @@ final class KeyboardBacklightController implements private final PersistentDataStore mDataStore; private final Handler mHandler; private final AnimatorFactory mAnimatorFactory; private final UEventManager mUEventManager; // Always access on handler thread or need to lock this for synchronization. private final SparseArray<KeyboardBacklightState> mKeyboardBacklights = new SparseArray<>(1); // Maintains state if all backlights should be on or turned off Loading Loading @@ -123,19 +124,21 @@ final class KeyboardBacklightController implements } KeyboardBacklightController(Context context, NativeInputManagerService nativeService, PersistentDataStore dataStore, Looper looper) { this(context, nativeService, dataStore, looper, ValueAnimator::ofInt); PersistentDataStore dataStore, Looper looper, UEventManager uEventManager) { this(context, nativeService, dataStore, looper, ValueAnimator::ofInt, uEventManager); } @VisibleForTesting KeyboardBacklightController(Context context, NativeInputManagerService nativeService, PersistentDataStore dataStore, Looper looper, AnimatorFactory animatorFactory) { PersistentDataStore dataStore, Looper looper, AnimatorFactory animatorFactory, UEventManager uEventManager) { mContext = context; mNative = nativeService; mDataStore = dataStore; mHandler = new Handler(looper, this::handleMessage); mAnimatorFactory = animatorFactory; mAmbientController = new AmbientKeyboardBacklightController(context, looper); mUEventManager = uEventManager; } @Override Loading @@ -152,13 +155,12 @@ final class KeyboardBacklightController implements // We want to observe creation of such LED nodes since they might be created after device // FD created and InputDevice creation logic doesn't initialize LED nodes which leads to // backlight not working. UEventObserver observer = new UEventObserver() { mUEventManager.addListener(new UEventManager.UEventListener() { @Override public void onUEvent(UEvent event) { public void onUEvent(UEventObserver.UEvent event) { onKeyboardBacklightUEvent(event); } }; observer.startObserving(UEVENT_KEYBOARD_BACKLIGHT_TAG); }, UEVENT_KEYBOARD_BACKLIGHT_TAG); if (InputFeatureFlagProvider.isAmbientKeyboardBacklightControlEnabled()) { // Start ambient backlight controller Loading services/core/java/com/android/server/input/UEventManager.java 0 → 100644 +42 −0 Original line number Diff line number Diff line /* * Copyright 2023 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.server.input; import android.os.UEventObserver; /** An interface used to change the API of UEventObserver to a more test-friendly format. */ interface UEventManager { abstract class UEventListener { private final UEventObserver mObserver = new UEventObserver() { @Override public void onUEvent(UEvent event) { UEventListener.this.onUEvent(event); } }; public abstract void onUEvent(UEventObserver.UEvent event); } default void addListener(UEventListener listener, String match) { listener.mObserver.startObserving(match); } default void removeListener(UEventListener listener) { listener.mObserver.stopObserving(); } } services/tests/servicestests/src/com/android/server/input/BatteryControllerTests.kt +1 −2 Original line number Diff line number Diff line Loading @@ -40,8 +40,7 @@ import androidx.test.core.app.ApplicationProvider import com.android.server.input.BatteryController.BluetoothBatteryManager import com.android.server.input.BatteryController.BluetoothBatteryManager.BluetoothBatteryListener import com.android.server.input.BatteryController.POLLING_PERIOD_MILLIS import com.android.server.input.BatteryController.UEventManager import com.android.server.input.BatteryController.UEventManager.UEventBatteryListener import com.android.server.input.BatteryController.UEventBatteryListener import com.android.server.input.BatteryController.USI_BATTERY_VALIDITY_DURATION_MILLIS import org.hamcrest.Description import org.hamcrest.Matcher Loading Loading
services/core/java/com/android/server/input/BatteryController.java +21 −36 Original line number Diff line number Diff line Loading @@ -46,7 +46,6 @@ import android.view.InputDevice; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; import com.android.server.input.BatteryController.UEventManager.UEventBatteryListener; import java.io.PrintWriter; import java.util.Arrays; Loading Loading @@ -102,8 +101,9 @@ final class BatteryController { @GuardedBy("mLock") private BluetoothBatteryManager.BluetoothBatteryListener mBluetoothBatteryListener; BatteryController(Context context, NativeInputManagerService nativeService, Looper looper) { this(context, nativeService, looper, new UEventManager() {}, BatteryController(Context context, NativeInputManagerService nativeService, Looper looper, UEventManager uEventManager) { this(context, nativeService, looper, uEventManager, new LocalBluetoothBatteryManager(context, looper)); } Loading Loading @@ -567,7 +567,7 @@ final class BatteryController { private BluetoothAdapter.OnMetadataChangedListener mBluetoothMetadataListener; @Nullable private UEventBatteryListener mUEventBatteryListener; private BatteryController.UEventBatteryListener mUEventBatteryListener; DeviceMonitor(int deviceId) { mState = new State(deviceId); Loading Loading @@ -630,7 +630,7 @@ final class BatteryController { return; } final int deviceId = mState.deviceId; mUEventBatteryListener = new UEventBatteryListener() { mUEventBatteryListener = new BatteryController.UEventBatteryListener() { @Override public void onBatteryUEvent(long eventTime) { handleUEventNotification(deviceId, eventTime); Loading Loading @@ -898,15 +898,10 @@ final class BatteryController { } } // An interface used to change the API of UEventObserver to a more test-friendly format. @VisibleForTesting interface UEventManager { @VisibleForTesting abstract class UEventBatteryListener { private final UEventObserver mObserver = new UEventObserver() { abstract static class UEventBatteryListener extends UEventManager.UEventListener { @Override public void onUEvent(UEvent event) { public void onUEvent(UEventObserver.UEvent event) { final long eventTime = SystemClock.uptimeMillis(); if (DEBUG) { Slog.d(TAG, Loading @@ -920,20 +915,10 @@ final class BatteryController { } UEventBatteryListener.this.onBatteryUEvent(eventTime); } }; public abstract void onBatteryUEvent(long eventTime); } default void addListener(UEventBatteryListener listener, String match) { listener.mObserver.startObserving(match); } default void removeListener(UEventBatteryListener listener) { listener.mObserver.stopObserving(); } } // An interface used to change the API of adding a bluetooth battery listener to a more // test-friendly format. @VisibleForTesting Loading
services/core/java/com/android/server/input/InputManagerService.java +12 −4 Original line number Diff line number Diff line Loading @@ -393,10 +393,12 @@ public class InputManagerService extends IInputManager.Stub static class Injector { private final Context mContext; private final Looper mLooper; private final UEventManager mUEventManager; Injector(Context context, Looper looper) { Injector(Context context, Looper looper, UEventManager uEventManager) { mContext = context; mLooper = looper; mUEventManager = uEventManager; } Context getContext() { Loading @@ -407,6 +409,10 @@ public class InputManagerService extends IInputManager.Stub return mLooper; } UEventManager getUEventManager() { return mUEventManager; } NativeInputManagerService getNativeService(InputManagerService service) { return new NativeInputManagerService.NativeImpl(service, mLooper.getQueue()); } Loading @@ -417,7 +423,7 @@ public class InputManagerService extends IInputManager.Stub } public InputManagerService(Context context) { this(new Injector(context, DisplayThread.get().getLooper())); this(new Injector(context, DisplayThread.get().getLooper(), new UEventManager() {})); } @VisibleForTesting Loading @@ -432,10 +438,12 @@ public class InputManagerService extends IInputManager.Stub mSettingsObserver = new InputSettingsObserver(mContext, mHandler, this, mNative); mKeyboardLayoutManager = new KeyboardLayoutManager(mContext, mNative, mDataStore, injector.getLooper()); mBatteryController = new BatteryController(mContext, mNative, injector.getLooper()); mBatteryController = new BatteryController(mContext, mNative, injector.getLooper(), injector.getUEventManager()); mKeyboardBacklightController = InputFeatureFlagProvider.isKeyboardBacklightControlEnabled() ? new KeyboardBacklightController(mContext, mNative, mDataStore, injector.getLooper()) : new KeyboardBacklightControllerInterface() {}; injector.getLooper(), injector.getUEventManager()) : new KeyboardBacklightControllerInterface() {}; mKeyRemapper = new KeyRemapper(mContext, mNative, mDataStore, injector.getLooper()); mUseDevInputEventForAudioJack = Loading
services/core/java/com/android/server/input/KeyboardBacklightController.java +9 −7 Original line number Diff line number Diff line Loading @@ -94,6 +94,7 @@ final class KeyboardBacklightController implements private final PersistentDataStore mDataStore; private final Handler mHandler; private final AnimatorFactory mAnimatorFactory; private final UEventManager mUEventManager; // Always access on handler thread or need to lock this for synchronization. private final SparseArray<KeyboardBacklightState> mKeyboardBacklights = new SparseArray<>(1); // Maintains state if all backlights should be on or turned off Loading Loading @@ -123,19 +124,21 @@ final class KeyboardBacklightController implements } KeyboardBacklightController(Context context, NativeInputManagerService nativeService, PersistentDataStore dataStore, Looper looper) { this(context, nativeService, dataStore, looper, ValueAnimator::ofInt); PersistentDataStore dataStore, Looper looper, UEventManager uEventManager) { this(context, nativeService, dataStore, looper, ValueAnimator::ofInt, uEventManager); } @VisibleForTesting KeyboardBacklightController(Context context, NativeInputManagerService nativeService, PersistentDataStore dataStore, Looper looper, AnimatorFactory animatorFactory) { PersistentDataStore dataStore, Looper looper, AnimatorFactory animatorFactory, UEventManager uEventManager) { mContext = context; mNative = nativeService; mDataStore = dataStore; mHandler = new Handler(looper, this::handleMessage); mAnimatorFactory = animatorFactory; mAmbientController = new AmbientKeyboardBacklightController(context, looper); mUEventManager = uEventManager; } @Override Loading @@ -152,13 +155,12 @@ final class KeyboardBacklightController implements // We want to observe creation of such LED nodes since they might be created after device // FD created and InputDevice creation logic doesn't initialize LED nodes which leads to // backlight not working. UEventObserver observer = new UEventObserver() { mUEventManager.addListener(new UEventManager.UEventListener() { @Override public void onUEvent(UEvent event) { public void onUEvent(UEventObserver.UEvent event) { onKeyboardBacklightUEvent(event); } }; observer.startObserving(UEVENT_KEYBOARD_BACKLIGHT_TAG); }, UEVENT_KEYBOARD_BACKLIGHT_TAG); if (InputFeatureFlagProvider.isAmbientKeyboardBacklightControlEnabled()) { // Start ambient backlight controller Loading
services/core/java/com/android/server/input/UEventManager.java 0 → 100644 +42 −0 Original line number Diff line number Diff line /* * Copyright 2023 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.server.input; import android.os.UEventObserver; /** An interface used to change the API of UEventObserver to a more test-friendly format. */ interface UEventManager { abstract class UEventListener { private final UEventObserver mObserver = new UEventObserver() { @Override public void onUEvent(UEvent event) { UEventListener.this.onUEvent(event); } }; public abstract void onUEvent(UEventObserver.UEvent event); } default void addListener(UEventListener listener, String match) { listener.mObserver.startObserving(match); } default void removeListener(UEventListener listener) { listener.mObserver.stopObserving(); } }
services/tests/servicestests/src/com/android/server/input/BatteryControllerTests.kt +1 −2 Original line number Diff line number Diff line Loading @@ -40,8 +40,7 @@ import androidx.test.core.app.ApplicationProvider import com.android.server.input.BatteryController.BluetoothBatteryManager import com.android.server.input.BatteryController.BluetoothBatteryManager.BluetoothBatteryListener import com.android.server.input.BatteryController.POLLING_PERIOD_MILLIS import com.android.server.input.BatteryController.UEventManager import com.android.server.input.BatteryController.UEventManager.UEventBatteryListener import com.android.server.input.BatteryController.UEventBatteryListener import com.android.server.input.BatteryController.USI_BATTERY_VALIDITY_DURATION_MILLIS import org.hamcrest.Description import org.hamcrest.Matcher Loading