blob: 0b99dc6d717a6851a001fe049df4f09297b38e95 [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;
Ihab Awad98a55602014-06-30 21:27:28 -070027import android.telecomm.PhoneAccount;
Yorke Lee33501632014-03-17 19:24:12 -070028import android.telecomm.TelecommConstants;
Tyler Gunnc4abd912014-07-08 14:22:10 -070029import android.telecomm.VideoCallProfile;
Yorke Lee33501632014-03-17 19:24:12 -070030import android.telephony.PhoneNumberUtils;
31import android.telephony.TelephonyManager;
32import android.text.TextUtils;
Yorke Lee33501632014-03-17 19:24:12 -070033
34/**
35 * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the
36 * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which
37 * contains the phone number being dialed. Applications can use this intent to (1) see which numbers
38 * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call
39 * from being placed.
40 *
41 * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it
42 * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}.
43 *
44 * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail
45 * number) are exempt from being broadcast.
46 *
47 * Calls to emergency numbers are still broadcast for informative purposes. The call is placed
48 * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented.
49 */
50class NewOutgoingCallIntentBroadcaster {
51 /** Required permission for any app that wants to consume ACTION_NEW_OUTGOING_CALL. */
52 private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS;
53
54 private static final String EXTRA_ACTUAL_NUMBER_TO_DIAL =
55 "android.telecomm.extra.ACTUAL_NUMBER_TO_DIAL";
56
57 /**
58 * Legacy string constants used to retrieve gateway provider extras from intents. These still
59 * need to be copied from the source call intent to the destination intent in order to
60 * support third party gateway providers that are still using old string constants in
61 * Telephony.
62 */
63 public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE =
64 "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE";
65 public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI";
Santos Cordon571f0732014-06-25 18:13:15 -070066 public static final String EXTRA_GATEWAY_ORIGINAL_URI =
67 "com.android.phone.extra.GATEWAY_ORIGINAL_URI";
Yorke Lee33501632014-03-17 19:24:12 -070068
Yorke Leeb9d4b362014-03-24 17:46:03 -070069 private static final String SCHEME_TEL = "tel";
70 private static final String SCHEME_SIP = "sip";
71
Yorke Lee33501632014-03-17 19:24:12 -070072 private final CallsManager mCallsManager;
Yorke Lee33501632014-03-17 19:24:12 -070073 private final Intent mIntent;
74
Yorke Lee6f3f7af2014-07-11 10:59:46 -070075 NewOutgoingCallIntentBroadcaster(CallsManager callsManager, Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -070076 mCallsManager = callsManager;
Yorke Lee33501632014-03-17 19:24:12 -070077 mIntent = intent;
78 }
79
80 /**
81 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
82 * the OutgoingCallIntentBroadcasterListener as necessary.
83 */
84 private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
85
86 @Override
87 public void onReceive(Context context, Intent intent) {
Sailesh Nepalb3308752014-04-07 14:12:47 -070088 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -070089
90 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
91 // actual number to call. (If null, no call will be placed.)
92 String resultHandle = getResultData();
93 Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle));
94
95 if (resultHandle == null) {
96 Log.v(this, "Call cancelled (null number), returning...");
97 return;
Yorke Lee66255452014-06-05 08:09:24 -070098 } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) {
Yorke Lee33501632014-03-17 19:24:12 -070099 Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
100 return;
101 }
102
Yorke Leeb9d4b362014-03-24 17:46:03 -0700103 Uri resultHandleUri = Uri.fromParts(
104 PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL,
105 resultHandle,
106 null);
Yorke Lee33501632014-03-17 19:24:12 -0700107
108 Uri originalUri = mIntent.getData();
109
110 if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
111 Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
112 } else {
113 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
114 + "Original: %s, Modified: %s",
115 Log.pii(originalUri),
116 Log.pii(resultHandleUri));
117 }
118
119 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
Ihab Awad98a55602014-06-30 21:27:28 -0700120 PhoneAccount account = getAccountFromIntent(intent);
Yorke Lee6f3f7af2014-07-11 10:59:46 -0700121 mCallsManager.placeOutgoingCall(resultHandleUri, gatewayInfo, account,
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700122 mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
Tyler Gunnc4abd912014-07-08 14:22:10 -0700123 false),
124 mIntent.getIntExtra(TelecommConstants.EXTRA_START_CALL_WITH_VIDEO_STATE,
125 VideoCallProfile.VIDEO_STATE_AUDIO_ONLY));
Yorke Lee33501632014-03-17 19:24:12 -0700126 }
127 }
128
129 /**
130 * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
131 * intent.
132 *
133 * This method will handle three kinds of actions:
134 *
135 * - CALL (intent launched by all third party dialers)
136 * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
137 * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
138 */
Yorke Leed362fe32014-06-19 22:24:28 +0000139 void processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700140 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
141
142 final Context context = TelecommApp.getInstance();
143 Intent intent = mIntent;
144
145 String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
146
147 if (TextUtils.isEmpty(handle)) {
148 Log.w(this, "Empty handle obtained from the call intent.");
Yorke Leed362fe32014-06-19 22:24:28 +0000149 return;
Yorke Lee33501632014-03-17 19:24:12 -0700150 }
151
Santos Cordonb58f4532014-05-29 11:59:06 -0700152 boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle);
153
154 if (!isUriNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700155 handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
156 handle = PhoneNumberUtils.stripSeparators(handle);
157 }
158
159 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
160 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
161
162 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
163 // True for certain types of numbers that are not intended to be intercepted or modified
164 // by third parties (e.g. emergency numbers).
165 boolean callImmediately = false;
166
167 String action = intent.getAction();
168 if (Intent.ACTION_CALL.equals(action)) {
169 if (isPotentialEmergencyNumber) {
Yorke Leed362fe32014-06-19 22:24:28 +0000170 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s.",
171 handle, intent);
172 launchSystemDialer(context, intent.getData());
Yorke Lee33501632014-03-17 19:24:12 -0700173 }
Yorke Leed362fe32014-06-19 22:24:28 +0000174 callImmediately = false;
Yorke Lee33501632014-03-17 19:24:12 -0700175 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
176 if (!isPotentialEmergencyNumber) {
177 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
178 + "Intent %s.", handle, intent);
Yorke Leed362fe32014-06-19 22:24:28 +0000179 return;
Yorke Lee33501632014-03-17 19:24:12 -0700180 }
181 callImmediately = true;
182 } else {
183 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Leed362fe32014-06-19 22:24:28 +0000184 return;
Yorke Lee33501632014-03-17 19:24:12 -0700185 }
186
187 if (callImmediately) {
188 Log.i(this, "Placing call immediately instead of waiting for "
189 + " OutgoingCallBroadcastReceiver: %s", intent);
Santos Cordonb58f4532014-05-29 11:59:06 -0700190 String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL;
191 mCallsManager.placeOutgoingCall(
Yorke Lee6f3f7af2014-07-11 10:59:46 -0700192 Uri.fromParts(scheme, handle, null), null, null, mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
Tyler Gunnc4abd912014-07-08 14:22:10 -0700193 false),
194 mIntent.getIntExtra(TelecommConstants.EXTRA_START_CALL_WITH_VIDEO_STATE,
195 VideoCallProfile.VIDEO_STATE_AUDIO_ONLY));
Yorke Lee33501632014-03-17 19:24:12 -0700196
197 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
198 // so that third parties can still inspect (but not intercept) the outgoing call. When
199 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
200 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
201 }
202
203 broadcastIntent(intent, handle, context, !callImmediately);
204 }
205
206 /**
207 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
208 * placement of the call or redirect it to a different number.
209 *
210 * @param originalCallIntent The original call intent.
211 * @param handle Call handle that was stored in the original call intent.
212 * @param context Valid context to send the ordered broadcast using.
213 * @param receiverRequired Whether or not the result from the ordered broadcast should be
214 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
215 */
216 private void broadcastIntent(
217 Intent originalCallIntent,
218 String handle,
219 Context context,
220 boolean receiverRequired) {
221 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
222 if (handle != null) {
223 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
224 }
225
226 // Force receivers of this broadcast intent to run at foreground priority because we
227 // want to finish processing the broadcast intent as soon as possible.
228 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
229 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
230
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700231 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
Yorke Lee33501632014-03-17 19:24:12 -0700232
233 context.sendOrderedBroadcastAsUser(
234 broadcastIntent,
235 UserHandle.OWNER,
236 PERMISSION,
237 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
238 null, // scheduler
239 Activity.RESULT_OK, // initialCode
240 handle, // initialData: initial value for the result data (number to be modified)
241 null); // initialExtras
242 }
243
244 /**
245 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
246 * source intent to the destination one.
247 *
248 * @param src Intent which may contain the provider's extras.
249 * @param dst Intent where a copy of the extras will be added if applicable.
250 */
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700251 public void checkAndCopyProviderExtras(Intent src, Intent dst) {
252 if (src == null) {
253 return;
254 }
Yorke Lee33501632014-03-17 19:24:12 -0700255 if (hasGatewayProviderExtras(src)) {
256 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
257 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
258 dst.putExtra(EXTRA_GATEWAY_URI,
259 src.getStringExtra(EXTRA_GATEWAY_URI));
260 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
261 return;
262 }
Ihab Awad98a55602014-06-30 21:27:28 -0700263 PhoneAccount extraAccount = src.getParcelableExtra(
264 TelephonyManager.EXTRA_ACCOUNT);
265 if (extraAccount != null) {
266 dst.putExtra(TelephonyManager.EXTRA_ACCOUNT, extraAccount);
267 Log.d(this, "Found and copied account extra to broadcast intent.");
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700268 }
Yorke Lee33501632014-03-17 19:24:12 -0700269
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700270 Log.d(this, "No provider extras found in call intent.");
Yorke Lee33501632014-03-17 19:24:12 -0700271 }
272
273 /**
274 * Check if valid gateway provider information is stored as extras in the intent
275 *
276 * @param intent to check for
277 * @return true if the intent has all the gateway information extras needed.
278 */
279 private boolean hasGatewayProviderExtras(Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -0700280 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
281 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
282
283 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
284 }
285
286 private static Uri getGatewayUriFromString(String gatewayUriString) {
287 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
288 }
289
290 /**
291 * Extracts gateway provider information from a provided intent..
292 *
293 * @param intent to extract gateway provider information from.
294 * @param trueHandle The actual call handle that the user is trying to dial
295 * @return GatewayInfo object containing extracted gateway provider information as well as
296 * the actual handle the user is trying to dial.
297 */
298 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
299 if (intent == null) {
300 return null;
301 }
302
303 // Check if gateway extras are present.
304 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
305 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
306 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
307 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
308 }
309
310 return null;
311 }
312
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700313 /**
Ihab Awad98a55602014-06-30 21:27:28 -0700314 * Extracts account/connection provider information from a provided intent..
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700315 *
Ihab Awad98a55602014-06-30 21:27:28 -0700316 * @param intent to extract account information from.
317 * @return Account object containing extracted account information
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700318 */
Ihab Awad98a55602014-06-30 21:27:28 -0700319 public static PhoneAccount getAccountFromIntent(Intent intent) {
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700320 if (intent == null) {
321 return null;
322 }
323
Ihab Awad98a55602014-06-30 21:27:28 -0700324 return intent.getParcelableExtra(TelephonyManager.EXTRA_ACCOUNT);
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700325 }
326
Yorke Lee33501632014-03-17 19:24:12 -0700327 private void launchSystemDialer(Context context, Uri handle) {
328 Intent systemDialerIntent = new Intent();
329 final Resources resources = context.getResources();
330 systemDialerIntent.setClassName(
331 resources.getString(R.string.ui_default_package),
332 resources.getString(R.string.dialer_default_class));
333 systemDialerIntent.setAction(Intent.ACTION_DIAL);
334 systemDialerIntent.setData(handle);
335 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
336 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
337 context.startActivity(systemDialerIntent);
338 }
339
340 /**
341 * Check whether or not this is an emergency number, in order to enforce the restriction
342 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
343 * calls.
344 *
345 * To prevent malicious 3rd party apps from making emergency calls by passing in an
346 * "invalid" number like "9111234" (that isn't technically an emergency number but might
347 * still result in an emergency call with some networks), we use
348 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
349 *
350 * @param context Valid context
351 * @param handle Handle to inspect in order to determine whether or not an emergency number
352 * is potentially being dialed
353 * @return True if the handle is potentially an emergency number.
354 */
355 private boolean isPotentialEmergencyNumber(Context context, String handle) {
356 Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
Yorke Lee66255452014-06-05 08:09:24 -0700357 return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle);
Yorke Lee33501632014-03-17 19:24:12 -0700358 }
359
360 /**
361 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
362 * the call intent action to an appropriate one.
363 *
364 * @param intent Intent to rewrite the action for
365 * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
366 * number.
367 */
368 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
369 if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
370 // If we were launched directly from the CallActivity, not one of its more privileged
371 // aliases, then make sure that only the non-privileged actions are allowed.
372 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
373 Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
374 intent.setAction(Intent.ACTION_CALL);
375 }
376 }
377
378 String action = intent.getAction();
379
380 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
381 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
382 if (isPotentialEmergencyNumber) {
383 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
384 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
385 action = Intent.ACTION_CALL_EMERGENCY;
386 } else {
387 action = Intent.ACTION_CALL;
388 }
389 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
390 intent.setAction(action);
391 }
392 }
393}