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