use of android.app.usage.UsageStats in project Resurrection_packages_apps_Settings by ResurrectionRemix.
the class AllAppsInfoPreferenceControllerTest method onReloadDataCompleted_recentAppsSet_hidePreference.
@Test
public void onReloadDataCompleted_recentAppsSet_hidePreference() {
final List<UsageStats> stats = new ArrayList<>();
final UsageStats stat1 = new UsageStats();
stat1.mLastTimeUsed = System.currentTimeMillis();
stat1.mPackageName = "pkg.class";
stats.add(stat1);
mController.onReloadDataCompleted(stats);
assertThat(mController.mPreference.isVisible()).isFalse();
}
use of android.app.usage.UsageStats in project AndroidUtilCode by Blankj.
the class ProcessUtils method getForegroundProcessName.
/**
* Return the foreground process name.
* <p>Target APIs greater than 21 must hold
* {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />}</p>
*
* @return the foreground process name
*/
public static String getForegroundProcessName() {
ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
// noinspection ConstantConditions
List<ActivityManager.RunningAppProcessInfo> pInfo = am.getRunningAppProcesses();
if (pInfo != null && pInfo.size() > 0) {
for (ActivityManager.RunningAppProcessInfo aInfo : pInfo) {
if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return aInfo.processName;
}
}
}
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
PackageManager pm = Utils.getApp().getPackageManager();
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Log.i("ProcessUtils", list.toString());
if (list.size() <= 0) {
Log.i("ProcessUtils", "getForegroundProcessName: noun of access to usage information.");
return "";
}
try {
// Access to usage information.
ApplicationInfo info = pm.getApplicationInfo(Utils.getApp().getPackageName(), 0);
AppOpsManager aom = (AppOpsManager) Utils.getApp().getSystemService(Context.APP_OPS_SERVICE);
if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Utils.getApp().startActivity(intent);
}
if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
Log.i("ProcessUtils", "getForegroundProcessName: refuse to device usage stats.");
return "";
}
UsageStatsManager usageStatsManager = (UsageStatsManager) Utils.getApp().getSystemService(Context.USAGE_STATS_SERVICE);
List<UsageStats> usageStatsList = null;
if (usageStatsManager != null) {
long endTime = System.currentTimeMillis();
long beginTime = endTime - 86400000 * 7;
usageStatsList = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, beginTime, endTime);
}
if (usageStatsList == null || usageStatsList.isEmpty())
return "";
UsageStats recentStats = null;
for (UsageStats usageStats : usageStatsList) {
if (recentStats == null || usageStats.getLastTimeUsed() > recentStats.getLastTimeUsed()) {
recentStats = usageStats;
}
}
return recentStats == null ? null : recentStats.getPackageName();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
return "";
}
use of android.app.usage.UsageStats in project android_packages_apps_Settings by omnirom.
the class RecentAppStatsMixinTest method loadDisplayableRecentApps_threeValidRecentAppsSet_shouldHaveThreeRecentApps.
@Test
public void loadDisplayableRecentApps_threeValidRecentAppsSet_shouldHaveThreeRecentApps() {
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 = "pkg.class2";
stats.add(stat2);
stat3.mLastTimeUsed = System.currentTimeMillis();
stat3.mPackageName = "pkg.class3";
stats.add(stat3);
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())).thenReturn(mAppEntry);
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId())).thenReturn(mAppEntry);
when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId())).thenReturn(mAppEntry);
when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(new ResolveInfo());
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())).thenReturn(stats);
mAppEntry.info = mApplicationInfo;
mRecentAppStatsMixin.loadDisplayableRecentApps(3);
assertThat(mRecentAppStatsMixin.mRecentApps.size()).isEqualTo(3);
}
use of android.app.usage.UsageStats in project android_packages_apps_Settings by omnirom.
the class RecentAppStatsMixinTest method loadDisplayableRecentApps_oneInstantAppSet_shouldHaveOneRecentApp.
@Test
public void loadDisplayableRecentApps_oneInstantAppSet_shouldHaveOneRecentApp() {
final List<UsageStats> stats = new ArrayList<>();
// Instant app.
final UsageStats stat = new UsageStats();
stat.mLastTimeUsed = System.currentTimeMillis() + 200;
stat.mPackageName = "com.foo.barinstant";
stats.add(stat);
ApplicationsState.AppEntry statEntry = mock(ApplicationsState.AppEntry.class);
statEntry.info = mApplicationInfo;
when(mAppState.getEntry(stat.mPackageName, UserHandle.myUserId())).thenReturn(statEntry);
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())).thenReturn(stats);
// Make sure stat is considered an instant app.
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider", (InstantAppDataProvider) (ApplicationInfo info) -> info == statEntry.info);
mRecentAppStatsMixin.loadDisplayableRecentApps(3);
assertThat(mRecentAppStatsMixin.mRecentApps.size()).isEqualTo(1);
}
use of android.app.usage.UsageStats in project android_packages_apps_Settings by omnirom.
the class RecentAppStatsMixinTest method loadDisplayableRecentApps_powerSaverModeOn_shouldHaveEmptyList.
@Test
public void loadDisplayableRecentApps_powerSaverModeOn_shouldHaveEmptyList() {
when(mPowerManager.isPowerSaveMode()).thenReturn(true);
final List<UsageStats> stats = new ArrayList<>();
final UsageStats stat1 = new UsageStats();
stat1.mLastTimeUsed = System.currentTimeMillis();
stat1.mPackageName = "pkg.class";
stats.add(stat1);
// stat1, stat2 are valid apps. stat3 is invalid.
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())).thenReturn(mAppEntry);
when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(new ResolveInfo());
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())).thenReturn(stats);
mAppEntry.info = mApplicationInfo;
mRecentAppStatsMixin.loadDisplayableRecentApps(3);
assertThat(mRecentAppStatsMixin.mRecentApps).isEmpty();
}
Aggregations