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.phone.Constants.CallStatusCode; 22 import com.android.phone.InCallUiState.InCallScreenMode; 23 import com.android.phone.OtaUtils.CdmaOtaScreenState; 24 25 import android.content.Intent; 26 import android.net.Uri; 27 import android.os.Handler; 28 import android.os.Message; 29 import android.os.SystemProperties; 30 import android.telephony.PhoneNumberUtils; 31 import android.telephony.ServiceState; 32 import android.text.TextUtils; 33 import android.util.Log; 34 import android.widget.Toast; 35 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 initate 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.setProviderOverlayInfo(intent); 247 } else { 248 inCallUiState.clearProviderOverlayInfo(); 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 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 = 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 Uri uri = intent.getData(); 356 String scheme = (uri != null) ? uri.getScheme() : null; 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 // Any time we initiate a call, force the DTMF dialpad to 500 // close. (We want to make sure the user can see the regular 501 // in-call UI while the new call is dialing, and when it 502 // first gets connected.) 503 inCallUiState.showDialpad = false; 504 505 // Also, in case a previous call was already active (i.e. if 506 // we just did "Add call"), clear out the "history" of DTMF 507 // digits you typed, to make sure it doesn't persist from the 508 // previous call to the new call. 509 // TODO: it would be more precise to do this when the actual 510 // phone state change happens (i.e. when a new foreground 511 // call appears and the previous call moves to the 512 // background), but the InCallScreen doesn't keep enough 513 // state right now to notice that specific transition in 514 // onPhoneStateChanged(). 515 inCallUiState.dialpadDigits = null; 516 517 // Check for an obscure ECM-related scenario: If the phone 518 // is currently in ECM (Emergency callback mode) and we 519 // dial a non-emergency number, that automatically 520 // *cancels* ECM. So warn the user about it. 521 // (See InCallScreen.showExitingECMDialog() for more info.) 522 boolean exitedEcm = false; 523 if (PhoneUtils.isPhoneInEcm(phone) && !isEmergencyNumber) { 524 Log.i(TAG, "About to exit ECM because of an outgoing non-emergency call"); 525 exitedEcm = true; // this will cause us to return EXITED_ECM from this method 526 } 527 528 if (phone.getPhoneType() == Phone.PHONE_TYPE_CDMA) { 529 // Start the timer for 3 Way CallerInfo 530 if (mApp.cdmaPhoneCallState.getCurrentCallState() 531 == CdmaPhoneCallState.PhoneCallState.THRWAY_ACTIVE) { 532 //Unmute for the second MO call 533 PhoneUtils.setMute(false); 534 535 // This is a "CDMA 3-way call", which means that you're dialing a 536 // 2nd outgoing call while a previous call is already in progress. 537 // 538 // Due to the limitations of CDMA this call doesn't actually go 539 // through the DIALING/ALERTING states, so we can't tell for sure 540 // when (or if) it's actually answered. But we want to show 541 // *some* indication of what's going on in the UI, so we "fake it" 542 // by displaying the "Dialing" state for 3 seconds. 543 544 // Set the mThreeWayCallOrigStateDialing state to true 545 mApp.cdmaPhoneCallState.setThreeWayCallOrigState(true); 546 547 // Schedule the "Dialing" indication to be taken down in 3 seconds: 548 sendEmptyMessageDelayed(THREEWAY_CALLERINFO_DISPLAY_DONE, 549 THREEWAY_CALLERINFO_DISPLAY_TIME); 550 } 551 } 552 553 // Success! 554 if (exitedEcm) { 555 return CallStatusCode.EXITED_ECM; 556 } else { 557 return CallStatusCode.SUCCESS; 558 } 559 560 case PhoneUtils.CALL_STATUS_DIALED_MMI: 561 if (DBG) log("placeCall: specified number was an MMI code: '" + number + "'."); 562 // The passed-in number was an MMI code, not a regular phone number! 563 // This isn't really a failure; the Dialer may have deliberately 564 // fired an ACTION_CALL intent to dial an MMI code, like for a 565 // USSD call. 566 // 567 // Presumably an MMI_INITIATE message will come in shortly 568 // (and we'll bring up the "MMI Started" dialog), or else 569 // an MMI_COMPLETE will come in (which will take us to a 570 // different Activity; see PhoneUtils.displayMMIComplete()). 571 return CallStatusCode.DIALED_MMI; 572 573 case PhoneUtils.CALL_STATUS_FAILED: 574 Log.w(TAG, "placeCall: PhoneUtils.placeCall() FAILED for number '" 575 + number + "'."); 576 // We couldn't successfully place the call; there was some 577 // failure in the telephony layer. 578 return CallStatusCode.CALL_FAILED; 579 580 default: 581 Log.wtf(TAG, "placeCall: unknown callStatus " + callStatus 582 + " from PhoneUtils.placeCall() for number '" + number + "'."); 583 return CallStatusCode.SUCCESS; // Try to continue anyway... 584 } 585 } 586 587 /** 588 * Checks the current ServiceState to make sure it's OK 589 * to try making an outgoing call to the specified number. 590 * 591 * @return CallStatusCode.SUCCESS if it's OK to try calling the specified 592 * number. If not, like if the radio is powered off or we have no 593 * signal, return one of the other CallStatusCode codes indicating what 594 * the problem is. 595 */ 596 private CallStatusCode checkIfOkToInitiateOutgoingCall(int state) { 597 if (VDBG) log("checkIfOkToInitiateOutgoingCall: ServiceState = " + state); 598 599 switch (state) { 600 case ServiceState.STATE_IN_SERVICE: 601 // Normal operation. It's OK to make outgoing calls. 602 return CallStatusCode.SUCCESS; 603 604 case ServiceState.STATE_POWER_OFF: 605 // Radio is explictly powered off. 606 return CallStatusCode.POWER_OFF; 607 608 case ServiceState.STATE_EMERGENCY_ONLY: 609 // The phone is registered, but locked. Only emergency 610 // numbers are allowed. 611 // Note that as of Android 2.0 at least, the telephony layer 612 // does not actually use ServiceState.STATE_EMERGENCY_ONLY, 613 // mainly since there's no guarantee that the radio/RIL can 614 // make this distinction. So in practice the 615 // CallStatusCode.EMERGENCY_ONLY state and the string 616 // "incall_error_emergency_only" are totally unused. 617 return CallStatusCode.EMERGENCY_ONLY; 618 619 case ServiceState.STATE_OUT_OF_SERVICE: 620 // No network connection. 621 return CallStatusCode.OUT_OF_SERVICE; 622 623 default: 624 throw new IllegalStateException("Unexpected ServiceState: " + state); 625 } 626 } 627 628 /** 629 * Given an Intent (which is presumably the ACTION_CALL intent that 630 * initiated this outgoing call), figure out the actual phone number we 631 * should dial. 632 * 633 * Note that the returned "number" may actually be a SIP address, 634 * if the specified intent contains a sip: URI. 635 * 636 * This method is basically a wrapper around PhoneUtils.getNumberFromIntent(), 637 * except it's also aware of the EXTRA_ACTUAL_NUMBER_TO_DIAL extra. 638 * (That extra, if present, tells us the exact string to pass down to the 639 * telephony layer. It's guaranteed to be safe to dial: it's either a PSTN 640 * phone number with separators and keypad letters stripped out, or a raw 641 * unencoded SIP address.) 642 * 643 * @return the phone number corresponding to the specified Intent, or null 644 * if the Intent has no action or if the intent's data is malformed or 645 * missing. 646 * 647 * @throws VoiceMailNumberMissingException if the intent 648 * contains a "voicemail" URI, but there's no voicemail 649 * number configured on the device. 650 */ 651 // TODO: Consider moving this out to PhoneUtils and/or combining it 652 // with PhoneUtils.getNumberFromIntent(). 653 public static String getInitialNumber(Intent intent) 654 throws PhoneUtils.VoiceMailNumberMissingException { 655 if (VDBG) log("getInitialNumber(): " + intent); 656 657 String action = intent.getAction(); 658 if (TextUtils.isEmpty(action)) { 659 return null; 660 } 661 662 // If the EXTRA_ACTUAL_NUMBER_TO_DIAL extra is present, get the phone 663 // number from there. (That extra takes precedence over the actual data 664 // included in the intent.) 665 if (intent.hasExtra(OutgoingCallBroadcaster.EXTRA_ACTUAL_NUMBER_TO_DIAL)) { 666 String actualNumberToDial = 667 intent.getStringExtra(OutgoingCallBroadcaster.EXTRA_ACTUAL_NUMBER_TO_DIAL); 668 if (VDBG) log("==> got EXTRA_ACTUAL_NUMBER_TO_DIAL; returning '" 669 + actualNumberToDial + "'"); 670 return actualNumberToDial; 671 } 672 673 return PhoneUtils.getNumberFromIntent(PhoneApp.getInstance(), intent); 674 } 675 676 /** 677 * Handles the various error conditions that can occur when initiating 678 * an outgoing call. 679 * 680 * Most error conditions are "handled" by simply displaying an error 681 * message to the user. This is accomplished by setting the 682 * inCallUiState pending call status code flag, which tells the 683 * InCallScreen to display an appropriate message to the user when the 684 * in-call UI comes to the foreground. 685 * 686 * @param status one of the CallStatusCode error codes. 687 */ 688 private void handleOutgoingCallError(CallStatusCode status) { 689 if (DBG) log("handleOutgoingCallError(): status = " + status); 690 final InCallUiState inCallUiState = mApp.inCallUiState; 691 692 // In most cases we simply want to have the InCallScreen display 693 // an appropriate error dialog, so we simply copy the specified 694 // status code into the InCallUiState "pending call status code" 695 // field. (See InCallScreen.showStatusIndication() for the next 696 // step of the sequence.) 697 698 switch (status) { 699 case SUCCESS: 700 // This case shouldn't happen; you're only supposed to call 701 // handleOutgoingCallError() if there was actually an error! 702 Log.wtf(TAG, "handleOutgoingCallError: SUCCESS isn't an error"); 703 break; 704 705 case VOICEMAIL_NUMBER_MISSING: 706 // Bring up the "Missing Voicemail Number" dialog, which 707 // will ultimately take us to some other Activity (or else 708 // just bail out of this activity.) 709 710 // Send a request to the InCallScreen to display the 711 // "voicemail missing" dialog when it (the InCallScreen) 712 // comes to the foreground. 713 inCallUiState.setPendingCallStatusCode(CallStatusCode.VOICEMAIL_NUMBER_MISSING); 714 break; 715 716 case POWER_OFF: 717 // Radio is explictly powered off, presumably because the 718 // device is in airplane mode. 719 // 720 // TODO: For now this UI is ultra-simple: we simply display 721 // a message telling the user to turn off airplane mode. 722 // But it might be nicer for the dialog to offer the option 723 // to turn the radio on right there (and automatically retry 724 // the call once network registration is complete.) 725 inCallUiState.setPendingCallStatusCode(CallStatusCode.POWER_OFF); 726 break; 727 728 case EMERGENCY_ONLY: 729 // Only emergency numbers are allowed, but we tried to dial 730 // a non-emergency number. 731 // (This state is currently unused; see comments above.) 732 inCallUiState.setPendingCallStatusCode(CallStatusCode.EMERGENCY_ONLY); 733 break; 734 735 case OUT_OF_SERVICE: 736 // No network connection. 737 inCallUiState.setPendingCallStatusCode(CallStatusCode.OUT_OF_SERVICE); 738 break; 739 740 case NO_PHONE_NUMBER_SUPPLIED: 741 // The supplied Intent didn't contain a valid phone number. 742 // (This is rare and should only ever happen with broken 743 // 3rd-party apps.) For now just show a generic error. 744 inCallUiState.setPendingCallStatusCode(CallStatusCode.NO_PHONE_NUMBER_SUPPLIED); 745 break; 746 747 case DIALED_MMI: 748 // Our initial phone number was actually an MMI sequence. 749 // There's no real "error" here, but we do bring up the 750 // a Toast (as requested of the New UI paradigm). 751 // 752 // In-call MMIs do not trigger the normal MMI Initiate 753 // Notifications, so we should notify the user here. 754 // Otherwise, the code in PhoneUtils.java should handle 755 // user notifications in the form of Toasts or Dialogs. 756 // 757 // TODO: Rather than launching a toast from here, it would 758 // be cleaner to just set a pending call status code here, 759 // and then let the InCallScreen display the toast... 760 if (mCM.getState() == Phone.State.OFFHOOK) { 761 Toast.makeText(mApp, R.string.incall_status_dialed_mmi, Toast.LENGTH_SHORT) 762 .show(); 763 } 764 break; 765 766 case CALL_FAILED: 767 // We couldn't successfully place the call; there was some 768 // failure in the telephony layer. 769 // TODO: Need UI spec for this failure case; for now just 770 // show a generic error. 771 inCallUiState.setPendingCallStatusCode(CallStatusCode.CALL_FAILED); 772 break; 773 774 default: 775 Log.wtf(TAG, "handleOutgoingCallError: unexpected status code " + status); 776 // Show a generic "call failed" error. 777 inCallUiState.setPendingCallStatusCode(CallStatusCode.CALL_FAILED); 778 break; 779 } 780 } 781 782 /** 783 * Checks the current outgoing call to see if it's an OTASP call (the 784 * "activation" call used to provision CDMA devices). If so, do any 785 * necessary OTASP-specific setup before actually placing the call. 786 */ 787 private void checkForOtaspCall(Intent intent) { 788 if (OtaUtils.isOtaspCallIntent(intent)) { 789 Log.i(TAG, "checkForOtaspCall: handling OTASP intent! " + intent); 790 791 // ("OTASP-specific setup" basically means creating and initializing 792 // the OtaUtils instance. Note that this setup needs to be here in 793 // the CallController.placeCall() sequence, *not* in 794 // OtaUtils.startInteractiveOtasp(), since it's also possible to 795 // start an OTASP call by manually dialing "*228" (in which case 796 // OtaUtils.startInteractiveOtasp() never gets run at all.) 797 OtaUtils.setupOtaspCall(intent); 798 } else { 799 if (DBG) log("checkForOtaspCall: not an OTASP call."); 800 } 801 } 802 803 804 // 805 // Debugging 806 // 807 808 private static void log(String msg) { 809 Log.d(TAG, msg); 810 } 811 } 812