use of android.apphibernation.AppHibernationManager in project android_packages_apps_Settings by omnirom.
the class HibernatedAppsPreferenceController method getUnusedCount.
@WorkerThread
private int getUnusedCount() {
// TODO(b/187465752): Find a way to export this logic from PermissionController module
final PackageManager pm = mContext.getPackageManager();
final AppHibernationManager ahm = mContext.getSystemService(AppHibernationManager.class);
final List<String> hibernatedPackages = ahm.getHibernatingPackagesForUser();
int numHibernated = hibernatedPackages.size();
// Also need to count packages that are auto revoked but not hibernated.
int numAutoRevoked = 0;
final UsageStatsManager usm = mContext.getSystemService(UsageStatsManager.class);
final long now = System.currentTimeMillis();
final long unusedThreshold = DeviceConfig.getLong(DeviceConfig.NAMESPACE_PERMISSIONS, PROPERTY_HIBERNATION_UNUSED_THRESHOLD_MILLIS, DEFAULT_UNUSED_THRESHOLD_MS);
final List<UsageStats> usageStatsList = usm.queryUsageStats(INTERVAL_MONTHLY, now - unusedThreshold, now);
final Map<String, UsageStats> recentlyUsedPackages = new ArrayMap<>();
for (UsageStats us : usageStatsList) {
recentlyUsedPackages.put(us.mPackageName, us);
}
final List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.MATCH_DISABLED_COMPONENTS | PackageManager.GET_PERMISSIONS);
for (PackageInfo pi : packages) {
final String packageName = pi.packageName;
final UsageStats usageStats = recentlyUsedPackages.get(packageName);
// Only count packages that have not been used recently as auto-revoked permissions may
// stay revoked even after use if the user has not regranted them.
final boolean usedRecently = (usageStats != null && (now - usageStats.getLastTimeAnyComponentUsed() < unusedThreshold || now - usageStats.getLastTimeVisible() < unusedThreshold));
if (!hibernatedPackages.contains(packageName) && pi.requestedPermissions != null && !usedRecently) {
for (String perm : pi.requestedPermissions) {
if ((pm.getPermissionFlags(perm, packageName, mContext.getUser()) & PackageManager.FLAG_PERMISSION_AUTO_REVOKED) != 0) {
numAutoRevoked++;
break;
}
}
}
}
return numHibernated + numAutoRevoked;
}
Aggregations