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