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