Home | History | Annotate | Download | only in phone
      1 /*
      2  * Copyright (C) 2011 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 com.android.internal.telephony.CallManager;
     20 import com.android.internal.telephony.Phone;
     21 import com.android.internal.telephony.TelephonyCapabilities;
     22 import com.android.phone.Constants.CallStatusCode;
     23 import com.android.phone.InCallUiState.InCallScreenMode;
     24 import com.android.phone.OtaUtils.CdmaOtaScreenState;
     25 
     26 import android.content.Intent;
     27 import android.net.Uri;
     28 import android.os.Handler;
     29 import android.os.Message;
     30 import android.os.SystemProperties;
     31 import android.telephony.PhoneNumberUtils;
     32 import android.telephony.ServiceState;
     33 import android.text.TextUtils;
     34 import android.util.Log;
     35 import android.widget.Toast;
     36 
     37 /**
     38  * Phone app module in charge of "call control".
     39  *
     40  * This is a singleton object which acts as the interface to the telephony layer
     41  * (and other parts of the Android framework) for all user-initiated telephony
     42  * functionality, like making outgoing calls.
     43  *
     44  * This functionality includes things like:
     45  *   - actually running the placeCall() method and handling errors or retries
     46  *   - running the whole "emergency call in airplane mode" sequence
     47  *   - running the state machine of MMI sequences
     48  *   - restoring/resetting mute and speaker state when a new call starts
     49  *   - updating the prox sensor wake lock state
     50  *   - resolving what the voicemail: intent should mean (and making the call)
     51  *
     52  * The single CallController instance stays around forever; it's not tied
     53  * to the lifecycle of any particular Activity (like the InCallScreen).
     54  * There's also no implementation of onscreen UI here (that's all in InCallScreen).
     55  *
     56  * Note that this class does not handle asynchronous events from the telephony
     57  * layer, like reacting to an incoming call; see CallNotifier for that.  This
     58  * class purely handles actions initiated by the user, like outgoing calls.
     59  */
     60 public class CallController extends Handler {
     61     private static final String TAG = "CallController";
     62     private static final boolean DBG =
     63             (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
     64     // Do not check in with VDBG = true, since that may write PII to the system log.
     65     private static final boolean VDBG = false;
     66 
     67     /** The singleton CallController instance. */
     68     private static CallController sInstance;
     69 
     70     private PhoneApp mApp;
     71     private CallManager mCM;
     72 
     73     /** Helper object for emergency calls in some rare use cases.  Created lazily. */
     74     private EmergencyCallHelper mEmergencyCallHelper;
     75 
     76 
     77     //
     78     // Message codes; see handleMessage().
     79     //
     80 
     81     private static final int THREEWAY_CALLERINFO_DISPLAY_DONE = 1;
     82 
     83 
     84     //
     85     // Misc constants.
     86     //
     87 
     88     // Amount of time the UI should display "Dialing" when initiating a CDMA
     89     // 3way call.  (See comments on the THRWAY_ACTIVE case in
     90     // placeCallInternal() for more info.)
     91     private static final int THREEWAY_CALLERINFO_DISPLAY_TIME = 3000; // msec
     92 
     93 
     94     /**
     95      * Initialize the singleton CallController instance.
     96      *
     97      * This is only done once, at startup, from PhoneApp.onCreate().
     98      * From then on, the CallController instance is available via the
     99      * PhoneApp's public "callController" field, which is why there's no
    100      * getInstance() method here.
    101      */
    102     /* package */ static CallController init(PhoneApp app) {
    103         synchronized (CallController.class) {
    104             if (sInstance == null) {
    105                 sInstance = new CallController(app);
    106             } else {
    107                 Log.wtf(TAG, "init() called multiple times!  sInstance = " + sInstance);
    108             }
    109             return sInstance;
    110         }
    111     }
    112 
    113     /**
    114      * Private constructor (this is a singleton).
    115      * @see init()
    116      */
    117     private CallController(PhoneApp app) {
    118         if (DBG) log("CallController constructor: app = " + app);
    119         mApp = app;
    120         mCM = app.mCM;
    121     }
    122 
    123     @Override
    124     public void handleMessage(Message msg) {
    125         if (VDBG) log("handleMessage: " + msg);
    126         switch (msg.what) {
    127 
    128             case THREEWAY_CALLERINFO_DISPLAY_DONE:
    129                 if (DBG) log("THREEWAY_CALLERINFO_DISPLAY_DONE...");
    130 
    131                 if (mApp.cdmaPhoneCallState.getCurrentCallState()
    132                     == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) {
    133                     // Reset the mThreeWayCallOrigStateDialing state
    134                     mApp.cdmaPhoneCallState.setThreeWayCallOrigState(false);
    135 
    136                     // Refresh the in-call UI (based on the current ongoing call)
    137                     mApp.updateInCallScreen();
    138                 }
    139                 break;
    140 
    141             default:
    142                 Log.wtf(TAG, "handleMessage: unexpected code: " + msg);
    143                 break;
    144         }
    145     }
    146 
    147     //
    148     // Outgoing call sequence
    149     //
    150 
    151     /**
    152      * Initiate an outgoing call.
    153      *
    154      * Here's the most typical outgoing call sequence:
    155      *
    156      *  (1) OutgoingCallBroadcaster receives a CALL intent and sends the
    157      *      NEW_OUTGOING_CALL broadcast
    158      *
    159      *  (2) The broadcast finally reaches OutgoingCallReceiver, which stashes
    160      *      away a copy of the original CALL intent and launches
    161      *      SipCallOptionHandler
    162      *
    163      *  (3) SipCallOptionHandler decides whether this is a PSTN or SIP call (and
    164      *      in some cases brings up a dialog to let the user choose), and
    165      *      ultimately calls CallController.placeCall() (from the
    166      *      setResultAndFinish() method) with the stashed-away intent from step
    167      *      (2) as the "intent" parameter.
    168      *
    169      *  (4) Here in CallController.placeCall() we read the phone number or SIP
    170      *      address out of the intent and actually initiate the call, and
    171      *      simultaneously launch the InCallScreen to display the in-call UI.
    172      *
    173      *  (5) We handle various errors by directing the InCallScreen to
    174      *      display error messages or dialogs (via the InCallUiState
    175      *      "pending call status code" flag), and in some cases we also
    176      *      sometimes continue working in the background to resolve the
    177      *      problem (like in the case of an emergency call while in
    178      *      airplane mode).  Any time that some onscreen indication to the
    179      *      user needs to change, we update the "status dialog" info in
    180      *      the inCallUiState and (re)launch the InCallScreen to make sure
    181      *      it's visible.
    182      */
    183     public void placeCall(Intent intent) {
    184         log("placeCall()...  intent = " + intent);
    185         if (VDBG) log("                extras = " + intent.getExtras());
    186 
    187         final InCallUiState inCallUiState = mApp.inCallUiState;
    188 
    189         // TODO: Do we need to hold a wake lock while this method runs?
    190         //       Or did we already acquire one somewhere earlier
    191         //       in this sequence (like when we first received the CALL intent?)
    192 
    193         if (intent == null) {
    194             Log.wtf(TAG, "placeCall: called with null intent");
    195             throw new IllegalArgumentException("placeCall: called with null intent");
    196         }
    197 
    198         String action = intent.getAction();
    199         Uri uri = intent.getData();
    200         if (uri == null) {
    201             Log.wtf(TAG, "placeCall: intent had no data");
    202             throw new IllegalArgumentException("placeCall: intent had no data");
    203         }
    204 
    205         String scheme = uri.getScheme();
    206         String number = PhoneNumberUtils.getNumberFromIntent(intent, mApp);
    207         if (VDBG) {
    208             log("- action: " + action);
    209             log("- uri: " + uri);
    210             log("- scheme: " + scheme);
    211             log("- number: " + number);
    212         }
    213 
    214         // This method should only be used with the various flavors of CALL
    215         // intents.  (It doesn't make sense for any other action to trigger an
    216         // outgoing call!)
    217         if (!(Intent.ACTION_CALL.equals(action)
    218               || Intent.ACTION_CALL_EMERGENCY.equals(action)
    219               || Intent.ACTION_CALL_PRIVILEGED.equals(action))) {
    220             Log.wtf(TAG, "placeCall: unexpected intent action " + action);
    221             throw new IllegalArgumentException("Unexpected action: " + action);
    222         }
    223 
    224         // Check to see if this is an OTASP call (the "activation" call
    225         // used to provision CDMA devices), and if so, do some
    226         // OTASP-specific setup.
    227         Phone phone = mApp.mCM.getDefaultPhone();
    228         if (TelephonyCapabilities.supportsOtasp(phone)) {
    229             checkForOtaspCall(intent);
    230         }
    231 
    232         // Clear out the "restore mute state" flag since we're
    233         // initiating a brand-new call.
    234         //
    235         // (This call to setRestoreMuteOnInCallResume(false) informs the
    236         // phone app that we're dealing with a new connection
    237         // (i.e. placing an outgoing call, and NOT handling an aborted
    238         // "Add Call" request), so we should let the mute state be handled
    239         // by the PhoneUtils phone state change handler.)
    240         mApp.setRestoreMuteOnInCallResume(false);
    241 
    242         // If a provider is used, extract the info to build the
    243         // overlay and route the call.  The overlay will be
    244         // displayed when the InCallScreen becomes visible.
    245         if (PhoneUtils.hasPhoneProviderExtras(intent)) {
    246             inCallUiState.setProviderInfo(intent);
    247         } else {
    248             inCallUiState.clearProviderInfo();
    249         }
    250 
    251         CallStatusCode status = placeCallInternal(intent);
    252 
    253         switch (status) {
    254             // Call was placed successfully:
    255             case SUCCESS:
    256             case EXITED_ECM:
    257                 if (DBG) log("==> placeCall(): success from placeCallInternal(): " + status);
    258 
    259                 if (status == CallStatusCode.EXITED_ECM) {
    260                     // Call succeeded, but we also need to tell the
    261                     // InCallScreen to show the "Exiting ECM" warning.
    262                     inCallUiState.setPendingCallStatusCode(CallStatusCode.EXITED_ECM);
    263                 } else {
    264                     // Call succeeded.  There's no "error condition" that
    265                     // needs to be displayed to the user, so clear out the
    266                     // InCallUiState's "pending call status code".
    267                     inCallUiState.clearPendingCallStatusCode();
    268                 }
    269 
    270                 // Notify the phone app that a call is beginning so it can
    271                 // enable the proximity sensor
    272                 mApp.setBeginningCall(true);
    273                 break;
    274 
    275             default:
    276                 // Any other status code is a failure.
    277                 log("==> placeCall(): failure code from placeCallInternal(): " + status);
    278                 // Handle the various error conditions that can occur when
    279                 // initiating an outgoing call, typically by directing the
    280                 // InCallScreen to display a diagnostic message (via the
    281                 // "pending call status code" flag.)
    282                 handleOutgoingCallError(status);
    283                 break;
    284         }
    285 
    286         // Finally, regardless of whether we successfully initiated the
    287         // outgoing call or not, force the InCallScreen to come to the
    288         // foreground.
    289         //
    290         // (For successful calls the the user will just see the normal
    291         // in-call UI.  Or if there was an error, the InCallScreen will
    292         // notice the InCallUiState pending call status code flag and display an
    293         // error indication instead.)
    294 
    295         // TODO: double-check the behavior of mApp.displayCallScreen()
    296         // if the InCallScreen is already visible:
    297         // - make sure it forces the UI to refresh
    298         // - make sure it does NOT launch a new InCallScreen on top
    299         //   of the current one (i.e. the Back button should not take
    300         //   you back to the previous InCallScreen)
    301         // - it's probably OK to go thru a fresh pause/resume sequence
    302         //   though (since that should be fast now)
    303         // - if necessary, though, maybe PhoneApp.displayCallScreen()
    304         //   could notice that the InCallScreen is already in the foreground,
    305         //   and if so simply call updateInCallScreen() instead.
    306 
    307         mApp.displayCallScreen();
    308     }
    309 
    310     /**
    311      * Actually make a call to whomever the intent tells us to.
    312      *
    313      * Note that there's no need to explicitly update (or refresh) the
    314      * in-call UI at any point in this method, since a fresh InCallScreen
    315      * instance will be launched automatically after we return (see
    316      * placeCall() above.)
    317      *
    318      * @param intent the CALL intent describing whom to call
    319      * @return CallStatusCode.SUCCESS if we successfully initiated an
    320      *    outgoing call.  If there was some kind of failure, return one of
    321      *    the other CallStatusCode codes indicating what went wrong.
    322      */
    323     private CallStatusCode placeCallInternal(Intent intent) {
    324         if (DBG) log("placeCallInternal()...  intent = " + intent);
    325 
    326         // TODO: This method is too long.  Break it down into more
    327         // manageable chunks.
    328 
    329         final InCallUiState inCallUiState = mApp.inCallUiState;
    330         final Uri uri = intent.getData();
    331         final String scheme = (uri != null) ? uri.getScheme() : null;
    332         String number;
    333         Phone phone = null;
    334 
    335         // Check the current ServiceState to make sure it's OK
    336         // to even try making a call.
    337         CallStatusCode okToCallStatus = checkIfOkToInitiateOutgoingCall(
    338                 mCM.getServiceState());
    339 
    340         // TODO: Streamline the logic here.  Currently, the code is
    341         // unchanged from its original form in InCallScreen.java.  But we
    342         // should fix a couple of things:
    343         // - Don't call checkIfOkToInitiateOutgoingCall() more than once
    344         // - Wrap the try/catch for VoiceMailNumberMissingException
    345         //   around *only* the call that can throw that exception.
    346 
    347         try {
    348             number = PhoneUtils.getInitialNumber(intent);
    349             if (VDBG) log("- actual number to dial: '" + number + "'");
    350 
    351             // find the phone first
    352             // TODO Need a way to determine which phone to place the call
    353             // It could be determined by SIP setting, i.e. always,
    354             // or by number, i.e. for international,
    355             // or by user selection, i.e., dialog query,
    356             // or any of combinations
    357             String sipPhoneUri = intent.getStringExtra(
    358                     OutgoingCallBroadcaster.EXTRA_SIP_PHONE_URI);
    359             phone = PhoneUtils.pickPhoneBasedOnNumber(mCM, scheme, number, sipPhoneUri);
    360             if (VDBG) log("- got Phone instance: " + phone + ", class = " + phone.getClass());
    361 
    362             // update okToCallStatus based on new phone
    363             okToCallStatus = checkIfOkToInitiateOutgoingCall(
    364                     phone.getServiceState().getState());
    365 
    366         } catch (PhoneUtils.VoiceMailNumberMissingException ex) {
    367             // If the call status is NOT in an acceptable state, it
    368             // may effect the way the voicemail number is being
    369             // retrieved.  Mask the VoiceMailNumberMissingException
    370             // with the underlying issue of the phone state.
    371             if (okToCallStatus != CallStatusCode.SUCCESS) {
    372                 if (DBG) log("Voicemail number not reachable in current SIM card state.");
    373                 return okToCallStatus;
    374             }
    375             if (DBG) log("VoiceMailNumberMissingException from getInitialNumber()");
    376             return CallStatusCode.VOICEMAIL_NUMBER_MISSING;
    377         }
    378 
    379         if (number == null) {
    380             Log.w(TAG, "placeCall: couldn't get a phone number from Intent " + intent);
    381             return CallStatusCode.NO_PHONE_NUMBER_SUPPLIED;
    382         }
    383 
    384 
    385         // Sanity-check that ACTION_CALL_EMERGENCY is used if and only if
    386         // this is a call to an emergency number
    387         // (This is just a sanity-check; this policy *should* really be
    388         // enforced in OutgoingCallBroadcaster.onCreate(), which is the
    389         // main entry point for the CALL and CALL_* intents.)
    390         boolean isEmergencyNumber = PhoneNumberUtils.isLocalEmergencyNumber(number, mApp);
    391         boolean isPotentialEmergencyNumber =
    392                 PhoneNumberUtils.isPotentialLocalEmergencyNumber(number, mApp);
    393         boolean isEmergencyIntent = Intent.ACTION_CALL_EMERGENCY.equals(intent.getAction());
    394 
    395         if (isPotentialEmergencyNumber && !isEmergencyIntent) {
    396             Log.e(TAG, "Non-CALL_EMERGENCY Intent " + intent
    397                     + " attempted to call potential emergency number " + number
    398                     + ".");
    399             return CallStatusCode.CALL_FAILED;
    400         } else if (!isPotentialEmergencyNumber && isEmergencyIntent) {
    401             Log.e(TAG, "Received CALL_EMERGENCY Intent " + intent
    402                     + " with non-potential-emergency number " + number
    403                     + " -- failing call.");
    404             return CallStatusCode.CALL_FAILED;
    405         }
    406 
    407         // If we're trying to call an emergency number, then it's OK to
    408         // proceed in certain states where we'd otherwise bring up
    409         // an error dialog:
    410         // - If we're in EMERGENCY_ONLY mode, then (obviously) you're allowed
    411         //   to dial emergency numbers.
    412         // - If we're OUT_OF_SERVICE, we still attempt to make a call,
    413         //   since the radio will register to any available network.
    414 
    415         if (isEmergencyNumber
    416             && ((okToCallStatus == CallStatusCode.EMERGENCY_ONLY)
    417                 || (okToCallStatus == CallStatusCode.OUT_OF_SERVICE))) {
    418             if (DBG) log("placeCall: Emergency number detected with status = " + okToCallStatus);
    419             okToCallStatus = CallStatusCode.SUCCESS;
    420             if (DBG) log("==> UPDATING status to: " + okToCallStatus);
    421         }
    422 
    423         if (okToCallStatus != CallStatusCode.SUCCESS) {
    424             // If this is an emergency call, launch the EmergencyCallHelperService
    425             // to turn on the radio and retry the call.
    426             if (isEmergencyNumber && (okToCallStatus == CallStatusCode.POWER_OFF)) {
    427                 Log.i(TAG, "placeCall: Trying to make emergency call while POWER_OFF!");
    428 
    429                 // If needed, lazily instantiate an EmergencyCallHelper instance.
    430                 synchronized (this) {
    431                     if (mEmergencyCallHelper == null) {
    432                         mEmergencyCallHelper = new EmergencyCallHelper(this);
    433                     }
    434                 }
    435 
    436                 // ...and kick off the "emergency call from airplane mode" sequence.
    437                 mEmergencyCallHelper.startEmergencyCallFromAirplaneModeSequence(number);
    438 
    439                 // Finally, return CallStatusCode.SUCCESS right now so
    440                 // that the in-call UI will remain visible (in order to
    441                 // display the progress indication.)
    442                 // TODO: or maybe it would be more clear to return a whole
    443                 // new CallStatusCode called "TURNING_ON_RADIO" here.
    444                 // That way, we'd update inCallUiState.progressIndication from
    445                 // the handleOutgoingCallError() method, rather than here.
    446                 return CallStatusCode.SUCCESS;
    447             } else {
    448                 // Otherwise, just return the (non-SUCCESS) status code
    449                 // back to our caller.
    450                 if (DBG) log("==> placeCallInternal(): non-success status: " + okToCallStatus);
    451                 return okToCallStatus;
    452             }
    453         }
    454 
    455         // Ok, we can proceed with this outgoing call.
    456 
    457         // Reset some InCallUiState flags, just in case they're still set
    458         // from a prior call.
    459         inCallUiState.needToShowCallLostDialog = false;
    460         inCallUiState.clearProgressIndication();
    461 
    462         // We have a valid number, so try to actually place a call:
    463         // make sure we pass along the intent's URI which is a
    464         // reference to the contact. We may have a provider gateway
    465         // phone number to use for the outgoing call.
    466         Uri contactUri = intent.getData();
    467 
    468         // Watch out: PhoneUtils.placeCall() returns one of the
    469         // CALL_STATUS_* constants, not a CallStatusCode enum value.
    470         int callStatus = PhoneUtils.placeCall(mApp,
    471                                               phone,
    472                                               number,
    473                                               contactUri,
    474                                               (isEmergencyNumber || isEmergencyIntent),
    475                                               inCallUiState.providerGatewayUri);
    476 
    477         switch (callStatus) {
    478             case PhoneUtils.CALL_STATUS_DIALED:
    479                 if (VDBG) log("placeCall: PhoneUtils.placeCall() succeeded for regular call '"
    480                              + number + "'.");
    481 
    482 
    483                 // TODO(OTASP): still need more cleanup to simplify the mApp.cdma*State objects:
    484                 // - Rather than checking inCallUiState.inCallScreenMode, the
    485                 //   code here could also check for
    486                 //   app.getCdmaOtaInCallScreenUiState() returning NORMAL.
    487                 // - But overall, app.inCallUiState.inCallScreenMode and
    488                 //   app.cdmaOtaInCallScreenUiState.state are redundant.
    489                 //   Combine them.
    490 
    491                 if (VDBG) log ("- inCallUiState.inCallScreenMode = "
    492                                + inCallUiState.inCallScreenMode);
    493                 if (inCallUiState.inCallScreenMode == InCallScreenMode.OTA_NORMAL) {
    494                     if (VDBG) log ("==>  OTA_NORMAL note: switching to OTA_STATUS_LISTENING.");
    495                     mApp.cdmaOtaScreenState.otaScreenState =
    496                             CdmaOtaScreenState.OtaScreenState.OTA_STATUS_LISTENING;
    497                 }
    498 
    499                 boolean voicemailUriSpecified = scheme != null && scheme.equals("voicemail");
    500                 // When voicemail is requested most likely the user wants to open
    501                 // dialpad immediately, so we show it in the first place.
    502                 // Otherwise we want to make sure the user can see the regular
    503                 // in-call UI while the new call is dialing, and when it
    504                 // first gets connected.)
    505                 inCallUiState.showDialpad = voicemailUriSpecified;
    506 
    507                 // For voicemails, we add context text to let the user know they
    508                 // are dialing their voicemail.
    509                 // TODO: This is only set here and becomes problematic when swapping calls
    510                 inCallUiState.dialpadContextText = voicemailUriSpecified ?
    511                     phone.getVoiceMailAlphaTag() : "";
    512 
    513                 // Also, in case a previous call was already active (i.e. if
    514                 // we just did "Add call"), clear out the "history" of DTMF
    515                 // digits you typed, to make sure it doesn't persist from the
    516                 // previous call to the new call.
    517                 // TODO: it would be more precise to do this when the actual
    518                 // phone state change happens (i.e. when a new foreground
    519                 // call appears and the previous call moves to the
    520                 // background), but the InCallScreen doesn't keep enough
    521                 // state right now to notice that specific transition in
    522                 // onPhoneStateChanged().
    523                 inCallUiState.dialpadDigits = null;
    524 
    525                 // Check for an obscure ECM-related scenario: If the phone
    526                 // is currently in ECM (Emergency callback mode) and we
    527                 // dial a non-emergency number, that automatically
    528                 // *cancels* ECM.  So warn the user about it.
    529                 // (See InCallScreen.showExitingECMDialog() for more info.)
    530                 boolean exitedEcm = false;
    531                 if (PhoneUtils.isPhoneInEcm(phone) && !isEmergencyNumber) {
    532                     Log.i(TAG, "About to exit ECM because of an outgoing non-emergency call");
    533                     exitedEcm = true;  // this will cause us to return EXITED_ECM from this method
    534                 }
    535 
    536                 if (phone.getPhoneType() == Phone.PHONE_TYPE_CDMA) {
    537                     // Start the timer for 3 Way CallerInfo
    538                     if (mApp.cdmaPhoneCallState.getCurrentCallState()
    539                             == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) {
    540                         //Unmute for the second MO call
    541                         PhoneUtils.setMute(false);
    542 
    543                         // This is a "CDMA 3-way call", which means that you're dialing a
    544                         // 2nd outgoing call while a previous call is already in progress.
    545                         //
    546                         // Due to the limitations of CDMA this call doesn't actually go
    547                         // through the DIALING/ALERTING states, so we can't tell for sure
    548                         // when (or if) it's actually answered.  But we want to show
    549                         // *some* indication of what's going on in the UI, so we "fake it"
    550                         // by displaying the "Dialing" state for 3 seconds.
    551 
    552                         // Set the mThreeWayCallOrigStateDialing state to true
    553                         mApp.cdmaPhoneCallState.setThreeWayCallOrigState(true);
    554 
    555                         // Schedule the "Dialing" indication to be taken down in 3 seconds:
    556                         sendEmptyMessageDelayed(THREEWAY_CALLERINFO_DISPLAY_DONE,
    557                                                 THREEWAY_CALLERINFO_DISPLAY_TIME);
    558                     }
    559                 }
    560 
    561                 // Success!
    562                 if (exitedEcm) {
    563                     return CallStatusCode.EXITED_ECM;
    564                 } else {
    565                     return CallStatusCode.SUCCESS;
    566                 }
    567 
    568             case PhoneUtils.CALL_STATUS_DIALED_MMI:
    569                 if (DBG) log("placeCall: specified number was an MMI code: '" + number + "'.");
    570                 // The passed-in number was an MMI code, not a regular phone number!
    571                 // This isn't really a failure; the Dialer may have deliberately
    572                 // fired an ACTION_CALL intent to dial an MMI code, like for a
    573                 // USSD call.
    574                 //
    575                 // Presumably an MMI_INITIATE message will come in shortly
    576                 // (and we'll bring up the "MMI Started" dialog), or else
    577                 // an MMI_COMPLETE will come in (which will take us to a
    578                 // different Activity; see PhoneUtils.displayMMIComplete()).
    579                 return CallStatusCode.DIALED_MMI;
    580 
    581             case PhoneUtils.CALL_STATUS_FAILED:
    582                 Log.w(TAG, "placeCall: PhoneUtils.placeCall() FAILED for number '"
    583                       + number + "'.");
    584                 // We couldn't successfully place the call; there was some
    585                 // failure in the telephony layer.
    586                 return CallStatusCode.CALL_FAILED;
    587 
    588             default:
    589                 Log.wtf(TAG, "placeCall: unknown callStatus " + callStatus
    590                         + " from PhoneUtils.placeCall() for number '" + number + "'.");
    591                 return CallStatusCode.SUCCESS;  // Try to continue anyway...
    592         }
    593     }
    594 
    595     /**
    596      * Checks the current ServiceState to make sure it's OK
    597      * to try making an outgoing call to the specified number.
    598      *
    599      * @return CallStatusCode.SUCCESS if it's OK to try calling the specified
    600      *    number.  If not, like if the radio is powered off or we have no
    601      *    signal, return one of the other CallStatusCode codes indicating what
    602      *    the problem is.
    603      */
    604     private CallStatusCode checkIfOkToInitiateOutgoingCall(int state) {
    605         if (VDBG) log("checkIfOkToInitiateOutgoingCall: ServiceState = " + state);
    606 
    607         switch (state) {
    608             case ServiceState.STATE_IN_SERVICE:
    609                 // Normal operation.  It's OK to make outgoing calls.
    610                 return CallStatusCode.SUCCESS;
    611 
    612             case ServiceState.STATE_POWER_OFF:
    613                 // Radio is explictly powered off.
    614                 return CallStatusCode.POWER_OFF;
    615 
    616             case ServiceState.STATE_EMERGENCY_ONLY:
    617                 // The phone is registered, but locked. Only emergency
    618                 // numbers are allowed.
    619                 // Note that as of Android 2.0 at least, the telephony layer
    620                 // does not actually use ServiceState.STATE_EMERGENCY_ONLY,
    621                 // mainly since there's no guarantee that the radio/RIL can
    622                 // make this distinction.  So in practice the
    623                 // CallStatusCode.EMERGENCY_ONLY state and the string
    624                 // "incall_error_emergency_only" are totally unused.
    625                 return CallStatusCode.EMERGENCY_ONLY;
    626 
    627             case ServiceState.STATE_OUT_OF_SERVICE:
    628                 // No network connection.
    629                 return CallStatusCode.OUT_OF_SERVICE;
    630 
    631             default:
    632                 throw new IllegalStateException("Unexpected ServiceState: " + state);
    633         }
    634     }
    635 
    636 
    637 
    638     /**
    639      * Handles the various error conditions that can occur when initiating
    640      * an outgoing call.
    641      *
    642      * Most error conditions are "handled" by simply displaying an error
    643      * message to the user.  This is accomplished by setting the
    644      * inCallUiState pending call status code flag, which tells the
    645      * InCallScreen to display an appropriate message to the user when the
    646      * in-call UI comes to the foreground.
    647      *
    648      * @param status one of the CallStatusCode error codes.
    649      */
    650     private void handleOutgoingCallError(CallStatusCode status) {
    651         if (DBG) log("handleOutgoingCallError(): status = " + status);
    652         final InCallUiState inCallUiState = mApp.inCallUiState;
    653 
    654         // In most cases we simply want to have the InCallScreen display
    655         // an appropriate error dialog, so we simply copy the specified
    656         // status code into the InCallUiState "pending call status code"
    657         // field.  (See InCallScreen.showStatusIndication() for the next
    658         // step of the sequence.)
    659 
    660         switch (status) {
    661             case SUCCESS:
    662                 // This case shouldn't happen; you're only supposed to call
    663                 // handleOutgoingCallError() if there was actually an error!
    664                 Log.wtf(TAG, "handleOutgoingCallError: SUCCESS isn't an error");
    665                 break;
    666 
    667             case VOICEMAIL_NUMBER_MISSING:
    668                 // Bring up the "Missing Voicemail Number" dialog, which
    669                 // will ultimately take us to some other Activity (or else
    670                 // just bail out of this activity.)
    671 
    672                 // Send a request to the InCallScreen to display the
    673                 // "voicemail missing" dialog when it (the InCallScreen)
    674                 // comes to the foreground.
    675                 inCallUiState.setPendingCallStatusCode(CallStatusCode.VOICEMAIL_NUMBER_MISSING);
    676                 break;
    677 
    678             case POWER_OFF:
    679                 // Radio is explictly powered off, presumably because the
    680                 // device is in airplane mode.
    681                 //
    682                 // TODO: For now this UI is ultra-simple: we simply display
    683                 // a message telling the user to turn off airplane mode.
    684                 // But it might be nicer for the dialog to offer the option
    685                 // to turn the radio on right there (and automatically retry
    686                 // the call once network registration is complete.)
    687                 inCallUiState.setPendingCallStatusCode(CallStatusCode.POWER_OFF);
    688                 break;
    689 
    690             case EMERGENCY_ONLY:
    691                 // Only emergency numbers are allowed, but we tried to dial
    692                 // a non-emergency number.
    693                 // (This state is currently unused; see comments above.)
    694                 inCallUiState.setPendingCallStatusCode(CallStatusCode.EMERGENCY_ONLY);
    695                 break;
    696 
    697             case OUT_OF_SERVICE:
    698                 // No network connection.
    699                 inCallUiState.setPendingCallStatusCode(CallStatusCode.OUT_OF_SERVICE);
    700                 break;
    701 
    702             case NO_PHONE_NUMBER_SUPPLIED:
    703                 // The supplied Intent didn't contain a valid phone number.
    704                 // (This is rare and should only ever happen with broken
    705                 // 3rd-party apps.)  For now just show a generic error.
    706                 inCallUiState.setPendingCallStatusCode(CallStatusCode.NO_PHONE_NUMBER_SUPPLIED);
    707                 break;
    708 
    709             case DIALED_MMI:
    710                 // Our initial phone number was actually an MMI sequence.
    711                 // There's no real "error" here, but we do bring up the
    712                 // a Toast (as requested of the New UI paradigm).
    713                 //
    714                 // In-call MMIs do not trigger the normal MMI Initiate
    715                 // Notifications, so we should notify the user here.
    716                 // Otherwise, the code in PhoneUtils.java should handle
    717                 // user notifications in the form of Toasts or Dialogs.
    718                 //
    719                 // TODO: Rather than launching a toast from here, it would
    720                 // be cleaner to just set a pending call status code here,
    721                 // and then let the InCallScreen display the toast...
    722                 if (mCM.getState() == Phone.State.OFFHOOK) {
    723                     Toast.makeText(mApp, R.string.incall_status_dialed_mmi, Toast.LENGTH_SHORT)
    724                             .show();
    725                 }
    726                 break;
    727 
    728             case CALL_FAILED:
    729                 // We couldn't successfully place the call; there was some
    730                 // failure in the telephony layer.
    731                 // TODO: Need UI spec for this failure case; for now just
    732                 // show a generic error.
    733                 inCallUiState.setPendingCallStatusCode(CallStatusCode.CALL_FAILED);
    734                 break;
    735 
    736             default:
    737                 Log.wtf(TAG, "handleOutgoingCallError: unexpected status code " + status);
    738                 // Show a generic "call failed" error.
    739                 inCallUiState.setPendingCallStatusCode(CallStatusCode.CALL_FAILED);
    740                 break;
    741         }
    742     }
    743 
    744     /**
    745      * Checks the current outgoing call to see if it's an OTASP call (the
    746      * "activation" call used to provision CDMA devices).  If so, do any
    747      * necessary OTASP-specific setup before actually placing the call.
    748      */
    749     private void checkForOtaspCall(Intent intent) {
    750         if (OtaUtils.isOtaspCallIntent(intent)) {
    751             Log.i(TAG, "checkForOtaspCall: handling OTASP intent! " + intent);
    752 
    753             // ("OTASP-specific setup" basically means creating and initializing
    754             // the OtaUtils instance.  Note that this setup needs to be here in
    755             // the CallController.placeCall() sequence, *not* in
    756             // OtaUtils.startInteractiveOtasp(), since it's also possible to
    757             // start an OTASP call by manually dialing "*228" (in which case
    758             // OtaUtils.startInteractiveOtasp() never gets run at all.)
    759             OtaUtils.setupOtaspCall(intent);
    760         } else {
    761             if (DBG) log("checkForOtaspCall: not an OTASP call.");
    762         }
    763     }
    764 
    765 
    766     //
    767     // Debugging
    768     //
    769 
    770     private static void log(String msg) {
    771         Log.d(TAG, msg);
    772     }
    773 }
    774