Search in sources :

Example 96 with UsageStats

use of android.app.usage.UsageStats in project android_packages_apps_Settings by omnirom.

the class AppsPreferenceController method displayRecentApps.

private void displayRecentApps() {
    if (mRecentAppsCategory != null) {
        final Map<String, Preference> existedAppPreferences = new ArrayMap<>();
        final int prefCount = mRecentAppsCategory.getPreferenceCount();
        for (int i = 0; i < prefCount; i++) {
            final Preference pref = mRecentAppsCategory.getPreference(i);
            final String key = pref.getKey();
            if (!TextUtils.equals(key, KEY_SEE_ALL)) {
                existedAppPreferences.put(key, pref);
            }
        }
        int showAppsCount = 0;
        for (UsageStats stat : mRecentApps) {
            final String pkgName = stat.getPackageName();
            final ApplicationsState.AppEntry appEntry = mApplicationsState.getEntry(pkgName, mUserId);
            if (appEntry == null) {
                continue;
            }
            boolean rebindPref = true;
            Preference pref = existedAppPreferences.remove(pkgName);
            if (pref == null) {
                pref = new AppPreference(mContext);
                rebindPref = false;
            }
            pref.setKey(pkgName);
            pref.setTitle(appEntry.label);
            pref.setIcon(Utils.getBadgedIcon(mContext, appEntry.info));
            pref.setSummary(StringUtil.formatRelativeTime(mContext, System.currentTimeMillis() - stat.getLastTimeUsed(), false, RelativeDateTimeFormatter.Style.SHORT));
            pref.setOrder(showAppsCount++);
            pref.setOnPreferenceClickListener(preference -> {
                AppInfoBase.startAppInfoFragment(AppInfoDashboardFragment.class, R.string.application_info_label, pkgName, appEntry.info.uid, mHost, 1001, /*RequestCode*/
                getMetricsCategory());
                return true;
            });
            if (!rebindPref) {
                mRecentAppsCategory.addPreference(pref);
            }
        }
        // Remove unused preferences from pref category.
        for (Preference unusedPref : existedAppPreferences.values()) {
            mRecentAppsCategory.removePreference(unusedPref);
        }
    }
}
Also used : AppPreference(com.android.settingslib.widget.AppPreference) Preference(androidx.preference.Preference) AppPreference(com.android.settingslib.widget.AppPreference) ApplicationsState(com.android.settingslib.applications.ApplicationsState) ArrayMap(android.util.ArrayMap) UsageStats(android.app.usage.UsageStats)

Example 97 with UsageStats

use of android.app.usage.UsageStats in project android_packages_apps_Settings by omnirom.

the class HibernatedAppsPreferenceControllerTest method setAutoRevokedPackageUsageStats.

private void setAutoRevokedPackageUsageStats() {
    final UsageStats us = new UsageStats();
    us.mPackageName = AUTO_REVOKED_PACKAGE_NAME;
    us.mLastTimeVisible = System.currentTimeMillis();
    try {
        when(mIUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong(), anyString(), anyInt())).thenReturn(new ParceledListSlice(Arrays.asList(us)));
    } catch (RemoteException e) {
    // no-op
    }
}
Also used : UsageStats(android.app.usage.UsageStats) RemoteException(android.os.RemoteException) ParceledListSlice(android.content.pm.ParceledListSlice)

Example 98 with UsageStats

use of android.app.usage.UsageStats in project Taskbar by farmerbb.

the class TaskbarController method getAppEntriesUsingUsageStats.

@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private List<AppEntry> getAppEntriesUsingUsageStats() {
    UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
    List<UsageStats> usageStatsList = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, searchInterval, System.currentTimeMillis());
    List<AppEntry> entries = new ArrayList<>();
    for (UsageStats usageStats : usageStatsList) {
        AppEntry newEntry = new AppEntry(usageStats.getPackageName(), null, null, null, false);
        newEntry.setTotalTimeInForeground(usageStats.getTotalTimeInForeground());
        newEntry.setLastTimeUsed(usageStats.getLastTimeUsed());
        entries.add(newEntry);
    }
    return entries;
}
Also used : AppEntry(com.farmerbb.taskbar.util.AppEntry) ArrayList(java.util.ArrayList) UsageStatsManager(android.app.usage.UsageStatsManager) UsageStats(android.app.usage.UsageStats) TargetApi(android.annotation.TargetApi)

Aggregations

UsageStats (android.app.usage.UsageStats)98 ArrayList (java.util.ArrayList)45 Test (org.junit.Test)38 Intent (android.content.Intent)35 ResolveInfo (android.content.pm.ResolveInfo)33 Configuration (android.content.res.Configuration)20 ApplicationsState (com.android.settingslib.applications.ApplicationsState)19 Preference (android.support.v7.preference.Preference)18 ArrayMap (android.util.ArrayMap)16 UsageStatsManager (android.app.usage.UsageStatsManager)13 ApplicationInfo (android.content.pm.ApplicationInfo)7 PackageManager (android.content.pm.PackageManager)6 ActivityManager (android.app.ActivityManager)5 AppOpsManager (android.app.AppOpsManager)5 ResolvedComponentInfo (com.android.internal.app.ResolverActivity.ResolvedComponentInfo)5 TargetApi (android.annotation.TargetApi)4 ConfigurationStats (android.app.usage.ConfigurationStats)4 UsageEvents (android.app.usage.UsageEvents)4 Event (android.app.usage.UsageEvents.Event)4 ArraySet (android.util.ArraySet)4