Search in sources :

Example 21 with UsageEvents

use of android.app.usage.UsageEvents in project android_packages_apps_Settings by omnirom.

the class AppStateNotificationBridge method getAggregatedUsageEvents.

protected Map<String, NotificationsSentState> getAggregatedUsageEvents() {
    ArrayMap<String, NotificationsSentState> aggregatedStats = new ArrayMap<>();
    long now = System.currentTimeMillis();
    long startTime = now - (DateUtils.DAY_IN_MILLIS * DAYS_TO_CHECK);
    for (int userId : mUserIds) {
        UsageEvents events = null;
        try {
            events = mUsageStatsManager.queryEventsForUser(startTime, now, userId, mContext.getPackageName());
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        if (events != null) {
            UsageEvents.Event event = new UsageEvents.Event();
            while (events.hasNextEvent()) {
                events.getNextEvent(event);
                NotificationsSentState stats = aggregatedStats.get(getKey(userId, event.getPackageName()));
                if (stats == null) {
                    stats = new NotificationsSentState();
                    aggregatedStats.put(getKey(userId, event.getPackageName()), stats);
                }
                if (event.getEventType() == UsageEvents.Event.NOTIFICATION_INTERRUPTION) {
                    if (event.getTimeStamp() > stats.lastSent) {
                        stats.lastSent = event.getTimeStamp();
                    }
                    stats.sentCount++;
                }
            }
        }
    }
    return aggregatedStats;
}
Also used : ArrayMap(android.util.ArrayMap) UsageEvents(android.app.usage.UsageEvents) RemoteException(android.os.RemoteException)

Example 22 with UsageEvents

use of android.app.usage.UsageEvents in project Taskbar by farmerbb.

the class TaskbarController method filterForegroundApp.

@TargetApi(Build.VERSION_CODES.M)
@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);
        }
    }
}
Also used : HomeActivityDelegate(com.farmerbb.taskbar.activity.HomeActivityDelegate) InvisibleActivityFreeform(com.farmerbb.taskbar.activity.InvisibleActivityFreeform) UsageStatsManager(android.app.usage.UsageStatsManager) MotionEvent(android.view.MotionEvent) MainActivity(com.farmerbb.taskbar.activity.MainActivity) UsageEvents(android.app.usage.UsageEvents) VisibleForTesting(androidx.annotation.VisibleForTesting) TargetApi(android.annotation.TargetApi)

Example 23 with UsageEvents

use of android.app.usage.UsageEvents in project android_frameworks_base by ResurrectionRemix.

the class UsageLogActivity method run.

@Override
public void run() {
    long now = System.currentTimeMillis();
    UsageEvents events = mUsageStatsManager.queryEvents(mLastTime, now);
    long lastEventTime = mAdapter.update(events);
    if (lastEventTime >= 0) {
        mLastTime = lastEventTime + 1;
    }
    mHandler.postDelayed(this, 1000 * 5);
}
Also used : UsageEvents(android.app.usage.UsageEvents)

Example 24 with UsageEvents

use of android.app.usage.UsageEvents in project platform_frameworks_base by android.

the class UsageLogActivity method run.

@Override
public void run() {
    long now = System.currentTimeMillis();
    UsageEvents events = mUsageStatsManager.queryEvents(mLastTime, now);
    long lastEventTime = mAdapter.update(events);
    if (lastEventTime >= 0) {
        mLastTime = lastEventTime + 1;
    }
    mHandler.postDelayed(this, 1000 * 5);
}
Also used : UsageEvents(android.app.usage.UsageEvents)

Example 25 with UsageEvents

use of android.app.usage.UsageEvents in project Signal-Android by WhisperSystems.

the class BucketInfo method getInfo.

@NonNull
public static 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);
}
Also used : UsageEvents(android.app.usage.UsageEvents) Date(java.util.Date) NonNull(androidx.annotation.NonNull)

Aggregations

UsageEvents (android.app.usage.UsageEvents)45 Event (android.app.usage.UsageEvents.Event)24 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)19 NotificationsSentState (com.android.settings.applications.AppStateNotificationBridge.NotificationsSentState)14 AppEntry (com.android.settingslib.applications.ApplicationsState.AppEntry)8 RemoteException (android.os.RemoteException)7 Parcel (android.os.Parcel)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 ResolveInfo (android.content.pm.ResolveInfo)4 UsageStatsManager (android.app.usage.UsageStatsManager)3 Intent (android.content.Intent)3 ArrayMap (android.util.ArrayMap)3 MotionEvent (android.view.MotionEvent)3 Date (java.util.Date)3 SuppressLint (android.annotation.SuppressLint)2 TargetApi (android.annotation.TargetApi)2 NotifyingApp (android.service.notification.NotifyingApp)2 NonNull (androidx.annotation.NonNull)2 VisibleForTesting (androidx.annotation.VisibleForTesting)2