From 46ec0e97b483b17e5f39f25f5e4aafdca831c099 Mon Sep 17 00:00:00 2001 From: Sudheer Shanka Date: Thu, 31 Aug 2017 17:42:41 -0700 Subject: [PATCH] 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 --- ...ractRestrictBackgroundNetworkTestCase.java | 21 +++++++------------ .../MyNotificationListenerService.java | 6 ++++++ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java index 35e84c5f19..e65db3197f 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java @@ -27,12 +27,12 @@ import java.util.concurrent.LinkedBlockingQueue; 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 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation * 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/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyNotificationListenerService.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyNotificationListenerService.java index 0893511071..013253670a 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyNotificationListenerService.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyNotificationListenerService.java @@ -19,6 +19,7 @@ import android.app.Notification; 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 @@ public class MyNotificationListenerService extends NotificationListenerService { 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;