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