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