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