Search in sources :

Example 1 with AppHibernationManager

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;
}
Also used : AppHibernationManager(android.apphibernation.AppHibernationManager) PackageInfo(android.content.pm.PackageInfo) ArrayMap(android.util.ArrayMap) UsageStats(android.app.usage.UsageStats) PackageManager(android.content.pm.PackageManager) UsageStatsManager(android.app.usage.UsageStatsManager) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

UsageStats (android.app.usage.UsageStats)1 UsageStatsManager (android.app.usage.UsageStatsManager)1 AppHibernationManager (android.apphibernation.AppHibernationManager)1 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 ArrayMap (android.util.ArrayMap)1 WorkerThread (androidx.annotation.WorkerThread)1