Add notification listener using the new shell command.

Adding notification listener by updating secure setting has
been deprecated.

Fixes: 65245599
Test: cts-tradefed run singleCommand cts-dev -m CtsHostsideNetworkTests -t \
      com.android.cts.net.HostsideRestrictBackgroundNetworkTests

Change-Id: I98dbff45bffed592cba2cd964f1a148450a1197d
diff --git a/hostsidetests/net/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java b/hostsidetests/net/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java
index 35e84c5..e65db31 100644
--- a/hostsidetests/net/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java
+++ b/hostsidetests/net/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java
@@ -27,12 +27,12 @@
 import java.util.concurrent.TimeUnit;
 
 import android.app.Instrumentation;
+import android.app.NotificationManager;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.content.pm.PackageManager;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 import android.net.NetworkInfo.DetailedState;
@@ -898,19 +898,12 @@
      * notification actions right after the notification is sent.
      */
     protected void registerNotificationListenerService() throws Exception {
-        final StringBuilder listeners = new StringBuilder(getNotificationListenerServices());
-        if (listeners.length() > 0) {
-            listeners.append(":");
-        }
-        listeners.append(MyNotificationListenerService.getId());
-        executeShellCommand("settings put secure enabled_notification_listeners " + listeners);
-        final String newListeners = getNotificationListenerServices();
-        assertEquals("Failed to set 'enabled_notification_listeners'",
-                listeners.toString(), newListeners);
-    }
-
-    private String getNotificationListenerServices() throws Exception {
-        return executeShellCommand("settings get secure enabled_notification_listeners");
+        executeShellCommand("cmd notification allow_listener "
+                + MyNotificationListenerService.getId());
+        final NotificationManager nm = mContext.getSystemService(NotificationManager.class);
+        final ComponentName listenerComponent = MyNotificationListenerService.getComponentName();
+        assertTrue(listenerComponent + " has not been granted access",
+                nm.isNotificationListenerAccessGranted(listenerComponent));
     }
 
     protected void setPendingIntentWhitelistDuration(int durationMs) throws Exception {
diff --git a/hostsidetests/net/app/src/com/android/cts/net/hostside/MyNotificationListenerService.java b/hostsidetests/net/app/src/com/android/cts/net/hostside/MyNotificationListenerService.java
index 0893511..0132536 100644
--- a/hostsidetests/net/app/src/com/android/cts/net/hostside/MyNotificationListenerService.java
+++ b/hostsidetests/net/app/src/com/android/cts/net/hostside/MyNotificationListenerService.java
@@ -19,6 +19,7 @@
 import android.app.PendingIntent;
 import android.app.PendingIntent.CanceledException;
 import android.app.RemoteInput;
+import android.content.ComponentName;
 import android.os.Bundle;
 import android.service.notification.NotificationListenerService;
 import android.service.notification.StatusBarNotification;
@@ -74,6 +75,11 @@
                 MyNotificationListenerService.class.getName());
     }
 
+    static ComponentName getComponentName() {
+        return new ComponentName(MyNotificationListenerService.class.getPackage().getName(),
+                MyNotificationListenerService.class.getName());
+    }
+
     private static final class PendingIntentSender {
         private PendingIntent mSentIntent = null;
         private String mReason = null;