Combine StylusCallback and StylusBatteryCallback.

These were initially separate to allow listening to
battery events separately from InputDevice events. However,
in practice, these events are always listened to together in
StylusManager. Separating the two does not provide any performance
improvement, and has in fact caused bugs (ag/21382305).

Bug: 269088577
Test: StylusManagerTest, StylusUsiPowerStartableTest,
StylusBluetoothPowerStartableTest

Change-Id: I2c83d8e7db93601ec9d32fc6b868396d073910f8
This commit is contained in:
Vania Januar
2023-03-02 11:04:24 +00:00
parent 6c31e64588
commit 06d81efbdf
4 changed files with 8 additions and 52 deletions

View File

@@ -62,8 +62,6 @@ constructor(
BluetoothAdapter.OnMetadataChangedListener {
private val stylusCallbacks: CopyOnWriteArrayList<StylusCallback> = CopyOnWriteArrayList()
private val stylusBatteryCallbacks: CopyOnWriteArrayList<StylusBatteryCallback> =
CopyOnWriteArrayList()
// This map should only be accessed on the handler
private val inputDeviceAddressMap: MutableMap<Int, String?> = ArrayMap()
@@ -106,14 +104,6 @@ constructor(
stylusCallbacks.remove(callback)
}
fun registerBatteryCallback(callback: StylusBatteryCallback) {
stylusBatteryCallbacks.add(callback)
}
fun unregisterBatteryCallback(callback: StylusBatteryCallback) {
stylusBatteryCallbacks.remove(callback)
}
override fun onInputDeviceAdded(deviceId: Int) {
if (!hasStarted) return
@@ -195,7 +185,7 @@ constructor(
"${device.address}: $isCharging"
}
executeStylusBatteryCallbacks { cb ->
executeStylusCallbacks { cb ->
cb.onStylusBluetoothChargingStateChanged(inputDeviceId, device, isCharging)
}
}
@@ -221,7 +211,7 @@ constructor(
onStylusUsed()
}
executeStylusBatteryCallbacks { cb ->
executeStylusCallbacks { cb ->
cb.onStylusUsiBatteryStateChanged(deviceId, eventTimeMillis, batteryState)
}
}
@@ -329,10 +319,6 @@ constructor(
stylusCallbacks.forEach(run)
}
private fun executeStylusBatteryCallbacks(run: (cb: StylusBatteryCallback) -> Unit) {
stylusBatteryCallbacks.forEach(run)
}
private fun registerBatteryListener(deviceId: Int) {
try {
inputManager.addInputDeviceBatteryListener(deviceId, executor, this)
@@ -378,13 +364,6 @@ constructor(
fun onStylusBluetoothConnected(deviceId: Int, btAddress: String) {}
fun onStylusBluetoothDisconnected(deviceId: Int, btAddress: String) {}
fun onStylusFirstUsed() {}
}
/**
* Callback interface to receive stylus battery events from the StylusManager. All callbacks are
* runs on the same background handler.
*/
interface StylusBatteryCallback {
fun onStylusBluetoothChargingStateChanged(
inputDeviceId: Int,
btDevice: BluetoothDevice,

View File

@@ -37,7 +37,7 @@ constructor(
private val inputManager: InputManager,
private val stylusUsiPowerUi: StylusUsiPowerUI,
private val featureFlags: FeatureFlags,
) : CoreStartable, StylusManager.StylusCallback, StylusManager.StylusBatteryCallback {
) : CoreStartable, StylusManager.StylusCallback {
override fun onStylusAdded(deviceId: Int) {
// On some devices, the addition of a new internal stylus indicates the use of a
@@ -74,7 +74,6 @@ constructor(
stylusUsiPowerUi.init()
stylusManager.registerCallback(this)
stylusManager.registerBatteryCallback(this)
stylusManager.startListener()
}

View File

@@ -65,8 +65,6 @@ class StylusManagerTest : SysuiTestCase() {
@Mock lateinit var uiEventLogger: UiEventLogger
@Mock lateinit var stylusCallback: StylusManager.StylusCallback
@Mock lateinit var otherStylusCallback: StylusManager.StylusCallback
@Mock lateinit var stylusBatteryCallback: StylusManager.StylusBatteryCallback
@Mock lateinit var otherStylusBatteryCallback: StylusManager.StylusBatteryCallback
private lateinit var mockitoSession: StaticMockitoSession
private lateinit var stylusManager: StylusManager
@@ -123,7 +121,6 @@ class StylusManagerTest : SysuiTestCase() {
stylusManager.startListener()
stylusManager.registerCallback(stylusCallback)
stylusManager.registerBatteryCallback(stylusBatteryCallback)
clearInvocations(inputManager)
}
@@ -433,23 +430,6 @@ class StylusManagerTest : SysuiTestCase() {
.logWithInstanceId(StylusUiEvent.BLUETOOTH_STYLUS_DISCONNECTED, 0, null, instanceId)
}
@Test
fun onMetadataChanged_multipleRegisteredBatteryCallbacks_executesAll() {
stylusManager.onInputDeviceAdded(BT_STYLUS_DEVICE_ID)
stylusManager.registerBatteryCallback(otherStylusBatteryCallback)
stylusManager.onMetadataChanged(
bluetoothDevice,
BluetoothDevice.METADATA_MAIN_CHARGING,
"true".toByteArray()
)
verify(stylusBatteryCallback, times(1))
.onStylusBluetoothChargingStateChanged(BT_STYLUS_DEVICE_ID, bluetoothDevice, true)
verify(otherStylusBatteryCallback, times(1))
.onStylusBluetoothChargingStateChanged(BT_STYLUS_DEVICE_ID, bluetoothDevice, true)
}
@Test
fun onMetadataChanged_chargingStateTrue_executesBatteryCallbacks() {
stylusManager.onInputDeviceAdded(BT_STYLUS_DEVICE_ID)
@@ -460,7 +440,7 @@ class StylusManagerTest : SysuiTestCase() {
"true".toByteArray()
)
verify(stylusBatteryCallback, times(1))
verify(stylusCallback, times(1))
.onStylusBluetoothChargingStateChanged(BT_STYLUS_DEVICE_ID, bluetoothDevice, true)
}
@@ -474,7 +454,7 @@ class StylusManagerTest : SysuiTestCase() {
"false".toByteArray()
)
verify(stylusBatteryCallback, times(1))
verify(stylusCallback, times(1))
.onStylusBluetoothChargingStateChanged(BT_STYLUS_DEVICE_ID, bluetoothDevice, false)
}
@@ -486,7 +466,7 @@ class StylusManagerTest : SysuiTestCase() {
"true".toByteArray()
)
verifyNoMoreInteractions(stylusBatteryCallback)
verifyNoMoreInteractions(stylusCallback)
}
@Test
@@ -499,8 +479,7 @@ class StylusManagerTest : SysuiTestCase() {
"true".toByteArray()
)
verify(stylusBatteryCallback, never())
.onStylusBluetoothChargingStateChanged(any(), any(), any())
verify(stylusCallback, never()).onStylusBluetoothChargingStateChanged(any(), any(), any())
}
@Test
@@ -614,7 +593,7 @@ class StylusManagerTest : SysuiTestCase() {
fun onBatteryStateChanged_executesBatteryCallbacks() {
stylusManager.onBatteryStateChanged(STYLUS_DEVICE_ID, 1, batteryState)
verify(stylusBatteryCallback, times(1))
verify(stylusCallback, times(1))
.onStylusUsiBatteryStateChanged(STYLUS_DEVICE_ID, 1, batteryState)
}

View File

@@ -96,7 +96,6 @@ class StylusUsiPowerStartableTest : SysuiTestCase() {
startable.start()
verify(stylusManager, times(1)).registerCallback(startable)
verify(stylusManager, times(1)).registerBatteryCallback(startable)
}
@Test