Java 类android.app.usage.UsageEvents 实例源码
项目:AppPlus
文件:UStats.java
@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());
}
}
}
项目:FCM-for-Mojo
文件:UsageStatsUtils.java
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;
}
项目:AppsTimeline
文件:DetailActivity.java
@Override
protected void onPostExecute(List<AppItem> appItems) {
if (mContext.get() != null) {
List<AppItem> newList = new ArrayList<>();
long duration = 0;
for (AppItem item : appItems) {
if (item.mEventType == UsageEvents.Event.USER_INTERACTION || item.mEventType == UsageEvents.Event.NONE) {
continue;
}
duration += item.mUsageTime;
if (item.mEventType == UsageEvents.Event.MOVE_TO_BACKGROUND) {
AppItem newItem = item.copy();
newItem.mEventType = -1;
newList.add(newItem);
}
newList.add(item);
}
mTime.setText(String.format(getResources().getString(R.string.times), AppUtil.formatMilliSeconds(duration), appItems.get(appItems.size() - 1).mCount));
mAdapter.setData(newList);
}
}
项目:foregroundappchecker
文件:LollipopDetector.java
@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 ;
}
项目:App-Monitor
文件:UStats.java
@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());
}
}
}
项目:UsageStatsSample
文件:UStats.java
@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());
}
}
}
项目:LollipopExplorer
文件:UsageEventsViewerActivity.java
@Override
protected List<UsageEventsItem> doInBackground(Void... params) {
//noinspection ResourceType
UsageStatsManager usageStatsMgr = (UsageStatsManager) getSystemService("usagestats");
UsageEvents events = usageStatsMgr.queryEvents(mStartTime, mEndTime);
List<UsageEventsItem> results = new ArrayList<>();
UsageEvents.Event event = new UsageEvents.Event();
PackageManager pm = getPackageManager();
while (events.getNextEvent(event)) {
UsageEventsItem item = new UsageEventsItem();
item.pkgName = event.getPackageName();
item.className = event.getClassName();
item.type = event.getEventType();
item.timeStamp = event.getTimeStamp();
item.appName = item.pkgName;
try {
item.appName = pm.getApplicationInfo(item.pkgName, 0).loadLabel(pm).toString();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
results.add(item);
}
Collections.sort(results, new UsageEventsItem.AppNameComparator());
return results;
}
项目:LollipopExplorer
文件:UsageEventsViewerAdapter.java
@Override
protected void bindView(UsageEventsItem item, ViewHolderBase holder) {
ViewHolder vh = (ViewHolder) holder;
vh.appNameView.setText(item.appName);
vh.pkgNameView.setText(item.pkgName);
vh.classNameView.setText(item.className);
if (item.type == UsageEvents.Event.CONFIGURATION_CHANGE) {
vh.eventTypeView.setText(R.string.usage_events_type_config);
} else if (item.type == UsageEvents.Event.MOVE_TO_FOREGROUND) {
vh.eventTypeView.setText(R.string.usage_events_type_goto_fg);
} else if (item.type == UsageEvents.Event.MOVE_TO_BACKGROUND) {
vh.eventTypeView.setText(R.string.usage_events_type_goto_bg);
} else if (item.type == UsageEvents.Event.NONE) {
vh.eventTypeView.setText(R.string.usage_events_type_none);
}
vh.timeStampView.setText(DateTimeUtils.getReadableTimeStamp(item.timeStamp));
}
项目:AppsTimeline
文件:DetailActivity.java
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
AppItem item = mData.get(position);
String desc = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()).format(new Date(item.mEventTime));
if (item.mEventType == UsageEvents.Event.MOVE_TO_BACKGROUND) {
holder.mLayout.setPadding(dpToPx(16), 0, dpToPx(16), dpToPx(4));
} else if (item.mEventType == -1) {
holder.mLayout.setPadding(dpToPx(16), dpToPx(4), dpToPx(16), dpToPx(4));
desc = AppUtil.formatMilliSeconds(item.mUsageTime);
} else if (item.mEventType == UsageEvents.Event.MOVE_TO_FOREGROUND) {
holder.mLayout.setPadding(dpToPx(16), dpToPx(12), dpToPx(16), 0);
}
holder.mEvent.setText(String.format("%s %s", getPrefix(item.mEventType), desc));
}
项目:Night-Light
文件:NotificationService.java
private void updateRecentApps() {
Blacklist blacklist = Blacklist.getInstance(this);
String currentForegroundApp = null;
if(!U.isNightModeOn(this))
stopSelf();
else if(Settings.Secure.getInt(getContentResolver(), "accessibility_display_inversion_enabled", 0) == 1)
U.setTint(this, false);
else {
if(blacklist.getBlockedApps().size() > 0) {
UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
UsageEvents events = mUsageStatsManager.queryEvents(
System.currentTimeMillis() - AlarmManager.INTERVAL_DAY,
System.currentTimeMillis());
UsageEvents.Event eventCache = new UsageEvents.Event();
while(events.hasNextEvent()) {
events.getNextEvent(eventCache);
if(eventCache.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND)
currentForegroundApp = eventCache.getPackageName();
}
}
U.setTint(this, !blacklist.isBlocked(currentForegroundApp));
}
}
项目:AppUsageWatcher
文件:WatchUtil.java
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
private static String getTopPackageNameAfterLollipop(Context context) {
PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
UsageStatsManager usageStatsManager = (UsageStatsManager)context.getSystemService(USAGE_STATS);
if (!powerManager.isInteractive()) {
sForegroundMap.clear();
return null;
}
long time = System.currentTimeMillis();
UsageEvents usageEvents = usageStatsManager.queryEvents(time-5000, time);
if (null == usageEvents) {
return null;
}
String packageName = null;
while (usageEvents.hasNextEvent()) {
UsageEvents.Event event = new UsageEvents.Event();
usageEvents.getNextEvent(event);
packageName = event.getPackageName();
switch (event.getEventType()) {
case UsageEvents.Event.MOVE_TO_FOREGROUND:
sForegroundMap.put(packageName, Long.valueOf(event.getTimeStamp()));
break;
case UsageEvents.Event.MOVE_TO_BACKGROUND:
sForegroundMap.remove(packageName);
break;
}
}
long latest = 0;
for (Map.Entry<String, Long> entry : sForegroundMap.entrySet()) {
long timestamp = entry.getValue().longValue();
if (latest < timestamp) {
packageName = entry.getKey();
latest = timestamp;
}
}
return packageName;
}
项目:AppsTimeline
文件:DataManager.java
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;
}
项目:AppsTimeline
文件:DataManager.java
ClonedEvent(UsageEvents.Event event) {
packageName = event.getPackageName();
eventClass = event.getClassName();
timeStamp = event.getTimeStamp();
eventType = event.getEventType();
}