Add cache for PackageManager.getPackageUid to improvement performance
Context: `PackageManager.getPackageUid` is IPC which could be slow. Add a map in BatteryDiffEntry to cache the data. The cache lifecycle would be the same as other cache in BatteryDiffEntry. Test: manual Bug: 304439237 Fix: 304439237 Change-Id: I98d9c8cfee24dd43e9a4037b1b61c8353bf1bcea
This commit is contained in:
@@ -45,6 +45,9 @@ public class BatteryDiffEntry {
|
||||
// Caches app label and icon to improve loading performance.
|
||||
static final Map<String, BatteryEntry.NameAndIcon> sResourceCache = new HashMap<>();
|
||||
|
||||
// Caches package name and uid to improve loading performance.
|
||||
static final Map<String, Integer> sPackageNameAndUidCache = new HashMap<>();
|
||||
|
||||
// Whether a specific item is valid to launch restriction page?
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
|
||||
static final Map<String, Boolean> sValidForRestriction = new HashMap<>();
|
||||
@@ -289,10 +292,20 @@ public class BatteryDiffEntry {
|
||||
return false;
|
||||
}
|
||||
|
||||
final int uid = BatteryUtils.getInstance(mContext).getPackageUid(packageName);
|
||||
final int uid = getPackageUid(packageName);
|
||||
return uid == BatteryUtils.UID_REMOVED_APPS || uid == BatteryUtils.UID_NULL;
|
||||
}
|
||||
|
||||
private int getPackageUid(String packageName) {
|
||||
if (sPackageNameAndUidCache.containsKey(packageName)) {
|
||||
return sPackageNameAndUidCache.get(packageName);
|
||||
}
|
||||
|
||||
int uid = BatteryUtils.getInstance(mContext).getPackageUid(packageName);
|
||||
sPackageNameAndUidCache.put(packageName, uid);
|
||||
return uid;
|
||||
}
|
||||
|
||||
void loadLabelAndIcon() {
|
||||
if (mIsLoaded) {
|
||||
return;
|
||||
@@ -498,10 +511,11 @@ public class BatteryDiffEntry {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/** Clears app icon and label cache data. */
|
||||
/** Clears all cache data. */
|
||||
public static void clearCache() {
|
||||
sResourceCache.clear();
|
||||
sValidForRestriction.clear();
|
||||
sPackageNameAndUidCache.clear();
|
||||
}
|
||||
|
||||
private Drawable getBadgeIconForUser(Drawable icon) {
|
||||
|
@@ -352,16 +352,18 @@ public final class BatteryDiffEntryTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClearCache_clearDataForResourcesAndFlags() {
|
||||
public void testClearCache_clearDataForAllCaches() {
|
||||
BatteryDiffEntry.sResourceCache.put(
|
||||
"fake application key",
|
||||
new BatteryEntry.NameAndIcon("app label", null, /* iconId= */ 0));
|
||||
BatteryDiffEntry.sValidForRestriction.put("fake application key", Boolean.valueOf(false));
|
||||
BatteryDiffEntry.sPackageNameAndUidCache.put(PACKAGE_NAME, UID);
|
||||
|
||||
BatteryDiffEntry.clearCache();
|
||||
|
||||
assertThat(BatteryDiffEntry.sResourceCache).isEmpty();
|
||||
assertThat(BatteryDiffEntry.sValidForRestriction).isEmpty();
|
||||
assertThat(BatteryDiffEntry.sPackageNameAndUidCache).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -445,7 +447,11 @@ public final class BatteryDiffEntryTest {
|
||||
final BatteryDiffEntry entry = createBatteryDiffEntry(10, new BatteryHistEntry(values));
|
||||
|
||||
assertThat(entry.isSystemEntry()).isFalse();
|
||||
assertThat(BatteryDiffEntry.sPackageNameAndUidCache.containsKey(PACKAGE_NAME)).isFalse();
|
||||
assertThat(entry.isUninstalledEntry()).isFalse();
|
||||
assertThat(BatteryDiffEntry.sPackageNameAndUidCache.containsKey(PACKAGE_NAME)).isTrue();
|
||||
assertThat(BatteryDiffEntry.sPackageNameAndUidCache.get(PACKAGE_NAME)).isEqualTo(UID);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -457,7 +463,9 @@ public final class BatteryDiffEntryTest {
|
||||
final BatteryDiffEntry entry = createBatteryDiffEntry(10, new BatteryHistEntry(values));
|
||||
|
||||
assertThat(entry.isSystemEntry()).isFalse();
|
||||
assertThat(BatteryDiffEntry.sPackageNameAndUidCache.containsKey(PACKAGE_NAME)).isFalse();
|
||||
assertThat(entry.isUninstalledEntry()).isFalse();
|
||||
assertThat(BatteryDiffEntry.sPackageNameAndUidCache.containsKey(PACKAGE_NAME)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -469,7 +477,11 @@ public final class BatteryDiffEntryTest {
|
||||
final BatteryDiffEntry entry = createBatteryDiffEntry(10, new BatteryHistEntry(values));
|
||||
|
||||
assertThat(entry.isSystemEntry()).isFalse();
|
||||
assertThat(BatteryDiffEntry.sPackageNameAndUidCache.containsKey(UNINSTALLED_PACKAGE_NAME))
|
||||
.isFalse();
|
||||
assertThat(entry.isUninstalledEntry()).isTrue();
|
||||
assertThat(BatteryDiffEntry.sPackageNameAndUidCache.get(UNINSTALLED_PACKAGE_NAME))
|
||||
.isEqualTo(BatteryUtils.UID_NULL);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -590,7 +602,7 @@ public final class BatteryDiffEntryTest {
|
||||
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
||||
doReturn(drawable).when(mMockPackageManager).getDefaultActivityIcon();
|
||||
doReturn(null).when(mMockPackageManager).getApplicationInfo("com.a.b.c", 0);
|
||||
doReturn(new String[] {"com.a.b.c"}).when(mMockPackageManager).getPackagesForUid(1001);
|
||||
doReturn(new String[]{"com.a.b.c"}).when(mMockPackageManager).getPackagesForUid(1001);
|
||||
return createBatteryDiffEntry(10, batteryHistEntry);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user