TelephonyManager.java revision 52d0f9882fc8f33d1aef1d0e79f15733daf4ebe2
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.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 static Context sContext; 59 private static ITelephonyRegistry sRegistry; 60 61 /** @hide */ 62 public TelephonyManager(Context context) { 63 context = context.getApplicationContext(); 64 if (sContext == null) { 65 sContext = context; 66 67 sRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService( 68 "telephony.registry")); 69 } else if (sContext != context) { 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 public static final int NETWORK_TYPE_HSPAP = 15; 432 433 /** 434 * Returns a constant indicating the radio technology (network type) 435 * currently in use on the device for data transmission. 436 * @return the network type 437 * 438 * @see #NETWORK_TYPE_UNKNOWN 439 * @see #NETWORK_TYPE_GPRS 440 * @see #NETWORK_TYPE_EDGE 441 * @see #NETWORK_TYPE_UMTS 442 * @see #NETWORK_TYPE_HSDPA 443 * @see #NETWORK_TYPE_HSUPA 444 * @see #NETWORK_TYPE_HSPA 445 * @see #NETWORK_TYPE_CDMA 446 * @see #NETWORK_TYPE_EVDO_0 447 * @see #NETWORK_TYPE_EVDO_A 448 * @see #NETWORK_TYPE_EVDO_B 449 * @see #NETWORK_TYPE_1xRTT 450 * @see #NETWORK_TYPE_IDEN 451 * @see #NETWORK_TYPE_LTE 452 * @see #NETWORK_TYPE_EHRPD 453 * @see #NETWORK_TYPE_HSPAP 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 /** Unknown network class. {@hide} */ 474 public static final int NETWORK_CLASS_UNKNOWN = 0; 475 /** Class of broadly defined "2G" networks. {@hide} */ 476 public static final int NETWORK_CLASS_2_G = 1; 477 /** Class of broadly defined "3G" networks. {@hide} */ 478 public static final int NETWORK_CLASS_3_G = 2; 479 /** Class of broadly defined "4G" networks. {@hide} */ 480 public static final int NETWORK_CLASS_4_G = 3; 481 482 /** 483 * Return general class of network type, such as "3G" or "4G". In cases 484 * where classification is contentious, this method is conservative. 485 * 486 * @hide 487 */ 488 public static int getNetworkClass(int networkType) { 489 switch (networkType) { 490 case NETWORK_TYPE_GPRS: 491 case NETWORK_TYPE_EDGE: 492 case NETWORK_TYPE_CDMA: 493 case NETWORK_TYPE_1xRTT: 494 case NETWORK_TYPE_IDEN: 495 return NETWORK_CLASS_2_G; 496 case NETWORK_TYPE_UMTS: 497 case NETWORK_TYPE_EVDO_0: 498 case NETWORK_TYPE_EVDO_A: 499 case NETWORK_TYPE_HSDPA: 500 case NETWORK_TYPE_HSUPA: 501 case NETWORK_TYPE_HSPA: 502 case NETWORK_TYPE_EVDO_B: 503 case NETWORK_TYPE_EHRPD: 504 case NETWORK_TYPE_HSPAP: 505 return NETWORK_CLASS_3_G; 506 case NETWORK_TYPE_LTE: 507 return NETWORK_CLASS_4_G; 508 default: 509 return NETWORK_CLASS_UNKNOWN; 510 } 511 } 512 513 /** 514 * Returns a string representation of the radio technology (network type) 515 * currently in use on the device. 516 * @return the name of the radio technology 517 * 518 * @hide pending API council review 519 */ 520 public String getNetworkTypeName() { 521 return getNetworkTypeName(getNetworkType()); 522 } 523 524 /** {@hide} */ 525 public static String getNetworkTypeName(int type) { 526 switch (type) { 527 case NETWORK_TYPE_GPRS: 528 return "GPRS"; 529 case NETWORK_TYPE_EDGE: 530 return "EDGE"; 531 case NETWORK_TYPE_UMTS: 532 return "UMTS"; 533 case NETWORK_TYPE_HSDPA: 534 return "HSDPA"; 535 case NETWORK_TYPE_HSUPA: 536 return "HSUPA"; 537 case NETWORK_TYPE_HSPA: 538 return "HSPA"; 539 case NETWORK_TYPE_CDMA: 540 return "CDMA"; 541 case NETWORK_TYPE_EVDO_0: 542 return "CDMA - EvDo rev. 0"; 543 case NETWORK_TYPE_EVDO_A: 544 return "CDMA - EvDo rev. A"; 545 case NETWORK_TYPE_EVDO_B: 546 return "CDMA - EvDo rev. B"; 547 case NETWORK_TYPE_1xRTT: 548 return "CDMA - 1xRTT"; 549 case NETWORK_TYPE_LTE: 550 return "LTE"; 551 case NETWORK_TYPE_EHRPD: 552 return "CDMA - eHRPD"; 553 case NETWORK_TYPE_IDEN: 554 return "iDEN"; 555 case NETWORK_TYPE_HSPAP: 556 return "HSPA+"; 557 default: 558 return "UNKNOWN"; 559 } 560 } 561 562 // 563 // 564 // SIM Card 565 // 566 // 567 568 /** SIM card state: Unknown. Signifies that the SIM is in transition 569 * between states. For example, when the user inputs the SIM pin 570 * under PIN_REQUIRED state, a query for sim status returns 571 * this state before turning to SIM_STATE_READY. */ 572 public static final int SIM_STATE_UNKNOWN = 0; 573 /** SIM card state: no SIM card is available in the device */ 574 public static final int SIM_STATE_ABSENT = 1; 575 /** SIM card state: Locked: requires the user's SIM PIN to unlock */ 576 public static final int SIM_STATE_PIN_REQUIRED = 2; 577 /** SIM card state: Locked: requires the user's SIM PUK to unlock */ 578 public static final int SIM_STATE_PUK_REQUIRED = 3; 579 /** SIM card state: Locked: requries a network PIN to unlock */ 580 public static final int SIM_STATE_NETWORK_LOCKED = 4; 581 /** SIM card state: Ready */ 582 public static final int SIM_STATE_READY = 5; 583 584 /** 585 * @return true if a ICC card is present 586 */ 587 public boolean hasIccCard() { 588 try { 589 return getITelephony().hasIccCard(); 590 } catch (RemoteException ex) { 591 // Assume no ICC card if remote exception which shouldn't happen 592 return false; 593 } catch (NullPointerException ex) { 594 // This could happen before phone restarts due to crashing 595 return false; 596 } 597 } 598 599 /** 600 * Returns a constant indicating the state of the 601 * device SIM card. 602 * 603 * @see #SIM_STATE_UNKNOWN 604 * @see #SIM_STATE_ABSENT 605 * @see #SIM_STATE_PIN_REQUIRED 606 * @see #SIM_STATE_PUK_REQUIRED 607 * @see #SIM_STATE_NETWORK_LOCKED 608 * @see #SIM_STATE_READY 609 */ 610 public int getSimState() { 611 String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE); 612 if ("ABSENT".equals(prop)) { 613 return SIM_STATE_ABSENT; 614 } 615 else if ("PIN_REQUIRED".equals(prop)) { 616 return SIM_STATE_PIN_REQUIRED; 617 } 618 else if ("PUK_REQUIRED".equals(prop)) { 619 return SIM_STATE_PUK_REQUIRED; 620 } 621 else if ("NETWORK_LOCKED".equals(prop)) { 622 return SIM_STATE_NETWORK_LOCKED; 623 } 624 else if ("READY".equals(prop)) { 625 return SIM_STATE_READY; 626 } 627 else { 628 return SIM_STATE_UNKNOWN; 629 } 630 } 631 632 /** 633 * Returns the MCC+MNC (mobile country code + mobile network code) of the 634 * provider of the SIM. 5 or 6 decimal digits. 635 * <p> 636 * Availability: SIM state must be {@link #SIM_STATE_READY} 637 * 638 * @see #getSimState 639 */ 640 public String getSimOperator() { 641 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC); 642 } 643 644 /** 645 * Returns the Service Provider Name (SPN). 646 * <p> 647 * Availability: SIM state must be {@link #SIM_STATE_READY} 648 * 649 * @see #getSimState 650 */ 651 public String getSimOperatorName() { 652 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA); 653 } 654 655 /** 656 * Returns the ISO country code equivalent for the SIM provider's country code. 657 */ 658 public String getSimCountryIso() { 659 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY); 660 } 661 662 /** 663 * Returns the serial number of the SIM, if applicable. Return null if it is 664 * unavailable. 665 * <p> 666 * Requires Permission: 667 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 668 */ 669 public String getSimSerialNumber() { 670 try { 671 return getSubscriberInfo().getIccSerialNumber(); 672 } catch (RemoteException ex) { 673 return null; 674 } catch (NullPointerException ex) { 675 // This could happen before phone restarts due to crashing 676 return null; 677 } 678 } 679 680 /** 681 * Return if the current radio is LTE on CDMA. This 682 * is a tri-state return value as for a period of time 683 * the mode may be unknown. 684 * 685 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE} 686 * or {@link Phone#LTE_ON_CDMA_TRUE} 687 * 688 * @hide 689 */ 690 public int getLteOnCdmaMode() { 691 try { 692 return getITelephony().getLteOnCdmaMode(); 693 } catch (RemoteException ex) { 694 // Assume no ICC card if remote exception which shouldn't happen 695 return Phone.LTE_ON_CDMA_UNKNOWN; 696 } catch (NullPointerException ex) { 697 // This could happen before phone restarts due to crashing 698 return Phone.LTE_ON_CDMA_UNKNOWN; 699 } 700 } 701 702 // 703 // 704 // Subscriber Info 705 // 706 // 707 708 /** 709 * Returns the unique subscriber ID, for example, the IMSI for a GSM phone. 710 * Return null if it is unavailable. 711 * <p> 712 * Requires Permission: 713 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 714 */ 715 public String getSubscriberId() { 716 try { 717 return getSubscriberInfo().getSubscriberId(); 718 } catch (RemoteException ex) { 719 return null; 720 } catch (NullPointerException ex) { 721 // This could happen before phone restarts due to crashing 722 return null; 723 } 724 } 725 726 /** 727 * Returns the phone number string for line 1, for example, the MSISDN 728 * for a GSM phone. Return null if it is unavailable. 729 * <p> 730 * Requires Permission: 731 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 732 */ 733 public String getLine1Number() { 734 try { 735 return getSubscriberInfo().getLine1Number(); 736 } catch (RemoteException ex) { 737 return null; 738 } catch (NullPointerException ex) { 739 // This could happen before phone restarts due to crashing 740 return null; 741 } 742 } 743 744 /** 745 * Returns the alphabetic identifier associated with the line 1 number. 746 * Return null if it is unavailable. 747 * <p> 748 * Requires Permission: 749 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 750 * @hide 751 * nobody seems to call this. 752 */ 753 public String getLine1AlphaTag() { 754 try { 755 return getSubscriberInfo().getLine1AlphaTag(); 756 } catch (RemoteException ex) { 757 return null; 758 } catch (NullPointerException ex) { 759 // This could happen before phone restarts due to crashing 760 return null; 761 } 762 } 763 764 /** 765 * Returns the voice mail number. Return null if it is unavailable. 766 * <p> 767 * Requires Permission: 768 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 769 */ 770 public String getVoiceMailNumber() { 771 try { 772 return getSubscriberInfo().getVoiceMailNumber(); 773 } catch (RemoteException ex) { 774 return null; 775 } catch (NullPointerException ex) { 776 // This could happen before phone restarts due to crashing 777 return null; 778 } 779 } 780 781 /** 782 * Returns the complete voice mail number. Return null if it is unavailable. 783 * <p> 784 * Requires Permission: 785 * {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED} 786 * 787 * @hide 788 */ 789 public String getCompleteVoiceMailNumber() { 790 try { 791 return getSubscriberInfo().getCompleteVoiceMailNumber(); 792 } catch (RemoteException ex) { 793 return null; 794 } catch (NullPointerException ex) { 795 // This could happen before phone restarts due to crashing 796 return null; 797 } 798 } 799 800 /** 801 * Returns the voice mail count. Return 0 if unavailable. 802 * <p> 803 * Requires Permission: 804 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 805 * @hide 806 */ 807 public int getVoiceMessageCount() { 808 try { 809 return getITelephony().getVoiceMessageCount(); 810 } catch (RemoteException ex) { 811 return 0; 812 } catch (NullPointerException ex) { 813 // This could happen before phone restarts due to crashing 814 return 0; 815 } 816 } 817 818 /** 819 * Retrieves the alphabetic identifier associated with the voice 820 * mail number. 821 * <p> 822 * Requires Permission: 823 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 824 */ 825 public String getVoiceMailAlphaTag() { 826 try { 827 return getSubscriberInfo().getVoiceMailAlphaTag(); 828 } catch (RemoteException ex) { 829 return null; 830 } catch (NullPointerException ex) { 831 // This could happen before phone restarts due to crashing 832 return null; 833 } 834 } 835 836 /** 837 * Returns the IMS private user identity (IMPI) that was loaded from the ISIM. 838 * @return the IMPI, or null if not present or not loaded 839 * @hide 840 */ 841 public String getIsimImpi() { 842 try { 843 return getSubscriberInfo().getIsimImpi(); 844 } catch (RemoteException ex) { 845 return null; 846 } catch (NullPointerException ex) { 847 // This could happen before phone restarts due to crashing 848 return null; 849 } 850 } 851 852 /** 853 * Returns the IMS home network domain name that was loaded from the ISIM. 854 * @return the IMS domain name, or null if not present or not loaded 855 * @hide 856 */ 857 public String getIsimDomain() { 858 try { 859 return getSubscriberInfo().getIsimDomain(); 860 } catch (RemoteException ex) { 861 return null; 862 } catch (NullPointerException ex) { 863 // This could happen before phone restarts due to crashing 864 return null; 865 } 866 } 867 868 /** 869 * Returns the IMS public user identities (IMPU) that were loaded from the ISIM. 870 * @return an array of IMPU strings, with one IMPU per string, or null if 871 * not present or not loaded 872 * @hide 873 */ 874 public String[] getIsimImpu() { 875 try { 876 return getSubscriberInfo().getIsimImpu(); 877 } catch (RemoteException ex) { 878 return null; 879 } catch (NullPointerException ex) { 880 // This could happen before phone restarts due to crashing 881 return null; 882 } 883 } 884 885 private IPhoneSubInfo getSubscriberInfo() { 886 // get it each time because that process crashes a lot 887 return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo")); 888 } 889 890 891 /** Device call state: No activity. */ 892 public static final int CALL_STATE_IDLE = 0; 893 /** Device call state: Ringing. A new call arrived and is 894 * ringing or waiting. In the latter case, another call is 895 * already active. */ 896 public static final int CALL_STATE_RINGING = 1; 897 /** Device call state: Off-hook. At least one call exists 898 * that is dialing, active, or on hold, and no calls are ringing 899 * or waiting. */ 900 public static final int CALL_STATE_OFFHOOK = 2; 901 902 /** 903 * Returns a constant indicating the call state (cellular) on the device. 904 */ 905 public int getCallState() { 906 try { 907 return getITelephony().getCallState(); 908 } catch (RemoteException ex) { 909 // the phone process is restarting. 910 return CALL_STATE_IDLE; 911 } catch (NullPointerException ex) { 912 // the phone process is restarting. 913 return CALL_STATE_IDLE; 914 } 915 } 916 917 /** Data connection activity: No traffic. */ 918 public static final int DATA_ACTIVITY_NONE = 0x00000000; 919 /** Data connection activity: Currently receiving IP PPP traffic. */ 920 public static final int DATA_ACTIVITY_IN = 0x00000001; 921 /** Data connection activity: Currently sending IP PPP traffic. */ 922 public static final int DATA_ACTIVITY_OUT = 0x00000002; 923 /** Data connection activity: Currently both sending and receiving 924 * IP PPP traffic. */ 925 public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT; 926 /** 927 * Data connection is active, but physical link is down 928 */ 929 public static final int DATA_ACTIVITY_DORMANT = 0x00000004; 930 931 /** 932 * Returns a constant indicating the type of activity on a data connection 933 * (cellular). 934 * 935 * @see #DATA_ACTIVITY_NONE 936 * @see #DATA_ACTIVITY_IN 937 * @see #DATA_ACTIVITY_OUT 938 * @see #DATA_ACTIVITY_INOUT 939 * @see #DATA_ACTIVITY_DORMANT 940 */ 941 public int getDataActivity() { 942 try { 943 return getITelephony().getDataActivity(); 944 } catch (RemoteException ex) { 945 // the phone process is restarting. 946 return DATA_ACTIVITY_NONE; 947 } catch (NullPointerException ex) { 948 // the phone process is restarting. 949 return DATA_ACTIVITY_NONE; 950 } 951 } 952 953 /** Data connection state: Unknown. Used before we know the state. 954 * @hide 955 */ 956 public static final int DATA_UNKNOWN = -1; 957 /** Data connection state: Disconnected. IP traffic not available. */ 958 public static final int DATA_DISCONNECTED = 0; 959 /** Data connection state: Currently setting up a data connection. */ 960 public static final int DATA_CONNECTING = 1; 961 /** Data connection state: Connected. IP traffic should be available. */ 962 public static final int DATA_CONNECTED = 2; 963 /** Data connection state: Suspended. The connection is up, but IP 964 * traffic is temporarily unavailable. For example, in a 2G network, 965 * data activity may be suspended when a voice call arrives. */ 966 public static final int DATA_SUSPENDED = 3; 967 968 /** 969 * Returns a constant indicating the current data connection state 970 * (cellular). 971 * 972 * @see #DATA_DISCONNECTED 973 * @see #DATA_CONNECTING 974 * @see #DATA_CONNECTED 975 * @see #DATA_SUSPENDED 976 */ 977 public int getDataState() { 978 try { 979 return getITelephony().getDataState(); 980 } catch (RemoteException ex) { 981 // the phone process is restarting. 982 return DATA_DISCONNECTED; 983 } catch (NullPointerException ex) { 984 return DATA_DISCONNECTED; 985 } 986 } 987 988 private ITelephony getITelephony() { 989 return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE)); 990 } 991 992 // 993 // 994 // PhoneStateListener 995 // 996 // 997 998 /** 999 * Registers a listener object to receive notification of changes 1000 * in specified telephony states. 1001 * <p> 1002 * To register a listener, pass a {@link PhoneStateListener} 1003 * and specify at least one telephony state of interest in 1004 * the events argument. 1005 * 1006 * At registration, and when a specified telephony state 1007 * changes, the telephony manager invokes the appropriate 1008 * callback method on the listener object and passes the 1009 * current (udpated) values. 1010 * <p> 1011 * To unregister a listener, pass the listener object and set the 1012 * events argument to 1013 * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0). 1014 * 1015 * @param listener The {@link PhoneStateListener} object to register 1016 * (or unregister) 1017 * @param events The telephony state(s) of interest to the listener, 1018 * as a bitwise-OR combination of {@link PhoneStateListener} 1019 * LISTEN_ flags. 1020 */ 1021 public void listen(PhoneStateListener listener, int events) { 1022 String pkgForDebug = sContext != null ? sContext.getPackageName() : "<unknown>"; 1023 try { 1024 Boolean notifyNow = (getITelephony() != null); 1025 sRegistry.listen(pkgForDebug, listener.callback, events, notifyNow); 1026 } catch (RemoteException ex) { 1027 // system process dead 1028 } catch (NullPointerException ex) { 1029 // system process dead 1030 } 1031 } 1032 1033 /** 1034 * Returns the CDMA ERI icon index to display 1035 * 1036 * @hide 1037 */ 1038 public int getCdmaEriIconIndex() { 1039 try { 1040 return getITelephony().getCdmaEriIconIndex(); 1041 } catch (RemoteException ex) { 1042 // the phone process is restarting. 1043 return -1; 1044 } catch (NullPointerException ex) { 1045 return -1; 1046 } 1047 } 1048 1049 /** 1050 * Returns the CDMA ERI icon mode, 1051 * 0 - ON 1052 * 1 - FLASHING 1053 * 1054 * @hide 1055 */ 1056 public int getCdmaEriIconMode() { 1057 try { 1058 return getITelephony().getCdmaEriIconMode(); 1059 } catch (RemoteException ex) { 1060 // the phone process is restarting. 1061 return -1; 1062 } catch (NullPointerException ex) { 1063 return -1; 1064 } 1065 } 1066 1067 /** 1068 * Returns the CDMA ERI text, 1069 * 1070 * @hide 1071 */ 1072 public String getCdmaEriText() { 1073 try { 1074 return getITelephony().getCdmaEriText(); 1075 } catch (RemoteException ex) { 1076 // the phone process is restarting. 1077 return null; 1078 } catch (NullPointerException ex) { 1079 return null; 1080 } 1081 } 1082 1083 /** 1084 * @return true if the current device is "voice capable". 1085 * <p> 1086 * "Voice capable" means that this device supports circuit-switched 1087 * (i.e. voice) phone calls over the telephony network, and is allowed 1088 * to display the in-call UI while a cellular voice call is active. 1089 * This will be false on "data only" devices which can't make voice 1090 * calls and don't support any in-call UI. 1091 * <p> 1092 * Note: the meaning of this flag is subtly different from the 1093 * PackageManager.FEATURE_TELEPHONY system feature, which is available 1094 * on any device with a telephony radio, even if the device is 1095 * data-only. 1096 * 1097 * @hide pending API review 1098 */ 1099 public boolean isVoiceCapable() { 1100 if (sContext == null) return true; 1101 return sContext.getResources().getBoolean( 1102 com.android.internal.R.bool.config_voice_capable); 1103 } 1104 1105 /** 1106 * @return true if the current device supports sms service. 1107 * <p> 1108 * If true, this means that the device supports both sending and 1109 * receiving sms via the telephony network. 1110 * <p> 1111 * Note: Voicemail waiting sms, cell broadcasting sms, and MMS are 1112 * disabled when device doesn't support sms. 1113 * 1114 * @hide pending API review 1115 */ 1116 public boolean isSmsCapable() { 1117 if (sContext == null) return true; 1118 return sContext.getResources().getBoolean( 1119 com.android.internal.R.bool.config_sms_capable); 1120 } 1121} 1122