Fix a lot of OVERLAY_CHANGED broadcasts

A regression was introduced during refactoring where replacing with
PackageStateMutation. This caused a lot of OVERLAY_CHANGED sent while
switching to Owner user from Secondary user.

This CL modifies the logic to check if OverlayPaths can be updated
like Android S.

Bug: 245485968
Test: manually verified that OVERLAY_CHANGED not sent as expected.
Change-Id: I247aa6d2abce28eebe60e622dbffad12d1c9540c
diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index 39a6eb7..b2f3d58 100644
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -6539,7 +6539,7 @@
                             if (dependentState == null) {
                                 continue;
                             }
-                            if (!Objects.equals(dependentState.getUserStateOrDefault(userId)
+                            if (canSetOverlayPaths(dependentState.getUserStateOrDefault(userId)
                                     .getSharedLibraryOverlayPaths()
                                     .get(libName), newOverlayPaths)) {
                                 String dependentPackageName = dependent.getPackageName();
@@ -6563,7 +6563,10 @@
                     }
                 }
 
-                outUpdatedPackageNames.add(targetPackageName);
+                if (canSetOverlayPaths(packageState.getUserStateOrDefault(userId).getOverlayPaths(),
+                        newOverlayPaths)) {
+                    outUpdatedPackageNames.add(targetPackageName);
+                }
             }
 
             commitPackageStateMutation(null, mutator -> {
@@ -6614,6 +6617,17 @@
         invalidatePackageInfoCache();
     }
 
+    private boolean canSetOverlayPaths(OverlayPaths origPaths, OverlayPaths newPaths) {
+        if (Objects.equals(origPaths, newPaths)) {
+            return false;
+        }
+        if ((origPaths == null && newPaths.isEmpty())
+                || (newPaths == null && origPaths.isEmpty())) {
+            return false;
+        }
+        return true;
+    }
+
     private void maybeUpdateSystemOverlays(String targetPackageName, OverlayPaths newOverlayPaths) {
         if (!mResolverReplaced) {
             if (targetPackageName.equals("android")) {