Search in sources :

Example 71 with UsageStats

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

the class RecentAppsPreferenceController method displayRecentApps.

private void displayRecentApps(Context prefContext, List<UsageStats> recentApps) {
    mCategory.setTitle(R.string.recent_app_category_title);
    mDivider.setVisible(true);
    mSeeAllPref.setSummary(null);
    mSeeAllPref.setIcon(R.drawable.ic_chevron_right_24dp);
    // Rebind prefs/avoid adding new prefs if possible. Adding/removing prefs causes jank.
    // Build a cached preference pool
    final Map<String, Preference> appPreferences = new ArrayMap<>();
    int prefCount = mCategory.getPreferenceCount();
    for (int i = 0; i < prefCount; i++) {
        final Preference pref = mCategory.getPreference(i);
        final String key = pref.getKey();
        if (!TextUtils.equals(key, KEY_SEE_ALL)) {
            appPreferences.put(key, pref);
        }
    }
    final int recentAppsCount = recentApps.size();
    for (int i = 0; i < recentAppsCount; i++) {
        final UsageStats stat = recentApps.get(i);
        // Bind recent apps to existing prefs if possible, or create a new pref.
        final String pkgName = stat.getPackageName();
        final ApplicationsState.AppEntry appEntry = mApplicationsState.getEntry(pkgName, mUserId);
        if (appEntry == null) {
            continue;
        }
        boolean rebindPref = true;
        Preference pref = appPreferences.remove(pkgName);
        if (pref == null) {
            pref = new Preference(prefContext);
            rebindPref = false;
        }
        pref.setKey(pkgName);
        pref.setTitle(appEntry.label);
        pref.setIcon(mIconDrawableFactory.getBadgedIcon(appEntry.info));
        pref.setSummary(TextUtils.expandTemplate(mContext.getResources().getText(R.string.recent_app_summary), Utils.formatElapsedTime(mContext, System.currentTimeMillis() - stat.getLastTimeUsed(), false)));
        pref.setOrder(i);
        pref.setOnPreferenceClickListener(preference -> {
            AppInfoBase.startAppInfoFragment(InstalledAppDetails.class, R.string.application_info_label, pkgName, appEntry.info.uid, mHost, 1001, /*RequestCode*/
            SETTINGS_APP_NOTIF_CATEGORY);
            return true;
        });
        if (!rebindPref) {
            mCategory.addPreference(pref);
        }
    }
    // Remove unused prefs from pref cache pool
    for (Preference unusedPrefs : appPreferences.values()) {
        mCategory.removePreference(unusedPrefs);
    }
}
Also used : Preference(android.support.v7.preference.Preference) ApplicationsState(com.android.settingslib.applications.ApplicationsState) ArrayMap(android.util.ArrayMap) UsageStats(android.app.usage.UsageStats)

Example 72 with UsageStats

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

the class RecentAppsPreferenceControllerTest method display_showRecents.

@Test
public void display_showRecents() {
    when(mMockContext.getResources().getBoolean(R.bool.config_display_recent_apps)).thenReturn(true);
    final List<UsageStats> stats = new ArrayList<>();
    final UsageStats stat1 = new UsageStats();
    final UsageStats stat2 = new UsageStats();
    final UsageStats stat3 = new UsageStats();
    stat1.mLastTimeUsed = System.currentTimeMillis();
    stat1.mPackageName = "pkg.class";
    stats.add(stat1);
    stat2.mLastTimeUsed = System.currentTimeMillis();
    stat2.mPackageName = "com.android.settings";
    stats.add(stat2);
    stat3.mLastTimeUsed = System.currentTimeMillis();
    stat3.mPackageName = "pkg.class2";
    stats.add(stat3);
    // stat1, stat2 are valid apps. stat3 is invalid.
    when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())).thenReturn(mock(ApplicationsState.AppEntry.class));
    when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId())).thenReturn(mock(ApplicationsState.AppEntry.class));
    when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId())).thenReturn(null);
    when(mMockContext.getPackageManager().resolveActivity(any(Intent.class), anyInt())).thenReturn(new ResolveInfo());
    when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())).thenReturn(stats);
    final Configuration configuration = new Configuration();
    configuration.locale = Locale.US;
    when(mMockContext.getResources().getConfiguration()).thenReturn(configuration);
    mController = new RecentAppsPreferenceController(mMockContext, mAppState, null);
    mController.displayPreference(mScreen);
    verify(mCategory).setTitle(R.string.recent_app_category_title);
    // Only add stat1. stat2 is skipped because of the package name, stat3 skipped because
    // it's invalid app.
    verify(mCategory, times(1)).addPreference(any(Preference.class));
    verify(mSeeAllPref).setSummary(null);
    verify(mSeeAllPref).setIcon(R.drawable.ic_chevron_right_24dp);
    verify(mDivider).setVisible(true);
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) Configuration(android.content.res.Configuration) Preference(android.support.v7.preference.Preference) ArrayList(java.util.ArrayList) Intent(android.content.Intent) UsageStats(android.app.usage.UsageStats) Test(org.junit.Test)

Example 73 with UsageStats

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

the class RecentAppsPreferenceControllerTest method display_showRecents_formatSummary.

@Test
public void display_showRecents_formatSummary() {
    when(mMockContext.getResources().getBoolean(R.bool.config_display_recent_apps)).thenReturn(true);
    final List<UsageStats> stats = new ArrayList<>();
    final UsageStats stat1 = new UsageStats();
    stat1.mLastTimeUsed = System.currentTimeMillis();
    stat1.mPackageName = "pkg.class";
    stats.add(stat1);
    when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())).thenReturn(mock(ApplicationsState.AppEntry.class));
    when(mMockContext.getPackageManager().resolveActivity(any(Intent.class), anyInt())).thenReturn(new ResolveInfo());
    when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())).thenReturn(stats);
    when(mMockContext.getResources().getText(eq(R.string.recent_app_summary))).thenReturn(mContext.getResources().getText(R.string.recent_app_summary));
    final Configuration configuration = new Configuration();
    configuration.locale = Locale.US;
    when(mMockContext.getResources().getConfiguration()).thenReturn(configuration);
    mController = new RecentAppsPreferenceController(mMockContext, mAppState, null);
    mController.displayPreference(mScreen);
    verify(mCategory).addPreference(argThat(summaryMatches("0m ago")));
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) Configuration(android.content.res.Configuration) ArrayList(java.util.ArrayList) Intent(android.content.Intent) UsageStats(android.app.usage.UsageStats) Test(org.junit.Test)

Example 74 with UsageStats

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

the class RecentAppsPreferenceControllerTest method display_showRecents.

@Test
public void display_showRecents() {
    when(mMockContext.getResources().getBoolean(R.bool.config_display_recent_apps)).thenReturn(true);
    final List<UsageStats> stats = new ArrayList<>();
    final UsageStats stat1 = new UsageStats();
    final UsageStats stat2 = new UsageStats();
    final UsageStats stat3 = new UsageStats();
    stat1.mLastTimeUsed = System.currentTimeMillis();
    stat1.mPackageName = "pkg.class";
    stats.add(stat1);
    stat2.mLastTimeUsed = System.currentTimeMillis();
    stat2.mPackageName = "com.android.settings";
    stats.add(stat2);
    stat3.mLastTimeUsed = System.currentTimeMillis();
    stat3.mPackageName = "pkg.class2";
    stats.add(stat3);
    // stat1, stat2 are valid apps. stat3 is invalid.
    when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())).thenReturn(mock(ApplicationsState.AppEntry.class));
    when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId())).thenReturn(mock(ApplicationsState.AppEntry.class));
    when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId())).thenReturn(null);
    when(mMockContext.getPackageManager().resolveActivity(any(Intent.class), anyInt())).thenReturn(new ResolveInfo());
    when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())).thenReturn(stats);
    final Configuration configuration = new Configuration();
    configuration.locale = Locale.US;
    when(mMockContext.getResources().getConfiguration()).thenReturn(configuration);
    mController = new RecentAppsPreferenceController(mMockContext, mAppState, null);
    mController.displayPreference(mScreen);
    verify(mCategory).setTitle(R.string.recent_app_category_title);
    // Only add stat1. stat2 is skipped because of the package name, stat3 skipped because
    // it's invalid app.
    verify(mCategory, times(1)).addPreference(any(Preference.class));
    verify(mSeeAllPref).setSummary(null);
    verify(mSeeAllPref).setIcon(R.drawable.ic_chevron_right_24dp);
    verify(mDivider).setVisible(true);
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) Configuration(android.content.res.Configuration) Preference(android.support.v7.preference.Preference) ArrayList(java.util.ArrayList) Intent(android.content.Intent) UsageStats(android.app.usage.UsageStats) Test(org.junit.Test)

Example 75 with UsageStats

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

the class RecentAppsPreferenceControllerTest method display_showRecents_formatSummary.

@Test
public void display_showRecents_formatSummary() {
    when(mMockContext.getResources().getBoolean(R.bool.config_display_recent_apps)).thenReturn(true);
    final List<UsageStats> stats = new ArrayList<>();
    final UsageStats stat1 = new UsageStats();
    stat1.mLastTimeUsed = System.currentTimeMillis();
    stat1.mPackageName = "pkg.class";
    stats.add(stat1);
    when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())).thenReturn(mock(ApplicationsState.AppEntry.class));
    when(mMockContext.getPackageManager().resolveActivity(any(Intent.class), anyInt())).thenReturn(new ResolveInfo());
    when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())).thenReturn(stats);
    when(mMockContext.getResources().getText(eq(R.string.recent_app_summary))).thenReturn(mContext.getResources().getText(R.string.recent_app_summary));
    final Configuration configuration = new Configuration();
    configuration.locale = Locale.US;
    when(mMockContext.getResources().getConfiguration()).thenReturn(configuration);
    mController = new RecentAppsPreferenceController(mMockContext, mAppState, null);
    mController.displayPreference(mScreen);
    verify(mCategory).addPreference(argThat(summaryMatches("0m ago")));
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) Configuration(android.content.res.Configuration) ArrayList(java.util.ArrayList) Intent(android.content.Intent) UsageStats(android.app.usage.UsageStats) Test(org.junit.Test)

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