TelephonyManager.java revision fb2b04b7bc505f5e600e360a9de6f63e16c21bf9
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 acess 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 * 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 return CellLocation.newFromBundle(bundle); 207 } catch (RemoteException ex) { 208 return null; 209 } catch (NullPointerException ex) { 210 return null; 211 } 212 } 213 214 /** 215 * Enables location update notifications. {@link PhoneStateListener#onCellLocationChanged 216 * PhoneStateListener.onCellLocationChanged} will be called on location updates. 217 * 218 * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES 219 * CONTROL_LOCATION_UPDATES} 220 * 221 * @hide 222 */ 223 public void enableLocationUpdates() { 224 try { 225 getITelephony().enableLocationUpdates(); 226 } catch (RemoteException ex) { 227 } catch (NullPointerException ex) { 228 } 229 } 230 231 /** 232 * Disables location update notifications. {@link PhoneStateListener#onCellLocationChanged 233 * PhoneStateListener.onCellLocationChanged} will be called on location updates. 234 * 235 * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES 236 * CONTROL_LOCATION_UPDATES} 237 * 238 * @hide 239 */ 240 public void disableLocationUpdates() { 241 try { 242 getITelephony().disableLocationUpdates(); 243 } catch (RemoteException ex) { 244 } catch (NullPointerException ex) { 245 } 246 } 247 248 /** 249 * Returns the neighboring cell information of the device. 250 * 251 * @return List of NeighboringCellInfo or null if info unavailable. 252 * 253 * <p>Requires Permission: 254 * (@link android.Manifest.permission#ACCESS_COARSE_UPDATES} 255 */ 256 public List<NeighboringCellInfo> getNeighboringCellInfo() { 257 try { 258 return getITelephony().getNeighboringCellInfo(); 259 } catch (RemoteException ex) { 260 return null; 261 } catch (NullPointerException ex) { 262 return null; 263 } 264 } 265 266 /** No phone radio. */ 267 public static final int PHONE_TYPE_NONE = Phone.PHONE_TYPE_NONE; 268 /** Phone radio is GSM. */ 269 public static final int PHONE_TYPE_GSM = Phone.PHONE_TYPE_GSM; 270 /** Phone radio is CDMA. */ 271 public static final int PHONE_TYPE_CDMA = Phone.PHONE_TYPE_CDMA; 272 273 /** 274 * Returns a constant indicating the device phone type. 275 * 276 * @see #PHONE_TYPE_NONE 277 * @see #PHONE_TYPE_GSM 278 * @see #PHONE_TYPE_CDMA 279 */ 280 public int getPhoneType() { 281 try{ 282 ITelephony telephony = getITelephony(); 283 if (telephony != null) { 284 return telephony.getActivePhoneType(); 285 } else { 286 // This can happen when the ITelephony interface is not up yet. 287 return getPhoneTypeFromProperty(); 288 } 289 } catch (RemoteException ex) { 290 // This shouldn't happen in the normal case, as a backup we 291 // read from the system property. 292 return getPhoneTypeFromProperty(); 293 } catch (NullPointerException ex) { 294 // This shouldn't happen in the normal case, as a backup we 295 // read from the system property. 296 return getPhoneTypeFromProperty(); 297 } 298 } 299 300 301 private int getPhoneTypeFromProperty() { 302 int type = 303 SystemProperties.getInt(TelephonyProperties.CURRENT_ACTIVE_PHONE, 304 getPhoneTypeFromNetworkType()); 305 return type; 306 } 307 308 private int getPhoneTypeFromNetworkType() { 309 // When the system property CURRENT_ACTIVE_PHONE, has not been set, 310 // use the system property for default network type. 311 // This is a fail safe, and can only happen at first boot. 312 int mode = SystemProperties.getInt("ro.telephony.default_network", -1); 313 if (mode == -1) 314 return PHONE_TYPE_NONE; 315 return PhoneFactory.getPhoneType(mode); 316 } 317 // 318 // 319 // Current Network 320 // 321 // 322 323 /** 324 * Returns the alphabetic name of current registered operator. 325 * <p> 326 * Availability: Only when user is registered to a network. Result may be 327 * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if 328 * on a CDMA network). 329 */ 330 public String getNetworkOperatorName() { 331 return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ALPHA); 332 } 333 334 /** 335 * Returns the numeric name (MCC+MNC) of current registered operator. 336 * <p> 337 * Availability: Only when user is registered to a network. Result may be 338 * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if 339 * on a CDMA network). 340 */ 341 public String getNetworkOperator() { 342 return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_NUMERIC); 343 } 344 345 /** 346 * Returns true if the device is considered roaming on the current 347 * network, for GSM purposes. 348 * <p> 349 * Availability: Only when user registered to a network. 350 */ 351 public boolean isNetworkRoaming() { 352 return "true".equals(SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING)); 353 } 354 355 /** 356 * Returns the ISO country code equivilent of the current registered 357 * operator's MCC (Mobile Country Code). 358 * <p> 359 * Availability: Only when user is registered to a network. Result may be 360 * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if 361 * on a CDMA network). 362 */ 363 public String getNetworkCountryIso() { 364 return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY); 365 } 366 367 /** Network type is unknown */ 368 public static final int NETWORK_TYPE_UNKNOWN = 0; 369 /** Current network is GPRS */ 370 public static final int NETWORK_TYPE_GPRS = 1; 371 /** Current network is EDGE */ 372 public static final int NETWORK_TYPE_EDGE = 2; 373 /** Current network is UMTS */ 374 public static final int NETWORK_TYPE_UMTS = 3; 375 /** Current network is CDMA: Either IS95A or IS95B*/ 376 public static final int NETWORK_TYPE_CDMA = 4; 377 /** Current network is EVDO revision 0*/ 378 public static final int NETWORK_TYPE_EVDO_0 = 5; 379 /** Current network is EVDO revision A*/ 380 public static final int NETWORK_TYPE_EVDO_A = 6; 381 /** Current network is 1xRTT*/ 382 public static final int NETWORK_TYPE_1xRTT = 7; 383 /** Current network is HSDPA */ 384 public static final int NETWORK_TYPE_HSDPA = 8; 385 /** Current network is HSUPA */ 386 public static final int NETWORK_TYPE_HSUPA = 9; 387 /** Current network is HSPA */ 388 public static final int NETWORK_TYPE_HSPA = 10; 389 390 /** 391 * Returns a constant indicating the radio technology (network type) 392 * currently in use on the device. 393 * @return the network type 394 * 395 * @see #NETWORK_TYPE_UNKNOWN 396 * @see #NETWORK_TYPE_GPRS 397 * @see #NETWORK_TYPE_EDGE 398 * @see #NETWORK_TYPE_UMTS 399 * @see #NETWORK_TYPE_HSDPA 400 * @see #NETWORK_TYPE_HSUPA 401 * @see #NETWORK_TYPE_HSPA 402 * @see #NETWORK_TYPE_CDMA 403 * @see #NETWORK_TYPE_EVDO_0 404 * @see #NETWORK_TYPE_EVDO_A 405 * @see #NETWORK_TYPE_1xRTT 406 */ 407 public int getNetworkType() { 408 try{ 409 ITelephony telephony = getITelephony(); 410 if (telephony != null) { 411 return telephony.getNetworkType(); 412 } else { 413 // This can happen when the ITelephony interface is not up yet. 414 return NETWORK_TYPE_UNKNOWN; 415 } 416 } catch(RemoteException ex) { 417 // This shouldn't happen in the normal case 418 return NETWORK_TYPE_UNKNOWN; 419 } catch (NullPointerException ex) { 420 // This could happen before phone restarts due to crashing 421 return NETWORK_TYPE_UNKNOWN; 422 } 423 } 424 425 /** 426 * Returns a string representation of the radio technology (network type) 427 * currently in use on the device. 428 * @return the name of the radio technology 429 * 430 * @hide pending API council review 431 */ 432 public String getNetworkTypeName() { 433 switch (getNetworkType()) { 434 case NETWORK_TYPE_GPRS: 435 return "GPRS"; 436 case NETWORK_TYPE_EDGE: 437 return "EDGE"; 438 case NETWORK_TYPE_UMTS: 439 return "UMTS"; 440 case NETWORK_TYPE_HSDPA: 441 return "HSDPA"; 442 case NETWORK_TYPE_HSUPA: 443 return "HSUPA"; 444 case NETWORK_TYPE_HSPA: 445 return "HSPA"; 446 case NETWORK_TYPE_CDMA: 447 return "CDMA"; 448 case NETWORK_TYPE_EVDO_0: 449 return "CDMA - EvDo rev. 0"; 450 case NETWORK_TYPE_EVDO_A: 451 return "CDMA - EvDo rev. A"; 452 case NETWORK_TYPE_1xRTT: 453 return "CDMA - 1xRTT"; 454 default: 455 return "UNKNOWN"; 456 } 457 } 458 459 // 460 // 461 // SIM Card 462 // 463 // 464 465 /** SIM card state: Unknown. Signifies that the SIM is in transition 466 * between states. For example, when the user inputs the SIM pin 467 * under PIN_REQUIRED state, a query for sim status returns 468 * this state before turning to SIM_STATE_READY. */ 469 public static final int SIM_STATE_UNKNOWN = 0; 470 /** SIM card state: no SIM card is available in the device */ 471 public static final int SIM_STATE_ABSENT = 1; 472 /** SIM card state: Locked: requires the user's SIM PIN to unlock */ 473 public static final int SIM_STATE_PIN_REQUIRED = 2; 474 /** SIM card state: Locked: requires the user's SIM PUK to unlock */ 475 public static final int SIM_STATE_PUK_REQUIRED = 3; 476 /** SIM card state: Locked: requries a network PIN to unlock */ 477 public static final int SIM_STATE_NETWORK_LOCKED = 4; 478 /** SIM card state: Ready */ 479 public static final int SIM_STATE_READY = 5; 480 481 /** 482 * @return true if a ICC card is present 483 */ 484 public boolean hasIccCard() { 485 try { 486 return getITelephony().hasIccCard(); 487 } catch (RemoteException ex) { 488 // Assume no ICC card if remote exception which shouldn't happen 489 return false; 490 } catch (NullPointerException ex) { 491 // This could happen before phone restarts due to crashing 492 return false; 493 } 494 } 495 496 /** 497 * Returns a constant indicating the state of the 498 * device SIM card. 499 * 500 * @see #SIM_STATE_UNKNOWN 501 * @see #SIM_STATE_ABSENT 502 * @see #SIM_STATE_PIN_REQUIRED 503 * @see #SIM_STATE_PUK_REQUIRED 504 * @see #SIM_STATE_NETWORK_LOCKED 505 * @see #SIM_STATE_READY 506 */ 507 public int getSimState() { 508 String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE); 509 if ("ABSENT".equals(prop)) { 510 return SIM_STATE_ABSENT; 511 } 512 else if ("PIN_REQUIRED".equals(prop)) { 513 return SIM_STATE_PIN_REQUIRED; 514 } 515 else if ("PUK_REQUIRED".equals(prop)) { 516 return SIM_STATE_PUK_REQUIRED; 517 } 518 else if ("NETWORK_LOCKED".equals(prop)) { 519 return SIM_STATE_NETWORK_LOCKED; 520 } 521 else if ("READY".equals(prop)) { 522 return SIM_STATE_READY; 523 } 524 else { 525 return SIM_STATE_UNKNOWN; 526 } 527 } 528 529 /** 530 * Returns the MCC+MNC (mobile country code + mobile network code) of the 531 * provider of the SIM. 5 or 6 decimal digits. 532 * <p> 533 * Availability: SIM state must be {@link #SIM_STATE_READY} 534 * 535 * @see #getSimState 536 */ 537 public String getSimOperator() { 538 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC); 539 } 540 541 /** 542 * Returns the Service Provider Name (SPN). 543 * <p> 544 * Availability: SIM state must be {@link #SIM_STATE_READY} 545 * 546 * @see #getSimState 547 */ 548 public String getSimOperatorName() { 549 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA); 550 } 551 552 /** 553 * Returns the ISO country code equivalent for the SIM provider's country code. 554 */ 555 public String getSimCountryIso() { 556 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY); 557 } 558 559 /** 560 * Returns the serial number of the SIM, if applicable. Return null if it is 561 * unavailable. 562 * <p> 563 * Requires Permission: 564 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 565 */ 566 public String getSimSerialNumber() { 567 try { 568 return getSubscriberInfo().getIccSerialNumber(); 569 } catch (RemoteException ex) { 570 return null; 571 } catch (NullPointerException ex) { 572 // This could happen before phone restarts due to crashing 573 return null; 574 } 575 } 576 577 // 578 // 579 // Subscriber Info 580 // 581 // 582 583 /** 584 * Returns the unique subscriber ID, for example, the IMSI for a GSM phone. 585 * Return null if it is unavailable. 586 * <p> 587 * Requires Permission: 588 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 589 */ 590 public String getSubscriberId() { 591 try { 592 return getSubscriberInfo().getSubscriberId(); 593 } catch (RemoteException ex) { 594 return null; 595 } catch (NullPointerException ex) { 596 // This could happen before phone restarts due to crashing 597 return null; 598 } 599 } 600 601 /** 602 * Returns the phone number string for line 1, for example, the MSISDN 603 * for a GSM phone. Return null if it is unavailable. 604 * <p> 605 * Requires Permission: 606 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 607 */ 608 public String getLine1Number() { 609 try { 610 return getSubscriberInfo().getLine1Number(); 611 } catch (RemoteException ex) { 612 return null; 613 } catch (NullPointerException ex) { 614 // This could happen before phone restarts due to crashing 615 return null; 616 } 617 } 618 619 /** 620 * Returns the alphabetic identifier associated with the line 1 number. 621 * Return null if it is unavailable. 622 * <p> 623 * Requires Permission: 624 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 625 * @hide 626 * nobody seems to call this. 627 */ 628 public String getLine1AlphaTag() { 629 try { 630 return getSubscriberInfo().getLine1AlphaTag(); 631 } catch (RemoteException ex) { 632 return null; 633 } catch (NullPointerException ex) { 634 // This could happen before phone restarts due to crashing 635 return null; 636 } 637 } 638 639 /** 640 * Returns the voice mail number. Return null if it is unavailable. 641 * <p> 642 * Requires Permission: 643 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 644 */ 645 public String getVoiceMailNumber() { 646 try { 647 return getSubscriberInfo().getVoiceMailNumber(); 648 } catch (RemoteException ex) { 649 return null; 650 } catch (NullPointerException ex) { 651 // This could happen before phone restarts due to crashing 652 return null; 653 } 654 } 655 656 /** 657 * Returns the voice mail count. Return 0 if unavailable. 658 * <p> 659 * Requires Permission: 660 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 661 * @hide 662 */ 663 public int getVoiceMessageCount() { 664 try { 665 return getITelephony().getVoiceMessageCount(); 666 } catch (RemoteException ex) { 667 return 0; 668 } catch (NullPointerException ex) { 669 // This could happen before phone restarts due to crashing 670 return 0; 671 } 672 } 673 674 /** 675 * Retrieves the alphabetic identifier associated with the voice 676 * mail number. 677 * <p> 678 * Requires Permission: 679 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 680 */ 681 public String getVoiceMailAlphaTag() { 682 try { 683 return getSubscriberInfo().getVoiceMailAlphaTag(); 684 } catch (RemoteException ex) { 685 return null; 686 } catch (NullPointerException ex) { 687 // This could happen before phone restarts due to crashing 688 return null; 689 } 690 } 691 692 private IPhoneSubInfo getSubscriberInfo() { 693 // get it each time because that process crashes a lot 694 return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo")); 695 } 696 697 698 /** Device call state: No activity. */ 699 public static final int CALL_STATE_IDLE = 0; 700 /** Device call state: Ringing. A new call arrived and is 701 * ringing or waiting. In the latter case, another call is 702 * already active. */ 703 public static final int CALL_STATE_RINGING = 1; 704 /** Device call state: Off-hook. At least one call exists 705 * that is dialing, active, or on hold, and no calls are ringing 706 * or waiting. */ 707 public static final int CALL_STATE_OFFHOOK = 2; 708 709 /** 710 * Returns a constant indicating the call state (cellular) on the device. 711 */ 712 public int getCallState() { 713 try { 714 return getITelephony().getCallState(); 715 } catch (RemoteException ex) { 716 // the phone process is restarting. 717 return CALL_STATE_IDLE; 718 } catch (NullPointerException ex) { 719 // the phone process is restarting. 720 return CALL_STATE_IDLE; 721 } 722 } 723 724 /** Data connection activity: No traffic. */ 725 public static final int DATA_ACTIVITY_NONE = 0x00000000; 726 /** Data connection activity: Currently receiving IP PPP traffic. */ 727 public static final int DATA_ACTIVITY_IN = 0x00000001; 728 /** Data connection activity: Currently sending IP PPP traffic. */ 729 public static final int DATA_ACTIVITY_OUT = 0x00000002; 730 /** Data connection activity: Currently both sending and receiving 731 * IP PPP traffic. */ 732 public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT; 733 /** 734 * Data connection is active, but physical link is down 735 * @hide 736 */ 737 public static final int DATA_ACTIVITY_DORMANT = 0x00000004; 738 739 /** 740 * Returns a constant indicating the type of activity on a data connection 741 * (cellular). 742 * 743 * @see #DATA_ACTIVITY_NONE 744 * @see #DATA_ACTIVITY_IN 745 * @see #DATA_ACTIVITY_OUT 746 * @see #DATA_ACTIVITY_INOUT 747 * @see #DATA_ACTIVITY_DORMANT 748 */ 749 public int getDataActivity() { 750 try { 751 return getITelephony().getDataActivity(); 752 } catch (RemoteException ex) { 753 // the phone process is restarting. 754 return DATA_ACTIVITY_NONE; 755 } catch (NullPointerException ex) { 756 // the phone process is restarting. 757 return DATA_ACTIVITY_NONE; 758 } 759 } 760 761 /** Data connection state: Disconnected. IP traffic not available. */ 762 public static final int DATA_DISCONNECTED = 0; 763 /** Data connection state: Currently setting up a data connection. */ 764 public static final int DATA_CONNECTING = 1; 765 /** Data connection state: Connected. IP traffic should be available. */ 766 public static final int DATA_CONNECTED = 2; 767 /** Data connection state: Suspended. The connection is up, but IP 768 * traffic is temporarily unavailable. For example, in a 2G network, 769 * data activity may be suspended when a voice call arrives. */ 770 public static final int DATA_SUSPENDED = 3; 771 772 /** 773 * Returns a constant indicating the current data connection state 774 * (cellular). 775 * 776 * @see #DATA_DISCONNECTED 777 * @see #DATA_CONNECTING 778 * @see #DATA_CONNECTED 779 * @see #DATA_SUSPENDED 780 */ 781 public int getDataState() { 782 try { 783 return getITelephony().getDataState(); 784 } catch (RemoteException ex) { 785 // the phone process is restarting. 786 return DATA_DISCONNECTED; 787 } catch (NullPointerException ex) { 788 return DATA_DISCONNECTED; 789 } 790 } 791 792 private ITelephony getITelephony() { 793 return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE)); 794 } 795 796 // 797 // 798 // PhoneStateListener 799 // 800 // 801 802 /** 803 * Registers a listener object to receive notification of changes 804 * in specified telephony states. 805 * <p> 806 * To register a listener, pass a {@link PhoneStateListener} 807 * and specify at least one telephony state of interest in 808 * the events argument. 809 * 810 * At registration, and when a specified telephony state 811 * changes, the telephony manager invokes the appropriate 812 * callback method on the listener object and passes the 813 * current (udpated) values. 814 * <p> 815 * To unregister a listener, pass the listener object and set the 816 * events argument to 817 * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0). 818 * 819 * @param listener The {@link PhoneStateListener} object to register 820 * (or unregister) 821 * @param events The telephony state(s) of interest to the listener, 822 * as a bitwise-OR combination of {@link PhoneStateListener} 823 * LISTEN_ flags. 824 */ 825 public void listen(PhoneStateListener listener, int events) { 826 String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>"; 827 try { 828 Boolean notifyNow = (getITelephony() != null); 829 mRegistry.listen(pkgForDebug, listener.callback, events, notifyNow); 830 } catch (RemoteException ex) { 831 // system process dead 832 } catch (NullPointerException ex) { 833 // system process dead 834 } 835 } 836 837 /** 838 * Returns the CDMA ERI icon index to display 839 * 840 * @hide 841 */ 842 public int getCdmaEriIconIndex() { 843 try { 844 return getITelephony().getCdmaEriIconIndex(); 845 } catch (RemoteException ex) { 846 // the phone process is restarting. 847 return -1; 848 } catch (NullPointerException ex) { 849 return -1; 850 } 851 } 852 853 /** 854 * Returns the CDMA ERI icon mode, 855 * 0 - ON 856 * 1 - FLASHING 857 * 858 * @hide 859 */ 860 public int getCdmaEriIconMode() { 861 try { 862 return getITelephony().getCdmaEriIconMode(); 863 } catch (RemoteException ex) { 864 // the phone process is restarting. 865 return -1; 866 } catch (NullPointerException ex) { 867 return -1; 868 } 869 } 870 871 /** 872 * Returns the CDMA ERI text, 873 * 874 * @hide 875 */ 876 public String getCdmaEriText() { 877 try { 878 return getITelephony().getCdmaEriText(); 879 } catch (RemoteException ex) { 880 // the phone process is restarting. 881 return null; 882 } catch (NullPointerException ex) { 883 return null; 884 } 885 } 886} 887