StylusManager registers bluetooth devices present before start.

In case SystemUI crashes and StylusManager restarts, StylusManager will
now detect currently connected bluetooth devices at start.

Bug: 277738343
Test: StylusManagerTest
Change-Id: If94992e14ab8a0025062fe44ac5c54d6c21a7627
diff --git a/packages/SystemUI/src/com/android/systemui/stylus/StylusManager.kt b/packages/SystemUI/src/com/android/systemui/stylus/StylusManager.kt
index 3805019..412b315 100644
--- a/packages/SystemUI/src/com/android/systemui/stylus/StylusManager.kt
+++ b/packages/SystemUI/src/com/android/systemui/stylus/StylusManager.kt
@@ -353,6 +353,8 @@
                     // before CoreStartables run, and will not be removed.
                     // In many cases, it reports the battery level of the stylus.
                     registerBatteryListener(deviceId)
+                } else if (device.bluetoothAddress != null) {
+                    onStylusBluetoothConnected(deviceId, device.bluetoothAddress)
                 }
             }
         }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/stylus/StylusManagerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/stylus/StylusManagerTest.kt
index 17f8ec2..0b3dd66 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/stylus/StylusManagerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/stylus/StylusManagerTest.kt
@@ -101,9 +101,12 @@
         whenever(stylusDevice.supportsSource(InputDevice.SOURCE_STYLUS)).thenReturn(true)
         whenever(btStylusDevice.supportsSource(InputDevice.SOURCE_STYLUS)).thenReturn(true)
 
+        whenever(btStylusDevice.isExternal).thenReturn(true)
+
         whenever(stylusDevice.bluetoothAddress).thenReturn(null)
         whenever(btStylusDevice.bluetoothAddress).thenReturn(STYLUS_BT_ADDRESS)
 
+        whenever(btStylusDevice.batteryState).thenReturn(batteryState)
         whenever(stylusDevice.batteryState).thenReturn(batteryState)
         whenever(batteryState.capacity).thenReturn(0.5f)
 
@@ -148,6 +151,27 @@
     }
 
     @Test
+    fun startListener_hasNotStarted_registersExistingBluetoothDevice() {
+        whenever(inputManager.inputDeviceIds).thenReturn(intArrayOf(BT_STYLUS_DEVICE_ID))
+
+        stylusManager =
+            StylusManager(
+                mContext,
+                inputManager,
+                bluetoothAdapter,
+                handler,
+                EXECUTOR,
+                featureFlags,
+                uiEventLogger
+            )
+
+        stylusManager.startListener()
+
+        verify(bluetoothAdapter, times(1))
+            .addOnMetadataChangedListener(bluetoothDevice, EXECUTOR, stylusManager)
+    }
+
+    @Test
     fun startListener_hasStarted_doesNothing() {
         stylusManager.startListener()