From d5eadafa77a74d3792083a52b0659c46542d42e1 Mon Sep 17 00:00:00 2001 From: Geoffrey Pitsch Date: Mon, 30 Jan 2017 13:32:15 -0500 Subject: [PATCH] All cts uses notification channels and new Builder constructor Test: ran cts for all *Test.java files in CL Change-Id: I62f6eae53b539a1cfc79a05a2aa4070bf30fbfc0 --- .../net/hostside/app2/MyBroadcastReceiver.java | 5 +++-- .../net/hostside/app2/MyForegroundService.java | 10 ++++++++-- .../cts/net/hostside/app2/MyService.java | 18 ++++++++++++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java index f59cba1252..aa54075783 100644 --- a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java +++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java @@ -196,7 +196,8 @@ public class MyBroadcastReceiver extends BroadcastReceiver { * Sends a system notification containing actions with pending intents to launch the app's * main activitiy or service. */ - static void sendNotification(Context context, int notificationId, String notificationType ) { + static void sendNotification(Context context, String channelId, int notificationId, + String notificationType ) { Log.d(TAG, "sendNotification: id=" + notificationId + ", type=" + notificationType); final Intent serviceIntent = new Intent(context, MyService.class); final PendingIntent pendingIntent = PendingIntent.getService(context, 0, serviceIntent, @@ -204,7 +205,7 @@ public class MyBroadcastReceiver extends BroadcastReceiver { final Bundle bundle = new Bundle(); bundle.putCharSequence("parcelable", "I am not"); - final Notification.Builder builder = new Notification.Builder(context) + final Notification.Builder builder = new Notification.Builder(context, channelId) .setSmallIcon(R.drawable.ic_notification); Action action = null; diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyForegroundService.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyForegroundService.java index b88c45dbb4..fa3fdd148c 100644 --- a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyForegroundService.java +++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyForegroundService.java @@ -18,6 +18,8 @@ package com.android.cts.net.hostside.app2; import static com.android.cts.net.hostside.app2.Common.TAG; import android.R; import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.Service; import android.content.Intent; import android.os.IBinder; @@ -27,7 +29,7 @@ import android.util.Log; * Service used to change app state to FOREGROUND_SERVICE. */ public class MyForegroundService extends Service { - + private static final String NOTIFICATION_CHANNEL_ID = "cts/MyForegroundService"; private static final int FLAG_START_FOREGROUND = 1; private static final int FLAG_STOP_FOREGROUND = 2; @@ -39,10 +41,14 @@ public class MyForegroundService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.v(TAG, "MyForegroundService.onStartCommand(): " + intent); + NotificationManager notificationManager = getSystemService(NotificationManager.class); + notificationManager.createNotificationChannel(new NotificationChannel( + NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_ID, + NotificationManager.IMPORTANCE_DEFAULT)); switch (intent.getFlags()) { case FLAG_START_FOREGROUND: Log.d(TAG, "Starting foreground"); - startForeground(42, new Notification.Builder(this) + startForeground(42, new Notification.Builder(this, NOTIFICATION_CHANNEL_ID) .setSmallIcon(R.drawable.ic_dialog_alert) // any icon is fine .build()); break; diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java index 9c19e50238..2496c4ac7d 100644 --- a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java +++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java @@ -20,6 +20,8 @@ import static com.android.cts.net.hostside.app2.Common.ACTION_RECEIVER_READY; import static com.android.cts.net.hostside.app2.Common.DYNAMIC_RECEIVER; import static com.android.cts.net.hostside.app2.Common.TAG; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.Service; import android.content.Context; import android.content.Intent; @@ -36,6 +38,7 @@ import com.android.cts.net.hostside.IMyService; * Service used to dynamically register a broadcast receiver. */ public class MyService extends Service { + private static final String NOTIFICATION_CHANNEL_ID = "MyService"; private MyBroadcastReceiver mReceiver; @@ -75,8 +78,8 @@ public class MyService extends Service { @Override public void sendNotification(int notificationId, String notificationType) { - MyBroadcastReceiver - .sendNotification(getApplicationContext(), notificationId, notificationType); + MyBroadcastReceiver .sendNotification(getApplicationContext(), NOTIFICATION_CHANNEL_ID, + notificationId, notificationType); } }; @@ -85,8 +88,19 @@ public class MyService extends Service { return mBinder; } + @Override + public void onCreate() { + final Context context = getApplicationContext(); + ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)) + .createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID, + NOTIFICATION_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT)); + } + @Override public void onDestroy() { + final Context context = getApplicationContext(); + ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)) + .deleteNotificationChannel(NOTIFICATION_CHANNEL_ID); if (mReceiver != null) { Log.d(TAG, "onDestroy(): unregistering " + mReceiver); getApplicationContext().unregisterReceiver(mReceiver);