Search in sources :

Example 1 with UsageStats

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

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

use of android.app.usage.UsageStats in project android_frameworks_base by DirtyUnicorns.

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)

Example 3 with UsageStats

use of android.app.usage.UsageStats in project android_frameworks_base by DirtyUnicorns.

the class UserUsageStatsService method init.

void init(final long currentTimeMillis) {
    mDatabase.init(currentTimeMillis);
    int nullCount = 0;
    for (int i = 0; i < mCurrentStats.length; i++) {
        mCurrentStats[i] = mDatabase.getLatestUsageStats(i);
        if (mCurrentStats[i] == null) {
            // Find out how many intervals we don't have data for.
            // Ideally it should be all or none.
            nullCount++;
        }
    }
    if (nullCount > 0) {
        if (nullCount != mCurrentStats.length) {
            // This is weird, but we shouldn't fail if something like this
            // happens.
            Slog.w(TAG, mLogPrefix + "Some stats have no latest available");
        } else {
        // This must be first boot.
        }
        // By calling loadActiveStats, we will
        // generate new stats for each bucket.
        loadActiveStats(currentTimeMillis);
    } else {
        // Set up the expiry date to be one day from the latest daily stat.
        // This may actually be today and we will rollover on the first event
        // that is reported.
        updateRolloverDeadline();
    }
    // Now close off any events that were open at the time this was saved.
    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) {
                stat.update(pkgStats.mPackageName, stat.lastTimeSaved, UsageEvents.Event.END_OF_DAY);
                notifyStatsChanged();
            }
        }
        stat.updateConfigurationStats(null, stat.lastTimeSaved);
    }
    if (mDatabase.isNewUpdate()) {
        notifyNewUpdate();
    }
}
Also used : UsageStats(android.app.usage.UsageStats)

Example 4 with UsageStats

use of android.app.usage.UsageStats in project android_frameworks_base by DirtyUnicorns.

the class ResolverComparator method compute.

public void compute(List<ResolvedComponentInfo> targets) {
    mScoredTargets.clear();
    final long recentSinceTime = mCurrentTime - RECENCY_TIME_PERIOD;
    long mostRecentlyUsedTime = recentSinceTime + 1;
    long mostTimeSpent = 1;
    int mostLaunched = 1;
    for (ResolvedComponentInfo target : targets) {
        final ScoredTarget scoredTarget = new ScoredTarget(target.getResolveInfoAt(0).activityInfo);
        mScoredTargets.put(target.name, scoredTarget);
        final UsageStats pkStats = mStats.get(target.name.getPackageName());
        if (pkStats != null) {
            // Persistent processes muck this up, so omit them too.
            if (!target.name.getPackageName().equals(mReferrerPackage) && !isPersistentProcess(target)) {
                final long lastTimeUsed = pkStats.getLastTimeUsed();
                scoredTarget.lastTimeUsed = lastTimeUsed;
                if (lastTimeUsed > mostRecentlyUsedTime) {
                    mostRecentlyUsedTime = lastTimeUsed;
                }
            }
            final long timeSpent = pkStats.getTotalTimeInForeground();
            scoredTarget.timeSpent = timeSpent;
            if (timeSpent > mostTimeSpent) {
                mostTimeSpent = timeSpent;
            }
            final int launched = pkStats.mLaunchCount;
            scoredTarget.launchCount = launched;
            if (launched > mostLaunched) {
                mostLaunched = launched;
            }
        }
    }
    if (DEBUG) {
        Log.d(TAG, "compute - mostRecentlyUsedTime: " + mostRecentlyUsedTime + " mostTimeSpent: " + mostTimeSpent + " recentSinceTime: " + recentSinceTime + " mostLaunched: " + mostLaunched);
    }
    for (ScoredTarget target : mScoredTargets.values()) {
        final float recency = (float) Math.max(target.lastTimeUsed - recentSinceTime, 0) / (mostRecentlyUsedTime - recentSinceTime);
        final float recencyScore = recency * recency * RECENCY_MULTIPLIER;
        final float usageTimeScore = (float) target.timeSpent / mostTimeSpent;
        final float launchCountScore = (float) target.launchCount / mostLaunched;
        target.score = recencyScore + usageTimeScore + launchCountScore;
        if (DEBUG) {
            Log.d(TAG, "Scores: recencyScore: " + recencyScore + " usageTimeScore: " + usageTimeScore + " launchCountScore: " + launchCountScore + " - " + target);
        }
    }
}
Also used : UsageStats(android.app.usage.UsageStats) ResolvedComponentInfo(com.android.internal.app.ResolverActivity.ResolvedComponentInfo)

Example 5 with UsageStats

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

the class UsageStatsXmlV1 method loadUsageStats.

private static void loadUsageStats(XmlPullParser parser, IntervalStats statsOut) throws IOException {
    final String pkg = parser.getAttributeValue(null, PACKAGE_ATTR);
    if (pkg == null) {
        throw new ProtocolException("no " + PACKAGE_ATTR + " attribute present");
    }
    final UsageStats stats = statsOut.getOrCreateUsageStats(pkg);
    // Apply the offset to the beginTime to find the absolute time.
    stats.mLastTimeUsed = statsOut.beginTime + XmlUtils.readLongAttribute(parser, LAST_TIME_ACTIVE_ATTR);
    stats.mTotalTimeInForeground = XmlUtils.readLongAttribute(parser, TOTAL_TIME_ACTIVE_ATTR);
    stats.mLastEvent = XmlUtils.readIntAttribute(parser, LAST_EVENT_ATTR);
}
Also used : ProtocolException(java.net.ProtocolException) UsageStats(android.app.usage.UsageStats)

Aggregations

UsageStats (android.app.usage.UsageStats)85 ArrayList (java.util.ArrayList)36 Test (org.junit.Test)31 Intent (android.content.Intent)30 ResolveInfo (android.content.pm.ResolveInfo)28 Configuration (android.content.res.Configuration)20 Preference (android.support.v7.preference.Preference)18 ApplicationsState (com.android.settingslib.applications.ApplicationsState)15 ArrayMap (android.util.ArrayMap)13 UsageStatsManager (android.app.usage.UsageStatsManager)11 ApplicationInfo (android.content.pm.ApplicationInfo)6 ActivityManager (android.app.ActivityManager)5 AppOpsManager (android.app.AppOpsManager)5 PackageManager (android.content.pm.PackageManager)5 ResolvedComponentInfo (com.android.internal.app.ResolverActivity.ResolvedComponentInfo)5 ConfigurationStats (android.app.usage.ConfigurationStats)4 UsageEvents (android.app.usage.UsageEvents)4 Event (android.app.usage.UsageEvents.Event)4 ArraySet (android.util.ArraySet)4 ProtocolException (java.net.ProtocolException)4