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