Revert "Synchronize cache-is-ready boolean with cache readiness."

This reverts commit db9ae43ac51796574830da1a70a954d548fc61e6.

Reason for revert: perf regression b/232504244

Change-Id: I38cf43cfe3f19c6faa9279cc06cdad94fd65dd0b
diff --git a/services/core/java/com/android/server/pm/AppsFilterBase.java b/services/core/java/com/android/server/pm/AppsFilterBase.java
index e7bcb0a..7004c73 100644
--- a/services/core/java/com/android/server/pm/AppsFilterBase.java
+++ b/services/core/java/com/android/server/pm/AppsFilterBase.java
@@ -173,7 +173,7 @@
      * {@link #shouldFilterApplicationInternal(PackageDataSnapshot, int, Object,
      * PackageStateInternal, int)} call.
      * NOTE: It can only be relied upon after the system is ready to avoid unnecessary update on
-     * initial scam and is empty until {@link #mCacheReady} is true.
+     * initial scam and is empty until {@link #mSystemReady} is true.
      */
     @NonNull
     @Watched
@@ -181,7 +181,7 @@
     @NonNull
     protected SnapshotCache<WatchedSparseBooleanMatrix> mShouldFilterCacheSnapshot;
 
-    protected volatile boolean mCacheReady = false;
+    protected volatile boolean mSystemReady = false;
 
     protected boolean isForceQueryable(int callingAppId) {
         return mForceQueryable.contains(callingAppId);
@@ -312,7 +312,7 @@
                     || callingAppId == targetPkgSetting.getAppId()) {
                 return false;
             }
-            if (mCacheReady) { // use cache
+            if (mSystemReady) { // use cache
                 if (!shouldFilterApplicationUsingCache(callingUid,
                         targetPkgSetting.getAppId(),
                         userId)) {
diff --git a/services/core/java/com/android/server/pm/AppsFilterImpl.java b/services/core/java/com/android/server/pm/AppsFilterImpl.java
index c817e24..952711d 100644
--- a/services/core/java/com/android/server/pm/AppsFilterImpl.java
+++ b/services/core/java/com/android/server/pm/AppsFilterImpl.java
@@ -404,8 +404,7 @@
                     + recipientUid + " -> " + visibleUid);
         }
 
-        // TODO(b/231528435): invalidate cache instead of locking.
-        if (true/*mCacheReady*/) {
+        if (mSystemReady) {
             synchronized (mCacheLock) {
                 // update the cache in a one-off manner since we've got all the information we
                 // need.
@@ -421,6 +420,7 @@
         mFeatureConfig.onSystemReady();
 
         updateEntireShouldFilterCacheAsync(pmInternal);
+        mSystemReady = true;
     }
 
     /**
@@ -444,8 +444,7 @@
             final UserInfo[] users = snapshot.getUserInfos();
             final ArraySet<String> additionalChangedPackages =
                     addPackageInternal(newPkgSetting, settings);
-            // TODO(b/231528435): invalidate cache instead of locking.
-            if (true/*mCacheReady*/) {
+            if (mSystemReady) {
                 synchronized (mCacheLock) {
                     updateShouldFilterCacheForPackage(snapshot, null, newPkgSetting,
                             settings, users, USER_ALL, settings.size());
@@ -587,7 +586,7 @@
     }
 
     private void removeAppIdFromVisibilityCache(int appId) {
-        if (!mCacheReady) {
+        if (!mSystemReady) {
             return;
         }
         synchronized (mCacheLock) {
@@ -662,20 +661,18 @@
 
             updateEntireShouldFilterCacheInner(snapshot, settings, usersRef[0], USER_ALL);
             onChanged();
-
-            mCacheReady = true;
         });
     }
 
     public void onUserCreated(PackageDataSnapshot snapshot, int newUserId) {
-        if (!mCacheReady) {
+        if (!mSystemReady) {
             return;
         }
         updateEntireShouldFilterCache(snapshot, newUserId);
     }
 
     public void onUserDeleted(@UserIdInt int userId) {
-        if (!mCacheReady) {
+        if (!mSystemReady) {
             return;
         }
         removeShouldFilterCacheForUser(userId);
@@ -684,7 +681,7 @@
 
     private void updateShouldFilterCacheForPackage(PackageDataSnapshot snapshot,
             String packageName) {
-        if (!mCacheReady) {
+        if (!mSystemReady) {
             return;
         }
         final ArrayMap<String, ? extends PackageStateInternal> settings =
@@ -933,8 +930,7 @@
         }
 
         removeAppIdFromVisibilityCache(setting.getAppId());
-        // TODO(b/231528435): invalidate cache instead of locking.
-        if (/*mCacheReady && */setting.hasSharedUser()) {
+        if (mSystemReady && setting.hasSharedUser()) {
             final ArraySet<? extends PackageStateInternal> sharedUserPackages =
                     getSharedUserPackages(setting.getSharedUserAppId(), sharedUserSettings);
             for (int i = sharedUserPackages.size() - 1; i >= 0; i--) {
@@ -951,8 +947,7 @@
             }
         }
 
-        // TODO(b/231528435): invalidate cache instead of locking.
-        if (true/*mCacheReady*/) {
+        if (mSystemReady) {
             if (additionalChangedPackages != null) {
                 for (int index = 0; index < additionalChangedPackages.size(); index++) {
                     String changedPackage = additionalChangedPackages.valueAt(index);
diff --git a/services/core/java/com/android/server/pm/AppsFilterLocked.java b/services/core/java/com/android/server/pm/AppsFilterLocked.java
index b85bd8f..e8e6cd9 100644
--- a/services/core/java/com/android/server/pm/AppsFilterLocked.java
+++ b/services/core/java/com/android/server/pm/AppsFilterLocked.java
@@ -32,7 +32,7 @@
     /**
      * Guards the access for {@link AppsFilterBase#mShouldFilterCache};
      */
-    protected final Object mCacheLock = new Object();
+    protected Object mCacheLock = new Object();
 
     @Override
     protected boolean isForceQueryable(int appId) {
diff --git a/services/core/java/com/android/server/pm/AppsFilterSnapshotImpl.java b/services/core/java/com/android/server/pm/AppsFilterSnapshotImpl.java
index 39b6341..c12aa6d 100644
--- a/services/core/java/com/android/server/pm/AppsFilterSnapshotImpl.java
+++ b/services/core/java/com/android/server/pm/AppsFilterSnapshotImpl.java
@@ -17,7 +17,6 @@
 package com.android.server.pm;
 
 import com.android.server.utils.SnapshotCache;
-import com.android.server.utils.WatchedSparseBooleanMatrix;
 
 import java.util.Arrays;
 
@@ -50,18 +49,12 @@
         mFeatureConfig = orig.mFeatureConfig.snapshot();
         mOverlayReferenceMapper = orig.mOverlayReferenceMapper;
         mSystemSigningDetails = orig.mSystemSigningDetails;
-
-        mCacheReady = orig.mCacheReady;
-        if (mCacheReady) {
-            synchronized (orig.mCacheLock) {
-                mShouldFilterCache = orig.mShouldFilterCacheSnapshot.snapshot();
-            }
-        } else {
-            // cache is not ready, use an empty cache for the snapshot
-            mShouldFilterCache = new WatchedSparseBooleanMatrix();
+        synchronized (orig.mCacheLock) {
+            mShouldFilterCache = orig.mShouldFilterCacheSnapshot.snapshot();
+            mShouldFilterCacheSnapshot = new SnapshotCache.Sealed<>();
         }
-        mShouldFilterCacheSnapshot = new SnapshotCache.Sealed<>();
 
         mBackgroundExecutor = null;
+        mSystemReady = orig.mSystemReady;
     }
 }