blob: 71d8a960eefa23283810b2fab0719b2bc8addb9c [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;
Tyler Gunnc4abd912014-07-08 14:22:10 -070029import android.telecomm.VideoCallProfile;
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
103 if (resultHandle == null) {
104 Log.v(this, "Call cancelled (null number), returning...");
105 return;
Yorke Lee66255452014-06-05 08:09:24 -0700106 } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) {
Yorke Lee33501632014-03-17 19:24:12 -0700107 Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
108 return;
109 }
110
Yorke Leeb9d4b362014-03-24 17:46:03 -0700111 Uri resultHandleUri = Uri.fromParts(
112 PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL,
113 resultHandle,
114 null);
Yorke Lee33501632014-03-17 19:24:12 -0700115
116 Uri originalUri = mIntent.getData();
117
118 if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
119 Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
120 } else {
121 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
122 + "Original: %s, Modified: %s",
123 Log.pii(originalUri),
124 Log.pii(resultHandleUri));
125 }
126
127 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
Evan Charlton89176372014-07-19 18:23:09 -0700128 PhoneAccountHandle accountHandle = getAccountHandleFromIntent(intent);
Nancy Chen0d3076c2014-07-30 14:45:44 -0700129 mCallsManager.placeOutgoingCall(mCall, resultHandleUri, gatewayInfo, accountHandle,
Evan Charltonb35fc312014-07-19 15:02:55 -0700130 mIntent.getBooleanExtra(TelecommManager.EXTRA_START_CALL_WITH_SPEAKERPHONE,
Tyler Gunnc4abd912014-07-08 14:22:10 -0700131 false),
Evan Charltonb35fc312014-07-19 15:02:55 -0700132 mIntent.getIntExtra(TelecommManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Andrew Lee1d90b552014-07-28 14:36:59 -0700133 VideoCallProfile.VideoState.AUDIO_ONLY));
Yorke Lee33501632014-03-17 19:24:12 -0700134 }
135 }
136
137 /**
138 * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
139 * intent.
140 *
141 * This method will handle three kinds of actions:
142 *
143 * - CALL (intent launched by all third party dialers)
144 * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
145 * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
Yorke Leecce5deb2014-06-18 11:27:42 -0700146 *
147 * @return whether or not the caller was allowed to start the outgoing call.
Yorke Lee33501632014-03-17 19:24:12 -0700148 */
Yorke Leecce5deb2014-06-18 11:27:42 -0700149 boolean processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700150 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
151
152 final Context context = TelecommApp.getInstance();
153 Intent intent = mIntent;
154
155 String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
156
157 if (TextUtils.isEmpty(handle)) {
158 Log.w(this, "Empty handle obtained from the call intent.");
Yorke Leecce5deb2014-06-18 11:27:42 -0700159 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700160 }
161
Santos Cordonb58f4532014-05-29 11:59:06 -0700162 boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle);
163
164 if (!isUriNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700165 handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
166 handle = PhoneNumberUtils.stripSeparators(handle);
167 }
168
169 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
170 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
171
172 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
173 // True for certain types of numbers that are not intended to be intercepted or modified
174 // by third parties (e.g. emergency numbers).
175 boolean callImmediately = false;
176
177 String action = intent.getAction();
178 if (Intent.ACTION_CALL.equals(action)) {
179 if (isPotentialEmergencyNumber) {
Yorke Leecce5deb2014-06-18 11:27:42 -0700180 if (!mIsDefaultOrSystemPhoneApp) {
181 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s "
182 + "unless caller is system or default dialer.", handle, intent);
183 launchSystemDialer(context, intent.getData());
184 return false;
185 } else {
186 callImmediately = true;
187 }
Yorke Lee33501632014-03-17 19:24:12 -0700188 }
Yorke Lee33501632014-03-17 19:24:12 -0700189 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
190 if (!isPotentialEmergencyNumber) {
191 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
192 + "Intent %s.", handle, intent);
Yorke Leecce5deb2014-06-18 11:27:42 -0700193 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700194 }
195 callImmediately = true;
196 } else {
197 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Leecce5deb2014-06-18 11:27:42 -0700198 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700199 }
200
201 if (callImmediately) {
202 Log.i(this, "Placing call immediately instead of waiting for "
203 + " OutgoingCallBroadcastReceiver: %s", intent);
Santos Cordonb58f4532014-05-29 11:59:06 -0700204 String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL;
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700205 boolean speakerphoneOn = mIntent.getBooleanExtra(
Evan Charltonb35fc312014-07-19 15:02:55 -0700206 TelecommManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700207 int videoState = mIntent.getIntExtra(
Evan Charltonb35fc312014-07-19 15:02:55 -0700208 TelecommManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Andrew Lee1d90b552014-07-28 14:36:59 -0700209 VideoCallProfile.VideoState.AUDIO_ONLY);
Nancy Chen0d3076c2014-07-30 14:45:44 -0700210 mCallsManager.placeOutgoingCall(mCall, Uri.fromParts(scheme, handle, null), null, null,
211 speakerphoneOn, videoState);
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);
Yorke Leecce5deb2014-06-18 11:27:42 -0700220 return true;
Yorke Lee33501632014-03-17 19:24:12 -0700221 }
222
223 /**
224 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
225 * placement of the call or redirect it to a different number.
226 *
227 * @param originalCallIntent The original call intent.
228 * @param handle Call handle that was stored in the original call intent.
229 * @param context Valid context to send the ordered broadcast using.
230 * @param receiverRequired Whether or not the result from the ordered broadcast should be
231 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
232 */
233 private void broadcastIntent(
234 Intent originalCallIntent,
235 String handle,
236 Context context,
237 boolean receiverRequired) {
238 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
239 if (handle != null) {
240 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
241 }
242
243 // Force receivers of this broadcast intent to run at foreground priority because we
244 // want to finish processing the broadcast intent as soon as possible.
245 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
246 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
247
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700248 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
Yorke Lee33501632014-03-17 19:24:12 -0700249
250 context.sendOrderedBroadcastAsUser(
251 broadcastIntent,
252 UserHandle.OWNER,
253 PERMISSION,
254 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
255 null, // scheduler
256 Activity.RESULT_OK, // initialCode
257 handle, // initialData: initial value for the result data (number to be modified)
258 null); // initialExtras
259 }
260
261 /**
262 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
263 * source intent to the destination one.
264 *
265 * @param src Intent which may contain the provider's extras.
266 * @param dst Intent where a copy of the extras will be added if applicable.
267 */
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700268 public void checkAndCopyProviderExtras(Intent src, Intent dst) {
269 if (src == null) {
270 return;
271 }
Yorke Lee33501632014-03-17 19:24:12 -0700272 if (hasGatewayProviderExtras(src)) {
273 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
274 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
275 dst.putExtra(EXTRA_GATEWAY_URI,
276 src.getStringExtra(EXTRA_GATEWAY_URI));
277 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
278 return;
279 }
Evan Charlton89176372014-07-19 18:23:09 -0700280 PhoneAccountHandle extraAccountHandle =
281 src.getParcelableExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE);
282 if (extraAccountHandle != null) {
283 dst.putExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE, extraAccountHandle);
Ihab Awad98a55602014-06-30 21:27:28 -0700284 Log.d(this, "Found and copied account extra to broadcast intent.");
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700285 }
Yorke Lee33501632014-03-17 19:24:12 -0700286
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700287 Log.d(this, "No provider extras found in call intent.");
Yorke Lee33501632014-03-17 19:24:12 -0700288 }
289
290 /**
291 * Check if valid gateway provider information is stored as extras in the intent
292 *
293 * @param intent to check for
294 * @return true if the intent has all the gateway information extras needed.
295 */
296 private boolean hasGatewayProviderExtras(Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -0700297 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
298 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
299
300 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
301 }
302
303 private static Uri getGatewayUriFromString(String gatewayUriString) {
304 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
305 }
306
307 /**
308 * Extracts gateway provider information from a provided intent..
309 *
310 * @param intent to extract gateway provider information from.
311 * @param trueHandle The actual call handle that the user is trying to dial
312 * @return GatewayInfo object containing extracted gateway provider information as well as
313 * the actual handle the user is trying to dial.
314 */
315 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
316 if (intent == null) {
317 return null;
318 }
319
320 // Check if gateway extras are present.
321 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
322 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
323 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
324 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
325 }
326
327 return null;
328 }
329
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700330 /**
Ihab Awad98a55602014-06-30 21:27:28 -0700331 * Extracts account/connection provider information from a provided intent..
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700332 *
Ihab Awad98a55602014-06-30 21:27:28 -0700333 * @param intent to extract account information from.
334 * @return Account object containing extracted account information
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700335 */
Evan Charlton89176372014-07-19 18:23:09 -0700336 public static PhoneAccountHandle getAccountHandleFromIntent(Intent intent) {
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700337 if (intent == null) {
338 return null;
339 }
340
Evan Charlton89176372014-07-19 18:23:09 -0700341 return intent.getParcelableExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE);
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700342 }
343
Yorke Lee33501632014-03-17 19:24:12 -0700344 private void launchSystemDialer(Context context, Uri handle) {
345 Intent systemDialerIntent = new Intent();
346 final Resources resources = context.getResources();
347 systemDialerIntent.setClassName(
348 resources.getString(R.string.ui_default_package),
349 resources.getString(R.string.dialer_default_class));
350 systemDialerIntent.setAction(Intent.ACTION_DIAL);
351 systemDialerIntent.setData(handle);
352 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
353 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
354 context.startActivity(systemDialerIntent);
355 }
356
357 /**
358 * Check whether or not this is an emergency number, in order to enforce the restriction
359 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
360 * calls.
361 *
362 * To prevent malicious 3rd party apps from making emergency calls by passing in an
363 * "invalid" number like "9111234" (that isn't technically an emergency number but might
364 * still result in an emergency call with some networks), we use
365 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
366 *
367 * @param context Valid context
368 * @param handle Handle to inspect in order to determine whether or not an emergency number
369 * is potentially being dialed
370 * @return True if the handle is potentially an emergency number.
371 */
372 private boolean isPotentialEmergencyNumber(Context context, String handle) {
373 Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
Yorke Lee66255452014-06-05 08:09:24 -0700374 return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle);
Yorke Lee33501632014-03-17 19:24:12 -0700375 }
376
377 /**
378 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
379 * the call intent action to an appropriate one.
380 *
381 * @param intent Intent to rewrite the action for
382 * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
383 * number.
384 */
385 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
386 if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
387 // If we were launched directly from the CallActivity, not one of its more privileged
388 // aliases, then make sure that only the non-privileged actions are allowed.
389 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
390 Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
391 intent.setAction(Intent.ACTION_CALL);
392 }
393 }
394
395 String action = intent.getAction();
396
397 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
398 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
399 if (isPotentialEmergencyNumber) {
400 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
401 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
402 action = Intent.ACTION_CALL_EMERGENCY;
403 } else {
404 action = Intent.ACTION_CALL;
405 }
406 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
407 intent.setAction(action);
408 }
409 }
410}