blob: 1b19d9a638728bdb5fe6fecfc5d78e801c26738b [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;
25import com.android.phone.InCallUiState.InCallScreenMode;
26import com.android.phone.OtaUtils.CdmaOtaScreenState;
27
28import android.content.Intent;
29import android.net.Uri;
30import android.os.Handler;
31import android.os.Message;
32import android.os.SystemProperties;
33import android.provider.CallLog.Calls;
34import android.telephony.PhoneNumberUtils;
35import android.telephony.ServiceState;
36import android.text.TextUtils;
37import android.util.Log;
38import android.widget.Toast;
39
40/**
41 * Phone app module in charge of "call control".
42 *
43 * This is a singleton object which acts as the interface to the telephony layer
44 * (and other parts of the Android framework) for all user-initiated telephony
45 * functionality, like making outgoing calls.
46 *
47 * This functionality includes things like:
48 * - actually running the placeCall() method and handling errors or retries
49 * - running the whole "emergency call in airplane mode" sequence
50 * - running the state machine of MMI sequences
51 * - restoring/resetting mute and speaker state when a new call starts
52 * - updating the prox sensor wake lock state
53 * - resolving what the voicemail: intent should mean (and making the call)
54 *
55 * The single CallController instance stays around forever; it's not tied
56 * to the lifecycle of any particular Activity (like the InCallScreen).
57 * There's also no implementation of onscreen UI here (that's all in InCallScreen).
58 *
59 * Note that this class does not handle asynchronous events from the telephony
60 * layer, like reacting to an incoming call; see CallNotifier for that. This
61 * class purely handles actions initiated by the user, like outgoing calls.
62 */
63public class CallController extends Handler {
64 private static final String TAG = "CallController";
65 private static final boolean DBG =
66 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
67 // Do not check in with VDBG = true, since that may write PII to the system log.
68 private static final boolean VDBG = false;
69
70 /** The singleton CallController instance. */
71 private static CallController sInstance;
72
Santos Cordon69a69192013-08-22 14:25:42 -070073 final private PhoneGlobals mApp;
74 final private CallManager mCM;
75 final private CallLogger mCallLogger;
76 final private CallGatewayManager mCallGatewayManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077
78 /** Helper object for emergency calls in some rare use cases. Created lazily. */
79 private EmergencyCallHelper mEmergencyCallHelper;
80
81
82 //
83 // Message codes; see handleMessage().
84 //
85
86 private static final int THREEWAY_CALLERINFO_DISPLAY_DONE = 1;
87
88
89 //
90 // Misc constants.
91 //
92
93 // Amount of time the UI should display "Dialing" when initiating a CDMA
94 // 3way call. (See comments on the THRWAY_ACTIVE case in
95 // placeCallInternal() for more info.)
96 private static final int THREEWAY_CALLERINFO_DISPLAY_TIME = 3000; // msec
97
98
99 /**
100 * Initialize the singleton CallController instance.
101 *
102 * This is only done once, at startup, from PhoneApp.onCreate().
103 * From then on, the CallController instance is available via the
104 * PhoneApp's public "callController" field, which is why there's no
105 * getInstance() method here.
106 */
Santos Cordon69a69192013-08-22 14:25:42 -0700107 /* package */ static CallController init(PhoneGlobals app, CallLogger callLogger,
108 CallGatewayManager callGatewayManager) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109 synchronized (CallController.class) {
110 if (sInstance == null) {
Santos Cordon69a69192013-08-22 14:25:42 -0700111 sInstance = new CallController(app, callLogger, callGatewayManager);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700112 } else {
113 Log.wtf(TAG, "init() called multiple times! sInstance = " + sInstance);
114 }
115 return sInstance;
116 }
117 }
118
119 /**
120 * Private constructor (this is a singleton).
121 * @see init()
122 */
Santos Cordon69a69192013-08-22 14:25:42 -0700123 private CallController(PhoneGlobals app, CallLogger callLogger,
124 CallGatewayManager callGatewayManager) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700125 if (DBG) log("CallController constructor: app = " + app);
126 mApp = app;
127 mCM = app.mCM;
128 mCallLogger = callLogger;
Santos Cordon69a69192013-08-22 14:25:42 -0700129 mCallGatewayManager = callGatewayManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700130 }
131
132 @Override
133 public void handleMessage(Message msg) {
134 if (VDBG) log("handleMessage: " + msg);
135 switch (msg.what) {
136
137 case THREEWAY_CALLERINFO_DISPLAY_DONE:
138 if (DBG) log("THREEWAY_CALLERINFO_DISPLAY_DONE...");
139
140 if (mApp.cdmaPhoneCallState.getCurrentCallState()
141 == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) {
142 // Reset the mThreeWayCallOrigStateDialing state
143 mApp.cdmaPhoneCallState.setThreeWayCallOrigState(false);
144
145 // Refresh the in-call UI (based on the current ongoing call)
146 mApp.updateInCallScreen();
147 }
148 break;
149
150 default:
151 Log.wtf(TAG, "handleMessage: unexpected code: " + msg);
152 break;
153 }
154 }
155
156 //
157 // Outgoing call sequence
158 //
159
160 /**
161 * Initiate an outgoing call.
162 *
163 * Here's the most typical outgoing call sequence:
164 *
165 * (1) OutgoingCallBroadcaster receives a CALL intent and sends the
166 * NEW_OUTGOING_CALL broadcast
167 *
168 * (2) The broadcast finally reaches OutgoingCallReceiver, which stashes
169 * away a copy of the original CALL intent and launches
170 * SipCallOptionHandler
171 *
172 * (3) SipCallOptionHandler decides whether this is a PSTN or SIP call (and
173 * in some cases brings up a dialog to let the user choose), and
174 * ultimately calls CallController.placeCall() (from the
175 * setResultAndFinish() method) with the stashed-away intent from step
176 * (2) as the "intent" parameter.
177 *
178 * (4) Here in CallController.placeCall() we read the phone number or SIP
179 * address out of the intent and actually initiate the call, and
180 * simultaneously launch the InCallScreen to display the in-call UI.
181 *
182 * (5) We handle various errors by directing the InCallScreen to
183 * display error messages or dialogs (via the InCallUiState
184 * "pending call status code" flag), and in some cases we also
185 * sometimes continue working in the background to resolve the
186 * problem (like in the case of an emergency call while in
187 * airplane mode). Any time that some onscreen indication to the
188 * user needs to change, we update the "status dialog" info in
189 * the inCallUiState and (re)launch the InCallScreen to make sure
190 * it's visible.
191 */
192 public void placeCall(Intent intent) {
193 log("placeCall()... intent = " + intent);
194 if (VDBG) log(" extras = " + intent.getExtras());
195
196 final InCallUiState inCallUiState = mApp.inCallUiState;
197
198 // TODO: Do we need to hold a wake lock while this method runs?
199 // Or did we already acquire one somewhere earlier
200 // in this sequence (like when we first received the CALL intent?)
201
202 if (intent == null) {
203 Log.wtf(TAG, "placeCall: called with null intent");
204 throw new IllegalArgumentException("placeCall: called with null intent");
205 }
206
207 String action = intent.getAction();
208 Uri uri = intent.getData();
209 if (uri == null) {
210 Log.wtf(TAG, "placeCall: intent had no data");
211 throw new IllegalArgumentException("placeCall: intent had no data");
212 }
213
214 String scheme = uri.getScheme();
215 String number = PhoneNumberUtils.getNumberFromIntent(intent, mApp);
216 if (VDBG) {
217 log("- action: " + action);
218 log("- uri: " + uri);
219 log("- scheme: " + scheme);
220 log("- number: " + number);
221 }
222
223 // This method should only be used with the various flavors of CALL
224 // intents. (It doesn't make sense for any other action to trigger an
225 // outgoing call!)
226 if (!(Intent.ACTION_CALL.equals(action)
227 || Intent.ACTION_CALL_EMERGENCY.equals(action)
228 || Intent.ACTION_CALL_PRIVILEGED.equals(action))) {
229 Log.wtf(TAG, "placeCall: unexpected intent action " + action);
230 throw new IllegalArgumentException("Unexpected action: " + action);
231 }
232
233 // Check to see if this is an OTASP call (the "activation" call
234 // used to provision CDMA devices), and if so, do some
235 // OTASP-specific setup.
236 Phone phone = mApp.mCM.getDefaultPhone();
237 if (TelephonyCapabilities.supportsOtasp(phone)) {
238 checkForOtaspCall(intent);
239 }
240
241 // Clear out the "restore mute state" flag since we're
242 // initiating a brand-new call.
243 //
244 // (This call to setRestoreMuteOnInCallResume(false) informs the
245 // phone app that we're dealing with a new connection
246 // (i.e. placing an outgoing call, and NOT handling an aborted
247 // "Add Call" request), so we should let the mute state be handled
248 // by the PhoneUtils phone state change handler.)
249 mApp.setRestoreMuteOnInCallResume(false);
250
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251 CallStatusCode status = placeCallInternal(intent);
252
253 switch (status) {
254 // Call was placed successfully:
255 case SUCCESS:
256 case EXITED_ECM:
257 if (DBG) log("==> placeCall(): success from placeCallInternal(): " + status);
258
259 if (status == CallStatusCode.EXITED_ECM) {
260 // Call succeeded, but we also need to tell the
261 // InCallScreen to show the "Exiting ECM" warning.
262 inCallUiState.setPendingCallStatusCode(CallStatusCode.EXITED_ECM);
263 } else {
264 // Call succeeded. There's no "error condition" that
265 // needs to be displayed to the user, so clear out the
266 // InCallUiState's "pending call status code".
267 inCallUiState.clearPendingCallStatusCode();
268 }
269
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700270 break;
271
272 default:
273 // Any other status code is a failure.
274 log("==> placeCall(): failure code from placeCallInternal(): " + status);
275 // Handle the various error conditions that can occur when
276 // initiating an outgoing call, typically by directing the
277 // InCallScreen to display a diagnostic message (via the
278 // "pending call status code" flag.)
279 handleOutgoingCallError(status);
280 break;
281 }
282
283 // Finally, regardless of whether we successfully initiated the
284 // outgoing call or not, force the InCallScreen to come to the
285 // foreground.
286 //
287 // (For successful calls the the user will just see the normal
288 // in-call UI. Or if there was an error, the InCallScreen will
289 // notice the InCallUiState pending call status code flag and display an
290 // error indication instead.)
291
292 // TODO: double-check the behavior of mApp.displayCallScreen()
293 // if the InCallScreen is already visible:
294 // - make sure it forces the UI to refresh
295 // - make sure it does NOT launch a new InCallScreen on top
296 // of the current one (i.e. the Back button should not take
297 // you back to the previous InCallScreen)
298 // - it's probably OK to go thru a fresh pause/resume sequence
299 // though (since that should be fast now)
300 // - if necessary, though, maybe PhoneApp.displayCallScreen()
301 // could notice that the InCallScreen is already in the foreground,
302 // and if so simply call updateInCallScreen() instead.
303
304 mApp.displayCallScreen();
305 }
306
307 /**
308 * Actually make a call to whomever the intent tells us to.
309 *
310 * Note that there's no need to explicitly update (or refresh) the
311 * in-call UI at any point in this method, since a fresh InCallScreen
312 * instance will be launched automatically after we return (see
313 * placeCall() above.)
314 *
315 * @param intent the CALL intent describing whom to call
316 * @return CallStatusCode.SUCCESS if we successfully initiated an
317 * outgoing call. If there was some kind of failure, return one of
318 * the other CallStatusCode codes indicating what went wrong.
319 */
320 private CallStatusCode placeCallInternal(Intent intent) {
321 if (DBG) log("placeCallInternal()... intent = " + intent);
322
323 // TODO: This method is too long. Break it down into more
324 // manageable chunks.
325
326 final InCallUiState inCallUiState = mApp.inCallUiState;
327 final Uri uri = intent.getData();
328 final String scheme = (uri != null) ? uri.getScheme() : null;
329 String number;
330 Phone phone = null;
331
332 // Check the current ServiceState to make sure it's OK
333 // to even try making a call.
334 CallStatusCode okToCallStatus = checkIfOkToInitiateOutgoingCall(
335 mCM.getServiceState());
336
337 // TODO: Streamline the logic here. Currently, the code is
338 // unchanged from its original form in InCallScreen.java. But we
339 // should fix a couple of things:
340 // - Don't call checkIfOkToInitiateOutgoingCall() more than once
341 // - Wrap the try/catch for VoiceMailNumberMissingException
342 // around *only* the call that can throw that exception.
343
344 try {
345 number = PhoneUtils.getInitialNumber(intent);
346 if (VDBG) log("- actual number to dial: '" + number + "'");
347
348 // find the phone first
349 // TODO Need a way to determine which phone to place the call
350 // It could be determined by SIP setting, i.e. always,
351 // or by number, i.e. for international,
352 // or by user selection, i.e., dialog query,
353 // or any of combinations
354 String sipPhoneUri = intent.getStringExtra(
355 OutgoingCallBroadcaster.EXTRA_SIP_PHONE_URI);
356 phone = PhoneUtils.pickPhoneBasedOnNumber(mCM, scheme, number, sipPhoneUri);
357 if (VDBG) log("- got Phone instance: " + phone + ", class = " + phone.getClass());
358
359 // update okToCallStatus based on new phone
360 okToCallStatus = checkIfOkToInitiateOutgoingCall(
361 phone.getServiceState().getState());
362
363 } catch (PhoneUtils.VoiceMailNumberMissingException ex) {
364 // If the call status is NOT in an acceptable state, it
365 // may effect the way the voicemail number is being
366 // retrieved. Mask the VoiceMailNumberMissingException
367 // with the underlying issue of the phone state.
368 if (okToCallStatus != CallStatusCode.SUCCESS) {
369 if (DBG) log("Voicemail number not reachable in current SIM card state.");
370 return okToCallStatus;
371 }
372 if (DBG) log("VoiceMailNumberMissingException from getInitialNumber()");
373 return CallStatusCode.VOICEMAIL_NUMBER_MISSING;
374 }
375
376 if (number == null) {
377 Log.w(TAG, "placeCall: couldn't get a phone number from Intent " + intent);
378 return CallStatusCode.NO_PHONE_NUMBER_SUPPLIED;
379 }
380
381
382 // Sanity-check that ACTION_CALL_EMERGENCY is used if and only if
383 // this is a call to an emergency number
384 // (This is just a sanity-check; this policy *should* really be
385 // enforced in OutgoingCallBroadcaster.onCreate(), which is the
386 // main entry point for the CALL and CALL_* intents.)
387 boolean isEmergencyNumber = PhoneNumberUtils.isLocalEmergencyNumber(number, mApp);
388 boolean isPotentialEmergencyNumber =
389 PhoneNumberUtils.isPotentialLocalEmergencyNumber(number, mApp);
390 boolean isEmergencyIntent = Intent.ACTION_CALL_EMERGENCY.equals(intent.getAction());
391
392 if (isPotentialEmergencyNumber && !isEmergencyIntent) {
393 Log.e(TAG, "Non-CALL_EMERGENCY Intent " + intent
394 + " attempted to call potential emergency number " + number
395 + ".");
396 return CallStatusCode.CALL_FAILED;
397 } else if (!isPotentialEmergencyNumber && isEmergencyIntent) {
398 Log.e(TAG, "Received CALL_EMERGENCY Intent " + intent
399 + " with non-potential-emergency number " + number
400 + " -- failing call.");
401 return CallStatusCode.CALL_FAILED;
402 }
403
404 // If we're trying to call an emergency number, then it's OK to
405 // proceed in certain states where we'd otherwise bring up
406 // an error dialog:
407 // - If we're in EMERGENCY_ONLY mode, then (obviously) you're allowed
408 // to dial emergency numbers.
409 // - If we're OUT_OF_SERVICE, we still attempt to make a call,
410 // since the radio will register to any available network.
411
412 if (isEmergencyNumber
413 && ((okToCallStatus == CallStatusCode.EMERGENCY_ONLY)
414 || (okToCallStatus == CallStatusCode.OUT_OF_SERVICE))) {
415 if (DBG) log("placeCall: Emergency number detected with status = " + okToCallStatus);
416 okToCallStatus = CallStatusCode.SUCCESS;
417 if (DBG) log("==> UPDATING status to: " + okToCallStatus);
418 }
419
420 if (okToCallStatus != CallStatusCode.SUCCESS) {
421 // If this is an emergency call, launch the EmergencyCallHelperService
422 // to turn on the radio and retry the call.
423 if (isEmergencyNumber && (okToCallStatus == CallStatusCode.POWER_OFF)) {
424 Log.i(TAG, "placeCall: Trying to make emergency call while POWER_OFF!");
425
426 // If needed, lazily instantiate an EmergencyCallHelper instance.
427 synchronized (this) {
428 if (mEmergencyCallHelper == null) {
429 mEmergencyCallHelper = new EmergencyCallHelper(this);
430 }
431 }
432
433 // ...and kick off the "emergency call from airplane mode" sequence.
434 mEmergencyCallHelper.startEmergencyCallFromAirplaneModeSequence(number);
435
436 // Finally, return CallStatusCode.SUCCESS right now so
437 // that the in-call UI will remain visible (in order to
438 // display the progress indication.)
439 // TODO: or maybe it would be more clear to return a whole
440 // new CallStatusCode called "TURNING_ON_RADIO" here.
441 // That way, we'd update inCallUiState.progressIndication from
442 // the handleOutgoingCallError() method, rather than here.
443 return CallStatusCode.SUCCESS;
444 } else {
445 // Otherwise, just return the (non-SUCCESS) status code
446 // back to our caller.
447 if (DBG) log("==> placeCallInternal(): non-success status: " + okToCallStatus);
448
449 // Log failed call.
450 // Note: Normally, many of these values we gather from the Connection object but
451 // since no such object is created for unconnected calls, we have to build them
452 // manually.
453 // TODO(santoscordon): Try to restructure code so that we can handle failure-
454 // condition call logging in a single place (placeCall()) that also has access to
455 // the number we attempted to dial (not placeCall()).
456 mCallLogger.logCall(null /* callerInfo */, number, 0 /* presentation */,
457 Calls.OUTGOING_TYPE, System.currentTimeMillis(), 0 /* duration */);
458
459 return okToCallStatus;
460 }
461 }
462
463 // Ok, we can proceed with this outgoing call.
464
465 // Reset some InCallUiState flags, just in case they're still set
466 // from a prior call.
467 inCallUiState.needToShowCallLostDialog = false;
468 inCallUiState.clearProgressIndication();
469
470 // We have a valid number, so try to actually place a call:
471 // make sure we pass along the intent's URI which is a
472 // reference to the contact. We may have a provider gateway
473 // phone number to use for the outgoing call.
474 Uri contactUri = intent.getData();
475
Santos Cordon69a69192013-08-22 14:25:42 -0700476 // If a gateway is used, extract the data here and pass that into placeCall.
477 final RawGatewayInfo rawGatewayInfo = mCallGatewayManager.getRawGatewayInfo(intent, number);
478
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700479 // Watch out: PhoneUtils.placeCall() returns one of the
480 // CALL_STATUS_* constants, not a CallStatusCode enum value.
481 int callStatus = PhoneUtils.placeCall(mApp,
482 phone,
483 number,
484 contactUri,
485 (isEmergencyNumber || isEmergencyIntent),
Santos Cordon69a69192013-08-22 14:25:42 -0700486 rawGatewayInfo,
487 mCallGatewayManager);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700488
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}