Search in sources :

Example 36 with UsageEvents

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

the class NotificationBackendTest method getUsageEvents.

private UsageEvents getUsageEvents(List<UsageEvents.Event> events) {
    UsageEvents usageEvents = new UsageEvents(events, new String[] { "pkg" });
    Parcel parcel = Parcel.obtain();
    parcel.setDataPosition(0);
    usageEvents.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    return UsageEvents.CREATOR.createFromParcel(parcel);
}
Also used : Parcel(android.os.Parcel) UsageEvents(android.app.usage.UsageEvents)

Example 37 with UsageEvents

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

the class RecentNotifyingAppsPreferenceControllerTest method display_showRecents.

@Test
public void display_showRecents() throws Exception {
    List<Event> events = new ArrayList<>();
    Event app = new Event();
    app.mEventType = Event.NOTIFICATION_INTERRUPTION;
    app.mPackage = "a";
    app.mTimeStamp = System.currentTimeMillis();
    events.add(app);
    Event app1 = new Event();
    app1.mEventType = Event.NOTIFICATION_INTERRUPTION;
    app1.mPackage = "com.android.settings";
    app1.mTimeStamp = System.currentTimeMillis();
    events.add(app1);
    Event app2 = new Event();
    app2.mEventType = Event.NOTIFICATION_INTERRUPTION;
    app2.mPackage = "pkg.class2";
    app2.mTimeStamp = System.currentTimeMillis() - 1000;
    events.add(app2);
    // app1, app2 are valid apps. app3 is invalid.
    when(mAppState.getEntry(app.getPackageName(), UserHandle.myUserId())).thenReturn(mAppEntry);
    when(mAppState.getEntry(app1.getPackageName(), UserHandle.myUserId())).thenReturn(mAppEntry);
    when(mAppState.getEntry(app2.getPackageName(), UserHandle.myUserId())).thenReturn(null);
    when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(new ResolveInfo());
    UsageEvents usageEvents = getUsageEvents(new String[] { app.getPackageName(), app1.getPackageName(), app2.getPackageName() }, events);
    when(mIUsageStatsManager.queryEventsForUser(anyLong(), anyLong(), anyInt(), anyString())).thenReturn(usageEvents);
    mAppEntry.info = mApplicationInfo;
    mController.displayPreference(mScreen);
    verify(mCategory).setTitle(R.string.recent_notifications);
    // Only add app1 & app2. app3 skipped because it's invalid app.
    verify(mCategory, times(2)).addPreference(any(Preference.class));
    verify(mSeeAllPref).setSummary(null);
    verify(mSeeAllPref).setIcon(R.drawable.ic_chevron_right_24dp);
    verify(mDivider).setVisible(true);
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) Preference(androidx.preference.Preference) ArrayList(java.util.ArrayList) Event(android.app.usage.UsageEvents.Event) Intent(android.content.Intent) UsageEvents(android.app.usage.UsageEvents) Test(org.junit.Test)

Example 38 with UsageEvents

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

the class AppStateNotificationBridge method getAggregatedUsageEvents.

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

Example 39 with UsageEvents

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

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 40 with UsageEvents

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

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