Java Code Examples for android.app.usage.UsageEvents#hasNextEvent()
The following examples show how to use
android.app.usage.UsageEvents#hasNextEvent() .
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: UsageStatsUtils.java From FCM-for-Mojo with GNU General Public License v3.0 | 6 votes |
public static String getForegroundPackage(UsageStatsManager usageStatsManager) { String packageName = null; final long INTERVAL = 1000 * 60; final long end = System.currentTimeMillis(); final long begin = end - INTERVAL; final UsageEvents usageEvents = usageStatsManager.queryEvents(begin, end); while (usageEvents.hasNextEvent()) { UsageEvents.Event event = new UsageEvents.Event(); usageEvents.getNextEvent(event); switch (event.getEventType()) { case UsageEvents.Event.MOVE_TO_FOREGROUND: packageName = event.getPackageName(); break; case UsageEvents.Event.MOVE_TO_BACKGROUND: if (event.getPackageName().equals(packageName)) { packageName = null; } } } return packageName; }
Example 2
Source File: EventUtils.java From UseTimeStatistic with MIT License | 6 votes |
@SuppressWarnings("ResourceType") @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static ArrayList<UsageEvents.Event> getEventList(Context context, long startTime, long endTime){ ArrayList<UsageEvents.Event> mEventList = new ArrayList<>(); Log.i(TAG," EventUtils-getEventList() Range start:" + startTime); Log.i(TAG," EventUtils-getEventList() Range end:" +endTime); Log.i(TAG," EventUtils-getEventList() Range start:" + dateFormat.format(startTime)); Log.i(TAG," EventUtils-getEventList() Range end:" + dateFormat.format(endTime)); UsageStatsManager mUsmManager = (UsageStatsManager) context.getSystemService("usagestats"); UsageEvents events = mUsmManager.queryEvents(startTime, endTime); while (events.hasNextEvent()) { UsageEvents.Event e = new UsageEvents.Event(); events.getNextEvent(e); if (e != null && (e.getEventType() == 1 || e.getEventType() == 2)) { Log.i(TAG," EventUtils-getEventList() "+e.getTimeStamp()+" event:" + e.getClassName() + " type = " + e.getEventType()); mEventList.add(e); } } return mEventList; }
Example 3
Source File: LollipopDetector.java From foregroundappchecker with Apache License 2.0 | 6 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public String getForegroundApp(final Context context) { if(!Utils.hasUsageStatsPermission(context)) return null; String foregroundApp = null; UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Service.USAGE_STATS_SERVICE); long time = System.currentTimeMillis(); UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - 1000 * 3600, time); UsageEvents.Event event = new UsageEvents.Event(); while (usageEvents.hasNextEvent()) { usageEvents.getNextEvent(event); if(event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) { foregroundApp = event.getPackageName(); } } return foregroundApp ; }
Example 4
Source File: UStats.java From AppPlus with MIT License | 6 votes |
@SuppressWarnings("ResourceType") public static void getStats(Context context){ UsageStatsManager usm = (UsageStatsManager) context.getSystemService("usagestats"); int interval = UsageStatsManager.INTERVAL_YEARLY; Calendar calendar = Calendar.getInstance(); long endTime = calendar.getTimeInMillis(); calendar.add(Calendar.YEAR, -1); long startTime = calendar.getTimeInMillis(); Log.d(TAG, "Range start:" + dateFormat.format(startTime) ); Log.d(TAG, "Range end:" + dateFormat.format(endTime)); UsageEvents uEvents = usm.queryEvents(startTime,endTime); while (uEvents.hasNextEvent()){ UsageEvents.Event e = new UsageEvents.Event(); uEvents.getNextEvent(e); if (e != null){ Log.d(TAG, "Event: " + e.getPackageName() + "\t" + e.getTimeStamp()); } } }
Example 5
Source File: BucketInfo.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
public static @NonNull BucketInfo getInfo(@NonNull UsageStatsManager usageStatsManager, long overLastDurationMs) { StringBuilder stringBuilder = new StringBuilder(); int currentBucket = usageStatsManager.getAppStandbyBucket(); int worseBucket = currentBucket; int bestBucket = currentBucket; long now = System.currentTimeMillis(); UsageEvents.Event event = new UsageEvents.Event(); UsageEvents usageEvents = usageStatsManager.queryEventsForSelf(now - overLastDurationMs, now); while (usageEvents.hasNextEvent()) { usageEvents.getNextEvent(event); if (event.getEventType() == UsageEvents.Event.STANDBY_BUCKET_CHANGED) { int appStandbyBucket = event.getAppStandbyBucket(); stringBuilder.append(new Date(event.getTimeStamp())) .append(": ") .append("Bucket Change: ") .append(bucketToString(appStandbyBucket)) .append("\n"); if (appStandbyBucket > worseBucket) { worseBucket = appStandbyBucket; } if (appStandbyBucket < bestBucket) { bestBucket = appStandbyBucket; } } } return new BucketInfo(currentBucket, worseBucket, bestBucket, stringBuilder); }
Example 6
Source File: TaskbarController.java From Taskbar with Apache License 2.0 | 5 votes |
@VisibleForTesting void filterForegroundApp(Context context, SharedPreferences pref, long searchInterval, List<String> applicationIdsToRemove) { if (pref.getBoolean(PREF_HIDE_FOREGROUND, false)) { UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE); UsageEvents events = mUsageStatsManager.queryEvents(searchInterval, System.currentTimeMillis()); UsageEvents.Event eventCache = new UsageEvents.Event(); String currentForegroundApp = null; while (events.hasNextEvent()) { events.getNextEvent(eventCache); if (eventCache.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) { if (!(eventCache.getPackageName().contains(BuildConfig.BASE_APPLICATION_ID) && !eventCache.getClassName().equals(MainActivity.class.getCanonicalName()) && !eventCache.getClassName().equals(HomeActivity.class.getCanonicalName()) && !eventCache.getClassName().equals(HomeActivityDelegate.class.getCanonicalName()) && !eventCache.getClassName().equals(SecondaryHomeActivity.class.getCanonicalName()) && !eventCache.getClassName().equals(InvisibleActivityFreeform.class.getCanonicalName()))) { currentForegroundApp = eventCache.getPackageName(); } } } if (!applicationIdsToRemove.contains(currentForegroundApp)) { applicationIdsToRemove.add(currentForegroundApp); } } }
Example 7
Source File: DataManager.java From AppsMonitor with MIT License | 4 votes |
public List<AppItem> getTargetAppTimeline(Context context, String target, int offset) { List<AppItem> items = new ArrayList<>(); UsageStatsManager manager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE); if (manager != null) { long[] range = AppUtil.getTimeRange(SortEnum.getSortEnum(offset)); UsageEvents events = manager.queryEvents(range[0], range[1]); UsageEvents.Event event = new UsageEvents.Event(); AppItem item = new AppItem(); item.mPackageName = target; item.mName = AppUtil.parsePackageName(context.getPackageManager(), target); // 缓存 ClonedEvent prevEndEvent = null; long start = 0; while (events.hasNextEvent()) { events.getNextEvent(event); String currentPackage = event.getPackageName(); int eventType = event.getEventType(); long eventTime = event.getTimeStamp(); Log.d("||||------>", currentPackage + " " + target + " " + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault()).format(new Date(eventTime)) + " " + eventType); if (currentPackage.equals(target)) { // 本次交互开始 Log.d("||||||||||>", currentPackage + " " + target + " " + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault()).format(new Date(eventTime)) + " " + eventType); // 记录第一次开始时间 if (eventType == UsageEvents.Event.MOVE_TO_FOREGROUND) { Log.d("********", "start " + start); if (start == 0) { start = eventTime; item.mEventTime = eventTime; item.mEventType = eventType; item.mUsageTime = 0; items.add(item.copy()); } } else if (eventType == UsageEvents.Event.MOVE_TO_BACKGROUND) { // 结束事件 if (start > 0) { prevEndEvent = new ClonedEvent(event); } Log.d("********", "add end " + start); } } else { // 记录最后一次结束事件 if (prevEndEvent != null && start > 0) { item.mEventTime = prevEndEvent.timeStamp; item.mEventType = prevEndEvent.eventType; item.mUsageTime = prevEndEvent.timeStamp - start; if (item.mUsageTime <= 0) item.mUsageTime = 0; if (item.mUsageTime > AppConst.USAGE_TIME_MIX) item.mCount++; items.add(item.copy()); start = 0; prevEndEvent = null; } } } } return items; }