Search in sources :

Example 11 with UsageEvents

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

the class RecentNotifyingAppsPreferenceControllerTest method reloadData.

@Test
public void reloadData() throws Exception {
    when(mUserManager.getProfileIdsWithDisabled(0)).thenReturn(new int[] { 0, 10 });
    mController = new RecentNotifyingAppsPreferenceController(mContext, mBackend, mIUsageStatsManager, mUserManager, mAppState, mHost);
    List<Event> events = new ArrayList<>();
    Event app = new Event();
    app.mEventType = Event.NOTIFICATION_INTERRUPTION;
    app.mPackage = "b";
    app.mTimeStamp = 1;
    events.add(app);
    Event app1 = new Event();
    app1.mEventType = Event.MAX_EVENT_TYPE;
    app1.mPackage = "com.foo.bar";
    app1.mTimeStamp = 10;
    events.add(app1);
    UsageEvents usageEvents = getUsageEvents(new String[] { "b", "com.foo.bar" }, events);
    when(mIUsageStatsManager.queryEventsForUser(anyLong(), anyLong(), eq(0), anyString())).thenReturn(usageEvents);
    List<Event> events10 = new ArrayList<>();
    Event app10 = new Event();
    app10.mEventType = Event.NOTIFICATION_INTERRUPTION;
    app10.mPackage = "a";
    app10.mTimeStamp = 2;
    events10.add(app10);
    Event app10a = new Event();
    app10a.mEventType = Event.NOTIFICATION_INTERRUPTION;
    app10a.mPackage = "a";
    app10a.mTimeStamp = 20;
    events10.add(app10a);
    UsageEvents usageEvents10 = getUsageEvents(new String[] { "a" }, events10);
    when(mIUsageStatsManager.queryEventsForUser(anyLong(), anyLong(), eq(10), anyString())).thenReturn(usageEvents10);
    mController.reloadData();
    assertThat(mController.mApps.size()).isEqualTo(2);
    boolean foundPkg0 = false;
    boolean foundPkg10 = false;
    for (NotifyingApp notifyingApp : mController.mApps) {
        if (notifyingApp.getLastNotified() == 20 && notifyingApp.getPackage().equals("a") && notifyingApp.getUserId() == 10) {
            foundPkg10 = true;
        }
        if (notifyingApp.getLastNotified() == 1 && notifyingApp.getPackage().equals("b") && notifyingApp.getUserId() == 0) {
            foundPkg0 = true;
        }
    }
    assertThat(foundPkg0).isTrue();
    assertThat(foundPkg10).isTrue();
}
Also used : ArrayList(java.util.ArrayList) Event(android.app.usage.UsageEvents.Event) UsageEvents(android.app.usage.UsageEvents) NotifyingApp(android.service.notification.NotifyingApp) Test(org.junit.Test)

Example 12 with UsageEvents

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

the class RecentNotifyingAppsPreferenceControllerTest method display_showRecentsWithInstantApp.

@Test
public void display_showRecentsWithInstantApp() throws Exception {
    List<Event> events = new ArrayList<>();
    Event app = new Event();
    app.mEventType = Event.NOTIFICATION_INTERRUPTION;
    app.mPackage = "com.foo.bar";
    app.mTimeStamp = System.currentTimeMillis();
    events.add(app);
    Event app1 = new Event();
    app1.mEventType = Event.NOTIFICATION_INTERRUPTION;
    app1.mPackage = "com.foo.barinstant";
    app1.mTimeStamp = System.currentTimeMillis() + 200;
    events.add(app1);
    UsageEvents usageEvents = getUsageEvents(new String[] { "com.foo.bar", "com.foo.barinstant" }, events);
    when(mIUsageStatsManager.queryEventsForUser(anyLong(), anyLong(), anyInt(), anyString())).thenReturn(usageEvents);
    ApplicationsState.AppEntry app1Entry = mock(ApplicationsState.AppEntry.class);
    ApplicationsState.AppEntry app2Entry = mock(ApplicationsState.AppEntry.class);
    app1Entry.info = mApplicationInfo;
    app2Entry.info = mApplicationInfo;
    when(mAppState.getEntry(app.getPackageName(), UserHandle.myUserId())).thenReturn(app1Entry);
    when(mAppState.getEntry(app1.getPackageName(), UserHandle.myUserId())).thenReturn(app2Entry);
    // Only the regular app app1 should have its intent resolve.
    when(mPackageManager.resolveActivity(argThat(intentMatcher(app.getPackageName())), anyInt())).thenReturn(new ResolveInfo());
    // Make sure app2 is considered an instant app.
    ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider", (InstantAppDataProvider) (ApplicationInfo info) -> {
        if (info == app2Entry.info) {
            return true;
        } else {
            return false;
        }
    });
    mController.displayPreference(mScreen);
    ArgumentCaptor<Preference> prefCaptor = ArgumentCaptor.forClass(Preference.class);
    verify(mCategory, times(2)).addPreference(prefCaptor.capture());
    List<Preference> prefs = prefCaptor.getAllValues();
    assertThat(prefs.get(1).getKey()).isEqualTo(RecentNotifyingAppsPreferenceController.getKey(UserHandle.myUserId(), app.getPackageName()));
    assertThat(prefs.get(0).getKey()).isEqualTo(RecentNotifyingAppsPreferenceController.getKey(UserHandle.myUserId(), app1.getPackageName()));
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) Preference(androidx.preference.Preference) ApplicationsState(com.android.settingslib.applications.ApplicationsState) ArrayList(java.util.ArrayList) ApplicationInfo(android.content.pm.ApplicationInfo) Event(android.app.usage.UsageEvents.Event) UsageEvents(android.app.usage.UsageEvents) Test(org.junit.Test)

Example 13 with UsageEvents

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

the class RecentNotifyingAppsPreferenceControllerTest method getUsageEvents.

private UsageEvents getUsageEvents(String[] pkgs, List<Event> events) {
    UsageEvents usageEvents = new UsageEvents(events, pkgs);
    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 14 with UsageEvents

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

the class RecentNotifyingAppsPreferenceController method reloadData.

@VisibleForTesting
void reloadData() {
    mApps = new ArrayList<>();
    mCal = Calendar.getInstance();
    mCal.add(Calendar.DAY_OF_YEAR, -DAYS);
    for (int userId : mUserIds) {
        UsageEvents events = null;
        try {
            events = mUsageStatsManager.queryEventsForUser(mCal.getTimeInMillis(), System.currentTimeMillis(), userId, mContext.getPackageName());
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        if (events != null) {
            ArrayMap<String, NotifyingApp> aggregatedStats = new ArrayMap<>();
            UsageEvents.Event event = new UsageEvents.Event();
            while (events.hasNextEvent()) {
                events.getNextEvent(event);
                if (event.getEventType() == UsageEvents.Event.NOTIFICATION_INTERRUPTION) {
                    NotifyingApp app = aggregatedStats.get(getKey(userId, event.getPackageName()));
                    if (app == null) {
                        app = new NotifyingApp();
                        aggregatedStats.put(getKey(userId, event.getPackageName()), app);
                        app.setPackage(event.getPackageName());
                        app.setUserId(userId);
                    }
                    if (event.getTimeStamp() > app.getLastNotified()) {
                        app.setLastNotified(event.getTimeStamp());
                    }
                }
            }
            mApps.addAll(aggregatedStats.values());
        }
    }
}
Also used : ArrayMap(android.util.ArrayMap) UsageEvents(android.app.usage.UsageEvents) RemoteException(android.os.RemoteException) NotifyingApp(android.service.notification.NotifyingApp) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 15 with UsageEvents

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

the class AppStateNotificationBridgeTest method getUsageEvents.

private UsageEvents getUsageEvents(List<Event> events) {
    UsageEvents usageEvents = new UsageEvents(events, new String[] { PKG1, PKG2 });
    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)

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