Search in sources :

Example 6 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 7 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 8 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 9 with UsageStats

use of android.app.usage.UsageStats in project AndroidProcess by wenmingvs.

the class BackgroundUtil method queryUsageStats.

/**
     * 方法4:通过使用UsageStatsManager获取,此方法是ndroid5.0A之后提供的API
     * 必须:
     * 1. 此方法只在android5.0以上有效
     * 2. AndroidManifest中加入此权限<uses-permission xmlns:tools="http://schemas.android.com/tools" android:name="android.permission.PACKAGE_USAGE_STATS"
     * tools:ignore="ProtectedPermissions" />
     * 3. 打开手机设置,点击安全-高级,在有权查看使用情况的应用中,为这个App打上勾
     *
     * @param context     上下文参数
     * @param packageName 需要检查是否位于栈顶的App的包名
     * @return
     */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static boolean queryUsageStats(Context context, String packageName) {
    class RecentUseComparator implements Comparator<UsageStats> {

        @Override
        public int compare(UsageStats lhs, UsageStats rhs) {
            return (lhs.getLastTimeUsed() > rhs.getLastTimeUsed()) ? -1 : (lhs.getLastTimeUsed() == rhs.getLastTimeUsed()) ? 0 : 1;
        }
    }
    RecentUseComparator mRecentComp = new RecentUseComparator();
    long ts = System.currentTimeMillis();
    UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService("usagestats");
    List<UsageStats> usageStats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, ts - 1000 * 10, ts);
    if (usageStats == null || usageStats.size() == 0) {
        if (HavaPermissionForTest(context) == false) {
            Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            Toast.makeText(context, "权限不够\n请打开手机设置,点击安全-高级,在有权查看使用情况的应用中,为这个App打上勾", Toast.LENGTH_SHORT).show();
        }
        return false;
    }
    Collections.sort(usageStats, mRecentComp);
    String currentTopPackage = usageStats.get(0).getPackageName();
    if (currentTopPackage.equals(packageName)) {
        return true;
    } else {
        return false;
    }
}
Also used : UsageStatsManager(android.app.usage.UsageStatsManager) Intent(android.content.Intent) UsageStats(android.app.usage.UsageStats) Comparator(java.util.Comparator) TargetApi(android.annotation.TargetApi)

Example 10 with UsageStats

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

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)

Aggregations

UsageStats (android.app.usage.UsageStats)31 Configuration (android.content.res.Configuration)8 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 UsageStatsManager (android.app.usage.UsageStatsManager)2 Intent (android.content.Intent)2 TargetApi (android.annotation.TargetApi)1 ActivityManager (android.app.ActivityManager)1 AppOpsManager (android.app.AppOpsManager)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageManager (android.content.pm.PackageManager)1 ResolveInfo (android.content.pm.ResolveInfo)1 Comparator (java.util.Comparator)1