Handle device presence registry when device is already active.
Bug: 218615198
Test: atest CtsCompanionDeviceManagerCoreTestCases:ObservingDevicePresenceTest
Change-Id: I2dd32754e0f7215d85a60b38d532bfc76617a82e
diff --git a/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java b/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java
index 2a22cac..55fe711 100644
--- a/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java
+++ b/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java
@@ -650,6 +650,12 @@
private void registerDevicePresenceListenerActive(String packageName, String deviceAddress,
boolean active) throws RemoteException {
+ if (DEBUG) {
+ Log.i(TAG, "registerDevicePresenceListenerActive()"
+ + " active=" + active
+ + " deviceAddress=" + deviceAddress);
+ }
+
getContext().enforceCallingOrSelfPermission(
android.Manifest.permission.REQUEST_OBSERVE_COMPANION_DEVICE_PRESENCE,
"[un]registerDevicePresenceListenerService");
@@ -665,6 +671,12 @@
+ " for user " + userId));
}
+ // If already at specified state, then no-op.
+ if (active == association.isNotifyOnDeviceNearby()) {
+ if (DEBUG) Log.d(TAG, "Device presence listener is already at desired state.");
+ return;
+ }
+
// AssociationInfo class is immutable: create a new AssociationInfo object with updated
// flag.
association = AssociationInfo.builder(association)
@@ -675,7 +687,17 @@
// an application sets/unsets the mNotifyOnDeviceNearby flag.
mAssociationStore.updateAssociation(association);
- // TODO(b/218615198): correctly handle the case when the device is currently present.
+ // If device is already present, then trigger callback.
+ if (active && mDevicePresenceMonitor.isDevicePresent(association.getId())) {
+ if (DEBUG) Log.d(TAG, "Device is already present. Triggering callback.");
+ onDeviceAppearedInternal(association.getId());
+ }
+
+ // If last listener is unregistered, then unbind application.
+ if (!active && !shouldBindPackage(userId, packageName)) {
+ if (DEBUG) Log.d(TAG, "Last listener unregistered. Unbinding application.");
+ mCompanionAppController.unbindCompanionApplication(userId, packageName);
+ }
}
@Override