TelephonyManager.java revision c5ac15a3e11c03951e269b243674858411204b67
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 876 /** 877 * @return the NETWORK_TYPE_xxxx for current data connection. 878 */ 879 public int getNetworkType() { 880 return getDataNetworkType(); 881 } 882 883 /** 884 * Returns a constant indicating the radio technology (network type) 885 * currently in use on the device for data transmission. 886 * @return the network type 887 * 888 * @see #NETWORK_TYPE_UNKNOWN 889 * @see #NETWORK_TYPE_GPRS 890 * @see #NETWORK_TYPE_EDGE 891 * @see #NETWORK_TYPE_UMTS 892 * @see #NETWORK_TYPE_HSDPA 893 * @see #NETWORK_TYPE_HSUPA 894 * @see #NETWORK_TYPE_HSPA 895 * @see #NETWORK_TYPE_CDMA 896 * @see #NETWORK_TYPE_EVDO_0 897 * @see #NETWORK_TYPE_EVDO_A 898 * @see #NETWORK_TYPE_EVDO_B 899 * @see #NETWORK_TYPE_1xRTT 900 * @see #NETWORK_TYPE_IDEN 901 * @see #NETWORK_TYPE_LTE 902 * @see #NETWORK_TYPE_EHRPD 903 * @see #NETWORK_TYPE_HSPAP 904 * 905 * @hide 906 */ 907 public int getDataNetworkType() { 908 try{ 909 ITelephony telephony = getITelephony(); 910 if (telephony != null) { 911 return telephony.getDataNetworkType(); 912 } else { 913 // This can happen when the ITelephony interface is not up yet. 914 return NETWORK_TYPE_UNKNOWN; 915 } 916 } catch(RemoteException ex) { 917 // This shouldn't happen in the normal case 918 return NETWORK_TYPE_UNKNOWN; 919 } catch (NullPointerException ex) { 920 // This could happen before phone restarts due to crashing 921 return NETWORK_TYPE_UNKNOWN; 922 } 923 } 924 925 /** 926 * Returns the NETWORK_TYPE_xxxx for voice 927 * 928 * @hide 929 */ 930 public int getVoiceNetworkType() { 931 try{ 932 ITelephony telephony = getITelephony(); 933 if (telephony != null) { 934 return telephony.getVoiceNetworkType(); 935 } else { 936 // This can happen when the ITelephony interface is not up yet. 937 return NETWORK_TYPE_UNKNOWN; 938 } 939 } catch(RemoteException ex) { 940 // This shouldn't happen in the normal case 941 return NETWORK_TYPE_UNKNOWN; 942 } catch (NullPointerException ex) { 943 // This could happen before phone restarts due to crashing 944 return NETWORK_TYPE_UNKNOWN; 945 } 946 } 947 948 /** Unknown network class. {@hide} */ 949 public static final int NETWORK_CLASS_UNKNOWN = 0; 950 /** Class of broadly defined "2G" networks. {@hide} */ 951 public static final int NETWORK_CLASS_2_G = 1; 952 /** Class of broadly defined "3G" networks. {@hide} */ 953 public static final int NETWORK_CLASS_3_G = 2; 954 /** Class of broadly defined "4G" networks. {@hide} */ 955 public static final int NETWORK_CLASS_4_G = 3; 956 957 /** 958 * Return general class of network type, such as "3G" or "4G". In cases 959 * where classification is contentious, this method is conservative. 960 * 961 * @hide 962 */ 963 public static int getNetworkClass(int networkType) { 964 switch (networkType) { 965 case NETWORK_TYPE_GPRS: 966 case NETWORK_TYPE_EDGE: 967 case NETWORK_TYPE_CDMA: 968 case NETWORK_TYPE_1xRTT: 969 case NETWORK_TYPE_IDEN: 970 return NETWORK_CLASS_2_G; 971 case NETWORK_TYPE_UMTS: 972 case NETWORK_TYPE_EVDO_0: 973 case NETWORK_TYPE_EVDO_A: 974 case NETWORK_TYPE_HSDPA: 975 case NETWORK_TYPE_HSUPA: 976 case NETWORK_TYPE_HSPA: 977 case NETWORK_TYPE_EVDO_B: 978 case NETWORK_TYPE_EHRPD: 979 case NETWORK_TYPE_HSPAP: 980 return NETWORK_CLASS_3_G; 981 case NETWORK_TYPE_LTE: 982 return NETWORK_CLASS_4_G; 983 default: 984 return NETWORK_CLASS_UNKNOWN; 985 } 986 } 987 988 /** 989 * Returns a string representation of the radio technology (network type) 990 * currently in use on the device. 991 * @return the name of the radio technology 992 * 993 * @hide pending API council review 994 */ 995 public String getNetworkTypeName() { 996 return getNetworkTypeName(getNetworkType()); 997 } 998 999 /** {@hide} */ 1000 public static String getNetworkTypeName(int type) { 1001 switch (type) { 1002 case NETWORK_TYPE_GPRS: 1003 return "GPRS"; 1004 case NETWORK_TYPE_EDGE: 1005 return "EDGE"; 1006 case NETWORK_TYPE_UMTS: 1007 return "UMTS"; 1008 case NETWORK_TYPE_HSDPA: 1009 return "HSDPA"; 1010 case NETWORK_TYPE_HSUPA: 1011 return "HSUPA"; 1012 case NETWORK_TYPE_HSPA: 1013 return "HSPA"; 1014 case NETWORK_TYPE_CDMA: 1015 return "CDMA"; 1016 case NETWORK_TYPE_EVDO_0: 1017 return "CDMA - EvDo rev. 0"; 1018 case NETWORK_TYPE_EVDO_A: 1019 return "CDMA - EvDo rev. A"; 1020 case NETWORK_TYPE_EVDO_B: 1021 return "CDMA - EvDo rev. B"; 1022 case NETWORK_TYPE_1xRTT: 1023 return "CDMA - 1xRTT"; 1024 case NETWORK_TYPE_LTE: 1025 return "LTE"; 1026 case NETWORK_TYPE_EHRPD: 1027 return "CDMA - eHRPD"; 1028 case NETWORK_TYPE_IDEN: 1029 return "iDEN"; 1030 case NETWORK_TYPE_HSPAP: 1031 return "HSPA+"; 1032 default: 1033 return "UNKNOWN"; 1034 } 1035 } 1036 1037 // 1038 // 1039 // SIM Card 1040 // 1041 // 1042 1043 /** SIM card state: Unknown. Signifies that the SIM is in transition 1044 * between states. For example, when the user inputs the SIM pin 1045 * under PIN_REQUIRED state, a query for sim status returns 1046 * this state before turning to SIM_STATE_READY. */ 1047 public static final int SIM_STATE_UNKNOWN = 0; 1048 /** SIM card state: no SIM card is available in the device */ 1049 public static final int SIM_STATE_ABSENT = 1; 1050 /** SIM card state: Locked: requires the user's SIM PIN to unlock */ 1051 public static final int SIM_STATE_PIN_REQUIRED = 2; 1052 /** SIM card state: Locked: requires the user's SIM PUK to unlock */ 1053 public static final int SIM_STATE_PUK_REQUIRED = 3; 1054 /** SIM card state: Locked: requries a network PIN to unlock */ 1055 public static final int SIM_STATE_NETWORK_LOCKED = 4; 1056 /** SIM card state: Ready */ 1057 public static final int SIM_STATE_READY = 5; 1058 1059 /** 1060 * @return true if a ICC card is present 1061 */ 1062 public boolean hasIccCard() { 1063 try { 1064 return getITelephony().hasIccCard(); 1065 } catch (RemoteException ex) { 1066 // Assume no ICC card if remote exception which shouldn't happen 1067 return false; 1068 } catch (NullPointerException ex) { 1069 // This could happen before phone restarts due to crashing 1070 return false; 1071 } 1072 } 1073 1074 /** 1075 * Returns a constant indicating the state of the 1076 * device SIM card. 1077 * 1078 * @see #SIM_STATE_UNKNOWN 1079 * @see #SIM_STATE_ABSENT 1080 * @see #SIM_STATE_PIN_REQUIRED 1081 * @see #SIM_STATE_PUK_REQUIRED 1082 * @see #SIM_STATE_NETWORK_LOCKED 1083 * @see #SIM_STATE_READY 1084 */ 1085 public int getSimState() { 1086 String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE); 1087 if ("ABSENT".equals(prop)) { 1088 return SIM_STATE_ABSENT; 1089 } 1090 else if ("PIN_REQUIRED".equals(prop)) { 1091 return SIM_STATE_PIN_REQUIRED; 1092 } 1093 else if ("PUK_REQUIRED".equals(prop)) { 1094 return SIM_STATE_PUK_REQUIRED; 1095 } 1096 else if ("NETWORK_LOCKED".equals(prop)) { 1097 return SIM_STATE_NETWORK_LOCKED; 1098 } 1099 else if ("READY".equals(prop)) { 1100 return SIM_STATE_READY; 1101 } 1102 else { 1103 return SIM_STATE_UNKNOWN; 1104 } 1105 } 1106 1107 /** 1108 * Returns the MCC+MNC (mobile country code + mobile network code) of the 1109 * provider of the SIM. 5 or 6 decimal digits. 1110 * <p> 1111 * Availability: SIM state must be {@link #SIM_STATE_READY} 1112 * 1113 * @see #getSimState 1114 */ 1115 public String getSimOperator() { 1116 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC); 1117 } 1118 1119 /** 1120 * Returns the Service Provider Name (SPN). 1121 * <p> 1122 * Availability: SIM state must be {@link #SIM_STATE_READY} 1123 * 1124 * @see #getSimState 1125 */ 1126 public String getSimOperatorName() { 1127 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA); 1128 } 1129 1130 /** 1131 * Returns the ISO country code equivalent for the SIM provider's country code. 1132 */ 1133 public String getSimCountryIso() { 1134 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY); 1135 } 1136 1137 /** 1138 * Returns the serial number of the SIM, if applicable. Return null if it is 1139 * unavailable. 1140 * <p> 1141 * Requires Permission: 1142 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1143 */ 1144 public String getSimSerialNumber() { 1145 try { 1146 return getSubscriberInfo().getIccSerialNumber(); 1147 } catch (RemoteException ex) { 1148 return null; 1149 } catch (NullPointerException ex) { 1150 // This could happen before phone restarts due to crashing 1151 return null; 1152 } 1153 } 1154 1155 /** 1156 * Return if the current radio is LTE on CDMA. This 1157 * is a tri-state return value as for a period of time 1158 * the mode may be unknown. 1159 * 1160 * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE} 1161 * or {@link PhoneConstants#LTE_ON_CDMA_TRUE} 1162 * 1163 * @hide 1164 */ 1165 public int getLteOnCdmaMode() { 1166 try { 1167 return getITelephony().getLteOnCdmaMode(); 1168 } catch (RemoteException ex) { 1169 // Assume no ICC card if remote exception which shouldn't happen 1170 return PhoneConstants.LTE_ON_CDMA_UNKNOWN; 1171 } catch (NullPointerException ex) { 1172 // This could happen before phone restarts due to crashing 1173 return PhoneConstants.LTE_ON_CDMA_UNKNOWN; 1174 } 1175 } 1176 1177 // 1178 // 1179 // Subscriber Info 1180 // 1181 // 1182 1183 /** 1184 * Returns the unique subscriber ID, for example, the IMSI for a GSM phone. 1185 * Return null if it is unavailable. 1186 * <p> 1187 * Requires Permission: 1188 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1189 */ 1190 public String getSubscriberId() { 1191 try { 1192 return getSubscriberInfo().getSubscriberId(); 1193 } catch (RemoteException ex) { 1194 return null; 1195 } catch (NullPointerException ex) { 1196 // This could happen before phone restarts due to crashing 1197 return null; 1198 } 1199 } 1200 1201 /** 1202 * Returns the Group Identifier Level1 for a GSM phone. 1203 * Return null if it is unavailable. 1204 * <p> 1205 * Requires Permission: 1206 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1207 */ 1208 public String getGroupIdLevel1() { 1209 try { 1210 return getSubscriberInfo().getGroupIdLevel1(); 1211 } catch (RemoteException ex) { 1212 return null; 1213 } catch (NullPointerException ex) { 1214 // This could happen before phone restarts due to crashing 1215 return null; 1216 } 1217 } 1218 1219 /** 1220 * Returns the phone number string for line 1, for example, the MSISDN 1221 * for a GSM phone. Return null if it is unavailable. 1222 * <p> 1223 * Requires Permission: 1224 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1225 */ 1226 public String getLine1Number() { 1227 try { 1228 return getSubscriberInfo().getLine1Number(); 1229 } catch (RemoteException ex) { 1230 return null; 1231 } catch (NullPointerException ex) { 1232 // This could happen before phone restarts due to crashing 1233 return null; 1234 } 1235 } 1236 1237 /** 1238 * Returns the alphabetic identifier associated with the line 1 number. 1239 * Return null if it is unavailable. 1240 * <p> 1241 * Requires Permission: 1242 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1243 * @hide 1244 * nobody seems to call this. 1245 */ 1246 public String getLine1AlphaTag() { 1247 try { 1248 return getSubscriberInfo().getLine1AlphaTag(); 1249 } catch (RemoteException ex) { 1250 return null; 1251 } catch (NullPointerException ex) { 1252 // This could happen before phone restarts due to crashing 1253 return null; 1254 } 1255 } 1256 1257 /** 1258 * Returns the MSISDN string. 1259 * for a GSM phone. Return null if it is unavailable. 1260 * <p> 1261 * Requires Permission: 1262 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1263 * 1264 * @hide 1265 */ 1266 public String getMsisdn() { 1267 try { 1268 return getSubscriberInfo().getMsisdn(); 1269 } catch (RemoteException ex) { 1270 return null; 1271 } catch (NullPointerException ex) { 1272 // This could happen before phone restarts due to crashing 1273 return null; 1274 } 1275 } 1276 1277 /** 1278 * Returns the voice mail number. Return null if it is unavailable. 1279 * <p> 1280 * Requires Permission: 1281 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1282 */ 1283 public String getVoiceMailNumber() { 1284 try { 1285 return getSubscriberInfo().getVoiceMailNumber(); 1286 } catch (RemoteException ex) { 1287 return null; 1288 } catch (NullPointerException ex) { 1289 // This could happen before phone restarts due to crashing 1290 return null; 1291 } 1292 } 1293 1294 /** 1295 * Returns the complete voice mail number. Return null if it is unavailable. 1296 * <p> 1297 * Requires Permission: 1298 * {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED} 1299 * 1300 * @hide 1301 */ 1302 public String getCompleteVoiceMailNumber() { 1303 try { 1304 return getSubscriberInfo().getCompleteVoiceMailNumber(); 1305 } catch (RemoteException ex) { 1306 return null; 1307 } catch (NullPointerException ex) { 1308 // This could happen before phone restarts due to crashing 1309 return null; 1310 } 1311 } 1312 1313 /** 1314 * Returns the voice mail count. Return 0 if unavailable. 1315 * <p> 1316 * Requires Permission: 1317 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1318 * @hide 1319 */ 1320 public int getVoiceMessageCount() { 1321 try { 1322 return getITelephony().getVoiceMessageCount(); 1323 } catch (RemoteException ex) { 1324 return 0; 1325 } catch (NullPointerException ex) { 1326 // This could happen before phone restarts due to crashing 1327 return 0; 1328 } 1329 } 1330 1331 /** 1332 * Retrieves the alphabetic identifier associated with the voice 1333 * mail number. 1334 * <p> 1335 * Requires Permission: 1336 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 1337 */ 1338 public String getVoiceMailAlphaTag() { 1339 try { 1340 return getSubscriberInfo().getVoiceMailAlphaTag(); 1341 } catch (RemoteException ex) { 1342 return null; 1343 } catch (NullPointerException ex) { 1344 // This could happen before phone restarts due to crashing 1345 return null; 1346 } 1347 } 1348 1349 /** 1350 * Returns the IMS private user identity (IMPI) that was loaded from the ISIM. 1351 * @return the IMPI, or null if not present or not loaded 1352 * @hide 1353 */ 1354 public String getIsimImpi() { 1355 try { 1356 return getSubscriberInfo().getIsimImpi(); 1357 } catch (RemoteException ex) { 1358 return null; 1359 } catch (NullPointerException ex) { 1360 // This could happen before phone restarts due to crashing 1361 return null; 1362 } 1363 } 1364 1365 /** 1366 * Returns the IMS home network domain name that was loaded from the ISIM. 1367 * @return the IMS domain name, or null if not present or not loaded 1368 * @hide 1369 */ 1370 public String getIsimDomain() { 1371 try { 1372 return getSubscriberInfo().getIsimDomain(); 1373 } catch (RemoteException ex) { 1374 return null; 1375 } catch (NullPointerException ex) { 1376 // This could happen before phone restarts due to crashing 1377 return null; 1378 } 1379 } 1380 1381 /** 1382 * Returns the IMS public user identities (IMPU) that were loaded from the ISIM. 1383 * @return an array of IMPU strings, with one IMPU per string, or null if 1384 * not present or not loaded 1385 * @hide 1386 */ 1387 public String[] getIsimImpu() { 1388 try { 1389 return getSubscriberInfo().getIsimImpu(); 1390 } catch (RemoteException ex) { 1391 return null; 1392 } catch (NullPointerException ex) { 1393 // This could happen before phone restarts due to crashing 1394 return null; 1395 } 1396 } 1397 1398 private IPhoneSubInfo getSubscriberInfo() { 1399 // get it each time because that process crashes a lot 1400 return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo")); 1401 } 1402 1403 1404 /** Device call state: No activity. */ 1405 public static final int CALL_STATE_IDLE = 0; 1406 /** Device call state: Ringing. A new call arrived and is 1407 * ringing or waiting. In the latter case, another call is 1408 * already active. */ 1409 public static final int CALL_STATE_RINGING = 1; 1410 /** Device call state: Off-hook. At least one call exists 1411 * that is dialing, active, or on hold, and no calls are ringing 1412 * or waiting. */ 1413 public static final int CALL_STATE_OFFHOOK = 2; 1414 1415 /** 1416 * Returns a constant indicating the call state (cellular) on the device. 1417 */ 1418 public int getCallState() { 1419 try { 1420 return getITelephony().getCallState(); 1421 } catch (RemoteException ex) { 1422 // the phone process is restarting. 1423 return CALL_STATE_IDLE; 1424 } catch (NullPointerException ex) { 1425 // the phone process is restarting. 1426 return CALL_STATE_IDLE; 1427 } 1428 } 1429 1430 /** Data connection activity: No traffic. */ 1431 public static final int DATA_ACTIVITY_NONE = 0x00000000; 1432 /** Data connection activity: Currently receiving IP PPP traffic. */ 1433 public static final int DATA_ACTIVITY_IN = 0x00000001; 1434 /** Data connection activity: Currently sending IP PPP traffic. */ 1435 public static final int DATA_ACTIVITY_OUT = 0x00000002; 1436 /** Data connection activity: Currently both sending and receiving 1437 * IP PPP traffic. */ 1438 public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT; 1439 /** 1440 * Data connection is active, but physical link is down 1441 */ 1442 public static final int DATA_ACTIVITY_DORMANT = 0x00000004; 1443 1444 /** 1445 * Returns a constant indicating the type of activity on a data connection 1446 * (cellular). 1447 * 1448 * @see #DATA_ACTIVITY_NONE 1449 * @see #DATA_ACTIVITY_IN 1450 * @see #DATA_ACTIVITY_OUT 1451 * @see #DATA_ACTIVITY_INOUT 1452 * @see #DATA_ACTIVITY_DORMANT 1453 */ 1454 public int getDataActivity() { 1455 try { 1456 return getITelephony().getDataActivity(); 1457 } catch (RemoteException ex) { 1458 // the phone process is restarting. 1459 return DATA_ACTIVITY_NONE; 1460 } catch (NullPointerException ex) { 1461 // the phone process is restarting. 1462 return DATA_ACTIVITY_NONE; 1463 } 1464 } 1465 1466 /** Data connection state: Unknown. Used before we know the state. 1467 * @hide 1468 */ 1469 public static final int DATA_UNKNOWN = -1; 1470 /** Data connection state: Disconnected. IP traffic not available. */ 1471 public static final int DATA_DISCONNECTED = 0; 1472 /** Data connection state: Currently setting up a data connection. */ 1473 public static final int DATA_CONNECTING = 1; 1474 /** Data connection state: Connected. IP traffic should be available. */ 1475 public static final int DATA_CONNECTED = 2; 1476 /** Data connection state: Suspended. The connection is up, but IP 1477 * traffic is temporarily unavailable. For example, in a 2G network, 1478 * data activity may be suspended when a voice call arrives. */ 1479 public static final int DATA_SUSPENDED = 3; 1480 1481 /** 1482 * Returns a constant indicating the current data connection state 1483 * (cellular). 1484 * 1485 * @see #DATA_DISCONNECTED 1486 * @see #DATA_CONNECTING 1487 * @see #DATA_CONNECTED 1488 * @see #DATA_SUSPENDED 1489 */ 1490 public int getDataState() { 1491 try { 1492 return getITelephony().getDataState(); 1493 } catch (RemoteException ex) { 1494 // the phone process is restarting. 1495 return DATA_DISCONNECTED; 1496 } catch (NullPointerException ex) { 1497 return DATA_DISCONNECTED; 1498 } 1499 } 1500 1501 private ITelephony getITelephony() { 1502 return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE)); 1503 } 1504 1505 // 1506 // 1507 // PhoneStateListener 1508 // 1509 // 1510 1511 /** 1512 * Registers a listener object to receive notification of changes 1513 * in specified telephony states. 1514 * <p> 1515 * To register a listener, pass a {@link PhoneStateListener} 1516 * and specify at least one telephony state of interest in 1517 * the events argument. 1518 * 1519 * At registration, and when a specified telephony state 1520 * changes, the telephony manager invokes the appropriate 1521 * callback method on the listener object and passes the 1522 * current (udpated) values. 1523 * <p> 1524 * To unregister a listener, pass the listener object and set the 1525 * events argument to 1526 * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0). 1527 * 1528 * @param listener The {@link PhoneStateListener} object to register 1529 * (or unregister) 1530 * @param events The telephony state(s) of interest to the listener, 1531 * as a bitwise-OR combination of {@link PhoneStateListener} 1532 * LISTEN_ flags. 1533 */ 1534 public void listen(PhoneStateListener listener, int events) { 1535 String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>"; 1536 try { 1537 Boolean notifyNow = true; 1538 sRegistry.listen(pkgForDebug, listener.callback, events, notifyNow); 1539 } catch (RemoteException ex) { 1540 // system process dead 1541 } catch (NullPointerException ex) { 1542 // system process dead 1543 } 1544 } 1545 1546 /** 1547 * Returns the CDMA ERI icon index to display 1548 * 1549 * @hide 1550 */ 1551 public int getCdmaEriIconIndex() { 1552 try { 1553 return getITelephony().getCdmaEriIconIndex(); 1554 } catch (RemoteException ex) { 1555 // the phone process is restarting. 1556 return -1; 1557 } catch (NullPointerException ex) { 1558 return -1; 1559 } 1560 } 1561 1562 /** 1563 * Returns the CDMA ERI icon mode, 1564 * 0 - ON 1565 * 1 - FLASHING 1566 * 1567 * @hide 1568 */ 1569 public int getCdmaEriIconMode() { 1570 try { 1571 return getITelephony().getCdmaEriIconMode(); 1572 } catch (RemoteException ex) { 1573 // the phone process is restarting. 1574 return -1; 1575 } catch (NullPointerException ex) { 1576 return -1; 1577 } 1578 } 1579 1580 /** 1581 * Returns the CDMA ERI text, 1582 * 1583 * @hide 1584 */ 1585 public String getCdmaEriText() { 1586 try { 1587 return getITelephony().getCdmaEriText(); 1588 } catch (RemoteException ex) { 1589 // the phone process is restarting. 1590 return null; 1591 } catch (NullPointerException ex) { 1592 return null; 1593 } 1594 } 1595 1596 /** 1597 * @return true if the current device is "voice capable". 1598 * <p> 1599 * "Voice capable" means that this device supports circuit-switched 1600 * (i.e. voice) phone calls over the telephony network, and is allowed 1601 * to display the in-call UI while a cellular voice call is active. 1602 * This will be false on "data only" devices which can't make voice 1603 * calls and don't support any in-call UI. 1604 * <p> 1605 * Note: the meaning of this flag is subtly different from the 1606 * PackageManager.FEATURE_TELEPHONY system feature, which is available 1607 * on any device with a telephony radio, even if the device is 1608 * data-only. 1609 * 1610 * @hide pending API review 1611 */ 1612 public boolean isVoiceCapable() { 1613 if (mContext == null) return true; 1614 return mContext.getResources().getBoolean( 1615 com.android.internal.R.bool.config_voice_capable); 1616 } 1617 1618 /** 1619 * @return true if the current device supports sms service. 1620 * <p> 1621 * If true, this means that the device supports both sending and 1622 * receiving sms via the telephony network. 1623 * <p> 1624 * Note: Voicemail waiting sms, cell broadcasting sms, and MMS are 1625 * disabled when device doesn't support sms. 1626 * 1627 * @hide pending API review 1628 */ 1629 public boolean isSmsCapable() { 1630 if (mContext == null) return true; 1631 return mContext.getResources().getBoolean( 1632 com.android.internal.R.bool.config_sms_capable); 1633 } 1634 1635 /** 1636 * Returns all observed cell information from all radios on the 1637 * device including the primary and neighboring cells. This does 1638 * not cause or change the rate of PhoneStateListner#onCellInfoChanged. 1639 *<p> 1640 * The list can include one or more of {@link android.telephony.CellInfoGsm CellInfoGsm}, 1641 * {@link android.telephony.CellInfoCdma CellInfoCdma}, 1642 * {@link android.telephony.CellInfoLte CellInfoLte} and 1643 * {@link android.telephony.CellInfoWcdma CellInfoCdma} in any combination. 1644 * Specifically on devices with multiple radios it is typical to see instances of 1645 * one or more of any these in the list. In addition 0, 1 or more CellInfo 1646 * objects may return isRegistered() true. 1647 *<p> 1648 * This is preferred over using getCellLocation although for older 1649 * devices this may return null in which case getCellLocation should 1650 * be called. 1651 *<p> 1652 * @return List of CellInfo or null if info unavailable. 1653 * 1654 * <p>Requires Permission: {@link android.Manifest.permission#ACCESS_COARSE_LOCATION} 1655 */ 1656 public List<CellInfo> getAllCellInfo() { 1657 try { 1658 return getITelephony().getAllCellInfo(); 1659 } catch (RemoteException ex) { 1660 return null; 1661 } catch (NullPointerException ex) { 1662 return null; 1663 } 1664 } 1665 1666 /** 1667 * Sets the minimum time in milli-seconds between {@link PhoneStateListener#onCellInfoChanged 1668 * PhoneStateListener.onCellInfoChanged} will be invoked. 1669 *<p> 1670 * The default, 0, means invoke onCellInfoChanged when any of the reported 1671 * information changes. Setting the value to INT_MAX(0x7fffffff) means never issue 1672 * A onCellInfoChanged. 1673 *<p> 1674 * @param rateInMillis the rate 1675 * 1676 * @hide 1677 */ 1678 public void setCellInfoListRate(int rateInMillis) { 1679 try { 1680 getITelephony().setCellInfoListRate(rateInMillis); 1681 } catch (RemoteException ex) { 1682 } catch (NullPointerException ex) { 1683 } 1684 } 1685 1686 /** 1687 * Returns the MMS user agent. 1688 */ 1689 public String getMmsUserAgent() { 1690 if (mContext == null) return null; 1691 return mContext.getResources().getString( 1692 com.android.internal.R.string.config_mms_user_agent); 1693 } 1694 1695 /** 1696 * Returns the MMS user agent profile URL. 1697 */ 1698 public String getMmsUAProfUrl() { 1699 if (mContext == null) return null; 1700 return mContext.getResources().getString( 1701 com.android.internal.R.string.config_mms_user_agent_profile_url); 1702 } 1703} 1704