Reduce locking duration when checking idle states

Synchronize only the methods that need to be, so that
the lock is not held for a few hundred milliseconds, blocking
other usagestats operations.

Bug: 27208519
Change-Id: I43bda0791dd8b2576a8af506bcdc67a09a5830f2
This commit is contained in:
Amith Yamasani
2016-02-18 11:07:01 -08:00
parent 0137c3daf4
commit b1e153bcd2

View File

@@ -431,17 +431,17 @@ public class UsageStatsService extends SystemService implements
List<PackageInfo> packages = mPackageManager.getInstalledPackagesAsUser(
PackageManager.MATCH_DISABLED_COMPONENTS,
userId);
synchronized (mLock) {
final int packageCount = packages.size();
for (int p = 0; p < packageCount; p++) {
final PackageInfo pi = packages.get(p);
final String packageName = pi.packageName;
final boolean isIdle = isAppIdleFiltered(packageName,
UserHandle.getAppId(pi.applicationInfo.uid),
userId, elapsedRealtime);
mHandler.sendMessage(mHandler.obtainMessage(MSG_INFORM_LISTENERS,
userId, isIdle ? 1 : 0, packageName));
if (isIdle) {
final int packageCount = packages.size();
for (int p = 0; p < packageCount; p++) {
final PackageInfo pi = packages.get(p);
final String packageName = pi.packageName;
final boolean isIdle = isAppIdleFiltered(packageName,
UserHandle.getAppId(pi.applicationInfo.uid),
userId, elapsedRealtime);
mHandler.sendMessage(mHandler.obtainMessage(MSG_INFORM_LISTENERS,
userId, isIdle ? 1 : 0, packageName));
if (isIdle) {
synchronized (mLock) {
mAppIdleHistory.setIdle(packageName, userId, elapsedRealtime);
}
}