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