Search in sources :

Example 1 with NotifyingApp

use of android.service.notification.NotifyingApp 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 2 with NotifyingApp

use of android.service.notification.NotifyingApp 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 3 with NotifyingApp

use of android.service.notification.NotifyingApp in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class RecentNotifyingAppsPreferenceController method getDisplayableRecentAppList.

private List<NotifyingApp> getDisplayableRecentAppList() {
    Collections.sort(mApps);
    List<NotifyingApp> displayableApps = new ArrayList<>(SHOW_RECENT_APP_COUNT);
    int count = 0;
    for (NotifyingApp app : mApps) {
        final ApplicationsState.AppEntry appEntry = mApplicationsState.getEntry(app.getPackage(), app.getUserId());
        if (appEntry == null) {
            continue;
        }
        if (!shouldIncludePkgInRecents(app.getPackage(), app.getUserId())) {
            continue;
        }
        displayableApps.add(app);
        count++;
        if (count >= SHOW_RECENT_APP_COUNT) {
            break;
        }
    }
    return displayableApps;
}
Also used : ApplicationsState(com.android.settingslib.applications.ApplicationsState) ArrayList(java.util.ArrayList) NotifyingApp(android.service.notification.NotifyingApp)

Example 4 with NotifyingApp

use of android.service.notification.NotifyingApp in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class RecentNotifyingAppsPreferenceController method displayRecentApps.

private void displayRecentApps(Context prefContext, List<NotifyingApp> recentApps) {
    mCategory.setTitle(R.string.recent_notifications);
    mDivider.setVisible(true);
    mSeeAllPref.setSummary(null);
    mSeeAllPref.setIcon(R.drawable.ic_chevron_right_24dp);
    // Rebind prefs/avoid adding new prefs if possible. Adding/removing prefs causes jank.
    // Build a cached preference pool
    final Map<String, NotificationAppPreference> appPreferences = new ArrayMap<>();
    int prefCount = mCategory.getPreferenceCount();
    for (int i = 0; i < prefCount; i++) {
        final Preference pref = mCategory.getPreference(i);
        final String key = pref.getKey();
        if (!TextUtils.equals(key, KEY_SEE_ALL)) {
            appPreferences.put(key, (NotificationAppPreference) pref);
        }
    }
    final int recentAppsCount = recentApps.size();
    for (int i = 0; i < recentAppsCount; i++) {
        final NotifyingApp app = recentApps.get(i);
        // Bind recent apps to existing prefs if possible, or create a new pref.
        final String pkgName = app.getPackage();
        final ApplicationsState.AppEntry appEntry = mApplicationsState.getEntry(app.getPackage(), app.getUserId());
        if (appEntry == null) {
            continue;
        }
        boolean rebindPref = true;
        NotificationAppPreference pref = appPreferences.remove(getKey(app.getUserId(), pkgName));
        if (pref == null) {
            pref = new NotificationAppPreference(prefContext);
            rebindPref = false;
        }
        pref.setKey(getKey(app.getUserId(), pkgName));
        pref.setTitle(appEntry.label);
        pref.setIcon(mIconDrawableFactory.getBadgedIcon(appEntry.info));
        pref.setIconSize(TwoTargetPreference.ICON_SIZE_SMALL);
        pref.setSummary(StringUtil.formatRelativeTime(mContext, System.currentTimeMillis() - app.getLastNotified(), true));
        pref.setOrder(i);
        Bundle args = new Bundle();
        args.putString(AppInfoBase.ARG_PACKAGE_NAME, pkgName);
        args.putInt(AppInfoBase.ARG_PACKAGE_UID, appEntry.info.uid);
        pref.setOnPreferenceClickListener(preference -> {
            new SubSettingLauncher(mHost.getActivity()).setDestination(AppNotificationSettings.class.getName()).setTitleRes(R.string.notifications_title).setArguments(args).setUserHandle(new UserHandle(UserHandle.getUserId(appEntry.info.uid))).setSourceMetricsCategory(SettingsEnums.MANAGE_APPLICATIONS_NOTIFICATIONS).launch();
            return true;
        });
        pref.setSwitchEnabled(mNotificationBackend.isBlockable(mContext, appEntry.info));
        pref.setOnPreferenceChangeListener((preference, newValue) -> {
            boolean blocked = !(Boolean) newValue;
            mNotificationBackend.setNotificationsEnabledForPackage(pkgName, appEntry.info.uid, !blocked);
            return true;
        });
        pref.setChecked(!mNotificationBackend.getNotificationsBanned(pkgName, appEntry.info.uid));
        if (!rebindPref) {
            mCategory.addPreference(pref);
        }
    }
    // Remove unused prefs from pref cache pool
    for (Preference unusedPrefs : appPreferences.values()) {
        mCategory.removePreference(unusedPrefs);
    }
}
Also used : ApplicationsState(com.android.settingslib.applications.ApplicationsState) Bundle(android.os.Bundle) ArrayMap(android.util.ArrayMap) Preference(androidx.preference.Preference) TwoTargetPreference(com.android.settingslib.TwoTargetPreference) SubSettingLauncher(com.android.settings.core.SubSettingLauncher) UserHandle(android.os.UserHandle) NotifyingApp(android.service.notification.NotifyingApp)

Aggregations

NotifyingApp (android.service.notification.NotifyingApp)4 UsageEvents (android.app.usage.UsageEvents)2 ArrayMap (android.util.ArrayMap)2 ApplicationsState (com.android.settingslib.applications.ApplicationsState)2 ArrayList (java.util.ArrayList)2 Event (android.app.usage.UsageEvents.Event)1 Bundle (android.os.Bundle)1 RemoteException (android.os.RemoteException)1 UserHandle (android.os.UserHandle)1 VisibleForTesting (androidx.annotation.VisibleForTesting)1 Preference (androidx.preference.Preference)1 SubSettingLauncher (com.android.settings.core.SubSettingLauncher)1 TwoTargetPreference (com.android.settingslib.TwoTargetPreference)1 Test (org.junit.Test)1