TelephonyManager.java revision 1540a7ea1afcb772b5fcf90f635153f6489eb8ed
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.telephony; 18 19import android.annotation.SdkConstant; 20import android.annotation.SdkConstant.SdkConstantType; 21import android.content.Context; 22import android.os.Bundle; 23import android.os.RemoteException; 24import android.os.ServiceManager; 25import android.os.SystemProperties; 26 27import com.android.internal.telephony.IPhoneSubInfo; 28import com.android.internal.telephony.ITelephony; 29import com.android.internal.telephony.ITelephonyRegistry; 30import com.android.internal.telephony.Phone; 31import com.android.internal.telephony.PhoneFactory; 32import com.android.internal.telephony.RILConstants; 33import com.android.internal.telephony.TelephonyProperties; 34 35import java.util.List; 36 37/** 38 * Provides access to information about the telephony services on 39 * the device. Applications can use the methods in this class to 40 * determine telephony services and states, as well as to access some 41 * types of subscriber information. Applications can also register 42 * a listener to receive notification of telephony state changes. 43 * <p> 44 * You do not instantiate this class directly; instead, you retrieve 45 * a reference to an instance through 46 * {@link android.content.Context#getSystemService 47 * Context.getSystemService(Context.TELEPHONY_SERVICE)}. 48 * <p> 49 * Note that access to some telephony information is 50 * permission-protected. Your application cannot access the protected 51 * information unless it has the appropriate permissions declared in 52 * its manifest file. Where permissions apply, they are noted in the 53 * the methods through which you access the protected information. 54 */ 55public class TelephonyManager { 56 private static final String TAG = "TelephonyManager"; 57 58 private Context mContext; 59 private ITelephonyRegistry mRegistry; 60 61 /** @hide */ 62 public TelephonyManager(Context context) { 63 mContext = context; 64 mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService( 65 "telephony.registry")); 66 } 67 68 /** @hide */ 69 private TelephonyManager() { 70 } 71 72 private static TelephonyManager sInstance = new TelephonyManager(); 73 74 /** @hide */ 75 public static TelephonyManager getDefault() { 76 return sInstance; 77 } 78 79 80 // 81 // Broadcast Intent actions 82 // 83 84 /** 85 * Broadcast intent action indicating that the call state (cellular) 86 * on the device has changed. 87 * 88 * <p> 89 * The {@link #EXTRA_STATE} extra indicates the new call state. 90 * If the new state is RINGING, a second extra 91 * {@link #EXTRA_INCOMING_NUMBER} provides the incoming phone number as 92 * a String. 93 * 94 * <p class="note"> 95 * Requires the READ_PHONE_STATE permission. 96 * 97 * <p class="note"> 98 * This was a {@link android.content.Context#sendStickyBroadcast sticky} 99 * broadcast in version 1.0, but it is no longer sticky. 100 * Instead, use {@link #getCallState} to synchronously query the current call state. 101 * 102 * @see #EXTRA_STATE 103 * @see #EXTRA_INCOMING_NUMBER 104 * @see #getCallState 105 */ 106 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 107 public static final String ACTION_PHONE_STATE_CHANGED = 108 "android.intent.action.PHONE_STATE"; 109 110 /** 111 * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast 112 * for a String containing the new call state. 113 * 114 * @see #EXTRA_STATE_IDLE 115 * @see #EXTRA_STATE_RINGING 116 * @see #EXTRA_STATE_OFFHOOK 117 * 118 * <p class="note"> 119 * Retrieve with 120 * {@link android.content.Intent#getStringExtra(String)}. 121 */ 122 public static final String EXTRA_STATE = Phone.STATE_KEY; 123 124 /** 125 * Value used with {@link #EXTRA_STATE} corresponding to 126 * {@link #CALL_STATE_IDLE}. 127 */ 128 public static final String EXTRA_STATE_IDLE = Phone.State.IDLE.toString(); 129 130 /** 131 * Value used with {@link #EXTRA_STATE} corresponding to 132 * {@link #CALL_STATE_RINGING}. 133 */ 134 public static final String EXTRA_STATE_RINGING = Phone.State.RINGING.toString(); 135 136 /** 137 * Value used with {@link #EXTRA_STATE} corresponding to 138 * {@link #CALL_STATE_OFFHOOK}. 139 */ 140 public static final String EXTRA_STATE_OFFHOOK = Phone.State.OFFHOOK.toString(); 141 142 /** 143 * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast 144 * for a String containing the incoming phone number. 145 * Only valid when the new call state is RINGING. 146 * 147 * <p class="note"> 148 * Retrieve with 149 * {@link android.content.Intent#getStringExtra(String)}. 150 */ 151 public static final String EXTRA_INCOMING_NUMBER = "incoming_number"; 152 153 154 // 155 // 156 // Device Info 157 // 158 // 159 160 /** 161 * Returns the software version number for the device, for example, 162 * the IMEI/SV for GSM phones. Return null if the software version is 163 * not available. 164 * 165 * <p>Requires Permission: 166 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 167 */ 168 public String getDeviceSoftwareVersion() { 169 try { 170 return getSubscriberInfo().getDeviceSvn(); 171 } catch (RemoteException ex) { 172 return null; 173 } catch (NullPointerException ex) { 174 return null; 175 } 176 } 177 178 /** 179 * Returns the unique device ID, for example, the IMEI for GSM and the MEID 180 * for CDMA phones. Return null if device ID is not available. 181 * 182 * <p>Requires Permission: 183 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 184 */ 185 public String getDeviceId() { 186 try { 187 return getSubscriberInfo().getDeviceId(); 188 } catch (RemoteException ex) { 189 return null; 190 } catch (NullPointerException ex) { 191 return null; 192 } 193 } 194 195 /** 196 * Returns the current location of the device. 197 * Return null if current location is not available. 198 * 199 * <p>Requires Permission: 200 * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} or 201 * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION}. 202 */ 203 public CellLocation getCellLocation() { 204 try { 205 Bundle bundle = getITelephony().getCellLocation(); 206 CellLocation cl = CellLocation.newFromBundle(bundle); 207 if (cl.isEmpty()) 208 return null; 209 return cl; 210 } catch (RemoteException ex) { 211 return null; 212 } catch (NullPointerException ex) { 213 return null; 214 } 215 } 216 217 /** 218 * Enables location update notifications. {@link PhoneStateListener#onCellLocationChanged 219 * PhoneStateListener.onCellLocationChanged} will be called on location updates. 220 * 221 * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES 222 * CONTROL_LOCATION_UPDATES} 223 * 224 * @hide 225 */ 226 public void enableLocationUpdates() { 227 try { 228 getITelephony().enableLocationUpdates(); 229 } catch (RemoteException ex) { 230 } catch (NullPointerException ex) { 231 } 232 } 233 234 /** 235 * Disables location update notifications. {@link PhoneStateListener#onCellLocationChanged 236 * PhoneStateListener.onCellLocationChanged} will be called on location updates. 237 * 238 * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES 239 * CONTROL_LOCATION_UPDATES} 240 * 241 * @hide 242 */ 243 public void disableLocationUpdates() { 244 try { 245 getITelephony().disableLocationUpdates(); 246 } catch (RemoteException ex) { 247 } catch (NullPointerException ex) { 248 } 249 } 250 251 /** 252 * Returns the neighboring cell information of the device. 253 * 254 * @return List of NeighboringCellInfo or null if info unavailable. 255 * 256 * <p>Requires Permission: 257 * (@link android.Manifest.permission#ACCESS_COARSE_UPDATES} 258 */ 259 public List<NeighboringCellInfo> getNeighboringCellInfo() { 260 try { 261 return getITelephony().getNeighboringCellInfo(); 262 } catch (RemoteException ex) { 263 return null; 264 } catch (NullPointerException ex) { 265 return null; 266 } 267 } 268 269 /** No phone radio. */ 270 public static final int PHONE_TYPE_NONE = Phone.PHONE_TYPE_NONE; 271 /** Phone radio is GSM. */ 272 public static final int PHONE_TYPE_GSM = Phone.PHONE_TYPE_GSM; 273 /** Phone radio is CDMA. */ 274 public static final int PHONE_TYPE_CDMA = Phone.PHONE_TYPE_CDMA; 275 276 /** 277 * Returns a constant indicating the device phone type. 278 * 279 * @see #PHONE_TYPE_NONE 280 * @see #PHONE_TYPE_GSM 281 * @see #PHONE_TYPE_CDMA 282 */ 283 public int getPhoneType() { 284 try{ 285 ITelephony telephony = getITelephony(); 286 if (telephony != null) { 287 return telephony.getActivePhoneType(); 288 } else { 289 // This can happen when the ITelephony interface is not up yet. 290 return getPhoneTypeFromProperty(); 291 } 292 } catch (RemoteException ex) { 293 // This shouldn't happen in the normal case, as a backup we 294 // read from the system property. 295 return getPhoneTypeFromProperty(); 296 } catch (NullPointerException ex) { 297 // This shouldn't happen in the normal case, as a backup we 298 // read from the system property. 299 return getPhoneTypeFromProperty(); 300 } 301 } 302 303 304 private int getPhoneTypeFromProperty() { 305 int type = 306 SystemProperties.getInt(TelephonyProperties.CURRENT_ACTIVE_PHONE, 307 getPhoneTypeFromNetworkType()); 308 return type; 309 } 310 311 private int getPhoneTypeFromNetworkType() { 312 // When the system property CURRENT_ACTIVE_PHONE, has not been set, 313 // use the system property for default network type. 314 // This is a fail safe, and can only happen at first boot. 315 int mode = SystemProperties.getInt("ro.telephony.default_network", -1); 316 if (mode == -1) 317 return PHONE_TYPE_NONE; 318 return PhoneFactory.getPhoneType(mode); 319 } 320 // 321 // 322 // Current Network 323 // 324 // 325 326 /** 327 * Returns the alphabetic name of current registered operator. 328 * <p> 329 * Availability: Only when user is registered to a network. Result may be 330 * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if 331 * on a CDMA network). 332 */ 333 public String getNetworkOperatorName() { 334 return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ALPHA); 335 } 336 337 /** 338 * Returns the numeric name (MCC+MNC) of current registered operator. 339 * <p> 340 * Availability: Only when user is registered to a network. Result may be 341 * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if 342 * on a CDMA network). 343 */ 344 public String getNetworkOperator() { 345 return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_NUMERIC); 346 } 347 348 /** 349 * Returns true if the device is considered roaming on the current 350 * network, for GSM purposes. 351 * <p> 352 * Availability: Only when user registered to a network. 353 */ 354 public boolean isNetworkRoaming() { 355 return "true".equals(SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING)); 356 } 357 358 /** 359 * Returns the ISO country code equivalent of the current registered 360 * operator's MCC (Mobile Country Code). 361 * <p> 362 * Availability: Only when user is registered to a network. Result may be 363 * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if 364 * on a CDMA network). 365 */ 366 public String getNetworkCountryIso() { 367 return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY); 368 } 369 370 /** Network type is unknown */ 371 public static final int NETWORK_TYPE_UNKNOWN = 0; 372 /** Current network is GPRS */ 373 public static final int NETWORK_TYPE_GPRS = 1; 374 /** Current network is EDGE */ 375 public static final int NETWORK_TYPE_EDGE = 2; 376 /** Current network is UMTS */ 377 public static final int NETWORK_TYPE_UMTS = 3; 378 /** Current network is CDMA: Either IS95A or IS95B*/ 379 public static final int NETWORK_TYPE_CDMA = 4; 380 /** Current network is EVDO revision 0*/ 381 public static final int NETWORK_TYPE_EVDO_0 = 5; 382 /** Current network is EVDO revision A*/ 383 public static final int NETWORK_TYPE_EVDO_A = 6; 384 /** Current network is 1xRTT*/ 385 public static final int NETWORK_TYPE_1xRTT = 7; 386 /** Current network is HSDPA */ 387 public static final int NETWORK_TYPE_HSDPA = 8; 388 /** Current network is HSUPA */ 389 public static final int NETWORK_TYPE_HSUPA = 9; 390 /** Current network is HSPA */ 391 public static final int NETWORK_TYPE_HSPA = 10; 392 /** Current network is iDen */ 393 public static final int NETWORK_TYPE_IDEN = 11; 394 /** Current network is EVDO revision B*/ 395 public static final int NETWORK_TYPE_EVDO_B = 12; 396 397 398 /** 399 * Returns a constant indicating the radio technology (network type) 400 * currently in use on the device. 401 * @return the network type 402 * 403 * @see #NETWORK_TYPE_UNKNOWN 404 * @see #NETWORK_TYPE_GPRS 405 * @see #NETWORK_TYPE_EDGE 406 * @see #NETWORK_TYPE_UMTS 407 * @see #NETWORK_TYPE_HSDPA 408 * @see #NETWORK_TYPE_HSUPA 409 * @see #NETWORK_TYPE_HSPA 410 * @see #NETWORK_TYPE_CDMA 411 * @see #NETWORK_TYPE_EVDO_0 412 * @see #NETWORK_TYPE_EVDO_A 413 * @see #NETWORK_TYPE_EVDO_B 414 * @see #NETWORK_TYPE_1xRTT 415 */ 416 public int getNetworkType() { 417 try{ 418 ITelephony telephony = getITelephony(); 419 if (telephony != null) { 420 return telephony.getNetworkType(); 421 } else { 422 // This can happen when the ITelephony interface is not up yet. 423 return NETWORK_TYPE_UNKNOWN; 424 } 425 } catch(RemoteException ex) { 426 // This shouldn't happen in the normal case 427 return NETWORK_TYPE_UNKNOWN; 428 } catch (NullPointerException ex) { 429 // This could happen before phone restarts due to crashing 430 return NETWORK_TYPE_UNKNOWN; 431 } 432 } 433 434 /** 435 * Returns a string representation of the radio technology (network type) 436 * currently in use on the device. 437 * @return the name of the radio technology 438 * 439 * @hide pending API council review 440 */ 441 public String getNetworkTypeName() { 442 switch (getNetworkType()) { 443 case NETWORK_TYPE_GPRS: 444 return "GPRS"; 445 case NETWORK_TYPE_EDGE: 446 return "EDGE"; 447 case NETWORK_TYPE_UMTS: 448 return "UMTS"; 449 case NETWORK_TYPE_HSDPA: 450 return "HSDPA"; 451 case NETWORK_TYPE_HSUPA: 452 return "HSUPA"; 453 case NETWORK_TYPE_HSPA: 454 return "HSPA"; 455 case NETWORK_TYPE_CDMA: 456 return "CDMA"; 457 case NETWORK_TYPE_EVDO_0: 458 return "CDMA - EvDo rev. 0"; 459 case NETWORK_TYPE_EVDO_A: 460 return "CDMA - EvDo rev. A"; 461 case NETWORK_TYPE_EVDO_B: 462 return "CDMA - EvDo rev. B"; 463 case NETWORK_TYPE_1xRTT: 464 return "CDMA - 1xRTT"; 465 default: 466 return "UNKNOWN"; 467 } 468 } 469 470 // 471 // 472 // SIM Card 473 // 474 // 475 476 /** SIM card state: Unknown. Signifies that the SIM is in transition 477 * between states. For example, when the user inputs the SIM pin 478 * under PIN_REQUIRED state, a query for sim status returns 479 * this state before turning to SIM_STATE_READY. */ 480 public static final int SIM_STATE_UNKNOWN = 0; 481 /** SIM card state: no SIM card is available in the device */ 482 public static final int SIM_STATE_ABSENT = 1; 483 /** SIM card state: Locked: requires the user's SIM PIN to unlock */ 484 public static final int SIM_STATE_PIN_REQUIRED = 2; 485 /** SIM card state: Locked: requires the user's SIM PUK to unlock */ 486 public static final int SIM_STATE_PUK_REQUIRED = 3; 487 /** SIM card state: Locked: requries a network PIN to unlock */ 488 public static final int SIM_STATE_NETWORK_LOCKED = 4; 489 /** SIM card state: Ready */ 490 public static final int SIM_STATE_READY = 5; 491 492 /** 493 * @return true if a ICC card is present 494 */ 495 public boolean hasIccCard() { 496 try { 497 return getITelephony().hasIccCard(); 498 } catch (RemoteException ex) { 499 // Assume no ICC card if remote exception which shouldn't happen 500 return false; 501 } catch (NullPointerException ex) { 502 // This could happen before phone restarts due to crashing 503 return false; 504 } 505 } 506 507 /** 508 * Returns a constant indicating the state of the 509 * device SIM card. 510 * 511 * @see #SIM_STATE_UNKNOWN 512 * @see #SIM_STATE_ABSENT 513 * @see #SIM_STATE_PIN_REQUIRED 514 * @see #SIM_STATE_PUK_REQUIRED 515 * @see #SIM_STATE_NETWORK_LOCKED 516 * @see #SIM_STATE_READY 517 */ 518 public int getSimState() { 519 String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE); 520 if ("ABSENT".equals(prop)) { 521 return SIM_STATE_ABSENT; 522 } 523 else if ("PIN_REQUIRED".equals(prop)) { 524 return SIM_STATE_PIN_REQUIRED; 525 } 526 else if ("PUK_REQUIRED".equals(prop)) { 527 return SIM_STATE_PUK_REQUIRED; 528 } 529 else if ("NETWORK_LOCKED".equals(prop)) { 530 return SIM_STATE_NETWORK_LOCKED; 531 } 532 else if ("READY".equals(prop)) { 533 return SIM_STATE_READY; 534 } 535 else { 536 return SIM_STATE_UNKNOWN; 537 } 538 } 539 540 /** 541 * Returns the MCC+MNC (mobile country code + mobile network code) of the 542 * provider of the SIM. 5 or 6 decimal digits. 543 * <p> 544 * Availability: SIM state must be {@link #SIM_STATE_READY} 545 * 546 * @see #getSimState 547 */ 548 public String getSimOperator() { 549 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC); 550 } 551 552 /** 553 * Returns the Service Provider Name (SPN). 554 * <p> 555 * Availability: SIM state must be {@link #SIM_STATE_READY} 556 * 557 * @see #getSimState 558 */ 559 public String getSimOperatorName() { 560 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA); 561 } 562 563 /** 564 * Returns the ISO country code equivalent for the SIM provider's country code. 565 */ 566 public String getSimCountryIso() { 567 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY); 568 } 569 570 /** 571 * Returns the serial number of the SIM, if applicable. Return null if it is 572 * unavailable. 573 * <p> 574 * Requires Permission: 575 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 576 */ 577 public String getSimSerialNumber() { 578 try { 579 return getSubscriberInfo().getIccSerialNumber(); 580 } catch (RemoteException ex) { 581 return null; 582 } catch (NullPointerException ex) { 583 // This could happen before phone restarts due to crashing 584 return null; 585 } 586 } 587 588 // 589 // 590 // Subscriber Info 591 // 592 // 593 594 /** 595 * Returns the unique subscriber ID, for example, the IMSI for a GSM phone. 596 * Return null if it is unavailable. 597 * <p> 598 * Requires Permission: 599 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 600 */ 601 public String getSubscriberId() { 602 try { 603 return getSubscriberInfo().getSubscriberId(); 604 } catch (RemoteException ex) { 605 return null; 606 } catch (NullPointerException ex) { 607 // This could happen before phone restarts due to crashing 608 return null; 609 } 610 } 611 612 /** 613 * Returns the phone number string for line 1, for example, the MSISDN 614 * for a GSM phone. Return null if it is unavailable. 615 * <p> 616 * Requires Permission: 617 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 618 */ 619 public String getLine1Number() { 620 try { 621 return getSubscriberInfo().getLine1Number(); 622 } catch (RemoteException ex) { 623 return null; 624 } catch (NullPointerException ex) { 625 // This could happen before phone restarts due to crashing 626 return null; 627 } 628 } 629 630 /** 631 * Returns the alphabetic identifier associated with the line 1 number. 632 * Return null if it is unavailable. 633 * <p> 634 * Requires Permission: 635 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 636 * @hide 637 * nobody seems to call this. 638 */ 639 public String getLine1AlphaTag() { 640 try { 641 return getSubscriberInfo().getLine1AlphaTag(); 642 } catch (RemoteException ex) { 643 return null; 644 } catch (NullPointerException ex) { 645 // This could happen before phone restarts due to crashing 646 return null; 647 } 648 } 649 650 /** 651 * Returns the voice mail number. Return null if it is unavailable. 652 * <p> 653 * Requires Permission: 654 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 655 */ 656 public String getVoiceMailNumber() { 657 try { 658 return getSubscriberInfo().getVoiceMailNumber(); 659 } catch (RemoteException ex) { 660 return null; 661 } catch (NullPointerException ex) { 662 // This could happen before phone restarts due to crashing 663 return null; 664 } 665 } 666 667 /** 668 * Returns the complete voice mail number. Return null if it is unavailable. 669 * <p> 670 * Requires Permission: 671 * {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED} 672 * 673 * @hide 674 */ 675 public String getCompleteVoiceMailNumber() { 676 try { 677 return getSubscriberInfo().getCompleteVoiceMailNumber(); 678 } catch (RemoteException ex) { 679 return null; 680 } catch (NullPointerException ex) { 681 // This could happen before phone restarts due to crashing 682 return null; 683 } 684 } 685 686 /** 687 * Returns the voice mail count. Return 0 if unavailable. 688 * <p> 689 * Requires Permission: 690 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 691 * @hide 692 */ 693 public int getVoiceMessageCount() { 694 try { 695 return getITelephony().getVoiceMessageCount(); 696 } catch (RemoteException ex) { 697 return 0; 698 } catch (NullPointerException ex) { 699 // This could happen before phone restarts due to crashing 700 return 0; 701 } 702 } 703 704 /** 705 * Retrieves the alphabetic identifier associated with the voice 706 * mail number. 707 * <p> 708 * Requires Permission: 709 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 710 */ 711 public String getVoiceMailAlphaTag() { 712 try { 713 return getSubscriberInfo().getVoiceMailAlphaTag(); 714 } catch (RemoteException ex) { 715 return null; 716 } catch (NullPointerException ex) { 717 // This could happen before phone restarts due to crashing 718 return null; 719 } 720 } 721 722 private IPhoneSubInfo getSubscriberInfo() { 723 // get it each time because that process crashes a lot 724 return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo")); 725 } 726 727 728 /** Device call state: No activity. */ 729 public static final int CALL_STATE_IDLE = 0; 730 /** Device call state: Ringing. A new call arrived and is 731 * ringing or waiting. In the latter case, another call is 732 * already active. */ 733 public static final int CALL_STATE_RINGING = 1; 734 /** Device call state: Off-hook. At least one call exists 735 * that is dialing, active, or on hold, and no calls are ringing 736 * or waiting. */ 737 public static final int CALL_STATE_OFFHOOK = 2; 738 739 /** 740 * Returns a constant indicating the call state (cellular) on the device. 741 */ 742 public int getCallState() { 743 try { 744 return getITelephony().getCallState(); 745 } catch (RemoteException ex) { 746 // the phone process is restarting. 747 return CALL_STATE_IDLE; 748 } catch (NullPointerException ex) { 749 // the phone process is restarting. 750 return CALL_STATE_IDLE; 751 } 752 } 753 754 /** Data connection activity: No traffic. */ 755 public static final int DATA_ACTIVITY_NONE = 0x00000000; 756 /** Data connection activity: Currently receiving IP PPP traffic. */ 757 public static final int DATA_ACTIVITY_IN = 0x00000001; 758 /** Data connection activity: Currently sending IP PPP traffic. */ 759 public static final int DATA_ACTIVITY_OUT = 0x00000002; 760 /** Data connection activity: Currently both sending and receiving 761 * IP PPP traffic. */ 762 public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT; 763 /** 764 * Data connection is active, but physical link is down 765 */ 766 public static final int DATA_ACTIVITY_DORMANT = 0x00000004; 767 768 /** 769 * Returns a constant indicating the type of activity on a data connection 770 * (cellular). 771 * 772 * @see #DATA_ACTIVITY_NONE 773 * @see #DATA_ACTIVITY_IN 774 * @see #DATA_ACTIVITY_OUT 775 * @see #DATA_ACTIVITY_INOUT 776 * @see #DATA_ACTIVITY_DORMANT 777 */ 778 public int getDataActivity() { 779 try { 780 return getITelephony().getDataActivity(); 781 } catch (RemoteException ex) { 782 // the phone process is restarting. 783 return DATA_ACTIVITY_NONE; 784 } catch (NullPointerException ex) { 785 // the phone process is restarting. 786 return DATA_ACTIVITY_NONE; 787 } 788 } 789 790 /** Data connection state: Disconnected. IP traffic not available. */ 791 public static final int DATA_DISCONNECTED = 0; 792 /** Data connection state: Currently setting up a data connection. */ 793 public static final int DATA_CONNECTING = 1; 794 /** Data connection state: Connected. IP traffic should be available. */ 795 public static final int DATA_CONNECTED = 2; 796 /** Data connection state: Suspended. The connection is up, but IP 797 * traffic is temporarily unavailable. For example, in a 2G network, 798 * data activity may be suspended when a voice call arrives. */ 799 public static final int DATA_SUSPENDED = 3; 800 801 /** 802 * Returns a constant indicating the current data connection state 803 * (cellular). 804 * 805 * @see #DATA_DISCONNECTED 806 * @see #DATA_CONNECTING 807 * @see #DATA_CONNECTED 808 * @see #DATA_SUSPENDED 809 */ 810 public int getDataState() { 811 try { 812 return getITelephony().getDataState(); 813 } catch (RemoteException ex) { 814 // the phone process is restarting. 815 return DATA_DISCONNECTED; 816 } catch (NullPointerException ex) { 817 return DATA_DISCONNECTED; 818 } 819 } 820 821 private ITelephony getITelephony() { 822 return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE)); 823 } 824 825 // 826 // 827 // PhoneStateListener 828 // 829 // 830 831 /** 832 * Registers a listener object to receive notification of changes 833 * in specified telephony states. 834 * <p> 835 * To register a listener, pass a {@link PhoneStateListener} 836 * and specify at least one telephony state of interest in 837 * the events argument. 838 * 839 * At registration, and when a specified telephony state 840 * changes, the telephony manager invokes the appropriate 841 * callback method on the listener object and passes the 842 * current (udpated) values. 843 * <p> 844 * To unregister a listener, pass the listener object and set the 845 * events argument to 846 * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0). 847 * 848 * @param listener The {@link PhoneStateListener} object to register 849 * (or unregister) 850 * @param events The telephony state(s) of interest to the listener, 851 * as a bitwise-OR combination of {@link PhoneStateListener} 852 * LISTEN_ flags. 853 */ 854 public void listen(PhoneStateListener listener, int events) { 855 String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>"; 856 try { 857 Boolean notifyNow = (getITelephony() != null); 858 mRegistry.listen(pkgForDebug, listener.callback, events, notifyNow); 859 } catch (RemoteException ex) { 860 // system process dead 861 } catch (NullPointerException ex) { 862 // system process dead 863 } 864 } 865 866 /** 867 * Returns the CDMA ERI icon index to display 868 * 869 * @hide 870 */ 871 public int getCdmaEriIconIndex() { 872 try { 873 return getITelephony().getCdmaEriIconIndex(); 874 } catch (RemoteException ex) { 875 // the phone process is restarting. 876 return -1; 877 } catch (NullPointerException ex) { 878 return -1; 879 } 880 } 881 882 /** 883 * Returns the CDMA ERI icon mode, 884 * 0 - ON 885 * 1 - FLASHING 886 * 887 * @hide 888 */ 889 public int getCdmaEriIconMode() { 890 try { 891 return getITelephony().getCdmaEriIconMode(); 892 } catch (RemoteException ex) { 893 // the phone process is restarting. 894 return -1; 895 } catch (NullPointerException ex) { 896 return -1; 897 } 898 } 899 900 /** 901 * Returns the CDMA ERI text, 902 * 903 * @hide 904 */ 905 public String getCdmaEriText() { 906 try { 907 return getITelephony().getCdmaEriText(); 908 } catch (RemoteException ex) { 909 // the phone process is restarting. 910 return null; 911 } catch (NullPointerException ex) { 912 return null; 913 } 914 } 915} 916