Remove the OverlayInfos without impacts target set

Most of packages are not related to overlay. It means that
* the package is not overlaid by others
* the package doesn't overlay any package.
The rate of packages overlaid by other packages is 31/231.

This patch not only removes the unnecessary OverlayInfo in memory
but also speeds the performance up.

Bug: 174206591

Test: atest \
            OverlayHostTests \
            OverlayDeviceTests \
            OverlayRemountedTest \
            FrameworksServicesTests:com.android.server.om \
            CtsContentTestCases:android.content.om.cts \
            idmap2_tests
Change-Id: Iafd88b89d0d0a51f737e10aa86ec0b348cd9d431
diff --git a/services/core/java/com/android/server/om/OverlayManagerServiceImpl.java b/services/core/java/com/android/server/om/OverlayManagerServiceImpl.java
index 8e672c3..17bb39c 100644
--- a/services/core/java/com/android/server/om/OverlayManagerServiceImpl.java
+++ b/services/core/java/com/android/server/om/OverlayManagerServiceImpl.java
@@ -166,6 +166,14 @@
         CollectionUtils.addAll(updatedTargets, removeOverlaysForUser(
                 (info) -> !userPackages.containsKey(info.packageName), newUserId));
 
+        final ArraySet<String> overlaidByOthers = new ArraySet<>();
+        for (AndroidPackage androidPackage : userPackages.values()) {
+            final String overlayTarget = androidPackage.getOverlayTarget();
+            if (!TextUtils.isEmpty(overlayTarget)) {
+                overlaidByOthers.add(overlayTarget);
+            }
+        }
+
         // Update the state of all installed packages containing overlays, and initialize new
         // overlays that are not currently in the settings.
         for (int i = 0, n = userPackages.size(); i < n; i++) {
@@ -175,8 +183,10 @@
                         updatePackageOverlays(pkg, newUserId, 0 /* flags */));
 
                 // When a new user is switched to for the first time, package manager must be
-                // informed of the overlay paths for all packages installed in the user.
-                updatedTargets.add(new PackageAndUser(pkg.getPackageName(), newUserId));
+                // informed of the overlay paths for all overlaid packages installed in the user.
+                if (overlaidByOthers.contains(pkg.getPackageName())) {
+                    updatedTargets.add(new PackageAndUser(pkg.getPackageName(), newUserId));
+                }
             } catch (OperationFailedException e) {
                 Slog.e(TAG, "failed to initialize overlays of '" + pkg.getPackageName()
                         + "' for user " + newUserId + "", e);
diff --git a/services/tests/servicestests/src/com/android/server/om/OverlayManagerServiceImplRebootTests.java b/services/tests/servicestests/src/com/android/server/om/OverlayManagerServiceImplRebootTests.java
index 94e67d1..3f55f1b 100644
--- a/services/tests/servicestests/src/com/android/server/om/OverlayManagerServiceImplRebootTests.java
+++ b/services/tests/servicestests/src/com/android/server/om/OverlayManagerServiceImplRebootTests.java
@@ -16,16 +16,16 @@
 
 package com.android.server.om;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static com.google.common.truth.Truth.assertThat;
 
 import android.content.om.OverlayIdentifier;
 import android.content.om.OverlayInfo;
 
 import androidx.test.runner.AndroidJUnit4;
 
+import com.google.common.truth.Expect;
+
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -43,6 +43,9 @@
     private static final String OVERLAY2 = OVERLAY + "2";
     private static final OverlayIdentifier IDENTIFIER2 = new OverlayIdentifier(OVERLAY2);
 
+    @Rule
+    public final Expect expect = Expect.create();
+
     @Test
     public void alwaysInitializeAllPackages() {
         final OverlayManagerServiceImpl impl = getImpl();
@@ -51,13 +54,11 @@
         addPackage(target(otherTarget), USER);
         addPackage(overlay(OVERLAY, TARGET), USER);
 
-        final Set<PackageAndUser> allPackages =
-                Set.of(new PackageAndUser(TARGET, USER),
-                        new PackageAndUser(otherTarget, USER),
-                        new PackageAndUser(OVERLAY, USER));
+        final Set<PackageAndUser> allPackages = Set.of(new PackageAndUser(TARGET, USER));
 
-        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
-        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
+        // The result should be the same for every time
+        assertThat(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
+        assertThat(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
     }
 
     @Test
@@ -66,29 +67,31 @@
         addPackage(target(TARGET), USER);
         addPackage(overlay(OVERLAY, TARGET), USER);
 
-        final Set<PackageAndUser> allPackages =
-                Set.of(new PackageAndUser(TARGET, USER), new PackageAndUser(OVERLAY, USER));
+        final Set<PackageAndUser> allPackages = Set.of(new PackageAndUser(TARGET, USER));
 
         configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_DISABLED, 0 /* priority */);
-        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
+        expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
         final OverlayInfo o1 = impl.getOverlayInfo(IDENTIFIER, USER);
-        assertNotNull(o1);
-        assertFalse(o1.isEnabled());
-        assertFalse(o1.isMutable);
+        expect.that(o1).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o1.isEnabled()).isFalse();
+        expect.that(o1.isMutable).isFalse();
 
         configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_ENABLED, 0 /* priority */);
-        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
+        expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
         final OverlayInfo o2 = impl.getOverlayInfo(IDENTIFIER, USER);
-        assertNotNull(o2);
-        assertTrue(o2.isEnabled());
-        assertFalse(o2.isMutable);
+        expect.that(o2).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o2.isEnabled()).isTrue();
+        expect.that(o2.isMutable).isFalse();
 
         configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_DISABLED, 0 /* priority */);
-        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
+        expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
         final OverlayInfo o3 = impl.getOverlayInfo(IDENTIFIER, USER);
-        assertNotNull(o3);
-        assertFalse(o3.isEnabled());
-        assertFalse(o3.isMutable);
+        expect.that(o3).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o3.isEnabled()).isFalse();
+        expect.that(o3.isMutable).isFalse();
     }
 
     @Test
@@ -98,28 +101,30 @@
         addPackage(overlay(OVERLAY, TARGET), USER);
         configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_DISABLED, 0 /* priority */);
 
-        final Set<PackageAndUser> allPackages =
-                Set.of(new PackageAndUser(TARGET, USER), new PackageAndUser(OVERLAY, USER));
+        final Set<PackageAndUser> allPackages = Set.of(new PackageAndUser(TARGET, USER));
 
-        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
+        expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
         final OverlayInfo o1 = impl.getOverlayInfo(IDENTIFIER, USER);
-        assertNotNull(o1);
-        assertFalse(o1.isEnabled());
-        assertTrue(o1.isMutable);
+        expect.that(o1).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o1.isEnabled()).isFalse();
+        expect.that(o1.isMutable).isTrue();
 
         configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_ENABLED, 0 /* priority */);
-        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
+        expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
         final OverlayInfo o2 = impl.getOverlayInfo(IDENTIFIER, USER);
-        assertNotNull(o2);
-        assertFalse(o2.isEnabled());
-        assertTrue(o2.isMutable);
+        expect.that(o2).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o2.isEnabled()).isFalse();
+        expect.that(o2.isMutable).isTrue();
 
         configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_DISABLED, 0 /* priority */);
-        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
+        expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
         final OverlayInfo o3 = impl.getOverlayInfo(IDENTIFIER, USER);
-        assertNotNull(o3);
-        assertFalse(o3.isEnabled());
-        assertTrue(o3.isMutable);
+        expect.that(o3).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o3.isEnabled()).isFalse();
+        expect.that(o3.isMutable).isTrue();
     }
 
     @Test
@@ -128,17 +133,17 @@
         addPackage(target(TARGET), USER);
         addPackage(overlay(OVERLAY, TARGET), USER);
 
-        final Set<PackageAndUser> allPackages =
-                Set.of(new PackageAndUser(TARGET, USER), new PackageAndUser(OVERLAY, USER));
+        final Set<PackageAndUser> allPackages = Set.of(new PackageAndUser(TARGET, USER));
 
         final Consumer<ConfigState> setOverlay = (state -> {
             configureSystemOverlay(OVERLAY, state, 0 /* priority */);
-            assertEquals(allPackages, impl.updateOverlaysForUser(USER));
+            expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
             final OverlayInfo o = impl.getOverlayInfo(IDENTIFIER, USER);
-            assertNotNull(o);
-            assertEquals(o.isEnabled(), state == ConfigState.IMMUTABLE_ENABLED
+            expect.that(o).isNotNull();
+            assertThat(expect.hasFailures()).isFalse();
+            expect.that(o.isEnabled()).isEqualTo(state == ConfigState.IMMUTABLE_ENABLED
                     || state == ConfigState.MUTABLE_ENABLED);
-            assertEquals(o.isMutable, state == ConfigState.MUTABLE_DISABLED
+            expect.that(o.isMutable).isEqualTo(state == ConfigState.MUTABLE_DISABLED
                     || state == ConfigState.MUTABLE_ENABLED);
         });
 
@@ -180,20 +185,20 @@
         configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_DISABLED, 0 /* priority */);
         configureSystemOverlay(OVERLAY2, ConfigState.MUTABLE_DISABLED, 1 /* priority */);
 
-        final Set<PackageAndUser> allPackages =
-                Set.of(new PackageAndUser(TARGET, USER), new PackageAndUser(OVERLAY, USER),
-                        new PackageAndUser(OVERLAY2, USER));
+        final Set<PackageAndUser> allPackages = Set.of(new PackageAndUser(TARGET, USER));
 
-        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
+        expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
         final OverlayInfo o1 = impl.getOverlayInfo(IDENTIFIER, USER);
-        assertNotNull(o1);
-        assertEquals(0, o1.priority);
-        assertFalse(o1.isEnabled());
+        expect.that(o1).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o1.priority).isEqualTo(0);
+        expect.that(o1.isEnabled()).isFalse();
 
         final OverlayInfo o2 = impl.getOverlayInfo(IDENTIFIER2, USER);
-        assertNotNull(o2);
-        assertEquals(1, o2.priority);
-        assertFalse(o2.isEnabled());
+        expect.that(o2).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o2.priority).isEqualTo(1);
+        expect.that(o2.isEnabled()).isFalse();
 
         // Overlay priority changing between reboots should not affect enable state of mutable
         // overlays.
@@ -202,16 +207,18 @@
         // Reorder the overlays
         configureSystemOverlay(OVERLAY, ConfigState.MUTABLE_DISABLED, 1 /* priority */);
         configureSystemOverlay(OVERLAY2, ConfigState.MUTABLE_DISABLED, 0 /* priority */);
-        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
+        expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
         final OverlayInfo o3 = impl.getOverlayInfo(IDENTIFIER, USER);
-        assertNotNull(o3);
-        assertEquals(1, o3.priority);
-        assertTrue(o3.isEnabled());
+        expect.that(o3).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o3.priority).isEqualTo(1);
+        expect.that(o3.isEnabled()).isTrue();
 
         final OverlayInfo o4 = impl.getOverlayInfo(IDENTIFIER2, USER);
-        assertNotNull(o4);
-        assertEquals(0, o4.priority);
-        assertFalse(o4.isEnabled());
+        expect.that(o4).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o4.priority).isEqualTo(0);
+        expect.that(o4.isEnabled()).isFalse();
     }
 
     @Test
@@ -223,33 +230,35 @@
         configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_ENABLED, 0 /* priority */);
         configureSystemOverlay(OVERLAY2, ConfigState.IMMUTABLE_ENABLED, 1 /* priority */);
 
-        final Set<PackageAndUser> allPackages =
-                Set.of(new PackageAndUser(TARGET, USER), new PackageAndUser(OVERLAY, USER),
-                        new PackageAndUser(OVERLAY2, USER));
+        final Set<PackageAndUser> allPackages = Set.of(new PackageAndUser(TARGET, USER));
 
-        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
+        expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
         final OverlayInfo o1 = impl.getOverlayInfo(IDENTIFIER, USER);
-        assertNotNull(o1);
-        assertEquals(0, o1.priority);
-        assertTrue(o1.isEnabled());
+        expect.that(o1).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o1.priority).isEqualTo(0);
+        expect.that(o1.isEnabled()).isTrue();
 
         final OverlayInfo o2 = impl.getOverlayInfo(IDENTIFIER2, USER);
-        assertNotNull(o2);
-        assertEquals(1, o2.priority);
-        assertTrue(o2.isEnabled());
+        expect.that(o2).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o2.priority).isEqualTo(1);
+        expect.that(o2.isEnabled()).isTrue();
 
         // Reorder the overlays
         configureSystemOverlay(OVERLAY, ConfigState.IMMUTABLE_ENABLED, 1 /* priority */);
         configureSystemOverlay(OVERLAY2, ConfigState.IMMUTABLE_ENABLED, 0 /* priority */);
-        assertEquals(allPackages, impl.updateOverlaysForUser(USER));
+        expect.that(impl.updateOverlaysForUser(USER)).isEqualTo(allPackages);
         final OverlayInfo o3 = impl.getOverlayInfo(IDENTIFIER, USER);
-        assertNotNull(o3);
-        assertEquals(1, o3.priority);
-        assertTrue(o3.isEnabled());
+        expect.that(o3).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o3.priority).isEqualTo(1);
+        expect.that(o3.isEnabled()).isTrue();
 
         final OverlayInfo o4 = impl.getOverlayInfo(IDENTIFIER2, USER);
-        assertNotNull(o4);
-        assertEquals(0, o4.priority);
-        assertTrue(o4.isEnabled());
+        expect.that(o4).isNotNull();
+        assertThat(expect.hasFailures()).isFalse();
+        expect.that(o4.priority).isEqualTo(0);
+        expect.that(o4.isEnabled()).isTrue();
     }
 }