blob: 7b889dee3d58b8839866d8069cd3d262e990096f [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
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
17package com.android.phone;
18
19import com.android.internal.telephony.CallManager;
20import com.android.internal.telephony.Phone;
21import com.android.internal.telephony.PhoneConstants;
22import com.android.internal.telephony.TelephonyCapabilities;
23import com.android.phone.Constants.CallStatusCode;
24import com.android.phone.InCallUiState.InCallScreenMode;
25import com.android.phone.OtaUtils.CdmaOtaScreenState;
26
27import android.content.Intent;
28import android.net.Uri;
29import android.os.Handler;
30import android.os.Message;
31import android.os.SystemProperties;
32import android.provider.CallLog.Calls;
33import android.telephony.PhoneNumberUtils;
34import android.telephony.ServiceState;
35import android.text.TextUtils;
36import android.util.Log;
37import android.widget.Toast;
38
39/**
40 * Phone app module in charge of "call control".
41 *
42 * This is a singleton object which acts as the interface to the telephony layer
43 * (and other parts of the Android framework) for all user-initiated telephony
44 * functionality, like making outgoing calls.
45 *
46 * This functionality includes things like:
47 * - actually running the placeCall() method and handling errors or retries
48 * - running the whole "emergency call in airplane mode" sequence
49 * - running the state machine of MMI sequences
50 * - restoring/resetting mute and speaker state when a new call starts
51 * - updating the prox sensor wake lock state
52 * - resolving what the voicemail: intent should mean (and making the call)
53 *
54 * The single CallController instance stays around forever; it's not tied
55 * to the lifecycle of any particular Activity (like the InCallScreen).
56 * There's also no implementation of onscreen UI here (that's all in InCallScreen).
57 *
58 * Note that this class does not handle asynchronous events from the telephony
59 * layer, like reacting to an incoming call; see CallNotifier for that. This
60 * class purely handles actions initiated by the user, like outgoing calls.
61 */
62public class CallController extends Handler {
63 private static final String TAG = "CallController";
64 private static final boolean DBG =
65 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
66 // Do not check in with VDBG = true, since that may write PII to the system log.
67 private static final boolean VDBG = false;
68
69 /** The singleton CallController instance. */
70 private static CallController sInstance;
71
72 private PhoneGlobals mApp;
73 private CallManager mCM;
74 private CallLogger mCallLogger;
75
76 /** Helper object for emergency calls in some rare use cases. Created lazily. */
77 private EmergencyCallHelper mEmergencyCallHelper;
78
79
80 //
81 // Message codes; see handleMessage().
82 //
83
84 private static final int THREEWAY_CALLERINFO_DISPLAY_DONE = 1;
85
86
87 //
88 // Misc constants.
89 //
90
91 // Amount of time the UI should display "Dialing" when initiating a CDMA
92 // 3way call. (See comments on the THRWAY_ACTIVE case in
93 // placeCallInternal() for more info.)
94 private static final int THREEWAY_CALLERINFO_DISPLAY_TIME = 3000; // msec
95
96
97 /**
98 * Initialize the singleton CallController instance.
99 *
100 * This is only done once, at startup, from PhoneApp.onCreate().
101 * From then on, the CallController instance is available via the
102 * PhoneApp's public "callController" field, which is why there's no
103 * getInstance() method here.
104 */
105 /* package */ static CallController init(PhoneGlobals app, CallLogger callLogger) {
106 synchronized (CallController.class) {
107 if (sInstance == null) {
108 sInstance = new CallController(app, callLogger);
109 } else {
110 Log.wtf(TAG, "init() called multiple times! sInstance = " + sInstance);
111 }
112 return sInstance;
113 }
114 }
115
116 /**
117 * Private constructor (this is a singleton).
118 * @see init()
119 */
120 private CallController(PhoneGlobals app, CallLogger callLogger) {
121 if (DBG) log("CallController constructor: app = " + app);
122 mApp = app;
123 mCM = app.mCM;
124 mCallLogger = callLogger;
125 }
126
127 @Override
128 public void handleMessage(Message msg) {
129 if (VDBG) log("handleMessage: " + msg);
130 switch (msg.what) {
131
132 case THREEWAY_CALLERINFO_DISPLAY_DONE:
133 if (DBG) log("THREEWAY_CALLERINFO_DISPLAY_DONE...");
134
135 if (mApp.cdmaPhoneCallState.getCurrentCallState()
136 == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) {
137 // Reset the mThreeWayCallOrigStateDialing state
138 mApp.cdmaPhoneCallState.setThreeWayCallOrigState(false);
139
140 // Refresh the in-call UI (based on the current ongoing call)
141 mApp.updateInCallScreen();
142 }
143 break;
144
145 default:
146 Log.wtf(TAG, "handleMessage: unexpected code: " + msg);
147 break;
148 }
149 }
150
151 //
152 // Outgoing call sequence
153 //
154
155 /**
156 * Initiate an outgoing call.
157 *
158 * Here's the most typical outgoing call sequence:
159 *
160 * (1) OutgoingCallBroadcaster receives a CALL intent and sends the
161 * NEW_OUTGOING_CALL broadcast
162 *
163 * (2) The broadcast finally reaches OutgoingCallReceiver, which stashes
164 * away a copy of the original CALL intent and launches
165 * SipCallOptionHandler
166 *
167 * (3) SipCallOptionHandler decides whether this is a PSTN or SIP call (and
168 * in some cases brings up a dialog to let the user choose), and
169 * ultimately calls CallController.placeCall() (from the
170 * setResultAndFinish() method) with the stashed-away intent from step
171 * (2) as the "intent" parameter.
172 *
173 * (4) Here in CallController.placeCall() we read the phone number or SIP
174 * address out of the intent and actually initiate the call, and
175 * simultaneously launch the InCallScreen to display the in-call UI.
176 *
177 * (5) We handle various errors by directing the InCallScreen to
178 * display error messages or dialogs (via the InCallUiState
179 * "pending call status code" flag), and in some cases we also
180 * sometimes continue working in the background to resolve the
181 * problem (like in the case of an emergency call while in
182 * airplane mode). Any time that some onscreen indication to the
183 * user needs to change, we update the "status dialog" info in
184 * the inCallUiState and (re)launch the InCallScreen to make sure
185 * it's visible.
186 */
187 public void placeCall(Intent intent) {
188 log("placeCall()... intent = " + intent);
189 if (VDBG) log(" extras = " + intent.getExtras());
190
191 final InCallUiState inCallUiState = mApp.inCallUiState;
192
193 // TODO: Do we need to hold a wake lock while this method runs?
194 // Or did we already acquire one somewhere earlier
195 // in this sequence (like when we first received the CALL intent?)
196
197 if (intent == null) {
198 Log.wtf(TAG, "placeCall: called with null intent");
199 throw new IllegalArgumentException("placeCall: called with null intent");
200 }
201
202 String action = intent.getAction();
203 Uri uri = intent.getData();
204 if (uri == null) {
205 Log.wtf(TAG, "placeCall: intent had no data");
206 throw new IllegalArgumentException("placeCall: intent had no data");
207 }
208
209 String scheme = uri.getScheme();
210 String number = PhoneNumberUtils.getNumberFromIntent(intent, mApp);
211 if (VDBG) {
212 log("- action: " + action);
213 log("- uri: " + uri);
214 log("- scheme: " + scheme);
215 log("- number: " + number);
216 }
217
218 // This method should only be used with the various flavors of CALL
219 // intents. (It doesn't make sense for any other action to trigger an
220 // outgoing call!)
221 if (!(Intent.ACTION_CALL.equals(action)
222 || Intent.ACTION_CALL_EMERGENCY.equals(action)
223 || Intent.ACTION_CALL_PRIVILEGED.equals(action))) {
224 Log.wtf(TAG, "placeCall: unexpected intent action " + action);
225 throw new IllegalArgumentException("Unexpected action: " + action);
226 }
227
228 // Check to see if this is an OTASP call (the "activation" call
229 // used to provision CDMA devices), and if so, do some
230 // OTASP-specific setup.
231 Phone phone = mApp.mCM.getDefaultPhone();
232 if (TelephonyCapabilities.supportsOtasp(phone)) {
233 checkForOtaspCall(intent);
234 }
235
236 // Clear out the "restore mute state" flag since we're
237 // initiating a brand-new call.
238 //
239 // (This call to setRestoreMuteOnInCallResume(false) informs the
240 // phone app that we're dealing with a new connection
241 // (i.e. placing an outgoing call, and NOT handling an aborted
242 // "Add Call" request), so we should let the mute state be handled
243 // by the PhoneUtils phone state change handler.)
244 mApp.setRestoreMuteOnInCallResume(false);
245
246 // If a provider is used, extract the info to build the
247 // overlay and route the call. The overlay will be
248 // displayed when the InCallScreen becomes visible.
249 if (PhoneUtils.hasPhoneProviderExtras(intent)) {
250 inCallUiState.setProviderInfo(intent);
251 } else {
252 inCallUiState.clearProviderInfo();
253 }
254
255 CallStatusCode status = placeCallInternal(intent);
256
257 switch (status) {
258 // Call was placed successfully:
259 case SUCCESS:
260 case EXITED_ECM:
261 if (DBG) log("==> placeCall(): success from placeCallInternal(): " + status);
262
263 if (status == CallStatusCode.EXITED_ECM) {
264 // Call succeeded, but we also need to tell the
265 // InCallScreen to show the "Exiting ECM" warning.
266 inCallUiState.setPendingCallStatusCode(CallStatusCode.EXITED_ECM);
267 } else {
268 // Call succeeded. There's no "error condition" that
269 // needs to be displayed to the user, so clear out the
270 // InCallUiState's "pending call status code".
271 inCallUiState.clearPendingCallStatusCode();
272 }
273
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700274 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 */
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
453 // Log failed call.
454 // Note: Normally, many of these values we gather from the Connection object but
455 // since no such object is created for unconnected calls, we have to build them
456 // manually.
457 // TODO(santoscordon): Try to restructure code so that we can handle failure-
458 // condition call logging in a single place (placeCall()) that also has access to
459 // the number we attempted to dial (not placeCall()).
460 mCallLogger.logCall(null /* callerInfo */, number, 0 /* presentation */,
461 Calls.OUTGOING_TYPE, System.currentTimeMillis(), 0 /* duration */);
462
463 return okToCallStatus;
464 }
465 }
466
467 // Ok, we can proceed with this outgoing call.
468
469 // Reset some InCallUiState flags, just in case they're still set
470 // from a prior call.
471 inCallUiState.needToShowCallLostDialog = false;
472 inCallUiState.clearProgressIndication();
473
474 // We have a valid number, so try to actually place a call:
475 // make sure we pass along the intent's URI which is a
476 // reference to the contact. We may have a provider gateway
477 // phone number to use for the outgoing call.
478 Uri contactUri = intent.getData();
479
480 // Watch out: PhoneUtils.placeCall() returns one of the
481 // CALL_STATUS_* constants, not a CallStatusCode enum value.
482 int callStatus = PhoneUtils.placeCall(mApp,
483 phone,
484 number,
485 contactUri,
486 (isEmergencyNumber || isEmergencyIntent),
487 inCallUiState.providerGatewayUri);
488
489 switch (callStatus) {
490 case PhoneUtils.CALL_STATUS_DIALED:
491 if (VDBG) log("placeCall: PhoneUtils.placeCall() succeeded for regular call '"
492 + number + "'.");
493
494
495 // TODO(OTASP): still need more cleanup to simplify the mApp.cdma*State objects:
496 // - Rather than checking inCallUiState.inCallScreenMode, the
497 // code here could also check for
498 // app.getCdmaOtaInCallScreenUiState() returning NORMAL.
499 // - But overall, app.inCallUiState.inCallScreenMode and
500 // app.cdmaOtaInCallScreenUiState.state are redundant.
501 // Combine them.
502
503 if (VDBG) log ("- inCallUiState.inCallScreenMode = "
504 + inCallUiState.inCallScreenMode);
505 if (inCallUiState.inCallScreenMode == InCallScreenMode.OTA_NORMAL) {
506 if (VDBG) log ("==> OTA_NORMAL note: switching to OTA_STATUS_LISTENING.");
507 mApp.cdmaOtaScreenState.otaScreenState =
508 CdmaOtaScreenState.OtaScreenState.OTA_STATUS_LISTENING;
509 }
510
511 boolean voicemailUriSpecified = scheme != null && scheme.equals("voicemail");
512 // When voicemail is requested most likely the user wants to open
513 // dialpad immediately, so we show it in the first place.
514 // Otherwise we want to make sure the user can see the regular
515 // in-call UI while the new call is dialing, and when it
516 // first gets connected.)
517 inCallUiState.showDialpad = voicemailUriSpecified;
518
519 // For voicemails, we add context text to let the user know they
520 // are dialing their voicemail.
521 // TODO: This is only set here and becomes problematic when swapping calls
522 inCallUiState.dialpadContextText = voicemailUriSpecified ?
523 phone.getVoiceMailAlphaTag() : "";
524
525 // Also, in case a previous call was already active (i.e. if
526 // we just did "Add call"), clear out the "history" of DTMF
527 // digits you typed, to make sure it doesn't persist from the
528 // previous call to the new call.
529 // TODO: it would be more precise to do this when the actual
530 // phone state change happens (i.e. when a new foreground
531 // call appears and the previous call moves to the
532 // background), but the InCallScreen doesn't keep enough
533 // state right now to notice that specific transition in
534 // onPhoneStateChanged().
535 inCallUiState.dialpadDigits = null;
536
537 // Check for an obscure ECM-related scenario: If the phone
538 // is currently in ECM (Emergency callback mode) and we
539 // dial a non-emergency number, that automatically
540 // *cancels* ECM. So warn the user about it.
541 // (See InCallScreen.showExitingECMDialog() for more info.)
542 boolean exitedEcm = false;
543 if (PhoneUtils.isPhoneInEcm(phone) && !isEmergencyNumber) {
544 Log.i(TAG, "About to exit ECM because of an outgoing non-emergency call");
545 exitedEcm = true; // this will cause us to return EXITED_ECM from this method
546 }
547
548 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
549 // Start the timer for 3 Way CallerInfo
550 if (mApp.cdmaPhoneCallState.getCurrentCallState()
551 == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) {
552 //Unmute for the second MO call
553 PhoneUtils.setMute(false);
554
555 // This is a "CDMA 3-way call", which means that you're dialing a
556 // 2nd outgoing call while a previous call is already in progress.
557 //
558 // Due to the limitations of CDMA this call doesn't actually go
559 // through the DIALING/ALERTING states, so we can't tell for sure
560 // when (or if) it's actually answered. But we want to show
561 // *some* indication of what's going on in the UI, so we "fake it"
562 // by displaying the "Dialing" state for 3 seconds.
563
564 // Set the mThreeWayCallOrigStateDialing state to true
565 mApp.cdmaPhoneCallState.setThreeWayCallOrigState(true);
566
567 // Schedule the "Dialing" indication to be taken down in 3 seconds:
568 sendEmptyMessageDelayed(THREEWAY_CALLERINFO_DISPLAY_DONE,
569 THREEWAY_CALLERINFO_DISPLAY_TIME);
570 }
571 }
572
573 // Success!
574 if (exitedEcm) {
575 return CallStatusCode.EXITED_ECM;
576 } else {
577 return CallStatusCode.SUCCESS;
578 }
579
580 case PhoneUtils.CALL_STATUS_DIALED_MMI:
581 if (DBG) log("placeCall: specified number was an MMI code: '" + number + "'.");
582 // The passed-in number was an MMI code, not a regular phone number!
583 // This isn't really a failure; the Dialer may have deliberately
584 // fired an ACTION_CALL intent to dial an MMI code, like for a
585 // USSD call.
586 //
587 // Presumably an MMI_INITIATE message will come in shortly
588 // (and we'll bring up the "MMI Started" dialog), or else
589 // an MMI_COMPLETE will come in (which will take us to a
590 // different Activity; see PhoneUtils.displayMMIComplete()).
591 return CallStatusCode.DIALED_MMI;
592
593 case PhoneUtils.CALL_STATUS_FAILED:
594 Log.w(TAG, "placeCall: PhoneUtils.placeCall() FAILED for number '"
595 + number + "'.");
596 // We couldn't successfully place the call; there was some
597 // failure in the telephony layer.
598
599 // Log failed call.
600 mCallLogger.logCall(null /* callerInfo */, number, 0 /* presentation */,
601 Calls.OUTGOING_TYPE, System.currentTimeMillis(), 0 /* duration */);
602
603 return CallStatusCode.CALL_FAILED;
604
605 default:
606 Log.wtf(TAG, "placeCall: unknown callStatus " + callStatus
607 + " from PhoneUtils.placeCall() for number '" + number + "'.");
608 return CallStatusCode.SUCCESS; // Try to continue anyway...
609 }
610 }
611
612 /**
613 * Checks the current ServiceState to make sure it's OK
614 * to try making an outgoing call to the specified number.
615 *
616 * @return CallStatusCode.SUCCESS if it's OK to try calling the specified
617 * number. If not, like if the radio is powered off or we have no
618 * signal, return one of the other CallStatusCode codes indicating what
619 * the problem is.
620 */
621 private CallStatusCode checkIfOkToInitiateOutgoingCall(int state) {
622 if (VDBG) log("checkIfOkToInitiateOutgoingCall: ServiceState = " + state);
623
624 switch (state) {
625 case ServiceState.STATE_IN_SERVICE:
626 // Normal operation. It's OK to make outgoing calls.
627 return CallStatusCode.SUCCESS;
628
629 case ServiceState.STATE_POWER_OFF:
630 // Radio is explictly powered off.
631 return CallStatusCode.POWER_OFF;
632
633 case ServiceState.STATE_EMERGENCY_ONLY:
634 // The phone is registered, but locked. Only emergency
635 // numbers are allowed.
636 // Note that as of Android 2.0 at least, the telephony layer
637 // does not actually use ServiceState.STATE_EMERGENCY_ONLY,
638 // mainly since there's no guarantee that the radio/RIL can
639 // make this distinction. So in practice the
640 // CallStatusCode.EMERGENCY_ONLY state and the string
641 // "incall_error_emergency_only" are totally unused.
642 return CallStatusCode.EMERGENCY_ONLY;
643
644 case ServiceState.STATE_OUT_OF_SERVICE:
645 // No network connection.
646 return CallStatusCode.OUT_OF_SERVICE;
647
648 default:
649 throw new IllegalStateException("Unexpected ServiceState: " + state);
650 }
651 }
652
653
654
655 /**
656 * Handles the various error conditions that can occur when initiating
657 * an outgoing call.
658 *
659 * Most error conditions are "handled" by simply displaying an error
660 * message to the user. This is accomplished by setting the
661 * inCallUiState pending call status code flag, which tells the
662 * InCallScreen to display an appropriate message to the user when the
663 * in-call UI comes to the foreground.
664 *
665 * @param status one of the CallStatusCode error codes.
666 */
667 private void handleOutgoingCallError(CallStatusCode status) {
668 if (DBG) log("handleOutgoingCallError(): status = " + status);
669 final InCallUiState inCallUiState = mApp.inCallUiState;
670
671 // In most cases we simply want to have the InCallScreen display
672 // an appropriate error dialog, so we simply copy the specified
673 // status code into the InCallUiState "pending call status code"
674 // field. (See InCallScreen.showStatusIndication() for the next
675 // step of the sequence.)
676
677 switch (status) {
678 case SUCCESS:
679 // This case shouldn't happen; you're only supposed to call
680 // handleOutgoingCallError() if there was actually an error!
681 Log.wtf(TAG, "handleOutgoingCallError: SUCCESS isn't an error");
682 break;
683
684 case VOICEMAIL_NUMBER_MISSING:
685 // Bring up the "Missing Voicemail Number" dialog, which
686 // will ultimately take us to some other Activity (or else
687 // just bail out of this activity.)
688
689 // Send a request to the InCallScreen to display the
690 // "voicemail missing" dialog when it (the InCallScreen)
691 // comes to the foreground.
692 inCallUiState.setPendingCallStatusCode(CallStatusCode.VOICEMAIL_NUMBER_MISSING);
693 break;
694
695 case POWER_OFF:
696 // Radio is explictly powered off, presumably because the
697 // device is in airplane mode.
698 //
699 // TODO: For now this UI is ultra-simple: we simply display
700 // a message telling the user to turn off airplane mode.
701 // But it might be nicer for the dialog to offer the option
702 // to turn the radio on right there (and automatically retry
703 // the call once network registration is complete.)
704 inCallUiState.setPendingCallStatusCode(CallStatusCode.POWER_OFF);
705 break;
706
707 case EMERGENCY_ONLY:
708 // Only emergency numbers are allowed, but we tried to dial
709 // a non-emergency number.
710 // (This state is currently unused; see comments above.)
711 inCallUiState.setPendingCallStatusCode(CallStatusCode.EMERGENCY_ONLY);
712 break;
713
714 case OUT_OF_SERVICE:
715 // No network connection.
716 inCallUiState.setPendingCallStatusCode(CallStatusCode.OUT_OF_SERVICE);
717 break;
718
719 case NO_PHONE_NUMBER_SUPPLIED:
720 // The supplied Intent didn't contain a valid phone number.
721 // (This is rare and should only ever happen with broken
722 // 3rd-party apps.) For now just show a generic error.
723 inCallUiState.setPendingCallStatusCode(CallStatusCode.NO_PHONE_NUMBER_SUPPLIED);
724 break;
725
726 case DIALED_MMI:
727 // Our initial phone number was actually an MMI sequence.
728 // There's no real "error" here, but we do bring up the
729 // a Toast (as requested of the New UI paradigm).
730 //
731 // In-call MMIs do not trigger the normal MMI Initiate
732 // Notifications, so we should notify the user here.
733 // Otherwise, the code in PhoneUtils.java should handle
734 // user notifications in the form of Toasts or Dialogs.
735 //
736 // TODO: Rather than launching a toast from here, it would
737 // be cleaner to just set a pending call status code here,
738 // and then let the InCallScreen display the toast...
739 if (mCM.getState() == PhoneConstants.State.OFFHOOK) {
740 Toast.makeText(mApp, R.string.incall_status_dialed_mmi, Toast.LENGTH_SHORT)
741 .show();
742 }
743 break;
744
745 case CALL_FAILED:
746 // We couldn't successfully place the call; there was some
747 // failure in the telephony layer.
748 // TODO: Need UI spec for this failure case; for now just
749 // show a generic error.
750 inCallUiState.setPendingCallStatusCode(CallStatusCode.CALL_FAILED);
751 break;
752
753 default:
754 Log.wtf(TAG, "handleOutgoingCallError: unexpected status code " + status);
755 // Show a generic "call failed" error.
756 inCallUiState.setPendingCallStatusCode(CallStatusCode.CALL_FAILED);
757 break;
758 }
759 }
760
761 /**
762 * Checks the current outgoing call to see if it's an OTASP call (the
763 * "activation" call used to provision CDMA devices). If so, do any
764 * necessary OTASP-specific setup before actually placing the call.
765 */
766 private void checkForOtaspCall(Intent intent) {
767 if (OtaUtils.isOtaspCallIntent(intent)) {
768 Log.i(TAG, "checkForOtaspCall: handling OTASP intent! " + intent);
769
770 // ("OTASP-specific setup" basically means creating and initializing
771 // the OtaUtils instance. Note that this setup needs to be here in
772 // the CallController.placeCall() sequence, *not* in
773 // OtaUtils.startInteractiveOtasp(), since it's also possible to
774 // start an OTASP call by manually dialing "*228" (in which case
775 // OtaUtils.startInteractiveOtasp() never gets run at all.)
776 OtaUtils.setupOtaspCall(intent);
777 } else {
778 if (DBG) log("checkForOtaspCall: not an OTASP call.");
779 }
780 }
781
782
783 //
784 // Debugging
785 //
786
787 private static void log(String msg) {
788 Log.d(TAG, msg);
789 }
790}