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