Java Code Examples for android.app.usage.UsageEvents#Event
The following examples show how to use
android.app.usage.UsageEvents#Event .
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: 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 3
Source File: UseTimeDataManager.java From UseTimeStatistic with MIT License | 6 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private ArrayList<UsageEvents.Event> getEventList(int dayNumber){ ArrayList<UsageEvents.Event> mEventList = new ArrayList<>(); // Calendar calendar = Calendar.getInstance(); // long endTime = calendar.getTimeInMillis(); // calendar.add(Calendar.YEAR, -1); // //long startTime = calendar.getTimeInMillis()- 3 * DateTransUtils.DAY_IN_MILLIS; // long startTime = calendar.getTimeInMillis(); long endTime = 0,startTime = 0; if(dayNumber == 0 ){ endTime = System.currentTimeMillis(); startTime = DateTransUtils.getZeroClockTimestamp(endTime); }else { endTime = DateTransUtils.getZeroClockTimestamp(System.currentTimeMillis() - (dayNumber-1) * DateTransUtils.DAY_IN_MILLIS ) - 1; startTime = endTime - DateTransUtils.DAY_IN_MILLIS + 1; } return EventUtils.getEventList(mContext,startTime,endTime); }
Example 4
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 5
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 6
Source File: EventCopyToFileUtils.java From UseTimeStatistic with MIT License | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public static void write(Context context, long startTime, long endTime){ ArrayList<UsageEvents.Event> eventList = EventUtils.getEventList(context, startTime, endTime); if(eventList == null || eventList.size() == 0){ return; } long fileName = DateTransUtils.getZeroClockTimestamp(startTime); String filePath = BASE_FILE_PATH + "/" + fileName + ".txt"; try { checkFile(filePath); // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 FileWriter writer = new FileWriter(filePath, true); UsageEvents.Event lastEvent = null; for (int i = 0 ; i < eventList.size() ; i++){ if(context.getPackageName().equals(eventList.get(i).getPackageName())){ Log.i(TAG," "+eventList.get(i).getClassName()); UsageEvents.Event thisEvent = eventList.get(i); if(lastEvent != null && lastEvent.getEventType() == 1 && thisEvent.getEventType() == 2 && lastEvent.getClassName().equals(thisEvent.getClassName())){ writer.write(StringUtils.getInputString(thisEvent.getTimeStamp(),thisEvent.getClassName(),thisEvent.getEventType(),thisEvent.getTimeStamp()-lastEvent.getTimeStamp())); }else { writer.write(StringUtils.getInputString(thisEvent.getTimeStamp(),thisEvent.getClassName(),thisEvent.getEventType(),0)); } lastEvent = thisEvent; } } writer.close(); Log.i(TAG," WriteRecordFileUtils--writeToFile() 写入文件成功 " + fileName); } catch (IOException e) { e.printStackTrace(); } }
Example 7
Source File: UseTimeDataManager.java From UseTimeStatistic with MIT License | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private ArrayList<UsageEvents.Event> getEventListChecked(){ ArrayList<UsageEvents.Event> mList = new ArrayList<>(); for(int i = 0; i < mEventList.size() ; i++){ if(mEventList.get(i).getEventType() == 1 || mEventList.get(i).getEventType() == 2){ mList.add(mEventList.get(i)); } } return mList; }
Example 8
Source File: UseTimeDataManager.java From UseTimeStatistic with MIT License | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private ArrayList<UsageEvents.Event> getEventListCheckWithoutErrorData(){ ArrayList<UsageEvents.Event> mList = new ArrayList<>(); for(int i = 0; i < mEventList.size() ; i++){ if(mEventList.get(i).getEventType() == 1 || mEventList.get(i).getEventType() == 2){ mList.add(mEventList.get(i)); } } return mList; }
Example 9
Source File: UseTimeDataManager.java From UseTimeStatistic with MIT License | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void checkEventList(ArrayList<UsageEvents.Event> list){ boolean isCheckAgain = false ; for (int i = 0 ; i < list.size() - 1 ; i += 2){ if(list.get(i).getClassName().equals(list.get(i+1).getClassName())){ if(list.get(i).getEventType() != 1){ Log.i(UseTimeDataManager.TAG," EventList 出错 : "+list.get(i).getPackageName() +" "+ DateUtils.formatSameDayTime(list.get(i).getTimeStamp(), System.currentTimeMillis(), DateFormat.MEDIUM, DateFormat.MEDIUM).toString()); list.remove(i); isCheckAgain = true; break; } if(list.get(i+1).getEventType() != 2){ Log.i(UseTimeDataManager.TAG," EventList 出错 : "+list.get(i+1).getPackageName() +" "+ DateUtils.formatSameDayTime(list.get(i+1).getTimeStamp(), System.currentTimeMillis(), DateFormat.MEDIUM, DateFormat.MEDIUM).toString()); list.remove(i); isCheckAgain = true; break; } }else { //i和i+1的className对不上,则删除第i个数据,重新检查 list.remove(i); isCheckAgain = true; break; } } if(isCheckAgain){ checkEventList(list); } }
Example 10
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 11
Source File: TaskbarControllerTest.java From Taskbar with Apache License 2.0 | 5 votes |
private UsageEvents.Event buildTaskbarForegroundAppEvent(String className, long timestamp) { return EventBuilder .buildEvent() .setPackage(className) .setTimeStamp(timestamp) .setClass(className) .setEventType(MOVE_TO_FOREGROUND) .build(); }
Example 12
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 13
Source File: KcaViewButtonService.java From kcanotify with GNU General Public License v3.0 | 5 votes |
private static boolean isForeGroundEvent(UsageEvents.Event event) { if(event == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { return event.getEventType() == UsageEvents.Event.ACTIVITY_RESUMED; } else { return event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND; } }
Example 14
Source File: UseTimeEveryDetailAdapter.java From UseTimeStatistic with MIT License | 4 votes |
public UseTimeEveryDetailAdapter(ArrayList<UsageEvents.Event> mOneTimeDetailEventInfoList) { this.mOneTimeDetailEventInfoList = mOneTimeDetailEventInfoList; }
Example 15
Source File: OneTimeDetails.java From UseTimeStatistic with MIT License | 4 votes |
public OneTimeDetails(String pkg,long useTime, ArrayList<UsageEvents.Event> oneTimeDetailList) { this.pkgName = pkg; this.useTime = useTime; OneTimeDetailEventList = oneTimeDetailList; }
Example 16
Source File: OneTimeDetails.java From UseTimeStatistic with MIT License | 4 votes |
public ArrayList<UsageEvents.Event> getOneTimeDetailEventList() { return OneTimeDetailEventList; }
Example 17
Source File: OneTimeDetails.java From UseTimeStatistic with MIT License | 4 votes |
public void setOneTimeDetailEventList(ArrayList<UsageEvents.Event> oneTimeDetailEventList) { OneTimeDetailEventList = oneTimeDetailEventList; }
Example 18
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; }
Example 19
Source File: UseTimeDataManager.java From UseTimeStatistic with MIT License | 4 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void refreshOneTimeDetailList(int startIndex){ Log.i(TAG," refreshOneTimeDetailList() startIndex : " + startIndex); if(startIndex == 0){ Log.i(TAG," refreshOneTimeDetailList() 每次从0开始,将原本的 mOneTimeDetailList 清除一次,然后开始分类 " ); if(mOneTimeDetailList != null){ mOneTimeDetailList.clear(); } } long totalTime = 0 ; int usedIndex = 0; String pkg = null; ArrayList<UsageEvents.Event> list = new ArrayList(); for (int i = startIndex ; i < mEventListChecked.size() ; i++){ if( i == startIndex ){ if(mEventListChecked.get(i).getEventType() == 2){ Log.i(TAG," refreshOneTimeDetailList() warning : 每次打开一个app 第一个activity的类型是 2 "); } pkg = mEventListChecked.get(i).getPackageName(); list.add(mEventListChecked.get(i)); }else { if(pkg != null ){ if(pkg.equals(mEventListChecked.get(i).getPackageName())){ list.add(mEventListChecked.get(i)); if( i == mEventListChecked.size()-1 ){ usedIndex = i ; } }else { usedIndex = i; break; } } } } Log.i(TAG," mEventListChecked 分类: before check : list.size() = " + list.size()); checkEventList(list); Log.i(TAG," mEventListChecked 分类: after check : list.size() = " + list.size()); // startTime = list.get(0).getTimeStamp(); // endTime = list.get( list.size() - 1 ).getTimeStamp(); Log.i(TAG," mEventListChecked 分类: 本次启动的包名:"+list.get(0).getPackageName()+ " 时间:" + DateUtils.formatSameDayTime(list.get(0).getTimeStamp(), System.currentTimeMillis(), DateFormat.MEDIUM, DateFormat.MEDIUM)); for(int i = 1 ; i < list.size() ; i += 2){ if(list.get(i).getEventType() == 2 && list.get( i - 1).getEventType() == 1 ){ totalTime += ( list.get(i).getTimeStamp() - list.get( i - 1).getTimeStamp()); } } OneTimeDetails oneTimeDetails = new OneTimeDetails(pkg,totalTime,list); mOneTimeDetailList.add(oneTimeDetails); if(usedIndex < mEventListChecked.size() - 1){ refreshOneTimeDetailList(usedIndex); }else { Log.i(TAG," refreshOneTimeDetailList() 已经将 mEventListChecked 分类完毕 "); } }
Example 20
Source File: TaskbarControllerTest.java From Taskbar with Apache License 2.0 | 4 votes |
@Test public void testFilterForegroundApp() { prefs.edit().putBoolean(PREF_HIDE_FOREGROUND, true).apply(); long searchInterval = 0L; List<String> applicationIdsToRemove = new ArrayList<>(); UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE); final String entryTestPackage1 = ENTRY_TEST_PACKAGE + "-1"; UsageEvents.Event event = EventBuilder .buildEvent() .setEventType(MOVE_TO_FOREGROUND) .setTimeStamp(100L) .setPackage(entryTestPackage1) .build(); shadowOf(usageStatsManager).addEvent(event); uiController.filterForegroundApp(context, prefs, searchInterval, applicationIdsToRemove); assertEquals(entryTestPackage1, applicationIdsToRemove.remove(0)); event = EventBuilder .buildEvent() .setEventType(MOVE_TO_BACKGROUND) .setTimeStamp(200L) .setPackage(ENTRY_TEST_PACKAGE + "-2") .build(); shadowOf(usageStatsManager).addEvent(event); uiController.filterForegroundApp(context, prefs, searchInterval, applicationIdsToRemove); assertEquals(entryTestPackage1, applicationIdsToRemove.remove(0)); event = buildTaskbarForegroundAppEvent(MainActivity.class.getCanonicalName(), 300L); shadowOf(usageStatsManager).addEvent(event); uiController.filterForegroundApp(context, prefs, searchInterval, applicationIdsToRemove); assertEquals(MainActivity.class.getCanonicalName(), applicationIdsToRemove.remove(0)); event = buildTaskbarForegroundAppEvent(HomeActivity.class.getCanonicalName(), 400L); shadowOf(usageStatsManager).addEvent(event); uiController.filterForegroundApp(context, prefs, searchInterval, applicationIdsToRemove); assertEquals(HomeActivity.class.getCanonicalName(), applicationIdsToRemove.remove(0)); event = buildTaskbarForegroundAppEvent(HomeActivityDelegate.class.getCanonicalName(), 500L); shadowOf(usageStatsManager).addEvent(event); uiController.filterForegroundApp(context, prefs, searchInterval, applicationIdsToRemove); assertEquals(HomeActivityDelegate.class.getCanonicalName(), applicationIdsToRemove.remove(0)); event = buildTaskbarForegroundAppEvent(SecondaryHomeActivity.class.getCanonicalName(), 600L); shadowOf(usageStatsManager).addEvent(event); uiController.filterForegroundApp(context, prefs, searchInterval, applicationIdsToRemove); assertEquals(SecondaryHomeActivity.class.getCanonicalName(), applicationIdsToRemove.remove(0)); event = buildTaskbarForegroundAppEvent(InvisibleActivityFreeform.class.getCanonicalName(), 700L); shadowOf(usageStatsManager).addEvent(event); uiController.filterForegroundApp(context, prefs, searchInterval, applicationIdsToRemove); assertEquals(InvisibleActivityFreeform.class.getCanonicalName(), applicationIdsToRemove.remove(0)); event = buildTaskbarForegroundAppEvent(UNSUPPORTED, 800L); shadowOf(usageStatsManager).addEvent(event); uiController.filterForegroundApp(context, prefs, searchInterval, applicationIdsToRemove); assertEquals(UNSUPPORTED, applicationIdsToRemove.remove(0)); prefs.edit().remove(PREF_HIDE_FOREGROUND).apply(); uiController.filterForegroundApp(context, prefs, searchInterval, applicationIdsToRemove); assertEquals(0, applicationIdsToRemove.size()); }