Search in sources :

Example 21 with Event

use of android.app.usage.UsageEvents.Event in project robolectric by robolectric.

the class ShadowUsageStatsManagerTest method testQueryEvents_overlappingEvents.

@Test
public void testQueryEvents_overlappingEvents() throws Exception {
    shadowOf(usageStatsManager).addEvent(TEST_PACKAGE_NAME1, 1000L, Event.MOVE_TO_BACKGROUND);
    shadowOf(usageStatsManager).addEvent(ShadowUsageStatsManager.EventBuilder.buildEvent().setTimeStamp(1000L).setEventType(Event.SYSTEM_INTERACTION).build());
    shadowOf(usageStatsManager).addEvent(TEST_PACKAGE_NAME1, 1000L, Event.MOVE_TO_FOREGROUND);
    UsageEvents events = usageStatsManager.queryEvents(1000L, 2000L);
    Event event = new Event();
    assertThat(events.hasNextEvent()).isTrue();
    assertThat(events.getNextEvent(event)).isTrue();
    assertThat(event.getPackageName()).isEqualTo(TEST_PACKAGE_NAME1);
    assertThat(event.getTimeStamp()).isEqualTo(1000L);
    assertThat(event.getEventType()).isEqualTo(Event.MOVE_TO_BACKGROUND);
    assertThat(events.hasNextEvent()).isTrue();
    assertThat(events.getNextEvent(event)).isTrue();
    assertThat(event.getPackageName()).isNull();
    assertThat(event.getTimeStamp()).isEqualTo(1000L);
    assertThat(event.getEventType()).isEqualTo(Event.SYSTEM_INTERACTION);
    assertThat(events.hasNextEvent()).isTrue();
    assertThat(events.getNextEvent(event)).isTrue();
    assertThat(event.getPackageName()).isEqualTo(TEST_PACKAGE_NAME1);
    assertThat(event.getTimeStamp()).isEqualTo(1000L);
    assertThat(event.getEventType()).isEqualTo(Event.MOVE_TO_FOREGROUND);
}
Also used : Event(android.app.usage.UsageEvents.Event) UsageEvents(android.app.usage.UsageEvents) Test(org.junit.Test)

Example 22 with Event

use of android.app.usage.UsageEvents.Event in project robolectric by robolectric.

the class ShadowUsageStatsManagerTest method testQueryEvents_appendEventData_simulateTimeChange_shouldAddOffsetToPreviousData.

@Test
public void testQueryEvents_appendEventData_simulateTimeChange_shouldAddOffsetToPreviousData() throws Exception {
    shadowOf(usageStatsManager).addEvent(TEST_PACKAGE_NAME1, 500L, Event.MOVE_TO_FOREGROUND);
    shadowOf(usageStatsManager).addEvent(TEST_PACKAGE_NAME1, 1000L, Event.MOVE_TO_BACKGROUND);
    shadowOf(usageStatsManager).addEvent(ShadowUsageStatsManager.EventBuilder.buildEvent().setTimeStamp(1500L).setPackage(TEST_PACKAGE_NAME2).setClass(TEST_ACTIVITY_NAME).setEventType(Event.MOVE_TO_FOREGROUND).build());
    shadowOf(usageStatsManager).addEvent(TEST_PACKAGE_NAME2, 2000L, Event.MOVE_TO_BACKGROUND);
    shadowOf(usageStatsManager).addEvent(ShadowUsageStatsManager.EventBuilder.buildEvent().setTimeStamp(2500L).setPackage(TEST_PACKAGE_NAME1).setEventType(Event.MOVE_TO_FOREGROUND).setClass(TEST_ACTIVITY_NAME).build());
    shadowOf(usageStatsManager).simulateTimeChange(10000L);
    UsageEvents events = usageStatsManager.queryEvents(11000L, 12000L);
    Event event = new Event();
    assertThat(events.hasNextEvent()).isTrue();
    assertThat(events.getNextEvent(event)).isTrue();
    assertThat(event.getPackageName()).isEqualTo(TEST_PACKAGE_NAME1);
    assertThat(event.getTimeStamp()).isEqualTo(11000L);
    assertThat(event.getEventType()).isEqualTo(Event.MOVE_TO_BACKGROUND);
    assertThat(events.hasNextEvent()).isTrue();
    assertThat(events.getNextEvent(event)).isTrue();
    assertThat(event.getPackageName()).isEqualTo(TEST_PACKAGE_NAME2);
    assertThat(event.getTimeStamp()).isEqualTo(11500L);
    assertThat(event.getEventType()).isEqualTo(Event.MOVE_TO_FOREGROUND);
    assertThat(event.getClassName()).isEqualTo(TEST_ACTIVITY_NAME);
    assertThat(events.hasNextEvent()).isFalse();
    assertThat(events.getNextEvent(event)).isFalse();
}
Also used : Event(android.app.usage.UsageEvents.Event) UsageEvents(android.app.usage.UsageEvents) Test(org.junit.Test)

Example 23 with Event

use of android.app.usage.UsageEvents.Event in project robolectric by robolectric.

the class ShadowUsageStatsManager method simulateTimeChange.

/**
 * Simulates the operations done by the framework when there is a time change. If the time is
 * changed, the timestamps of all existing usage events will be shifted by the same offset as the
 * time change, in order to make sure they remain stable relative to the new time.
 *
 * <p>This method won't affect the results of {@link #queryUsageStats} method.
 *
 * @param offsetToAddInMillis the offset to be applied to all events. For example, if {@code
 *     offsetInMillis} is 60,000, then all {@link Event}s will be shifted forward by 1 minute
 *     (into the future). Likewise, if {@code offsetInMillis} is -60,000, then all {@link Event}s
 *     will be shifted backward by 1 minute (into the past).
 */
public void simulateTimeChange(long offsetToAddInMillis) {
    List<Event> oldEvents = ImmutableList.copyOf(Iterables.concat(eventsByTimeStamp.values()));
    eventsByTimeStamp.clear();
    for (Event event : oldEvents) {
        long newTimestamp = event.getTimeStamp() + offsetToAddInMillis;
        addEvent(EventBuilder.fromEvent(event).setTimeStamp(newTimestamp).build());
    }
}
Also used : Event(android.app.usage.UsageEvents.Event)

Example 24 with Event

use of android.app.usage.UsageEvents.Event in project robolectric by robolectric.

the class ShadowUsageStatsManager method createUsageEvents.

private static UsageEvents createUsageEvents(List<Event> results) {
    ArraySet<String> names = new ArraySet<>();
    for (Event result : results) {
        if (result.mPackage != null) {
            names.add(result.mPackage);
        }
        if (result.mClass != null) {
            names.add(result.mClass);
        }
    }
    String[] table = names.toArray(new String[0]);
    Arrays.sort(table);
    // We can't directly construct usable UsageEvents, so we replicate what the framework does:
    // First the system marshalls the usage events into a Parcel.
    UsageEvents usageEvents = new UsageEvents(results, table);
    Parcel parcel = Parcel.obtain();
    usageEvents.writeToParcel(parcel, 0);
    // Then the app unmarshalls the usage events from the Parcel.
    parcel.setDataPosition(0);
    return new UsageEvents(parcel);
}
Also used : ArraySet(android.util.ArraySet) Parcel(android.os.Parcel) Event(android.app.usage.UsageEvents.Event) UsageEvents(android.app.usage.UsageEvents)

Example 25 with Event

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

the class AppStateNotificationBridgeTest method testGetAggregatedUsageEvents_onlyNotificationEvents.

@Test
public void testGetAggregatedUsageEvents_onlyNotificationEvents() throws Exception {
    List<Event> events = new ArrayList<>();
    Event good = new Event();
    good.mEventType = Event.NOTIFICATION_INTERRUPTION;
    good.mPackage = PKG1;
    good.mTimeStamp = 1;
    events.add(good);
    Event bad = new Event();
    bad.mEventType = Event.CHOOSER_ACTION;
    bad.mPackage = PKG1;
    bad.mTimeStamp = 2;
    events.add(bad);
    UsageEvents usageEvents = getUsageEvents(events);
    when(mUsageStats.queryEventsForUser(anyLong(), anyLong(), anyInt(), anyString())).thenReturn(usageEvents);
    Map<String, NotificationsSentState> map = mBridge.getAggregatedUsageEvents();
    assertThat(map.get(AppStateNotificationBridge.getKey(0, PKG1)).sentCount).isEqualTo(1);
}
Also used : NotificationsSentState(com.android.settings.applications.AppStateNotificationBridge.NotificationsSentState) ArrayList(java.util.ArrayList) Event(android.app.usage.UsageEvents.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UsageEvents(android.app.usage.UsageEvents) Test(org.junit.Test)

Aggregations

Event (android.app.usage.UsageEvents.Event)31 UsageEvents (android.app.usage.UsageEvents)28 Test (org.junit.Test)24 ArrayList (java.util.ArrayList)18 NotificationsSentState (com.android.settings.applications.AppStateNotificationBridge.NotificationsSentState)14 AppEntry (com.android.settingslib.applications.ApplicationsState.AppEntry)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 ConfigurationStats (android.app.usage.ConfigurationStats)4 UsageStats (android.app.usage.UsageStats)4 Configuration (android.content.res.Configuration)4 ResolveInfo (android.content.pm.ResolveInfo)3 Intent (android.content.Intent)2 Preference (androidx.preference.Preference)2 Config (org.robolectric.annotation.Config)2 ApplicationInfo (android.content.pm.ApplicationInfo)1 Parcel (android.os.Parcel)1 NotifyingApp (android.service.notification.NotifyingApp)1 ArraySet (android.util.ArraySet)1 ApplicationsState (com.android.settingslib.applications.ApplicationsState)1 ImmutableList (com.google.common.collect.ImmutableList)1