From 0e36d959d0fc2dbf7bbec2d33f297b400ac01197 Mon Sep 17 00:00:00 2001 From: Julia Reynolds Date: Tue, 30 Mar 2021 16:32:48 -0400 Subject: Allow apps to migrate NLS filter settings On a per user vs manifest wide basis. Test: atest, cts-verifier Bug: 184027330 Change-Id: Iae591765e410e360f289692bac012a1f26574add --- core/java/android/app/INotificationManager.aidl | 1 + .../notification/NotificationListenerFilter.java | 14 +++++++++---- .../notification/NotificationListenerService.java | 23 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) (limited to 'core/java/android') diff --git a/core/java/android/app/INotificationManager.aidl b/core/java/android/app/INotificationManager.aidl index a5f8f103e437..858fa0605ea1 100644 --- a/core/java/android/app/INotificationManager.aidl +++ b/core/java/android/app/INotificationManager.aidl @@ -227,6 +227,7 @@ interface INotificationManager NotificationListenerFilter getListenerFilter(in ComponentName cn, int userId); void setListenerFilter(in ComponentName cn, int userId, in NotificationListenerFilter nlf); + void migrateNotificationFilter(in INotificationListener token, int defaultTypes, in List disallowedPkgs); void setToastRateLimitingEnabled(boolean enable); } diff --git a/core/java/android/service/notification/NotificationListenerFilter.java b/core/java/android/service/notification/NotificationListenerFilter.java index 9de75cac159a..a69d33c17e9d 100644 --- a/core/java/android/service/notification/NotificationListenerFilter.java +++ b/core/java/android/service/notification/NotificationListenerFilter.java @@ -31,15 +31,17 @@ import android.util.ArraySet; * @hide */ public class NotificationListenerFilter implements Parcelable { + + private static final int DEFAULT_TYPES = FLAG_FILTER_TYPE_CONVERSATIONS + | FLAG_FILTER_TYPE_ALERTING + | FLAG_FILTER_TYPE_SILENT + | FLAG_FILTER_TYPE_ONGOING; private int mAllowedNotificationTypes; // VersionedPackage is holding the pkg name and pkg uid private ArraySet mDisallowedPackages; public NotificationListenerFilter() { - mAllowedNotificationTypes = FLAG_FILTER_TYPE_CONVERSATIONS - | FLAG_FILTER_TYPE_ALERTING - | FLAG_FILTER_TYPE_SILENT - | FLAG_FILTER_TYPE_ONGOING; + mAllowedNotificationTypes = DEFAULT_TYPES; mDisallowedPackages = new ArraySet<>(); } @@ -80,6 +82,10 @@ public class NotificationListenerFilter implements Parcelable { return (mAllowedNotificationTypes & type) != 0; } + public boolean areAllTypesAllowed() { + return DEFAULT_TYPES == mAllowedNotificationTypes; + } + public boolean isPackageAllowed(VersionedPackage pkg) { return !mDisallowedPackages.contains(pkg); } diff --git a/core/java/android/service/notification/NotificationListenerService.java b/core/java/android/service/notification/NotificationListenerService.java index 09c3b2effcb0..6c775852fcee 100644 --- a/core/java/android/service/notification/NotificationListenerService.java +++ b/core/java/android/service/notification/NotificationListenerService.java @@ -792,6 +792,29 @@ public abstract class NotificationListenerService extends Service { } } + /** + * Lets an app migrate notification filters from its app into the OS. + * + *

This call will be ignored if the app has already migrated these settings or the user + * has set filters in the UI. This method is intended for user specific settings; if an app has + * already specified defaults types in its manifest with + * {@link #META_DATA_DEFAULT_FILTER_TYPES}, the defaultTypes option will be ignored.

+ * @param defaultTypes A value representing the types of notifications that this listener should + * receive by default + * @param disallowedPkgs A list of package names whose notifications should not be seen by this + * listener, by default, because the listener does not process or display them, or because a + * user had previously disallowed these packages in the listener app's UI + */ + public final void migrateNotificationFilter(@NotificationFilterTypes int defaultTypes, + @Nullable List disallowedPkgs) { + if (!isBound()) return; + try { + getNotificationInterface().migrateNotificationFilter( + mWrapper, defaultTypes, disallowedPkgs); + } catch (android.os.RemoteException ex) { + Log.v(TAG, "Unable to contact notification manager", ex); + } + } /** * Inform the notification manager that these notifications have been viewed by the -- cgit v1.2.3