blob: 65c1309cd36ab93784f3b2fc8917f736646a2ef4 [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;
Evan Charlton89176372014-07-19 18:23:09 -070027import android.telecomm.PhoneAccountHandle;
Evan Charltonb35fc312014-07-19 15:02:55 -070028import android.telecomm.TelecommManager;
Ihab Awad6fb37c82014-08-07 19:48:57 -070029import android.telecomm.VideoProfile;
Yorke Lee33501632014-03-17 19:24:12 -070030import android.telephony.PhoneNumberUtils;
Yorke Lee33501632014-03-17 19:24:12 -070031import 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;
Nancy Chen0d3076c2014-07-30 14:45:44 -070072 private final Call mCall;
Yorke Lee33501632014-03-17 19:24:12 -070073 private final Intent mIntent;
Yorke Leecce5deb2014-06-18 11:27:42 -070074 /*
75 * Whether or not the outgoing call intent originated from the default phone application. If
76 * so, it will be allowed to make emergency calls, even with the ACTION_CALL intent.
77 */
78 private final boolean mIsDefaultOrSystemPhoneApp;
Yorke Lee33501632014-03-17 19:24:12 -070079
Nancy Chen0d3076c2014-07-30 14:45:44 -070080 NewOutgoingCallIntentBroadcaster(CallsManager callsManager, Call call, Intent intent,
Yorke Leecce5deb2014-06-18 11:27:42 -070081 boolean isDefaultPhoneApp) {
Yorke Lee33501632014-03-17 19:24:12 -070082 mCallsManager = callsManager;
Nancy Chen0d3076c2014-07-30 14:45:44 -070083 mCall = call;
Yorke Lee33501632014-03-17 19:24:12 -070084 mIntent = intent;
Yorke Leecce5deb2014-06-18 11:27:42 -070085 mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp;
Yorke Lee33501632014-03-17 19:24:12 -070086 }
87
88 /**
89 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
90 * the OutgoingCallIntentBroadcasterListener as necessary.
91 */
92 private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
93
94 @Override
95 public void onReceive(Context context, Intent intent) {
Sailesh Nepalb3308752014-04-07 14:12:47 -070096 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -070097
98 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
99 // actual number to call. (If null, no call will be placed.)
100 String resultHandle = getResultData();
101 Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle));
102
Santos Cordon2d0b3312014-08-15 15:04:17 -0700103 boolean endEarly = false;
Yorke Lee33501632014-03-17 19:24:12 -0700104 if (resultHandle == null) {
105 Log.v(this, "Call cancelled (null number), returning...");
Santos Cordon2d0b3312014-08-15 15:04:17 -0700106 endEarly = true;
Yorke Lee66255452014-06-05 08:09:24 -0700107 } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) {
Yorke Lee33501632014-03-17 19:24:12 -0700108 Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
Santos Cordon2d0b3312014-08-15 15:04:17 -0700109 endEarly = true;
110 }
111
112 if (endEarly) {
113 if (mCall != null) {
114 mCall.disconnect();
115 }
Yorke Lee33501632014-03-17 19:24:12 -0700116 return;
117 }
118
Yorke Leeb9d4b362014-03-24 17:46:03 -0700119 Uri resultHandleUri = Uri.fromParts(
120 PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL,
121 resultHandle,
122 null);
Yorke Lee33501632014-03-17 19:24:12 -0700123
124 Uri originalUri = mIntent.getData();
125
126 if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
127 Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
128 } else {
129 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
130 + "Original: %s, Modified: %s",
131 Log.pii(originalUri),
132 Log.pii(resultHandleUri));
133 }
134
135 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
Evan Charlton89176372014-07-19 18:23:09 -0700136 PhoneAccountHandle accountHandle = getAccountHandleFromIntent(intent);
Nancy Chen0d3076c2014-07-30 14:45:44 -0700137 mCallsManager.placeOutgoingCall(mCall, resultHandleUri, gatewayInfo, accountHandle,
Evan Charltonb35fc312014-07-19 15:02:55 -0700138 mIntent.getBooleanExtra(TelecommManager.EXTRA_START_CALL_WITH_SPEAKERPHONE,
Tyler Gunnc4abd912014-07-08 14:22:10 -0700139 false),
Evan Charltonb35fc312014-07-19 15:02:55 -0700140 mIntent.getIntExtra(TelecommManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Ihab Awad6fb37c82014-08-07 19:48:57 -0700141 VideoProfile.VideoState.AUDIO_ONLY));
Yorke Lee33501632014-03-17 19:24:12 -0700142 }
143 }
144
145 /**
146 * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
147 * intent.
148 *
149 * This method will handle three kinds of actions:
150 *
151 * - CALL (intent launched by all third party dialers)
152 * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
153 * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
Yorke Leecce5deb2014-06-18 11:27:42 -0700154 *
155 * @return whether or not the caller was allowed to start the outgoing call.
Yorke Lee33501632014-03-17 19:24:12 -0700156 */
Yorke Leecce5deb2014-06-18 11:27:42 -0700157 boolean processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700158 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
159
160 final Context context = TelecommApp.getInstance();
161 Intent intent = mIntent;
162
163 String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
164
165 if (TextUtils.isEmpty(handle)) {
166 Log.w(this, "Empty handle obtained from the call intent.");
Yorke Leecce5deb2014-06-18 11:27:42 -0700167 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700168 }
169
Santos Cordonb58f4532014-05-29 11:59:06 -0700170 boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle);
171
172 if (!isUriNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700173 handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
174 handle = PhoneNumberUtils.stripSeparators(handle);
175 }
176
177 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
178 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
179
180 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
181 // True for certain types of numbers that are not intended to be intercepted or modified
182 // by third parties (e.g. emergency numbers).
183 boolean callImmediately = false;
184
185 String action = intent.getAction();
186 if (Intent.ACTION_CALL.equals(action)) {
187 if (isPotentialEmergencyNumber) {
Yorke Leecce5deb2014-06-18 11:27:42 -0700188 if (!mIsDefaultOrSystemPhoneApp) {
189 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s "
190 + "unless caller is system or default dialer.", handle, intent);
191 launchSystemDialer(context, intent.getData());
192 return false;
193 } else {
194 callImmediately = true;
195 }
Yorke Lee33501632014-03-17 19:24:12 -0700196 }
Yorke Lee33501632014-03-17 19:24:12 -0700197 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
198 if (!isPotentialEmergencyNumber) {
199 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
200 + "Intent %s.", handle, intent);
Yorke Leecce5deb2014-06-18 11:27:42 -0700201 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700202 }
203 callImmediately = true;
204 } else {
205 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Leecce5deb2014-06-18 11:27:42 -0700206 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700207 }
208
209 if (callImmediately) {
210 Log.i(this, "Placing call immediately instead of waiting for "
211 + " OutgoingCallBroadcastReceiver: %s", intent);
Santos Cordonb58f4532014-05-29 11:59:06 -0700212 String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL;
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700213 boolean speakerphoneOn = mIntent.getBooleanExtra(
Evan Charltonb35fc312014-07-19 15:02:55 -0700214 TelecommManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700215 int videoState = mIntent.getIntExtra(
Evan Charltonb35fc312014-07-19 15:02:55 -0700216 TelecommManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Ihab Awad6fb37c82014-08-07 19:48:57 -0700217 VideoProfile.VideoState.AUDIO_ONLY);
Nancy Chen0d3076c2014-07-30 14:45:44 -0700218 mCallsManager.placeOutgoingCall(mCall, Uri.fromParts(scheme, handle, null), null, null,
219 speakerphoneOn, videoState);
Yorke Lee33501632014-03-17 19:24:12 -0700220
221 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
222 // so that third parties can still inspect (but not intercept) the outgoing call. When
223 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
224 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
225 }
226
227 broadcastIntent(intent, handle, context, !callImmediately);
Yorke Leecce5deb2014-06-18 11:27:42 -0700228 return true;
Yorke Lee33501632014-03-17 19:24:12 -0700229 }
230
231 /**
232 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
233 * placement of the call or redirect it to a different number.
234 *
235 * @param originalCallIntent The original call intent.
236 * @param handle Call handle that was stored in the original call intent.
237 * @param context Valid context to send the ordered broadcast using.
238 * @param receiverRequired Whether or not the result from the ordered broadcast should be
239 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
240 */
241 private void broadcastIntent(
242 Intent originalCallIntent,
243 String handle,
244 Context context,
245 boolean receiverRequired) {
246 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
247 if (handle != null) {
248 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
249 }
250
251 // Force receivers of this broadcast intent to run at foreground priority because we
252 // want to finish processing the broadcast intent as soon as possible.
253 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
254 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
255
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700256 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
Yorke Lee33501632014-03-17 19:24:12 -0700257
258 context.sendOrderedBroadcastAsUser(
259 broadcastIntent,
260 UserHandle.OWNER,
261 PERMISSION,
262 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
263 null, // scheduler
264 Activity.RESULT_OK, // initialCode
265 handle, // initialData: initial value for the result data (number to be modified)
266 null); // initialExtras
267 }
268
269 /**
270 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
271 * source intent to the destination one.
272 *
273 * @param src Intent which may contain the provider's extras.
274 * @param dst Intent where a copy of the extras will be added if applicable.
275 */
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700276 public void checkAndCopyProviderExtras(Intent src, Intent dst) {
277 if (src == null) {
278 return;
279 }
Yorke Lee33501632014-03-17 19:24:12 -0700280 if (hasGatewayProviderExtras(src)) {
281 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
282 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
283 dst.putExtra(EXTRA_GATEWAY_URI,
284 src.getStringExtra(EXTRA_GATEWAY_URI));
285 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
286 return;
287 }
Evan Charlton89176372014-07-19 18:23:09 -0700288 PhoneAccountHandle extraAccountHandle =
289 src.getParcelableExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE);
290 if (extraAccountHandle != null) {
291 dst.putExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE, extraAccountHandle);
Ihab Awad98a55602014-06-30 21:27:28 -0700292 Log.d(this, "Found and copied account extra to broadcast intent.");
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700293 }
Yorke Lee33501632014-03-17 19:24:12 -0700294
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700295 Log.d(this, "No provider extras found in call intent.");
Yorke Lee33501632014-03-17 19:24:12 -0700296 }
297
298 /**
299 * Check if valid gateway provider information is stored as extras in the intent
300 *
301 * @param intent to check for
302 * @return true if the intent has all the gateway information extras needed.
303 */
304 private boolean hasGatewayProviderExtras(Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -0700305 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
306 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
307
308 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
309 }
310
311 private static Uri getGatewayUriFromString(String gatewayUriString) {
312 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
313 }
314
315 /**
316 * Extracts gateway provider information from a provided intent..
317 *
318 * @param intent to extract gateway provider information from.
319 * @param trueHandle The actual call handle that the user is trying to dial
320 * @return GatewayInfo object containing extracted gateway provider information as well as
321 * the actual handle the user is trying to dial.
322 */
323 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
324 if (intent == null) {
325 return null;
326 }
327
328 // Check if gateway extras are present.
329 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
330 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
331 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
332 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
333 }
334
335 return null;
336 }
337
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700338 /**
Ihab Awad98a55602014-06-30 21:27:28 -0700339 * Extracts account/connection provider information from a provided intent..
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700340 *
Ihab Awad98a55602014-06-30 21:27:28 -0700341 * @param intent to extract account information from.
342 * @return Account object containing extracted account information
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700343 */
Evan Charlton89176372014-07-19 18:23:09 -0700344 public static PhoneAccountHandle getAccountHandleFromIntent(Intent intent) {
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700345 if (intent == null) {
346 return null;
347 }
348
Evan Charlton89176372014-07-19 18:23:09 -0700349 return intent.getParcelableExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE);
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700350 }
351
Yorke Lee33501632014-03-17 19:24:12 -0700352 private void launchSystemDialer(Context context, Uri handle) {
353 Intent systemDialerIntent = new Intent();
354 final Resources resources = context.getResources();
355 systemDialerIntent.setClassName(
356 resources.getString(R.string.ui_default_package),
357 resources.getString(R.string.dialer_default_class));
358 systemDialerIntent.setAction(Intent.ACTION_DIAL);
359 systemDialerIntent.setData(handle);
360 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
361 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
362 context.startActivity(systemDialerIntent);
363 }
364
365 /**
366 * Check whether or not this is an emergency number, in order to enforce the restriction
367 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
368 * calls.
369 *
370 * To prevent malicious 3rd party apps from making emergency calls by passing in an
371 * "invalid" number like "9111234" (that isn't technically an emergency number but might
372 * still result in an emergency call with some networks), we use
373 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
374 *
375 * @param context Valid context
376 * @param handle Handle to inspect in order to determine whether or not an emergency number
377 * is potentially being dialed
378 * @return True if the handle is potentially an emergency number.
379 */
380 private boolean isPotentialEmergencyNumber(Context context, String handle) {
381 Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
Yorke Lee66255452014-06-05 08:09:24 -0700382 return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle);
Yorke Lee33501632014-03-17 19:24:12 -0700383 }
384
385 /**
386 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
387 * the call intent action to an appropriate one.
388 *
389 * @param intent Intent to rewrite the action for
390 * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
391 * number.
392 */
393 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
394 if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
395 // If we were launched directly from the CallActivity, not one of its more privileged
396 // aliases, then make sure that only the non-privileged actions are allowed.
397 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
398 Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
399 intent.setAction(Intent.ACTION_CALL);
400 }
401 }
402
403 String action = intent.getAction();
404
405 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
406 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
407 if (isPotentialEmergencyNumber) {
408 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
409 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
410 action = Intent.ACTION_CALL_EMERGENCY;
411 } else {
412 action = Intent.ACTION_CALL;
413 }
414 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
415 intent.setAction(action);
416 }
417 }
418}