Java Code Examples for android.app.usage.UsageStats#getTotalTimeInForeground()
The following examples show how to use
android.app.usage.UsageStats#getTotalTimeInForeground() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Stats.java From flutter-plugins with MIT License | 6 votes |
/** Produces a map for each installed app package name, * with the corresponding time in foreground in seconds for that application. */ @SuppressWarnings("ResourceType") public static HashMap<String, Double> getUsageMap(Context context, long start, long end) { UsageStatsManager manager = (UsageStatsManager) context.getSystemService("usagestats"); Map<String, UsageStats> usageStatsMap = manager.queryAndAggregateUsageStats(start, end); HashMap<String, Double> usageMap = new HashMap<>(); for (String packageName : usageStatsMap.keySet()) { UsageStats us = usageStatsMap.get(packageName); try { long timeMs = us.getTotalTimeInForeground(); Double timeSeconds = new Double(timeMs / 1000); usageMap.put(packageName, timeSeconds); } catch (Exception e) { Log.d(TAG, "Getting timeInForeground resulted in an exception"); } } return usageMap; }
Example 2
Source File: EventUtils.java From UseTimeStatistic with MIT License | 6 votes |
@SuppressWarnings("ResourceType") @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static ArrayList<UsageStats> getUsageList(Context context, long startTime, long endTime) { Log.i(TAG," EventUtils-getUsageList() Range start:" + startTime); Log.i(TAG," EventUtils-getUsageList() Range end:" + endTime); Log.i(TAG," EventUtils-getUsageList() Range start:" + dateFormat.format(startTime)); Log.i(TAG," EventUtils-getUsageList() Range end:" + dateFormat.format(endTime)); ArrayList<UsageStats> list = new ArrayList<>(); UsageStatsManager mUsmManager = (UsageStatsManager) context.getSystemService("usagestats"); Map<String, UsageStats> map = mUsmManager.queryAndAggregateUsageStats(startTime, endTime); for (Map.Entry<String, UsageStats> entry : map.entrySet()) { UsageStats stats = entry.getValue(); if(stats.getTotalTimeInForeground() > 0){ list.add(stats); Log.i(TAG," EventUtils-getUsageList() stats:" + stats.getPackageName() + " TotalTimeInForeground = " + stats.getTotalTimeInForeground()); } } return list; }
Example 3
Source File: BedtimeNotificationReceiver.java From GotoSleep with GNU General Public License v3.0 | 5 votes |
private boolean isUserActive(long startTime, long currentTime){ String TAG = "isUserActive"; if (currentNotification == 1){ startTime = startTime - notificationDelay * ONE_MINUTE_MILLIS; } //#TODO experiment with using a daily interval (make sure it works past midnight) List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_WEEKLY, startTime, currentTime); UsageStats minUsageStat = null; long min = Long.MAX_VALUE; for (UsageStats usageStat : queryUsageStats){ if ((System.currentTimeMillis() - usageStat.getLastTimeStamp() < min) && (usageStat.getTotalTimeInForeground() > ONE_MINUTE_MILLIS) && !usageStat.getPackageName().equals("com.corvettecole.gotosleep")){ //make sure app has been in foreground for more than one minute to filter out background apps minUsageStat = usageStat; min = System.currentTimeMillis() - usageStat.getLastTimeStamp(); } } if (minUsageStat != null) { Log.d(TAG, minUsageStat.getPackageName() + " last time used: " + minUsageStat.getLastTimeUsed() + " time in foreground: " + minUsageStat.getTotalTimeInForeground()); Log.d(TAG, "getLastTimeStamp: " + minUsageStat.getLastTimeStamp() + " getLastUsed: " + minUsageStat.getLastTimeUsed() + " current time: " + System.currentTimeMillis()); Log.d(TAG, (System.currentTimeMillis() - minUsageStat.getLastTimeUsed() <= userActiveMargin * ONE_MINUTE_MILLIS) + ""); return System.currentTimeMillis() - minUsageStat.getLastTimeStamp() <= userActiveMargin * ONE_MINUTE_MILLIS; } else { Log.e(TAG, "minUsageStat was null!"); Log.e(TAG, queryUsageStats.toString()); return false; } }
Example 4
Source File: UseTimeAdapter.java From UseTimeStatistic with MIT License | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private long getTotalTimeFromUsage(String pkg){ UsageStats stats = mUseTimeDataManager.getUsageStats(pkg); if(stats == null){ return 0; } return stats.getTotalTimeInForeground(); }
Example 5
Source File: Tools.java From Hangar with GNU General Public License v3.0 | 5 votes |
@TargetApi(21) public static LollipopTaskInfo parseUsageStats(List<UsageStats> stats, LollipopTaskInfo lollipopTaskInfo) { UsageStats aRunner = stats.get(0); UsageStats bRunner = null; if (lollipopTaskInfo == null) { // setup new lollipopTaskInfo object! lollipopTaskInfo = new Tools.LollipopTaskInfo(aRunner.getPackageName()); } else if (lollipopTaskInfo.packageName.equals(aRunner.getPackageName())) { // Tools.HangarLog("Last package same as current top, skipping! [" + lollipopTaskInfo.packageName + "]"); return lollipopTaskInfo; } // TODO change this to keep track of all usagestats and compare timeinFg deltas // Will need to refactor buildTasks to manage bulk time change to db as well as // new runningTask. for (UsageStats s : stats) { if (s.getPackageName().equals(lollipopTaskInfo.packageName)) { bRunner = s; } } lollipopTaskInfo.lastPackageName = lollipopTaskInfo.packageName; lollipopTaskInfo.packageName = aRunner.getPackageName(); if (bRunner == null) { Tools.HangarLog("Couldn't find previous task [" + lollipopTaskInfo.packageName + "]"); } else { lollipopTaskInfo.timeInFGDelta = (lollipopTaskInfo.timeInFG > 0) ? bRunner.getTotalTimeInForeground() - lollipopTaskInfo.timeInFG : 0; } lollipopTaskInfo.timeInFG = aRunner.getTotalTimeInForeground(); Tools.HangarLog("New [" + lollipopTaskInfo.packageName + "] old [" + lollipopTaskInfo.lastPackageName + "] old FG delta: " + lollipopTaskInfo.timeInFGDelta); return lollipopTaskInfo; }