blob: 770b75dbd6e69a0e073dbf274dfcba9e948ff601 [file] [log] [blame]
Yorke Lee33501632014-03-17 19:24:12 -07001/*
2 * Copyright (C) 2014 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.telecomm;
18
19import android.app.Activity;
Yorke Lee33501632014-03-17 19:24:12 -070020import android.content.BroadcastReceiver;
21import android.content.Context;
Yorke Lee33501632014-03-17 19:24:12 -070022import android.content.Intent;
Yorke Lee33501632014-03-17 19:24:12 -070023import android.content.res.Resources;
Yorke Lee33501632014-03-17 19:24:12 -070024import android.net.Uri;
Yorke Lee33501632014-03-17 19:24:12 -070025import android.os.UserHandle;
Yorke Lee33501632014-03-17 19:24:12 -070026import android.telecomm.GatewayInfo;
Ihab Awad98a55602014-06-30 21:27:28 -070027import android.telecomm.PhoneAccount;
Yorke Lee33501632014-03-17 19:24:12 -070028import android.telecomm.TelecommConstants;
29import android.telephony.PhoneNumberUtils;
30import android.telephony.TelephonyManager;
31import android.text.TextUtils;
Yorke Lee33501632014-03-17 19:24:12 -070032
33/**
34 * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the
35 * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which
36 * contains the phone number being dialed. Applications can use this intent to (1) see which numbers
37 * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call
38 * from being placed.
39 *
40 * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it
41 * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}.
42 *
43 * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail
44 * number) are exempt from being broadcast.
45 *
46 * Calls to emergency numbers are still broadcast for informative purposes. The call is placed
47 * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented.
48 */
49class NewOutgoingCallIntentBroadcaster {
50 /** Required permission for any app that wants to consume ACTION_NEW_OUTGOING_CALL. */
51 private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS;
52
53 private static final String EXTRA_ACTUAL_NUMBER_TO_DIAL =
54 "android.telecomm.extra.ACTUAL_NUMBER_TO_DIAL";
55
56 /**
57 * Legacy string constants used to retrieve gateway provider extras from intents. These still
58 * need to be copied from the source call intent to the destination intent in order to
59 * support third party gateway providers that are still using old string constants in
60 * Telephony.
61 */
62 public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE =
63 "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE";
64 public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI";
Santos Cordon571f0732014-06-25 18:13:15 -070065 public static final String EXTRA_GATEWAY_ORIGINAL_URI =
66 "com.android.phone.extra.GATEWAY_ORIGINAL_URI";
Yorke Lee33501632014-03-17 19:24:12 -070067
Yorke Leeb9d4b362014-03-24 17:46:03 -070068 private static final String SCHEME_TEL = "tel";
69 private static final String SCHEME_SIP = "sip";
70
Yorke Lee33501632014-03-17 19:24:12 -070071 private final CallsManager mCallsManager;
72 private final ContactInfo mContactInfo;
73 private final Intent mIntent;
74
75 NewOutgoingCallIntentBroadcaster(CallsManager callsManager, ContactInfo contactInfo,
Yorke Leed362fe32014-06-19 22:24:28 +000076 Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -070077 mCallsManager = callsManager;
78 mContactInfo = contactInfo;
79 mIntent = intent;
80 }
81
82 /**
83 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
84 * the OutgoingCallIntentBroadcasterListener as necessary.
85 */
86 private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
87
88 @Override
89 public void onReceive(Context context, Intent intent) {
Sailesh Nepalb3308752014-04-07 14:12:47 -070090 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -070091
92 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
93 // actual number to call. (If null, no call will be placed.)
94 String resultHandle = getResultData();
95 Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle));
96
97 if (resultHandle == null) {
98 Log.v(this, "Call cancelled (null number), returning...");
99 return;
Yorke Lee66255452014-06-05 08:09:24 -0700100 } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) {
Yorke Lee33501632014-03-17 19:24:12 -0700101 Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
102 return;
103 }
104
Yorke Leeb9d4b362014-03-24 17:46:03 -0700105 Uri resultHandleUri = Uri.fromParts(
106 PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL,
107 resultHandle,
108 null);
Yorke Lee33501632014-03-17 19:24:12 -0700109
110 Uri originalUri = mIntent.getData();
111
112 if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
113 Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
114 } else {
115 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
116 + "Original: %s, Modified: %s",
117 Log.pii(originalUri),
118 Log.pii(resultHandleUri));
119 }
120
121 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
Ihab Awad98a55602014-06-30 21:27:28 -0700122 PhoneAccount account = getAccountFromIntent(intent);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700123 mCallsManager.placeOutgoingCall(resultHandleUri, mContactInfo, gatewayInfo,
Ihab Awad98a55602014-06-30 21:27:28 -0700124 account,
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700125 mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
126 false));
Yorke Lee33501632014-03-17 19:24:12 -0700127 }
128 }
129
130 /**
131 * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
132 * intent.
133 *
134 * This method will handle three kinds of actions:
135 *
136 * - CALL (intent launched by all third party dialers)
137 * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
138 * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
139 */
Yorke Leed362fe32014-06-19 22:24:28 +0000140 void processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700141 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
142
143 final Context context = TelecommApp.getInstance();
144 Intent intent = mIntent;
145
146 String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
147
148 if (TextUtils.isEmpty(handle)) {
149 Log.w(this, "Empty handle obtained from the call intent.");
Yorke Leed362fe32014-06-19 22:24:28 +0000150 return;
Yorke Lee33501632014-03-17 19:24:12 -0700151 }
152
Santos Cordonb58f4532014-05-29 11:59:06 -0700153 boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle);
154
155 if (!isUriNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700156 handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
157 handle = PhoneNumberUtils.stripSeparators(handle);
158 }
159
160 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
161 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
162
163 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
164 // True for certain types of numbers that are not intended to be intercepted or modified
165 // by third parties (e.g. emergency numbers).
166 boolean callImmediately = false;
167
168 String action = intent.getAction();
169 if (Intent.ACTION_CALL.equals(action)) {
170 if (isPotentialEmergencyNumber) {
Yorke Leed362fe32014-06-19 22:24:28 +0000171 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s.",
172 handle, intent);
173 launchSystemDialer(context, intent.getData());
Yorke Lee33501632014-03-17 19:24:12 -0700174 }
Yorke Leed362fe32014-06-19 22:24:28 +0000175 callImmediately = false;
Yorke Lee33501632014-03-17 19:24:12 -0700176 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
177 if (!isPotentialEmergencyNumber) {
178 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
179 + "Intent %s.", handle, intent);
Yorke Leed362fe32014-06-19 22:24:28 +0000180 return;
Yorke Lee33501632014-03-17 19:24:12 -0700181 }
182 callImmediately = true;
183 } else {
184 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Leed362fe32014-06-19 22:24:28 +0000185 return;
Yorke Lee33501632014-03-17 19:24:12 -0700186 }
187
188 if (callImmediately) {
189 Log.i(this, "Placing call immediately instead of waiting for "
190 + " OutgoingCallBroadcastReceiver: %s", intent);
Santos Cordonb58f4532014-05-29 11:59:06 -0700191 String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL;
192 mCallsManager.placeOutgoingCall(
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700193 Uri.fromParts(scheme, handle, null), mContactInfo, null, null,
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700194 mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
195 false));
Yorke Lee33501632014-03-17 19:24:12 -0700196
197 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
198 // so that third parties can still inspect (but not intercept) the outgoing call. When
199 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
200 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
201 }
202
203 broadcastIntent(intent, handle, context, !callImmediately);
204 }
205
206 /**
207 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
208 * placement of the call or redirect it to a different number.
209 *
210 * @param originalCallIntent The original call intent.
211 * @param handle Call handle that was stored in the original call intent.
212 * @param context Valid context to send the ordered broadcast using.
213 * @param receiverRequired Whether or not the result from the ordered broadcast should be
214 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
215 */
216 private void broadcastIntent(
217 Intent originalCallIntent,
218 String handle,
219 Context context,
220 boolean receiverRequired) {
221 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
222 if (handle != null) {
223 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
224 }
225
226 // Force receivers of this broadcast intent to run at foreground priority because we
227 // want to finish processing the broadcast intent as soon as possible.
228 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
229 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
230
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700231 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
Yorke Lee33501632014-03-17 19:24:12 -0700232
233 context.sendOrderedBroadcastAsUser(
234 broadcastIntent,
235 UserHandle.OWNER,
236 PERMISSION,
237 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
238 null, // scheduler
239 Activity.RESULT_OK, // initialCode
240 handle, // initialData: initial value for the result data (number to be modified)
241 null); // initialExtras
242 }
243
244 /**
245 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
246 * source intent to the destination one.
247 *
248 * @param src Intent which may contain the provider's extras.
249 * @param dst Intent where a copy of the extras will be added if applicable.
250 */
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700251 public void checkAndCopyProviderExtras(Intent src, Intent dst) {
252 if (src == null) {
253 return;
254 }
Yorke Lee33501632014-03-17 19:24:12 -0700255 if (hasGatewayProviderExtras(src)) {
256 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
257 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
258 dst.putExtra(EXTRA_GATEWAY_URI,
259 src.getStringExtra(EXTRA_GATEWAY_URI));
260 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
261 return;
262 }
Ihab Awad98a55602014-06-30 21:27:28 -0700263 PhoneAccount extraAccount = src.getParcelableExtra(
264 TelephonyManager.EXTRA_ACCOUNT);
265 if (extraAccount != null) {
266 dst.putExtra(TelephonyManager.EXTRA_ACCOUNT, extraAccount);
267 Log.d(this, "Found and copied account extra to broadcast intent.");
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700268 }
Yorke Lee33501632014-03-17 19:24:12 -0700269
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700270 Log.d(this, "No provider extras found in call intent.");
Yorke Lee33501632014-03-17 19:24:12 -0700271 }
272
273 /**
274 * Check if valid gateway provider information is stored as extras in the intent
275 *
276 * @param intent to check for
277 * @return true if the intent has all the gateway information extras needed.
278 */
279 private boolean hasGatewayProviderExtras(Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -0700280 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
281 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
282
283 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
284 }
285
286 private static Uri getGatewayUriFromString(String gatewayUriString) {
287 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
288 }
289
290 /**
291 * Extracts gateway provider information from a provided intent..
292 *
293 * @param intent to extract gateway provider information from.
294 * @param trueHandle The actual call handle that the user is trying to dial
295 * @return GatewayInfo object containing extracted gateway provider information as well as
296 * the actual handle the user is trying to dial.
297 */
298 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
299 if (intent == null) {
300 return null;
301 }
302
303 // Check if gateway extras are present.
304 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
305 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
306 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
307 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
308 }
309
310 return null;
311 }
312
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700313 /**
Ihab Awad98a55602014-06-30 21:27:28 -0700314 * Extracts account/connection provider information from a provided intent..
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700315 *
Ihab Awad98a55602014-06-30 21:27:28 -0700316 * @param intent to extract account information from.
317 * @return Account object containing extracted account information
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700318 */
Ihab Awad98a55602014-06-30 21:27:28 -0700319 public static PhoneAccount getAccountFromIntent(Intent intent) {
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700320 if (intent == null) {
321 return null;
322 }
323
Ihab Awad98a55602014-06-30 21:27:28 -0700324 return intent.getParcelableExtra(TelephonyManager.EXTRA_ACCOUNT);
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700325 }
326
Yorke Lee33501632014-03-17 19:24:12 -0700327 private void launchSystemDialer(Context context, Uri handle) {
328 Intent systemDialerIntent = new Intent();
329 final Resources resources = context.getResources();
330 systemDialerIntent.setClassName(
331 resources.getString(R.string.ui_default_package),
332 resources.getString(R.string.dialer_default_class));
333 systemDialerIntent.setAction(Intent.ACTION_DIAL);
334 systemDialerIntent.setData(handle);
335 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
336 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
337 context.startActivity(systemDialerIntent);
338 }
339
340 /**
341 * Check whether or not this is an emergency number, in order to enforce the restriction
342 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
343 * calls.
344 *
345 * To prevent malicious 3rd party apps from making emergency calls by passing in an
346 * "invalid" number like "9111234" (that isn't technically an emergency number but might
347 * still result in an emergency call with some networks), we use
348 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
349 *
350 * @param context Valid context
351 * @param handle Handle to inspect in order to determine whether or not an emergency number
352 * is potentially being dialed
353 * @return True if the handle is potentially an emergency number.
354 */
355 private boolean isPotentialEmergencyNumber(Context context, String handle) {
356 Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
Yorke Lee66255452014-06-05 08:09:24 -0700357 return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle);
Yorke Lee33501632014-03-17 19:24:12 -0700358 }
359
360 /**
361 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
362 * the call intent action to an appropriate one.
363 *
364 * @param intent Intent to rewrite the action for
365 * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
366 * number.
367 */
368 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
369 if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
370 // If we were launched directly from the CallActivity, not one of its more privileged
371 // aliases, then make sure that only the non-privileged actions are allowed.
372 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
373 Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
374 intent.setAction(Intent.ACTION_CALL);
375 }
376 }
377
378 String action = intent.getAction();
379
380 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
381 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
382 if (isPotentialEmergencyNumber) {
383 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
384 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
385 action = Intent.ACTION_CALL_EMERGENCY;
386 } else {
387 action = Intent.ACTION_CALL;
388 }
389 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
390 intent.setAction(action);
391 }
392 }
393}