blob: 3af4d7d86640c67db4185a124bf1e5f7ec177245 [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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025
Sailesh Nepalbfb68322013-11-07 14:07:41 -080026import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.Intent;
28import android.net.Uri;
29import android.os.Handler;
30import android.os.Message;
31import android.os.SystemProperties;
32import android.provider.CallLog.Calls;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070033import android.telecom.PhoneAccount;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.telephony.PhoneNumberUtils;
35import android.telephony.ServiceState;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.util.Log;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037
38/**
39 * Phone app module in charge of "call control".
40 *
41 * This is a singleton object which acts as the interface to the telephony layer
42 * (and other parts of the Android framework) for all user-initiated telephony
43 * functionality, like making outgoing calls.
44 *
45 * This functionality includes things like:
46 * - actually running the placeCall() method and handling errors or retries
47 * - running the whole "emergency call in airplane mode" sequence
48 * - running the state machine of MMI sequences
49 * - restoring/resetting mute and speaker state when a new call starts
50 * - updating the prox sensor wake lock state
51 * - resolving what the voicemail: intent should mean (and making the call)
52 *
53 * The single CallController instance stays around forever; it's not tied
54 * to the lifecycle of any particular Activity (like the InCallScreen).
55 * There's also no implementation of onscreen UI here (that's all in InCallScreen).
56 *
57 * Note that this class does not handle asynchronous events from the telephony
58 * layer, like reacting to an incoming call; see CallNotifier for that. This
59 * class purely handles actions initiated by the user, like outgoing calls.
60 */
61public class CallController extends Handler {
62 private static final String TAG = "CallController";
63 private static final boolean DBG =
64 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
65 // Do not check in with VDBG = true, since that may write PII to the system log.
66 private static final boolean VDBG = false;
67
68 /** The singleton CallController instance. */
69 private static CallController sInstance;
70
Santos Cordon69a69192013-08-22 14:25:42 -070071 final private PhoneGlobals mApp;
72 final private CallManager mCM;
73 final private CallLogger mCallLogger;
74 final private CallGatewayManager mCallGatewayManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075
76 /** Helper object for emergency calls in some rare use cases. Created lazily. */
77 private EmergencyCallHelper mEmergencyCallHelper;
78
79
80 //
81 // Message codes; see handleMessage().
82 //
83
84 private static final int THREEWAY_CALLERINFO_DISPLAY_DONE = 1;
85
86
87 //
88 // Misc constants.
89 //
90
91 // Amount of time the UI should display "Dialing" when initiating a CDMA
92 // 3way call. (See comments on the THRWAY_ACTIVE case in
93 // placeCallInternal() for more info.)
94 private static final int THREEWAY_CALLERINFO_DISPLAY_TIME = 3000; // msec
95
96
97 /**
98 * Initialize the singleton CallController instance.
99 *
100 * This is only done once, at startup, from PhoneApp.onCreate().
101 * From then on, the CallController instance is available via the
102 * PhoneApp's public "callController" field, which is why there's no
103 * getInstance() method here.
104 */
Santos Cordon69a69192013-08-22 14:25:42 -0700105 /* package */ static CallController init(PhoneGlobals app, CallLogger callLogger,
106 CallGatewayManager callGatewayManager) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107 synchronized (CallController.class) {
108 if (sInstance == null) {
Santos Cordon69a69192013-08-22 14:25:42 -0700109 sInstance = new CallController(app, callLogger, callGatewayManager);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700110 } else {
111 Log.wtf(TAG, "init() called multiple times! sInstance = " + sInstance);
112 }
113 return sInstance;
114 }
115 }
116
117 /**
118 * Private constructor (this is a singleton).
119 * @see init()
120 */
Santos Cordon69a69192013-08-22 14:25:42 -0700121 private CallController(PhoneGlobals app, CallLogger callLogger,
122 CallGatewayManager callGatewayManager) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700123 if (DBG) log("CallController constructor: app = " + app);
124 mApp = app;
125 mCM = app.mCM;
126 mCallLogger = callLogger;
Santos Cordon69a69192013-08-22 14:25:42 -0700127 mCallGatewayManager = callGatewayManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128 }
129
130 @Override
131 public void handleMessage(Message msg) {
132 if (VDBG) log("handleMessage: " + msg);
133 switch (msg.what) {
134
135 case THREEWAY_CALLERINFO_DISPLAY_DONE:
136 if (DBG) log("THREEWAY_CALLERINFO_DISPLAY_DONE...");
137
138 if (mApp.cdmaPhoneCallState.getCurrentCallState()
139 == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) {
140 // Reset the mThreeWayCallOrigStateDialing state
141 mApp.cdmaPhoneCallState.setThreeWayCallOrigState(false);
142
Santos Cordonda120f42014-08-06 04:44:34 -0700143 // TODO: Remove this code.
Sailesh Nepal23d9ed72014-07-03 09:40:26 -0700144 //mApp.getCallModeler().setCdmaOutgoing3WayCall(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700145 }
146 break;
147
148 default:
149 Log.wtf(TAG, "handleMessage: unexpected code: " + msg);
150 break;
151 }
152 }
153
154 //
155 // Outgoing call sequence
156 //
157
158 /**
159 * Initiate an outgoing call.
160 *
161 * Here's the most typical outgoing call sequence:
162 *
163 * (1) OutgoingCallBroadcaster receives a CALL intent and sends the
164 * NEW_OUTGOING_CALL broadcast
165 *
166 * (2) The broadcast finally reaches OutgoingCallReceiver, which stashes
167 * away a copy of the original CALL intent and launches
168 * SipCallOptionHandler
169 *
170 * (3) SipCallOptionHandler decides whether this is a PSTN or SIP call (and
171 * in some cases brings up a dialog to let the user choose), and
172 * ultimately calls CallController.placeCall() (from the
173 * setResultAndFinish() method) with the stashed-away intent from step
174 * (2) as the "intent" parameter.
175 *
176 * (4) Here in CallController.placeCall() we read the phone number or SIP
177 * address out of the intent and actually initiate the call, and
178 * simultaneously launch the InCallScreen to display the in-call UI.
179 *
180 * (5) We handle various errors by directing the InCallScreen to
181 * display error messages or dialogs (via the InCallUiState
182 * "pending call status code" flag), and in some cases we also
183 * sometimes continue working in the background to resolve the
184 * problem (like in the case of an emergency call while in
185 * airplane mode). Any time that some onscreen indication to the
186 * user needs to change, we update the "status dialog" info in
187 * the inCallUiState and (re)launch the InCallScreen to make sure
188 * it's visible.
189 */
190 public void placeCall(Intent intent) {
191 log("placeCall()... intent = " + intent);
192 if (VDBG) log(" extras = " + intent.getExtras());
193
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 // TODO: Do we need to hold a wake lock while this method runs?
195 // Or did we already acquire one somewhere earlier
196 // in this sequence (like when we first received the CALL intent?)
197
198 if (intent == null) {
199 Log.wtf(TAG, "placeCall: called with null intent");
200 throw new IllegalArgumentException("placeCall: called with null intent");
201 }
202
203 String action = intent.getAction();
204 Uri uri = intent.getData();
205 if (uri == null) {
206 Log.wtf(TAG, "placeCall: intent had no data");
207 throw new IllegalArgumentException("placeCall: intent had no data");
208 }
209
210 String scheme = uri.getScheme();
211 String number = PhoneNumberUtils.getNumberFromIntent(intent, mApp);
212 if (VDBG) {
213 log("- action: " + action);
214 log("- uri: " + uri);
215 log("- scheme: " + scheme);
216 log("- number: " + number);
217 }
218
219 // This method should only be used with the various flavors of CALL
220 // intents. (It doesn't make sense for any other action to trigger an
221 // outgoing call!)
222 if (!(Intent.ACTION_CALL.equals(action)
223 || Intent.ACTION_CALL_EMERGENCY.equals(action)
224 || Intent.ACTION_CALL_PRIVILEGED.equals(action))) {
225 Log.wtf(TAG, "placeCall: unexpected intent action " + action);
226 throw new IllegalArgumentException("Unexpected action: " + action);
227 }
228
229 // Check to see if this is an OTASP call (the "activation" call
230 // used to provision CDMA devices), and if so, do some
231 // OTASP-specific setup.
232 Phone phone = mApp.mCM.getDefaultPhone();
233 if (TelephonyCapabilities.supportsOtasp(phone)) {
234 checkForOtaspCall(intent);
235 }
236
237 // Clear out the "restore mute state" flag since we're
238 // initiating a brand-new call.
239 //
240 // (This call to setRestoreMuteOnInCallResume(false) informs the
241 // phone app that we're dealing with a new connection
242 // (i.e. placing an outgoing call, and NOT handling an aborted
243 // "Add Call" request), so we should let the mute state be handled
244 // by the PhoneUtils phone state change handler.)
245 mApp.setRestoreMuteOnInCallResume(false);
246
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 CallStatusCode status = placeCallInternal(intent);
248
249 switch (status) {
250 // Call was placed successfully:
251 case SUCCESS:
252 case EXITED_ECM:
253 if (DBG) log("==> placeCall(): success from placeCallInternal(): " + status);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254 break;
255
256 default:
257 // Any other status code is a failure.
258 log("==> placeCall(): failure code from placeCallInternal(): " + status);
259 // Handle the various error conditions that can occur when
260 // initiating an outgoing call, typically by directing the
261 // InCallScreen to display a diagnostic message (via the
262 // "pending call status code" flag.)
263 handleOutgoingCallError(status);
264 break;
265 }
266
267 // Finally, regardless of whether we successfully initiated the
268 // outgoing call or not, force the InCallScreen to come to the
269 // foreground.
270 //
271 // (For successful calls the the user will just see the normal
272 // in-call UI. Or if there was an error, the InCallScreen will
273 // notice the InCallUiState pending call status code flag and display an
274 // error indication instead.)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700275 }
276
277 /**
278 * Actually make a call to whomever the intent tells us to.
279 *
280 * Note that there's no need to explicitly update (or refresh) the
281 * in-call UI at any point in this method, since a fresh InCallScreen
282 * instance will be launched automatically after we return (see
283 * placeCall() above.)
284 *
285 * @param intent the CALL intent describing whom to call
286 * @return CallStatusCode.SUCCESS if we successfully initiated an
287 * outgoing call. If there was some kind of failure, return one of
288 * the other CallStatusCode codes indicating what went wrong.
289 */
290 private CallStatusCode placeCallInternal(Intent intent) {
291 if (DBG) log("placeCallInternal()... intent = " + intent);
292
293 // TODO: This method is too long. Break it down into more
294 // manageable chunks.
295
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700296 final Uri uri = intent.getData();
297 final String scheme = (uri != null) ? uri.getScheme() : null;
298 String number;
299 Phone phone = null;
300
301 // Check the current ServiceState to make sure it's OK
302 // to even try making a call.
303 CallStatusCode okToCallStatus = checkIfOkToInitiateOutgoingCall(
304 mCM.getServiceState());
305
306 // TODO: Streamline the logic here. Currently, the code is
307 // unchanged from its original form in InCallScreen.java. But we
308 // should fix a couple of things:
309 // - Don't call checkIfOkToInitiateOutgoingCall() more than once
310 // - Wrap the try/catch for VoiceMailNumberMissingException
311 // around *only* the call that can throw that exception.
312
313 try {
314 number = PhoneUtils.getInitialNumber(intent);
315 if (VDBG) log("- actual number to dial: '" + number + "'");
316
317 // find the phone first
318 // TODO Need a way to determine which phone to place the call
319 // It could be determined by SIP setting, i.e. always,
320 // or by number, i.e. for international,
321 // or by user selection, i.e., dialog query,
322 // or any of combinations
323 String sipPhoneUri = intent.getStringExtra(
324 OutgoingCallBroadcaster.EXTRA_SIP_PHONE_URI);
Sailesh Nepalbfb68322013-11-07 14:07:41 -0800325 ComponentName thirdPartyCallComponent = (ComponentName) intent.getParcelableExtra(
326 OutgoingCallBroadcaster.EXTRA_THIRD_PARTY_CALL_COMPONENT);
327 phone = PhoneUtils.pickPhoneBasedOnNumber(mCM, scheme, number, sipPhoneUri,
328 thirdPartyCallComponent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329 if (VDBG) log("- got Phone instance: " + phone + ", class = " + phone.getClass());
330
331 // update okToCallStatus based on new phone
332 okToCallStatus = checkIfOkToInitiateOutgoingCall(
333 phone.getServiceState().getState());
334
335 } catch (PhoneUtils.VoiceMailNumberMissingException ex) {
336 // If the call status is NOT in an acceptable state, it
337 // may effect the way the voicemail number is being
338 // retrieved. Mask the VoiceMailNumberMissingException
339 // with the underlying issue of the phone state.
340 if (okToCallStatus != CallStatusCode.SUCCESS) {
341 if (DBG) log("Voicemail number not reachable in current SIM card state.");
342 return okToCallStatus;
343 }
344 if (DBG) log("VoiceMailNumberMissingException from getInitialNumber()");
345 return CallStatusCode.VOICEMAIL_NUMBER_MISSING;
346 }
347
348 if (number == null) {
349 Log.w(TAG, "placeCall: couldn't get a phone number from Intent " + intent);
350 return CallStatusCode.NO_PHONE_NUMBER_SUPPLIED;
351 }
352
353
354 // Sanity-check that ACTION_CALL_EMERGENCY is used if and only if
355 // this is a call to an emergency number
356 // (This is just a sanity-check; this policy *should* really be
357 // enforced in OutgoingCallBroadcaster.onCreate(), which is the
358 // main entry point for the CALL and CALL_* intents.)
Yorke Lee36bb2542014-06-05 08:09:52 -0700359 boolean isEmergencyNumber = PhoneNumberUtils.isLocalEmergencyNumber(mApp, number);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700360 boolean isPotentialEmergencyNumber =
Yorke Lee36bb2542014-06-05 08:09:52 -0700361 PhoneNumberUtils.isPotentialLocalEmergencyNumber(mApp, number);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700362 boolean isEmergencyIntent = Intent.ACTION_CALL_EMERGENCY.equals(intent.getAction());
363
364 if (isPotentialEmergencyNumber && !isEmergencyIntent) {
365 Log.e(TAG, "Non-CALL_EMERGENCY Intent " + intent
366 + " attempted to call potential emergency number " + number
367 + ".");
368 return CallStatusCode.CALL_FAILED;
369 } else if (!isPotentialEmergencyNumber && isEmergencyIntent) {
370 Log.e(TAG, "Received CALL_EMERGENCY Intent " + intent
371 + " with non-potential-emergency number " + number
372 + " -- failing call.");
373 return CallStatusCode.CALL_FAILED;
374 }
375
376 // If we're trying to call an emergency number, then it's OK to
377 // proceed in certain states where we'd otherwise bring up
378 // an error dialog:
379 // - If we're in EMERGENCY_ONLY mode, then (obviously) you're allowed
380 // to dial emergency numbers.
381 // - If we're OUT_OF_SERVICE, we still attempt to make a call,
382 // since the radio will register to any available network.
383
384 if (isEmergencyNumber
385 && ((okToCallStatus == CallStatusCode.EMERGENCY_ONLY)
386 || (okToCallStatus == CallStatusCode.OUT_OF_SERVICE))) {
387 if (DBG) log("placeCall: Emergency number detected with status = " + okToCallStatus);
388 okToCallStatus = CallStatusCode.SUCCESS;
389 if (DBG) log("==> UPDATING status to: " + okToCallStatus);
390 }
391
392 if (okToCallStatus != CallStatusCode.SUCCESS) {
393 // If this is an emergency call, launch the EmergencyCallHelperService
394 // to turn on the radio and retry the call.
395 if (isEmergencyNumber && (okToCallStatus == CallStatusCode.POWER_OFF)) {
396 Log.i(TAG, "placeCall: Trying to make emergency call while POWER_OFF!");
397
398 // If needed, lazily instantiate an EmergencyCallHelper instance.
399 synchronized (this) {
400 if (mEmergencyCallHelper == null) {
401 mEmergencyCallHelper = new EmergencyCallHelper(this);
402 }
403 }
404
405 // ...and kick off the "emergency call from airplane mode" sequence.
406 mEmergencyCallHelper.startEmergencyCallFromAirplaneModeSequence(number);
407
408 // Finally, return CallStatusCode.SUCCESS right now so
409 // that the in-call UI will remain visible (in order to
410 // display the progress indication.)
411 // TODO: or maybe it would be more clear to return a whole
412 // new CallStatusCode called "TURNING_ON_RADIO" here.
413 // That way, we'd update inCallUiState.progressIndication from
414 // the handleOutgoingCallError() method, rather than here.
415 return CallStatusCode.SUCCESS;
416 } else {
417 // Otherwise, just return the (non-SUCCESS) status code
418 // back to our caller.
419 if (DBG) log("==> placeCallInternal(): non-success status: " + okToCallStatus);
420
421 // Log failed call.
422 // Note: Normally, many of these values we gather from the Connection object but
423 // since no such object is created for unconnected calls, we have to build them
424 // manually.
Santos Cordonda120f42014-08-06 04:44:34 -0700425 // TODO: Try to restructure code so that we can handle failure-
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700426 // condition call logging in a single place (placeCall()) that also has access to
427 // the number we attempted to dial (not placeCall()).
428 mCallLogger.logCall(null /* callerInfo */, number, 0 /* presentation */,
429 Calls.OUTGOING_TYPE, System.currentTimeMillis(), 0 /* duration */);
430
431 return okToCallStatus;
432 }
433 }
434
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700435 // We have a valid number, so try to actually place a call:
436 // make sure we pass along the intent's URI which is a
437 // reference to the contact. We may have a provider gateway
438 // phone number to use for the outgoing call.
439 Uri contactUri = intent.getData();
440
Santos Cordon69a69192013-08-22 14:25:42 -0700441 // If a gateway is used, extract the data here and pass that into placeCall.
442 final RawGatewayInfo rawGatewayInfo = mCallGatewayManager.getRawGatewayInfo(intent, number);
443
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700444 // Watch out: PhoneUtils.placeCall() returns one of the
445 // CALL_STATUS_* constants, not a CallStatusCode enum value.
446 int callStatus = PhoneUtils.placeCall(mApp,
447 phone,
448 number,
449 contactUri,
450 (isEmergencyNumber || isEmergencyIntent),
Santos Cordon69a69192013-08-22 14:25:42 -0700451 rawGatewayInfo,
452 mCallGatewayManager);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700453
454 switch (callStatus) {
455 case PhoneUtils.CALL_STATUS_DIALED:
456 if (VDBG) log("placeCall: PhoneUtils.placeCall() succeeded for regular call '"
457 + number + "'.");
458
459
460 // TODO(OTASP): still need more cleanup to simplify the mApp.cdma*State objects:
461 // - Rather than checking inCallUiState.inCallScreenMode, the
462 // code here could also check for
463 // app.getCdmaOtaInCallScreenUiState() returning NORMAL.
464 // - But overall, app.inCallUiState.inCallScreenMode and
465 // app.cdmaOtaInCallScreenUiState.state are redundant.
466 // Combine them.
467
Jay Shrauner137458b2014-09-05 14:27:25 -0700468 boolean voicemailUriSpecified = scheme != null &&
469 scheme.equals(PhoneAccount.SCHEME_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) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700485
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...
Yorke Lee598dac52013-11-01 11:30:55 -0700665 final Intent mmiIntent = new Intent(mApp, MMIDialogActivity.class);
666 mmiIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
667 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
668 mApp.startActivity(mmiIntent);
Yorke Lee71028d02013-08-28 12:24:01 -0700669 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700670 default:
671 Log.wtf(TAG, "handleOutgoingCallError: unexpected status code " + status);
672 // Show a generic "call failed" error.
Yorke Lee71028d02013-08-28 12:24:01 -0700673 errorMessageId = R.string.incall_error_call_failed;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700674 break;
675 }
Yorke Lee71028d02013-08-28 12:24:01 -0700676 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
677 if (errorMessageId != -1) {
678 intent.putExtra(ErrorDialogActivity.ERROR_MESSAGE_ID_EXTRA, errorMessageId);
679 }
680 mApp.startActivity(intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700681 }
682
683 /**
684 * Checks the current outgoing call to see if it's an OTASP call (the
685 * "activation" call used to provision CDMA devices). If so, do any
686 * necessary OTASP-specific setup before actually placing the call.
687 */
688 private void checkForOtaspCall(Intent intent) {
689 if (OtaUtils.isOtaspCallIntent(intent)) {
690 Log.i(TAG, "checkForOtaspCall: handling OTASP intent! " + intent);
691
692 // ("OTASP-specific setup" basically means creating and initializing
693 // the OtaUtils instance. Note that this setup needs to be here in
694 // the CallController.placeCall() sequence, *not* in
695 // OtaUtils.startInteractiveOtasp(), since it's also possible to
696 // start an OTASP call by manually dialing "*228" (in which case
697 // OtaUtils.startInteractiveOtasp() never gets run at all.)
698 OtaUtils.setupOtaspCall(intent);
699 } else {
700 if (DBG) log("checkForOtaspCall: not an OTASP call.");
701 }
702 }
703
704
705 //
706 // Debugging
707 //
708
709 private static void log(String msg) {
710 Log.d(TAG, msg);
711 }
712}