blob: c75e184c8c403ba68394d004f55e7eee51911cf0 [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;
73 private final ContactInfo mContactInfo;
74 private final Intent mIntent;
75
76 NewOutgoingCallIntentBroadcaster(CallsManager callsManager, ContactInfo contactInfo,
Yorke Leed362fe32014-06-19 22:24:28 +000077 Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -070078 mCallsManager = callsManager;
79 mContactInfo = contactInfo;
80 mIntent = intent;
81 }
82
83 /**
84 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
85 * the OutgoingCallIntentBroadcasterListener as necessary.
86 */
87 private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
88
89 @Override
90 public void onReceive(Context context, Intent intent) {
Sailesh Nepalb3308752014-04-07 14:12:47 -070091 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -070092
93 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
94 // actual number to call. (If null, no call will be placed.)
95 String resultHandle = getResultData();
96 Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle));
97
98 if (resultHandle == null) {
99 Log.v(this, "Call cancelled (null number), returning...");
100 return;
Yorke Lee66255452014-06-05 08:09:24 -0700101 } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) {
Yorke Lee33501632014-03-17 19:24:12 -0700102 Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
103 return;
104 }
105
Yorke Leeb9d4b362014-03-24 17:46:03 -0700106 Uri resultHandleUri = Uri.fromParts(
107 PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL,
108 resultHandle,
109 null);
Yorke Lee33501632014-03-17 19:24:12 -0700110
111 Uri originalUri = mIntent.getData();
112
113 if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
114 Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
115 } else {
116 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
117 + "Original: %s, Modified: %s",
118 Log.pii(originalUri),
119 Log.pii(resultHandleUri));
120 }
121
122 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
Ihab Awad98a55602014-06-30 21:27:28 -0700123 PhoneAccount account = getAccountFromIntent(intent);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700124 mCallsManager.placeOutgoingCall(resultHandleUri, mContactInfo, gatewayInfo,
Ihab Awad98a55602014-06-30 21:27:28 -0700125 account,
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700126 mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
Tyler Gunnc4abd912014-07-08 14:22:10 -0700127 false),
128 mIntent.getIntExtra(TelecommConstants.EXTRA_START_CALL_WITH_VIDEO_STATE,
129 VideoCallProfile.VIDEO_STATE_AUDIO_ONLY));
Yorke Lee33501632014-03-17 19:24:12 -0700130 }
131 }
132
133 /**
134 * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
135 * intent.
136 *
137 * This method will handle three kinds of actions:
138 *
139 * - CALL (intent launched by all third party dialers)
140 * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
141 * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
142 */
Yorke Leed362fe32014-06-19 22:24:28 +0000143 void processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700144 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
145
146 final Context context = TelecommApp.getInstance();
147 Intent intent = mIntent;
148
149 String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
150
151 if (TextUtils.isEmpty(handle)) {
152 Log.w(this, "Empty handle obtained from the call intent.");
Yorke Leed362fe32014-06-19 22:24:28 +0000153 return;
Yorke Lee33501632014-03-17 19:24:12 -0700154 }
155
Santos Cordonb58f4532014-05-29 11:59:06 -0700156 boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle);
157
158 if (!isUriNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700159 handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
160 handle = PhoneNumberUtils.stripSeparators(handle);
161 }
162
163 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
164 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
165
166 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
167 // True for certain types of numbers that are not intended to be intercepted or modified
168 // by third parties (e.g. emergency numbers).
169 boolean callImmediately = false;
170
171 String action = intent.getAction();
172 if (Intent.ACTION_CALL.equals(action)) {
173 if (isPotentialEmergencyNumber) {
Yorke Leed362fe32014-06-19 22:24:28 +0000174 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s.",
175 handle, intent);
176 launchSystemDialer(context, intent.getData());
Yorke Lee33501632014-03-17 19:24:12 -0700177 }
Yorke Leed362fe32014-06-19 22:24:28 +0000178 callImmediately = false;
Yorke Lee33501632014-03-17 19:24:12 -0700179 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
180 if (!isPotentialEmergencyNumber) {
181 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
182 + "Intent %s.", handle, intent);
Yorke Leed362fe32014-06-19 22:24:28 +0000183 return;
Yorke Lee33501632014-03-17 19:24:12 -0700184 }
185 callImmediately = true;
186 } else {
187 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Leed362fe32014-06-19 22:24:28 +0000188 return;
Yorke Lee33501632014-03-17 19:24:12 -0700189 }
190
191 if (callImmediately) {
192 Log.i(this, "Placing call immediately instead of waiting for "
193 + " OutgoingCallBroadcastReceiver: %s", intent);
Santos Cordonb58f4532014-05-29 11:59:06 -0700194 String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL;
195 mCallsManager.placeOutgoingCall(
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700196 Uri.fromParts(scheme, handle, null), mContactInfo, null, null,
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700197 mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
Tyler Gunnc4abd912014-07-08 14:22:10 -0700198 false),
199 mIntent.getIntExtra(TelecommConstants.EXTRA_START_CALL_WITH_VIDEO_STATE,
200 VideoCallProfile.VIDEO_STATE_AUDIO_ONLY));
Yorke Lee33501632014-03-17 19:24:12 -0700201
202 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
203 // so that third parties can still inspect (but not intercept) the outgoing call. When
204 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
205 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
206 }
207
208 broadcastIntent(intent, handle, context, !callImmediately);
209 }
210
211 /**
212 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
213 * placement of the call or redirect it to a different number.
214 *
215 * @param originalCallIntent The original call intent.
216 * @param handle Call handle that was stored in the original call intent.
217 * @param context Valid context to send the ordered broadcast using.
218 * @param receiverRequired Whether or not the result from the ordered broadcast should be
219 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
220 */
221 private void broadcastIntent(
222 Intent originalCallIntent,
223 String handle,
224 Context context,
225 boolean receiverRequired) {
226 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
227 if (handle != null) {
228 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
229 }
230
231 // Force receivers of this broadcast intent to run at foreground priority because we
232 // want to finish processing the broadcast intent as soon as possible.
233 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
234 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
235
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700236 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
Yorke Lee33501632014-03-17 19:24:12 -0700237
238 context.sendOrderedBroadcastAsUser(
239 broadcastIntent,
240 UserHandle.OWNER,
241 PERMISSION,
242 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
243 null, // scheduler
244 Activity.RESULT_OK, // initialCode
245 handle, // initialData: initial value for the result data (number to be modified)
246 null); // initialExtras
247 }
248
249 /**
250 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
251 * source intent to the destination one.
252 *
253 * @param src Intent which may contain the provider's extras.
254 * @param dst Intent where a copy of the extras will be added if applicable.
255 */
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700256 public void checkAndCopyProviderExtras(Intent src, Intent dst) {
257 if (src == null) {
258 return;
259 }
Yorke Lee33501632014-03-17 19:24:12 -0700260 if (hasGatewayProviderExtras(src)) {
261 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
262 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
263 dst.putExtra(EXTRA_GATEWAY_URI,
264 src.getStringExtra(EXTRA_GATEWAY_URI));
265 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
266 return;
267 }
Ihab Awad98a55602014-06-30 21:27:28 -0700268 PhoneAccount extraAccount = src.getParcelableExtra(
269 TelephonyManager.EXTRA_ACCOUNT);
270 if (extraAccount != null) {
271 dst.putExtra(TelephonyManager.EXTRA_ACCOUNT, extraAccount);
272 Log.d(this, "Found and copied account extra to broadcast intent.");
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700273 }
Yorke Lee33501632014-03-17 19:24:12 -0700274
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700275 Log.d(this, "No provider extras found in call intent.");
Yorke Lee33501632014-03-17 19:24:12 -0700276 }
277
278 /**
279 * Check if valid gateway provider information is stored as extras in the intent
280 *
281 * @param intent to check for
282 * @return true if the intent has all the gateway information extras needed.
283 */
284 private boolean hasGatewayProviderExtras(Intent intent) {
Yorke Lee33501632014-03-17 19:24:12 -0700285 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
286 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
287
288 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
289 }
290
291 private static Uri getGatewayUriFromString(String gatewayUriString) {
292 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
293 }
294
295 /**
296 * Extracts gateway provider information from a provided intent..
297 *
298 * @param intent to extract gateway provider information from.
299 * @param trueHandle The actual call handle that the user is trying to dial
300 * @return GatewayInfo object containing extracted gateway provider information as well as
301 * the actual handle the user is trying to dial.
302 */
303 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
304 if (intent == null) {
305 return null;
306 }
307
308 // Check if gateway extras are present.
309 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
310 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
311 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
312 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
313 }
314
315 return null;
316 }
317
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700318 /**
Ihab Awad98a55602014-06-30 21:27:28 -0700319 * Extracts account/connection provider information from a provided intent..
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700320 *
Ihab Awad98a55602014-06-30 21:27:28 -0700321 * @param intent to extract account information from.
322 * @return Account object containing extracted account information
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700323 */
Ihab Awad98a55602014-06-30 21:27:28 -0700324 public static PhoneAccount getAccountFromIntent(Intent intent) {
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700325 if (intent == null) {
326 return null;
327 }
328
Ihab Awad98a55602014-06-30 21:27:28 -0700329 return intent.getParcelableExtra(TelephonyManager.EXTRA_ACCOUNT);
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700330 }
331
Yorke Lee33501632014-03-17 19:24:12 -0700332 private void launchSystemDialer(Context context, Uri handle) {
333 Intent systemDialerIntent = new Intent();
334 final Resources resources = context.getResources();
335 systemDialerIntent.setClassName(
336 resources.getString(R.string.ui_default_package),
337 resources.getString(R.string.dialer_default_class));
338 systemDialerIntent.setAction(Intent.ACTION_DIAL);
339 systemDialerIntent.setData(handle);
340 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
341 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
342 context.startActivity(systemDialerIntent);
343 }
344
345 /**
346 * Check whether or not this is an emergency number, in order to enforce the restriction
347 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
348 * calls.
349 *
350 * To prevent malicious 3rd party apps from making emergency calls by passing in an
351 * "invalid" number like "9111234" (that isn't technically an emergency number but might
352 * still result in an emergency call with some networks), we use
353 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
354 *
355 * @param context Valid context
356 * @param handle Handle to inspect in order to determine whether or not an emergency number
357 * is potentially being dialed
358 * @return True if the handle is potentially an emergency number.
359 */
360 private boolean isPotentialEmergencyNumber(Context context, String handle) {
361 Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
Yorke Lee66255452014-06-05 08:09:24 -0700362 return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle);
Yorke Lee33501632014-03-17 19:24:12 -0700363 }
364
365 /**
366 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
367 * the call intent action to an appropriate one.
368 *
369 * @param intent Intent to rewrite the action for
370 * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
371 * number.
372 */
373 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
374 if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
375 // If we were launched directly from the CallActivity, not one of its more privileged
376 // aliases, then make sure that only the non-privileged actions are allowed.
377 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
378 Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
379 intent.setAction(Intent.ACTION_CALL);
380 }
381 }
382
383 String action = intent.getAction();
384
385 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
386 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
387 if (isPotentialEmergencyNumber) {
388 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
389 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
390 action = Intent.ACTION_CALL_EMERGENCY;
391 } else {
392 action = Intent.ACTION_CALL;
393 }
394 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
395 intent.setAction(action);
396 }
397 }
398}