Merge "Unbind explicit health check controller when possible" into rvc-dev am: d43ac6b413 am: ca7187adfe am: 703fcb01c9

Original change: undetermined

Change-Id: I226b4ba9597cd13e5b082450da38fc5dd3e6b57c
diff --git a/services/core/java/com/android/server/PackageWatchdog.java b/services/core/java/com/android/server/PackageWatchdog.java
index e77458c..fd7abfa 100644
--- a/services/core/java/com/android/server/PackageWatchdog.java
+++ b/services/core/java/com/android/server/PackageWatchdog.java
@@ -239,7 +239,10 @@
             mIsPackagesReady = true;
             mHealthCheckController.setCallbacks(packageName -> onHealthCheckPassed(packageName),
                     packages -> onSupportedPackages(packages),
-                    () -> syncRequestsAsync());
+                    () -> {
+                            syncRequestsAsync();
+                            mSyncRequired = true;
+                    });
             setPropertyChangedListenerLocked();
             updateConfigs();
             registerConnectivityModuleHealthListener();
@@ -255,7 +258,6 @@
      */
     public void registerHealthObserver(PackageHealthObserver observer) {
         synchronized (mLock) {
-            mSyncRequired = true;
             ObserverInternal internalObserver = mAllObservers.get(observer.getName());
             if (internalObserver != null) {
                 internalObserver.registeredObserver = observer;
@@ -642,7 +644,8 @@
         synchronized (mLock) {
             if (mIsPackagesReady) {
                 Set<String> packages = getPackagesPendingHealthChecksLocked();
-                if (!packages.equals(mRequestedHealthCheckPackages) || mSyncRequired) {
+                if (mSyncRequired || !packages.equals(mRequestedHealthCheckPackages)
+                        || packages.isEmpty()) {
                     syncRequired = true;
                     mRequestedHealthCheckPackages = packages;
                 }
diff --git a/tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java b/tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java
index d011dbb..ae93a81 100644
--- a/tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java
+++ b/tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java
@@ -1113,6 +1113,7 @@
         mTestLooper.dispatchAll();
 
         List<Set> expectedSyncRequests = List.of(
+                Set.of(),
                 Set.of(APP_A),
                 Set.of(APP_A, APP_B),
                 Set.of(APP_A, APP_B, APP_C),