1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.telecom.cts.apps; 17 18 import android.app.Notification; 19 import android.app.NotificationManager; 20 import android.app.PendingIntent; 21 import android.app.Person; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.service.notification.StatusBarNotification; 25 import android.util.Log; 26 27 public class NotificationUtils { 28 private static final String TAG = NotificationUtils.class.getSimpleName(); 29 private static final String CALL_STYLE_INITIAL_TEXT = "New Call"; 30 private static final String CALL_STYLE_ONGOING_TEXT = "Ongoing Call"; 31 private static final String CALL_STYLE_TITLE = "Telecom CTS Test App"; 32 createCallStyleNotification( Context context, String channelId, String callerName, boolean isOutgoing)33 public static Notification createCallStyleNotification( 34 Context context, 35 String channelId, 36 String callerName, 37 boolean isOutgoing) { 38 PendingIntent fullScreenIntent = createPendingIntent(context); 39 Person person = new Person.Builder().setName(callerName).setImportant(true).build(); 40 return new Notification.Builder(context, channelId) 41 .setContentText(CALL_STYLE_INITIAL_TEXT) 42 .setContentTitle(CALL_STYLE_TITLE) 43 .setSmallIcon(android.R.drawable.sym_def_app_icon) 44 .setStyle(getCallStyle(isOutgoing, person, fullScreenIntent)) 45 .setFullScreenIntent(fullScreenIntent, true) 46 .setOngoing(isOutgoing) 47 .build(); 48 } 49 getCallStyle( boolean isOutgoing, Person person, PendingIntent fullScreenIntent)50 private static Notification.CallStyle getCallStyle( 51 boolean isOutgoing, 52 Person person, 53 PendingIntent fullScreenIntent) { 54 if (isOutgoing) { 55 return Notification.CallStyle.forOngoingCall( 56 person, 57 fullScreenIntent); 58 } else { 59 return Notification.CallStyle.forIncomingCall( 60 person, 61 fullScreenIntent, 62 fullScreenIntent); 63 } 64 } 65 updateNotificationToOngoing(Context context, String channelId, String callerName, int notificationId)66 public static void updateNotificationToOngoing(Context context, 67 String channelId, 68 String callerName, 69 int notificationId) { 70 PendingIntent ongoingCall = createPendingIntent(context); 71 72 Notification callStyleNotification = new Notification.Builder(context, 73 channelId) 74 .setContentText(CALL_STYLE_ONGOING_TEXT) 75 .setContentTitle(CALL_STYLE_TITLE) 76 .setSmallIcon(android.R.drawable.sym_def_app_icon) 77 .setStyle(Notification.CallStyle.forOngoingCall( 78 new Person.Builder().setName(callerName).setImportant(true).build(), 79 ongoingCall) 80 ) 81 .setFullScreenIntent(ongoingCall, true) 82 .setOngoing(true) 83 .build(); 84 85 NotificationManager notificationManager = 86 context.getSystemService(NotificationManager.class); 87 88 notificationManager.notify(notificationId, callStyleNotification); 89 } 90 createPendingIntent(Context context)91 private static PendingIntent createPendingIntent(Context context) { 92 return PendingIntent.getActivity(context, 0, 93 new Intent(context, context.getClass()), PendingIntent.FLAG_IMMUTABLE); 94 } 95 clearNotification(Context c, int notificationId)96 public static void clearNotification(Context c, int notificationId) { 97 NotificationManager notificationManager = c.getSystemService(NotificationManager.class); 98 if (notificationManager != null) { 99 notificationManager.cancel(notificationId); 100 } 101 } 102 deleteNotificationChannel(Context c, String notificationChannelId)103 public static void deleteNotificationChannel(Context c, String notificationChannelId){ 104 NotificationManager notificationManager = c.getSystemService(NotificationManager.class); 105 try { 106 // deleteNotificationChannel has sometimes thrown exceptions causing test failures 107 notificationManager.deleteNotificationChannel(notificationChannelId); 108 } 109 catch (Exception e){ 110 Log.i(TAG, String.format("onUnbind: hit exception=[%s] while deleting the" 111 + " call channel with id=[%s]", e, notificationChannelId)); 112 } 113 } 114 isTargetNotificationPosted(Context c, int targetNotificationId)115 public static boolean isTargetNotificationPosted(Context c, int targetNotificationId) { 116 NotificationManager notificationManager = c.getSystemService(NotificationManager.class); 117 StatusBarNotification[] sbinArray = notificationManager.getActiveNotifications(); 118 for (StatusBarNotification sbn : sbinArray) { 119 if (sbn.getId() == targetNotificationId) { 120 return true; 121 } 122 } 123 return false; 124 } 125 } 126