TelephonyManager.java revision 10731a6140ad9e1be895406206ced1db7b6d85cd
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.telephony.Rlog; 27 28import com.android.internal.telephony.IPhoneSubInfo; 29import com.android.internal.telephony.ITelephony; 30import com.android.internal.telephony.ITelephonyRegistry; 31import com.android.internal.telephony.PhoneConstants; 32import com.android.internal.telephony.RILConstants; 33import com.android.internal.telephony.TelephonyProperties; 34 35import java.io.FileInputStream; 36import java.io.IOException; 37import java.util.List; 38import java.util.regex.Matcher; 39import java.util.regex.Pattern; 40 41/** 42 * Provides access to information about the telephony services on 43 * the device. Applications can use the methods in this class to 44 * determine telephony services and states, as well as to access some 45 * types of subscriber information. Applications can also register 46 * a listener to receive notification of telephony state changes. 47 * <p> 48 * You do not instantiate this class directly; instead, you retrieve 49 * a reference to an instance through 50 * {@link android.content.Context#getSystemService 51 * Context.getSystemService(Context.TELEPHONY_SERVICE)}. 52 * <p> 53 * Note that access to some telephony information is 54 * permission-protected. Your application cannot access the protected 55 * information unless it has the appropriate permissions declared in 56 * its manifest file. Where permissions apply, they are noted in the 57 * the methods through which you access the protected information. 58 */ 59public class TelephonyManager { 60 private static final String TAG = "TelephonyManager"; 61 62 private static ITelephonyRegistry sRegistry; 63 private final Context mContext; 64 65 /** @hide */ 66 public TelephonyManager(Context context) { 67 Context appContext = context.getApplicationContext(); 68 if (appContext != null) { 69 mContext = appContext; 70 } else { 71 mContext = context; 72 } 73 74 if (sRegistry == null) { 75 sRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService( 76 "telephony.registry")); 77 } 78 } 79 80 /** @hide */ 81 private TelephonyManager() { 82 mContext = null; 83 } 84 85 private static TelephonyManager sInstance = new TelephonyManager(); 86 87 /** @hide 88 /* @deprecated - use getSystemService as described above */ 89 public static TelephonyManager getDefault() { 90 return sInstance; 91 } 92 93 /** {@hide} */ 94 public static TelephonyManager from(Context context) { 95 return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 96 } 97 98 // 99 // Broadcast Intent actions 100 // 101 102 /** 103 * Broadcast intent action indicating that the call state (cellular) 104 * on the device has changed. 105 * 106 * <p> 107 * The {@link #EXTRA_STATE} extra indicates the new call state. 108 * If the new state is RINGING, a second extra 109 * {@link #EXTRA_INCOMING_NUMBER} provides the incoming phone number as 110 * a String. 111 * 112 * <p class="note"> 113 * Requires the READ_PHONE_STATE permission. 114 * 115 * <p class="note"> 116 * This was a {@link android.content.Context#sendStickyBroadcast sticky} 117 * broadcast in version 1.0, but it is no longer sticky. 118 * Instead, use {@link #getCallState} to synchronously query the current call state. 119 * 120 * @see #EXTRA_STATE 121 * @see #EXTRA_INCOMING_NUMBER 122 * @see #getCallState 123 */ 124 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 125 public static final String ACTION_PHONE_STATE_CHANGED = 126 "android.intent.action.PHONE_STATE"; 127 128 /** 129 * The Phone app sends this intent when a user opts to respond-via-message during an incoming 130 * call. By default, the device's default SMS app consumes this message and sends a text message 131 * to the caller. A third party app can also provide this functionality by consuming this Intent 132 * with a {@link android.app.Service} and sending the message using its own messaging system. 133 * <p>The intent contains a URI (available from {@link android.content.Intent#getData}) 134 * describing the recipient, using either the {@code sms:}, {@code smsto:}, {@code mms:}, 135 * or {@code mmsto:} URI schema. Each of these URI schema carry the recipient information the 136 * same way: the path part of the URI contains the recipient's phone number or a comma-separated 137 * set of phone numbers if there are multiple recipients. For example, {@code 138 * smsto:2065551234}.</p> 139 * 140 * <p>The intent may also contain extras for the message text (in {@link 141 * android.content.Intent#EXTRA_TEXT}) and a message subject 142 * (in {@link android.content.Intent#EXTRA_SUBJECT}).</p> 143 * 144 * <p class="note"><strong>Note:</strong> 145 * The intent-filter that consumes this Intent needs to be in a {@link android.app.Service} 146 * that requires the 147 * permission {@link android.Manifest.permission#SEND_RESPOND_VIA_MESSAGE}.</p> 148 * <p>For example, the service that receives this intent can be declared in the manifest file 149 * with an intent filter like this:</p> 150 * <pre> 151 * <!-- Service that delivers SMS messages received from the phone "quick response" --> 152 * <service android:name=".HeadlessSmsSendService" 153 * android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE" 154 * android:exported="true" > 155 * <intent-filter> 156 * <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" /> 157 * <category android:name="android.intent.category.DEFAULT" /> 158 * <data android:scheme="sms" /> 159 * <data android:scheme="smsto" /> 160 * <data android:scheme="mms" /> 161 * <data android:scheme="mmsto" /> 162 * </intent-filter> 163 * </service></pre> 164 * <p> 165 * Output: nothing. 166 */ 167 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) 168 public static final String ACTION_RESPOND_VIA_MESSAGE = 169 "android.intent.action.RESPOND_VIA_MESSAGE"; 170 171 /** 172 * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast 173 * for a String containing the new call state. 174 * 175 * @see #EXTRA_STATE_IDLE 176 * @see #EXTRA_STATE_RINGING 177 * @see #EXTRA_STATE_OFFHOOK 178 * 179 * <p class="note"> 180 * Retrieve with 181 * {@link android.content.Intent#getStringExtra(String)}. 182 */ 183 public static final String EXTRA_STATE = PhoneConstants.STATE_KEY; 184 185 /** 186 * Value used with {@link #EXTRA_STATE} corresponding to 187 * {@link #CALL_STATE_IDLE}. 188 */ 189 public static final String EXTRA_STATE_IDLE = PhoneConstants.State.IDLE.toString(); 190 191 /** 192 * Value used with {@link #EXTRA_STATE} corresponding to 193 * {@link #CALL_STATE_RINGING}. 194 */ 195 public static final String EXTRA_STATE_RINGING = PhoneConstants.State.RINGING.toString(); 196 197 /** 198 * Value used with {@link #EXTRA_STATE} corresponding to 199 * {@link #CALL_STATE_OFFHOOK}. 200 */ 201 public static final String EXTRA_STATE_OFFHOOK = PhoneConstants.State.OFFHOOK.toString(); 202 203 /** 204 * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast 205 * for a String containing the incoming phone number. 206 * Only valid when the new call state is RINGING. 207 * 208 * <p class="note"> 209 * Retrieve with 210 * {@link android.content.Intent#getStringExtra(String)}. 211 */ 212 public static final String EXTRA_INCOMING_NUMBER = "incoming_number"; 213 214 /** 215 * Broadcast intent action indicating that a precise call state 216 * (cellular) on the device has changed. 217 * 218 * <p> 219 * The {@link #EXTRA_RINGING_CALL_STATE} extra indicates the ringing call state. 220 * The {@link #EXTRA_FOREGROUND_CALL_STATE} extra indicates the foreground call state. 221 * The {@link #EXTRA_BACKGROUND_CALL_STATE} extra indicates the background call state. 222 * The {@link #EXTRA_DISCONNECT_CAUSE} extra indicates the disconnect cause. 223 * The {@link #EXTRA_PRECISE_DISCONNECT_CAUSE} extra indicates the precise disconnect cause. 224 * 225 * <p class="note"> 226 * Requires the READ_PRECISE_PHONE_STATE permission. 227 * 228 * @see #EXTRA_RINGING_CALL_STATE 229 * @see #EXTRA_FOREGROUND_CALL_STATE 230 * @see #EXTRA_BACKGROUND_CALL_STATE 231 * @see #EXTRA_DISCONNECT_CAUSE 232 * @see #EXTRA_PRECISE_DISCONNECT_CAUSE 233 * 234 * <p class="note"> 235 * Requires the READ_PRECISE_PHONE_STATE permission. 236 * 237 * @hide 238 */ 239 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 240 public static final String ACTION_PRECISE_CALL_STATE_CHANGED = 241 "android.intent.action.PRECISE_CALL_STATE"; 242 243 /** 244 * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast 245 * for an integer containing the state of the current ringing call. 246 * 247 * @see PreciseCallState#PRECISE_CALL_STATE_NOT_VALID 248 * @see PreciseCallState#PRECISE_CALL_STATE_IDLE 249 * @see PreciseCallState#PRECISE_CALL_STATE_ACTIVE 250 * @see PreciseCallState#PRECISE_CALL_STATE_HOLDING 251 * @see PreciseCallState#PRECISE_CALL_STATE_DIALING 252 * @see PreciseCallState#PRECISE_CALL_STATE_ALERTING 253 * @see PreciseCallState#PRECISE_CALL_STATE_INCOMING 254 * @see PreciseCallState#PRECISE_CALL_STATE_WAITING 255 * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTED 256 * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTING 257 * 258 * <p class="note"> 259 * Retrieve with 260 * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}. 261 * 262 * @hide 263 */ 264 public static final String EXTRA_RINGING_CALL_STATE = "ringing_state"; 265 266 /** 267 * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast 268 * for an integer containing the state of the current foreground call. 269 * 270 * @see PreciseCallState#PRECISE_CALL_STATE_NOT_VALID 271 * @see PreciseCallState#PRECISE_CALL_STATE_IDLE 272 * @see PreciseCallState#PRECISE_CALL_STATE_ACTIVE 273 * @see PreciseCallState#PRECISE_CALL_STATE_HOLDING 274 * @see PreciseCallState#PRECISE_CALL_STATE_DIALING 275 * @see PreciseCallState#PRECISE_CALL_STATE_ALERTING 276 * @see PreciseCallState#PRECISE_CALL_STATE_INCOMING 277 * @see PreciseCallState#PRECISE_CALL_STATE_WAITING 278 * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTED 279 * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTING 280 * 281 * <p class="note"> 282 * Retrieve with 283 * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}. 284 * 285 * @hide 286 */ 287 public static final String EXTRA_FOREGROUND_CALL_STATE = "foreground_state"; 288 289 /** 290 * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast 291 * for an integer containing the state of the current background call. 292 * 293 * @see PreciseCallState#PRECISE_CALL_STATE_NOT_VALID 294 * @see PreciseCallState#PRECISE_CALL_STATE_IDLE 295 * @see PreciseCallState#PRECISE_CALL_STATE_ACTIVE 296 * @see PreciseCallState#PRECISE_CALL_STATE_HOLDING 297 * @see PreciseCallState#PRECISE_CALL_STATE_DIALING 298 * @see PreciseCallState#PRECISE_CALL_STATE_ALERTING 299 * @see PreciseCallState#PRECISE_CALL_STATE_INCOMING 300 * @see PreciseCallState#PRECISE_CALL_STATE_WAITING 301 * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTED 302 * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTING 303 * 304 * <p class="note"> 305 * Retrieve with 306 * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}. 307 * 308 * @hide 309 */ 310 public static final String EXTRA_BACKGROUND_CALL_STATE = "background_state"; 311 312 /** 313 * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast 314 * for an integer containing the disconnect cause. 315 * 316 * @see DisconnectCause 317 * 318 * <p class="note"> 319 * Retrieve with 320 * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}. 321 * 322 * @hide 323 */ 324 public static final String EXTRA_DISCONNECT_CAUSE = "disconnect_cause"; 325 326 /** 327 * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast 328 * for an integer containing the disconnect cause provided by the RIL. 329 * 330 * @see PreciseDisconnectCause 331 * 332 * <p class="note"> 333 * Retrieve with 334 * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}. 335 * 336 * @hide 337 */ 338 public static final String EXTRA_PRECISE_DISCONNECT_CAUSE = "precise_disconnect_cause"; 339 340 /** 341 * Broadcast intent action indicating a data connection has changed, 342 * providing precise information about the connection. 343 * 344 * <p> 345 * The {@link #EXTRA_DATA_STATE} extra indicates the connection state. 346 * The {@link #EXTRA_DATA_NETWORK_TYPE} extra indicates the connection network type. 347 * The {@link #EXTRA_DATA_APN_TYPE} extra indicates the APN type. 348 * The {@link #EXTRA_DATA_APN} extra indicates the APN. 349 * The {@link #EXTRA_DATA_CHANGE_REASON} extra indicates the connection change reason. 350 * The {@link #EXTRA_DATA_IFACE_PROPERTIES} extra indicates the connection interface. 351 * The {@link #EXTRA_DATA_FAILURE_CAUSE} extra indicates the connection fail cause. 352 * 353 * <p class="note"> 354 * Requires the READ_PRECISE_PHONE_STATE permission. 355 * 356 * @see #EXTRA_DATA_STATE 357 * @see #EXTRA_DATA_NETWORK_TYPE 358 * @see #EXTRA_DATA_APN_TYPE 359 * @see #EXTRA_DATA_APN 360 * @see #EXTRA_DATA_CHANGE_REASON 361 * @see #EXTRA_DATA_IFACE 362 * @see #EXTRA_DATA_FAILURE_CAUSE 363 * @hide 364 */ 365 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 366 public static final String ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED = 367 "android.intent.action.PRECISE_DATA_CONNECTION_STATE_CHANGED"; 368 369 /** 370 * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast 371 * for an integer containing the state of the current data connection. 372 * 373 * @see TelephonyManager#DATA_UNKNOWN 374 * @see TelephonyManager#DATA_DISCONNECTED 375 * @see TelephonyManager#DATA_CONNECTING 376 * @see TelephonyManager#DATA_CONNECTED 377 * @see TelephonyManager#DATA_SUSPENDED 378 * 379 * <p class="note"> 380 * Retrieve with 381 * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}. 382 * 383 * @hide 384 */ 385 public static final String EXTRA_DATA_STATE = PhoneConstants.STATE_KEY; 386 387 /** 388 * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast 389 * for an integer containing the network type. 390 * 391 * @see TelephonyManager#NETWORK_TYPE_UNKNOWN 392 * @see TelephonyManager#NETWORK_TYPE_GPRS 393 * @see TelephonyManager#NETWORK_TYPE_EDGE 394 * @see TelephonyManager#NETWORK_TYPE_UMTS 395 * @see TelephonyManager#NETWORK_TYPE_CDMA 396 * @see TelephonyManager#NETWORK_TYPE_EVDO_0 397 * @see TelephonyManager#NETWORK_TYPE_EVDO_A 398 * @see TelephonyManager#NETWORK_TYPE_1xRTT 399 * @see TelephonyManager#NETWORK_TYPE_HSDPA 400 * @see TelephonyManager#NETWORK_TYPE_HSUPA 401 * @see TelephonyManager#NETWORK_TYPE_HSPA 402 * @see TelephonyManager#NETWORK_TYPE_IDEN 403 * @see TelephonyManager#NETWORK_TYPE_EVDO_B 404 * @see TelephonyManager#NETWORK_TYPE_LTE 405 * @see TelephonyManager#NETWORK_TYPE_EHRPD 406 * @see TelephonyManager#NETWORK_TYPE_HSPAP 407 * 408 * <p class="note"> 409 * Retrieve with 410 * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}. 411 * 412 * @hide 413 */ 414 public static final String EXTRA_DATA_NETWORK_TYPE = PhoneConstants.DATA_NETWORK_TYPE_KEY; 415 416 /** 417 * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast 418 * for an String containing the data APN type. 419 * 420 * <p class="note"> 421 * Retrieve with 422 * {@link android.content.Intent#getStringExtra(String name)}. 423 * 424 * @hide 425 */ 426 public static final String EXTRA_DATA_APN_TYPE = PhoneConstants.DATA_APN_TYPE_KEY; 427 428 /** 429 * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast 430 * for an String containing the data APN. 431 * 432 * <p class="note"> 433 * Retrieve with 434 * {@link android.content.Intent#getStringExtra(String name)}. 435 * 436 * @hide 437 */ 438 public static final String EXTRA_DATA_APN = PhoneConstants.DATA_APN_KEY; 439 440 /** 441 * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast 442 * for an String representation of the change reason. 443 * 444 * <p class="note"> 445 * Retrieve with 446 * {@link android.content.Intent#getStringExtra(String name)}. 447 * 448 * @hide 449 */ 450 public static final String EXTRA_DATA_CHANGE_REASON = PhoneConstants.STATE_CHANGE_REASON_KEY; 451 452 /** 453 * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast 454 * for an String representation of the data interface. 455 * 456 * <p class="note"> 457 * Retrieve with 458 * {@link android.content.Intent#getParcelableExtra(String name)}. 459 * 460 * @hide 461 */ 462 public static final String EXTRA_DATA_LINK_PROPERTIES_KEY = PhoneConstants.DATA_LINK_PROPERTIES_KEY; 463 464 /** 465 * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast 466 * for the data connection fail cause. 467 * 468 * <p class="note"> 469 * Retrieve with 470 * {@link android.content.Intent#getStringExtra(String name)}. 471 * 472 * @hide 473 */ 474 public static final String EXTRA_DATA_FAILURE_CAUSE = PhoneConstants.DATA_FAILURE_CAUSE_KEY; 475 476 // 477 // 478 // Device Info 479 // 480 // 481 482 /** 483 * Returns the software version number for the device, for example, 484 * the IMEI/SV for GSM phones. Return null if the software version is 485 * not available. 486 * 487 * <p>Requires Permission: 488 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 489 */ 490 public String getDeviceSoftwareVersion() { 491 try { 492 return getSubscriberInfo().getDeviceSvn(); 493 } catch (RemoteException ex) { 494 return null; 495 } catch (NullPointerException ex) { 496 return null; 497 } 498 } 499 500 /** 501 * Returns the unique device ID, for example, the IMEI for GSM and the MEID 502 * or ESN for CDMA phones. Return null if device ID is not available. 503 * 504 * <p>Requires Permission: 505 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 506 */ 507 public String getDeviceId() { 508 try { 509 return getSubscriberInfo().getDeviceId(); 510 } catch (RemoteException ex) { 511 return null; 512 } catch (NullPointerException ex) { 513 return null; 514 } 515 } 516 517 /** 518 * Returns the current location of the device. 519 *<p> 520 * If there is only one radio in the device and that radio has an LTE connection, 521 * this method will return null. The implementation must not to try add LTE 522 * identifiers into the existing cdma/gsm classes. 523 *<p> 524 * In the future this call will be deprecated. 525 *<p> 526 * @return Current location of the device or null if not available. 527 * 528 * <p>Requires Permission: 529 * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} or 530 * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION}. 531 */ 532 public CellLocation getCellLocation() { 533 try { 534 Bundle bundle = getITelephony().getCellLocation(); 535 if (bundle.isEmpty()) return null; 536 CellLocation cl = CellLocation.newFromBundle(bundle); 537 if (cl.isEmpty()) 538 return null; 539 return cl; 540 } catch (RemoteException ex) { 541 return null; 542 } catch (NullPointerException ex) { 543 return null; 544 } 545 } 546 547 /** 548 * Enables location update notifications. {@link PhoneStateListener#onCellLocationChanged 549 * PhoneStateListener.onCellLocationChanged} will be called on location updates. 550 * 551 * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES 552 * CONTROL_LOCATION_UPDATES} 553 * 554 * @hide 555 */ 556 public void enableLocationUpdates() { 557 try { 558 getITelephony().enableLocationUpdates(); 559 } catch (RemoteException ex) { 560 } catch (NullPointerException ex) { 561 } 562 } 563 564 /** 565 * Disables location update notifications. {@link PhoneStateListener#onCellLocationChanged 566 * PhoneStateListener.onCellLocationChanged} will be called on location updates. 567 * 568 * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES 569 * CONTROL_LOCATION_UPDATES} 570 * 571 * @hide 572 */ 573 public void disableLocationUpdates() { 574 try { 575 getITelephony().disableLocationUpdates(); 576 } catch (RemoteException ex) { 577 } catch (NullPointerException ex) { 578 } 579 } 580 581 /** 582 * Returns the neighboring cell information of the device. The getAllCellInfo is preferred 583 * and use this only if getAllCellInfo return nulls or an empty list. 584 *<p> 585 * In the future this call will be deprecated. 586 *<p> 587 * @return List of NeighboringCellInfo or null if info unavailable. 588 * 589 * <p>Requires Permission: 590 * (@link android.Manifest.permission#ACCESS_COARSE_UPDATES} 591 */ 592 public List<NeighboringCellInfo> getNeighboringCellInfo() { 593 try { 594 return getITelephony().getNeighboringCellInfo(mContext.getOpPackageName()); 595 } catch (RemoteException ex) { 596 return null; 597 } catch (NullPointerException ex) { 598 return null; 599 } 600 } 601 602 /** No phone radio. */ 603 public static final int PHONE_TYPE_NONE = PhoneConstants.PHONE_TYPE_NONE; 604 /** Phone radio is GSM. */ 605 public static final int PHONE_TYPE_GSM = PhoneConstants.PHONE_TYPE_GSM; 606 /** Phone radio is CDMA. */ 607 public static final int PHONE_TYPE_CDMA = PhoneConstants.PHONE_TYPE_CDMA; 608 /** Phone is via SIP. */ 609 public static final int PHONE_TYPE_SIP = PhoneConstants.PHONE_TYPE_SIP; 610 611 /** 612 * Returns the current phone type. 613 * TODO: This is a last minute change and hence hidden. 614 * 615 * @see #PHONE_TYPE_NONE 616 * @see #PHONE_TYPE_GSM 617 * @see #PHONE_TYPE_CDMA 618 * @see #PHONE_TYPE_SIP 619 * 620 * {@hide} 621 */ 622 public int getCurrentPhoneType() { 623 try{ 624 ITelephony telephony = getITelephony(); 625 if (telephony != null) { 626 return telephony.getActivePhoneType(); 627 } else { 628 // This can happen when the ITelephony interface is not up yet. 629 return getPhoneTypeFromProperty(); 630 } 631 } catch (RemoteException ex) { 632 // This shouldn't happen in the normal case, as a backup we 633 // read from the system property. 634 return getPhoneTypeFromProperty(); 635 } catch (NullPointerException ex) { 636 // This shouldn't happen in the normal case, as a backup we 637 // read from the system property. 638 return getPhoneTypeFromProperty(); 639 } 640 } 641 642 /** 643 * Returns a constant indicating the device phone type. This 644 * indicates the type of radio used to transmit voice calls. 645 * 646 * @see #PHONE_TYPE_NONE 647 * @see #PHONE_TYPE_GSM 648 * @see #PHONE_TYPE_CDMA 649 * @see #PHONE_TYPE_SIP 650 */ 651 public int getPhoneType() { 652 if (!isVoiceCapable()) { 653 return PHONE_TYPE_NONE; 654 } 655 return getCurrentPhoneType(); 656 } 657 658 private int getPhoneTypeFromProperty() { 659 int type = 660 SystemProperties.getInt(TelephonyProperties.CURRENT_ACTIVE_PHONE, 661 getPhoneTypeFromNetworkType()); 662 return type; 663 } 664 665 private int getPhoneTypeFromNetworkType() { 666 // When the system property CURRENT_ACTIVE_PHONE, has not been set, 667 // use the system property for default network type. 668 // This is a fail safe, and can only happen at first boot. 669 int mode = SystemProperties.getInt("ro.telephony.default_network", -1); 670 if (mode == -1) 671 return PHONE_TYPE_NONE; 672 return getPhoneType(mode); 673 } 674 675 /** 676 * This function returns the type of the phone, depending 677 * on the network mode. 678 * 679 * @param networkMode 680 * @return Phone Type 681 * 682 * @hide 683 */ 684 public static int getPhoneType(int networkMode) { 685 switch(networkMode) { 686 case RILConstants.NETWORK_MODE_CDMA: 687 case RILConstants.NETWORK_MODE_CDMA_NO_EVDO: 688 case RILConstants.NETWORK_MODE_EVDO_NO_CDMA: 689 return PhoneConstants.PHONE_TYPE_CDMA; 690 691 case RILConstants.NETWORK_MODE_WCDMA_PREF: 692 case RILConstants.NETWORK_MODE_GSM_ONLY: 693 case RILConstants.NETWORK_MODE_WCDMA_ONLY: 694 case RILConstants.NETWORK_MODE_GSM_UMTS: 695 case RILConstants.NETWORK_MODE_LTE_GSM_WCDMA: 696 case RILConstants.NETWORK_MODE_LTE_WCDMA: 697 case RILConstants.NETWORK_MODE_LTE_CMDA_EVDO_GSM_WCDMA: 698 return PhoneConstants.PHONE_TYPE_GSM; 699 700 // Use CDMA Phone for the global mode including CDMA 701 case RILConstants.NETWORK_MODE_GLOBAL: 702 case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO: 703 return PhoneConstants.PHONE_TYPE_CDMA; 704 705 case RILConstants.NETWORK_MODE_LTE_ONLY: 706 if (getLteOnCdmaModeStatic() == PhoneConstants.LTE_ON_CDMA_TRUE) { 707 return PhoneConstants.PHONE_TYPE_CDMA; 708 } else { 709 return PhoneConstants.PHONE_TYPE_GSM; 710 } 711 default: 712 return PhoneConstants.PHONE_TYPE_GSM; 713 } 714 } 715 716 /** 717 * The contents of the /proc/cmdline file 718 */ 719 private static String getProcCmdLine() 720 { 721 String cmdline = ""; 722 FileInputStream is = null; 723 try { 724 is = new FileInputStream("/proc/cmdline"); 725 byte [] buffer = new byte[2048]; 726 int count = is.read(buffer); 727 if (count > 0) { 728 cmdline = new String(buffer, 0, count); 729 } 730 } catch (IOException e) { 731 Rlog.d(TAG, "No /proc/cmdline exception=" + e); 732 } finally { 733 if (is != null) { 734 try { 735 is.close(); 736 } catch (IOException e) { 737 } 738 } 739 } 740 Rlog.d(TAG, "/proc/cmdline=" + cmdline); 741 return cmdline; 742 } 743 744 /** Kernel command line */ 745 private static final String sKernelCmdLine = getProcCmdLine(); 746 747 /** Pattern for selecting the product type from the kernel command line */ 748 private static final Pattern sProductTypePattern = 749 Pattern.compile("\\sproduct_type\\s*=\\s*(\\w+)"); 750 751 /** The ProductType used for LTE on CDMA devices */ 752 private static final String sLteOnCdmaProductType = 753 SystemProperties.get(TelephonyProperties.PROPERTY_LTE_ON_CDMA_PRODUCT_TYPE, ""); 754 755 /** 756 * Return if the current radio is LTE on CDMA. This 757 * is a tri-state return value as for a period of time 758 * the mode may be unknown. 759 * 760 * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE} 761 * or {@link PhoneConstants#LTE_ON_CDMA_TRUE} 762 * 763 * @hide 764 */ 765 public static int getLteOnCdmaModeStatic() { 766 int retVal; 767 int curVal; 768 String productType = ""; 769 770 curVal = SystemProperties.getInt(TelephonyProperties.PROPERTY_LTE_ON_CDMA_DEVICE, 771 PhoneConstants.LTE_ON_CDMA_UNKNOWN); 772 retVal = curVal; 773 if (retVal == PhoneConstants.LTE_ON_CDMA_UNKNOWN) { 774 Matcher matcher = sProductTypePattern.matcher(sKernelCmdLine); 775 if (matcher.find()) { 776 productType = matcher.group(1); 777 if (sLteOnCdmaProductType.equals(productType)) { 778 retVal = PhoneConstants.LTE_ON_CDMA_TRUE; 779 } else { 780 retVal = PhoneConstants.LTE_ON_CDMA_FALSE; 781 } 782 } else { 783 retVal = PhoneConstants.LTE_ON_CDMA_FALSE; 784 } 785 } 786 787 Rlog.d(TAG, "getLteOnCdmaMode=" + retVal + " curVal=" + curVal + 788 " product_type='" + productType + 789 "' lteOnCdmaProductType='" + sLteOnCdmaProductType + "'"); 790 return retVal; 791 } 792 793 // 794 // 795 // Current Network 796 // 797 // 798 799 /** 800 * Returns the alphabetic name of current registered operator. 801 * <p> 802 * Availability: Only when user is registered to a network. Result may be 803 * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if 804 * on a CDMA network). 805 */ 806 public String getNetworkOperatorName() { 807 return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ALPHA); 808 } 809 810 /** 811 * Returns the numeric name (MCC+MNC) of current registered operator. 812 * <p> 813 * Availability: Only when user is registered to a network. Result may be 814 * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if 815 * on a CDMA network). 816 */ 817 public String getNetworkOperator() { 818 return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_NUMERIC); 819 } 820 821 /** 822 * Returns true if the device is considered roaming on the current 823 * network, for GSM purposes. 824 * <p> 825 * Availability: Only when user registered to a network. 826 */ 827 public boolean isNetworkRoaming() { 828 return "true".equals(SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING)); 829 } 830 831 /** 832 * Returns the ISO country code equivalent of the current registered 833 * operator's MCC (Mobile Country Code). 834 * <p> 835 * Availability: Only when user is registered to a network. Result may be 836 * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if 837 * on a CDMA network). 838 */ 839 public String getNetworkCountryIso() { 840 return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY); 841 } 842 843 /** Network type is unknown */ 844 public static final int NETWORK_TYPE_UNKNOWN = 0; 845 /** Current network is GPRS */ 846 public static final int NETWORK_TYPE_GPRS = 1; 847 /** Current network is EDGE */ 848 public static final int NETWORK_TYPE_EDGE = 2; 849 /** Current network is UMTS */ 850 public static final int NETWORK_TYPE_UMTS = 3; 851 /** Current network is CDMA: Either IS95A or IS95B*/ 852 public static final int NETWORK_TYPE_CDMA = 4; 853 /** Current network is EVDO revision 0*/ 854 public static final int NETWORK_TYPE_EVDO_0 = 5; 855 /** Current network is EVDO revision A*/ 856 public static final int NETWORK_TYPE_EVDO_A = 6; 857 /** Current network is 1xRTT*/ 858 public static final int NETWORK_TYPE_1xRTT = 7; 859 /** Current network is HSDPA */ 860 public static final int NETWORK_TYPE_HSDPA = 8; 861 /** Current network is HSUPA */ 862 public static final int NETWORK_TYPE_HSUPA = 9; 863 /** Current network is HSPA */ 864 public static final int NETWORK_TYPE_HSPA = 10; 865 /** Current network is iDen */ 866 public static final int NETWORK_TYPE_IDEN = 11; 867 /** Current network is EVDO revision B*/ 868 public static final int NETWORK_TYPE_EVDO_B = 12; 869 /** Current network is LTE */ 870 public static final int NETWORK_TYPE_LTE = 13; 871 /** Current network is eHRPD */ 872 public static final int NETWORK_TYPE_EHRPD = 14; 873 /** Current network is HSPA+ */ 874 public static final int NETWORK_TYPE_HSPAP = 15; 875 /** Current network is GSM {@hide} */ 876 public static final int NETWORK_TYPE_GSM = 16; 877 878 /** 879 * @return the NETWORK_TYPE_xxxx for current data connection. 880 */ 881 public int getNetworkType() { 882 return getDataNetworkType(); 883 } 884 885 /** 886 * Returns a constant indicating the radio technology (network type) 887 * currently in use on the device for data transmission. 888 * @return the network type 889 * 890 * @see #NETWORK_TYPE_UNKNOWN 891 * @see #NETWORK_TYPE_GPRS 892 * @see #NETWORK_TYPE_EDGE 893 * @see #NETWORK_TYPE_UMTS 894 * @see #NETWORK_TYPE_HSDPA 895 * @see #NETWORK_TYPE_HSUPA 896 * @see #NETWORK_TYPE_HSPA 897 * @see #NETWORK_TYPE_CDMA 898 * @see #NETWORK_TYPE_EVDO_0 899 * @see #NETWORK_TYPE_EVDO_A 900 * @see #NETWORK_TYPE_EVDO_B 901 * @see #NETWORK_TYPE_1xRTT 902 * @see #NETWORK_TYPE_IDEN 903 * @see #NETWORK_TYPE_LTE 904 * @see #NETWORK_TYPE_EHRPD 905 * @see #NETWORK_TYPE_HSPAP 906 * 907 * @hide 908 */ 909 public int getDataNetworkType() { 910 try{ 911 ITelephony telephony = getITelephony(); 912 if (telephony != null) { 913 return telephony.getDataNetworkType(); 914 } else { 915 // This can happen when the ITelephony interface is not up yet. 916 return NETWORK_TYPE_UNKNOWN; 917 } 918 } catch(RemoteException ex) { 919 // This shouldn't happen in the normal case 920 return NETWORK_TYPE_UNKNOWN; 921 } catch (NullPointerException ex) { 922 // This could happen before phone restarts due to crashing 923 return NETWORK_TYPE_UNKNOWN; 924 } 925 } 926 927 /** 928 * Returns the NETWORK_TYPE_xxxx for voice 929 * 930 * @hide 931 */ 932 public int getVoiceNetworkType() { 933 try{ 934 ITelephony telephony = getITelephony(); 935 if (telephony != null) { 936 return telephony.getVoiceNetworkType(); 937 } else { 938 // This can happen when the ITelephony interface is not up yet. 939 return NETWORK_TYPE_UNKNOWN; 940 } 941 } catch(RemoteException ex) { 942 // This shouldn't happen in the normal case 943 return NETWORK_TYPE_UNKNOWN; 944 } catch (NullPointerException ex) { 945 // This could happen before phone restarts due to crashing 946 return NETWORK_TYPE_UNKNOWN; 947 } 948 } 949 950 /** Unknown network class. {@hide} */ 951 public static final int NETWORK_CLASS_UNKNOWN = 0; 952 /** Class of broadly defined "2G" networks. {@hide} */ 953 public static final int NETWORK_CLASS_2_G = 1; 954 /** Class of broadly defined "3G" networks. {@hide} */ 955 public static final int NETWORK_CLASS_3_G = 2; 956 /** Class of broadly defined "4G" networks. {@hide} */ 957 public static final int NETWORK_CLASS_4_G = 3; 958 959 /** 960 * Return general class of network type, such as "3G" or "4G". In cases 961 * where classification is contentious, this method is conservative. 962 * 963 * @hide 964 */ 965 public static int getNetworkClass(int networkType) { 966 switch (networkType) { 967 case NETWORK_TYPE_GPRS: 968 case NETWORK_TYPE_GSM: 969 case NETWORK_TYPE_EDGE: 970 case NETWORK_TYPE_CDMA: 971 case NETWORK_TYPE_1xRTT: 972 case NETWORK_TYPE_IDEN: 973 return NETWORK_CLASS_2_G; 974 case NETWORK_TYPE_UMTS: 975 case NETWORK_TYPE_EVDO_0: 976 case NETWORK_TYPE_EVDO_A: 977 case NETWORK_TYPE_HSDPA: 978 case NETWORK_TYPE_HSUPA: 979 case NETWORK_TYPE_HSPA: 980 case NETWORK_TYPE_EVDO_B: 981 case NETWORK_TYPE_EHRPD: 982 case NETWORK_TYPE_HSPAP: 983 return NETWORK_CLASS_3_G; 984 case NETWORK_TYPE_LTE: 985 return NETWORK_CLASS_4_G; 986 default: 987 return NETWORK_CLASS_UNKNOWN; 988 } 989 } 990 991 /** 992 * Returns a string representation of the radio technology (network type) 993 * currently in use on the device. 994 * @return the name of the radio technology 995 * 996 * @hide pending API council review 997 */ 998 public String getNetworkTypeName() { 999 return getNetworkTypeName(getNetworkType()); 1000 } 1001 1002 /** {@hide} */ 1003 public static String getNetworkTypeName(int type) { 1004 switch (type) { 1005 case NETWORK_TYPE_GPRS: 1006 return "GPRS"; 1007 case NETWORK_TYPE_EDGE: 1008 return "EDGE"; 1009 case NETWORK_TYPE_UMTS: 1010 return "UMTS"; 1011 case NETWORK_TYPE_HSDPA: 1012 return "HSDPA"; 1013 case NETWORK_TYPE_HSUPA: 1014 return "HSUPA"; 1015 case NETWORK_TYPE_HSPA: 1016 return "HSPA"; 1017 case NETWORK_TYPE_CDMA: 1018 return "CDMA"; 1019 case NETWORK_TYPE_EVDO_0: 1020 return "CDMA - EvDo rev. 0"; 1021 case NETWORK_TYPE_EVDO_A: 1022 return "CDMA - EvDo rev. A"; 1023 case NETWORK_TYPE_EVDO_B: 1024 return "CDMA - EvDo rev. B"; 1025 case NETWORK_TYPE_1xRTT: 1026 return "CDMA - 1xRTT"; 1027 case NETWORK_TYPE_LTE: 1028 return "LTE"; 1029 case NETWORK_TYPE_EHRPD: 1030 return "CDMA - eHRPD"; 1031 case NETWORK_TYPE_IDEN: 1032 return "iDEN"; 1033 case NETWORK_TYPE_HSPAP: 1034 return "HSPA+"; 1035 case NETWORK_TYPE_GSM: 1036 return "GSM"; 1037 default: 1038 return "UNKNOWN"; 1039 } 1040 } 1041 1042 // 1043 // 1044 // SIM Card 1045 // 1046 // 1047 1048 /** SIM card state: Unknown. Signifies that the SIM is in transition 1049 * between states. For example, when the user inputs the SIM pin 1050 * under PIN_REQUIRED state, a query for sim status returns 1051 * this state before turning to SIM_STATE_READY. */ 1052 public static final int SIM_STATE_UNKNOWN = 0; 1053 /** SIM card state: no SIM card is available in the device */ 1054 public static final int SIM_STATE_ABSENT = 1; 1055 /** SIM card state: Locked: requires the user's SIM PIN to unlock */ 1056 public static final int SIM_STATE_PIN_REQUIRED = 2; 1057 /** SIM card state: Locked: requires the user's SIM PUK to unlock */ 1058 public static final int SIM_STATE_PUK_REQUIRED = 3; 1059 /** SIM card state: Locked: requries a network PIN to unlock */ 1060 public static final int SIM_STATE_NETWORK_LOCKED = 4; 1061 /** SIM card state: Ready */ 1062 public static final int SIM_STATE_READY = 5; 1063 /** SIM card state: SIM Card Error, Sim Card is present but faulty 1064 *@hide 1065 */ 1066 public static final int SIM_STATE_CARD_IO_ERROR = 6; 1067 1068 /** 1069 * @return true if a ICC card is present 1070 */ 1071 public boolean hasIccCard() { 1072 try { 1073 return getITelephony().hasIccCard(); 1074 } catch (RemoteException ex) { 1075 // Assume no ICC card if remote exception which shouldn't happen 1076 return false; 1077 } catch (NullPointerException ex) { 1078 // This could happen before phone restarts due to crashing 1079 return false; 1080 } 1081 } 1082 1083 /** 1084 * Returns a constant indicating the state of the 1085 * device SIM card. 1086 * 1087 * @see #SIM_STATE_UNKNOWN 1088 * @see #SIM_STATE_ABSENT 1089 * @see #SIM_STATE_PIN_REQUIRED 1090 * @see #SIM_STATE_PUK_REQUIRED 1091 * @see #SIM_STATE_NETWORK_LOCKED 1092 * @see #SIM_STATE_READY 1093 * @see #SIM_STATE_CARD_IO_ERROR 1094 */ 1095 public int getSimState() { 1096 String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE); 1097 if ("ABSENT".equals(prop)) { 1098 return SIM_STATE_ABSENT; 1099 } 1100 else if ("PIN_REQUIRED".equals(prop)) { 1101 return SIM_STATE_PIN_REQUIRED; 1102 } 1103 else if ("PUK_REQUIRED".equals(prop)) { 1104 return SIM_STATE_PUK_REQUIRED; 1105 } 1106 else if ("NETWORK_LOCKED".equals(prop)) { 1107 return SIM_STATE_NETWORK_LOCKED; 1108 } 1109 else if ("READY".equals(prop)) { 1110 return SIM_STATE_READY; 1111 } 1112 else if ("CARD_IO_ERROR".equals(prop)) { 1113 return SIM_STATE_CARD_IO_ERROR; 1114 } 1115 else { 1116 return SIM_STATE_UNKNOWN; 1117 } 1118 } 1119 1120 /** 1121 * Returns the MCC+MNC (mobile country code + mobile network code) of the 1122 * provider of the SIM. 5 or 6 decimal digits. 1123 * <p> 1124 * Availability: SIM state must be {@link #SIM_STATE_READY} 1125 * 1126 * @see #getSimState 1127 */ 1128 public String getSimOperator() { 1129 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC); 1130 } 1131 1132 /** 1133 * Returns the Service Provider Name (SPN). 1134 * <p> 1135 * Availability: SIM state must be {@link #SIM_STATE_READY} 1136 * 1137 * @see #getSimState 1138 */ 1139 public String getSimOperatorName() { 1140 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA); 1141 } 1142 1143 /** 1144 * Returns the ISO country code equivalent for the SIM provider's country code. 1145 */ 1146 public String getSimCountryIso() { 1147 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY); 1148 } 1149 1150 /** 1151 * Returns the serial number of the SIM, if applicable. Return null if it is 1152 * unavailable. 1153 * <p> 1154 * Requires Permission: 1155 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1156 */ 1157 public String getSimSerialNumber() { 1158 try { 1159 return getSubscriberInfo().getIccSerialNumber(); 1160 } catch (RemoteException ex) { 1161 return null; 1162 } catch (NullPointerException ex) { 1163 // This could happen before phone restarts due to crashing 1164 return null; 1165 } 1166 } 1167 1168 /** 1169 * Return if the current radio is LTE on CDMA. This 1170 * is a tri-state return value as for a period of time 1171 * the mode may be unknown. 1172 * 1173 * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE} 1174 * or {@link PhoneConstants#LTE_ON_CDMA_TRUE} 1175 * 1176 * @hide 1177 */ 1178 public int getLteOnCdmaMode() { 1179 try { 1180 return getITelephony().getLteOnCdmaMode(); 1181 } catch (RemoteException ex) { 1182 // Assume no ICC card if remote exception which shouldn't happen 1183 return PhoneConstants.LTE_ON_CDMA_UNKNOWN; 1184 } catch (NullPointerException ex) { 1185 // This could happen before phone restarts due to crashing 1186 return PhoneConstants.LTE_ON_CDMA_UNKNOWN; 1187 } 1188 } 1189 1190 // 1191 // 1192 // Subscriber Info 1193 // 1194 // 1195 1196 /** 1197 * Returns the unique subscriber ID, for example, the IMSI for a GSM phone. 1198 * Return null if it is unavailable. 1199 * <p> 1200 * Requires Permission: 1201 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1202 */ 1203 public String getSubscriberId() { 1204 try { 1205 return getSubscriberInfo().getSubscriberId(); 1206 } catch (RemoteException ex) { 1207 return null; 1208 } catch (NullPointerException ex) { 1209 // This could happen before phone restarts due to crashing 1210 return null; 1211 } 1212 } 1213 1214 /** 1215 * Returns the Group Identifier Level1 for a GSM phone. 1216 * Return null if it is unavailable. 1217 * <p> 1218 * Requires Permission: 1219 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1220 */ 1221 public String getGroupIdLevel1() { 1222 try { 1223 return getSubscriberInfo().getGroupIdLevel1(); 1224 } catch (RemoteException ex) { 1225 return null; 1226 } catch (NullPointerException ex) { 1227 // This could happen before phone restarts due to crashing 1228 return null; 1229 } 1230 } 1231 1232 /** 1233 * Returns the phone number string for line 1, for example, the MSISDN 1234 * for a GSM phone. Return null if it is unavailable. 1235 * <p> 1236 * Requires Permission: 1237 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1238 */ 1239 public String getLine1Number() { 1240 try { 1241 return getSubscriberInfo().getLine1Number(); 1242 } catch (RemoteException ex) { 1243 return null; 1244 } catch (NullPointerException ex) { 1245 // This could happen before phone restarts due to crashing 1246 return null; 1247 } 1248 } 1249 1250 /** 1251 * Returns the alphabetic identifier associated with the line 1 number. 1252 * Return null if it is unavailable. 1253 * <p> 1254 * Requires Permission: 1255 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1256 * @hide 1257 * nobody seems to call this. 1258 */ 1259 public String getLine1AlphaTag() { 1260 try { 1261 return getSubscriberInfo().getLine1AlphaTag(); 1262 } catch (RemoteException ex) { 1263 return null; 1264 } catch (NullPointerException ex) { 1265 // This could happen before phone restarts due to crashing 1266 return null; 1267 } 1268 } 1269 1270 /** 1271 * Returns the MSISDN string. 1272 * for a GSM phone. Return null if it is unavailable. 1273 * <p> 1274 * Requires Permission: 1275 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1276 * 1277 * @hide 1278 */ 1279 public String getMsisdn() { 1280 try { 1281 return getSubscriberInfo().getMsisdn(); 1282 } catch (RemoteException ex) { 1283 return null; 1284 } catch (NullPointerException ex) { 1285 // This could happen before phone restarts due to crashing 1286 return null; 1287 } 1288 } 1289 1290 /** 1291 * Returns the voice mail number. Return null if it is unavailable. 1292 * <p> 1293 * Requires Permission: 1294 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1295 */ 1296 public String getVoiceMailNumber() { 1297 try { 1298 return getSubscriberInfo().getVoiceMailNumber(); 1299 } catch (RemoteException ex) { 1300 return null; 1301 } catch (NullPointerException ex) { 1302 // This could happen before phone restarts due to crashing 1303 return null; 1304 } 1305 } 1306 1307 /** 1308 * Returns the complete voice mail number. Return null if it is unavailable. 1309 * <p> 1310 * Requires Permission: 1311 * {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED} 1312 * 1313 * @hide 1314 */ 1315 public String getCompleteVoiceMailNumber() { 1316 try { 1317 return getSubscriberInfo().getCompleteVoiceMailNumber(); 1318 } catch (RemoteException ex) { 1319 return null; 1320 } catch (NullPointerException ex) { 1321 // This could happen before phone restarts due to crashing 1322 return null; 1323 } 1324 } 1325 1326 /** 1327 * Returns the voice mail count. Return 0 if unavailable. 1328 * <p> 1329 * Requires Permission: 1330 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1331 * @hide 1332 */ 1333 public int getVoiceMessageCount() { 1334 try { 1335 return getITelephony().getVoiceMessageCount(); 1336 } catch (RemoteException ex) { 1337 return 0; 1338 } catch (NullPointerException ex) { 1339 // This could happen before phone restarts due to crashing 1340 return 0; 1341 } 1342 } 1343 1344 /** 1345 * Retrieves the alphabetic identifier associated with the voice 1346 * mail number. 1347 * <p> 1348 * Requires Permission: 1349 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1350 */ 1351 public String getVoiceMailAlphaTag() { 1352 try { 1353 return getSubscriberInfo().getVoiceMailAlphaTag(); 1354 } catch (RemoteException ex) { 1355 return null; 1356 } catch (NullPointerException ex) { 1357 // This could happen before phone restarts due to crashing 1358 return null; 1359 } 1360 } 1361 1362 /** 1363 * Returns the IMS private user identity (IMPI) that was loaded from the ISIM. 1364 * @return the IMPI, or null if not present or not loaded 1365 * @hide 1366 */ 1367 public String getIsimImpi() { 1368 try { 1369 return getSubscriberInfo().getIsimImpi(); 1370 } catch (RemoteException ex) { 1371 return null; 1372 } catch (NullPointerException ex) { 1373 // This could happen before phone restarts due to crashing 1374 return null; 1375 } 1376 } 1377 1378 /** 1379 * Returns the IMS home network domain name that was loaded from the ISIM. 1380 * @return the IMS domain name, or null if not present or not loaded 1381 * @hide 1382 */ 1383 public String getIsimDomain() { 1384 try { 1385 return getSubscriberInfo().getIsimDomain(); 1386 } catch (RemoteException ex) { 1387 return null; 1388 } catch (NullPointerException ex) { 1389 // This could happen before phone restarts due to crashing 1390 return null; 1391 } 1392 } 1393 1394 /** 1395 * Returns the IMS public user identities (IMPU) that were loaded from the ISIM. 1396 * @return an array of IMPU strings, with one IMPU per string, or null if 1397 * not present or not loaded 1398 * @hide 1399 */ 1400 public String[] getIsimImpu() { 1401 try { 1402 return getSubscriberInfo().getIsimImpu(); 1403 } catch (RemoteException ex) { 1404 return null; 1405 } catch (NullPointerException ex) { 1406 // This could happen before phone restarts due to crashing 1407 return null; 1408 } 1409 } 1410 1411 private IPhoneSubInfo getSubscriberInfo() { 1412 // get it each time because that process crashes a lot 1413 return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo")); 1414 } 1415 1416 1417 /** Device call state: No activity. */ 1418 public static final int CALL_STATE_IDLE = 0; 1419 /** Device call state: Ringing. A new call arrived and is 1420 * ringing or waiting. In the latter case, another call is 1421 * already active. */ 1422 public static final int CALL_STATE_RINGING = 1; 1423 /** Device call state: Off-hook. At least one call exists 1424 * that is dialing, active, or on hold, and no calls are ringing 1425 * or waiting. */ 1426 public static final int CALL_STATE_OFFHOOK = 2; 1427 1428 /** 1429 * Returns a constant indicating the call state (cellular) on the device. 1430 */ 1431 public int getCallState() { 1432 try { 1433 return getITelephony().getCallState(); 1434 } catch (RemoteException ex) { 1435 // the phone process is restarting. 1436 return CALL_STATE_IDLE; 1437 } catch (NullPointerException ex) { 1438 // the phone process is restarting. 1439 return CALL_STATE_IDLE; 1440 } 1441 } 1442 1443 /** Data connection activity: No traffic. */ 1444 public static final int DATA_ACTIVITY_NONE = 0x00000000; 1445 /** Data connection activity: Currently receiving IP PPP traffic. */ 1446 public static final int DATA_ACTIVITY_IN = 0x00000001; 1447 /** Data connection activity: Currently sending IP PPP traffic. */ 1448 public static final int DATA_ACTIVITY_OUT = 0x00000002; 1449 /** Data connection activity: Currently both sending and receiving 1450 * IP PPP traffic. */ 1451 public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT; 1452 /** 1453 * Data connection is active, but physical link is down 1454 */ 1455 public static final int DATA_ACTIVITY_DORMANT = 0x00000004; 1456 1457 /** 1458 * Returns a constant indicating the type of activity on a data connection 1459 * (cellular). 1460 * 1461 * @see #DATA_ACTIVITY_NONE 1462 * @see #DATA_ACTIVITY_IN 1463 * @see #DATA_ACTIVITY_OUT 1464 * @see #DATA_ACTIVITY_INOUT 1465 * @see #DATA_ACTIVITY_DORMANT 1466 */ 1467 public int getDataActivity() { 1468 try { 1469 return getITelephony().getDataActivity(); 1470 } catch (RemoteException ex) { 1471 // the phone process is restarting. 1472 return DATA_ACTIVITY_NONE; 1473 } catch (NullPointerException ex) { 1474 // the phone process is restarting. 1475 return DATA_ACTIVITY_NONE; 1476 } 1477 } 1478 1479 /** Data connection state: Unknown. Used before we know the state. 1480 * @hide 1481 */ 1482 public static final int DATA_UNKNOWN = -1; 1483 /** Data connection state: Disconnected. IP traffic not available. */ 1484 public static final int DATA_DISCONNECTED = 0; 1485 /** Data connection state: Currently setting up a data connection. */ 1486 public static final int DATA_CONNECTING = 1; 1487 /** Data connection state: Connected. IP traffic should be available. */ 1488 public static final int DATA_CONNECTED = 2; 1489 /** Data connection state: Suspended. The connection is up, but IP 1490 * traffic is temporarily unavailable. For example, in a 2G network, 1491 * data activity may be suspended when a voice call arrives. */ 1492 public static final int DATA_SUSPENDED = 3; 1493 1494 /** 1495 * Returns a constant indicating the current data connection state 1496 * (cellular). 1497 * 1498 * @see #DATA_DISCONNECTED 1499 * @see #DATA_CONNECTING 1500 * @see #DATA_CONNECTED 1501 * @see #DATA_SUSPENDED 1502 */ 1503 public int getDataState() { 1504 try { 1505 return getITelephony().getDataState(); 1506 } catch (RemoteException ex) { 1507 // the phone process is restarting. 1508 return DATA_DISCONNECTED; 1509 } catch (NullPointerException ex) { 1510 return DATA_DISCONNECTED; 1511 } 1512 } 1513 1514 private ITelephony getITelephony() { 1515 return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE)); 1516 } 1517 1518 // 1519 // 1520 // PhoneStateListener 1521 // 1522 // 1523 1524 /** 1525 * Registers a listener object to receive notification of changes 1526 * in specified telephony states. 1527 * <p> 1528 * To register a listener, pass a {@link PhoneStateListener} 1529 * and specify at least one telephony state of interest in 1530 * the events argument. 1531 * 1532 * At registration, and when a specified telephony state 1533 * changes, the telephony manager invokes the appropriate 1534 * callback method on the listener object and passes the 1535 * current (udpated) values. 1536 * <p> 1537 * To unregister a listener, pass the listener object and set the 1538 * events argument to 1539 * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0). 1540 * 1541 * @param listener The {@link PhoneStateListener} object to register 1542 * (or unregister) 1543 * @param events The telephony state(s) of interest to the listener, 1544 * as a bitwise-OR combination of {@link PhoneStateListener} 1545 * LISTEN_ flags. 1546 */ 1547 public void listen(PhoneStateListener listener, int events) { 1548 String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>"; 1549 try { 1550 Boolean notifyNow = true; 1551 sRegistry.listen(pkgForDebug, listener.callback, events, notifyNow); 1552 } catch (RemoteException ex) { 1553 // system process dead 1554 } catch (NullPointerException ex) { 1555 // system process dead 1556 } 1557 } 1558 1559 /** 1560 * Returns the CDMA ERI icon index to display 1561 * 1562 * @hide 1563 */ 1564 public int getCdmaEriIconIndex() { 1565 try { 1566 return getITelephony().getCdmaEriIconIndex(); 1567 } catch (RemoteException ex) { 1568 // the phone process is restarting. 1569 return -1; 1570 } catch (NullPointerException ex) { 1571 return -1; 1572 } 1573 } 1574 1575 /** 1576 * Returns the CDMA ERI icon mode, 1577 * 0 - ON 1578 * 1 - FLASHING 1579 * 1580 * @hide 1581 */ 1582 public int getCdmaEriIconMode() { 1583 try { 1584 return getITelephony().getCdmaEriIconMode(); 1585 } catch (RemoteException ex) { 1586 // the phone process is restarting. 1587 return -1; 1588 } catch (NullPointerException ex) { 1589 return -1; 1590 } 1591 } 1592 1593 /** 1594 * Returns the CDMA ERI text, 1595 * 1596 * @hide 1597 */ 1598 public String getCdmaEriText() { 1599 try { 1600 return getITelephony().getCdmaEriText(); 1601 } catch (RemoteException ex) { 1602 // the phone process is restarting. 1603 return null; 1604 } catch (NullPointerException ex) { 1605 return null; 1606 } 1607 } 1608 1609 /** 1610 * @return true if the current device is "voice capable". 1611 * <p> 1612 * "Voice capable" means that this device supports circuit-switched 1613 * (i.e. voice) phone calls over the telephony network, and is allowed 1614 * to display the in-call UI while a cellular voice call is active. 1615 * This will be false on "data only" devices which can't make voice 1616 * calls and don't support any in-call UI. 1617 * <p> 1618 * Note: the meaning of this flag is subtly different from the 1619 * PackageManager.FEATURE_TELEPHONY system feature, which is available 1620 * on any device with a telephony radio, even if the device is 1621 * data-only. 1622 * 1623 * @hide pending API review 1624 */ 1625 public boolean isVoiceCapable() { 1626 if (mContext == null) return true; 1627 return mContext.getResources().getBoolean( 1628 com.android.internal.R.bool.config_voice_capable); 1629 } 1630 1631 /** 1632 * @return true if the current device supports sms service. 1633 * <p> 1634 * If true, this means that the device supports both sending and 1635 * receiving sms via the telephony network. 1636 * <p> 1637 * Note: Voicemail waiting sms, cell broadcasting sms, and MMS are 1638 * disabled when device doesn't support sms. 1639 * 1640 * @hide pending API review 1641 */ 1642 public boolean isSmsCapable() { 1643 if (mContext == null) return true; 1644 return mContext.getResources().getBoolean( 1645 com.android.internal.R.bool.config_sms_capable); 1646 } 1647 1648 /** 1649 * Returns all observed cell information from all radios on the 1650 * device including the primary and neighboring cells. This does 1651 * not cause or change the rate of PhoneStateListner#onCellInfoChanged. 1652 *<p> 1653 * The list can include one or more of {@link android.telephony.CellInfoGsm CellInfoGsm}, 1654 * {@link android.telephony.CellInfoCdma CellInfoCdma}, 1655 * {@link android.telephony.CellInfoLte CellInfoLte} and 1656 * {@link android.telephony.CellInfoWcdma CellInfoCdma} in any combination. 1657 * Specifically on devices with multiple radios it is typical to see instances of 1658 * one or more of any these in the list. In addition 0, 1 or more CellInfo 1659 * objects may return isRegistered() true. 1660 *<p> 1661 * This is preferred over using getCellLocation although for older 1662 * devices this may return null in which case getCellLocation should 1663 * be called. 1664 *<p> 1665 * @return List of CellInfo or null if info unavailable. 1666 * 1667 * <p>Requires Permission: {@link android.Manifest.permission#ACCESS_COARSE_LOCATION} 1668 */ 1669 public List<CellInfo> getAllCellInfo() { 1670 try { 1671 return getITelephony().getAllCellInfo(); 1672 } catch (RemoteException ex) { 1673 return null; 1674 } catch (NullPointerException ex) { 1675 return null; 1676 } 1677 } 1678 1679 /** 1680 * Sets the minimum time in milli-seconds between {@link PhoneStateListener#onCellInfoChanged 1681 * PhoneStateListener.onCellInfoChanged} will be invoked. 1682 *<p> 1683 * The default, 0, means invoke onCellInfoChanged when any of the reported 1684 * information changes. Setting the value to INT_MAX(0x7fffffff) means never issue 1685 * A onCellInfoChanged. 1686 *<p> 1687 * @param rateInMillis the rate 1688 * 1689 * @hide 1690 */ 1691 public void setCellInfoListRate(int rateInMillis) { 1692 try { 1693 getITelephony().setCellInfoListRate(rateInMillis); 1694 } catch (RemoteException ex) { 1695 } catch (NullPointerException ex) { 1696 } 1697 } 1698 1699 /** 1700 * Returns the MMS user agent. 1701 */ 1702 public String getMmsUserAgent() { 1703 if (mContext == null) return null; 1704 return mContext.getResources().getString( 1705 com.android.internal.R.string.config_mms_user_agent); 1706 } 1707 1708 /** 1709 * Returns the MMS user agent profile URL. 1710 */ 1711 public String getMmsUAProfUrl() { 1712 if (mContext == null) return null; 1713 return mContext.getResources().getString( 1714 com.android.internal.R.string.config_mms_user_agent_profile_url); 1715 } 1716} 1717