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