Change the package cache to a SparseArrayMap.

Using a SparseArrayMap will make it easier to get the
InstalledPackageInfo object associated with a specific app.

Bug: 243588926
Test: atest frameworks/base/services/tests/mockingservicestests/src/com/android/server/tare
Test: atest frameworks/base/services/tests/servicestests/src/com/android/server/tare
Change-Id: I400e2d9d3ec8b76a293856c44375bb1d302c530f
This commit is contained in:
Kweku Adams
2022-08-25 00:45:35 +00:00
parent b16d908f43
commit fccaf824d2
4 changed files with 57 additions and 53 deletions

View File

@@ -721,15 +721,17 @@ class Agent {
@GuardedBy("mLock") @GuardedBy("mLock")
void distributeBasicIncomeLocked(int batteryLevel) { void distributeBasicIncomeLocked(int batteryLevel) {
final List<InstalledPackageInfo> pkgs = mIrs.getInstalledPackages(); final SparseArrayMap<String, InstalledPackageInfo> pkgs = mIrs.getInstalledPackages();
final long now = getCurrentTimeMillis(); final long now = getCurrentTimeMillis();
for (int i = 0; i < pkgs.size(); ++i) { for (int uIdx = pkgs.numMaps() - 1; uIdx >= 0; --uIdx) {
final InstalledPackageInfo pkgInfo = pkgs.get(i); final int userId = pkgs.keyAt(uIdx);
for (int pIdx = pkgs.numElementsForKeyAt(uIdx) - 1; pIdx >= 0; --pIdx) {
final InstalledPackageInfo pkgInfo = pkgs.valueAt(uIdx, pIdx);
if (!shouldGiveCredits(pkgInfo)) { if (!shouldGiveCredits(pkgInfo)) {
continue; continue;
} }
final int userId = UserHandle.getUserId(pkgInfo.uid);
final String pkgName = pkgInfo.packageName; final String pkgName = pkgInfo.packageName;
final Ledger ledger = mScribe.getLedgerLocked(userId, pkgName); final Ledger ledger = mScribe.getLedgerLocked(userId, pkgName);
final long minBalance = mIrs.getMinBalanceLocked(userId, pkgName); final long minBalance = mIrs.getMinBalanceLocked(userId, pkgName);
@@ -743,6 +745,7 @@ class Agent {
} }
} }
} }
}
/** Give each app an initial balance. */ /** Give each app an initial balance. */
@GuardedBy("mLock") @GuardedBy("mLock")

View File

@@ -136,7 +136,7 @@ public class InternalResourceService extends SystemService {
@NonNull @NonNull
@GuardedBy("mLock") @GuardedBy("mLock")
private final List<InstalledPackageInfo> mPkgCache = new ArrayList<>(); private final SparseArrayMap<String, InstalledPackageInfo> mPkgCache = new SparseArrayMap<>();
/** Cached mapping of UIDs (for all users) to a list of packages in the UID. */ /** Cached mapping of UIDs (for all users) to a list of packages in the UID. */
@GuardedBy("mLock") @GuardedBy("mLock")
@@ -343,7 +343,7 @@ public class InternalResourceService extends SystemService {
} }
@NonNull @NonNull
List<InstalledPackageInfo> getInstalledPackages() { SparseArrayMap<String, InstalledPackageInfo> getInstalledPackages() {
synchronized (mLock) { synchronized (mLock) {
return mPkgCache; return mPkgCache;
} }
@@ -354,11 +354,13 @@ public class InternalResourceService extends SystemService {
List<InstalledPackageInfo> getInstalledPackages(final int userId) { List<InstalledPackageInfo> getInstalledPackages(final int userId) {
final List<InstalledPackageInfo> userPkgs = new ArrayList<>(); final List<InstalledPackageInfo> userPkgs = new ArrayList<>();
synchronized (mLock) { synchronized (mLock) {
for (int i = 0; i < mPkgCache.size(); ++i) { final int uIdx = mPkgCache.indexOfKey(userId);
final InstalledPackageInfo packageInfo = mPkgCache.get(i); if (uIdx < 0) {
if (UserHandle.getUserId(packageInfo.uid) == userId) { return userPkgs;
userPkgs.add(packageInfo);
} }
for (int p = mPkgCache.numElementsForKeyAt(uIdx) - 1; p >= 0; --p) {
final InstalledPackageInfo packageInfo = mPkgCache.valueAt(uIdx, p);
userPkgs.add(packageInfo);
} }
} }
return userPkgs; return userPkgs;
@@ -511,7 +513,7 @@ public class InternalResourceService extends SystemService {
mPackageToUidCache.add(userId, pkgName, uid); mPackageToUidCache.add(userId, pkgName, uid);
} }
synchronized (mLock) { synchronized (mLock) {
mPkgCache.add(new InstalledPackageInfo(packageInfo)); mPkgCache.add(userId, pkgName, new InstalledPackageInfo(packageInfo));
mUidToPackageCache.add(uid, pkgName); mUidToPackageCache.add(uid, pkgName);
// TODO: only do this when the user first launches the app (app leaves stopped state) // TODO: only do this when the user first launches the app (app leaves stopped state)
mAgent.grantBirthrightLocked(userId, pkgName); mAgent.grantBirthrightLocked(userId, pkgName);
@@ -532,14 +534,7 @@ public class InternalResourceService extends SystemService {
synchronized (mLock) { synchronized (mLock) {
mUidToPackageCache.remove(uid, pkgName); mUidToPackageCache.remove(uid, pkgName);
mVipOverrides.delete(userId, pkgName); mVipOverrides.delete(userId, pkgName);
for (int i = 0; i < mPkgCache.size(); ++i) { mPkgCache.delete(userId, pkgName);
final InstalledPackageInfo pkgInfo = mPkgCache.get(i);
if (UserHandle.getUserId(pkgInfo.uid) == userId
&& pkgName.equals(pkgInfo.packageName)) {
mPkgCache.remove(i);
break;
}
}
mAgent.onPackageRemovedLocked(userId, pkgName); mAgent.onPackageRemovedLocked(userId, pkgName);
} }
} }
@@ -560,7 +555,8 @@ public class InternalResourceService extends SystemService {
final List<PackageInfo> pkgs = final List<PackageInfo> pkgs =
mPackageManager.getInstalledPackagesAsUser(PACKAGE_QUERY_FLAGS, userId); mPackageManager.getInstalledPackagesAsUser(PACKAGE_QUERY_FLAGS, userId);
for (int i = pkgs.size() - 1; i >= 0; --i) { for (int i = pkgs.size() - 1; i >= 0; --i) {
mPkgCache.add(new InstalledPackageInfo(pkgs.get(i))); final InstalledPackageInfo ipo = new InstalledPackageInfo(pkgs.get(i));
mPkgCache.add(userId, ipo.packageName, ipo);
} }
mAgent.grantBirthrightsLocked(userId); mAgent.grantBirthrightsLocked(userId);
} }
@@ -570,15 +566,15 @@ public class InternalResourceService extends SystemService {
synchronized (mLock) { synchronized (mLock) {
mVipOverrides.delete(userId); mVipOverrides.delete(userId);
ArrayList<String> removedPkgs = new ArrayList<>(); ArrayList<String> removedPkgs = new ArrayList<>();
for (int i = mPkgCache.size() - 1; i >= 0; --i) { final int uIdx = mPkgCache.indexOfKey(userId);
final InstalledPackageInfo pkgInfo = mPkgCache.get(i); if (uIdx >= 0) {
if (UserHandle.getUserId(pkgInfo.uid) == userId) { for (int p = mPkgCache.numElementsForKeyAt(uIdx) - 1; p >= 0; --p) {
final InstalledPackageInfo pkgInfo = mPkgCache.valueAt(uIdx, p);
removedPkgs.add(pkgInfo.packageName); removedPkgs.add(pkgInfo.packageName);
mUidToPackageCache.remove(pkgInfo.uid); mUidToPackageCache.remove(pkgInfo.uid);
mPkgCache.remove(i);
break;
} }
} }
mPkgCache.delete(userId);
mAgent.onUserRemovedLocked(userId, removedPkgs); mAgent.onUserRemovedLocked(userId, removedPkgs);
} }
} }
@@ -727,7 +723,8 @@ public class InternalResourceService extends SystemService {
final List<PackageInfo> pkgs = final List<PackageInfo> pkgs =
mPackageManager.getInstalledPackagesAsUser(PACKAGE_QUERY_FLAGS, userId); mPackageManager.getInstalledPackagesAsUser(PACKAGE_QUERY_FLAGS, userId);
for (int i = pkgs.size() - 1; i >= 0; --i) { for (int i = pkgs.size() - 1; i >= 0; --i) {
mPkgCache.add(new InstalledPackageInfo(pkgs.get(i))); final InstalledPackageInfo ipo = new InstalledPackageInfo(pkgs.get(i));
mPkgCache.add(userId, ipo.packageName, ipo);
} }
} }
} }
@@ -1185,7 +1182,6 @@ public class InternalResourceService extends SystemService {
// User setting should override DeviceConfig setting. // User setting should override DeviceConfig setting.
// NOTE: There's currently no way for a user to reset the value (via UI), so if a user // NOTE: There's currently no way for a user to reset the value (via UI), so if a user
// manually toggles TARE via UI, we'll always defer to the user's current setting // manually toggles TARE via UI, we'll always defer to the user's current setting
// TODO: add a "reset" value if the user toggle is an issue
final boolean isTareEnabledDC = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TARE, final boolean isTareEnabledDC = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TARE,
KEY_DC_ENABLE_TARE, Settings.Global.DEFAULT_ENABLE_TARE == 1); KEY_DC_ENABLE_TARE, Settings.Global.DEFAULT_ENABLE_TARE == 1);
final boolean isTareEnabled = Settings.Global.getInt(mContentResolver, final boolean isTareEnabled = Settings.Global.getInt(mContentResolver,

View File

@@ -216,11 +216,14 @@ public class Scribe {
mRemainingConsumableCakes = 0; mRemainingConsumableCakes = 0;
final SparseArray<ArraySet<String>> installedPackagesPerUser = new SparseArray<>(); final SparseArray<ArraySet<String>> installedPackagesPerUser = new SparseArray<>();
final List<InstalledPackageInfo> installedPackages = mIrs.getInstalledPackages(); final SparseArrayMap<String, InstalledPackageInfo> installedPackages =
for (int i = 0; i < installedPackages.size(); ++i) { mIrs.getInstalledPackages();
final InstalledPackageInfo packageInfo = installedPackages.get(i); for (int uIdx = installedPackages.numMaps() - 1; uIdx >= 0; --uIdx) {
final int userId = installedPackages.keyAt(uIdx);
for (int pIdx = installedPackages.numElementsForKeyAt(uIdx) - 1; pIdx >= 0; --pIdx) {
final InstalledPackageInfo packageInfo = installedPackages.valueAt(uIdx, pIdx);
if (packageInfo.uid != InstalledPackageInfo.NO_UID) { if (packageInfo.uid != InstalledPackageInfo.NO_UID) {
final int userId = UserHandle.getUserId(packageInfo.uid);
ArraySet<String> pkgsForUser = installedPackagesPerUser.get(userId); ArraySet<String> pkgsForUser = installedPackagesPerUser.get(userId);
if (pkgsForUser == null) { if (pkgsForUser == null) {
pkgsForUser = new ArraySet<>(); pkgsForUser = new ArraySet<>();
@@ -229,6 +232,7 @@ public class Scribe {
pkgsForUser.add(packageInfo.packageName); pkgsForUser.add(packageInfo.packageName);
} }
} }
}
final List<Analyst.Report> reports = new ArrayList<>(); final List<Analyst.Report> reports = new ArrayList<>();
try (FileInputStream fis = mStateFile.openRead()) { try (FileInputStream fis = mStateFile.openRead()) {

View File

@@ -68,7 +68,8 @@ public class ScribeTest {
private MockitoSession mMockingSession; private MockitoSession mMockingSession;
private Scribe mScribeUnderTest; private Scribe mScribeUnderTest;
private File mTestFileDir; private File mTestFileDir;
private final List<InstalledPackageInfo> mInstalledPackages = new ArrayList<>(); private final SparseArrayMap<String, InstalledPackageInfo> mInstalledPackages =
new SparseArrayMap<>();
private final List<Analyst.Report> mReports = new ArrayList<>(); private final List<Analyst.Report> mReports = new ArrayList<>();
@Mock @Mock
@@ -455,6 +456,6 @@ public class ScribeTest {
ApplicationInfo applicationInfo = new ApplicationInfo(); ApplicationInfo applicationInfo = new ApplicationInfo();
applicationInfo.uid = UserHandle.getUid(userId, Math.abs(pkgName.hashCode())); applicationInfo.uid = UserHandle.getUid(userId, Math.abs(pkgName.hashCode()));
pkgInfo.applicationInfo = applicationInfo; pkgInfo.applicationInfo = applicationInfo;
mInstalledPackages.add(new InstalledPackageInfo(pkgInfo)); mInstalledPackages.add(userId, pkgName, new InstalledPackageInfo(pkgInfo));
} }
} }