Migrate managed services from setting to policy xml

Change-Id: Ie20f91dbdd0ba6b57b5909cbf0152a32754fe02d
Fixes: 62263757
Test: runtest systemui-notification, cts AudioManagerTest,
cts-verifier DND tests, verify bug reports after toggling
access for various types of managed services, verified
default approved services aren't renabled on boot; verified that
they are reenabled after a device reset, verified that
settings are migrated after a restore from OC backup.
diff --git a/core/java/android/app/INotificationManager.aidl b/core/java/android/app/INotificationManager.aidl
index 1c1883b..08821be 100644
--- a/core/java/android/app/INotificationManager.aidl
+++ b/core/java/android/app/INotificationManager.aidl
@@ -116,6 +116,16 @@
     boolean matchesCallFilter(in Bundle extras);
     boolean isSystemConditionProviderEnabled(String path);
 
+    boolean isNotificationListenerAccessGranted(in ComponentName listener);
+    boolean isNotificationListenerAccessGrantedForUser(in ComponentName listener, int userId);
+    boolean isNotificationAssistantAccessGranted(in ComponentName assistant);
+    void setNotificationListenerAccessGranted(in ComponentName listener, boolean enabled);
+    void setNotificationAssistantAccessGranted(in ComponentName assistant, boolean enabled);
+    void setNotificationListenerAccessGrantedForUser(in ComponentName listener, int userId, boolean enabled);
+    void setNotificationAssistantAccessGrantedForUser(in ComponentName assistant, int userId, boolean enabled);
+    List<String> getEnabledNotificationListenerPackages();
+    List<ComponentName> getEnabledNotificationListeners(int userId);
+
     int getZenMode();
     ZenModeConfig getZenModeConfig();
     oneway void setZenMode(int mode, in Uri conditionId, String reason);
@@ -123,7 +133,6 @@
     boolean isNotificationPolicyAccessGranted(String pkg);
     NotificationManager.Policy getNotificationPolicy(String pkg);
     void setNotificationPolicy(String pkg, in NotificationManager.Policy policy);
-    String[] getPackagesRequestingNotificationPolicyAccess();
     boolean isNotificationPolicyAccessGrantedForPackage(String pkg);
     void setNotificationPolicyAccessGranted(String pkg, boolean granted);
     AutomaticZenRule getAutomaticZenRule(String id);
diff --git a/core/java/android/app/NotificationManager.java b/core/java/android/app/NotificationManager.java
index 235b8d4..a519125 100644
--- a/core/java/android/app/NotificationManager.java
+++ b/core/java/android/app/NotificationManager.java
@@ -18,14 +18,12 @@
 
 import android.annotation.IntDef;
 import android.annotation.NonNull;
-import android.annotation.Nullable;
 import android.annotation.SdkConstant;
 import android.annotation.SystemService;
 import android.annotation.TestApi;
 import android.app.Notification.Builder;
 import android.content.ComponentName;
 import android.content.Context;
-import android.content.Intent;
 import android.content.pm.ParceledListSlice;
 import android.graphics.drawable.Icon;
 import android.net.Uri;
@@ -33,7 +31,6 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
-import android.os.Looper;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.os.RemoteException;
@@ -41,10 +38,8 @@
 import android.os.StrictMode;
 import android.os.UserHandle;
 import android.provider.Settings.Global;
-import android.service.notification.NotificationListenerService.Ranking;
 import android.service.notification.StatusBarNotification;
 import android.service.notification.ZenModeConfig;
-import android.util.ArraySet;
 import android.util.Log;
 
 import java.lang.annotation.Retention;
@@ -747,14 +742,14 @@
     }
 
     /**
-     * Checks the ability to read/modify notification policy for the calling package.
+     * Checks the ability to read/modify notification do not disturb policy for the calling package.
      *
      * <p>
      * Returns true if the calling package can read/modify notification policy.
      *
      * <p>
-     * Request policy access by sending the user to the activity that matches the system intent
-     * action {@link android.provider.Settings#ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS}.
+     * Apps can request policy access by sending the user to the activity that matches the system
+     * intent action {@link android.provider.Settings#ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS}.
      *
      * <p>
      * Use {@link #ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED} to listen for
@@ -769,6 +764,39 @@
         }
     }
 
+    /**
+     * Checks whether the user has approved a given
+     * {@link android.service.notification.NotificationListenerService}.
+     *
+     * <p>
+     * The listener service must belong to the calling app.
+     *
+     * <p>
+     * Apps can request notification listener access by sending the user to the activity that
+     * matches the system intent action
+     * {@link android.provider.Settings#ACTION_NOTIFICATION_LISTENER_SETTINGS}.
+     */
+    public boolean isNotificationListenerAccessGranted(ComponentName listener) {
+        INotificationManager service = getService();
+        try {
+            return service.isNotificationListenerAccessGranted(listener);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * @hide
+     */
+    public boolean isNotificationAssistantAccessGranted(ComponentName assistant) {
+        INotificationManager service = getService();
+        try {
+            return service.isNotificationAssistantAccessGranted(assistant);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
     /** @hide */
     public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {
         INotificationManager service = getService();
@@ -780,6 +808,18 @@
     }
 
     /**
+     * @hide
+     */
+    public List<String> getEnabledNotificationListenerPackages() {
+        INotificationManager service = getService();
+        try {
+            return service.getEnabledNotificationListenerPackages();
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
      * Gets the current notification policy.
      *
      * <p>
@@ -825,21 +865,34 @@
     }
 
     /** @hide */
-    public ArraySet<String> getPackagesRequestingNotificationPolicyAccess() {
+    public void setNotificationListenerAccessGranted(ComponentName listener, boolean granted) {
         INotificationManager service = getService();
         try {
-            final String[] pkgs = service.getPackagesRequestingNotificationPolicyAccess();
-            if (pkgs != null && pkgs.length > 0) {
-                final ArraySet<String> rt = new ArraySet<>(pkgs.length);
-                for (int i = 0; i < pkgs.length; i++) {
-                    rt.add(pkgs[i]);
-                }
-                return rt;
-            }
+            service.setNotificationListenerAccessGranted(listener, granted);
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
-        return new ArraySet<>();
+    }
+
+    /** @hide */
+    public void setNotificationListenerAccessGrantedForUser(ComponentName listener, int userId,
+            boolean granted) {
+        INotificationManager service = getService();
+        try {
+            service.setNotificationListenerAccessGrantedForUser(listener, userId, granted);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /** @hide */
+    public List<ComponentName> getEnabledNotificationListeners(int userId) {
+        INotificationManager service = getService();
+        try {
+            return service.getEnabledNotificationListeners(userId);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
     }
 
     private Context mContext;
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index ed2b0b2..f5b5006 100755
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -31,6 +31,7 @@
 import android.app.AppOpsManager;
 import android.app.Application;
 import android.app.NotificationChannel;
+import android.app.NotificationManager;
 import android.app.SearchManager;
 import android.app.WallpaperManager;
 import android.content.ComponentName;
@@ -6607,28 +6608,36 @@
         public static final String ASSIST_DISCLOSURE_ENABLED = "assist_disclosure_enabled";
 
         /**
-         * Name of the service components that the current user has explicitly allowed to
+         * Read only list of the service components that the current user has explicitly allowed to
          * see and assist with all of the user's notifications.
          *
+         * @deprecated Use
+         * {@link NotificationManager#isNotificationListenerAccessGranted(ComponentName)}.
          * @hide
          */
+        @Deprecated
         public static final String ENABLED_NOTIFICATION_ASSISTANT =
                 "enabled_notification_assistant";
 
         /**
-         * Names of the service components that the current user has explicitly allowed to
+         * Read only list of the service components that the current user has explicitly allowed to
          * see all of the user's notifications, separated by ':'.
          *
          * @hide
+         * @deprecated Use
+         * {@link NotificationManager#isNotificationAssistantAccessGranted(ComponentName)}.
          */
+        @Deprecated
         public static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners";
 
         /**
-         * Names of the packages that the current user has explicitly allowed to
-         * manage notification policy configuration, separated by ':'.
+         * Read only list of the packages that the current user has explicitly allowed to
+         * manage do not disturb, separated by ':'.
          *
+         * @deprecated Use {@link NotificationManager#isNotificationPolicyAccessGranted()}.
          * @hide
          */
+        @Deprecated
         @TestApi
         public static final String ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES =
                 "enabled_notification_policy_access_packages";
@@ -7049,7 +7058,6 @@
             AUTOFILL_SERVICE,
             ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE,
             ENABLED_ACCESSIBILITY_SERVICES,
-            ENABLED_NOTIFICATION_LISTENERS,
             ENABLED_VR_LISTENERS,
             ENABLED_INPUT_METHODS,
             TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
@@ -7128,6 +7136,9 @@
 
         /** @hide */
         public static final String[] LEGACY_RESTORE_SETTINGS = {
+                ENABLED_NOTIFICATION_LISTENERS,
+                ENABLED_NOTIFICATION_ASSISTANT,
+                ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES
         };
 
         /**
diff --git a/core/tests/coretests/src/android/provider/SettingsBackupTest.java b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
index 7dc72db7..53ba9f7 100644
--- a/core/tests/coretests/src/android/provider/SettingsBackupTest.java
+++ b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
@@ -17,24 +17,25 @@
 package android.provider;
 
 import static com.google.android.collect.Sets.newHashSet;
+
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.empty;
 import static org.hamcrest.Matchers.is;
+
 import static java.lang.reflect.Modifier.isFinal;
 import static java.lang.reflect.Modifier.isPublic;
 import static java.lang.reflect.Modifier.isStatic;
 
-import android.platform.test.annotations.Presubmit;
 import android.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
 
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
 import java.lang.reflect.Field;
 import java.util.HashSet;
 import java.util.Set;
 
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
 /** Tests that ensure appropriate settings are backed up. */
 @RunWith(AndroidJUnit4.class)
 @SmallTest
@@ -428,6 +429,7 @@
                  Settings.Secure.DOZE_ALWAYS_ON,
                  Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION,
                  Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT,
+                 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS,
                  Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES,
                  Settings.Secure.ENABLED_PRINT_SERVICES,
                  Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS,