Search in sources :

Example 46 with UsageStats

use of android.app.usage.UsageStats in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class RecentAppStatsMixinTest method loadDisplayableRecentApps_withNullAppEntryOrInfo_shouldNotCrash.

@Test
public void loadDisplayableRecentApps_withNullAppEntryOrInfo_shouldNotCrash() {
    final List<UsageStats> stats = new ArrayList<>();
    final UsageStats stat1 = new UsageStats();
    final UsageStats stat2 = new UsageStats();
    stat1.mLastTimeUsed = System.currentTimeMillis();
    stat1.mPackageName = "pkg.class";
    stats.add(stat1);
    stat2.mLastTimeUsed = System.currentTimeMillis();
    stat2.mPackageName = "pkg.class2";
    stats.add(stat2);
    // app1 has AppEntry with null info, app2 has null AppEntry.
    mAppEntry.info = null;
    when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())).thenReturn(mAppEntry);
    when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId())).thenReturn(null);
    when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())).thenReturn(stats);
    // We should not crash here.
    mRecentAppStatsMixin.loadDisplayableRecentApps(3);
}
Also used : ArrayList(java.util.ArrayList) UsageStats(android.app.usage.UsageStats) Test(org.junit.Test)

Example 47 with UsageStats

use of android.app.usage.UsageStats in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class RecentAppStatsMixin method loadDisplayableRecentApps.

@VisibleForTesting
void loadDisplayableRecentApps(int number) {
    mRecentApps.clear();
    mCalendar = Calendar.getInstance();
    mCalendar.add(Calendar.DAY_OF_YEAR, -1);
    final List<UsageStats> mStats = mPowerManager.isPowerSaveMode() ? new ArrayList<>() : mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, mCalendar.getTimeInMillis(), System.currentTimeMillis());
    final Map<String, UsageStats> map = new ArrayMap<>();
    final int statCount = mStats.size();
    for (int i = 0; i < statCount; i++) {
        final UsageStats pkgStats = mStats.get(i);
        if (!shouldIncludePkgInRecents(pkgStats)) {
            continue;
        }
        final String pkgName = pkgStats.getPackageName();
        final UsageStats existingStats = map.get(pkgName);
        if (existingStats == null) {
            map.put(pkgName, pkgStats);
        } else {
            existingStats.add(pkgStats);
        }
    }
    final List<UsageStats> packageStats = new ArrayList<>();
    packageStats.addAll(map.values());
    Collections.sort(packageStats, this);
    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;
        }
    }
}
Also used : ApplicationsState(com.android.settingslib.applications.ApplicationsState) ArrayList(java.util.ArrayList) ArrayMap(android.util.ArrayMap) UsageStats(android.app.usage.UsageStats) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 48 with UsageStats

use of android.app.usage.UsageStats in project platform_frameworks_base by android.

the class IntervalStats method getOrCreateUsageStats.

/**
     * Gets the UsageStats object for the given package, or creates one and adds it internally.
     */
UsageStats getOrCreateUsageStats(String packageName) {
    UsageStats usageStats = packageStats.get(packageName);
    if (usageStats == null) {
        usageStats = new UsageStats();
        usageStats.mPackageName = getCachedStringRef(packageName);
        usageStats.mBeginTimeStamp = beginTime;
        usageStats.mEndTimeStamp = endTime;
        packageStats.put(usageStats.mPackageName, usageStats);
    }
    return usageStats;
}
Also used : UsageStats(android.app.usage.UsageStats)

Example 49 with UsageStats

use of android.app.usage.UsageStats in project platform_frameworks_base by android.

the class UserUsageStatsService method printIntervalStats.

void printIntervalStats(IndentingPrintWriter pw, IntervalStats stats, boolean prettyDates) {
    if (prettyDates) {
        pw.printPair("timeRange", "\"" + DateUtils.formatDateRange(mContext, stats.beginTime, stats.endTime, sDateFormatFlags) + "\"");
    } else {
        pw.printPair("beginTime", stats.beginTime);
        pw.printPair("endTime", stats.endTime);
    }
    pw.println();
    pw.increaseIndent();
    pw.println("packages");
    pw.increaseIndent();
    final ArrayMap<String, UsageStats> pkgStats = stats.packageStats;
    final int pkgCount = pkgStats.size();
    for (int i = 0; i < pkgCount; i++) {
        final UsageStats usageStats = pkgStats.valueAt(i);
        pw.printPair("package", usageStats.mPackageName);
        pw.printPair("totalTime", formatElapsedTime(usageStats.mTotalTimeInForeground, prettyDates));
        pw.printPair("lastTime", formatDateTime(usageStats.mLastTimeUsed, prettyDates));
        pw.println();
    }
    pw.decreaseIndent();
    pw.println("configurations");
    pw.increaseIndent();
    final ArrayMap<Configuration, ConfigurationStats> configStats = stats.configurations;
    final int configCount = configStats.size();
    for (int i = 0; i < configCount; i++) {
        final ConfigurationStats config = configStats.valueAt(i);
        pw.printPair("config", Configuration.resourceQualifierString(config.mConfiguration));
        pw.printPair("totalTime", formatElapsedTime(config.mTotalTimeActive, prettyDates));
        pw.printPair("lastTime", formatDateTime(config.mLastTimeActive, prettyDates));
        pw.printPair("count", config.mActivationCount);
        pw.println();
    }
    pw.decreaseIndent();
    pw.println("events");
    pw.increaseIndent();
    final TimeSparseArray<UsageEvents.Event> events = stats.events;
    final int eventCount = events != null ? events.size() : 0;
    for (int i = 0; i < eventCount; i++) {
        final UsageEvents.Event event = events.valueAt(i);
        pw.printPair("time", formatDateTime(event.mTimeStamp, prettyDates));
        pw.printPair("type", eventToString(event.mEventType));
        pw.printPair("package", event.mPackage);
        if (event.mClass != null) {
            pw.printPair("class", event.mClass);
        }
        if (event.mConfiguration != null) {
            pw.printPair("config", Configuration.resourceQualifierString(event.mConfiguration));
        }
        if (event.mShortcutId != null) {
            pw.printPair("shortcutId", event.mShortcutId);
        }
        pw.println();
    }
    pw.decreaseIndent();
    pw.decreaseIndent();
}
Also used : ConfigurationStats(android.app.usage.ConfigurationStats) Configuration(android.content.res.Configuration) Event(android.app.usage.UsageEvents.Event) Event(android.app.usage.UsageEvents.Event) UsageStats(android.app.usage.UsageStats) UsageEvents(android.app.usage.UsageEvents)

Example 50 with UsageStats

use of android.app.usage.UsageStats in project platform_frameworks_base by android.

the class UserUsageStatsService method rolloverStats.

private void rolloverStats(final long currentTimeMillis) {
    final long startTime = SystemClock.elapsedRealtime();
    Slog.i(TAG, mLogPrefix + "Rolling over usage stats");
    // Finish any ongoing events with an END_OF_DAY event. Make a note of which components
    // need a new CONTINUE_PREVIOUS_DAY entry.
    final Configuration previousConfig = mCurrentStats[UsageStatsManager.INTERVAL_DAILY].activeConfiguration;
    ArraySet<String> continuePreviousDay = new ArraySet<>();
    for (IntervalStats stat : mCurrentStats) {
        final int pkgCount = stat.packageStats.size();
        for (int i = 0; i < pkgCount; i++) {
            UsageStats pkgStats = stat.packageStats.valueAt(i);
            if (pkgStats.mLastEvent == UsageEvents.Event.MOVE_TO_FOREGROUND || pkgStats.mLastEvent == UsageEvents.Event.CONTINUE_PREVIOUS_DAY) {
                continuePreviousDay.add(pkgStats.mPackageName);
                stat.update(pkgStats.mPackageName, mDailyExpiryDate.getTimeInMillis() - 1, UsageEvents.Event.END_OF_DAY);
                notifyStatsChanged();
            }
        }
        stat.updateConfigurationStats(null, mDailyExpiryDate.getTimeInMillis() - 1);
    }
    persistActiveStats();
    mDatabase.prune(currentTimeMillis);
    loadActiveStats(currentTimeMillis);
    final int continueCount = continuePreviousDay.size();
    for (int i = 0; i < continueCount; i++) {
        String name = continuePreviousDay.valueAt(i);
        final long beginTime = mCurrentStats[UsageStatsManager.INTERVAL_DAILY].beginTime;
        for (IntervalStats stat : mCurrentStats) {
            stat.update(name, beginTime, UsageEvents.Event.CONTINUE_PREVIOUS_DAY);
            stat.updateConfigurationStats(previousConfig, beginTime);
            notifyStatsChanged();
        }
    }
    persistActiveStats();
    final long totalTime = SystemClock.elapsedRealtime() - startTime;
    Slog.i(TAG, mLogPrefix + "Rolling over usage stats complete. Took " + totalTime + " milliseconds");
}
Also used : ArraySet(android.util.ArraySet) Configuration(android.content.res.Configuration) 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