blob: 6d8830e98642f64fb6e311b94aab42ce7f1b7064 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 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.phone;
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;
Yorke Leed3105fe2013-09-25 12:44:45 -070029import android.content.res.Resources;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070030import android.net.Uri;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.Message;
35import android.os.RemoteException;
36import android.os.SystemProperties;
37import android.os.UserHandle;
38import android.telephony.PhoneNumberUtils;
39import android.text.TextUtils;
40import android.util.Log;
41import android.view.View;
42import android.widget.ProgressBar;
43
44import com.android.internal.telephony.Phone;
45import com.android.internal.telephony.PhoneConstants;
46import com.android.internal.telephony.TelephonyCapabilities;
47
48/**
Santos Cordonc65769e2014-03-03 15:57:48 -080049 * OutgoingCallBroadcaster 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 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055 * After the other applications have had a chance to see the
56 * ACTION_NEW_OUTGOING_CALL intent, it finally reaches the
57 * {@link OutgoingCallReceiver}, which passes the (possibly modified)
58 * intent on to the {@link SipCallOptionHandler}, which will
59 * ultimately start the call using the CallController.placeCall() API.
60 *
Santos Cordonc65769e2014-03-03 15:57:48 -080061 * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail
62 * number) are exempt from being broadcast.
63 * Calls to emergency numbers are still broadcast for informative purposes. The call is placed
64 * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065 */
66public class OutgoingCallBroadcaster extends Activity
67 implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
68
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069 private static final String TAG = "OutgoingCallBroadcaster";
70 private static final boolean DBG =
71 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
72 // Do not check in with VDBG = true, since that may write PII to the system log.
73 private static final boolean VDBG = false;
74
Santos Cordonc65769e2014-03-03 15:57:48 -080075 /** Required permission for any app that wants to consume ACTION_NEW_OUTGOING_CALL. */
76 private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS;
77
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078 public static final String ACTION_SIP_SELECT_PHONE = "com.android.phone.SIP_SELECT_PHONE";
79 public static final String EXTRA_ALREADY_CALLED = "android.phone.extra.ALREADY_CALLED";
80 public static final String EXTRA_ORIGINAL_URI = "android.phone.extra.ORIGINAL_URI";
81 public static final String EXTRA_NEW_CALL_INTENT = "android.phone.extra.NEW_CALL_INTENT";
82 public static final String EXTRA_SIP_PHONE_URI = "android.phone.extra.SIP_PHONE_URI";
83 public static final String EXTRA_ACTUAL_NUMBER_TO_DIAL =
84 "android.phone.extra.ACTUAL_NUMBER_TO_DIAL";
Sailesh Nepalbfb68322013-11-07 14:07:41 -080085 public static final String EXTRA_THIRD_PARTY_CALL_COMPONENT =
86 "android.phone.extra.THIRD_PARTY_CALL_COMPONENT";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087
88 /**
89 * Identifier for intent extra for sending an empty Flash message for
90 * CDMA networks. This message is used by the network to simulate a
91 * press/depress of the "hookswitch" of a landline phone. Aka "empty flash".
92 *
93 * TODO: Receiving an intent extra to tell the phone to send this flash is a
94 * temporary measure. To be replaced with an external ITelephony call in the future.
95 * TODO: Keep in sync with the string defined in TwelveKeyDialer.java in Contacts app
96 * until this is replaced with the ITelephony API.
97 */
98 public static final String EXTRA_SEND_EMPTY_FLASH =
99 "com.android.phone.extra.SEND_EMPTY_FLASH";
100
101 // Dialog IDs
102 private static final int DIALOG_NOT_VOICE_CAPABLE = 1;
103
104 /** Note message codes < 100 are reserved for the PhoneApp. */
105 private static final int EVENT_OUTGOING_CALL_TIMEOUT = 101;
Santos Cordon7d86bec2013-08-08 02:01:35 -0700106 private static final int EVENT_DELAYED_FINISH = 102;
107
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108 private static final int OUTGOING_CALL_TIMEOUT_THRESHOLD = 2000; // msec
Santos Cordon7d86bec2013-08-08 02:01:35 -0700109 private static final int DELAYED_FINISH_TIME = 2000; // msec
110
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111 /**
112 * ProgressBar object with "spinner" style, which will be shown if we take more than
113 * {@link #EVENT_OUTGOING_CALL_TIMEOUT} msec to handle the incoming Intent.
114 */
115 private ProgressBar mWaitingSpinner;
116 private final Handler mHandler = new Handler() {
117 @Override
118 public void handleMessage(Message msg) {
119 if (msg.what == EVENT_OUTGOING_CALL_TIMEOUT) {
120 Log.i(TAG, "Outgoing call takes too long. Showing the spinner.");
121 mWaitingSpinner.setVisibility(View.VISIBLE);
Santos Cordon7d86bec2013-08-08 02:01:35 -0700122 } else if (msg.what == EVENT_DELAYED_FINISH) {
123 finish();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 } else {
125 Log.wtf(TAG, "Unknown message id: " + msg.what);
126 }
127 }
128 };
129
130 /**
Santos Cordon7d86bec2013-08-08 02:01:35 -0700131 * Starts the delayed finish() of OutgoingCallBroadcaster in order to give the UI
132 * some time to start up.
133 */
134 private void startDelayedFinish() {
135 mHandler.sendEmptyMessageDelayed(EVENT_DELAYED_FINISH, DELAYED_FINISH_TIME);
136 }
137
138 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700139 * OutgoingCallReceiver finishes NEW_OUTGOING_CALL broadcasts, starting
140 * the InCallScreen if the broadcast has not been canceled, possibly with
141 * a modified phone number and optional provider info (uri + package name + remote views.)
142 */
143 public class OutgoingCallReceiver extends BroadcastReceiver {
144 private static final String TAG = "OutgoingCallReceiver";
145
146 @Override
147 public void onReceive(Context context, Intent intent) {
148 mHandler.removeMessages(EVENT_OUTGOING_CALL_TIMEOUT);
Santos Cordon4ebe45d2013-10-08 10:00:35 -0700149 final boolean isAttemptingCall = doReceive(context, intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 if (DBG) Log.v(TAG, "OutgoingCallReceiver is going to finish the Activity itself.");
Santos Cordon7d86bec2013-08-08 02:01:35 -0700151
152 // We cannot finish the activity immediately here because it would cause the temporary
153 // black screen of OutgoingBroadcaster to go away and we need it to stay up until the
154 // UI (in a different process) has time to come up.
Santos Cordon4ebe45d2013-10-08 10:00:35 -0700155 // However, if we know we are not attemping a call, we need to finish the activity
156 // immediately so that subsequent CALL intents will retrigger a new
157 // OutgoingCallReceiver. see b/10857203
158 if (isAttemptingCall) {
159 startDelayedFinish();
160 } else {
161 finish();
162 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163 }
164
Santos Cordon4ebe45d2013-10-08 10:00:35 -0700165
166 /**
167 * Handes receipt of ordered new_outgoing_call intent. Verifies that the return from the
168 * ordered intent is valid.
169 * @return true if the call is being attempted; false if we are canceling the call.
170 */
171 public boolean doReceive(Context context, Intent intent) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172 if (DBG) Log.v(TAG, "doReceive: " + intent);
173
174 boolean alreadyCalled;
175 String number;
176 String originalUri;
177
178 alreadyCalled = intent.getBooleanExtra(
179 OutgoingCallBroadcaster.EXTRA_ALREADY_CALLED, false);
180 if (alreadyCalled) {
181 if (DBG) Log.v(TAG, "CALL already placed -- returning.");
Santos Cordon4ebe45d2013-10-08 10:00:35 -0700182 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183 }
184
185 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData
186 // is used as the actual number to call. (If null, no call will be
187 // placed.)
188
189 number = getResultData();
190 if (VDBG) Log.v(TAG, "- got number from resultData: '" + number + "'");
191
192 final PhoneGlobals app = PhoneGlobals.getInstance();
193
194 // OTASP-specific checks.
195 // TODO: This should probably all happen in
196 // OutgoingCallBroadcaster.onCreate(), since there's no reason to
197 // even bother with the NEW_OUTGOING_CALL broadcast if we're going
198 // to disallow the outgoing call anyway...
199 if (TelephonyCapabilities.supportsOtasp(app.phone)) {
200 boolean activateState = (app.cdmaOtaScreenState.otaScreenState
201 == OtaUtils.CdmaOtaScreenState.OtaScreenState.OTA_STATUS_ACTIVATION);
202 boolean dialogState = (app.cdmaOtaScreenState.otaScreenState
203 == OtaUtils.CdmaOtaScreenState.OtaScreenState
204 .OTA_STATUS_SUCCESS_FAILURE_DLG);
205 boolean isOtaCallActive = false;
206
207 // TODO: Need cleaner way to check if OTA is active.
208 // Also, this check seems to be broken in one obscure case: if
209 // you interrupt an OTASP call by pressing Back then Skip,
210 // otaScreenState somehow gets left in either PROGRESS or
211 // LISTENING.
212 if ((app.cdmaOtaScreenState.otaScreenState
213 == OtaUtils.CdmaOtaScreenState.OtaScreenState.OTA_STATUS_PROGRESS)
214 || (app.cdmaOtaScreenState.otaScreenState
215 == OtaUtils.CdmaOtaScreenState.OtaScreenState.OTA_STATUS_LISTENING)) {
216 isOtaCallActive = true;
217 }
218
219 if (activateState || dialogState) {
220 // The OTASP sequence is active, but either (1) the call
221 // hasn't started yet, or (2) the call has ended and we're
222 // showing the success/failure screen. In either of these
223 // cases it's OK to make a new outgoing call, but we need
224 // to take down any OTASP-related UI first.
225 if (dialogState) app.dismissOtaDialogs();
226 app.clearOtaState();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227 } else if (isOtaCallActive) {
228 // The actual OTASP call is active. Don't allow new
229 // outgoing calls at all from this state.
230 Log.w(TAG, "OTASP call is active: disallowing a new outgoing call.");
Santos Cordon4ebe45d2013-10-08 10:00:35 -0700231 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700232 }
233 }
234
235 if (number == null) {
236 if (DBG) Log.v(TAG, "CALL cancelled (null number), returning...");
Santos Cordon4ebe45d2013-10-08 10:00:35 -0700237 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238 } else if (TelephonyCapabilities.supportsOtasp(app.phone)
239 && (app.phone.getState() != PhoneConstants.State.IDLE)
240 && (app.phone.isOtaSpNumber(number))) {
241 if (DBG) Log.v(TAG, "Call is active, a 2nd OTA call cancelled -- returning.");
Santos Cordon4ebe45d2013-10-08 10:00:35 -0700242 return false;
Yorke Lee36bb2542014-06-05 08:09:52 -0700243 } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, number)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 // Just like 3rd-party apps aren't allowed to place emergency
245 // calls via the ACTION_CALL intent, we also don't allow 3rd
246 // party apps to use the NEW_OUTGOING_CALL broadcast to rewrite
247 // an outgoing call into an emergency number.
248 Log.w(TAG, "Cannot modify outgoing call to emergency number " + number + ".");
Santos Cordon4ebe45d2013-10-08 10:00:35 -0700249 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700250 }
251
252 originalUri = intent.getStringExtra(
253 OutgoingCallBroadcaster.EXTRA_ORIGINAL_URI);
254 if (originalUri == null) {
255 Log.e(TAG, "Intent is missing EXTRA_ORIGINAL_URI -- returning.");
Santos Cordon4ebe45d2013-10-08 10:00:35 -0700256 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257 }
258
259 Uri uri = Uri.parse(originalUri);
260
261 // We already called convertKeypadLettersToDigits() and
262 // stripSeparators() way back in onCreate(), before we sent out the
263 // NEW_OUTGOING_CALL broadcast. But we need to do it again here
264 // too, since the number might have been modified/rewritten during
265 // the broadcast (and may now contain letters or separators again.)
266 number = PhoneNumberUtils.convertKeypadLettersToDigits(number);
267 number = PhoneNumberUtils.stripSeparators(number);
268
269 if (DBG) Log.v(TAG, "doReceive: proceeding with call...");
270 if (VDBG) Log.v(TAG, "- uri: " + uri);
271 if (VDBG) Log.v(TAG, "- actual number to dial: '" + number + "'");
272
273 startSipCallOptionHandler(context, intent, uri, number);
Santos Cordon4ebe45d2013-10-08 10:00:35 -0700274
275 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700276 }
277 }
278
279 /**
280 * Launch the SipCallOptionHandler, which is the next step(*) in the
281 * outgoing-call sequence after the outgoing call broadcast is
282 * complete.
283 *
284 * (*) We now know exactly what phone number we need to dial, so the next
285 * step is for the SipCallOptionHandler to decide which Phone type (SIP
286 * or PSTN) should be used. (Depending on the user's preferences, this
287 * decision may also involve popping up a dialog to ask the user to
288 * choose what type of call this should be.)
289 *
290 * @param context used for the startActivity() call
291 *
292 * @param intent the intent from the previous step of the outgoing-call
293 * sequence. Normally this will be the NEW_OUTGOING_CALL broadcast intent
294 * that came in to the OutgoingCallReceiver, although it can also be the
295 * original ACTION_CALL intent that started the whole sequence (in cases
296 * where we don't do the NEW_OUTGOING_CALL broadcast at all, like for
297 * emergency numbers or SIP addresses).
298 *
299 * @param uri the data URI from the original CALL intent, presumably either
300 * a tel: or sip: URI. For tel: URIs, note that the scheme-specific part
301 * does *not* necessarily have separators and keypad letters stripped (so
302 * we might see URIs like "tel:(650)%20555-1234" or "tel:1-800-GOOG-411"
303 * here.)
304 *
305 * @param number the actual number (or SIP address) to dial. This is
306 * guaranteed to be either a PSTN phone number with separators stripped
307 * out and keypad letters converted to digits (like "16505551234"), or a
308 * raw SIP address (like "user@example.com").
309 */
310 private void startSipCallOptionHandler(Context context, Intent intent,
311 Uri uri, String number) {
Santos Cordonda120f42014-08-06 04:44:34 -0700312 // TODO: Remove this code.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700313 }
314
315 /**
316 * This method is the single point of entry for the CALL intent, which is used (by built-in
317 * apps like Contacts / Dialer, as well as 3rd-party apps) to initiate an outgoing voice call.
318 *
319 *
320 */
321 @Override
322 protected void onCreate(Bundle icicle) {
323 super.onCreate(icicle);
324 setContentView(R.layout.outgoing_call_broadcaster);
325 mWaitingSpinner = (ProgressBar) findViewById(R.id.spinner);
326
327 Intent intent = getIntent();
328 if (DBG) {
329 final Configuration configuration = getResources().getConfiguration();
330 Log.v(TAG, "onCreate: this = " + this + ", icicle = " + icicle);
331 Log.v(TAG, " - getIntent() = " + intent);
332 Log.v(TAG, " - configuration = " + configuration);
333 }
334
335 if (icicle != null) {
336 // A non-null icicle means that this activity is being
337 // re-initialized after previously being shut down.
338 //
339 // In practice this happens very rarely (because the lifetime
340 // of this activity is so short!), but it *can* happen if the
341 // framework detects a configuration change at exactly the
342 // right moment; see bug 2202413.
343 //
344 // In this case, do nothing. Our onCreate() method has already
345 // run once (with icicle==null the first time), which means
346 // that the NEW_OUTGOING_CALL broadcast for this new call has
347 // already been sent.
348 Log.i(TAG, "onCreate: non-null icicle! "
349 + "Bailing out, not sending NEW_OUTGOING_CALL broadcast...");
350
351 // No need to finish() here, since the OutgoingCallReceiver from
352 // our original instance will do that. (It'll actually call
353 // finish() on our original instance, which apparently works fine
354 // even though the ActivityManager has already shut that instance
355 // down. And note that if we *do* call finish() here, that just
356 // results in an "ActivityManager: Duplicate finish request"
357 // warning when the OutgoingCallReceiver runs.)
358
359 return;
360 }
361
362 processIntent(intent);
363
364 // isFinishing() return false when 1. broadcast is still ongoing, or 2. dialog is being
365 // shown. Otherwise finish() is called inside processIntent(), is isFinishing() here will
366 // return true.
367 if (DBG) Log.v(TAG, "At the end of onCreate(). isFinishing(): " + isFinishing());
368 }
369
370 /**
371 * Interprets a given Intent and starts something relevant to the Intent.
372 *
373 * This method will handle three kinds of actions:
374 *
375 * - CALL (action for usual outgoing voice calls)
376 * - CALL_PRIVILEGED (can come from built-in apps like contacts / voice dialer / bluetooth)
377 * - CALL_EMERGENCY (from the EmergencyDialer that's reachable from the lockscreen.)
378 *
379 * The exact behavior depends on the intent's data:
380 *
381 * - The most typical is a tel: URI, which we handle by starting the
382 * NEW_OUTGOING_CALL broadcast. That broadcast eventually triggers
383 * the sequence OutgoingCallReceiver -> SipCallOptionHandler ->
384 * InCallScreen.
385 *
386 * - Or, with a sip: URI we skip the NEW_OUTGOING_CALL broadcast and
387 * go directly to SipCallOptionHandler, which then leads to the
388 * InCallScreen.
389 *
390 * - voicemail: URIs take the same path as regular tel: URIs.
391 *
392 * Other special cases:
393 *
394 * - Outgoing calls are totally disallowed on non-voice-capable
395 * devices (see handleNonVoiceCapable()).
396 *
397 * - A CALL intent with the EXTRA_SEND_EMPTY_FLASH extra (and
398 * presumably no data at all) means "send an empty flash" (which
399 * is only meaningful on CDMA devices while a call is already
400 * active.)
401 *
402 */
403 private void processIntent(Intent intent) {
404 if (DBG) {
405 Log.v(TAG, "processIntent() = " + intent + ", thread: " + Thread.currentThread());
406 }
407 final Configuration configuration = getResources().getConfiguration();
408
409 // Outgoing phone calls are only allowed on "voice-capable" devices.
410 if (!PhoneGlobals.sVoiceCapable) {
411 Log.i(TAG, "This device is detected as non-voice-capable device.");
412 handleNonVoiceCapable(intent);
413 return;
414 }
415
416 String action = intent.getAction();
417 String number = PhoneNumberUtils.getNumberFromIntent(intent, this);
418 // Check the number, don't convert for sip uri
419 // TODO put uriNumber under PhoneNumberUtils
420 if (number != null) {
421 if (!PhoneNumberUtils.isUriNumber(number)) {
422 number = PhoneNumberUtils.convertKeypadLettersToDigits(number);
423 number = PhoneNumberUtils.stripSeparators(number);
424 }
425 } else {
426 Log.w(TAG, "The number obtained from Intent is null.");
427 }
428
429 AppOpsManager appOps = (AppOpsManager)getSystemService(Context.APP_OPS_SERVICE);
430 int launchedFromUid;
431 String launchedFromPackage;
432 try {
433 launchedFromUid = ActivityManagerNative.getDefault().getLaunchedFromUid(
434 getActivityToken());
435 launchedFromPackage = ActivityManagerNative.getDefault().getLaunchedFromPackage(
436 getActivityToken());
437 } catch (RemoteException e) {
438 launchedFromUid = -1;
439 launchedFromPackage = null;
440 }
Dianne Hackborn829b26e2014-03-12 11:17:05 -0700441 if (appOps.noteOpNoThrow(AppOpsManager.OP_CALL_PHONE, launchedFromUid, launchedFromPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700442 != AppOpsManager.MODE_ALLOWED) {
443 Log.w(TAG, "Rejecting call from uid " + launchedFromUid + " package "
444 + launchedFromPackage);
445 finish();
446 return;
447 }
448
449 // If true, this flag will indicate that the current call is a special kind
450 // of call (most likely an emergency number) that 3rd parties aren't allowed
451 // to intercept or affect in any way. (In that case, we start the call
452 // immediately rather than going through the NEW_OUTGOING_CALL sequence.)
453 boolean callNow;
454
455 if (getClass().getName().equals(intent.getComponent().getClassName())) {
456 // If we were launched directly from the OutgoingCallBroadcaster,
457 // not one of its more privileged aliases, then make sure that
458 // only the non-privileged actions are allowed.
459 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
460 Log.w(TAG, "Attempt to deliver non-CALL action; forcing to CALL");
461 intent.setAction(Intent.ACTION_CALL);
462 }
463 }
464
465 // Check whether or not this is an emergency number, in order to
466 // enforce the restriction that only the CALL_PRIVILEGED and
467 // CALL_EMERGENCY intents are allowed to make emergency calls.
468 //
469 // (Note that the ACTION_CALL check below depends on the result of
470 // isPotentialLocalEmergencyNumber() rather than just plain
471 // isLocalEmergencyNumber(), to be 100% certain that we *don't*
472 // allow 3rd party apps to make emergency calls by passing in an
473 // "invalid" number like "9111234" that isn't technically an
474 // emergency number but might still result in an emergency call
475 // with some networks.)
476 final boolean isExactEmergencyNumber =
Yorke Lee36bb2542014-06-05 08:09:52 -0700477 (number != null) && PhoneNumberUtils.isLocalEmergencyNumber(this, number);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700478 final boolean isPotentialEmergencyNumber =
Yorke Lee36bb2542014-06-05 08:09:52 -0700479 (number != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(this, number);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700480 if (VDBG) {
481 Log.v(TAG, " - Checking restrictions for number '" + number + "':");
482 Log.v(TAG, " isExactEmergencyNumber = " + isExactEmergencyNumber);
483 Log.v(TAG, " isPotentialEmergencyNumber = " + isPotentialEmergencyNumber);
484 }
485
486 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
487 // TODO: This code is redundant with some code in InCallScreen: refactor.
488 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
489 // We're handling a CALL_PRIVILEGED intent, so we know this request came
490 // from a trusted source (like the built-in dialer.) So even a number
491 // that's *potentially* an emergency number can safely be promoted to
492 // CALL_EMERGENCY (since we *should* allow you to dial "91112345" from
493 // the dialer if you really want to.)
494 if (isPotentialEmergencyNumber) {
495 Log.i(TAG, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
496 + " emergency number. Use ACTION_CALL_EMERGENCY as an action instead.");
497 action = Intent.ACTION_CALL_EMERGENCY;
498 } else {
499 action = Intent.ACTION_CALL;
500 }
501 if (DBG) Log.v(TAG, " - updating action from CALL_PRIVILEGED to " + action);
502 intent.setAction(action);
503 }
504
505 if (Intent.ACTION_CALL.equals(action)) {
506 if (isPotentialEmergencyNumber) {
507 Log.w(TAG, "Cannot call potential emergency number '" + number
508 + "' with CALL Intent " + intent + ".");
509 Log.i(TAG, "Launching default dialer instead...");
510
511 Intent invokeFrameworkDialer = new Intent();
512
513 // TwelveKeyDialer is in a tab so we really want
514 // DialtactsActivity. Build the intent 'manually' to
515 // use the java resolver to find the dialer class (as
516 // opposed to a Context which look up known android
517 // packages only)
Yorke Leed3105fe2013-09-25 12:44:45 -0700518 final Resources resources = getResources();
519 invokeFrameworkDialer.setClassName(
520 resources.getString(R.string.ui_default_package),
521 resources.getString(R.string.dialer_default_class));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700522 invokeFrameworkDialer.setAction(Intent.ACTION_DIAL);
523 invokeFrameworkDialer.setData(intent.getData());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700524 if (DBG) Log.v(TAG, "onCreate(): calling startActivity for Dialer: "
525 + invokeFrameworkDialer);
526 startActivity(invokeFrameworkDialer);
527 finish();
528 return;
529 }
530 callNow = false;
531 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
532 // ACTION_CALL_EMERGENCY case: this is either a CALL_PRIVILEGED
533 // intent that we just turned into a CALL_EMERGENCY intent (see
534 // above), or else it really is an CALL_EMERGENCY intent that
535 // came directly from some other app (e.g. the EmergencyDialer
536 // activity built in to the Phone app.)
537 // Make sure it's at least *possible* that this is really an
538 // emergency number.
539 if (!isPotentialEmergencyNumber) {
540 Log.w(TAG, "Cannot call non-potential-emergency number " + number
541 + " with EMERGENCY_CALL Intent " + intent + "."
542 + " Finish the Activity immediately.");
543 finish();
544 return;
545 }
546 callNow = true;
547 } else {
548 Log.e(TAG, "Unhandled Intent " + intent + ". Finish the Activity immediately.");
549 finish();
550 return;
551 }
552
553 // Make sure the screen is turned on. This is probably the right
554 // thing to do, and more importantly it works around an issue in the
555 // activity manager where we will not launch activities consistently
556 // when the screen is off (since it is trying to keep them paused
557 // and has... issues).
558 //
559 // Also, this ensures the device stays awake while doing the following
560 // broadcast; technically we should be holding a wake lock here
561 // as well.
562 PhoneGlobals.getInstance().wakeUpScreen();
563
564 // If number is null, we're probably trying to call a non-existent voicemail number,
565 // send an empty flash or something else is fishy. Whatever the problem, there's no
566 // number, so there's no point in allowing apps to modify the number.
567 if (TextUtils.isEmpty(number)) {
568 if (intent.getBooleanExtra(EXTRA_SEND_EMPTY_FLASH, false)) {
569 Log.i(TAG, "onCreate: SEND_EMPTY_FLASH...");
570 PhoneUtils.sendEmptyFlash(PhoneGlobals.getPhone());
571 finish();
572 return;
573 } else {
574 Log.i(TAG, "onCreate: null or empty number, setting callNow=true...");
575 callNow = true;
576 }
577 }
578
579 if (callNow) {
580 // This is a special kind of call (most likely an emergency number)
581 // that 3rd parties aren't allowed to intercept or affect in any way.
582 // So initiate the outgoing call immediately.
583
584 Log.i(TAG, "onCreate(): callNow case! Calling placeCall(): " + intent);
585
586 // Initiate the outgoing call, and simultaneously launch the
587 // InCallScreen to display the in-call UI:
588 PhoneGlobals.getInstance().callController.placeCall(intent);
589
590 // Note we do *not* "return" here, but instead continue and
591 // send the ACTION_NEW_OUTGOING_CALL broadcast like for any
592 // other outgoing call. (But when the broadcast finally
593 // reaches the OutgoingCallReceiver, we'll know not to
594 // initiate the call again because of the presence of the
595 // EXTRA_ALREADY_CALLED extra.)
596 }
597
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700598 // For now, SIP calls will be processed directly without a
599 // NEW_OUTGOING_CALL broadcast.
600 //
601 // TODO: In the future, though, 3rd party apps *should* be allowed to
602 // intercept outgoing calls to SIP addresses as well. To do this, we should
603 // (1) update the NEW_OUTGOING_CALL intent documentation to explain this
604 // case, and (2) pass the outgoing SIP address by *not* overloading the
605 // EXTRA_PHONE_NUMBER extra, but instead using a new separate extra to hold
606 // the outgoing SIP address. (Be sure to document whether it's a URI or just
607 // a plain address, whether it could be a tel: URI, etc.)
608 Uri uri = intent.getData();
609 String scheme = uri.getScheme();
610 if (Constants.SCHEME_SIP.equals(scheme) || PhoneNumberUtils.isUriNumber(number)) {
611 Log.i(TAG, "The requested number was detected as SIP call.");
612 startSipCallOptionHandler(this, intent, uri, number);
613 finish();
614 return;
615
616 // TODO: if there's ever a way for SIP calls to trigger a
617 // "callNow=true" case (see above), we'll need to handle that
618 // case here too (most likely by just doing nothing at all.)
619 }
620
621 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
622 if (number != null) {
623 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
624 }
Santos Cordon69a69192013-08-22 14:25:42 -0700625 CallGatewayManager.checkAndCopyPhoneProviderExtras(intent, broadcastIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700626 broadcastIntent.putExtra(EXTRA_ALREADY_CALLED, callNow);
627 broadcastIntent.putExtra(EXTRA_ORIGINAL_URI, uri.toString());
628 // Need to raise foreground in-call UI as soon as possible while allowing 3rd party app
629 // to intercept the outgoing call.
630 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
631 if (DBG) Log.v(TAG, " - Broadcasting intent: " + broadcastIntent + ".");
632
633 // Set a timer so that we can prepare for unexpected delay introduced by the broadcast.
634 // If it takes too much time, the timer will show "waiting" spinner.
635 // This message will be removed when OutgoingCallReceiver#onReceive() is called before the
636 // timeout.
637 mHandler.sendEmptyMessageDelayed(EVENT_OUTGOING_CALL_TIMEOUT,
638 OUTGOING_CALL_TIMEOUT_THRESHOLD);
639 sendOrderedBroadcastAsUser(broadcastIntent, UserHandle.OWNER,
640 PERMISSION, new OutgoingCallReceiver(),
641 null, // scheduler
642 Activity.RESULT_OK, // initialCode
643 number, // initialData: initial value for the result data
644 null); // initialExtras
645 }
646
647 @Override
648 protected void onStop() {
649 // Clean up (and dismiss if necessary) any managed dialogs.
650 //
651 // We don't do this in onPause() since we can be paused/resumed
652 // due to orientation changes (in which case we don't want to
653 // disturb the dialog), but we *do* need it here in onStop() to be
654 // sure we clean up if the user hits HOME while the dialog is up.
655 //
656 // Note it's safe to call removeDialog() even if there's no dialog
657 // associated with that ID.
658 removeDialog(DIALOG_NOT_VOICE_CAPABLE);
659
660 super.onStop();
661 }
662
663 /**
664 * Handle the specified CALL or CALL_* intent on a non-voice-capable
665 * device.
666 *
667 * This method may launch a different intent (if there's some useful
668 * alternative action to take), or otherwise display an error dialog,
669 * and in either case will finish() the current activity when done.
670 */
671 private void handleNonVoiceCapable(Intent intent) {
672 if (DBG) Log.v(TAG, "handleNonVoiceCapable: handling " + intent
673 + " on non-voice-capable device...");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700674
Chiao Cheng88653e72013-10-04 14:30:14 -0700675 // Just show a generic "voice calling not supported" dialog.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700676 showDialog(DIALOG_NOT_VOICE_CAPABLE);
677 // ...and we'll eventually finish() when the user dismisses
678 // or cancels the dialog.
679 }
680
681 @Override
682 protected Dialog onCreateDialog(int id) {
683 Dialog dialog;
684 switch(id) {
685 case DIALOG_NOT_VOICE_CAPABLE:
686 dialog = new AlertDialog.Builder(this)
687 .setTitle(R.string.not_voice_capable)
688 .setIconAttribute(android.R.attr.alertDialogIcon)
689 .setPositiveButton(android.R.string.ok, this)
690 .setOnCancelListener(this)
691 .create();
692 break;
693 default:
694 Log.w(TAG, "onCreateDialog: unexpected ID " + id);
695 dialog = null;
696 break;
697 }
698 return dialog;
699 }
700
701 /** DialogInterface.OnClickListener implementation */
702 @Override
703 public void onClick(DialogInterface dialog, int id) {
704 // DIALOG_NOT_VOICE_CAPABLE is the only dialog we ever use (so far
705 // at least), and its only button is "OK".
706 finish();
707 }
708
709 /** DialogInterface.OnCancelListener implementation */
710 @Override
711 public void onCancel(DialogInterface dialog) {
712 // DIALOG_NOT_VOICE_CAPABLE is the only dialog we ever use (so far
713 // at least), and canceling it is just like hitting "OK".
714 finish();
715 }
716
717 /**
718 * Implement onConfigurationChanged() purely for debugging purposes,
719 * to make sure that the android:configChanges element in our manifest
720 * is working properly.
721 */
722 @Override
723 public void onConfigurationChanged(Configuration newConfig) {
724 super.onConfigurationChanged(newConfig);
725 if (DBG) Log.v(TAG, "onConfigurationChanged: newConfig = " + newConfig);
726 }
727}