blob: c40fde550ea496a10aabdbd6b92e5da2e29a8081 [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;
41import android.telecomm.TelecommConstants;
42import android.telephony.PhoneNumberUtils;
43import android.telephony.TelephonyManager;
44import android.text.TextUtils;
45import android.view.View;
46import android.widget.ProgressBar;
47
48/**
49 * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the
50 * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which
51 * contains the phone number being dialed. Applications can use this intent to (1) see which numbers
52 * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call
53 * from being placed.
54 *
55 * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it
56 * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}.
57 *
58 * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail
59 * number) are exempt from being broadcast.
60 *
61 * Calls to emergency numbers are still broadcast for informative purposes. The call is placed
62 * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented.
63 */
64class NewOutgoingCallIntentBroadcaster {
65 /** Required permission for any app that wants to consume ACTION_NEW_OUTGOING_CALL. */
66 private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS;
67
68 private static final String EXTRA_ACTUAL_NUMBER_TO_DIAL =
69 "android.telecomm.extra.ACTUAL_NUMBER_TO_DIAL";
70
71 /**
72 * Legacy string constants used to retrieve gateway provider extras from intents. These still
73 * need to be copied from the source call intent to the destination intent in order to
74 * support third party gateway providers that are still using old string constants in
75 * Telephony.
76 */
77 public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE =
78 "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE";
79 public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI";
80
Yorke Leeb9d4b362014-03-24 17:46:03 -070081 private static final String SCHEME_TEL = "tel";
82 private static final String SCHEME_SIP = "sip";
83
Yorke Lee33501632014-03-17 19:24:12 -070084 private final CallsManager mCallsManager;
85 private final ContactInfo mContactInfo;
86 private final Intent mIntent;
87
88 NewOutgoingCallIntentBroadcaster(CallsManager callsManager, ContactInfo contactInfo,
Yorke Leed362fe32014-06-19 22:24:28 +000089 Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -070090 mCallsManager = callsManager;
91 mContactInfo = contactInfo;
92 mIntent = intent;
93 }
94
95 /**
96 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
97 * the OutgoingCallIntentBroadcasterListener as necessary.
98 */
99 private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
100
101 @Override
102 public void onReceive(Context context, Intent intent) {
Sailesh Nepalb3308752014-04-07 14:12:47 -0700103 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -0700104
105 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
106 // actual number to call. (If null, no call will be placed.)
107 String resultHandle = getResultData();
108 Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle));
109
110 if (resultHandle == null) {
111 Log.v(this, "Call cancelled (null number), returning...");
112 return;
Yorke Lee66255452014-06-05 08:09:24 -0700113 } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) {
Yorke Lee33501632014-03-17 19:24:12 -0700114 Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
115 return;
116 }
117
Yorke Leeb9d4b362014-03-24 17:46:03 -0700118 Uri resultHandleUri = Uri.fromParts(
119 PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL,
120 resultHandle,
121 null);
Yorke Lee33501632014-03-17 19:24:12 -0700122
123 Uri originalUri = mIntent.getData();
124
125 if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
126 Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
127 } else {
128 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
129 + "Original: %s, Modified: %s",
130 Log.pii(originalUri),
131 Log.pii(resultHandleUri));
132 }
133
134 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700135 mCallsManager.placeOutgoingCall(resultHandleUri, mContactInfo, gatewayInfo,
136 mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
137 false));
Yorke Lee33501632014-03-17 19:24:12 -0700138 }
139 }
140
141 /**
142 * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
143 * intent.
144 *
145 * This method will handle three kinds of actions:
146 *
147 * - CALL (intent launched by all third party dialers)
148 * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
149 * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
150 */
Yorke Leed362fe32014-06-19 22:24:28 +0000151 void processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700152 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
153
154 final Context context = TelecommApp.getInstance();
155 Intent intent = mIntent;
156
157 String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
158
159 if (TextUtils.isEmpty(handle)) {
160 Log.w(this, "Empty handle obtained from the call intent.");
Yorke Leed362fe32014-06-19 22:24:28 +0000161 return;
Yorke Lee33501632014-03-17 19:24:12 -0700162 }
163
Santos Cordonb58f4532014-05-29 11:59:06 -0700164 boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle);
165
166 if (!isUriNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700167 handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
168 handle = PhoneNumberUtils.stripSeparators(handle);
169 }
170
171 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
172 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
173
174 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
175 // True for certain types of numbers that are not intended to be intercepted or modified
176 // by third parties (e.g. emergency numbers).
177 boolean callImmediately = false;
178
179 String action = intent.getAction();
180 if (Intent.ACTION_CALL.equals(action)) {
181 if (isPotentialEmergencyNumber) {
Yorke Leed362fe32014-06-19 22:24:28 +0000182 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s.",
183 handle, intent);
184 launchSystemDialer(context, intent.getData());
Yorke Lee33501632014-03-17 19:24:12 -0700185 }
Yorke Leed362fe32014-06-19 22:24:28 +0000186 callImmediately = false;
Yorke Lee33501632014-03-17 19:24:12 -0700187 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
188 if (!isPotentialEmergencyNumber) {
189 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
190 + "Intent %s.", handle, intent);
Yorke Leed362fe32014-06-19 22:24:28 +0000191 return;
Yorke Lee33501632014-03-17 19:24:12 -0700192 }
193 callImmediately = true;
194 } else {
195 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Leed362fe32014-06-19 22:24:28 +0000196 return;
Yorke Lee33501632014-03-17 19:24:12 -0700197 }
198
199 if (callImmediately) {
200 Log.i(this, "Placing call immediately instead of waiting for "
201 + " OutgoingCallBroadcastReceiver: %s", intent);
Santos Cordonb58f4532014-05-29 11:59:06 -0700202 String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL;
203 mCallsManager.placeOutgoingCall(
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700204 Uri.fromParts(scheme, handle, null), mContactInfo, null,
205 mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
206 false));
Yorke Lee33501632014-03-17 19:24:12 -0700207
208 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
209 // so that third parties can still inspect (but not intercept) the outgoing call. When
210 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
211 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
212 }
213
214 broadcastIntent(intent, handle, context, !callImmediately);
215 }
216
217 /**
218 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
219 * placement of the call or redirect it to a different number.
220 *
221 * @param originalCallIntent The original call intent.
222 * @param handle Call handle that was stored in the original call intent.
223 * @param context Valid context to send the ordered broadcast using.
224 * @param receiverRequired Whether or not the result from the ordered broadcast should be
225 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
226 */
227 private void broadcastIntent(
228 Intent originalCallIntent,
229 String handle,
230 Context context,
231 boolean receiverRequired) {
232 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
233 if (handle != null) {
234 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
235 }
236
237 // Force receivers of this broadcast intent to run at foreground priority because we
238 // want to finish processing the broadcast intent as soon as possible.
239 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
240 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
241
242 checkAndCopyGatewayProviderExtras(originalCallIntent, broadcastIntent);
243
244 context.sendOrderedBroadcastAsUser(
245 broadcastIntent,
246 UserHandle.OWNER,
247 PERMISSION,
248 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
249 null, // scheduler
250 Activity.RESULT_OK, // initialCode
251 handle, // initialData: initial value for the result data (number to be modified)
252 null); // initialExtras
253 }
254
255 /**
256 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
257 * source intent to the destination one.
258 *
259 * @param src Intent which may contain the provider's extras.
260 * @param dst Intent where a copy of the extras will be added if applicable.
261 */
262 public void checkAndCopyGatewayProviderExtras(Intent src, Intent dst) {
263 if (hasGatewayProviderExtras(src)) {
264 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
265 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
266 dst.putExtra(EXTRA_GATEWAY_URI,
267 src.getStringExtra(EXTRA_GATEWAY_URI));
268 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
269 return;
270 }
271
272 Log.d(this, "No gateway provider extras found in call intent.");
273 }
274
275 /**
276 * Check if valid gateway provider information is stored as extras in the intent
277 *
278 * @param intent to check for
279 * @return true if the intent has all the gateway information extras needed.
280 */
281 private boolean hasGatewayProviderExtras(Intent intent) {
282 if (intent == null) {
283 return false;
284 }
285 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
286 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
287
288 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
289 }
290
291 private static Uri getGatewayUriFromString(String gatewayUriString) {
292 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
293 }
294
295 /**
296 * Extracts gateway provider information from a provided intent..
297 *
298 * @param intent to extract gateway provider information from.
299 * @param trueHandle The actual call handle that the user is trying to dial
300 * @return GatewayInfo object containing extracted gateway provider information as well as
301 * the actual handle the user is trying to dial.
302 */
303 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
304 if (intent == null) {
305 return null;
306 }
307
308 // Check if gateway extras are present.
309 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
310 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
311 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
312 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
313 }
314
315 return null;
316 }
317
318 private void launchSystemDialer(Context context, Uri handle) {
319 Intent systemDialerIntent = new Intent();
320 final Resources resources = context.getResources();
321 systemDialerIntent.setClassName(
322 resources.getString(R.string.ui_default_package),
323 resources.getString(R.string.dialer_default_class));
324 systemDialerIntent.setAction(Intent.ACTION_DIAL);
325 systemDialerIntent.setData(handle);
326 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
327 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
328 context.startActivity(systemDialerIntent);
329 }
330
331 /**
332 * Check whether or not this is an emergency number, in order to enforce the restriction
333 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
334 * calls.
335 *
336 * To prevent malicious 3rd party apps from making emergency calls by passing in an
337 * "invalid" number like "9111234" (that isn't technically an emergency number but might
338 * still result in an emergency call with some networks), we use
339 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
340 *
341 * @param context Valid context
342 * @param handle Handle to inspect in order to determine whether or not an emergency number
343 * is potentially being dialed
344 * @return True if the handle is potentially an emergency number.
345 */
346 private boolean isPotentialEmergencyNumber(Context context, String handle) {
347 Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
Yorke Lee66255452014-06-05 08:09:24 -0700348 return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle);
Yorke Lee33501632014-03-17 19:24:12 -0700349 }
350
351 /**
352 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
353 * the call intent action to an appropriate one.
354 *
355 * @param intent Intent to rewrite the action for
356 * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
357 * number.
358 */
359 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
360 if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
361 // If we were launched directly from the CallActivity, not one of its more privileged
362 // aliases, then make sure that only the non-privileged actions are allowed.
363 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
364 Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
365 intent.setAction(Intent.ACTION_CALL);
366 }
367 }
368
369 String action = intent.getAction();
370
371 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
372 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
373 if (isPotentialEmergencyNumber) {
374 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
375 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
376 action = Intent.ACTION_CALL_EMERGENCY;
377 } else {
378 action = Intent.ACTION_CALL;
379 }
380 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
381 intent.setAction(action);
382 }
383 }
384}