blob: a45f4efec74f794452da2cfe7d7974831ae03a78 [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 Lee33501632014-03-17 19:24:12 -070029import android.telephony.PhoneNumberUtils;
Yorke Lee33501632014-03-17 19:24:12 -070030import android.text.TextUtils;
Yorke Lee33501632014-03-17 19:24:12 -070031
32/**
33 * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the
34 * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which
35 * contains the phone number being dialed. Applications can use this intent to (1) see which numbers
36 * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call
37 * from being placed.
38 *
39 * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it
40 * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}.
41 *
42 * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail
43 * number) are exempt from being broadcast.
44 *
45 * Calls to emergency numbers are still broadcast for informative purposes. The call is placed
46 * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented.
47 */
48class NewOutgoingCallIntentBroadcaster {
49 /** Required permission for any app that wants to consume ACTION_NEW_OUTGOING_CALL. */
50 private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS;
51
52 private static final String EXTRA_ACTUAL_NUMBER_TO_DIAL =
53 "android.telecomm.extra.ACTUAL_NUMBER_TO_DIAL";
54
55 /**
56 * Legacy string constants used to retrieve gateway provider extras from intents. These still
57 * need to be copied from the source call intent to the destination intent in order to
58 * support third party gateway providers that are still using old string constants in
59 * Telephony.
60 */
61 public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE =
62 "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE";
63 public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI";
Santos Cordon571f0732014-06-25 18:13:15 -070064 public static final String EXTRA_GATEWAY_ORIGINAL_URI =
65 "com.android.phone.extra.GATEWAY_ORIGINAL_URI";
Yorke Lee33501632014-03-17 19:24:12 -070066
Yorke Leeb9d4b362014-03-24 17:46:03 -070067 private static final String SCHEME_TEL = "tel";
68 private static final String SCHEME_SIP = "sip";
69
Yorke Lee33501632014-03-17 19:24:12 -070070 private final CallsManager mCallsManager;
Nancy Chen0d3076c2014-07-30 14:45:44 -070071 private final Call mCall;
Yorke Lee33501632014-03-17 19:24:12 -070072 private final Intent mIntent;
Yorke Leecce5deb2014-06-18 11:27:42 -070073 /*
74 * Whether or not the outgoing call intent originated from the default phone application. If
75 * so, it will be allowed to make emergency calls, even with the ACTION_CALL intent.
76 */
77 private final boolean mIsDefaultOrSystemPhoneApp;
Yorke Lee33501632014-03-17 19:24:12 -070078
Nancy Chen0d3076c2014-07-30 14:45:44 -070079 NewOutgoingCallIntentBroadcaster(CallsManager callsManager, Call call, Intent intent,
Yorke Leecce5deb2014-06-18 11:27:42 -070080 boolean isDefaultPhoneApp) {
Yorke Lee33501632014-03-17 19:24:12 -070081 mCallsManager = callsManager;
Nancy Chen0d3076c2014-07-30 14:45:44 -070082 mCall = call;
Yorke Lee33501632014-03-17 19:24:12 -070083 mIntent = intent;
Yorke Leecce5deb2014-06-18 11:27:42 -070084 mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp;
Yorke Lee33501632014-03-17 19:24:12 -070085 }
86
87 /**
88 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
89 * the OutgoingCallIntentBroadcasterListener as necessary.
90 */
91 private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
92
93 @Override
94 public void onReceive(Context context, Intent intent) {
Sailesh Nepalb3308752014-04-07 14:12:47 -070095 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -070096
97 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
98 // actual number to call. (If null, no call will be placed.)
99 String resultHandle = getResultData();
100 Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle));
101
Santos Cordon2d0b3312014-08-15 15:04:17 -0700102 boolean endEarly = false;
Yorke Lee33501632014-03-17 19:24:12 -0700103 if (resultHandle == null) {
104 Log.v(this, "Call cancelled (null number), returning...");
Santos Cordon2d0b3312014-08-15 15:04:17 -0700105 endEarly = true;
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);
Santos Cordon2d0b3312014-08-15 15:04:17 -0700108 endEarly = true;
109 }
110
111 if (endEarly) {
112 if (mCall != null) {
113 mCall.disconnect();
114 }
Yorke Lee33501632014-03-17 19:24:12 -0700115 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);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700135 mCallsManager.placeOutgoingCall(mCall, resultHandleUri, gatewayInfo,
Evan Charltonb35fc312014-07-19 15:02:55 -0700136 mIntent.getBooleanExtra(TelecommManager.EXTRA_START_CALL_WITH_SPEAKERPHONE,
Tyler Gunnc4abd912014-07-08 14:22:10 -0700137 false),
Evan Charltonb35fc312014-07-19 15:02:55 -0700138 mIntent.getIntExtra(TelecommManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Ihab Awad6fb37c82014-08-07 19:48:57 -0700139 VideoProfile.VideoState.AUDIO_ONLY));
Yorke Lee33501632014-03-17 19:24:12 -0700140 }
141 }
142
143 /**
144 * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
145 * intent.
146 *
147 * This method will handle three kinds of actions:
148 *
149 * - CALL (intent launched by all third party dialers)
150 * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
151 * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
Yorke Leecce5deb2014-06-18 11:27:42 -0700152 *
153 * @return whether or not the caller was allowed to start the outgoing call.
Yorke Lee33501632014-03-17 19:24:12 -0700154 */
Yorke Leecce5deb2014-06-18 11:27:42 -0700155 boolean processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700156 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
157
158 final Context context = TelecommApp.getInstance();
159 Intent intent = mIntent;
160
161 String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
162
163 if (TextUtils.isEmpty(handle)) {
164 Log.w(this, "Empty handle obtained from the call intent.");
Yorke Leecce5deb2014-06-18 11:27:42 -0700165 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700166 }
167
Santos Cordonb58f4532014-05-29 11:59:06 -0700168 boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle);
169
170 if (!isUriNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700171 handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
172 handle = PhoneNumberUtils.stripSeparators(handle);
173 }
174
175 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
176 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
177
178 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
179 // True for certain types of numbers that are not intended to be intercepted or modified
180 // by third parties (e.g. emergency numbers).
181 boolean callImmediately = false;
182
183 String action = intent.getAction();
184 if (Intent.ACTION_CALL.equals(action)) {
185 if (isPotentialEmergencyNumber) {
Yorke Leecce5deb2014-06-18 11:27:42 -0700186 if (!mIsDefaultOrSystemPhoneApp) {
187 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s "
188 + "unless caller is system or default dialer.", handle, intent);
189 launchSystemDialer(context, intent.getData());
190 return false;
191 } else {
192 callImmediately = true;
193 }
Yorke Lee33501632014-03-17 19:24:12 -0700194 }
Yorke Lee33501632014-03-17 19:24:12 -0700195 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
196 if (!isPotentialEmergencyNumber) {
197 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
198 + "Intent %s.", handle, intent);
Yorke Leecce5deb2014-06-18 11:27:42 -0700199 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700200 }
201 callImmediately = true;
202 } else {
203 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Leecce5deb2014-06-18 11:27:42 -0700204 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700205 }
206
207 if (callImmediately) {
208 Log.i(this, "Placing call immediately instead of waiting for "
209 + " OutgoingCallBroadcastReceiver: %s", intent);
Santos Cordonb58f4532014-05-29 11:59:06 -0700210 String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL;
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700211 boolean speakerphoneOn = mIntent.getBooleanExtra(
Evan Charltonb35fc312014-07-19 15:02:55 -0700212 TelecommManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700213 int videoState = mIntent.getIntExtra(
Evan Charltonb35fc312014-07-19 15:02:55 -0700214 TelecommManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Ihab Awad6fb37c82014-08-07 19:48:57 -0700215 VideoProfile.VideoState.AUDIO_ONLY);
Nancy Chenbc9ef172014-08-15 17:11:57 -0700216 mCallsManager.placeOutgoingCall(mCall, Uri.fromParts(scheme, handle, null), null,
Nancy Chen0d3076c2014-07-30 14:45:44 -0700217 speakerphoneOn, videoState);
Yorke Lee33501632014-03-17 19:24:12 -0700218
219 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
220 // so that third parties can still inspect (but not intercept) the outgoing call. When
221 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
222 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
223 }
224
225 broadcastIntent(intent, handle, context, !callImmediately);
Yorke Leecce5deb2014-06-18 11:27:42 -0700226 return true;
Yorke Lee33501632014-03-17 19:24:12 -0700227 }
228
229 /**
230 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
231 * placement of the call or redirect it to a different number.
232 *
233 * @param originalCallIntent The original call intent.
234 * @param handle Call handle that was stored in the original call intent.
235 * @param context Valid context to send the ordered broadcast using.
236 * @param receiverRequired Whether or not the result from the ordered broadcast should be
237 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
238 */
239 private void broadcastIntent(
240 Intent originalCallIntent,
241 String handle,
242 Context context,
243 boolean receiverRequired) {
244 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
245 if (handle != null) {
246 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
247 }
248
249 // Force receivers of this broadcast intent to run at foreground priority because we
250 // want to finish processing the broadcast intent as soon as possible.
251 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
252 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
253
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700254 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
Yorke Lee33501632014-03-17 19:24:12 -0700255
256 context.sendOrderedBroadcastAsUser(
257 broadcastIntent,
258 UserHandle.OWNER,
259 PERMISSION,
260 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
261 null, // scheduler
262 Activity.RESULT_OK, // initialCode
263 handle, // initialData: initial value for the result data (number to be modified)
264 null); // initialExtras
265 }
266
267 /**
268 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
269 * source intent to the destination one.
270 *
271 * @param src Intent which may contain the provider's extras.
272 * @param dst Intent where a copy of the extras will be added if applicable.
273 */
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700274 public void checkAndCopyProviderExtras(Intent src, Intent dst) {
275 if (src == null) {
276 return;
277 }
Yorke Lee33501632014-03-17 19:24:12 -0700278 if (hasGatewayProviderExtras(src)) {
279 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
280 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
281 dst.putExtra(EXTRA_GATEWAY_URI,
282 src.getStringExtra(EXTRA_GATEWAY_URI));
283 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
284 return;
285 }
286
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
330 private void launchSystemDialer(Context context, Uri handle) {
331 Intent systemDialerIntent = new Intent();
332 final Resources resources = context.getResources();
333 systemDialerIntent.setClassName(
334 resources.getString(R.string.ui_default_package),
335 resources.getString(R.string.dialer_default_class));
336 systemDialerIntent.setAction(Intent.ACTION_DIAL);
337 systemDialerIntent.setData(handle);
338 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
339 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
340 context.startActivity(systemDialerIntent);
341 }
342
343 /**
344 * Check whether or not this is an emergency number, in order to enforce the restriction
345 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
346 * calls.
347 *
348 * To prevent malicious 3rd party apps from making emergency calls by passing in an
349 * "invalid" number like "9111234" (that isn't technically an emergency number but might
350 * still result in an emergency call with some networks), we use
351 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
352 *
353 * @param context Valid context
354 * @param handle Handle to inspect in order to determine whether or not an emergency number
355 * is potentially being dialed
356 * @return True if the handle is potentially an emergency number.
357 */
358 private boolean isPotentialEmergencyNumber(Context context, String handle) {
359 Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
Yorke Lee66255452014-06-05 08:09:24 -0700360 return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle);
Yorke Lee33501632014-03-17 19:24:12 -0700361 }
362
363 /**
364 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
365 * the call intent action to an appropriate one.
366 *
367 * @param intent Intent to rewrite the action for
368 * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
369 * number.
370 */
371 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
372 if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
373 // If we were launched directly from the CallActivity, not one of its more privileged
374 // aliases, then make sure that only the non-privileged actions are allowed.
375 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
376 Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
377 intent.setAction(Intent.ACTION_CALL);
378 }
379 }
380
381 String action = intent.getAction();
382
383 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
384 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
385 if (isPotentialEmergencyNumber) {
386 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
387 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
388 action = Intent.ACTION_CALL_EMERGENCY;
389 } else {
390 action = Intent.ACTION_CALL;
391 }
392 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
393 intent.setAction(action);
394 }
395 }
396}