blob: 1c44fb2d73b3313119418b843733c0a0a5761a25 [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 Charltonb35fc312014-07-19 15:02:55 -070027import android.telecomm.TelecommManager;
Ihab Awad6fb37c82014-08-07 19:48:57 -070028import android.telecomm.VideoProfile;
Yorke Leed7255872014-08-25 15:03:51 -070029import android.telephony.DisconnectCause;
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";
Yorke Leed7255872014-08-25 15:03:51 -070070 private static final String SCHEME_VOICEMAIL = "voicemail";
Yorke Leeb9d4b362014-03-24 17:46:03 -070071
Yorke Lee33501632014-03-17 19:24:12 -070072 private final CallsManager mCallsManager;
Nancy Chen0d3076c2014-07-30 14:45:44 -070073 private final Call mCall;
Yorke Lee33501632014-03-17 19:24:12 -070074 private final Intent mIntent;
Yorke Leecce5deb2014-06-18 11:27:42 -070075 /*
76 * Whether or not the outgoing call intent originated from the default phone application. If
77 * so, it will be allowed to make emergency calls, even with the ACTION_CALL intent.
78 */
79 private final boolean mIsDefaultOrSystemPhoneApp;
Yorke Lee33501632014-03-17 19:24:12 -070080
Nancy Chen0d3076c2014-07-30 14:45:44 -070081 NewOutgoingCallIntentBroadcaster(CallsManager callsManager, Call call, Intent intent,
Yorke Leecce5deb2014-06-18 11:27:42 -070082 boolean isDefaultPhoneApp) {
Yorke Lee33501632014-03-17 19:24:12 -070083 mCallsManager = callsManager;
Nancy Chen0d3076c2014-07-30 14:45:44 -070084 mCall = call;
Yorke Lee33501632014-03-17 19:24:12 -070085 mIntent = intent;
Yorke Leecce5deb2014-06-18 11:27:42 -070086 mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp;
Yorke Lee33501632014-03-17 19:24:12 -070087 }
88
89 /**
90 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
91 * the OutgoingCallIntentBroadcasterListener as necessary.
92 */
93 private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
94
95 @Override
96 public void onReceive(Context context, Intent intent) {
Sailesh Nepalb3308752014-04-07 14:12:47 -070097 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -070098
99 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
100 // actual number to call. (If null, no call will be placed.)
101 String resultHandle = getResultData();
102 Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle));
103
Santos Cordon2d0b3312014-08-15 15:04:17 -0700104 boolean endEarly = false;
Yorke Lee33501632014-03-17 19:24:12 -0700105 if (resultHandle == null) {
106 Log.v(this, "Call cancelled (null number), returning...");
Santos Cordon2d0b3312014-08-15 15:04:17 -0700107 endEarly = true;
Yorke Lee66255452014-06-05 08:09:24 -0700108 } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) {
Yorke Lee33501632014-03-17 19:24:12 -0700109 Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
Santos Cordon2d0b3312014-08-15 15:04:17 -0700110 endEarly = true;
111 }
112
113 if (endEarly) {
114 if (mCall != null) {
115 mCall.disconnect();
116 }
Yorke Lee33501632014-03-17 19:24:12 -0700117 return;
118 }
119
Yorke Leeb9d4b362014-03-24 17:46:03 -0700120 Uri resultHandleUri = Uri.fromParts(
121 PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL,
122 resultHandle,
123 null);
Yorke Lee33501632014-03-17 19:24:12 -0700124
125 Uri originalUri = mIntent.getData();
126
127 if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
128 Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
129 } else {
130 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
131 + "Original: %s, Modified: %s",
132 Log.pii(originalUri),
133 Log.pii(resultHandleUri));
134 }
135
136 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700137 mCallsManager.placeOutgoingCall(mCall, resultHandleUri, gatewayInfo,
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 *
Yorke Leed7255872014-08-25 15:03:51 -0700155 * @return {@link CallActivity#OUTGOING_CALL_SUCCEEDED} if the call succeeded, and an
156 * appropriate {@link DisconnectCause} if the call did not, describing why it failed.
Yorke Lee33501632014-03-17 19:24:12 -0700157 */
Yorke Leed7255872014-08-25 15:03:51 -0700158 int processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700159 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
160
161 final Context context = TelecommApp.getInstance();
162 Intent intent = mIntent;
163
164 String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
165
166 if (TextUtils.isEmpty(handle)) {
Yorke Leed7255872014-08-25 15:03:51 -0700167 final Uri data = intent.getData();
168 if (data != null && SCHEME_VOICEMAIL.equals(data.getScheme())) {
169 Log.w(this, "Voicemail scheme provided but no voicemail number set.");
170 return DisconnectCause.VOICEMAIL_NUMBER_MISSING;
171 } else {
172 Log.w(this, "Empty handle obtained from the call intent.");
173 return DisconnectCause.INVALID_NUMBER;
174 }
Yorke Lee33501632014-03-17 19:24:12 -0700175 }
176
Santos Cordonb58f4532014-05-29 11:59:06 -0700177 boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle);
178
179 if (!isUriNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700180 handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
181 handle = PhoneNumberUtils.stripSeparators(handle);
182 }
183
184 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
185 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
186
187 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
188 // True for certain types of numbers that are not intended to be intercepted or modified
189 // by third parties (e.g. emergency numbers).
190 boolean callImmediately = false;
191
192 String action = intent.getAction();
193 if (Intent.ACTION_CALL.equals(action)) {
194 if (isPotentialEmergencyNumber) {
Yorke Leecce5deb2014-06-18 11:27:42 -0700195 if (!mIsDefaultOrSystemPhoneApp) {
196 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s "
197 + "unless caller is system or default dialer.", handle, intent);
198 launchSystemDialer(context, intent.getData());
Yorke Leed7255872014-08-25 15:03:51 -0700199 return DisconnectCause.OUTGOING_CANCELED;
Yorke Leecce5deb2014-06-18 11:27:42 -0700200 } else {
201 callImmediately = true;
202 }
Yorke Lee33501632014-03-17 19:24:12 -0700203 }
Yorke Lee33501632014-03-17 19:24:12 -0700204 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
205 if (!isPotentialEmergencyNumber) {
206 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
207 + "Intent %s.", handle, intent);
Yorke Leed7255872014-08-25 15:03:51 -0700208 return DisconnectCause.OUTGOING_CANCELED;
Yorke Lee33501632014-03-17 19:24:12 -0700209 }
210 callImmediately = true;
211 } else {
212 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Leed7255872014-08-25 15:03:51 -0700213 return DisconnectCause.INVALID_NUMBER;
Yorke Lee33501632014-03-17 19:24:12 -0700214 }
215
216 if (callImmediately) {
217 Log.i(this, "Placing call immediately instead of waiting for "
218 + " OutgoingCallBroadcastReceiver: %s", intent);
Santos Cordonb58f4532014-05-29 11:59:06 -0700219 String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL;
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700220 boolean speakerphoneOn = mIntent.getBooleanExtra(
Evan Charltonb35fc312014-07-19 15:02:55 -0700221 TelecommManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700222 int videoState = mIntent.getIntExtra(
Evan Charltonb35fc312014-07-19 15:02:55 -0700223 TelecommManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Ihab Awad6fb37c82014-08-07 19:48:57 -0700224 VideoProfile.VideoState.AUDIO_ONLY);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700225 mCallsManager.placeOutgoingCall(mCall, Uri.fromParts(scheme, handle, null), null,
Nancy Chen0d3076c2014-07-30 14:45:44 -0700226 speakerphoneOn, videoState);
Yorke Lee33501632014-03-17 19:24:12 -0700227
228 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
229 // so that third parties can still inspect (but not intercept) the outgoing call. When
230 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
231 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
232 }
233
234 broadcastIntent(intent, handle, context, !callImmediately);
Yorke Leed7255872014-08-25 15:03:51 -0700235 return DisconnectCause.NOT_DISCONNECTED;
Yorke Lee33501632014-03-17 19:24:12 -0700236 }
237
238 /**
239 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
240 * placement of the call or redirect it to a different number.
241 *
242 * @param originalCallIntent The original call intent.
243 * @param handle Call handle that was stored in the original call intent.
244 * @param context Valid context to send the ordered broadcast using.
245 * @param receiverRequired Whether or not the result from the ordered broadcast should be
246 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
247 */
248 private void broadcastIntent(
249 Intent originalCallIntent,
250 String handle,
251 Context context,
252 boolean receiverRequired) {
253 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
254 if (handle != null) {
255 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
256 }
257
258 // Force receivers of this broadcast intent to run at foreground priority because we
259 // want to finish processing the broadcast intent as soon as possible.
260 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
261 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
262
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700263 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
Yorke Lee33501632014-03-17 19:24:12 -0700264
265 context.sendOrderedBroadcastAsUser(
266 broadcastIntent,
267 UserHandle.OWNER,
268 PERMISSION,
269 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
270 null, // scheduler
271 Activity.RESULT_OK, // initialCode
272 handle, // initialData: initial value for the result data (number to be modified)
273 null); // initialExtras
274 }
275
276 /**
277 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
278 * source intent to the destination one.
279 *
280 * @param src Intent which may contain the provider's extras.
281 * @param dst Intent where a copy of the extras will be added if applicable.
282 */
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700283 public void checkAndCopyProviderExtras(Intent src, Intent dst) {
284 if (src == null) {
285 return;
286 }
Yorke Lee33501632014-03-17 19:24:12 -0700287 if (hasGatewayProviderExtras(src)) {
288 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
289 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
290 dst.putExtra(EXTRA_GATEWAY_URI,
291 src.getStringExtra(EXTRA_GATEWAY_URI));
292 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
293 return;
294 }
295
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700296 Log.d(this, "No provider extras found in call intent.");
Yorke Lee33501632014-03-17 19:24:12 -0700297 }
298
299 /**
300 * Check if valid gateway provider information is stored as extras in the intent
301 *
302 * @param intent to check for
303 * @return true if the intent has all the gateway information extras needed.
304 */
305 private boolean hasGatewayProviderExtras(Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -0700306 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
307 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
308
309 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
310 }
311
312 private static Uri getGatewayUriFromString(String gatewayUriString) {
313 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
314 }
315
316 /**
317 * Extracts gateway provider information from a provided intent..
318 *
319 * @param intent to extract gateway provider information from.
320 * @param trueHandle The actual call handle that the user is trying to dial
321 * @return GatewayInfo object containing extracted gateway provider information as well as
322 * the actual handle the user is trying to dial.
323 */
324 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
325 if (intent == null) {
326 return null;
327 }
328
329 // Check if gateway extras are present.
330 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
331 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
332 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
333 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
334 }
335
336 return null;
337 }
338
339 private void launchSystemDialer(Context context, Uri handle) {
340 Intent systemDialerIntent = new Intent();
341 final Resources resources = context.getResources();
342 systemDialerIntent.setClassName(
343 resources.getString(R.string.ui_default_package),
344 resources.getString(R.string.dialer_default_class));
345 systemDialerIntent.setAction(Intent.ACTION_DIAL);
346 systemDialerIntent.setData(handle);
347 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
348 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
349 context.startActivity(systemDialerIntent);
350 }
351
352 /**
353 * Check whether or not this is an emergency number, in order to enforce the restriction
354 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
355 * calls.
356 *
357 * To prevent malicious 3rd party apps from making emergency calls by passing in an
358 * "invalid" number like "9111234" (that isn't technically an emergency number but might
359 * still result in an emergency call with some networks), we use
360 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
361 *
362 * @param context Valid context
363 * @param handle Handle to inspect in order to determine whether or not an emergency number
364 * is potentially being dialed
365 * @return True if the handle is potentially an emergency number.
366 */
367 private boolean isPotentialEmergencyNumber(Context context, String handle) {
368 Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
Yorke Lee66255452014-06-05 08:09:24 -0700369 return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle);
Yorke Lee33501632014-03-17 19:24:12 -0700370 }
371
372 /**
373 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
374 * the call intent action to an appropriate one.
375 *
376 * @param intent Intent to rewrite the action for
377 * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
378 * number.
379 */
380 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
381 if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
382 // If we were launched directly from the CallActivity, not one of its more privileged
383 // aliases, then make sure that only the non-privileged actions are allowed.
384 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
385 Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
386 intent.setAction(Intent.ACTION_CALL);
387 }
388 }
389
390 String action = intent.getAction();
391
392 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
393 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
394 if (isPotentialEmergencyNumber) {
395 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
396 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
397 action = Intent.ACTION_CALL_EMERGENCY;
398 } else {
399 action = Intent.ACTION_CALL;
400 }
401 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
402 intent.setAction(action);
403 }
404 }
405}