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