blob: 369a1fd953644117bba99e66582ebc46122f956b [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;
20import android.app.ActivityManagerNative;
21import android.app.AlertDialog;
22import android.app.AppOpsManager;
23import android.app.Dialog;
24import android.content.BroadcastReceiver;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
28import android.content.res.Configuration;
29import android.content.res.Resources;
30import android.database.Cursor;
31import android.net.Uri;
32import android.os.Binder;
33import android.os.Bundle;
34import android.os.Handler;
35import android.os.Message;
36import android.os.RemoteException;
37import android.os.SystemProperties;
38import android.os.UserHandle;
39import android.provider.ContactsContract;
40import android.telecomm.GatewayInfo;
41import android.telecomm.TelecommConstants;
42import android.telephony.PhoneNumberUtils;
43import android.telephony.TelephonyManager;
44import android.text.TextUtils;
45import android.view.View;
46import android.widget.ProgressBar;
47
48/**
49 * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the
50 * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which
51 * contains the phone number being dialed. Applications can use this intent to (1) see which numbers
52 * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call
53 * from being placed.
54 *
55 * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it
56 * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}.
57 *
58 * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail
59 * number) are exempt from being broadcast.
60 *
61 * Calls to emergency numbers are still broadcast for informative purposes. The call is placed
62 * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented.
63 */
64class NewOutgoingCallIntentBroadcaster {
65 /** Required permission for any app that wants to consume ACTION_NEW_OUTGOING_CALL. */
66 private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS;
67
68 private static final String EXTRA_ACTUAL_NUMBER_TO_DIAL =
69 "android.telecomm.extra.ACTUAL_NUMBER_TO_DIAL";
70
71 /**
72 * Legacy string constants used to retrieve gateway provider extras from intents. These still
73 * need to be copied from the source call intent to the destination intent in order to
74 * support third party gateway providers that are still using old string constants in
75 * Telephony.
76 */
77 public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE =
78 "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE";
79 public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI";
80
81 private final CallsManager mCallsManager;
82 private final ContactInfo mContactInfo;
83 private final Intent mIntent;
84
85 NewOutgoingCallIntentBroadcaster(CallsManager callsManager, ContactInfo contactInfo,
86 Intent intent) {
87 mCallsManager = callsManager;
88 mContactInfo = contactInfo;
89 mIntent = intent;
90 }
91
92 /**
93 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
94 * the OutgoingCallIntentBroadcasterListener as necessary.
95 */
96 private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
97
98 @Override
99 public void onReceive(Context context, Intent intent) {
100 Log.v(this, "doReceive: %s");
101
102 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
103 // actual number to call. (If null, no call will be placed.)
104 String resultHandle = getResultData();
105 Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle));
106
107 if (resultHandle == null) {
108 Log.v(this, "Call cancelled (null number), returning...");
109 return;
110 } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(resultHandle, context)) {
111 Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
112 return;
113 }
114
115 Uri resultHandleUri = Uri.parse(resultHandle);
116
117 Uri originalUri = mIntent.getData();
118
119 if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
120 Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
121 } else {
122 Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
123 + "Original: %s, Modified: %s",
124 Log.pii(originalUri),
125 Log.pii(resultHandleUri));
126 }
127
128 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
129 mCallsManager.placeOutgoingCall(resultHandleUri, mContactInfo, gatewayInfo);
130 }
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 */
143 void processIntent() {
144 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.");
153 return;
154 }
155
156 if (!PhoneNumberUtils.isUriNumber(handle)) {
157 handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
158 handle = PhoneNumberUtils.stripSeparators(handle);
159 }
160
161 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
162 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
163
164 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
165 // True for certain types of numbers that are not intended to be intercepted or modified
166 // by third parties (e.g. emergency numbers).
167 boolean callImmediately = false;
168
169 String action = intent.getAction();
170 if (Intent.ACTION_CALL.equals(action)) {
171 if (isPotentialEmergencyNumber) {
172 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s.",
173 handle, intent);
174 launchSystemDialer(context, intent.getData());
175 }
176 callImmediately = false;
177 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
178 if (!isPotentialEmergencyNumber) {
179 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
180 + "Intent %s.", handle, intent);
181 return;
182 }
183 callImmediately = true;
184 } else {
185 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
186 return;
187 }
188
189 if (callImmediately) {
190 Log.i(this, "Placing call immediately instead of waiting for "
191 + " OutgoingCallBroadcastReceiver: %s", intent);
192 mCallsManager.placeOutgoingCall(Uri.parse(handle), mContactInfo, null);
193
194 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
195 // so that third parties can still inspect (but not intercept) the outgoing call. When
196 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
197 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
198 }
199
200 broadcastIntent(intent, handle, context, !callImmediately);
201 }
202
203 /**
204 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
205 * placement of the call or redirect it to a different number.
206 *
207 * @param originalCallIntent The original call intent.
208 * @param handle Call handle that was stored in the original call intent.
209 * @param context Valid context to send the ordered broadcast using.
210 * @param receiverRequired Whether or not the result from the ordered broadcast should be
211 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
212 */
213 private void broadcastIntent(
214 Intent originalCallIntent,
215 String handle,
216 Context context,
217 boolean receiverRequired) {
218 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
219 if (handle != null) {
220 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
221 }
222
223 // Force receivers of this broadcast intent to run at foreground priority because we
224 // want to finish processing the broadcast intent as soon as possible.
225 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
226 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
227
228 checkAndCopyGatewayProviderExtras(originalCallIntent, broadcastIntent);
229
230 context.sendOrderedBroadcastAsUser(
231 broadcastIntent,
232 UserHandle.OWNER,
233 PERMISSION,
234 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
235 null, // scheduler
236 Activity.RESULT_OK, // initialCode
237 handle, // initialData: initial value for the result data (number to be modified)
238 null); // initialExtras
239 }
240
241 /**
242 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
243 * source intent to the destination one.
244 *
245 * @param src Intent which may contain the provider's extras.
246 * @param dst Intent where a copy of the extras will be added if applicable.
247 */
248 public void checkAndCopyGatewayProviderExtras(Intent src, Intent dst) {
249 if (hasGatewayProviderExtras(src)) {
250 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
251 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
252 dst.putExtra(EXTRA_GATEWAY_URI,
253 src.getStringExtra(EXTRA_GATEWAY_URI));
254 Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
255 return;
256 }
257
258 Log.d(this, "No gateway provider extras found in call intent.");
259 }
260
261 /**
262 * Check if valid gateway provider information is stored as extras in the intent
263 *
264 * @param intent to check for
265 * @return true if the intent has all the gateway information extras needed.
266 */
267 private boolean hasGatewayProviderExtras(Intent intent) {
268 if (intent == null) {
269 return false;
270 }
271 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
272 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
273
274 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
275 }
276
277 private static Uri getGatewayUriFromString(String gatewayUriString) {
278 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
279 }
280
281 /**
282 * Extracts gateway provider information from a provided intent..
283 *
284 * @param intent to extract gateway provider information from.
285 * @param trueHandle The actual call handle that the user is trying to dial
286 * @return GatewayInfo object containing extracted gateway provider information as well as
287 * the actual handle the user is trying to dial.
288 */
289 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
290 if (intent == null) {
291 return null;
292 }
293
294 // Check if gateway extras are present.
295 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
296 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
297 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
298 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
299 }
300
301 return null;
302 }
303
304 private void launchSystemDialer(Context context, Uri handle) {
305 Intent systemDialerIntent = new Intent();
306 final Resources resources = context.getResources();
307 systemDialerIntent.setClassName(
308 resources.getString(R.string.ui_default_package),
309 resources.getString(R.string.dialer_default_class));
310 systemDialerIntent.setAction(Intent.ACTION_DIAL);
311 systemDialerIntent.setData(handle);
312 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
313 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
314 context.startActivity(systemDialerIntent);
315 }
316
317 /**
318 * Check whether or not this is an emergency number, in order to enforce the restriction
319 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
320 * calls.
321 *
322 * To prevent malicious 3rd party apps from making emergency calls by passing in an
323 * "invalid" number like "9111234" (that isn't technically an emergency number but might
324 * still result in an emergency call with some networks), we use
325 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
326 *
327 * @param context Valid context
328 * @param handle Handle to inspect in order to determine whether or not an emergency number
329 * is potentially being dialed
330 * @return True if the handle is potentially an emergency number.
331 */
332 private boolean isPotentialEmergencyNumber(Context context, String handle) {
333 Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
334 return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(handle,context);
335 }
336
337 /**
338 * Given a call intent and whether or not the number to dial is an emergency number, rewrite
339 * the call intent action to an appropriate one.
340 *
341 * @param intent Intent to rewrite the action for
342 * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
343 * number.
344 */
345 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
346 if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
347 // If we were launched directly from the CallActivity, not one of its more privileged
348 // aliases, then make sure that only the non-privileged actions are allowed.
349 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
350 Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
351 intent.setAction(Intent.ACTION_CALL);
352 }
353 }
354
355 String action = intent.getAction();
356
357 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
358 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
359 if (isPotentialEmergencyNumber) {
360 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
361 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
362 action = Intent.ACTION_CALL_EMERGENCY;
363 } else {
364 action = Intent.ACTION_CALL;
365 }
366 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
367 intent.setAction(action);
368 }
369 }
370}