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