Load work apps in RecentAppStatsMixin

This change will show work apps and personal apps together in the
recent apps settings page, sorted by decreasing last usage time.

Test: see both work and personal apps in the recent apps page in the
correct order.
Fix: 146921442

Change-Id: I174a556010529bc39c085cc006722bc2947535bd
This commit is contained in:
Liahav Eitan
2022-05-20 11:53:11 +00:00
parent 7cebb74e17
commit 82e81e23e0
4 changed files with 214 additions and 65 deletions

View File

@@ -20,7 +20,6 @@ import android.app.Application;
import android.app.usage.UsageStats; import android.app.usage.UsageStats;
import android.content.Context; import android.content.Context;
import android.icu.text.RelativeDateTimeFormatter; import android.icu.text.RelativeDateTimeFormatter;
import android.os.UserHandle;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.ArrayMap; import android.util.ArrayMap;
@@ -63,10 +62,9 @@ public class AppsPreferenceController extends BasePreferenceController implement
static final String KEY_SEE_ALL = "see_all_apps"; static final String KEY_SEE_ALL = "see_all_apps";
private final ApplicationsState mApplicationsState; private final ApplicationsState mApplicationsState;
private final int mUserId;
@VisibleForTesting @VisibleForTesting
List<UsageStats> mRecentApps; List<RecentAppStatsMixin.UsageStatsWrapper> mRecentApps;
@VisibleForTesting @VisibleForTesting
PreferenceCategory mRecentAppsCategory; PreferenceCategory mRecentAppsCategory;
@VisibleForTesting @VisibleForTesting
@@ -83,7 +81,6 @@ public class AppsPreferenceController extends BasePreferenceController implement
super(context, KEY_RECENT_APPS_CATEGORY); super(context, KEY_RECENT_APPS_CATEGORY);
mApplicationsState = ApplicationsState.getInstance( mApplicationsState = ApplicationsState.getInstance(
(Application) mContext.getApplicationContext()); (Application) mContext.getApplicationContext());
mUserId = UserHandle.myUserId();
} }
public void setFragment(Fragment fragment) { public void setFragment(Fragment fragment) {
@@ -156,7 +153,7 @@ public class AppsPreferenceController extends BasePreferenceController implement
} }
@VisibleForTesting @VisibleForTesting
List<UsageStats> loadRecentApps() { List<RecentAppStatsMixin.UsageStatsWrapper> loadRecentApps() {
final RecentAppStatsMixin recentAppStatsMixin = new RecentAppStatsMixin(mContext, final RecentAppStatsMixin recentAppStatsMixin = new RecentAppStatsMixin(mContext,
SHOW_RECENT_APP_COUNT); SHOW_RECENT_APP_COUNT);
recentAppStatsMixin.loadDisplayableRecentApps(SHOW_RECENT_APP_COUNT); recentAppStatsMixin.loadDisplayableRecentApps(SHOW_RECENT_APP_COUNT);
@@ -187,26 +184,28 @@ public class AppsPreferenceController extends BasePreferenceController implement
} }
int showAppsCount = 0; int showAppsCount = 0;
for (UsageStats stat : mRecentApps) { for (RecentAppStatsMixin.UsageStatsWrapper statsWrapper : mRecentApps) {
final String pkgName = stat.getPackageName(); final UsageStats stats = statsWrapper.mUsageStats;
final String pkgName = statsWrapper.mUsageStats.getPackageName();
final String key = pkgName + statsWrapper.mUserId;
final ApplicationsState.AppEntry appEntry = final ApplicationsState.AppEntry appEntry =
mApplicationsState.getEntry(pkgName, mUserId); mApplicationsState.getEntry(pkgName, statsWrapper.mUserId);
if (appEntry == null) { if (appEntry == null) {
continue; continue;
} }
boolean rebindPref = true; boolean rebindPref = true;
Preference pref = existedAppPreferences.remove(pkgName); Preference pref = existedAppPreferences.remove(key);
if (pref == null) { if (pref == null) {
pref = new AppPreference(mContext); pref = new AppPreference(mContext);
rebindPref = false; rebindPref = false;
} }
pref.setKey(pkgName); pref.setKey(key);
pref.setTitle(appEntry.label); pref.setTitle(appEntry.label);
pref.setIcon(Utils.getBadgedIcon(mContext, appEntry.info)); pref.setIcon(Utils.getBadgedIcon(mContext, appEntry.info));
pref.setSummary(StringUtil.formatRelativeTime(mContext, pref.setSummary(StringUtil.formatRelativeTime(mContext,
System.currentTimeMillis() - stat.getLastTimeUsed(), false, System.currentTimeMillis() - stats.getLastTimeUsed(), false,
RelativeDateTimeFormatter.Style.SHORT)); RelativeDateTimeFormatter.Style.SHORT));
pref.setOrder(showAppsCount++); pref.setOrder(showAppsCount++);
pref.setOnPreferenceClickListener(preference -> { pref.setOnPreferenceClickListener(preference -> {

View File

@@ -26,6 +26,7 @@ import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager;
import android.util.ArrayMap; import android.util.ArrayMap;
import android.util.ArraySet; import android.util.ArraySet;
import android.util.Log; import android.util.Log;
@@ -33,6 +34,7 @@ import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import com.android.settings.Utils;
import com.android.settingslib.applications.AppUtils; import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.applications.ApplicationsState; import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.LifecycleObserver;
@@ -42,26 +44,31 @@ import com.android.settingslib.utils.ThreadUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
/**
public class RecentAppStatsMixin implements Comparator<UsageStats>, LifecycleObserver, OnStart { * A helper class that loads recent app data in the background and sends it in a callback to a
* listener.
*/
public class RecentAppStatsMixin implements LifecycleObserver, OnStart {
private static final String TAG = "RecentAppStatsMixin"; private static final String TAG = "RecentAppStatsMixin";
private static final Set<String> SKIP_SYSTEM_PACKAGES = new ArraySet<>(); private static final Set<String> SKIP_SYSTEM_PACKAGES = new ArraySet<>();
@VisibleForTesting @VisibleForTesting
final List<UsageStats> mRecentApps; List<UsageStatsWrapper> mRecentApps;
private final int mUserId;
private final int mMaximumApps; private final int mMaximumApps;
private final Context mContext; private final Context mContext;
private final PackageManager mPm; private final PackageManager mPm;
private final PowerManager mPowerManager;; private final PowerManager mPowerManager;
private final UsageStatsManager mUsageStatsManager; private final int mWorkUserId;
private final UsageStatsManager mPersonalUsageStatsManager;
private final Optional<UsageStatsManager> mWorkUsageStatsManager;
private final ApplicationsState mApplicationsState; private final ApplicationsState mApplicationsState;
private final List<RecentAppStatsListener> mAppStatsListeners; private final List<RecentAppStatsListener> mAppStatsListeners;
private Calendar mCalendar; private Calendar mCalendar;
@@ -80,10 +87,15 @@ public class RecentAppStatsMixin implements Comparator<UsageStats>, LifecycleObs
public RecentAppStatsMixin(Context context, int maximumApps) { public RecentAppStatsMixin(Context context, int maximumApps) {
mContext = context; mContext = context;
mMaximumApps = maximumApps; mMaximumApps = maximumApps;
mUserId = UserHandle.myUserId();
mPm = mContext.getPackageManager(); mPm = mContext.getPackageManager();
mPowerManager = mContext.getSystemService(PowerManager.class); mPowerManager = mContext.getSystemService(PowerManager.class);
mUsageStatsManager = mContext.getSystemService(UsageStatsManager.class); final UserManager userManager = mContext.getSystemService(UserManager.class);
mWorkUserId = Utils.getManagedProfileId(userManager, UserHandle.myUserId());
mPersonalUsageStatsManager = mContext.getSystemService(UsageStatsManager.class);
final UserHandle workUserHandle = Utils.getManagedProfile(userManager);
mWorkUsageStatsManager = Optional.ofNullable(workUserHandle).map(
handle -> mContext.createContextAsUser(handle, /* flags */ 0)
.getSystemService(UsageStatsManager.class));
mApplicationsState = ApplicationsState.getInstance( mApplicationsState = ApplicationsState.getInstance(
(Application) mContext.getApplicationContext()); (Application) mContext.getApplicationContext());
mRecentApps = new ArrayList<>(); mRecentApps = new ArrayList<>();
@@ -100,32 +112,56 @@ public class RecentAppStatsMixin implements Comparator<UsageStats>, LifecycleObs
}); });
} }
@Override
public final int compare(UsageStats a, UsageStats b) {
// return by descending order
return Long.compare(b.getLastTimeUsed(), a.getLastTimeUsed());
}
public void addListener(@NonNull RecentAppStatsListener listener) { public void addListener(@NonNull RecentAppStatsListener listener) {
mAppStatsListeners.add(listener); mAppStatsListeners.add(listener);
} }
@VisibleForTesting @VisibleForTesting
void loadDisplayableRecentApps(int number) { void loadDisplayableRecentApps(int limit) {
mRecentApps.clear(); mRecentApps.clear();
mCalendar = Calendar.getInstance(); mCalendar = Calendar.getInstance();
mCalendar.add(Calendar.DAY_OF_YEAR, -1); mCalendar.add(Calendar.DAY_OF_YEAR, -1);
final List<UsageStats> mStats = mPowerManager.isPowerSaveMode()
final int personalUserId = UserHandle.myUserId();
final List<UsageStats> personalStats =
getRecentAppsStats(mPersonalUsageStatsManager, personalUserId);
final List<UsageStats> workStats = mWorkUsageStatsManager
.map(statsManager -> getRecentAppsStats(statsManager, mWorkUserId))
.orElse(new ArrayList<>());
// Both lists are already sorted, so we can create a sorted merge in linear time
int personal = 0;
int work = 0;
while (personal < personalStats.size() && work < workStats.size()
&& mRecentApps.size() < limit) {
UsageStats currentPersonal = personalStats.get(personal);
UsageStats currentWork = workStats.get(work);
if (currentPersonal.getLastTimeUsed() > currentWork.getLastTimeUsed()) {
mRecentApps.add(new UsageStatsWrapper(currentPersonal, personalUserId));
personal++;
} else {
mRecentApps.add(new UsageStatsWrapper(currentWork, mWorkUserId));
work++;
}
}
while (personal < personalStats.size() && mRecentApps.size() < limit) {
mRecentApps.add(new UsageStatsWrapper(personalStats.get(personal++), personalUserId));
}
while (work < workStats.size() && mRecentApps.size() < limit) {
mRecentApps.add(new UsageStatsWrapper(workStats.get(work++), mWorkUserId));
}
}
private List<UsageStats> getRecentAppsStats(UsageStatsManager usageStatsManager, int userId) {
final List<UsageStats> recentAppStats = mPowerManager.isPowerSaveMode()
? new ArrayList<>() ? new ArrayList<>()
: mUsageStatsManager.queryUsageStats( : usageStatsManager.queryUsageStats(
UsageStatsManager.INTERVAL_BEST, mCalendar.getTimeInMillis(), UsageStatsManager.INTERVAL_BEST, mCalendar.getTimeInMillis(),
System.currentTimeMillis()); System.currentTimeMillis());
final Map<String, UsageStats> map = new ArrayMap<>(); final Map<String, UsageStats> map = new ArrayMap<>();
final int statCount = mStats.size(); for (final UsageStats pkgStats : recentAppStats) {
for (int i = 0; i < statCount; i++) { if (!shouldIncludePkgInRecents(pkgStats, userId)) {
final UsageStats pkgStats = mStats.get(i);
if (!shouldIncludePkgInRecents(pkgStats)) {
continue; continue;
} }
final String pkgName = pkgStats.getPackageName(); final String pkgName = pkgStats.getPackageName();
@@ -136,28 +172,15 @@ public class RecentAppStatsMixin implements Comparator<UsageStats>, LifecycleObs
existingStats.add(pkgStats); existingStats.add(pkgStats);
} }
} }
final List<UsageStats> packageStats = new ArrayList<>(); final List<UsageStats> packageStats = new ArrayList<>(map.values());
packageStats.addAll(map.values()); packageStats.sort(Comparator.comparingLong(UsageStats::getLastTimeUsed).reversed());
Collections.sort(packageStats, this /* comparator */); return packageStats;
int count = 0;
for (UsageStats stat : packageStats) {
final ApplicationsState.AppEntry appEntry = mApplicationsState.getEntry(
stat.getPackageName(), mUserId);
if (appEntry == null) {
continue;
}
mRecentApps.add(stat);
count++;
if (count >= number) {
break;
}
}
} }
/** /**
* Whether or not the app should be included in recent list. * Whether or not the app should be included in recent list.
*/ */
private boolean shouldIncludePkgInRecents(UsageStats stat) { private boolean shouldIncludePkgInRecents(UsageStats stat, int userId) {
final String pkgName = stat.getPackageName(); final String pkgName = stat.getPackageName();
if (stat.getLastTimeUsed() < mCalendar.getTimeInMillis()) { if (stat.getLastTimeUsed() < mCalendar.getTimeInMillis()) {
Log.d(TAG, "Invalid timestamp (usage time is more than 24 hours ago), skipping " Log.d(TAG, "Invalid timestamp (usage time is more than 24 hours ago), skipping "
@@ -169,26 +192,49 @@ public class RecentAppStatsMixin implements Comparator<UsageStats>, LifecycleObs
Log.d(TAG, "System package, skipping " + pkgName); Log.d(TAG, "System package, skipping " + pkgName);
return false; return false;
} }
if (AppUtils.isHiddenSystemModule(mContext, pkgName)) { if (AppUtils.isHiddenSystemModule(mContext, pkgName)) {
return false; return false;
} }
final ApplicationsState.AppEntry appEntry = mApplicationsState.getEntry(pkgName, userId);
if (appEntry == null) {
return false;
}
final Intent launchIntent = new Intent().addCategory(Intent.CATEGORY_LAUNCHER) final Intent launchIntent = new Intent().addCategory(Intent.CATEGORY_LAUNCHER)
.setPackage(pkgName); .setPackage(pkgName);
if (mPm.resolveActivityAsUser(launchIntent, 0, userId) == null) {
if (mPm.resolveActivity(launchIntent, 0) == null) {
// Not visible on launcher -> likely not a user visible app, skip if non-instant. // Not visible on launcher -> likely not a user visible app, skip if non-instant.
final ApplicationsState.AppEntry appEntry = if (appEntry.info == null || !AppUtils.isInstant(appEntry.info)) {
mApplicationsState.getEntry(pkgName, mUserId);
if (appEntry == null || appEntry.info == null || !AppUtils.isInstant(appEntry.info)) {
Log.d(TAG, "Not a user visible or instant app, skipping " + pkgName); Log.d(TAG, "Not a user visible or instant app, skipping " + pkgName);
return false; return false;
} }
} }
return true; return true;
} }
public interface RecentAppStatsListener { public interface RecentAppStatsListener {
void onReloadDataCompleted(List<UsageStats> recentApps); /** A callback after loading the recent app data. */
void onReloadDataCompleted(List<UsageStatsWrapper> recentApps);
}
static class UsageStatsWrapper {
public final UsageStats mUsageStats;
public final int mUserId;
UsageStatsWrapper(UsageStats usageStats, int userId) {
mUsageStats = usageStats;
mUserId = userId;
}
@Override
public String toString() {
return String.format("UsageStatsWrapper(pkg:%s,uid:%s)",
mUsageStats.getPackageName(), mUserId);
}
} }
} }

View File

@@ -70,7 +70,7 @@ public class AppsPreferenceControllerTest {
private PreferenceScreen mScreen; private PreferenceScreen mScreen;
private AppsPreferenceController mController; private AppsPreferenceController mController;
private List<UsageStats> mUsageStats; private List<RecentAppStatsMixin.UsageStatsWrapper> mUsageStats;
private PreferenceCategory mRecentAppsCategory; private PreferenceCategory mRecentAppsCategory;
private PreferenceCategory mGeneralCategory; private PreferenceCategory mGeneralCategory;
private Preference mSeeAllPref; private Preference mSeeAllPref;
@@ -147,15 +147,15 @@ public class AppsPreferenceControllerTest {
final UsageStats stat3 = new UsageStats(); final UsageStats stat3 = new UsageStats();
stat1.mLastTimeUsed = System.currentTimeMillis(); stat1.mLastTimeUsed = System.currentTimeMillis();
stat1.mPackageName = "pkg.class"; stat1.mPackageName = "pkg.class";
mUsageStats.add(stat1); mUsageStats.add(statsWrapperOf(stat1));
stat2.mLastTimeUsed = System.currentTimeMillis(); stat2.mLastTimeUsed = System.currentTimeMillis();
stat2.mPackageName = "pkg.class2"; stat2.mPackageName = "pkg.class2";
mUsageStats.add(stat2); mUsageStats.add(statsWrapperOf(stat2));
stat3.mLastTimeUsed = System.currentTimeMillis(); stat3.mLastTimeUsed = System.currentTimeMillis();
stat3.mPackageName = "pkg.class3"; stat3.mPackageName = "pkg.class3";
mUsageStats.add(stat3); mUsageStats.add(statsWrapperOf(stat3));
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())) when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
.thenReturn(mAppEntry); .thenReturn(mAppEntry);
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId())) when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId()))
@@ -164,4 +164,9 @@ public class AppsPreferenceControllerTest {
.thenReturn(mAppEntry); .thenReturn(mAppEntry);
mAppEntry.info = mApplicationInfo; mAppEntry.info = mApplicationInfo;
} }
private static RecentAppStatsMixin.UsageStatsWrapper statsWrapperOf(
UsageStats stats) {
return new RecentAppStatsMixin.UsageStatsWrapper(stats, /* userId= */ 0);
}
} }

View File

@@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
@@ -53,6 +54,7 @@ import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class RecentAppStatsMixinTest { public class RecentAppStatsMixinTest {
@@ -60,6 +62,8 @@ public class RecentAppStatsMixinTest {
@Mock @Mock
private UsageStatsManager mUsageStatsManager; private UsageStatsManager mUsageStatsManager;
@Mock @Mock
private UsageStatsManager mWorkUsageStatsManager;
@Mock
private UserManager mUserManager; private UserManager mUserManager;
@Mock @Mock
private ApplicationsState mAppState; private ApplicationsState mAppState;
@@ -87,6 +91,8 @@ public class RecentAppStatsMixinTest {
when(mUserManager.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[]{}); when(mUserManager.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[]{});
mRecentAppStatsMixin = new RecentAppStatsMixin(context, 3 /* maximumApps */); mRecentAppStatsMixin = new RecentAppStatsMixin(context, 3 /* maximumApps */);
ReflectionHelpers.setField(mRecentAppStatsMixin, "mWorkUsageStatsManager",
Optional.of(mWorkUsageStatsManager));
} }
@Test @Test
@@ -99,7 +105,7 @@ public class RecentAppStatsMixinTest {
// stat1 is valid app. // stat1 is valid app.
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())) when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
.thenReturn(mAppEntry); .thenReturn(mAppEntry);
when(mPackageManager.resolveActivity(any(Intent.class), anyInt())) when(mPackageManager.resolveActivityAsUser(any(Intent.class), anyInt(), anyInt()))
.thenReturn(new ResolveInfo()); .thenReturn(new ResolveInfo());
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats); .thenReturn(stats);
@@ -134,7 +140,7 @@ public class RecentAppStatsMixinTest {
.thenReturn(mAppEntry); .thenReturn(mAppEntry);
when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId())) when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId()))
.thenReturn(mAppEntry); .thenReturn(mAppEntry);
when(mPackageManager.resolveActivity(any(Intent.class), anyInt())) when(mPackageManager.resolveActivityAsUser(any(Intent.class), anyInt(), anyInt()))
.thenReturn(new ResolveInfo()); .thenReturn(new ResolveInfo());
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats); .thenReturn(stats);
@@ -170,7 +176,7 @@ public class RecentAppStatsMixinTest {
.thenReturn(mAppEntry); .thenReturn(mAppEntry);
when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId())) when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId()))
.thenReturn(null); .thenReturn(null);
when(mPackageManager.resolveActivity(any(Intent.class), anyInt())) when(mPackageManager.resolveActivityAsUser(any(Intent.class), anyInt(), anyInt()))
.thenReturn(new ResolveInfo()); .thenReturn(new ResolveInfo());
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats); .thenReturn(stats);
@@ -272,7 +278,7 @@ public class RecentAppStatsMixinTest {
when(mPackageManager.getInstalledModules(anyInt() /* flags */)) when(mPackageManager.getInstalledModules(anyInt() /* flags */))
.thenReturn(modules); .thenReturn(modules);
when(mPackageManager.resolveActivity(any(Intent.class), anyInt())) when(mPackageManager.resolveActivityAsUser(any(Intent.class), anyInt(), anyInt()))
.thenReturn(new ResolveInfo()); .thenReturn(new ResolveInfo());
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats); .thenReturn(stats);
@@ -296,7 +302,7 @@ public class RecentAppStatsMixinTest {
// stat1, stat2 are valid apps. stat3 is invalid. // stat1, stat2 are valid apps. stat3 is invalid.
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())) when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
.thenReturn(mAppEntry); .thenReturn(mAppEntry);
when(mPackageManager.resolveActivity(any(Intent.class), anyInt())) when(mPackageManager.resolveActivityAsUser(any(Intent.class), anyInt(), anyInt()))
.thenReturn(new ResolveInfo()); .thenReturn(new ResolveInfo());
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats); .thenReturn(stats);
@@ -306,4 +312,97 @@ public class RecentAppStatsMixinTest {
assertThat(mRecentAppStatsMixin.mRecentApps).isEmpty(); assertThat(mRecentAppStatsMixin.mRecentApps).isEmpty();
} }
@Test
public void loadDisplayableRecentApps_usePersonalAndWorkApps_shouldBeSortedByLastTimeUse() {
final List<UsageStats> personalStats = new ArrayList<>();
final UsageStats stats1 = new UsageStats();
final UsageStats stats2 = new UsageStats();
stats1.mLastTimeUsed = System.currentTimeMillis();
stats1.mPackageName = "personal.pkg.class";
personalStats.add(stats1);
stats2.mLastTimeUsed = System.currentTimeMillis() - 5000;
stats2.mPackageName = "personal.pkg.class2";
personalStats.add(stats2);
final List<UsageStats> workStats = new ArrayList<>();
final UsageStats stat3 = new UsageStats();
stat3.mLastTimeUsed = System.currentTimeMillis() - 2000;
stat3.mPackageName = "work.pkg.class3";
workStats.add(stat3);
when(mAppState.getEntry(anyString(), anyInt()))
.thenReturn(mAppEntry);
when(mPackageManager.resolveActivityAsUser(any(Intent.class), anyInt(), anyInt()))
.thenReturn(new ResolveInfo());
// personal app stats
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(personalStats);
// work app stats
when(mWorkUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(workStats);
mAppEntry.info = mApplicationInfo;
mRecentAppStatsMixin.loadDisplayableRecentApps(3);
assertThat(mRecentAppStatsMixin.mRecentApps.size()).isEqualTo(3);
assertThat(mRecentAppStatsMixin.mRecentApps.get(0).mUsageStats.mPackageName).isEqualTo(
"personal.pkg.class");
assertThat(mRecentAppStatsMixin.mRecentApps.get(1).mUsageStats.mPackageName).isEqualTo(
"work.pkg.class3");
assertThat(mRecentAppStatsMixin.mRecentApps.get(2).mUsageStats.mPackageName).isEqualTo(
"personal.pkg.class2");
}
@Test
public void loadDisplayableRecentApps_usePersonalAndWorkApps_shouldBeUniquePerProfile() {
final String firstAppPackageName = "app1.pkg.class";
final String secondAppPackageName = "app2.pkg.class";
final List<UsageStats> personalStats = new ArrayList<>();
final UsageStats personalStatsFirstApp = new UsageStats();
final UsageStats personalStatsFirstAppOlderUse = new UsageStats();
final UsageStats personalStatsSecondApp = new UsageStats();
personalStatsFirstApp.mLastTimeUsed = System.currentTimeMillis();
personalStatsFirstApp.mPackageName = firstAppPackageName;
personalStats.add(personalStatsFirstApp);
personalStatsFirstAppOlderUse.mLastTimeUsed = System.currentTimeMillis() - 5000;
personalStatsFirstAppOlderUse.mPackageName = firstAppPackageName;
personalStats.add(personalStatsFirstAppOlderUse);
personalStatsSecondApp.mLastTimeUsed = System.currentTimeMillis() - 2000;
personalStatsSecondApp.mPackageName = secondAppPackageName;
personalStats.add(personalStatsSecondApp);
final List<UsageStats> workStats = new ArrayList<>();
final UsageStats workStatsSecondApp = new UsageStats();
workStatsSecondApp.mLastTimeUsed = System.currentTimeMillis() - 1000;
workStatsSecondApp.mPackageName = secondAppPackageName;
workStats.add(workStatsSecondApp);
when(mAppState.getEntry(anyString(), anyInt()))
.thenReturn(mAppEntry);
when(mPackageManager.resolveActivityAsUser(any(Intent.class), anyInt(), anyInt()))
.thenReturn(new ResolveInfo());
// personal app stats
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(personalStats);
// work app stats
when(mWorkUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(workStats);
mAppEntry.info = mApplicationInfo;
mRecentAppStatsMixin.loadDisplayableRecentApps(3);
// The output should have the first app once since the duplicate use in the personal profile
// is filtered out, and the second app twice - once for each profile.
assertThat(mRecentAppStatsMixin.mRecentApps.size()).isEqualTo(3);
assertThat(mRecentAppStatsMixin.mRecentApps.get(0).mUsageStats.mPackageName).isEqualTo(
firstAppPackageName);
assertThat(mRecentAppStatsMixin.mRecentApps.get(1).mUsageStats.mPackageName).isEqualTo(
secondAppPackageName);
assertThat(mRecentAppStatsMixin.mRecentApps.get(2).mUsageStats.mPackageName).isEqualTo(
secondAppPackageName);
}
} }