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