Merge "Caching format string for user label" into tm-qpr-dev am: 2e28f315cf am: d8e0edb4bd

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/libs/systemui/+/21236912

Change-Id: I36dfac88ef73451b4fbf6d835ea190b5e7df2e0d
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/iconloaderlib/src/com/android/launcher3/icons/cache/BaseIconCache.java b/iconloaderlib/src/com/android/launcher3/icons/cache/BaseIconCache.java
index 963e807..bdc4410 100644
--- a/iconloaderlib/src/com/android/launcher3/icons/cache/BaseIconCache.java
+++ b/iconloaderlib/src/com/android/launcher3/icons/cache/BaseIconCache.java
@@ -45,6 +45,7 @@
 import android.os.LocaleList;
 import android.os.Looper;
 import android.os.Process;
+import android.os.SystemClock;
 import android.os.Trace;
 import android.os.UserHandle;
 import android.text.TextUtils;
@@ -79,6 +80,8 @@
     private static final boolean DEBUG = false;
 
     private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
+    // A format string which returns the original string as is.
+    private static final String IDENTITY_FORMAT_STRING = "%1$s";
 
     // Empty class name is used for storing package default entry.
     public static final String EMPTY_CLASS_NAME = ".";
@@ -122,6 +125,8 @@
     @NonNull
     private final SparseArray<FlagOp> mUserFlagOpMap = new SparseArray<>();
 
+    private final SparseArray<String> mUserFormatString = new SparseArray<>();
+
     @Nullable
     private final String mDbFileName;
 
@@ -268,6 +273,7 @@
     private void updateSystemState() {
         mLocaleList = mContext.getResources().getConfiguration().getLocales();
         mSystemState = mLocaleList.toLanguageTags() + "," + Build.VERSION.SDK_INT;
+        mUserFormatString.clear();
     }
 
     @NonNull
@@ -275,6 +281,22 @@
         return mSystemState;
     }
 
+    public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
+        int key = user.hashCode();
+        int index = mUserFormatString.indexOfKey(key);
+        String format;
+        if (index < 0) {
+            format = mPackageManager.getUserBadgedLabel(IDENTITY_FORMAT_STRING, user).toString();
+            if (TextUtils.equals(IDENTITY_FORMAT_STRING, format)) {
+                format = null;
+            }
+            mUserFormatString.put(key, format);
+        } else {
+            format = mUserFormatString.valueAt(index);
+        }
+        return format == null ? label : String.format(format, label);
+    }
+
     /**
      * Adds an entry into the DB and the in-memory cache.
      * @param replaceExisting if true, it will recreate the bitmap even if it already exists in
@@ -313,7 +335,7 @@
         }
         entry.title = entryTitle;
 
-        entry.contentDescription = mPackageManager.getUserBadgedLabel(entry.title, user);
+        entry.contentDescription = getUserBadgedLabel(entry.title, user);
         if (cachingLogic.addToMemCache()) mCache.put(key, entry);
 
         ContentValues values = newContentValues(entry.bitmap, entry.title.toString(),
@@ -468,7 +490,7 @@
             @NonNull final T object, @NonNull final CacheEntry entry,
             @NonNull final CachingLogic<T> cachingLogic, @NonNull final UserHandle user) {
         entry.title = cachingLogic.getLabel(object);
-        entry.contentDescription = mPackageManager.getUserBadgedLabel(
+        entry.contentDescription = getUserBadgedLabel(
                 cachingLogic.getDescription(object, entry.title), user);
     }
 
@@ -549,7 +571,7 @@
                     li.close();
 
                     entry.title = appInfo.loadLabel(mPackageManager);
-                    entry.contentDescription = mPackageManager.getUserBadgedLabel(entry.title, user);
+                    entry.contentDescription = getUserBadgedLabel(entry.title, user);
                     entry.bitmap = BitmapInfo.of(
                             useLowResIcon ? LOW_RES_ICON : iconInfo.icon, iconInfo.color);
 
@@ -610,8 +632,7 @@
             entry.title = "";
             entry.contentDescription = "";
         } else {
-            entry.contentDescription = mPackageManager.getUserBadgedLabel(
-                    entry.title, cacheKey.user);
+            entry.contentDescription = getUserBadgedLabel(entry.title, cacheKey.user);
         }
 
         if (!lowRes) {