Use internal lock for ResourcesManager

- Create a new package private method for
  applyNewResourceDirs that takes the lock internally in
  the resources manager class
- Move all locking to use the internal lock object

Bug: 186411938
Test: on device and local build
Change-Id: Idb49c76ceca51ed656fd47b82fad0afca1b2740b
diff --git a/apct-tests/perftests/core/src/android/app/ResourcesManagerPerfTest.java b/apct-tests/perftests/core/src/android/app/ResourcesManagerPerfTest.java
index afd8e29..ac63653 100644
--- a/apct-tests/perftests/core/src/android/app/ResourcesManagerPerfTest.java
+++ b/apct-tests/perftests/core/src/android/app/ResourcesManagerPerfTest.java
@@ -144,7 +144,7 @@
         while (state.keepRunning()) {
             state.pauseTiming();
             // Invalidate cache.
-            resourcesManager.applyConfigurationToResourcesLocked(
+            resourcesManager.applyConfigurationToResources(
                     resourcesManager.getConfiguration(), null);
             state.resumeTiming();
 
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index ff210e1..a667767 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -5887,7 +5887,7 @@
 
     public final void applyConfigurationToResources(Configuration config) {
         synchronized (mResourcesManager) {
-            mResourcesManager.applyConfigurationToResourcesLocked(config, null);
+            mResourcesManager.applyConfigurationToResources(config, null);
         }
     }
 
@@ -5975,7 +5975,7 @@
 
         synchronized (mResourcesManager) {
             // Update all affected Resources objects to use new ResourcesImpl
-            mResourcesManager.applyNewResourceDirsLocked(ai, oldResDirs);
+            mResourcesManager.applyNewResourceDirs(ai, oldResDirs);
         }
     }
 
@@ -6231,7 +6231,7 @@
 
                                 synchronized (mResourcesManager) {
                                     // Update affected Resources objects to use new ResourcesImpl
-                                    mResourcesManager.applyNewResourceDirsLocked(aInfo, oldResDirs);
+                                    mResourcesManager.applyNewResourceDirs(aInfo, oldResDirs);
                                 }
                             } catch (RemoteException e) {
                             }
@@ -6474,7 +6474,7 @@
              * reflect configuration changes. The configuration object passed
              * in AppBindData can be safely assumed to be up to date
              */
-            mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
+            mResourcesManager.applyConfigurationToResources(data.config, data.compatInfo);
             mCurDefaultDisplayDpi = data.config.densityDpi;
 
             // This calls mResourcesManager so keep it within the synchronized block.
@@ -7509,7 +7509,7 @@
 
                 // We need to apply this change to the resources immediately, because upon returning
                 // the view hierarchy will be informed about it.
-                if (mResourcesManager.applyConfigurationToResourcesLocked(globalConfig,
+                if (mResourcesManager.applyConfigurationToResources(globalConfig,
                         null /* compat */,
                         mInitialApplication.getResources().getDisplayAdjustments())) {
                     mConfigurationController.updateLocaleListFromAppContext(
diff --git a/core/java/android/app/ConfigurationController.java b/core/java/android/app/ConfigurationController.java
index 0dbe3ba..6d92201 100644
--- a/core/java/android/app/ConfigurationController.java
+++ b/core/java/android/app/ConfigurationController.java
@@ -107,8 +107,7 @@
             mCompatConfiguration = new Configuration();
         }
         mCompatConfiguration.setTo(mConfiguration);
-        if (mResourcesManager.applyCompatConfigurationLocked(displayDensity,
-                mCompatConfiguration)) {
+        if (mResourcesManager.applyCompatConfiguration(displayDensity, mCompatConfiguration)) {
             config = mCompatConfiguration;
         }
         return config;
@@ -199,7 +198,7 @@
                 // configuration also needs to set to the adjustments for consistency.
                 appResources.getDisplayAdjustments().getConfiguration().updateFrom(config);
             }
-            mResourcesManager.applyConfigurationToResourcesLocked(config, compat,
+            mResourcesManager.applyConfigurationToResources(config, compat,
                     appResources.getDisplayAdjustments());
             updateLocaleListFromAppContext(app.getApplicationContext());
 
diff --git a/core/java/android/app/ResourcesManager.java b/core/java/android/app/ResourcesManager.java
index 74134e1..792336d 100644
--- a/core/java/android/app/ResourcesManager.java
+++ b/core/java/android/app/ResourcesManager.java
@@ -75,6 +75,11 @@
     private static ResourcesManager sResourcesManager;
 
     /**
+     * Internal lock object
+     */
+    private final Object mLock = new Object();
+
+    /**
      * The global compatibility settings.
      */
     private CompatibilityInfo mResCompatibilityInfo;
@@ -275,7 +280,7 @@
      * try as hard as possible to release any open FDs.
      */
     public void invalidatePath(String path) {
-        synchronized (this) {
+        synchronized (mLock) {
             int count = 0;
 
             for (int i = mResourceImpls.size() - 1; i >= 0; i--) {
@@ -304,7 +309,7 @@
     }
 
     public Configuration getConfiguration() {
-        synchronized (this) {
+        synchronized (mLock) {
             return mResConfiguration;
         }
     }
@@ -351,13 +356,15 @@
         config.compatSmallestScreenWidthDp = config.smallestScreenWidthDp;
     }
 
-    public boolean applyCompatConfigurationLocked(int displayDensity,
+    public boolean applyCompatConfiguration(int displayDensity,
             @NonNull Configuration compatConfiguration) {
-        if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
-            mResCompatibilityInfo.applyToConfiguration(displayDensity, compatConfiguration);
-            return true;
+        synchronized (mLock) {
+            if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
+                mResCompatibilityInfo.applyToConfiguration(displayDensity, compatConfiguration);
+                return true;
+            }
+            return false;
         }
-        return false;
     }
 
     /**
@@ -376,7 +383,7 @@
         final Pair<Integer, DisplayAdjustments> key =
                 Pair.create(displayId, displayAdjustmentsCopy);
         SoftReference<Display> sd;
-        synchronized (this) {
+        synchronized (mLock) {
             sd = mAdjustedDisplays.get(key);
         }
         if (sd != null) {
@@ -392,7 +399,7 @@
         }
         final Display display = dm.getCompatibleDisplay(displayId, key.second);
         if (display != null) {
-            synchronized (this) {
+            synchronized (mLock) {
                 mAdjustedDisplays.put(key, new SoftReference<>(display));
             }
         }
@@ -407,7 +414,7 @@
      * @param resources The {@link Resources} backing the display adjustments.
      */
     public Display getAdjustedDisplay(final int displayId, Resources resources) {
-        synchronized (this) {
+        synchronized (mLock) {
             final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
             if (dm == null) {
                 // may be null early in system startup
@@ -425,7 +432,7 @@
         ApkAssets apkAssets;
 
         // Optimistically check if this ApkAssets exists somewhere else.
-        synchronized (this) {
+        synchronized (mLock) {
             final WeakReference<ApkAssets> apkAssetsRef = mCachedApkAssets.get(key);
             if (apkAssetsRef != null) {
                 apkAssets = apkAssetsRef.get();
@@ -447,7 +454,7 @@
                     key.sharedLib ? ApkAssets.PROPERTY_DYNAMIC : 0);
         }
 
-        synchronized (this) {
+        synchronized (mLock) {
             mCachedApkAssets.put(key, new WeakReference<>(apkAssets));
         }
 
@@ -559,7 +566,7 @@
      * @hide
      */
     public void dump(String prefix, PrintWriter printWriter) {
-        synchronized (this) {
+        synchronized (mLock) {
             IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, "  ");
             for (int i = 0; i < prefix.length() / 2; i++) {
                 pw.increaseIndent();
@@ -688,7 +695,7 @@
      */
     boolean isSameResourcesOverrideConfig(@Nullable IBinder activityToken,
             @Nullable Configuration overrideConfig) {
-        synchronized (this) {
+        synchronized (mLock) {
             final ActivityResources activityResources
                     = activityToken != null ? mActivityResourceReferences.get(activityToken) : null;
             if (activityResources == null) {
@@ -834,7 +841,7 @@
                         + " with key=" + key);
             }
 
-            synchronized (this) {
+            synchronized (mLock) {
                 // Force the creation of an ActivityResourcesStruct.
                 getOrCreateActivityResourcesStructLocked(token);
             }
@@ -842,7 +849,7 @@
             // Update any existing Activity Resources references.
             updateResourcesForActivity(token, overrideConfig, displayId);
 
-            synchronized (this) {
+            synchronized (mLock) {
                 Resources resources = findResourcesForActivityLocked(token, key,
                         classLoader);
                 if (resources != null) {
@@ -868,7 +875,7 @@
      */
     private void rebaseKeyForActivity(IBinder activityToken, ResourcesKey key,
             boolean overridesActivityDisplay) {
-        synchronized (this) {
+        synchronized (mLock) {
             final ActivityResources activityResources =
                     getOrCreateActivityResourcesStructLocked(activityToken);
 
@@ -960,7 +967,7 @@
         Trace.traceBegin(Trace.TRACE_TAG_RESOURCES,
                 "ResourcesManager#createApkAssetsSupplierNotLocked");
         try {
-            if (DEBUG && Thread.holdsLock(this)) {
+            if (DEBUG && Thread.holdsLock(mLock)) {
                 Slog.w(TAG, "Calling thread " + Thread.currentThread().getName()
                     + " is holding mLock", new Throwable());
             }
@@ -994,7 +1001,7 @@
     @Nullable
     private Resources createResources(@NonNull ResourcesKey key, @NonNull ClassLoader classLoader,
             @Nullable ApkAssetsSupplier apkSupplier) {
-        synchronized (this) {
+        synchronized (mLock) {
             if (DEBUG) {
                 Throwable here = new Throwable();
                 here.fillInStackTrace();
@@ -1015,7 +1022,7 @@
             @NonNull ResourcesKey key, @NonNull Configuration initialOverrideConfig,
             @Nullable Integer overrideDisplayId, @NonNull ClassLoader classLoader,
             @Nullable ApkAssetsSupplier apkSupplier) {
-        synchronized (this) {
+        synchronized (mLock) {
             if (DEBUG) {
                 Throwable here = new Throwable();
                 here.fillInStackTrace();
@@ -1130,7 +1137,7 @@
             if (displayId == INVALID_DISPLAY) {
                 throw new IllegalArgumentException("displayId can not be INVALID_DISPLAY");
             }
-            synchronized (this) {
+            synchronized (mLock) {
                 final ActivityResources activityResources =
                         getOrCreateActivityResourcesStructLocked(activityToken);
 
@@ -1269,67 +1276,64 @@
 
     public final boolean applyConfigurationToResources(@NonNull Configuration config,
             @Nullable CompatibilityInfo compat) {
-        synchronized(this) {
-            return applyConfigurationToResourcesLocked(config, compat, null /* adjustments */);
-        }
-    }
-
-    public final boolean applyConfigurationToResourcesLocked(@NonNull Configuration config,
-            @Nullable CompatibilityInfo compat) {
-        return applyConfigurationToResourcesLocked(config, compat, null /* adjustments */);
+        return applyConfigurationToResources(config, compat, null /* adjustments */);
     }
 
     /** Applies the global configuration to the managed resources. */
-    public final boolean applyConfigurationToResourcesLocked(@NonNull Configuration config,
+    public final boolean applyConfigurationToResources(@NonNull Configuration config,
             @Nullable CompatibilityInfo compat, @Nullable DisplayAdjustments adjustments) {
-        try {
-            Trace.traceBegin(Trace.TRACE_TAG_RESOURCES,
-                    "ResourcesManager#applyConfigurationToResourcesLocked");
+        synchronized (mLock) {
+            try {
+                Trace.traceBegin(Trace.TRACE_TAG_RESOURCES,
+                        "ResourcesManager#applyConfigurationToResources");
 
-            if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
-                if (DEBUG || DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
-                        + mResConfiguration.seq + ", newSeq=" + config.seq);
-                return false;
-            }
-
-            // Things might have changed in display manager, so clear the cached displays.
-            mAdjustedDisplays.clear();
-
-            int changes = mResConfiguration.updateFrom(config);
-            if (compat != null && (mResCompatibilityInfo == null ||
-                    !mResCompatibilityInfo.equals(compat))) {
-                mResCompatibilityInfo = compat;
-                changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
-                        | ActivityInfo.CONFIG_SCREEN_SIZE
-                        | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
-            }
-
-            DisplayMetrics displayMetrics = getDisplayMetrics();
-            if (adjustments != null) {
-                // Currently the only case where the adjustment takes effect is to simulate placing
-                // an app in a rotated display.
-                adjustments.adjustGlobalAppMetrics(displayMetrics);
-            }
-            Resources.updateSystemConfiguration(config, displayMetrics, compat);
-
-            ApplicationPackageManager.configurationChanged();
-
-            Configuration tmpConfig = new Configuration();
-
-            for (int i = mResourceImpls.size() - 1; i >= 0; i--) {
-                ResourcesKey key = mResourceImpls.keyAt(i);
-                WeakReference<ResourcesImpl> weakImplRef = mResourceImpls.valueAt(i);
-                ResourcesImpl r = weakImplRef != null ? weakImplRef.get() : null;
-                if (r != null) {
-                    applyConfigurationToResourcesLocked(config, compat, tmpConfig, key, r);
-                } else {
-                    mResourceImpls.removeAt(i);
+                if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
+                    if (DEBUG || DEBUG_CONFIGURATION) {
+                        Slog.v(TAG, "Skipping new config: curSeq="
+                                + mResConfiguration.seq + ", newSeq=" + config.seq);
+                    }
+                    return false;
                 }
-            }
 
-            return changes != 0;
-        } finally {
-            Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
+                // Things might have changed in display manager, so clear the cached displays.
+                mAdjustedDisplays.clear();
+
+                int changes = mResConfiguration.updateFrom(config);
+                if (compat != null && (mResCompatibilityInfo == null
+                        || !mResCompatibilityInfo.equals(compat))) {
+                    mResCompatibilityInfo = compat;
+                    changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
+                            | ActivityInfo.CONFIG_SCREEN_SIZE
+                            | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
+                }
+
+                DisplayMetrics displayMetrics = getDisplayMetrics();
+                if (adjustments != null) {
+                    // Currently the only case where the adjustment takes effect is to simulate
+                    // placing an app in a rotated display.
+                    adjustments.adjustGlobalAppMetrics(displayMetrics);
+                }
+                Resources.updateSystemConfiguration(config, displayMetrics, compat);
+
+                ApplicationPackageManager.configurationChanged();
+
+                Configuration tmpConfig = new Configuration();
+
+                for (int i = mResourceImpls.size() - 1; i >= 0; i--) {
+                    ResourcesKey key = mResourceImpls.keyAt(i);
+                    WeakReference<ResourcesImpl> weakImplRef = mResourceImpls.valueAt(i);
+                    ResourcesImpl r = weakImplRef != null ? weakImplRef.get() : null;
+                    if (r != null) {
+                        applyConfigurationToResourcesLocked(config, compat, tmpConfig, key, r);
+                    } else {
+                        mResourceImpls.removeAt(i);
+                    }
+                }
+
+                return changes != 0;
+            } finally {
+                Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
+            }
         }
     }
 
@@ -1378,7 +1382,7 @@
      * @param libAssets The library asset paths to add.
      */
     public void appendLibAssetsForMainAssetPath(String assetPath, String[] libAssets) {
-        synchronized (this) {
+        synchronized (mLock) {
             // Record which ResourcesImpl need updating
             // (and what ResourcesKey they should update to).
             final ArrayMap<ResourcesImpl, ResourcesKey> updatedResourceKeys = new ArrayMap<>();
@@ -1414,54 +1418,56 @@
     }
 
     // TODO(adamlesinski): Make this accept more than just overlay directories.
-    final void applyNewResourceDirsLocked(@NonNull final ApplicationInfo appInfo,
+    void applyNewResourceDirs(@NonNull final ApplicationInfo appInfo,
             @Nullable final String[] oldPaths) {
-        try {
-            Trace.traceBegin(Trace.TRACE_TAG_RESOURCES,
-                    "ResourcesManager#applyNewResourceDirsLocked");
+        synchronized (mLock) {
+            try {
+                Trace.traceBegin(Trace.TRACE_TAG_RESOURCES,
+                        "ResourcesManager#applyNewResourceDirsLocked");
 
-            String baseCodePath = appInfo.getBaseCodePath();
+                String baseCodePath = appInfo.getBaseCodePath();
 
-            final int myUid = Process.myUid();
-            String[] newSplitDirs = appInfo.uid == myUid
-                    ? appInfo.splitSourceDirs
-                    : appInfo.splitPublicSourceDirs;
+                final int myUid = Process.myUid();
+                String[] newSplitDirs = appInfo.uid == myUid
+                        ? appInfo.splitSourceDirs
+                        : appInfo.splitPublicSourceDirs;
 
-            // ApplicationInfo is mutable, so clone the arrays to prevent outside modification
-            String[] copiedSplitDirs = ArrayUtils.cloneOrNull(newSplitDirs);
-            String[] copiedResourceDirs = combinedOverlayPaths(appInfo.resourceDirs,
-                    appInfo.overlayPaths);
+                // ApplicationInfo is mutable, so clone the arrays to prevent outside modification
+                String[] copiedSplitDirs = ArrayUtils.cloneOrNull(newSplitDirs);
+                String[] copiedResourceDirs = combinedOverlayPaths(appInfo.resourceDirs,
+                        appInfo.overlayPaths);
 
-            final ArrayMap<ResourcesImpl, ResourcesKey> updatedResourceKeys = new ArrayMap<>();
-            final int implCount = mResourceImpls.size();
-            for (int i = 0; i < implCount; i++) {
-                final ResourcesKey key = mResourceImpls.keyAt(i);
-                final WeakReference<ResourcesImpl> weakImplRef = mResourceImpls.valueAt(i);
-                final ResourcesImpl impl = weakImplRef != null ? weakImplRef.get() : null;
+                final ArrayMap<ResourcesImpl, ResourcesKey> updatedResourceKeys = new ArrayMap<>();
+                final int implCount = mResourceImpls.size();
+                for (int i = 0; i < implCount; i++) {
+                    final ResourcesKey key = mResourceImpls.keyAt(i);
+                    final WeakReference<ResourcesImpl> weakImplRef = mResourceImpls.valueAt(i);
+                    final ResourcesImpl impl = weakImplRef != null ? weakImplRef.get() : null;
 
-                if (impl == null) {
-                    continue;
+                    if (impl == null) {
+                        continue;
+                    }
+
+                    if (key.mResDir == null
+                            || key.mResDir.equals(baseCodePath)
+                            || ArrayUtils.contains(oldPaths, key.mResDir)) {
+                        updatedResourceKeys.put(impl, new ResourcesKey(
+                                baseCodePath,
+                                copiedSplitDirs,
+                                copiedResourceDirs,
+                                key.mLibDirs,
+                                key.mDisplayId,
+                                key.mOverrideConfiguration,
+                                key.mCompatInfo,
+                                key.mLoaders
+                        ));
+                    }
                 }
 
-                if (key.mResDir == null
-                        || key.mResDir.equals(baseCodePath)
-                        || ArrayUtils.contains(oldPaths, key.mResDir)) {
-                    updatedResourceKeys.put(impl, new ResourcesKey(
-                            baseCodePath,
-                            copiedSplitDirs,
-                            copiedResourceDirs,
-                            key.mLibDirs,
-                            key.mDisplayId,
-                            key.mOverrideConfiguration,
-                            key.mCompatInfo,
-                            key.mLoaders
-                    ));
-                }
+                redirectResourcesToNewImplLocked(updatedResourceKeys);
+            } finally {
+                Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
             }
-
-            redirectResourcesToNewImplLocked(updatedResourceKeys);
-        } finally {
-            Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
         }
     }
 
@@ -1556,7 +1562,7 @@
     public boolean overrideTokenDisplayAdjustments(IBinder token,
             @Nullable Consumer<DisplayAdjustments> override) {
         boolean handled = false;
-        synchronized (this) {
+        synchronized (mLock) {
             final ActivityResources tokenResources = mActivityResourceReferences.get(token);
             if (tokenResources == null) {
                 return false;
@@ -1589,7 +1595,7 @@
         @Override
         public void onLoadersChanged(@NonNull Resources resources,
                 @NonNull List<ResourcesLoader> newLoader) {
-            synchronized (ResourcesManager.this) {
+            synchronized (mLock) {
                 final ResourcesKey oldKey = findKeyForResourceImplLocked(resources.getImpl());
                 if (oldKey == null) {
                     throw new IllegalArgumentException("Cannot modify resource loaders of"
@@ -1617,7 +1623,7 @@
          **/
         @Override
         public void onLoaderUpdated(@NonNull ResourcesLoader loader) {
-            synchronized (ResourcesManager.this) {
+            synchronized (mLock) {
                 final ArrayMap<ResourcesImpl, ResourcesKey> updatedResourceImplKeys =
                         new ArrayMap<>();
 
diff --git a/core/tests/coretests/src/android/content/res/ResourcesManagerTest.java b/core/tests/coretests/src/android/content/res/ResourcesManagerTest.java
index 46dbe0f..e7ee9dc 100644
--- a/core/tests/coretests/src/android/content/res/ResourcesManagerTest.java
+++ b/core/tests/coretests/src/android/content/res/ResourcesManagerTest.java
@@ -166,7 +166,7 @@
 
         Configuration newConfig = new Configuration();
         newConfig.orientation = Configuration.ORIENTATION_LANDSCAPE;
-        mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null);
+        mResourcesManager.applyConfigurationToResources(newConfig, null);
 
         final Configuration expectedConfig = new Configuration();
         expectedConfig.setToDefaults();