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);
}
}
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);
}
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")));
}
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);
}
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")));
}
Aggregations