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