Search in sources :

Example 26 with UsageEvents

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();
}
Also used : Event(android.app.usage.UsageEvents.Event) UsageEvents(android.app.usage.UsageEvents) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 27 with UsageEvents

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);
}
Also used : Event(android.app.usage.UsageEvents.Event) UsageEvents(android.app.usage.UsageEvents) Test(org.junit.Test)

Example 28 with UsageEvents

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();
}
Also used : Event(android.app.usage.UsageEvents.Event) UsageEvents(android.app.usage.UsageEvents) Test(org.junit.Test)

Example 29 with UsageEvents

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);
}
Also used : ArraySet(android.util.ArraySet) Parcel(android.os.Parcel) Event(android.app.usage.UsageEvents.Event) UsageEvents(android.app.usage.UsageEvents)

Example 30 with UsageEvents

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;
}
Also used : UsageStatsManager(android.app.usage.UsageStatsManager) MotionEvent(android.view.MotionEvent) UsageEvents(android.app.usage.UsageEvents) ActivityManager(android.app.ActivityManager) Date(java.util.Date) SuppressLint(android.annotation.SuppressLint)

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