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