All cts uses notification channels and new Builder constructor

Test: ran cts for all *Test.java files in CL
Change-Id: I62f6eae53b539a1cfc79a05a2aa4070bf30fbfc0
This commit is contained in:
Geoffrey Pitsch
2017-01-30 13:32:15 -05:00
parent d4b573d247
commit d5eadafa77
3 changed files with 27 additions and 6 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);