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