Search in sources :

Example 91 with UsageStats

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();
}
Also used : ArrayList(java.util.ArrayList) UsageStats(android.app.usage.UsageStats) Test(org.junit.Test)

Example 92 with UsageStats

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 "";
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) Intent(android.content.Intent) UsageStats(android.app.usage.UsageStats) ActivityManager(android.app.ActivityManager) ResolveInfo(android.content.pm.ResolveInfo) AppOpsManager(android.app.AppOpsManager) PackageManager(android.content.pm.PackageManager) UsageStatsManager(android.app.usage.UsageStatsManager)

Example 93 with UsageStats

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);
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ArrayList(java.util.ArrayList) Intent(android.content.Intent) UsageStats(android.app.usage.UsageStats) Test(org.junit.Test)

Example 94 with UsageStats

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);
}
Also used : ApplicationsState(com.android.settingslib.applications.ApplicationsState) ArrayList(java.util.ArrayList) ApplicationInfo(android.content.pm.ApplicationInfo) UsageStats(android.app.usage.UsageStats) Test(org.junit.Test)

Example 95 with UsageStats

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();
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) 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