blob: 1da6d365b76e8f247c1121c704fa3c64fb1ad3c3 [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;
Sailesh Nepalbfb68322013-11-07 14:07:41 -080029import android.content.ComponentName;
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
Santos Cordonad1ed6d2013-09-16 03:04:23 -0700147 mApp.getCallModeler().setCdmaOutgoing3WayCall(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148 }
149 break;
150
151 default:
152 Log.wtf(TAG, "handleMessage: unexpected code: " + msg);
153 break;
154 }
155 }
156
157 //
158 // Outgoing call sequence
159 //
160
161 /**
162 * Initiate an outgoing call.
163 *
164 * Here's the most typical outgoing call sequence:
165 *
166 * (1) OutgoingCallBroadcaster receives a CALL intent and sends the
167 * NEW_OUTGOING_CALL broadcast
168 *
169 * (2) The broadcast finally reaches OutgoingCallReceiver, which stashes
170 * away a copy of the original CALL intent and launches
171 * SipCallOptionHandler
172 *
173 * (3) SipCallOptionHandler decides whether this is a PSTN or SIP call (and
174 * in some cases brings up a dialog to let the user choose), and
175 * ultimately calls CallController.placeCall() (from the
176 * setResultAndFinish() method) with the stashed-away intent from step
177 * (2) as the "intent" parameter.
178 *
179 * (4) Here in CallController.placeCall() we read the phone number or SIP
180 * address out of the intent and actually initiate the call, and
181 * simultaneously launch the InCallScreen to display the in-call UI.
182 *
183 * (5) We handle various errors by directing the InCallScreen to
184 * display error messages or dialogs (via the InCallUiState
185 * "pending call status code" flag), and in some cases we also
186 * sometimes continue working in the background to resolve the
187 * problem (like in the case of an emergency call while in
188 * airplane mode). Any time that some onscreen indication to the
189 * user needs to change, we update the "status dialog" info in
190 * the inCallUiState and (re)launch the InCallScreen to make sure
191 * it's visible.
192 */
193 public void placeCall(Intent intent) {
194 log("placeCall()... intent = " + intent);
195 if (VDBG) log(" extras = " + intent.getExtras());
196
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700197 // TODO: Do we need to hold a wake lock while this method runs?
198 // Or did we already acquire one somewhere earlier
199 // in this sequence (like when we first received the CALL intent?)
200
201 if (intent == null) {
202 Log.wtf(TAG, "placeCall: called with null intent");
203 throw new IllegalArgumentException("placeCall: called with null intent");
204 }
205
206 String action = intent.getAction();
207 Uri uri = intent.getData();
208 if (uri == null) {
209 Log.wtf(TAG, "placeCall: intent had no data");
210 throw new IllegalArgumentException("placeCall: intent had no data");
211 }
212
213 String scheme = uri.getScheme();
214 String number = PhoneNumberUtils.getNumberFromIntent(intent, mApp);
215 if (VDBG) {
216 log("- action: " + action);
217 log("- uri: " + uri);
218 log("- scheme: " + scheme);
219 log("- number: " + number);
220 }
221
222 // This method should only be used with the various flavors of CALL
223 // intents. (It doesn't make sense for any other action to trigger an
224 // outgoing call!)
225 if (!(Intent.ACTION_CALL.equals(action)
226 || Intent.ACTION_CALL_EMERGENCY.equals(action)
227 || Intent.ACTION_CALL_PRIVILEGED.equals(action))) {
228 Log.wtf(TAG, "placeCall: unexpected intent action " + action);
229 throw new IllegalArgumentException("Unexpected action: " + action);
230 }
231
232 // Check to see if this is an OTASP call (the "activation" call
233 // used to provision CDMA devices), and if so, do some
234 // OTASP-specific setup.
235 Phone phone = mApp.mCM.getDefaultPhone();
236 if (TelephonyCapabilities.supportsOtasp(phone)) {
237 checkForOtaspCall(intent);
238 }
239
240 // Clear out the "restore mute state" flag since we're
241 // initiating a brand-new call.
242 //
243 // (This call to setRestoreMuteOnInCallResume(false) informs the
244 // phone app that we're dealing with a new connection
245 // (i.e. placing an outgoing call, and NOT handling an aborted
246 // "Add Call" request), so we should let the mute state be handled
247 // by the PhoneUtils phone state change handler.)
248 mApp.setRestoreMuteOnInCallResume(false);
249
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700250 CallStatusCode status = placeCallInternal(intent);
251
252 switch (status) {
253 // Call was placed successfully:
254 case SUCCESS:
255 case EXITED_ECM:
256 if (DBG) log("==> placeCall(): success from placeCallInternal(): " + status);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257 break;
258
259 default:
260 // Any other status code is a failure.
261 log("==> placeCall(): failure code from placeCallInternal(): " + status);
262 // Handle the various error conditions that can occur when
263 // initiating an outgoing call, typically by directing the
264 // InCallScreen to display a diagnostic message (via the
265 // "pending call status code" flag.)
266 handleOutgoingCallError(status);
267 break;
268 }
269
270 // Finally, regardless of whether we successfully initiated the
271 // outgoing call or not, force the InCallScreen to come to the
272 // foreground.
273 //
274 // (For successful calls the the user will just see the normal
275 // in-call UI. Or if there was an error, the InCallScreen will
276 // notice the InCallUiState pending call status code flag and display an
277 // error indication instead.)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700278 }
279
280 /**
281 * Actually make a call to whomever the intent tells us to.
282 *
283 * Note that there's no need to explicitly update (or refresh) the
284 * in-call UI at any point in this method, since a fresh InCallScreen
285 * instance will be launched automatically after we return (see
286 * placeCall() above.)
287 *
288 * @param intent the CALL intent describing whom to call
289 * @return CallStatusCode.SUCCESS if we successfully initiated an
290 * outgoing call. If there was some kind of failure, return one of
291 * the other CallStatusCode codes indicating what went wrong.
292 */
293 private CallStatusCode placeCallInternal(Intent intent) {
294 if (DBG) log("placeCallInternal()... intent = " + intent);
295
296 // TODO: This method is too long. Break it down into more
297 // manageable chunks.
298
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 final Uri uri = intent.getData();
300 final String scheme = (uri != null) ? uri.getScheme() : null;
301 String number;
302 Phone phone = null;
303
304 // Check the current ServiceState to make sure it's OK
305 // to even try making a call.
306 CallStatusCode okToCallStatus = checkIfOkToInitiateOutgoingCall(
307 mCM.getServiceState());
308
309 // TODO: Streamline the logic here. Currently, the code is
310 // unchanged from its original form in InCallScreen.java. But we
311 // should fix a couple of things:
312 // - Don't call checkIfOkToInitiateOutgoingCall() more than once
313 // - Wrap the try/catch for VoiceMailNumberMissingException
314 // around *only* the call that can throw that exception.
315
316 try {
317 number = PhoneUtils.getInitialNumber(intent);
318 if (VDBG) log("- actual number to dial: '" + number + "'");
319
320 // find the phone first
321 // TODO Need a way to determine which phone to place the call
322 // It could be determined by SIP setting, i.e. always,
323 // or by number, i.e. for international,
324 // or by user selection, i.e., dialog query,
325 // or any of combinations
326 String sipPhoneUri = intent.getStringExtra(
327 OutgoingCallBroadcaster.EXTRA_SIP_PHONE_URI);
Sailesh Nepalbfb68322013-11-07 14:07:41 -0800328 ComponentName thirdPartyCallComponent = (ComponentName) intent.getParcelableExtra(
329 OutgoingCallBroadcaster.EXTRA_THIRD_PARTY_CALL_COMPONENT);
330 phone = PhoneUtils.pickPhoneBasedOnNumber(mCM, scheme, number, sipPhoneUri,
331 thirdPartyCallComponent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332 if (VDBG) log("- got Phone instance: " + phone + ", class = " + phone.getClass());
333
334 // update okToCallStatus based on new phone
335 okToCallStatus = checkIfOkToInitiateOutgoingCall(
336 phone.getServiceState().getState());
337
338 } catch (PhoneUtils.VoiceMailNumberMissingException ex) {
339 // If the call status is NOT in an acceptable state, it
340 // may effect the way the voicemail number is being
341 // retrieved. Mask the VoiceMailNumberMissingException
342 // with the underlying issue of the phone state.
343 if (okToCallStatus != CallStatusCode.SUCCESS) {
344 if (DBG) log("Voicemail number not reachable in current SIM card state.");
345 return okToCallStatus;
346 }
347 if (DBG) log("VoiceMailNumberMissingException from getInitialNumber()");
348 return CallStatusCode.VOICEMAIL_NUMBER_MISSING;
349 }
350
351 if (number == null) {
352 Log.w(TAG, "placeCall: couldn't get a phone number from Intent " + intent);
353 return CallStatusCode.NO_PHONE_NUMBER_SUPPLIED;
354 }
355
356
357 // Sanity-check that ACTION_CALL_EMERGENCY is used if and only if
358 // this is a call to an emergency number
359 // (This is just a sanity-check; this policy *should* really be
360 // enforced in OutgoingCallBroadcaster.onCreate(), which is the
361 // main entry point for the CALL and CALL_* intents.)
362 boolean isEmergencyNumber = PhoneNumberUtils.isLocalEmergencyNumber(number, mApp);
363 boolean isPotentialEmergencyNumber =
364 PhoneNumberUtils.isPotentialLocalEmergencyNumber(number, mApp);
365 boolean isEmergencyIntent = Intent.ACTION_CALL_EMERGENCY.equals(intent.getAction());
366
367 if (isPotentialEmergencyNumber && !isEmergencyIntent) {
368 Log.e(TAG, "Non-CALL_EMERGENCY Intent " + intent
369 + " attempted to call potential emergency number " + number
370 + ".");
371 return CallStatusCode.CALL_FAILED;
372 } else if (!isPotentialEmergencyNumber && isEmergencyIntent) {
373 Log.e(TAG, "Received CALL_EMERGENCY Intent " + intent
374 + " with non-potential-emergency number " + number
375 + " -- failing call.");
376 return CallStatusCode.CALL_FAILED;
377 }
378
379 // If we're trying to call an emergency number, then it's OK to
380 // proceed in certain states where we'd otherwise bring up
381 // an error dialog:
382 // - If we're in EMERGENCY_ONLY mode, then (obviously) you're allowed
383 // to dial emergency numbers.
384 // - If we're OUT_OF_SERVICE, we still attempt to make a call,
385 // since the radio will register to any available network.
386
387 if (isEmergencyNumber
388 && ((okToCallStatus == CallStatusCode.EMERGENCY_ONLY)
389 || (okToCallStatus == CallStatusCode.OUT_OF_SERVICE))) {
390 if (DBG) log("placeCall: Emergency number detected with status = " + okToCallStatus);
391 okToCallStatus = CallStatusCode.SUCCESS;
392 if (DBG) log("==> UPDATING status to: " + okToCallStatus);
393 }
394
395 if (okToCallStatus != CallStatusCode.SUCCESS) {
396 // If this is an emergency call, launch the EmergencyCallHelperService
397 // to turn on the radio and retry the call.
398 if (isEmergencyNumber && (okToCallStatus == CallStatusCode.POWER_OFF)) {
399 Log.i(TAG, "placeCall: Trying to make emergency call while POWER_OFF!");
400
401 // If needed, lazily instantiate an EmergencyCallHelper instance.
402 synchronized (this) {
403 if (mEmergencyCallHelper == null) {
404 mEmergencyCallHelper = new EmergencyCallHelper(this);
405 }
406 }
407
408 // ...and kick off the "emergency call from airplane mode" sequence.
409 mEmergencyCallHelper.startEmergencyCallFromAirplaneModeSequence(number);
410
411 // Finally, return CallStatusCode.SUCCESS right now so
412 // that the in-call UI will remain visible (in order to
413 // display the progress indication.)
414 // TODO: or maybe it would be more clear to return a whole
415 // new CallStatusCode called "TURNING_ON_RADIO" here.
416 // That way, we'd update inCallUiState.progressIndication from
417 // the handleOutgoingCallError() method, rather than here.
418 return CallStatusCode.SUCCESS;
419 } else {
420 // Otherwise, just return the (non-SUCCESS) status code
421 // back to our caller.
422 if (DBG) log("==> placeCallInternal(): non-success status: " + okToCallStatus);
423
424 // Log failed call.
425 // Note: Normally, many of these values we gather from the Connection object but
426 // since no such object is created for unconnected calls, we have to build them
427 // manually.
428 // TODO(santoscordon): Try to restructure code so that we can handle failure-
429 // condition call logging in a single place (placeCall()) that also has access to
430 // the number we attempted to dial (not placeCall()).
431 mCallLogger.logCall(null /* callerInfo */, number, 0 /* presentation */,
432 Calls.OUTGOING_TYPE, System.currentTimeMillis(), 0 /* duration */);
433
434 return okToCallStatus;
435 }
436 }
437
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700438 // We have a valid number, so try to actually place a call:
439 // make sure we pass along the intent's URI which is a
440 // reference to the contact. We may have a provider gateway
441 // phone number to use for the outgoing call.
442 Uri contactUri = intent.getData();
443
Santos Cordon69a69192013-08-22 14:25:42 -0700444 // If a gateway is used, extract the data here and pass that into placeCall.
445 final RawGatewayInfo rawGatewayInfo = mCallGatewayManager.getRawGatewayInfo(intent, number);
446
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700447 // Watch out: PhoneUtils.placeCall() returns one of the
448 // CALL_STATUS_* constants, not a CallStatusCode enum value.
449 int callStatus = PhoneUtils.placeCall(mApp,
450 phone,
451 number,
452 contactUri,
453 (isEmergencyNumber || isEmergencyIntent),
Santos Cordon69a69192013-08-22 14:25:42 -0700454 rawGatewayInfo,
455 mCallGatewayManager);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700456
457 switch (callStatus) {
458 case PhoneUtils.CALL_STATUS_DIALED:
459 if (VDBG) log("placeCall: PhoneUtils.placeCall() succeeded for regular call '"
460 + number + "'.");
461
462
463 // TODO(OTASP): still need more cleanup to simplify the mApp.cdma*State objects:
464 // - Rather than checking inCallUiState.inCallScreenMode, the
465 // code here could also check for
466 // app.getCdmaOtaInCallScreenUiState() returning NORMAL.
467 // - But overall, app.inCallUiState.inCallScreenMode and
468 // app.cdmaOtaInCallScreenUiState.state are redundant.
469 // Combine them.
470
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700471 boolean voicemailUriSpecified = scheme != null && scheme.equals("voicemail");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700472 // Check for an obscure ECM-related scenario: If the phone
473 // is currently in ECM (Emergency callback mode) and we
474 // dial a non-emergency number, that automatically
475 // *cancels* ECM. So warn the user about it.
476 // (See InCallScreen.showExitingECMDialog() for more info.)
477 boolean exitedEcm = false;
478 if (PhoneUtils.isPhoneInEcm(phone) && !isEmergencyNumber) {
479 Log.i(TAG, "About to exit ECM because of an outgoing non-emergency call");
480 exitedEcm = true; // this will cause us to return EXITED_ECM from this method
481 }
482
483 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
484 // Start the timer for 3 Way CallerInfo
485 if (mApp.cdmaPhoneCallState.getCurrentCallState()
486 == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) {
487 //Unmute for the second MO call
488 PhoneUtils.setMute(false);
489
490 // This is a "CDMA 3-way call", which means that you're dialing a
491 // 2nd outgoing call while a previous call is already in progress.
492 //
493 // Due to the limitations of CDMA this call doesn't actually go
494 // through the DIALING/ALERTING states, so we can't tell for sure
495 // when (or if) it's actually answered. But we want to show
496 // *some* indication of what's going on in the UI, so we "fake it"
497 // by displaying the "Dialing" state for 3 seconds.
498
499 // Set the mThreeWayCallOrigStateDialing state to true
500 mApp.cdmaPhoneCallState.setThreeWayCallOrigState(true);
501
502 // Schedule the "Dialing" indication to be taken down in 3 seconds:
503 sendEmptyMessageDelayed(THREEWAY_CALLERINFO_DISPLAY_DONE,
504 THREEWAY_CALLERINFO_DISPLAY_TIME);
505 }
506 }
507
508 // Success!
509 if (exitedEcm) {
510 return CallStatusCode.EXITED_ECM;
511 } else {
512 return CallStatusCode.SUCCESS;
513 }
514
515 case PhoneUtils.CALL_STATUS_DIALED_MMI:
516 if (DBG) log("placeCall: specified number was an MMI code: '" + number + "'.");
517 // The passed-in number was an MMI code, not a regular phone number!
518 // This isn't really a failure; the Dialer may have deliberately
519 // fired an ACTION_CALL intent to dial an MMI code, like for a
520 // USSD call.
521 //
522 // Presumably an MMI_INITIATE message will come in shortly
523 // (and we'll bring up the "MMI Started" dialog), or else
524 // an MMI_COMPLETE will come in (which will take us to a
525 // different Activity; see PhoneUtils.displayMMIComplete()).
526 return CallStatusCode.DIALED_MMI;
527
528 case PhoneUtils.CALL_STATUS_FAILED:
529 Log.w(TAG, "placeCall: PhoneUtils.placeCall() FAILED for number '"
530 + number + "'.");
531 // We couldn't successfully place the call; there was some
532 // failure in the telephony layer.
533
534 // Log failed call.
535 mCallLogger.logCall(null /* callerInfo */, number, 0 /* presentation */,
536 Calls.OUTGOING_TYPE, System.currentTimeMillis(), 0 /* duration */);
537
538 return CallStatusCode.CALL_FAILED;
539
540 default:
541 Log.wtf(TAG, "placeCall: unknown callStatus " + callStatus
542 + " from PhoneUtils.placeCall() for number '" + number + "'.");
543 return CallStatusCode.SUCCESS; // Try to continue anyway...
544 }
545 }
546
547 /**
548 * Checks the current ServiceState to make sure it's OK
549 * to try making an outgoing call to the specified number.
550 *
551 * @return CallStatusCode.SUCCESS if it's OK to try calling the specified
552 * number. If not, like if the radio is powered off or we have no
553 * signal, return one of the other CallStatusCode codes indicating what
554 * the problem is.
555 */
556 private CallStatusCode checkIfOkToInitiateOutgoingCall(int state) {
557 if (VDBG) log("checkIfOkToInitiateOutgoingCall: ServiceState = " + state);
558
559 switch (state) {
560 case ServiceState.STATE_IN_SERVICE:
561 // Normal operation. It's OK to make outgoing calls.
562 return CallStatusCode.SUCCESS;
563
564 case ServiceState.STATE_POWER_OFF:
565 // Radio is explictly powered off.
566 return CallStatusCode.POWER_OFF;
567
568 case ServiceState.STATE_EMERGENCY_ONLY:
569 // The phone is registered, but locked. Only emergency
570 // numbers are allowed.
571 // Note that as of Android 2.0 at least, the telephony layer
572 // does not actually use ServiceState.STATE_EMERGENCY_ONLY,
573 // mainly since there's no guarantee that the radio/RIL can
574 // make this distinction. So in practice the
575 // CallStatusCode.EMERGENCY_ONLY state and the string
576 // "incall_error_emergency_only" are totally unused.
577 return CallStatusCode.EMERGENCY_ONLY;
578
579 case ServiceState.STATE_OUT_OF_SERVICE:
580 // No network connection.
581 return CallStatusCode.OUT_OF_SERVICE;
582
583 default:
584 throw new IllegalStateException("Unexpected ServiceState: " + state);
585 }
586 }
587
588
589
590 /**
591 * Handles the various error conditions that can occur when initiating
592 * an outgoing call.
593 *
594 * Most error conditions are "handled" by simply displaying an error
Yorke Lee71028d02013-08-28 12:24:01 -0700595 * message to the user.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700596 *
597 * @param status one of the CallStatusCode error codes.
598 */
599 private void handleOutgoingCallError(CallStatusCode status) {
600 if (DBG) log("handleOutgoingCallError(): status = " + status);
Yorke Lee71028d02013-08-28 12:24:01 -0700601 final Intent intent = new Intent(mApp, ErrorDialogActivity.class);
602 int errorMessageId = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700603 switch (status) {
604 case SUCCESS:
605 // This case shouldn't happen; you're only supposed to call
606 // handleOutgoingCallError() if there was actually an error!
607 Log.wtf(TAG, "handleOutgoingCallError: SUCCESS isn't an error");
608 break;
609
Yorke Lee71028d02013-08-28 12:24:01 -0700610 case CALL_FAILED:
611 // We couldn't successfully place the call; there was some
612 // failure in the telephony layer.
613 // TODO: Need UI spec for this failure case; for now just
614 // show a generic error.
615 errorMessageId = R.string.incall_error_call_failed;
616 break;
617 case POWER_OFF:
618 // Radio is explictly powered off, presumably because the
619 // device is in airplane mode.
620 //
621 // TODO: For now this UI is ultra-simple: we simply display
622 // a message telling the user to turn off airplane mode.
623 // But it might be nicer for the dialog to offer the option
624 // to turn the radio on right there (and automatically retry
625 // the call once network registration is complete.)
626 errorMessageId = R.string.incall_error_power_off;
627 break;
628 case EMERGENCY_ONLY:
629 // Only emergency numbers are allowed, but we tried to dial
630 // a non-emergency number.
631 // (This state is currently unused; see comments above.)
632 errorMessageId = R.string.incall_error_emergency_only;
633 break;
634 case OUT_OF_SERVICE:
635 // No network connection.
636 errorMessageId = R.string.incall_error_out_of_service;
637 break;
638 case NO_PHONE_NUMBER_SUPPLIED:
639 // The supplied Intent didn't contain a valid phone number.
640 // (This is rare and should only ever happen with broken
641 // 3rd-party apps.) For now just show a generic error.
642 errorMessageId = R.string.incall_error_no_phone_number_supplied;
643 break;
644
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700645 case VOICEMAIL_NUMBER_MISSING:
646 // Bring up the "Missing Voicemail Number" dialog, which
647 // will ultimately take us to some other Activity (or else
648 // just bail out of this activity.)
649
650 // Send a request to the InCallScreen to display the
651 // "voicemail missing" dialog when it (the InCallScreen)
652 // comes to the foreground.
Yorke Lee71028d02013-08-28 12:24:01 -0700653 intent.putExtra(ErrorDialogActivity.SHOW_MISSING_VOICEMAIL_NO_DIALOG_EXTRA, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700654 break;
655
656 case DIALED_MMI:
657 // Our initial phone number was actually an MMI sequence.
658 // There's no real "error" here, but we do bring up the
659 // a Toast (as requested of the New UI paradigm).
660 //
661 // In-call MMIs do not trigger the normal MMI Initiate
662 // Notifications, so we should notify the user here.
663 // Otherwise, the code in PhoneUtils.java should handle
664 // user notifications in the form of Toasts or Dialogs.
665 //
666 // TODO: Rather than launching a toast from here, it would
667 // be cleaner to just set a pending call status code here,
668 // and then let the InCallScreen display the toast...
669 if (mCM.getState() == PhoneConstants.State.OFFHOOK) {
670 Toast.makeText(mApp, R.string.incall_status_dialed_mmi, Toast.LENGTH_SHORT)
671 .show();
672 }
Yorke Lee71028d02013-08-28 12:24:01 -0700673 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700674
675 default:
676 Log.wtf(TAG, "handleOutgoingCallError: unexpected status code " + status);
677 // Show a generic "call failed" error.
Yorke Lee71028d02013-08-28 12:24:01 -0700678 errorMessageId = R.string.incall_error_call_failed;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700679 break;
680 }
Yorke Lee71028d02013-08-28 12:24:01 -0700681 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
682 if (errorMessageId != -1) {
683 intent.putExtra(ErrorDialogActivity.ERROR_MESSAGE_ID_EXTRA, errorMessageId);
684 }
685 mApp.startActivity(intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700686 }
687
688 /**
689 * Checks the current outgoing call to see if it's an OTASP call (the
690 * "activation" call used to provision CDMA devices). If so, do any
691 * necessary OTASP-specific setup before actually placing the call.
692 */
693 private void checkForOtaspCall(Intent intent) {
694 if (OtaUtils.isOtaspCallIntent(intent)) {
695 Log.i(TAG, "checkForOtaspCall: handling OTASP intent! " + intent);
696
697 // ("OTASP-specific setup" basically means creating and initializing
698 // the OtaUtils instance. Note that this setup needs to be here in
699 // the CallController.placeCall() sequence, *not* in
700 // OtaUtils.startInteractiveOtasp(), since it's also possible to
701 // start an OTASP call by manually dialing "*228" (in which case
702 // OtaUtils.startInteractiveOtasp() never gets run at all.)
703 OtaUtils.setupOtaspCall(intent);
704 } else {
705 if (DBG) log("checkForOtaspCall: not an OTASP call.");
706 }
707 }
708
709
710 //
711 // Debugging
712 //
713
714 private static void log(String msg) {
715 Log.d(TAG, msg);
716 }
717}