use of android.app.usage.UsageEvents in project robolectric by robolectric.
the class ShadowUsageStatsManagerTest method testQueryEventsForSelf_shouldReturnsEventsForCurrentPackageOnly.
@Test
@Config(minSdk = Build.VERSION_CODES.P)
public void testQueryEventsForSelf_shouldReturnsEventsForCurrentPackageOnly() {
String packageName = context.getOpPackageName();
shadowOf(usageStatsManager).addEvent(ShadowUsageStatsManager.EventBuilder.buildEvent().setTimeStamp(1500L).setPackage(TEST_PACKAGE_NAME2).setClass(TEST_ACTIVITY_NAME).setEventType(Event.ACTIVITY_PAUSED).build());
shadowOf(usageStatsManager).addEvent(ShadowUsageStatsManager.EventBuilder.buildEvent().setTimeStamp(2200L).setPackage(packageName).setEventType(Event.ACTIVITY_RESUMED).setClass(TEST_ACTIVITY_NAME).build());
shadowOf(usageStatsManager).addEvent(ShadowUsageStatsManager.EventBuilder.buildEvent().setTimeStamp(2500L).setPackage(TEST_PACKAGE_NAME1).setEventType(Event.ACTIVITY_RESUMED).setClass(TEST_ACTIVITY_NAME).build());
shadowOf(usageStatsManager).addEvent(ShadowUsageStatsManager.EventBuilder.buildEvent().setTimeStamp(2800L).setPackage(packageName).setEventType(Event.ACTIVITY_STOPPED).setClass(TEST_ACTIVITY_NAME).build());
UsageEvents events = usageStatsManager.queryEventsForSelf(0L, 3000L);
Event event = new Event();
assertThat(events.hasNextEvent()).isTrue();
assertThat(events.getNextEvent(event)).isTrue();
assertThat(event.getPackageName()).isEqualTo(packageName);
assertThat(event.getTimeStamp()).isEqualTo(2200L);
assertThat(event.getEventType()).isEqualTo(Event.ACTIVITY_RESUMED);
assertThat(events.hasNextEvent()).isTrue();
assertThat(events.getNextEvent(event)).isTrue();
assertThat(event.getPackageName()).isEqualTo(packageName);
assertThat(event.getTimeStamp()).isEqualTo(2800L);
assertThat(event.getEventType()).isEqualTo(Event.ACTIVITY_STOPPED);
assertThat(events.hasNextEvent()).isFalse();
}
use of android.app.usage.UsageEvents 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 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 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 in project kcanotify by antest1.
the class KcaViewButtonService method checkForegroundPackage.
@SuppressLint("WrongConstant")
public String checkForegroundPackage() {
String classByUsageStats = null;
String packageNameByUsageStats = null;
String recentPackageName = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager mUsageStatsManager;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
mUsageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
} else {
// noinspection ResourceType
mUsageStatsManager = (UsageStatsManager) getSystemService("usagestats");
}
final long INTERVAL = 5000;
final long end = System.currentTimeMillis();
final long begin = end - INTERVAL;
final UsageEvents usageEvents = mUsageStatsManager.queryEvents(begin, end);
while (usageEvents.hasNextEvent()) {
UsageEvents.Event event = new UsageEvents.Event();
usageEvents.getNextEvent(event);
if (isForeGroundEvent(event)) {
packageNameByUsageStats = event.getPackageName();
Date d = new Date(event.getTimeStamp());
classByUsageStats = event.getClassName() + " " + d.toString();
recentPackageName = classByUsageStats;
}
}
} else {
recentPackageName = "not_kancolle_process";
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
if (appProcess.processName.contains(KC_PACKAGE_NAME)) {
recentPackageName = KC_PACKAGE_NAME;
} else if (appProcess.processName.contains(GOTO_PACKAGE_NAME)) {
recentPackageName = GOTO_PACKAGE_NAME;
}
}
}
}
return recentPackageName;
}
Aggregations