blob: e819bd3eefdccd88642d6e36623fa86326f5ce43 [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;
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
Yorke Leecce5deb2014-06-18 11:27:42 -070079 NewOutgoingCallIntentBroadcaster(CallsManager callsManager, Intent intent,
80 boolean isDefaultPhoneApp) {
Yorke Lee33501632014-03-17 19:24:12 -070081 mCallsManager = callsManager;
Yorke Lee33501632014-03-17 19:24:12 -070082 mIntent = intent;
Yorke Leecce5deb2014-06-18 11:27:42 -070083 mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp;
Yorke Lee33501632014-03-17 19:24:12 -070084 }
85
86 /**
87 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
88 * the OutgoingCallIntentBroadcasterListener as necessary.
89 */
90 private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
91
92 @Override
93 public void onReceive(Context context, Intent intent) {
Sailesh Nepalb3308752014-04-07 14:12:47 -070094 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -070095
96 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
97 // actual number to call. (If null, no call will be placed.)
98 String resultHandle = getResultData();
99 Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle));
100
101 if (resultHandle == null) {
102 Log.v(this, "Call cancelled (null number), returning...");
103 return;
Yorke Lee66255452014-06-05 08:09:24 -0700104 } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) {
Yorke Lee33501632014-03-17 19:24:12 -0700105 Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
106 return;
107 }
108
Yorke Leeb9d4b362014-03-24 17:46:03 -0700109 Uri resultHandleUri = Uri.fromParts(
110 PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL,
111 resultHandle,
112 null);
Yorke Lee33501632014-03-17 19:24:12 -0700113
114 Uri originalUri = mIntent.getData();
115
116 if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
117 Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
118 } else {
119 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
120 + "Original: %s, Modified: %s",
121 Log.pii(originalUri),
122 Log.pii(resultHandleUri));
123 }
124
125 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
Evan Charlton89176372014-07-19 18:23:09 -0700126 PhoneAccountHandle accountHandle = getAccountHandleFromIntent(intent);
127 mCallsManager.placeOutgoingCall(resultHandleUri, gatewayInfo, accountHandle,
Evan Charltonb35fc312014-07-19 15:02:55 -0700128 mIntent.getBooleanExtra(TelecommManager.EXTRA_START_CALL_WITH_SPEAKERPHONE,
Tyler Gunnc4abd912014-07-08 14:22:10 -0700129 false),
Evan Charltonb35fc312014-07-19 15:02:55 -0700130 mIntent.getIntExtra(TelecommManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Tyler Gunnc4abd912014-07-08 14:22:10 -0700131 VideoCallProfile.VIDEO_STATE_AUDIO_ONLY));
Yorke Lee33501632014-03-17 19:24:12 -0700132 }
133 }
134
135 /**
136 * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
137 * intent.
138 *
139 * This method will handle three kinds of actions:
140 *
141 * - CALL (intent launched by all third party dialers)
142 * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
143 * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
Yorke Leecce5deb2014-06-18 11:27:42 -0700144 *
145 * @return whether or not the caller was allowed to start the outgoing call.
Yorke Lee33501632014-03-17 19:24:12 -0700146 */
Yorke Leecce5deb2014-06-18 11:27:42 -0700147 boolean processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700148 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
149
150 final Context context = TelecommApp.getInstance();
151 Intent intent = mIntent;
152
153 String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
154
155 if (TextUtils.isEmpty(handle)) {
156 Log.w(this, "Empty handle obtained from the call intent.");
Yorke Leecce5deb2014-06-18 11:27:42 -0700157 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700158 }
159
Santos Cordonb58f4532014-05-29 11:59:06 -0700160 boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle);
161
162 if (!isUriNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700163 handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
164 handle = PhoneNumberUtils.stripSeparators(handle);
165 }
166
167 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
168 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
169
170 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
171 // True for certain types of numbers that are not intended to be intercepted or modified
172 // by third parties (e.g. emergency numbers).
173 boolean callImmediately = false;
174
175 String action = intent.getAction();
176 if (Intent.ACTION_CALL.equals(action)) {
177 if (isPotentialEmergencyNumber) {
Yorke Leecce5deb2014-06-18 11:27:42 -0700178 if (!mIsDefaultOrSystemPhoneApp) {
179 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s "
180 + "unless caller is system or default dialer.", handle, intent);
181 launchSystemDialer(context, intent.getData());
182 return false;
183 } else {
184 callImmediately = true;
185 }
Yorke Lee33501632014-03-17 19:24:12 -0700186 }
Yorke Lee33501632014-03-17 19:24:12 -0700187 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
188 if (!isPotentialEmergencyNumber) {
189 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
190 + "Intent %s.", handle, intent);
Yorke Leecce5deb2014-06-18 11:27:42 -0700191 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700192 }
193 callImmediately = true;
194 } else {
195 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Leecce5deb2014-06-18 11:27:42 -0700196 return false;
Yorke Lee33501632014-03-17 19:24:12 -0700197 }
198
199 if (callImmediately) {
200 Log.i(this, "Placing call immediately instead of waiting for "
201 + " OutgoingCallBroadcastReceiver: %s", intent);
Santos Cordonb58f4532014-05-29 11:59:06 -0700202 String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL;
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700203 boolean speakerphoneOn = mIntent.getBooleanExtra(
Evan Charltonb35fc312014-07-19 15:02:55 -0700204 TelecommManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700205 int videoState = mIntent.getIntExtra(
Evan Charltonb35fc312014-07-19 15:02:55 -0700206 TelecommManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700207 VideoCallProfile.VIDEO_STATE_AUDIO_ONLY);
Santos Cordonb58f4532014-05-29 11:59:06 -0700208 mCallsManager.placeOutgoingCall(
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700209 Uri.fromParts(scheme, handle, null), null, null, speakerphoneOn, videoState);
Yorke Lee33501632014-03-17 19:24:12 -0700210
211 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
212 // so that third parties can still inspect (but not intercept) the outgoing call. When
213 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
214 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
215 }
216
217 broadcastIntent(intent, handle, context, !callImmediately);
Yorke Leecce5deb2014-06-18 11:27:42 -0700218 return true;
Yorke Lee33501632014-03-17 19:24:12 -0700219 }
220
221 /**
222 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
223 * placement of the call or redirect it to a different number.
224 *
225 * @param originalCallIntent The original call intent.
226 * @param handle Call handle that was stored in the original call intent.
227 * @param context Valid context to send the ordered broadcast using.
228 * @param receiverRequired Whether or not the result from the ordered broadcast should be
229 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
230 */
231 private void broadcastIntent(
232 Intent originalCallIntent,
233 String handle,
234 Context context,
235 boolean receiverRequired) {
236 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
237 if (handle != null) {
238 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
239 }
240
241 // Force receivers of this broadcast intent to run at foreground priority because we
242 // want to finish processing the broadcast intent as soon as possible.
243 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
244 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
245
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700246 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
Yorke Lee33501632014-03-17 19:24:12 -0700247
248 context.sendOrderedBroadcastAsUser(
249 broadcastIntent,
250 UserHandle.OWNER,
251 PERMISSION,
252 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
253 null, // scheduler
254 Activity.RESULT_OK, // initialCode
255 handle, // initialData: initial value for the result data (number to be modified)
256 null); // initialExtras
257 }
258
259 /**
260 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
261 * source intent to the destination one.
262 *
263 * @param src Intent which may contain the provider's extras.
264 * @param dst Intent where a copy of the extras will be added if applicable.
265 */
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700266 public void checkAndCopyProviderExtras(Intent src, Intent dst) {
267 if (src == null) {
268 return;
269 }
Yorke Lee33501632014-03-17 19:24:12 -0700270 if (hasGatewayProviderExtras(src)) {
271 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
272 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
273 dst.putExtra(EXTRA_GATEWAY_URI,
274 src.getStringExtra(EXTRA_GATEWAY_URI));
275 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
276 return;
277 }
Evan Charlton89176372014-07-19 18:23:09 -0700278 PhoneAccountHandle extraAccountHandle =
279 src.getParcelableExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE);
280 if (extraAccountHandle != null) {
281 dst.putExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE, extraAccountHandle);
Ihab Awad98a55602014-06-30 21:27:28 -0700282 Log.d(this, "Found and copied account extra to broadcast intent.");
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700283 }
Yorke Lee33501632014-03-17 19:24:12 -0700284
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700285 Log.d(this, "No provider extras found in call intent.");
Yorke Lee33501632014-03-17 19:24:12 -0700286 }
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) {
Yorke Lee33501632014-03-17 19:24:12 -0700295 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
296 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
297
298 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
299 }
300
301 private static Uri getGatewayUriFromString(String gatewayUriString) {
302 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
303 }
304
305 /**
306 * Extracts gateway provider information from a provided intent..
307 *
308 * @param intent to extract gateway provider information from.
309 * @param trueHandle The actual call handle that the user is trying to dial
310 * @return GatewayInfo object containing extracted gateway provider information as well as
311 * the actual handle the user is trying to dial.
312 */
313 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
314 if (intent == null) {
315 return null;
316 }
317
318 // Check if gateway extras are present.
319 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
320 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
321 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
322 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
323 }
324
325 return null;
326 }
327
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700328 /**
Ihab Awad98a55602014-06-30 21:27:28 -0700329 * Extracts account/connection provider information from a provided intent..
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700330 *
Ihab Awad98a55602014-06-30 21:27:28 -0700331 * @param intent to extract account information from.
332 * @return Account object containing extracted account information
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700333 */
Evan Charlton89176372014-07-19 18:23:09 -0700334 public static PhoneAccountHandle getAccountHandleFromIntent(Intent intent) {
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700335 if (intent == null) {
336 return null;
337 }
338
Evan Charlton89176372014-07-19 18:23:09 -0700339 return intent.getParcelableExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE);
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700340 }
341
Yorke Lee33501632014-03-17 19:24:12 -0700342 private void launchSystemDialer(Context context, Uri handle) {
343 Intent systemDialerIntent = new Intent();
344 final Resources resources = context.getResources();
345 systemDialerIntent.setClassName(
346 resources.getString(R.string.ui_default_package),
347 resources.getString(R.string.dialer_default_class));
348 systemDialerIntent.setAction(Intent.ACTION_DIAL);
349 systemDialerIntent.setData(handle);
350 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
351 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
352 context.startActivity(systemDialerIntent);
353 }
354
355 /**
356 * Check whether or not this is an emergency number, in order to enforce the restriction
357 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
358 * calls.
359 *
360 * To prevent malicious 3rd party apps from making emergency calls by passing in an
361 * "invalid" number like "9111234" (that isn't technically an emergency number but might
362 * still result in an emergency call with some networks), we use
363 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
364 *
365 * @param context Valid context
366 * @param handle Handle to inspect in order to determine whether or not an emergency number
367 * is potentially being dialed
368 * @return True if the handle is potentially an emergency number.
369 */
370 private boolean isPotentialEmergencyNumber(Context context, String handle) {
371 Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
Yorke Lee66255452014-06-05 08:09:24 -0700372 return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle);
Yorke Lee33501632014-03-17 19:24:12 -0700373 }
374
375 /**
376 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
377 * the call intent action to an appropriate one.
378 *
379 * @param intent Intent to rewrite the action for
380 * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
381 * number.
382 */
383 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
384 if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
385 // If we were launched directly from the CallActivity, not one of its more privileged
386 // aliases, then make sure that only the non-privileged actions are allowed.
387 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
388 Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
389 intent.setAction(Intent.ACTION_CALL);
390 }
391 }
392
393 String action = intent.getAction();
394
395 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
396 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
397 if (isPotentialEmergencyNumber) {
398 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
399 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
400 action = Intent.ACTION_CALL_EMERGENCY;
401 } else {
402 action = Intent.ACTION_CALL;
403 }
404 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
405 intent.setAction(action);
406 }
407 }
408}