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