blob: 3ddeaf226995a34fc66f32c12b521b9dd21b4ba9 [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;
Yorke Lee31a94e32014-06-18 11:27:42 -070087 /*
88 * Whether or not the outgoing call intent originated from the default phone application. If
89 * so, it will be allowed to make emergency calls, even with the ACTION_CALL intent.
90 */
91 private final boolean mIsDefaultOrSystemPhoneApp;
Yorke Lee33501632014-03-17 19:24:12 -070092
93 NewOutgoingCallIntentBroadcaster(CallsManager callsManager, ContactInfo contactInfo,
Yorke Lee31a94e32014-06-18 11:27:42 -070094 Intent intent, boolean isDefaultPhoneApp) {
Yorke Lee33501632014-03-17 19:24:12 -070095 mCallsManager = callsManager;
96 mContactInfo = contactInfo;
97 mIntent = intent;
Yorke Lee31a94e32014-06-18 11:27:42 -070098 mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp;
Yorke Lee33501632014-03-17 19:24:12 -070099 }
100
101 /**
102 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
103 * the OutgoingCallIntentBroadcasterListener as necessary.
104 */
105 private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
106
107 @Override
108 public void onReceive(Context context, Intent intent) {
Sailesh Nepalb3308752014-04-07 14:12:47 -0700109 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -0700110
111 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
112 // actual number to call. (If null, no call will be placed.)
113 String resultHandle = getResultData();
114 Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle));
115
116 if (resultHandle == null) {
117 Log.v(this, "Call cancelled (null number), returning...");
118 return;
Yorke Lee66255452014-06-05 08:09:24 -0700119 } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) {
Yorke Lee33501632014-03-17 19:24:12 -0700120 Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
121 return;
122 }
123
Yorke Leeb9d4b362014-03-24 17:46:03 -0700124 Uri resultHandleUri = Uri.fromParts(
125 PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL,
126 resultHandle,
127 null);
Yorke Lee33501632014-03-17 19:24:12 -0700128
129 Uri originalUri = mIntent.getData();
130
131 if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
132 Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
133 } else {
134 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
135 + "Original: %s, Modified: %s",
136 Log.pii(originalUri),
137 Log.pii(resultHandleUri));
138 }
139
140 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700141 mCallsManager.placeOutgoingCall(resultHandleUri, mContactInfo, gatewayInfo,
142 mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
143 false));
Yorke Lee33501632014-03-17 19:24:12 -0700144 }
145 }
146
147 /**
148 * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
149 * intent.
150 *
151 * This method will handle three kinds of actions:
152 *
153 * - CALL (intent launched by all third party dialers)
154 * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
155 * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
Yorke Lee31a94e32014-06-18 11:27:42 -0700156 *
157 * @return whether or not the caller was allowed to start the outgoing call.
Yorke Lee33501632014-03-17 19:24:12 -0700158 */
Yorke Lee31a94e32014-06-18 11:27:42 -0700159 boolean processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700160 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
161
162 final Context context = TelecommApp.getInstance();
163 Intent intent = mIntent;
164
165 String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
166
167 if (TextUtils.isEmpty(handle)) {
168 Log.w(this, "Empty handle obtained from the call intent.");
Yorke Lee31a94e32014-06-18 11:27:42 -0700169 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700170 }
171
Santos Cordonb58f4532014-05-29 11:59:06 -0700172 boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle);
173
174 if (!isUriNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700175 handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
176 handle = PhoneNumberUtils.stripSeparators(handle);
177 }
178
179 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
180 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
181
182 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
183 // True for certain types of numbers that are not intended to be intercepted or modified
184 // by third parties (e.g. emergency numbers).
185 boolean callImmediately = false;
186
187 String action = intent.getAction();
188 if (Intent.ACTION_CALL.equals(action)) {
189 if (isPotentialEmergencyNumber) {
Yorke Lee31a94e32014-06-18 11:27:42 -0700190 if (!mIsDefaultOrSystemPhoneApp) {
191 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s "
192 + "unless caller is system or default dialer.", handle, intent);
193 launchSystemDialer(context, intent.getData());
194 return false;
195 } else {
196 callImmediately = true;
197 }
Yorke Lee33501632014-03-17 19:24:12 -0700198 }
Yorke Lee33501632014-03-17 19:24:12 -0700199 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
200 if (!isPotentialEmergencyNumber) {
201 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
202 + "Intent %s.", handle, intent);
Yorke Lee31a94e32014-06-18 11:27:42 -0700203 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700204 }
205 callImmediately = true;
206 } else {
207 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Lee31a94e32014-06-18 11:27:42 -0700208 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700209 }
210
211 if (callImmediately) {
212 Log.i(this, "Placing call immediately instead of waiting for "
213 + " OutgoingCallBroadcastReceiver: %s", intent);
Santos Cordonb58f4532014-05-29 11:59:06 -0700214 String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL;
215 mCallsManager.placeOutgoingCall(
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700216 Uri.fromParts(scheme, handle, null), mContactInfo, null,
217 mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
218 false));
Yorke Lee33501632014-03-17 19:24:12 -0700219
220 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
221 // so that third parties can still inspect (but not intercept) the outgoing call. When
222 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
223 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
224 }
225
226 broadcastIntent(intent, handle, context, !callImmediately);
Yorke Lee31a94e32014-06-18 11:27:42 -0700227 return true;
Yorke Lee33501632014-03-17 19:24:12 -0700228 }
229
230 /**
231 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
232 * placement of the call or redirect it to a different number.
233 *
234 * @param originalCallIntent The original call intent.
235 * @param handle Call handle that was stored in the original call intent.
236 * @param context Valid context to send the ordered broadcast using.
237 * @param receiverRequired Whether or not the result from the ordered broadcast should be
238 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
239 */
240 private void broadcastIntent(
241 Intent originalCallIntent,
242 String handle,
243 Context context,
244 boolean receiverRequired) {
245 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
246 if (handle != null) {
247 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
248 }
249
250 // Force receivers of this broadcast intent to run at foreground priority because we
251 // want to finish processing the broadcast intent as soon as possible.
252 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
253 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
254
255 checkAndCopyGatewayProviderExtras(originalCallIntent, broadcastIntent);
256
257 context.sendOrderedBroadcastAsUser(
258 broadcastIntent,
259 UserHandle.OWNER,
260 PERMISSION,
261 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
262 null, // scheduler
263 Activity.RESULT_OK, // initialCode
264 handle, // initialData: initial value for the result data (number to be modified)
265 null); // initialExtras
266 }
267
268 /**
269 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
270 * source intent to the destination one.
271 *
272 * @param src Intent which may contain the provider's extras.
273 * @param dst Intent where a copy of the extras will be added if applicable.
274 */
275 public void checkAndCopyGatewayProviderExtras(Intent src, Intent dst) {
276 if (hasGatewayProviderExtras(src)) {
277 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
278 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
279 dst.putExtra(EXTRA_GATEWAY_URI,
280 src.getStringExtra(EXTRA_GATEWAY_URI));
281 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
282 return;
283 }
284
285 Log.d(this, "No gateway provider extras found in call intent.");
286 }
287
288 /**
289 * Check if valid gateway provider information is stored as extras in the intent
290 *
291 * @param intent to check for
292 * @return true if the intent has all the gateway information extras needed.
293 */
294 private boolean hasGatewayProviderExtras(Intent intent) {
295 if (intent == null) {
296 return false;
297 }
298 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
299 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
300
301 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
302 }
303
304 private static Uri getGatewayUriFromString(String gatewayUriString) {
305 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
306 }
307
308 /**
309 * Extracts gateway provider information from a provided intent..
310 *
311 * @param intent to extract gateway provider information from.
312 * @param trueHandle The actual call handle that the user is trying to dial
313 * @return GatewayInfo object containing extracted gateway provider information as well as
314 * the actual handle the user is trying to dial.
315 */
316 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
317 if (intent == null) {
318 return null;
319 }
320
321 // Check if gateway extras are present.
322 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
323 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
324 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
325 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
326 }
327
328 return null;
329 }
330
331 private void launchSystemDialer(Context context, Uri handle) {
332 Intent systemDialerIntent = new Intent();
333 final Resources resources = context.getResources();
334 systemDialerIntent.setClassName(
335 resources.getString(R.string.ui_default_package),
336 resources.getString(R.string.dialer_default_class));
337 systemDialerIntent.setAction(Intent.ACTION_DIAL);
338 systemDialerIntent.setData(handle);
339 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
340 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
341 context.startActivity(systemDialerIntent);
342 }
343
344 /**
345 * Check whether or not this is an emergency number, in order to enforce the restriction
346 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
347 * calls.
348 *
349 * To prevent malicious 3rd party apps from making emergency calls by passing in an
350 * "invalid" number like "9111234" (that isn't technically an emergency number but might
351 * still result in an emergency call with some networks), we use
352 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
353 *
354 * @param context Valid context
355 * @param handle Handle to inspect in order to determine whether or not an emergency number
356 * is potentially being dialed
357 * @return True if the handle is potentially an emergency number.
358 */
359 private boolean isPotentialEmergencyNumber(Context context, String handle) {
360 Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
Yorke Lee66255452014-06-05 08:09:24 -0700361 return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle);
Yorke Lee33501632014-03-17 19:24:12 -0700362 }
363
364 /**
365 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
366 * the call intent action to an appropriate one.
367 *
368 * @param intent Intent to rewrite the action for
369 * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
370 * number.
371 */
372 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
373 if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
374 // If we were launched directly from the CallActivity, not one of its more privileged
375 // aliases, then make sure that only the non-privileged actions are allowed.
376 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
377 Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
378 intent.setAction(Intent.ACTION_CALL);
379 }
380 }
381
382 String action = intent.getAction();
383
384 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
385 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
386 if (isPotentialEmergencyNumber) {
387 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
388 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
389 action = Intent.ACTION_CALL_EMERGENCY;
390 } else {
391 action = Intent.ACTION_CALL;
392 }
393 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
394 intent.setAction(action);
395 }
396 }
397}