blob: 0b9482d787946e47772146e5b69619fe96828051 [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 Cordonad1ed6d2013-09-16 03:04:23 -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.
425 // TODO(santoscordon): Try to restructure code so that we can handle failure-
426 // 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
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700468 boolean voicemailUriSpecified = scheme != null && scheme.equals("voicemail");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700469 // Check for an obscure ECM-related scenario: If the phone
470 // is currently in ECM (Emergency callback mode) and we
471 // dial a non-emergency number, that automatically
472 // *cancels* ECM. So warn the user about it.
473 // (See InCallScreen.showExitingECMDialog() for more info.)
474 boolean exitedEcm = false;
475 if (PhoneUtils.isPhoneInEcm(phone) && !isEmergencyNumber) {
476 Log.i(TAG, "About to exit ECM because of an outgoing non-emergency call");
477 exitedEcm = true; // this will cause us to return EXITED_ECM from this method
478 }
479
480 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
481 // Start the timer for 3 Way CallerInfo
482 if (mApp.cdmaPhoneCallState.getCurrentCallState()
483 == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) {
484 //Unmute for the second MO call
485 PhoneUtils.setMute(false);
486
487 // This is a "CDMA 3-way call", which means that you're dialing a
488 // 2nd outgoing call while a previous call is already in progress.
489 //
490 // Due to the limitations of CDMA this call doesn't actually go
491 // through the DIALING/ALERTING states, so we can't tell for sure
492 // when (or if) it's actually answered. But we want to show
493 // *some* indication of what's going on in the UI, so we "fake it"
494 // by displaying the "Dialing" state for 3 seconds.
495
496 // Set the mThreeWayCallOrigStateDialing state to true
497 mApp.cdmaPhoneCallState.setThreeWayCallOrigState(true);
498
499 // Schedule the "Dialing" indication to be taken down in 3 seconds:
500 sendEmptyMessageDelayed(THREEWAY_CALLERINFO_DISPLAY_DONE,
501 THREEWAY_CALLERINFO_DISPLAY_TIME);
502 }
503 }
504
505 // Success!
506 if (exitedEcm) {
507 return CallStatusCode.EXITED_ECM;
508 } else {
509 return CallStatusCode.SUCCESS;
510 }
511
512 case PhoneUtils.CALL_STATUS_DIALED_MMI:
513 if (DBG) log("placeCall: specified number was an MMI code: '" + number + "'.");
514 // The passed-in number was an MMI code, not a regular phone number!
515 // This isn't really a failure; the Dialer may have deliberately
516 // fired an ACTION_CALL intent to dial an MMI code, like for a
517 // USSD call.
518 //
519 // Presumably an MMI_INITIATE message will come in shortly
520 // (and we'll bring up the "MMI Started" dialog), or else
521 // an MMI_COMPLETE will come in (which will take us to a
522 // different Activity; see PhoneUtils.displayMMIComplete()).
523 return CallStatusCode.DIALED_MMI;
524
525 case PhoneUtils.CALL_STATUS_FAILED:
526 Log.w(TAG, "placeCall: PhoneUtils.placeCall() FAILED for number '"
527 + number + "'.");
528 // We couldn't successfully place the call; there was some
529 // failure in the telephony layer.
530
531 // Log failed call.
532 mCallLogger.logCall(null /* callerInfo */, number, 0 /* presentation */,
533 Calls.OUTGOING_TYPE, System.currentTimeMillis(), 0 /* duration */);
534
535 return CallStatusCode.CALL_FAILED;
536
537 default:
538 Log.wtf(TAG, "placeCall: unknown callStatus " + callStatus
539 + " from PhoneUtils.placeCall() for number '" + number + "'.");
540 return CallStatusCode.SUCCESS; // Try to continue anyway...
541 }
542 }
543
544 /**
545 * Checks the current ServiceState to make sure it's OK
546 * to try making an outgoing call to the specified number.
547 *
548 * @return CallStatusCode.SUCCESS if it's OK to try calling the specified
549 * number. If not, like if the radio is powered off or we have no
550 * signal, return one of the other CallStatusCode codes indicating what
551 * the problem is.
552 */
553 private CallStatusCode checkIfOkToInitiateOutgoingCall(int state) {
554 if (VDBG) log("checkIfOkToInitiateOutgoingCall: ServiceState = " + state);
555
556 switch (state) {
557 case ServiceState.STATE_IN_SERVICE:
558 // Normal operation. It's OK to make outgoing calls.
559 return CallStatusCode.SUCCESS;
560
561 case ServiceState.STATE_POWER_OFF:
562 // Radio is explictly powered off.
563 return CallStatusCode.POWER_OFF;
564
565 case ServiceState.STATE_EMERGENCY_ONLY:
566 // The phone is registered, but locked. Only emergency
567 // numbers are allowed.
568 // Note that as of Android 2.0 at least, the telephony layer
569 // does not actually use ServiceState.STATE_EMERGENCY_ONLY,
570 // mainly since there's no guarantee that the radio/RIL can
571 // make this distinction. So in practice the
572 // CallStatusCode.EMERGENCY_ONLY state and the string
573 // "incall_error_emergency_only" are totally unused.
574 return CallStatusCode.EMERGENCY_ONLY;
575
576 case ServiceState.STATE_OUT_OF_SERVICE:
577 // No network connection.
578 return CallStatusCode.OUT_OF_SERVICE;
579
580 default:
581 throw new IllegalStateException("Unexpected ServiceState: " + state);
582 }
583 }
584
585
586
587 /**
588 * Handles the various error conditions that can occur when initiating
589 * an outgoing call.
590 *
591 * Most error conditions are "handled" by simply displaying an error
Yorke Lee71028d02013-08-28 12:24:01 -0700592 * message to the user.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700593 *
594 * @param status one of the CallStatusCode error codes.
595 */
596 private void handleOutgoingCallError(CallStatusCode status) {
597 if (DBG) log("handleOutgoingCallError(): status = " + status);
Yorke Lee71028d02013-08-28 12:24:01 -0700598 final Intent intent = new Intent(mApp, ErrorDialogActivity.class);
599 int errorMessageId = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600 switch (status) {
601 case SUCCESS:
602 // This case shouldn't happen; you're only supposed to call
603 // handleOutgoingCallError() if there was actually an error!
604 Log.wtf(TAG, "handleOutgoingCallError: SUCCESS isn't an error");
605 break;
606
Yorke Lee71028d02013-08-28 12:24:01 -0700607 case CALL_FAILED:
608 // We couldn't successfully place the call; there was some
609 // failure in the telephony layer.
610 // TODO: Need UI spec for this failure case; for now just
611 // show a generic error.
612 errorMessageId = R.string.incall_error_call_failed;
613 break;
614 case POWER_OFF:
615 // Radio is explictly powered off, presumably because the
616 // device is in airplane mode.
617 //
618 // TODO: For now this UI is ultra-simple: we simply display
619 // a message telling the user to turn off airplane mode.
620 // But it might be nicer for the dialog to offer the option
621 // to turn the radio on right there (and automatically retry
622 // the call once network registration is complete.)
623 errorMessageId = R.string.incall_error_power_off;
624 break;
625 case EMERGENCY_ONLY:
626 // Only emergency numbers are allowed, but we tried to dial
627 // a non-emergency number.
628 // (This state is currently unused; see comments above.)
629 errorMessageId = R.string.incall_error_emergency_only;
630 break;
631 case OUT_OF_SERVICE:
632 // No network connection.
633 errorMessageId = R.string.incall_error_out_of_service;
634 break;
635 case NO_PHONE_NUMBER_SUPPLIED:
636 // The supplied Intent didn't contain a valid phone number.
637 // (This is rare and should only ever happen with broken
638 // 3rd-party apps.) For now just show a generic error.
639 errorMessageId = R.string.incall_error_no_phone_number_supplied;
640 break;
641
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700642 case VOICEMAIL_NUMBER_MISSING:
643 // Bring up the "Missing Voicemail Number" dialog, which
644 // will ultimately take us to some other Activity (or else
645 // just bail out of this activity.)
646
647 // Send a request to the InCallScreen to display the
648 // "voicemail missing" dialog when it (the InCallScreen)
649 // comes to the foreground.
Yorke Lee71028d02013-08-28 12:24:01 -0700650 intent.putExtra(ErrorDialogActivity.SHOW_MISSING_VOICEMAIL_NO_DIALOG_EXTRA, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700651 break;
652
653 case DIALED_MMI:
654 // Our initial phone number was actually an MMI sequence.
655 // There's no real "error" here, but we do bring up the
656 // a Toast (as requested of the New UI paradigm).
657 //
658 // In-call MMIs do not trigger the normal MMI Initiate
659 // Notifications, so we should notify the user here.
660 // Otherwise, the code in PhoneUtils.java should handle
661 // user notifications in the form of Toasts or Dialogs.
662 //
663 // TODO: Rather than launching a toast from here, it would
664 // be cleaner to just set a pending call status code here,
665 // and then let the InCallScreen display the toast...
Yorke Lee598dac52013-11-01 11:30:55 -0700666 final Intent mmiIntent = new Intent(mApp, MMIDialogActivity.class);
667 mmiIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
668 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
669 mApp.startActivity(mmiIntent);
Yorke Lee71028d02013-08-28 12:24:01 -0700670 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700671 default:
672 Log.wtf(TAG, "handleOutgoingCallError: unexpected status code " + status);
673 // Show a generic "call failed" error.
Yorke Lee71028d02013-08-28 12:24:01 -0700674 errorMessageId = R.string.incall_error_call_failed;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700675 break;
676 }
Yorke Lee71028d02013-08-28 12:24:01 -0700677 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
678 if (errorMessageId != -1) {
679 intent.putExtra(ErrorDialogActivity.ERROR_MESSAGE_ID_EXTRA, errorMessageId);
680 }
681 mApp.startActivity(intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700682 }
683
684 /**
685 * Checks the current outgoing call to see if it's an OTASP call (the
686 * "activation" call used to provision CDMA devices). If so, do any
687 * necessary OTASP-specific setup before actually placing the call.
688 */
689 private void checkForOtaspCall(Intent intent) {
690 if (OtaUtils.isOtaspCallIntent(intent)) {
691 Log.i(TAG, "checkForOtaspCall: handling OTASP intent! " + intent);
692
693 // ("OTASP-specific setup" basically means creating and initializing
694 // the OtaUtils instance. Note that this setup needs to be here in
695 // the CallController.placeCall() sequence, *not* in
696 // OtaUtils.startInteractiveOtasp(), since it's also possible to
697 // start an OTASP call by manually dialing "*228" (in which case
698 // OtaUtils.startInteractiveOtasp() never gets run at all.)
699 OtaUtils.setupOtaspCall(intent);
700 } else {
701 if (DBG) log("checkForOtaspCall: not an OTASP call.");
702 }
703 }
704
705
706 //
707 // Debugging
708 //
709
710 private static void log(String msg) {
711 Log.d(TAG, msg);
712 }
713}