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);
}
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();
}
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());
}
}
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);
}
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);
}
Aggregations