blob: a4f7178950c94e89f2fe708185a6216bd8bd5f2e [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();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700206 } else if (isOtaCallActive) {
207 // The actual OTASP call is active. Don't allow new
208 // outgoing calls at all from this state.
209 Log.w(TAG, "OTASP call is active: disallowing a new outgoing call.");
210 return;
211 }
212 }
213
214 if (number == null) {
215 if (DBG) Log.v(TAG, "CALL cancelled (null number), returning...");
216 return;
217 } else if (TelephonyCapabilities.supportsOtasp(app.phone)
218 && (app.phone.getState() != PhoneConstants.State.IDLE)
219 && (app.phone.isOtaSpNumber(number))) {
220 if (DBG) Log.v(TAG, "Call is active, a 2nd OTA call cancelled -- returning.");
221 return;
222 } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(number, context)) {
223 // Just like 3rd-party apps aren't allowed to place emergency
224 // calls via the ACTION_CALL intent, we also don't allow 3rd
225 // party apps to use the NEW_OUTGOING_CALL broadcast to rewrite
226 // an outgoing call into an emergency number.
227 Log.w(TAG, "Cannot modify outgoing call to emergency number " + number + ".");
228 return;
229 }
230
231 originalUri = intent.getStringExtra(
232 OutgoingCallBroadcaster.EXTRA_ORIGINAL_URI);
233 if (originalUri == null) {
234 Log.e(TAG, "Intent is missing EXTRA_ORIGINAL_URI -- returning.");
235 return;
236 }
237
238 Uri uri = Uri.parse(originalUri);
239
240 // We already called convertKeypadLettersToDigits() and
241 // stripSeparators() way back in onCreate(), before we sent out the
242 // NEW_OUTGOING_CALL broadcast. But we need to do it again here
243 // too, since the number might have been modified/rewritten during
244 // the broadcast (and may now contain letters or separators again.)
245 number = PhoneNumberUtils.convertKeypadLettersToDigits(number);
246 number = PhoneNumberUtils.stripSeparators(number);
247
248 if (DBG) Log.v(TAG, "doReceive: proceeding with call...");
249 if (VDBG) Log.v(TAG, "- uri: " + uri);
250 if (VDBG) Log.v(TAG, "- actual number to dial: '" + number + "'");
251
252 startSipCallOptionHandler(context, intent, uri, number);
253 }
254 }
255
256 /**
257 * Launch the SipCallOptionHandler, which is the next step(*) in the
258 * outgoing-call sequence after the outgoing call broadcast is
259 * complete.
260 *
261 * (*) We now know exactly what phone number we need to dial, so the next
262 * step is for the SipCallOptionHandler to decide which Phone type (SIP
263 * or PSTN) should be used. (Depending on the user's preferences, this
264 * decision may also involve popping up a dialog to ask the user to
265 * choose what type of call this should be.)
266 *
267 * @param context used for the startActivity() call
268 *
269 * @param intent the intent from the previous step of the outgoing-call
270 * sequence. Normally this will be the NEW_OUTGOING_CALL broadcast intent
271 * that came in to the OutgoingCallReceiver, although it can also be the
272 * original ACTION_CALL intent that started the whole sequence (in cases
273 * where we don't do the NEW_OUTGOING_CALL broadcast at all, like for
274 * emergency numbers or SIP addresses).
275 *
276 * @param uri the data URI from the original CALL intent, presumably either
277 * a tel: or sip: URI. For tel: URIs, note that the scheme-specific part
278 * does *not* necessarily have separators and keypad letters stripped (so
279 * we might see URIs like "tel:(650)%20555-1234" or "tel:1-800-GOOG-411"
280 * here.)
281 *
282 * @param number the actual number (or SIP address) to dial. This is
283 * guaranteed to be either a PSTN phone number with separators stripped
284 * out and keypad letters converted to digits (like "16505551234"), or a
285 * raw SIP address (like "user@example.com").
286 */
287 private void startSipCallOptionHandler(Context context, Intent intent,
288 Uri uri, String number) {
289 if (VDBG) {
290 Log.i(TAG, "startSipCallOptionHandler...");
291 Log.i(TAG, "- intent: " + intent);
292 Log.i(TAG, "- uri: " + uri);
293 Log.i(TAG, "- number: " + number);
294 }
295
296 // Create a copy of the original CALL intent that started the whole
297 // outgoing-call sequence. This intent will ultimately be passed to
298 // CallController.placeCall() after the SipCallOptionHandler step.
299
300 Intent newIntent = new Intent(Intent.ACTION_CALL, uri);
301 newIntent.putExtra(EXTRA_ACTUAL_NUMBER_TO_DIAL, number);
Santos Cordon69a69192013-08-22 14:25:42 -0700302 CallGatewayManager.checkAndCopyPhoneProviderExtras(intent, newIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303
304 // Finally, launch the SipCallOptionHandler, with the copy of the
305 // original CALL intent stashed away in the EXTRA_NEW_CALL_INTENT
306 // extra.
307
308 Intent selectPhoneIntent = new Intent(ACTION_SIP_SELECT_PHONE, uri);
309 selectPhoneIntent.setClass(context, SipCallOptionHandler.class);
310 selectPhoneIntent.putExtra(EXTRA_NEW_CALL_INTENT, newIntent);
311 selectPhoneIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
312 if (DBG) {
313 Log.v(TAG, "startSipCallOptionHandler(): " +
314 "calling startActivity: " + selectPhoneIntent);
315 }
316 context.startActivity(selectPhoneIntent);
317 // ...and see SipCallOptionHandler.onCreate() for the next step of the sequence.
318 }
319
320 /**
321 * This method is the single point of entry for the CALL intent, which is used (by built-in
322 * apps like Contacts / Dialer, as well as 3rd-party apps) to initiate an outgoing voice call.
323 *
324 *
325 */
326 @Override
327 protected void onCreate(Bundle icicle) {
328 super.onCreate(icicle);
329 setContentView(R.layout.outgoing_call_broadcaster);
330 mWaitingSpinner = (ProgressBar) findViewById(R.id.spinner);
331
332 Intent intent = getIntent();
333 if (DBG) {
334 final Configuration configuration = getResources().getConfiguration();
335 Log.v(TAG, "onCreate: this = " + this + ", icicle = " + icicle);
336 Log.v(TAG, " - getIntent() = " + intent);
337 Log.v(TAG, " - configuration = " + configuration);
338 }
339
340 if (icicle != null) {
341 // A non-null icicle means that this activity is being
342 // re-initialized after previously being shut down.
343 //
344 // In practice this happens very rarely (because the lifetime
345 // of this activity is so short!), but it *can* happen if the
346 // framework detects a configuration change at exactly the
347 // right moment; see bug 2202413.
348 //
349 // In this case, do nothing. Our onCreate() method has already
350 // run once (with icicle==null the first time), which means
351 // that the NEW_OUTGOING_CALL broadcast for this new call has
352 // already been sent.
353 Log.i(TAG, "onCreate: non-null icicle! "
354 + "Bailing out, not sending NEW_OUTGOING_CALL broadcast...");
355
356 // No need to finish() here, since the OutgoingCallReceiver from
357 // our original instance will do that. (It'll actually call
358 // finish() on our original instance, which apparently works fine
359 // even though the ActivityManager has already shut that instance
360 // down. And note that if we *do* call finish() here, that just
361 // results in an "ActivityManager: Duplicate finish request"
362 // warning when the OutgoingCallReceiver runs.)
363
364 return;
365 }
366
367 processIntent(intent);
368
369 // isFinishing() return false when 1. broadcast is still ongoing, or 2. dialog is being
370 // shown. Otherwise finish() is called inside processIntent(), is isFinishing() here will
371 // return true.
372 if (DBG) Log.v(TAG, "At the end of onCreate(). isFinishing(): " + isFinishing());
373 }
374
375 /**
376 * Interprets a given Intent and starts something relevant to the Intent.
377 *
378 * This method will handle three kinds of actions:
379 *
380 * - CALL (action for usual outgoing voice calls)
381 * - CALL_PRIVILEGED (can come from built-in apps like contacts / voice dialer / bluetooth)
382 * - CALL_EMERGENCY (from the EmergencyDialer that's reachable from the lockscreen.)
383 *
384 * The exact behavior depends on the intent's data:
385 *
386 * - The most typical is a tel: URI, which we handle by starting the
387 * NEW_OUTGOING_CALL broadcast. That broadcast eventually triggers
388 * the sequence OutgoingCallReceiver -> SipCallOptionHandler ->
389 * InCallScreen.
390 *
391 * - Or, with a sip: URI we skip the NEW_OUTGOING_CALL broadcast and
392 * go directly to SipCallOptionHandler, which then leads to the
393 * InCallScreen.
394 *
395 * - voicemail: URIs take the same path as regular tel: URIs.
396 *
397 * Other special cases:
398 *
399 * - Outgoing calls are totally disallowed on non-voice-capable
400 * devices (see handleNonVoiceCapable()).
401 *
402 * - A CALL intent with the EXTRA_SEND_EMPTY_FLASH extra (and
403 * presumably no data at all) means "send an empty flash" (which
404 * is only meaningful on CDMA devices while a call is already
405 * active.)
406 *
407 */
408 private void processIntent(Intent intent) {
409 if (DBG) {
410 Log.v(TAG, "processIntent() = " + intent + ", thread: " + Thread.currentThread());
411 }
412 final Configuration configuration = getResources().getConfiguration();
413
414 // Outgoing phone calls are only allowed on "voice-capable" devices.
415 if (!PhoneGlobals.sVoiceCapable) {
416 Log.i(TAG, "This device is detected as non-voice-capable device.");
417 handleNonVoiceCapable(intent);
418 return;
419 }
420
421 String action = intent.getAction();
422 String number = PhoneNumberUtils.getNumberFromIntent(intent, this);
423 // Check the number, don't convert for sip uri
424 // TODO put uriNumber under PhoneNumberUtils
425 if (number != null) {
426 if (!PhoneNumberUtils.isUriNumber(number)) {
427 number = PhoneNumberUtils.convertKeypadLettersToDigits(number);
428 number = PhoneNumberUtils.stripSeparators(number);
429 }
430 } else {
431 Log.w(TAG, "The number obtained from Intent is null.");
432 }
433
434 AppOpsManager appOps = (AppOpsManager)getSystemService(Context.APP_OPS_SERVICE);
435 int launchedFromUid;
436 String launchedFromPackage;
437 try {
438 launchedFromUid = ActivityManagerNative.getDefault().getLaunchedFromUid(
439 getActivityToken());
440 launchedFromPackage = ActivityManagerNative.getDefault().getLaunchedFromPackage(
441 getActivityToken());
442 } catch (RemoteException e) {
443 launchedFromUid = -1;
444 launchedFromPackage = null;
445 }
446 if (appOps.noteOp(AppOpsManager.OP_CALL_PHONE, launchedFromUid, launchedFromPackage)
447 != AppOpsManager.MODE_ALLOWED) {
448 Log.w(TAG, "Rejecting call from uid " + launchedFromUid + " package "
449 + launchedFromPackage);
450 finish();
451 return;
452 }
453
454 // If true, this flag will indicate that the current call is a special kind
455 // of call (most likely an emergency number) that 3rd parties aren't allowed
456 // to intercept or affect in any way. (In that case, we start the call
457 // immediately rather than going through the NEW_OUTGOING_CALL sequence.)
458 boolean callNow;
459
460 if (getClass().getName().equals(intent.getComponent().getClassName())) {
461 // If we were launched directly from the OutgoingCallBroadcaster,
462 // not one of its more privileged aliases, then make sure that
463 // only the non-privileged actions are allowed.
464 if (!Intent.ACTION_CALL.equals(intent.getAction())) {
465 Log.w(TAG, "Attempt to deliver non-CALL action; forcing to CALL");
466 intent.setAction(Intent.ACTION_CALL);
467 }
468 }
469
470 // Check whether or not this is an emergency number, in order to
471 // enforce the restriction that only the CALL_PRIVILEGED and
472 // CALL_EMERGENCY intents are allowed to make emergency calls.
473 //
474 // (Note that the ACTION_CALL check below depends on the result of
475 // isPotentialLocalEmergencyNumber() rather than just plain
476 // isLocalEmergencyNumber(), to be 100% certain that we *don't*
477 // allow 3rd party apps to make emergency calls by passing in an
478 // "invalid" number like "9111234" that isn't technically an
479 // emergency number but might still result in an emergency call
480 // with some networks.)
481 final boolean isExactEmergencyNumber =
482 (number != null) && PhoneNumberUtils.isLocalEmergencyNumber(number, this);
483 final boolean isPotentialEmergencyNumber =
484 (number != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(number, this);
485 if (VDBG) {
486 Log.v(TAG, " - Checking restrictions for number '" + number + "':");
487 Log.v(TAG, " isExactEmergencyNumber = " + isExactEmergencyNumber);
488 Log.v(TAG, " isPotentialEmergencyNumber = " + isPotentialEmergencyNumber);
489 }
490
491 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
492 // TODO: This code is redundant with some code in InCallScreen: refactor.
493 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
494 // We're handling a CALL_PRIVILEGED intent, so we know this request came
495 // from a trusted source (like the built-in dialer.) So even a number
496 // that's *potentially* an emergency number can safely be promoted to
497 // CALL_EMERGENCY (since we *should* allow you to dial "91112345" from
498 // the dialer if you really want to.)
499 if (isPotentialEmergencyNumber) {
500 Log.i(TAG, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
501 + " emergency number. Use ACTION_CALL_EMERGENCY as an action instead.");
502 action = Intent.ACTION_CALL_EMERGENCY;
503 } else {
504 action = Intent.ACTION_CALL;
505 }
506 if (DBG) Log.v(TAG, " - updating action from CALL_PRIVILEGED to " + action);
507 intent.setAction(action);
508 }
509
510 if (Intent.ACTION_CALL.equals(action)) {
511 if (isPotentialEmergencyNumber) {
512 Log.w(TAG, "Cannot call potential emergency number '" + number
513 + "' with CALL Intent " + intent + ".");
514 Log.i(TAG, "Launching default dialer instead...");
515
516 Intent invokeFrameworkDialer = new Intent();
517
518 // TwelveKeyDialer is in a tab so we really want
519 // DialtactsActivity. Build the intent 'manually' to
520 // use the java resolver to find the dialer class (as
521 // opposed to a Context which look up known android
522 // packages only)
523 invokeFrameworkDialer.setClassName("com.android.dialer",
524 "com.android.dialer.DialtactsActivity");
525 invokeFrameworkDialer.setAction(Intent.ACTION_DIAL);
526 invokeFrameworkDialer.setData(intent.getData());
527
528 if (DBG) Log.v(TAG, "onCreate(): calling startActivity for Dialer: "
529 + invokeFrameworkDialer);
530 startActivity(invokeFrameworkDialer);
531 finish();
532 return;
533 }
534 callNow = false;
535 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
536 // ACTION_CALL_EMERGENCY case: this is either a CALL_PRIVILEGED
537 // intent that we just turned into a CALL_EMERGENCY intent (see
538 // above), or else it really is an CALL_EMERGENCY intent that
539 // came directly from some other app (e.g. the EmergencyDialer
540 // activity built in to the Phone app.)
541 // Make sure it's at least *possible* that this is really an
542 // emergency number.
543 if (!isPotentialEmergencyNumber) {
544 Log.w(TAG, "Cannot call non-potential-emergency number " + number
545 + " with EMERGENCY_CALL Intent " + intent + "."
546 + " Finish the Activity immediately.");
547 finish();
548 return;
549 }
550 callNow = true;
551 } else {
552 Log.e(TAG, "Unhandled Intent " + intent + ". Finish the Activity immediately.");
553 finish();
554 return;
555 }
556
557 // Make sure the screen is turned on. This is probably the right
558 // thing to do, and more importantly it works around an issue in the
559 // activity manager where we will not launch activities consistently
560 // when the screen is off (since it is trying to keep them paused
561 // and has... issues).
562 //
563 // Also, this ensures the device stays awake while doing the following
564 // broadcast; technically we should be holding a wake lock here
565 // as well.
566 PhoneGlobals.getInstance().wakeUpScreen();
567
568 // If number is null, we're probably trying to call a non-existent voicemail number,
569 // send an empty flash or something else is fishy. Whatever the problem, there's no
570 // number, so there's no point in allowing apps to modify the number.
571 if (TextUtils.isEmpty(number)) {
572 if (intent.getBooleanExtra(EXTRA_SEND_EMPTY_FLASH, false)) {
573 Log.i(TAG, "onCreate: SEND_EMPTY_FLASH...");
574 PhoneUtils.sendEmptyFlash(PhoneGlobals.getPhone());
575 finish();
576 return;
577 } else {
578 Log.i(TAG, "onCreate: null or empty number, setting callNow=true...");
579 callNow = true;
580 }
581 }
582
583 if (callNow) {
584 // This is a special kind of call (most likely an emergency number)
585 // that 3rd parties aren't allowed to intercept or affect in any way.
586 // So initiate the outgoing call immediately.
587
588 Log.i(TAG, "onCreate(): callNow case! Calling placeCall(): " + intent);
589
590 // Initiate the outgoing call, and simultaneously launch the
591 // InCallScreen to display the in-call UI:
592 PhoneGlobals.getInstance().callController.placeCall(intent);
593
594 // Note we do *not* "return" here, but instead continue and
595 // send the ACTION_NEW_OUTGOING_CALL broadcast like for any
596 // other outgoing call. (But when the broadcast finally
597 // reaches the OutgoingCallReceiver, we'll know not to
598 // initiate the call again because of the presence of the
599 // EXTRA_ALREADY_CALLED extra.)
600 }
601
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700602 // For now, SIP calls will be processed directly without a
603 // NEW_OUTGOING_CALL broadcast.
604 //
605 // TODO: In the future, though, 3rd party apps *should* be allowed to
606 // intercept outgoing calls to SIP addresses as well. To do this, we should
607 // (1) update the NEW_OUTGOING_CALL intent documentation to explain this
608 // case, and (2) pass the outgoing SIP address by *not* overloading the
609 // EXTRA_PHONE_NUMBER extra, but instead using a new separate extra to hold
610 // the outgoing SIP address. (Be sure to document whether it's a URI or just
611 // a plain address, whether it could be a tel: URI, etc.)
612 Uri uri = intent.getData();
613 String scheme = uri.getScheme();
614 if (Constants.SCHEME_SIP.equals(scheme) || PhoneNumberUtils.isUriNumber(number)) {
615 Log.i(TAG, "The requested number was detected as SIP call.");
616 startSipCallOptionHandler(this, intent, uri, number);
617 finish();
618 return;
619
620 // TODO: if there's ever a way for SIP calls to trigger a
621 // "callNow=true" case (see above), we'll need to handle that
622 // case here too (most likely by just doing nothing at all.)
623 }
624
625 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
626 if (number != null) {
627 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
628 }
Santos Cordon69a69192013-08-22 14:25:42 -0700629 CallGatewayManager.checkAndCopyPhoneProviderExtras(intent, broadcastIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700630 broadcastIntent.putExtra(EXTRA_ALREADY_CALLED, callNow);
631 broadcastIntent.putExtra(EXTRA_ORIGINAL_URI, uri.toString());
632 // Need to raise foreground in-call UI as soon as possible while allowing 3rd party app
633 // to intercept the outgoing call.
634 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
635 if (DBG) Log.v(TAG, " - Broadcasting intent: " + broadcastIntent + ".");
636
637 // Set a timer so that we can prepare for unexpected delay introduced by the broadcast.
638 // If it takes too much time, the timer will show "waiting" spinner.
639 // This message will be removed when OutgoingCallReceiver#onReceive() is called before the
640 // timeout.
641 mHandler.sendEmptyMessageDelayed(EVENT_OUTGOING_CALL_TIMEOUT,
642 OUTGOING_CALL_TIMEOUT_THRESHOLD);
643 sendOrderedBroadcastAsUser(broadcastIntent, UserHandle.OWNER,
644 PERMISSION, new OutgoingCallReceiver(),
645 null, // scheduler
646 Activity.RESULT_OK, // initialCode
647 number, // initialData: initial value for the result data
648 null); // initialExtras
649 }
650
651 @Override
652 protected void onStop() {
653 // Clean up (and dismiss if necessary) any managed dialogs.
654 //
655 // We don't do this in onPause() since we can be paused/resumed
656 // due to orientation changes (in which case we don't want to
657 // disturb the dialog), but we *do* need it here in onStop() to be
658 // sure we clean up if the user hits HOME while the dialog is up.
659 //
660 // Note it's safe to call removeDialog() even if there's no dialog
661 // associated with that ID.
662 removeDialog(DIALOG_NOT_VOICE_CAPABLE);
663
664 super.onStop();
665 }
666
667 /**
668 * Handle the specified CALL or CALL_* intent on a non-voice-capable
669 * device.
670 *
671 * This method may launch a different intent (if there's some useful
672 * alternative action to take), or otherwise display an error dialog,
673 * and in either case will finish() the current activity when done.
674 */
675 private void handleNonVoiceCapable(Intent intent) {
676 if (DBG) Log.v(TAG, "handleNonVoiceCapable: handling " + intent
677 + " on non-voice-capable device...");
678 String action = intent.getAction();
679 Uri uri = intent.getData();
680 String scheme = uri.getScheme();
681
682 // Handle one special case: If this is a regular CALL to a tel: URI,
683 // bring up a UI letting you do something useful with the phone number
684 // (like "Add to contacts" if it isn't a contact yet.)
685 //
686 // This UI is provided by the contacts app in response to a DIAL
687 // intent, so we bring it up here by demoting this CALL to a DIAL and
688 // relaunching.
689 //
690 // TODO: it's strange and unintuitive to manually launch a DIAL intent
691 // to do this; it would be cleaner to have some shared UI component
692 // that we could bring up directly. (But for now at least, since both
693 // Contacts and Phone are built-in apps, this implementation is fine.)
694
695 if (Intent.ACTION_CALL.equals(action) && (Constants.SCHEME_TEL.equals(scheme))) {
696 Intent newIntent = new Intent(Intent.ACTION_DIAL, uri);
697 if (DBG) Log.v(TAG, "- relaunching as a DIAL intent: " + newIntent);
698 startActivity(newIntent);
699 finish();
700 return;
701 }
702
703 // In all other cases, just show a generic "voice calling not
704 // supported" dialog.
705 showDialog(DIALOG_NOT_VOICE_CAPABLE);
706 // ...and we'll eventually finish() when the user dismisses
707 // or cancels the dialog.
708 }
709
710 @Override
711 protected Dialog onCreateDialog(int id) {
712 Dialog dialog;
713 switch(id) {
714 case DIALOG_NOT_VOICE_CAPABLE:
715 dialog = new AlertDialog.Builder(this)
716 .setTitle(R.string.not_voice_capable)
717 .setIconAttribute(android.R.attr.alertDialogIcon)
718 .setPositiveButton(android.R.string.ok, this)
719 .setOnCancelListener(this)
720 .create();
721 break;
722 default:
723 Log.w(TAG, "onCreateDialog: unexpected ID " + id);
724 dialog = null;
725 break;
726 }
727 return dialog;
728 }
729
730 /** DialogInterface.OnClickListener implementation */
731 @Override
732 public void onClick(DialogInterface dialog, int id) {
733 // DIALOG_NOT_VOICE_CAPABLE is the only dialog we ever use (so far
734 // at least), and its only button is "OK".
735 finish();
736 }
737
738 /** DialogInterface.OnCancelListener implementation */
739 @Override
740 public void onCancel(DialogInterface dialog) {
741 // DIALOG_NOT_VOICE_CAPABLE is the only dialog we ever use (so far
742 // at least), and canceling it is just like hitting "OK".
743 finish();
744 }
745
746 /**
747 * Implement onConfigurationChanged() purely for debugging purposes,
748 * to make sure that the android:configChanges element in our manifest
749 * is working properly.
750 */
751 @Override
752 public void onConfigurationChanged(Configuration newConfig) {
753 super.onConfigurationChanged(newConfig);
754 if (DBG) Log.v(TAG, "onConfigurationChanged: newConfig = " + newConfig);
755 }
756}