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