From f840dd481470efeec79b6578d67e5902827c9547 Mon Sep 17 00:00:00 2001 From: Prabir Pradhan Date: Wed, 5 Oct 2022 19:18:19 +0000 Subject: [PATCH] BatteryController: Remove the /sys prefix when adding UEvent listener UEvent DEVPATHs don't contain the /sys prefix. Bug: 243005009 Test: atest FrameworkServicesTests Test: manual with USI 2.0 device Change-Id: Iec6192f1e2afaee2736e5d442937be234da1d5e4 --- .../java/com/android/server/input/BatteryController.java | 7 ++++++- .../src/com/android/server/input/BatteryControllerTests.kt | 7 +++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/services/core/java/com/android/server/input/BatteryController.java b/services/core/java/com/android/server/input/BatteryController.java index 696b6047137a6..44c031da81dd3 100644 --- a/services/core/java/com/android/server/input/BatteryController.java +++ b/services/core/java/com/android/server/input/BatteryController.java @@ -479,7 +479,12 @@ final class BatteryController { handleUEventNotification(deviceId, eventTime); } }; - mUEventManager.addListener(mUEventListener, "DEVPATH=" + batteryPath); + mUEventManager.addListener(mUEventListener, "DEVPATH=" + formatDevPath(batteryPath)); + } + + private String formatDevPath(String path) { + // Remove the "/sys" prefix if it has one. + return path.startsWith("/sys") ? path.substring(4) : path; } // This must be called when the device is no longer being monitored. diff --git a/services/tests/servicestests/src/com/android/server/input/BatteryControllerTests.kt b/services/tests/servicestests/src/com/android/server/input/BatteryControllerTests.kt index 5f3f3d7714ef7..5a3148e2b6969 100644 --- a/services/tests/servicestests/src/com/android/server/input/BatteryControllerTests.kt +++ b/services/tests/servicestests/src/com/android/server/input/BatteryControllerTests.kt @@ -260,13 +260,16 @@ class BatteryControllerTests { @Test fun testListenersNotifiedOnUEventNotification() { - `when`(native.getBatteryDevicePath(DEVICE_ID)).thenReturn("/test/device1") + `when`(native.getBatteryDevicePath(DEVICE_ID)).thenReturn("/sys/dev/test/device1") `when`(native.getBatteryStatus(DEVICE_ID)).thenReturn(STATUS_CHARGING) `when`(native.getBatteryCapacity(DEVICE_ID)).thenReturn(78) val listener = createMockListener() val uEventListener = ArgumentCaptor.forClass(UEventManager.UEventListener::class.java) batteryController.registerBatteryListener(DEVICE_ID, listener, PID) - verify(uEventManager).addListener(uEventListener.capture(), eq("DEVPATH=/test/device1")) + // The device paths for UEvent notifications do not include the "/sys" prefix, so verify + // that the added listener is configured to match the path without that prefix. + verify(uEventManager) + .addListener(uEventListener.capture(), eq("DEVPATH=/dev/test/device1")) listener.verifyNotified(DEVICE_ID, status = STATUS_CHARGING, capacity = 0.78f) // If the battery state has changed when an UEvent is sent, the listeners are notified.