blob: c18dbc5f86ab17750c1fdd015b59b8d3ca36fac7 [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;
20import android.app.ActivityManagerNative;
21import android.app.AlertDialog;
22import android.app.AppOpsManager;
23import android.app.Dialog;
24import android.content.BroadcastReceiver;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
28import android.content.res.Configuration;
29import android.content.res.Resources;
30import android.database.Cursor;
31import android.net.Uri;
32import android.os.Binder;
33import android.os.Bundle;
34import android.os.Handler;
35import android.os.Message;
36import android.os.RemoteException;
37import android.os.SystemProperties;
38import android.os.UserHandle;
39import android.provider.ContactsContract;
40import android.telecomm.GatewayInfo;
Nancy Chen77d2d0e2014-06-24 12:06:03 -070041import android.telecomm.Subscription;
Yorke Lee33501632014-03-17 19:24:12 -070042import android.telecomm.TelecommConstants;
43import android.telephony.PhoneNumberUtils;
44import android.telephony.TelephonyManager;
45import android.text.TextUtils;
46import android.view.View;
47import android.widget.ProgressBar;
48
49/**
50 * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the
51 * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which
52 * contains the phone number being dialed. Applications can use this intent to (1) see which numbers
53 * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call
54 * from being placed.
55 *
56 * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it
57 * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}.
58 *
59 * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail
60 * number) are exempt from being broadcast.
61 *
62 * Calls to emergency numbers are still broadcast for informative purposes. The call is placed
63 * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented.
64 */
65class NewOutgoingCallIntentBroadcaster {
66 /** Required permission for any app that wants to consume ACTION_NEW_OUTGOING_CALL. */
67 private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS;
68
69 private static final String EXTRA_ACTUAL_NUMBER_TO_DIAL =
70 "android.telecomm.extra.ACTUAL_NUMBER_TO_DIAL";
71
72 /**
73 * Legacy string constants used to retrieve gateway provider extras from intents. These still
74 * need to be copied from the source call intent to the destination intent in order to
75 * support third party gateway providers that are still using old string constants in
76 * Telephony.
77 */
78 public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE =
79 "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE";
80 public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI";
Santos Cordon571f0732014-06-25 18:13:15 -070081 public static final String EXTRA_GATEWAY_ORIGINAL_URI =
82 "com.android.phone.extra.GATEWAY_ORIGINAL_URI";
Yorke Lee33501632014-03-17 19:24:12 -070083
Yorke Leeb9d4b362014-03-24 17:46:03 -070084 private static final String SCHEME_TEL = "tel";
85 private static final String SCHEME_SIP = "sip";
86
Yorke Lee33501632014-03-17 19:24:12 -070087 private final CallsManager mCallsManager;
88 private final ContactInfo mContactInfo;
89 private final Intent mIntent;
90
91 NewOutgoingCallIntentBroadcaster(CallsManager callsManager, ContactInfo contactInfo,
Yorke Leed362fe32014-06-19 22:24:28 +000092 Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -070093 mCallsManager = callsManager;
94 mContactInfo = contactInfo;
95 mIntent = intent;
96 }
97
98 /**
99 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
100 * the OutgoingCallIntentBroadcasterListener as necessary.
101 */
102 private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
103
104 @Override
105 public void onReceive(Context context, Intent intent) {
Sailesh Nepalb3308752014-04-07 14:12:47 -0700106 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -0700107
108 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
109 // actual number to call. (If null, no call will be placed.)
110 String resultHandle = getResultData();
111 Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle));
112
113 if (resultHandle == null) {
114 Log.v(this, "Call cancelled (null number), returning...");
115 return;
Yorke Lee66255452014-06-05 08:09:24 -0700116 } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) {
Yorke Lee33501632014-03-17 19:24:12 -0700117 Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
118 return;
119 }
120
Yorke Leeb9d4b362014-03-24 17:46:03 -0700121 Uri resultHandleUri = Uri.fromParts(
122 PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL,
123 resultHandle,
124 null);
Yorke Lee33501632014-03-17 19:24:12 -0700125
126 Uri originalUri = mIntent.getData();
127
128 if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
129 Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
130 } else {
131 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
132 + "Original: %s, Modified: %s",
133 Log.pii(originalUri),
134 Log.pii(resultHandleUri));
135 }
136
137 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700138 Subscription subscription = getSubscriptionFromIntent(intent);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700139 mCallsManager.placeOutgoingCall(resultHandleUri, mContactInfo, gatewayInfo,
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700140 subscription,
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700141 mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
142 false));
Yorke Lee33501632014-03-17 19:24:12 -0700143 }
144 }
145
146 /**
147 * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
148 * intent.
149 *
150 * This method will handle three kinds of actions:
151 *
152 * - CALL (intent launched by all third party dialers)
153 * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
154 * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
155 */
Yorke Leed362fe32014-06-19 22:24:28 +0000156 void processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700157 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
158
159 final Context context = TelecommApp.getInstance();
160 Intent intent = mIntent;
161
162 String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
163
164 if (TextUtils.isEmpty(handle)) {
165 Log.w(this, "Empty handle obtained from the call intent.");
Yorke Leed362fe32014-06-19 22:24:28 +0000166 return;
Yorke Lee33501632014-03-17 19:24:12 -0700167 }
168
Santos Cordonb58f4532014-05-29 11:59:06 -0700169 boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle);
170
171 if (!isUriNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700172 handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
173 handle = PhoneNumberUtils.stripSeparators(handle);
174 }
175
176 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
177 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
178
179 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
180 // True for certain types of numbers that are not intended to be intercepted or modified
181 // by third parties (e.g. emergency numbers).
182 boolean callImmediately = false;
183
184 String action = intent.getAction();
185 if (Intent.ACTION_CALL.equals(action)) {
186 if (isPotentialEmergencyNumber) {
Yorke Leed362fe32014-06-19 22:24:28 +0000187 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s.",
188 handle, intent);
189 launchSystemDialer(context, intent.getData());
Yorke Lee33501632014-03-17 19:24:12 -0700190 }
Yorke Leed362fe32014-06-19 22:24:28 +0000191 callImmediately = false;
Yorke Lee33501632014-03-17 19:24:12 -0700192 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
193 if (!isPotentialEmergencyNumber) {
194 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
195 + "Intent %s.", handle, intent);
Yorke Leed362fe32014-06-19 22:24:28 +0000196 return;
Yorke Lee33501632014-03-17 19:24:12 -0700197 }
198 callImmediately = true;
199 } else {
200 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Leed362fe32014-06-19 22:24:28 +0000201 return;
Yorke Lee33501632014-03-17 19:24:12 -0700202 }
203
204 if (callImmediately) {
205 Log.i(this, "Placing call immediately instead of waiting for "
206 + " OutgoingCallBroadcastReceiver: %s", intent);
Santos Cordonb58f4532014-05-29 11:59:06 -0700207 String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL;
208 mCallsManager.placeOutgoingCall(
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700209 Uri.fromParts(scheme, handle, null), mContactInfo, null, null,
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700210 mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
211 false));
Yorke Lee33501632014-03-17 19:24:12 -0700212
213 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
214 // so that third parties can still inspect (but not intercept) the outgoing call. When
215 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
216 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
217 }
218
219 broadcastIntent(intent, handle, context, !callImmediately);
220 }
221
222 /**
223 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
224 * placement of the call or redirect it to a different number.
225 *
226 * @param originalCallIntent The original call intent.
227 * @param handle Call handle that was stored in the original call intent.
228 * @param context Valid context to send the ordered broadcast using.
229 * @param receiverRequired Whether or not the result from the ordered broadcast should be
230 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
231 */
232 private void broadcastIntent(
233 Intent originalCallIntent,
234 String handle,
235 Context context,
236 boolean receiverRequired) {
237 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
238 if (handle != null) {
239 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
240 }
241
242 // Force receivers of this broadcast intent to run at foreground priority because we
243 // want to finish processing the broadcast intent as soon as possible.
244 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
245 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
246
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700247 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
Yorke Lee33501632014-03-17 19:24:12 -0700248
249 context.sendOrderedBroadcastAsUser(
250 broadcastIntent,
251 UserHandle.OWNER,
252 PERMISSION,
253 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
254 null, // scheduler
255 Activity.RESULT_OK, // initialCode
256 handle, // initialData: initial value for the result data (number to be modified)
257 null); // initialExtras
258 }
259
260 /**
261 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
262 * source intent to the destination one.
263 *
264 * @param src Intent which may contain the provider's extras.
265 * @param dst Intent where a copy of the extras will be added if applicable.
266 */
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700267 public void checkAndCopyProviderExtras(Intent src, Intent dst) {
268 if (src == null) {
269 return;
270 }
Yorke Lee33501632014-03-17 19:24:12 -0700271 if (hasGatewayProviderExtras(src)) {
272 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
273 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
274 dst.putExtra(EXTRA_GATEWAY_URI,
275 src.getStringExtra(EXTRA_GATEWAY_URI));
276 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
277 return;
278 }
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700279 Subscription extraSubscription = src.getParcelableExtra(
280 TelephonyManager.EXTRA_SUBSCRIPTION);
281 if (extraSubscription != null) {
282 dst.putExtra(TelephonyManager.EXTRA_SUBSCRIPTION, extraSubscription);
283 Log.d(this, "Found and copied subscription extra to broadcast intent.");
284 }
Yorke Lee33501632014-03-17 19:24:12 -0700285
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700286 Log.d(this, "No provider extras found in call intent.");
Yorke Lee33501632014-03-17 19:24:12 -0700287 }
288
289 /**
290 * Check if valid gateway provider information is stored as extras in the intent
291 *
292 * @param intent to check for
293 * @return true if the intent has all the gateway information extras needed.
294 */
295 private boolean hasGatewayProviderExtras(Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -0700296 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
297 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
298
299 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
300 }
301
302 private static Uri getGatewayUriFromString(String gatewayUriString) {
303 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
304 }
305
306 /**
307 * Extracts gateway provider information from a provided intent..
308 *
309 * @param intent to extract gateway provider information from.
310 * @param trueHandle The actual call handle that the user is trying to dial
311 * @return GatewayInfo object containing extracted gateway provider information as well as
312 * the actual handle the user is trying to dial.
313 */
314 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
315 if (intent == null) {
316 return null;
317 }
318
319 // Check if gateway extras are present.
320 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
321 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
322 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
323 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
324 }
325
326 return null;
327 }
328
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700329 /**
330 * Extracts subscription/connection provider information from a provided intent..
331 *
332 * @param intent to extract subscription information from.
333 * @return Subscription object containing extracted subscription information
334 */
335 public static Subscription getSubscriptionFromIntent(Intent intent) {
336 if (intent == null) {
337 return null;
338 }
339
340 return intent.getParcelableExtra(TelephonyManager.EXTRA_SUBSCRIPTION);
341 }
342
Yorke Lee33501632014-03-17 19:24:12 -0700343 private void launchSystemDialer(Context context, Uri handle) {
344 Intent systemDialerIntent = new Intent();
345 final Resources resources = context.getResources();
346 systemDialerIntent.setClassName(
347 resources.getString(R.string.ui_default_package),
348 resources.getString(R.string.dialer_default_class));
349 systemDialerIntent.setAction(Intent.ACTION_DIAL);
350 systemDialerIntent.setData(handle);
351 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
352 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
353 context.startActivity(systemDialerIntent);
354 }
355
356 /**
357 * Check whether or not this is an emergency number, in order to enforce the restriction
358 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
359 * calls.
360 *
361 * To prevent malicious 3rd party apps from making emergency calls by passing in an
362 * "invalid" number like "9111234" (that isn't technically an emergency number but might
363 * still result in an emergency call with some networks), we use
364 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
365 *
366 * @param context Valid context
367 * @param handle Handle to inspect in order to determine whether or not an emergency number
368 * is potentially being dialed
369 * @return True if the handle is potentially an emergency number.
370 */
371 private boolean isPotentialEmergencyNumber(Context context, String handle) {
372 Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
Yorke Lee66255452014-06-05 08:09:24 -0700373 return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle);
Yorke Lee33501632014-03-17 19:24:12 -0700374 }
375
376 /**
377 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
378 * the call intent action to an appropriate one.
379 *
380 * @param intent Intent to rewrite the action for
381 * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
382 * number.
383 */
384 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
385 if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
386 // If we were launched directly from the CallActivity, not one of its more privileged
387 // aliases, then make sure that only the non-privileged actions are allowed.
388 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
389 Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
390 intent.setAction(Intent.ACTION_CALL);
391 }
392 }
393
394 String action = intent.getAction();
395
396 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
397 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
398 if (isPotentialEmergencyNumber) {
399 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
400 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
401 action = Intent.ACTION_CALL_EMERGENCY;
402 } else {
403 action = Intent.ACTION_CALL;
404 }
405 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
406 intent.setAction(action);
407 }
408 }
409}