1 /* 2 * Copyright (C) 2013 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 17 package com.android.server.telecom.testapps; 18 19 import com.android.server.telecom.testapps.R; 20 21 import android.app.Notification; 22 import android.app.NotificationManager; 23 import android.app.PendingIntent; 24 import android.content.ComponentName; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.graphics.Color; 28 import android.graphics.drawable.Icon; 29 import android.net.Uri; 30 import android.os.Bundle; 31 import android.telecom.PhoneAccount; 32 import android.telecom.PhoneAccountHandle; 33 import android.telecom.TelecomManager; 34 import android.util.Log; 35 import android.widget.Toast; 36 37 import java.util.Arrays; 38 import java.util.List; 39 40 /** 41 * Class used to create, update and cancel the notification used to display and update call state 42 * for {@link TestConnectionService}. 43 */ 44 public class CallServiceNotifier { 45 private static final CallServiceNotifier INSTANCE = new CallServiceNotifier(); 46 47 static final String CALL_PROVIDER_ID = "testapps_TestConnectionService_CALL_PROVIDER_ID"; 48 static final String SIM_SUBSCRIPTION_ID = "testapps_TestConnectionService_SIM_SUBSCRIPTION_ID"; 49 static final String CONNECTION_MANAGER_ID = 50 "testapps_TestConnectionService_CONNECTION_MANAGER_ID"; 51 52 /** 53 * Static notification IDs. 54 */ 55 private static final int CALL_NOTIFICATION_ID = 1; 56 private static final int PHONE_ACCOUNT_NOTIFICATION_ID = 2; 57 58 /** 59 * Whether the added call should be started as a video call. Referenced by 60 * {@link TestConnectionService} to know whether to provide a call video provider. 61 */ 62 public static boolean mStartVideoCall; 63 64 /** 65 * Singleton accessor. 66 */ getInstance()67 public static CallServiceNotifier getInstance() { 68 return INSTANCE; 69 } 70 71 /** 72 * Creates a CallService & initializes notification manager. 73 */ CallServiceNotifier()74 private CallServiceNotifier() { 75 } 76 77 /** 78 * Updates the notification in the notification pane. 79 */ updateNotification(Context context)80 public void updateNotification(Context context) { 81 log("adding the notification ------------"); 82 getNotificationManager(context).notify(CALL_NOTIFICATION_ID, getMainNotification(context)); 83 getNotificationManager(context).notify( 84 PHONE_ACCOUNT_NOTIFICATION_ID, getPhoneAccountNotification(context)); 85 } 86 87 /** 88 * Cancels the notification. 89 */ cancelNotifications(Context context)90 public void cancelNotifications(Context context) { 91 log("canceling notification"); 92 getNotificationManager(context).cancel(CALL_NOTIFICATION_ID); 93 getNotificationManager(context).cancel(PHONE_ACCOUNT_NOTIFICATION_ID); 94 } 95 96 /** 97 * Registers a phone account with telecom. 98 */ registerPhoneAccount(Context context)99 public void registerPhoneAccount(Context context) { 100 TelecomManager telecomManager = 101 (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); 102 103 telecomManager.clearAccounts(); 104 105 Bundle testBundle = new Bundle(); 106 testBundle.putInt("EXTRA_INT_1", 1); 107 testBundle.putInt("EXTRA_INT_100", 100); 108 testBundle.putBoolean("EXTRA_BOOL_TRUE", true); 109 testBundle.putBoolean("EXTRA_BOOL_FALSE", false); 110 testBundle.putString("EXTRA_STR1", "Hello"); 111 testBundle.putString("EXTRA_STR2", "There"); 112 113 telecomManager.registerPhoneAccount(PhoneAccount.builder( 114 new PhoneAccountHandle( 115 new ComponentName(context, TestConnectionService.class), 116 CALL_PROVIDER_ID), 117 "TelecomTestApp Call Provider") 118 .setAddress(Uri.parse("tel:555-TEST")) 119 .setSubscriptionAddress(Uri.parse("tel:555-TEST")) 120 .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER | 121 PhoneAccount.CAPABILITY_VIDEO_CALLING | 122 PhoneAccount.CAPABILITY_VIDEO_CALLING_RELIES_ON_PRESENCE) 123 .setIcon(Icon.createWithResource( 124 context.getResources(), R.drawable.stat_sys_phone_call)) 125 .setHighlightColor(Color.RED) 126 // TODO: Add icon tint (Color.RED) 127 .setShortDescription("a short description for the call provider") 128 .setSupportedUriSchemes(Arrays.asList("tel")) 129 .setExtras(testBundle) 130 .build()); 131 132 telecomManager.registerPhoneAccount(PhoneAccount.builder( 133 new PhoneAccountHandle( 134 new ComponentName(context, TestConnectionService.class), 135 SIM_SUBSCRIPTION_ID), 136 "TelecomTestApp SIM Subscription") 137 .setAddress(Uri.parse("tel:555-TSIM")) 138 .setSubscriptionAddress(Uri.parse("tel:555-TSIM")) 139 .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER | 140 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION | 141 PhoneAccount.CAPABILITY_VIDEO_CALLING | 142 PhoneAccount.CAPABILITY_VIDEO_CALLING_RELIES_ON_PRESENCE) 143 .setIcon(Icon.createWithResource( 144 context.getResources(), R.drawable.stat_sys_phone_call)) 145 .setHighlightColor(Color.GREEN) 146 // TODO: Add icon tint (Color.GREEN) 147 .setShortDescription("a short description for the sim subscription") 148 .build()); 149 150 telecomManager.registerPhoneAccount(PhoneAccount.builder( 151 new PhoneAccountHandle( 152 new ComponentName(context, TestConnectionManager.class), 153 CONNECTION_MANAGER_ID), 154 "TelecomTestApp CONNECTION MANAGER") 155 .setAddress(Uri.parse("tel:555-CMGR")) 156 .setSubscriptionAddress(Uri.parse("tel:555-CMGR")) 157 .setCapabilities(PhoneAccount.CAPABILITY_CONNECTION_MANAGER) 158 .setIcon(Icon.createWithResource( 159 context.getResources(), R.drawable.stat_sys_phone_call)) 160 // TODO: Add icon tint (Color.BLUE) 161 .setShortDescription("a short description for the connection manager") 162 .build()); 163 } 164 165 /** 166 * Displays all phone accounts registered with telecom. 167 */ showAllPhoneAccounts(Context context)168 public void showAllPhoneAccounts(Context context) { 169 TelecomManager telecomManager = 170 (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); 171 List<PhoneAccountHandle> accounts = telecomManager.getCallCapablePhoneAccounts(); 172 173 Toast.makeText(context, accounts.toString(), Toast.LENGTH_LONG).show(); 174 } 175 176 /** 177 * Returns the system's notification manager needed to add/remove notifications. 178 */ getNotificationManager(Context context)179 private NotificationManager getNotificationManager(Context context) { 180 return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 181 } 182 183 /** 184 * Creates a notification object for using the telecom APIs. 185 */ getPhoneAccountNotification(Context context)186 private Notification getPhoneAccountNotification(Context context) { 187 final Notification.Builder builder = new Notification.Builder(context); 188 // Both notifications have buttons and only the first one with buttons will show its 189 // buttons. Since the phone accounts notification is always first, setting false ensures 190 // it can be dismissed to use the other notification. 191 builder.setOngoing(false); 192 builder.setPriority(Notification.PRIORITY_HIGH); 193 194 final PendingIntent intent = createShowAllPhoneAccountsIntent(context); 195 builder.setContentIntent(intent); 196 197 builder.setSmallIcon(android.R.drawable.stat_sys_phone_call); 198 // TODO: Consider moving this into a strings.xml 199 builder.setContentText("Test phone accounts via telecom APIs."); 200 builder.setContentTitle("Test Phone Accounts"); 201 202 addRegisterPhoneAccountAction(builder, context); 203 addShowAllPhoneAccountsAction(builder, context); 204 205 return builder.build(); 206 } 207 208 /** 209 * Creates a notification object out of the current calls state. 210 */ getMainNotification(Context context)211 private Notification getMainNotification(Context context) { 212 final Notification.Builder builder = new Notification.Builder(context); 213 builder.setOngoing(true); 214 builder.setPriority(Notification.PRIORITY_HIGH); 215 builder.setSmallIcon(android.R.drawable.stat_sys_phone_call); 216 builder.setContentText("Test calls via CallService API"); 217 builder.setContentTitle("Test Connection Service"); 218 219 addAddOneWayVideoCallAction(builder, context); 220 addAddTwoWayVideoCallAction(builder, context); 221 addAddCallAction(builder, context); 222 addExitAction(builder, context); 223 224 return builder.build(); 225 } 226 227 /** 228 * Creates the intent to remove the notification. 229 */ createExitIntent(Context context)230 private PendingIntent createExitIntent(Context context) { 231 final Intent intent = new Intent(CallNotificationReceiver.ACTION_CALL_SERVICE_EXIT, null, 232 context, CallNotificationReceiver.class); 233 234 return PendingIntent.getBroadcast(context, 0, intent, 0); 235 } 236 237 /** 238 * Creates the intent to register a phone account. 239 */ createRegisterPhoneAccountIntent(Context context)240 private PendingIntent createRegisterPhoneAccountIntent(Context context) { 241 final Intent intent = new Intent(CallNotificationReceiver.ACTION_REGISTER_PHONE_ACCOUNT, 242 null, context, CallNotificationReceiver.class); 243 return PendingIntent.getBroadcast(context, 0, intent, 0); 244 } 245 246 /** 247 * Creates the intent to show all phone accounts. 248 */ createShowAllPhoneAccountsIntent(Context context)249 private PendingIntent createShowAllPhoneAccountsIntent(Context context) { 250 final Intent intent = new Intent(CallNotificationReceiver.ACTION_SHOW_ALL_PHONE_ACCOUNTS, 251 null, context, CallNotificationReceiver.class); 252 return PendingIntent.getBroadcast(context, 0, intent, 0); 253 } 254 255 /** 256 * Creates the intent to start an incoming 1-way video call (receive-only) 257 */ createOneWayVideoCallIntent(Context context)258 private PendingIntent createOneWayVideoCallIntent(Context context) { 259 final Intent intent = new Intent(CallNotificationReceiver.ACTION_ONE_WAY_VIDEO_CALL, 260 null, context, CallNotificationReceiver.class); 261 return PendingIntent.getBroadcast(context, 0, intent, 0); 262 } 263 264 /** 265 * Creates the intent to start an incoming 2-way video call 266 */ createTwoWayVideoCallIntent(Context context)267 private PendingIntent createTwoWayVideoCallIntent(Context context) { 268 final Intent intent = new Intent(CallNotificationReceiver.ACTION_TWO_WAY_VIDEO_CALL, 269 null, context, CallNotificationReceiver.class); 270 return PendingIntent.getBroadcast(context, 0, intent, 0); 271 } 272 273 /** 274 * Creates the intent to start an incoming audio call 275 */ createIncomingAudioCall(Context context)276 private PendingIntent createIncomingAudioCall(Context context) { 277 final Intent intent = new Intent(CallNotificationReceiver.ACTION_AUDIO_CALL, 278 null, context, CallNotificationReceiver.class); 279 return PendingIntent.getBroadcast(context, 0, intent, 0); 280 } 281 282 /** 283 * Adds an action to the Notification Builder for adding an incoming call through Telecom. 284 * @param builder The Notification Builder. 285 */ addAddCallAction(Notification.Builder builder, Context context)286 private void addAddCallAction(Notification.Builder builder, Context context) { 287 builder.addAction(0, "Add Call", createIncomingAudioCall(context)); 288 } 289 290 /** 291 * Adds an action to the Notification Builder to add an incoming one-way video call through 292 * Telecom. 293 */ addAddOneWayVideoCallAction(Notification.Builder builder, Context context)294 private void addAddOneWayVideoCallAction(Notification.Builder builder, Context context) { 295 builder.addAction(0, "1-way Video", createOneWayVideoCallIntent(context)); 296 } 297 298 /** 299 * Adds an action to the Notification Builder to add an incoming 2-way video call through 300 * Telecom. 301 */ addAddTwoWayVideoCallAction(Notification.Builder builder, Context context)302 private void addAddTwoWayVideoCallAction(Notification.Builder builder, Context context) { 303 builder.addAction(0, "2-way Video", createTwoWayVideoCallIntent(context)); 304 } 305 306 /** 307 * Adds an action to remove the notification. 308 */ addExitAction(Notification.Builder builder, Context context)309 private void addExitAction(Notification.Builder builder, Context context) { 310 builder.addAction(0, "Exit", createExitIntent(context)); 311 } 312 313 /** 314 * Adds an action to show all registered phone accounts on a device. 315 */ addShowAllPhoneAccountsAction(Notification.Builder builder, Context context)316 private void addShowAllPhoneAccountsAction(Notification.Builder builder, Context context) { 317 builder.addAction(0, "Show Accts", createShowAllPhoneAccountsIntent(context)); 318 } 319 320 /** 321 * Adds an action to register a new phone account. 322 */ addRegisterPhoneAccountAction(Notification.Builder builder, Context context)323 private void addRegisterPhoneAccountAction(Notification.Builder builder, Context context) { 324 builder.addAction(0, "Reg.Acct.", createRegisterPhoneAccountIntent(context)); 325 } 326 shouldStartVideoCall()327 public boolean shouldStartVideoCall() { 328 return mStartVideoCall; 329 } 330 log(String msg)331 private static void log(String msg) { 332 Log.w("testcallservice", "[CallServiceNotifier] " + msg); 333 } 334 } 335