Search in sources :

Example 81 with UsageStats

use of android.app.usage.UsageStats in project platform_packages_apps_Settings by BlissRoms.

the class RecentAppsPreferenceControllerTest method display_hasRecentButNoneDisplayable_showAppInfo.

@Test
public void display_hasRecentButNoneDisplayable_showAppInfo() {
    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();
    stat1.mLastTimeUsed = System.currentTimeMillis();
    stat1.mPackageName = "com.android.phone";
    stats.add(stat1);
    stat2.mLastTimeUsed = System.currentTimeMillis();
    stat2.mPackageName = "com.android.settings";
    stats.add(stat2);
    // stat1, stat2 are not displayable
    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(mMockContext.getPackageManager().resolveActivity(any(Intent.class), anyInt())).thenReturn(new ResolveInfo());
    when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())).thenReturn(stats);
    mController = new RecentAppsPreferenceController(mMockContext, mAppState, null);
    mController.displayPreference(mScreen);
    verify(mCategory, never()).addPreference(any(Preference.class));
    verify(mCategory).setTitle(null);
    verify(mSeeAllPref).setTitle(R.string.applications_settings);
    verify(mSeeAllPref).setIcon(null);
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) Preference(android.support.v7.preference.Preference) ArrayList(java.util.ArrayList) Intent(android.content.Intent) UsageStats(android.app.usage.UsageStats) Test(org.junit.Test)

Example 82 with UsageStats

use of android.app.usage.UsageStats in project platform_packages_apps_Settings by BlissRoms.

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 83 with UsageStats

use of android.app.usage.UsageStats in project platform_packages_apps_Settings by BlissRoms.

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 84 with UsageStats

use of android.app.usage.UsageStats in project XUtil by xuexiangjys.

the class ProcessUtils method getForegroundProcessName.

/**
 * 获取前台线程包名
 * <p>当不是查看当前 App,且 SDK 大于 21 时,
 * 需添加权限
 * {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />}</p>
 *
 * @return 前台应用包名
 */
public static String getForegroundProcessName() {
    ActivityManager manager = (ActivityManager) XUtil.getContext().getSystemService(Context.ACTIVITY_SERVICE);
    if (manager == null)
        return null;
    List<ActivityManager.RunningAppProcessInfo> pInfo = manager.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 packageManager = XUtil.getContext().getPackageManager();
        Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        Log.i("ProcessUtils", list.toString());
        if (list.size() <= 0) {
            Log.i("ProcessUtils", "getForegroundProcessName() called" + ": 无\"有权查看使用权限的应用\"选项");
            return null;
        }
        try {
            // 有"有权查看使用权限的应用"选项
            ApplicationInfo info = packageManager.getApplicationInfo(XUtil.getContext().getPackageName(), 0);
            AppOpsManager aom = (AppOpsManager) XUtil.getContext().getSystemService(Context.APP_OPS_SERVICE);
            if (aom != null) {
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    XUtil.getContext().startActivity(intent);
                }
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    Log.i("ProcessUtils", "没有打开\"有权查看使用权限的应用\"选项");
                    return null;
                }
            }
            UsageStatsManager usageStatsManager = (UsageStatsManager) XUtil.getContext().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 null;
            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 null;
}
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 85 with UsageStats

use of android.app.usage.UsageStats in project platform_packages_apps_Settings by BlissRoms.

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)

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