2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.telephony;
19 import android.annotation.SdkConstant;
20 import android.annotation.SdkConstant.SdkConstantType;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.os.RemoteException;
24 import android.os.ServiceManager;
25 import android.os.SystemProperties;
26 import android.telephony.Rlog;
28 import com.android.internal.telephony.IPhoneSubInfo;
29 import com.android.internal.telephony.ITelephony;
30 import com.android.internal.telephony.ITelephonyRegistry;
31 import com.android.internal.telephony.PhoneConstants;
32 import com.android.internal.telephony.RILConstants;
33 import com.android.internal.telephony.TelephonyProperties;
35 import java.io.FileInputStream;
36 import java.io.IOException;
37 import java.util.List;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
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.
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)}.
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.
59 public class TelephonyManager {
60 private static final String TAG = "TelephonyManager";
62 private static ITelephonyRegistry sRegistry;
63 private final Context mContext;
66 public TelephonyManager(Context context) {
67 Context appContext = context.getApplicationContext();
68 if (appContext != null) {
69 mContext = appContext;
74 if (sRegistry == null) {
75 sRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
76 "telephony.registry"));
81 private TelephonyManager() {
85 private static TelephonyManager sInstance = new TelephonyManager();
88 /* @deprecated - use getSystemService as described above */
89 public static TelephonyManager getDefault() {
94 public static TelephonyManager from(Context context) {
95 return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
99 // Broadcast Intent actions
103 * Broadcast intent action indicating that the call state (cellular)
104 * on the device has changed.
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
113 * Requires the READ_PHONE_STATE permission.
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.
121 * @see #EXTRA_INCOMING_NUMBER
124 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
125 public static final String ACTION_PHONE_STATE_CHANGED =
126 "android.intent.action.PHONE_STATE";
129 * The Phone app sends this intent when a user opts to respond-via-message during an incoming
130 * call. By default, the MMS app consumes this message and sends a text message to the caller. A
131 * third party app can provide this functionality in lieu of MMS app by consuming this Intent
132 * and sending the message using their own messaging system. The intent contains a URI
133 * describing the recipient, and an EXTRA containing the message itself.
134 * <p class="note"><strong>Note:</strong>
135 * The intent-filter which consumes this Intent needs to be in a service which requires the
136 * permission {@link android.Manifest.permission#SEND_RESPOND_VIA_MESSAGE}.</p>
139 * {@link android.content.Intent#getData} is a URI describing the recipient of the message.
141 * The {@link android.content.Intent#EXTRA_TEXT} extra contains the message
146 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
147 public static final String ACTION_RESPOND_VIA_MESSAGE =
148 "android.intent.action.RESPOND_VIA_MESSAGE";
151 * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
152 * for a String containing the new call state.
154 * @see #EXTRA_STATE_IDLE
155 * @see #EXTRA_STATE_RINGING
156 * @see #EXTRA_STATE_OFFHOOK
160 * {@link android.content.Intent#getStringExtra(String)}.
162 public static final String EXTRA_STATE = PhoneConstants.STATE_KEY;
165 * Value used with {@link #EXTRA_STATE} corresponding to
166 * {@link #CALL_STATE_IDLE}.
168 public static final String EXTRA_STATE_IDLE = PhoneConstants.State.IDLE.toString();
171 * Value used with {@link #EXTRA_STATE} corresponding to
172 * {@link #CALL_STATE_RINGING}.
174 public static final String EXTRA_STATE_RINGING = PhoneConstants.State.RINGING.toString();
177 * Value used with {@link #EXTRA_STATE} corresponding to
178 * {@link #CALL_STATE_OFFHOOK}.
180 public static final String EXTRA_STATE_OFFHOOK = PhoneConstants.State.OFFHOOK.toString();
183 * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
184 * for a String containing the incoming phone number.
185 * Only valid when the new call state is RINGING.
189 * {@link android.content.Intent#getStringExtra(String)}.
191 public static final String EXTRA_INCOMING_NUMBER = "incoming_number";
201 * Returns the software version number for the device, for example,
202 * the IMEI/SV for GSM phones. Return null if the software version is
205 * <p>Requires Permission:
206 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
208 public String getDeviceSoftwareVersion() {
210 return getSubscriberInfo().getDeviceSvn();
211 } catch (RemoteException ex) {
213 } catch (NullPointerException ex) {
219 * Returns the unique device ID, for example, the IMEI for GSM and the MEID
220 * or ESN for CDMA phones. Return null if device ID is not available.
222 * <p>Requires Permission:
223 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
225 public String getDeviceId() {
227 return getSubscriberInfo().getDeviceId();
228 } catch (RemoteException ex) {
230 } catch (NullPointerException ex) {
236 * Returns the current location of the device.
238 * If there is only one radio in the device and that radio has an LTE connection,
239 * this method will return null. The implementation must not to try add LTE
240 * identifiers into the existing cdma/gsm classes.
242 * In the future this call will be deprecated.
244 * @return Current location of the device or null if not available.
246 * <p>Requires Permission:
247 * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} or
248 * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION}.
250 public CellLocation getCellLocation() {
252 Bundle bundle = getITelephony().getCellLocation();
253 if (bundle.isEmpty()) return null;
254 CellLocation cl = CellLocation.newFromBundle(bundle);
258 } catch (RemoteException ex) {
260 } catch (NullPointerException ex) {
266 * Enables location update notifications. {@link PhoneStateListener#onCellLocationChanged
267 * PhoneStateListener.onCellLocationChanged} will be called on location updates.
269 * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
270 * CONTROL_LOCATION_UPDATES}
274 public void enableLocationUpdates() {
276 getITelephony().enableLocationUpdates();
277 } catch (RemoteException ex) {
278 } catch (NullPointerException ex) {
283 * Disables location update notifications. {@link PhoneStateListener#onCellLocationChanged
284 * PhoneStateListener.onCellLocationChanged} will be called on location updates.
286 * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
287 * CONTROL_LOCATION_UPDATES}
291 public void disableLocationUpdates() {
293 getITelephony().disableLocationUpdates();
294 } catch (RemoteException ex) {
295 } catch (NullPointerException ex) {
300 * Returns the neighboring cell information of the device. The getAllCellInfo is preferred
301 * and use this only if getAllCellInfo return nulls or an empty list.
303 * In the future this call will be deprecated.
305 * @return List of NeighboringCellInfo or null if info unavailable.
307 * <p>Requires Permission:
308 * (@link android.Manifest.permission#ACCESS_COARSE_UPDATES}
310 public List<NeighboringCellInfo> getNeighboringCellInfo() {
312 return getITelephony().getNeighboringCellInfo(mContext.getOpPackageName());
313 } catch (RemoteException ex) {
315 } catch (NullPointerException ex) {
320 /** No phone radio. */
321 public static final int PHONE_TYPE_NONE = PhoneConstants.PHONE_TYPE_NONE;
322 /** Phone radio is GSM. */
323 public static final int PHONE_TYPE_GSM = PhoneConstants.PHONE_TYPE_GSM;
324 /** Phone radio is CDMA. */
325 public static final int PHONE_TYPE_CDMA = PhoneConstants.PHONE_TYPE_CDMA;
326 /** Phone is via SIP. */
327 public static final int PHONE_TYPE_SIP = PhoneConstants.PHONE_TYPE_SIP;
330 * Returns the current phone type.
331 * TODO: This is a last minute change and hence hidden.
333 * @see #PHONE_TYPE_NONE
334 * @see #PHONE_TYPE_GSM
335 * @see #PHONE_TYPE_CDMA
336 * @see #PHONE_TYPE_SIP
340 public int getCurrentPhoneType() {
342 ITelephony telephony = getITelephony();
343 if (telephony != null) {
344 return telephony.getActivePhoneType();
346 // This can happen when the ITelephony interface is not up yet.
347 return getPhoneTypeFromProperty();
349 } catch (RemoteException ex) {
350 // This shouldn't happen in the normal case, as a backup we
351 // read from the system property.
352 return getPhoneTypeFromProperty();
353 } catch (NullPointerException ex) {
354 // This shouldn't happen in the normal case, as a backup we
355 // read from the system property.
356 return getPhoneTypeFromProperty();
361 * Returns a constant indicating the device phone type. This
362 * indicates the type of radio used to transmit voice calls.
364 * @see #PHONE_TYPE_NONE
365 * @see #PHONE_TYPE_GSM
366 * @see #PHONE_TYPE_CDMA
367 * @see #PHONE_TYPE_SIP
369 public int getPhoneType() {
370 if (!isVoiceCapable()) {
371 return PHONE_TYPE_NONE;
373 return getCurrentPhoneType();
376 private int getPhoneTypeFromProperty() {
378 SystemProperties.getInt(TelephonyProperties.CURRENT_ACTIVE_PHONE,
379 getPhoneTypeFromNetworkType());
383 private int getPhoneTypeFromNetworkType() {
384 // When the system property CURRENT_ACTIVE_PHONE, has not been set,
385 // use the system property for default network type.
386 // This is a fail safe, and can only happen at first boot.
387 int mode = SystemProperties.getInt("ro.telephony.default_network", -1);
389 return PHONE_TYPE_NONE;
390 return getPhoneType(mode);
394 * This function returns the type of the phone, depending
395 * on the network mode.
402 public static int getPhoneType(int networkMode) {
403 switch(networkMode) {
404 case RILConstants.NETWORK_MODE_CDMA:
405 case RILConstants.NETWORK_MODE_CDMA_NO_EVDO:
406 case RILConstants.NETWORK_MODE_EVDO_NO_CDMA:
407 return PhoneConstants.PHONE_TYPE_CDMA;
409 case RILConstants.NETWORK_MODE_WCDMA_PREF:
410 case RILConstants.NETWORK_MODE_GSM_ONLY:
411 case RILConstants.NETWORK_MODE_WCDMA_ONLY:
412 case RILConstants.NETWORK_MODE_GSM_UMTS:
413 case RILConstants.NETWORK_MODE_LTE_GSM_WCDMA:
414 case RILConstants.NETWORK_MODE_LTE_WCDMA:
415 case RILConstants.NETWORK_MODE_LTE_CMDA_EVDO_GSM_WCDMA:
416 return PhoneConstants.PHONE_TYPE_GSM;
418 // Use CDMA Phone for the global mode including CDMA
419 case RILConstants.NETWORK_MODE_GLOBAL:
420 case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO:
421 return PhoneConstants.PHONE_TYPE_CDMA;
423 case RILConstants.NETWORK_MODE_LTE_ONLY:
424 if (getLteOnCdmaModeStatic() == PhoneConstants.LTE_ON_CDMA_TRUE) {
425 return PhoneConstants.PHONE_TYPE_CDMA;
427 return PhoneConstants.PHONE_TYPE_GSM;
430 return PhoneConstants.PHONE_TYPE_GSM;
435 * The contents of the /proc/cmdline file
437 private static String getProcCmdLine()
440 FileInputStream is = null;
442 is = new FileInputStream("/proc/cmdline");
443 byte [] buffer = new byte[2048];
444 int count = is.read(buffer);
446 cmdline = new String(buffer, 0, count);
448 } catch (IOException e) {
449 Rlog.d(TAG, "No /proc/cmdline exception=" + e);
454 } catch (IOException e) {
458 Rlog.d(TAG, "/proc/cmdline=" + cmdline);
462 /** Kernel command line */
463 private static final String sKernelCmdLine = getProcCmdLine();
465 /** Pattern for selecting the product type from the kernel command line */
466 private static final Pattern sProductTypePattern =
467 Pattern.compile("\\sproduct_type\\s*=\\s*(\\w+)");
469 /** The ProductType used for LTE on CDMA devices */
470 private static final String sLteOnCdmaProductType =
471 SystemProperties.get(TelephonyProperties.PROPERTY_LTE_ON_CDMA_PRODUCT_TYPE, "");
474 * Return if the current radio is LTE on CDMA. This
475 * is a tri-state return value as for a period of time
476 * the mode may be unknown.
478 * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE}
479 * or {@link PhoneConstants#LTE_ON_CDMA_TRUE}
483 public static int getLteOnCdmaModeStatic() {
486 String productType = "";
488 curVal = SystemProperties.getInt(TelephonyProperties.PROPERTY_LTE_ON_CDMA_DEVICE,
489 PhoneConstants.LTE_ON_CDMA_UNKNOWN);
491 if (retVal == PhoneConstants.LTE_ON_CDMA_UNKNOWN) {
492 Matcher matcher = sProductTypePattern.matcher(sKernelCmdLine);
493 if (matcher.find()) {
494 productType = matcher.group(1);
495 if (sLteOnCdmaProductType.equals(productType)) {
496 retVal = PhoneConstants.LTE_ON_CDMA_TRUE;
498 retVal = PhoneConstants.LTE_ON_CDMA_FALSE;
501 retVal = PhoneConstants.LTE_ON_CDMA_FALSE;
505 Rlog.d(TAG, "getLteOnCdmaMode=" + retVal + " curVal=" + curVal +
506 " product_type='" + productType +
507 "' lteOnCdmaProductType='" + sLteOnCdmaProductType + "'");
518 * Returns the alphabetic name of current registered operator.
520 * Availability: Only when user is registered to a network. Result may be
521 * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
522 * on a CDMA network).
524 public String getNetworkOperatorName() {
525 return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ALPHA);
529 * Returns the numeric name (MCC+MNC) of current registered operator.
531 * Availability: Only when user is registered to a network. Result may be
532 * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
533 * on a CDMA network).
535 public String getNetworkOperator() {
536 return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_NUMERIC);
540 * Returns true if the device is considered roaming on the current
541 * network, for GSM purposes.
543 * Availability: Only when user registered to a network.
545 public boolean isNetworkRoaming() {
546 return "true".equals(SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING));
550 * Returns the ISO country code equivalent of the current registered
551 * operator's MCC (Mobile Country Code).
553 * Availability: Only when user is registered to a network. Result may be
554 * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
555 * on a CDMA network).
557 public String getNetworkCountryIso() {
558 return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY);
561 /** Network type is unknown */
562 public static final int NETWORK_TYPE_UNKNOWN = 0;
563 /** Current network is GPRS */
564 public static final int NETWORK_TYPE_GPRS = 1;
565 /** Current network is EDGE */
566 public static final int NETWORK_TYPE_EDGE = 2;
567 /** Current network is UMTS */
568 public static final int NETWORK_TYPE_UMTS = 3;
569 /** Current network is CDMA: Either IS95A or IS95B*/
570 public static final int NETWORK_TYPE_CDMA = 4;
571 /** Current network is EVDO revision 0*/
572 public static final int NETWORK_TYPE_EVDO_0 = 5;
573 /** Current network is EVDO revision A*/
574 public static final int NETWORK_TYPE_EVDO_A = 6;
575 /** Current network is 1xRTT*/
576 public static final int NETWORK_TYPE_1xRTT = 7;
577 /** Current network is HSDPA */
578 public static final int NETWORK_TYPE_HSDPA = 8;
579 /** Current network is HSUPA */
580 public static final int NETWORK_TYPE_HSUPA = 9;
581 /** Current network is HSPA */
582 public static final int NETWORK_TYPE_HSPA = 10;
583 /** Current network is iDen */
584 public static final int NETWORK_TYPE_IDEN = 11;
585 /** Current network is EVDO revision B*/
586 public static final int NETWORK_TYPE_EVDO_B = 12;
587 /** Current network is LTE */
588 public static final int NETWORK_TYPE_LTE = 13;
589 /** Current network is eHRPD */
590 public static final int NETWORK_TYPE_EHRPD = 14;
591 /** Current network is HSPA+ */
592 public static final int NETWORK_TYPE_HSPAP = 15;
595 * @return the NETWORK_TYPE_xxxx for current data connection.
597 public int getNetworkType() {
598 return getDataNetworkType();
602 * Returns a constant indicating the radio technology (network type)
603 * currently in use on the device for data transmission.
604 * @return the network type
606 * @see #NETWORK_TYPE_UNKNOWN
607 * @see #NETWORK_TYPE_GPRS
608 * @see #NETWORK_TYPE_EDGE
609 * @see #NETWORK_TYPE_UMTS
610 * @see #NETWORK_TYPE_HSDPA
611 * @see #NETWORK_TYPE_HSUPA
612 * @see #NETWORK_TYPE_HSPA
613 * @see #NETWORK_TYPE_CDMA
614 * @see #NETWORK_TYPE_EVDO_0
615 * @see #NETWORK_TYPE_EVDO_A
616 * @see #NETWORK_TYPE_EVDO_B
617 * @see #NETWORK_TYPE_1xRTT
618 * @see #NETWORK_TYPE_IDEN
619 * @see #NETWORK_TYPE_LTE
620 * @see #NETWORK_TYPE_EHRPD
621 * @see #NETWORK_TYPE_HSPAP
625 public int getDataNetworkType() {
627 ITelephony telephony = getITelephony();
628 if (telephony != null) {
629 return telephony.getDataNetworkType();
631 // This can happen when the ITelephony interface is not up yet.
632 return NETWORK_TYPE_UNKNOWN;
634 } catch(RemoteException ex) {
635 // This shouldn't happen in the normal case
636 return NETWORK_TYPE_UNKNOWN;
637 } catch (NullPointerException ex) {
638 // This could happen before phone restarts due to crashing
639 return NETWORK_TYPE_UNKNOWN;
644 * Returns the NETWORK_TYPE_xxxx for voice
648 public int getVoiceNetworkType() {
650 ITelephony telephony = getITelephony();
651 if (telephony != null) {
652 return telephony.getVoiceNetworkType();
654 // This can happen when the ITelephony interface is not up yet.
655 return NETWORK_TYPE_UNKNOWN;
657 } catch(RemoteException ex) {
658 // This shouldn't happen in the normal case
659 return NETWORK_TYPE_UNKNOWN;
660 } catch (NullPointerException ex) {
661 // This could happen before phone restarts due to crashing
662 return NETWORK_TYPE_UNKNOWN;
666 /** Unknown network class. {@hide} */
667 public static final int NETWORK_CLASS_UNKNOWN = 0;
668 /** Class of broadly defined "2G" networks. {@hide} */
669 public static final int NETWORK_CLASS_2_G = 1;
670 /** Class of broadly defined "3G" networks. {@hide} */
671 public static final int NETWORK_CLASS_3_G = 2;
672 /** Class of broadly defined "4G" networks. {@hide} */
673 public static final int NETWORK_CLASS_4_G = 3;
676 * Return general class of network type, such as "3G" or "4G". In cases
677 * where classification is contentious, this method is conservative.
681 public static int getNetworkClass(int networkType) {
682 switch (networkType) {
683 case NETWORK_TYPE_GPRS:
684 case NETWORK_TYPE_EDGE:
685 case NETWORK_TYPE_CDMA:
686 case NETWORK_TYPE_1xRTT:
687 case NETWORK_TYPE_IDEN:
688 return NETWORK_CLASS_2_G;
689 case NETWORK_TYPE_UMTS:
690 case NETWORK_TYPE_EVDO_0:
691 case NETWORK_TYPE_EVDO_A:
692 case NETWORK_TYPE_HSDPA:
693 case NETWORK_TYPE_HSUPA:
694 case NETWORK_TYPE_HSPA:
695 case NETWORK_TYPE_EVDO_B:
696 case NETWORK_TYPE_EHRPD:
697 case NETWORK_TYPE_HSPAP:
698 return NETWORK_CLASS_3_G;
699 case NETWORK_TYPE_LTE:
700 return NETWORK_CLASS_4_G;
702 return NETWORK_CLASS_UNKNOWN;
707 * Returns a string representation of the radio technology (network type)
708 * currently in use on the device.
709 * @return the name of the radio technology
711 * @hide pending API council review
713 public String getNetworkTypeName() {
714 return getNetworkTypeName(getNetworkType());
718 public static String getNetworkTypeName(int type) {
720 case NETWORK_TYPE_GPRS:
722 case NETWORK_TYPE_EDGE:
724 case NETWORK_TYPE_UMTS:
726 case NETWORK_TYPE_HSDPA:
728 case NETWORK_TYPE_HSUPA:
730 case NETWORK_TYPE_HSPA:
732 case NETWORK_TYPE_CDMA:
734 case NETWORK_TYPE_EVDO_0:
735 return "CDMA - EvDo rev. 0";
736 case NETWORK_TYPE_EVDO_A:
737 return "CDMA - EvDo rev. A";
738 case NETWORK_TYPE_EVDO_B:
739 return "CDMA - EvDo rev. B";
740 case NETWORK_TYPE_1xRTT:
741 return "CDMA - 1xRTT";
742 case NETWORK_TYPE_LTE:
744 case NETWORK_TYPE_EHRPD:
745 return "CDMA - eHRPD";
746 case NETWORK_TYPE_IDEN:
748 case NETWORK_TYPE_HSPAP:
761 /** SIM card state: Unknown. Signifies that the SIM is in transition
762 * between states. For example, when the user inputs the SIM pin
763 * under PIN_REQUIRED state, a query for sim status returns
764 * this state before turning to SIM_STATE_READY. */
765 public static final int SIM_STATE_UNKNOWN = 0;
766 /** SIM card state: no SIM card is available in the device */
767 public static final int SIM_STATE_ABSENT = 1;
768 /** SIM card state: Locked: requires the user's SIM PIN to unlock */
769 public static final int SIM_STATE_PIN_REQUIRED = 2;
770 /** SIM card state: Locked: requires the user's SIM PUK to unlock */
771 public static final int SIM_STATE_PUK_REQUIRED = 3;
772 /** SIM card state: Locked: requries a network PIN to unlock */
773 public static final int SIM_STATE_NETWORK_LOCKED = 4;
774 /** SIM card state: Ready */
775 public static final int SIM_STATE_READY = 5;
778 * @return true if a ICC card is present
780 public boolean hasIccCard() {
782 return getITelephony().hasIccCard();
783 } catch (RemoteException ex) {
784 // Assume no ICC card if remote exception which shouldn't happen
786 } catch (NullPointerException ex) {
787 // This could happen before phone restarts due to crashing
793 * Returns a constant indicating the state of the
796 * @see #SIM_STATE_UNKNOWN
797 * @see #SIM_STATE_ABSENT
798 * @see #SIM_STATE_PIN_REQUIRED
799 * @see #SIM_STATE_PUK_REQUIRED
800 * @see #SIM_STATE_NETWORK_LOCKED
801 * @see #SIM_STATE_READY
803 public int getSimState() {
804 String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE);
805 if ("ABSENT".equals(prop)) {
806 return SIM_STATE_ABSENT;
808 else if ("PIN_REQUIRED".equals(prop)) {
809 return SIM_STATE_PIN_REQUIRED;
811 else if ("PUK_REQUIRED".equals(prop)) {
812 return SIM_STATE_PUK_REQUIRED;
814 else if ("NETWORK_LOCKED".equals(prop)) {
815 return SIM_STATE_NETWORK_LOCKED;
817 else if ("READY".equals(prop)) {
818 return SIM_STATE_READY;
821 return SIM_STATE_UNKNOWN;
826 * Returns the MCC+MNC (mobile country code + mobile network code) of the
827 * provider of the SIM. 5 or 6 decimal digits.
829 * Availability: SIM state must be {@link #SIM_STATE_READY}
833 public String getSimOperator() {
834 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC);
838 * Returns the Service Provider Name (SPN).
840 * Availability: SIM state must be {@link #SIM_STATE_READY}
844 public String getSimOperatorName() {
845 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA);
849 * Returns the ISO country code equivalent for the SIM provider's country code.
851 public String getSimCountryIso() {
852 return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY);
856 * Returns the serial number of the SIM, if applicable. Return null if it is
859 * Requires Permission:
860 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
862 public String getSimSerialNumber() {
864 return getSubscriberInfo().getIccSerialNumber();
865 } catch (RemoteException ex) {
867 } catch (NullPointerException ex) {
868 // This could happen before phone restarts due to crashing
874 * Return if the current radio is LTE on CDMA. This
875 * is a tri-state return value as for a period of time
876 * the mode may be unknown.
878 * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE}
879 * or {@link PhoneConstants#LTE_ON_CDMA_TRUE}
883 public int getLteOnCdmaMode() {
885 return getITelephony().getLteOnCdmaMode();
886 } catch (RemoteException ex) {
887 // Assume no ICC card if remote exception which shouldn't happen
888 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
889 } catch (NullPointerException ex) {
890 // This could happen before phone restarts due to crashing
891 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
902 * Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
903 * Return null if it is unavailable.
905 * Requires Permission:
906 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
908 public String getSubscriberId() {
910 return getSubscriberInfo().getSubscriberId();
911 } catch (RemoteException ex) {
913 } catch (NullPointerException ex) {
914 // This could happen before phone restarts due to crashing
920 * Returns the Group Identifier Level1 for a GSM phone.
921 * Return null if it is unavailable.
923 * Requires Permission:
924 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
926 public String getGroupIdLevel1() {
928 return getSubscriberInfo().getGroupIdLevel1();
929 } catch (RemoteException ex) {
931 } catch (NullPointerException ex) {
932 // This could happen before phone restarts due to crashing
938 * Returns the phone number string for line 1, for example, the MSISDN
939 * for a GSM phone. Return null if it is unavailable.
941 * Requires Permission:
942 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
944 public String getLine1Number() {
946 return getSubscriberInfo().getLine1Number();
947 } catch (RemoteException ex) {
949 } catch (NullPointerException ex) {
950 // This could happen before phone restarts due to crashing
956 * Returns the alphabetic identifier associated with the line 1 number.
957 * Return null if it is unavailable.
959 * Requires Permission:
960 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
962 * nobody seems to call this.
964 public String getLine1AlphaTag() {
966 return getSubscriberInfo().getLine1AlphaTag();
967 } catch (RemoteException ex) {
969 } catch (NullPointerException ex) {
970 // This could happen before phone restarts due to crashing
976 * Returns the MSISDN string.
977 * for a GSM phone. Return null if it is unavailable.
979 * Requires Permission:
980 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
984 public String getMsisdn() {
986 return getSubscriberInfo().getMsisdn();
987 } catch (RemoteException ex) {
989 } catch (NullPointerException ex) {
990 // This could happen before phone restarts due to crashing
996 * Returns the voice mail number. Return null if it is unavailable.
998 * Requires Permission:
999 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1001 public String getVoiceMailNumber() {
1003 return getSubscriberInfo().getVoiceMailNumber();
1004 } catch (RemoteException ex) {
1006 } catch (NullPointerException ex) {
1007 // This could happen before phone restarts due to crashing
1013 * Returns the complete voice mail number. Return null if it is unavailable.
1015 * Requires Permission:
1016 * {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED}
1020 public String getCompleteVoiceMailNumber() {
1022 return getSubscriberInfo().getCompleteVoiceMailNumber();
1023 } catch (RemoteException ex) {
1025 } catch (NullPointerException ex) {
1026 // This could happen before phone restarts due to crashing
1032 * Returns the voice mail count. Return 0 if unavailable.
1034 * Requires Permission:
1035 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1038 public int getVoiceMessageCount() {
1040 return getITelephony().getVoiceMessageCount();
1041 } catch (RemoteException ex) {
1043 } catch (NullPointerException ex) {
1044 // This could happen before phone restarts due to crashing
1050 * Retrieves the alphabetic identifier associated with the voice
1053 * Requires Permission:
1054 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1056 public String getVoiceMailAlphaTag() {
1058 return getSubscriberInfo().getVoiceMailAlphaTag();
1059 } catch (RemoteException ex) {
1061 } catch (NullPointerException ex) {
1062 // This could happen before phone restarts due to crashing
1068 * Returns the IMS private user identity (IMPI) that was loaded from the ISIM.
1069 * @return the IMPI, or null if not present or not loaded
1072 public String getIsimImpi() {
1074 return getSubscriberInfo().getIsimImpi();
1075 } catch (RemoteException ex) {
1077 } catch (NullPointerException ex) {
1078 // This could happen before phone restarts due to crashing
1084 * Returns the IMS home network domain name that was loaded from the ISIM.
1085 * @return the IMS domain name, or null if not present or not loaded
1088 public String getIsimDomain() {
1090 return getSubscriberInfo().getIsimDomain();
1091 } catch (RemoteException ex) {
1093 } catch (NullPointerException ex) {
1094 // This could happen before phone restarts due to crashing
1100 * Returns the IMS public user identities (IMPU) that were loaded from the ISIM.
1101 * @return an array of IMPU strings, with one IMPU per string, or null if
1102 * not present or not loaded
1105 public String[] getIsimImpu() {
1107 return getSubscriberInfo().getIsimImpu();
1108 } catch (RemoteException ex) {
1110 } catch (NullPointerException ex) {
1111 // This could happen before phone restarts due to crashing
1116 private IPhoneSubInfo getSubscriberInfo() {
1117 // get it each time because that process crashes a lot
1118 return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
1122 /** Device call state: No activity. */
1123 public static final int CALL_STATE_IDLE = 0;
1124 /** Device call state: Ringing. A new call arrived and is
1125 * ringing or waiting. In the latter case, another call is
1126 * already active. */
1127 public static final int CALL_STATE_RINGING = 1;
1128 /** Device call state: Off-hook. At least one call exists
1129 * that is dialing, active, or on hold, and no calls are ringing
1131 public static final int CALL_STATE_OFFHOOK = 2;
1134 * Returns a constant indicating the call state (cellular) on the device.
1136 public int getCallState() {
1138 return getITelephony().getCallState();
1139 } catch (RemoteException ex) {
1140 // the phone process is restarting.
1141 return CALL_STATE_IDLE;
1142 } catch (NullPointerException ex) {
1143 // the phone process is restarting.
1144 return CALL_STATE_IDLE;
1148 /** Data connection activity: No traffic. */
1149 public static final int DATA_ACTIVITY_NONE = 0x00000000;
1150 /** Data connection activity: Currently receiving IP PPP traffic. */
1151 public static final int DATA_ACTIVITY_IN = 0x00000001;
1152 /** Data connection activity: Currently sending IP PPP traffic. */
1153 public static final int DATA_ACTIVITY_OUT = 0x00000002;
1154 /** Data connection activity: Currently both sending and receiving
1155 * IP PPP traffic. */
1156 public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT;
1158 * Data connection is active, but physical link is down
1160 public static final int DATA_ACTIVITY_DORMANT = 0x00000004;
1163 * Returns a constant indicating the type of activity on a data connection
1166 * @see #DATA_ACTIVITY_NONE
1167 * @see #DATA_ACTIVITY_IN
1168 * @see #DATA_ACTIVITY_OUT
1169 * @see #DATA_ACTIVITY_INOUT
1170 * @see #DATA_ACTIVITY_DORMANT
1172 public int getDataActivity() {
1174 return getITelephony().getDataActivity();
1175 } catch (RemoteException ex) {
1176 // the phone process is restarting.
1177 return DATA_ACTIVITY_NONE;
1178 } catch (NullPointerException ex) {
1179 // the phone process is restarting.
1180 return DATA_ACTIVITY_NONE;
1184 /** Data connection state: Unknown. Used before we know the state.
1187 public static final int DATA_UNKNOWN = -1;
1188 /** Data connection state: Disconnected. IP traffic not available. */
1189 public static final int DATA_DISCONNECTED = 0;
1190 /** Data connection state: Currently setting up a data connection. */
1191 public static final int DATA_CONNECTING = 1;
1192 /** Data connection state: Connected. IP traffic should be available. */
1193 public static final int DATA_CONNECTED = 2;
1194 /** Data connection state: Suspended. The connection is up, but IP
1195 * traffic is temporarily unavailable. For example, in a 2G network,
1196 * data activity may be suspended when a voice call arrives. */
1197 public static final int DATA_SUSPENDED = 3;
1200 * Returns a constant indicating the current data connection state
1203 * @see #DATA_DISCONNECTED
1204 * @see #DATA_CONNECTING
1205 * @see #DATA_CONNECTED
1206 * @see #DATA_SUSPENDED
1208 public int getDataState() {
1210 return getITelephony().getDataState();
1211 } catch (RemoteException ex) {
1212 // the phone process is restarting.
1213 return DATA_DISCONNECTED;
1214 } catch (NullPointerException ex) {
1215 return DATA_DISCONNECTED;
1219 private ITelephony getITelephony() {
1220 return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
1225 // PhoneStateListener
1230 * Registers a listener object to receive notification of changes
1231 * in specified telephony states.
1233 * To register a listener, pass a {@link PhoneStateListener}
1234 * and specify at least one telephony state of interest in
1235 * the events argument.
1237 * At registration, and when a specified telephony state
1238 * changes, the telephony manager invokes the appropriate
1239 * callback method on the listener object and passes the
1240 * current (udpated) values.
1242 * To unregister a listener, pass the listener object and set the
1243 * events argument to
1244 * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0).
1246 * @param listener The {@link PhoneStateListener} object to register
1248 * @param events The telephony state(s) of interest to the listener,
1249 * as a bitwise-OR combination of {@link PhoneStateListener}
1252 public void listen(PhoneStateListener listener, int events) {
1253 String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>";
1255 Boolean notifyNow = true;
1256 sRegistry.listen(pkgForDebug, listener.callback, events, notifyNow);
1257 } catch (RemoteException ex) {
1258 // system process dead
1259 } catch (NullPointerException ex) {
1260 // system process dead
1265 * Returns the CDMA ERI icon index to display
1269 public int getCdmaEriIconIndex() {
1271 return getITelephony().getCdmaEriIconIndex();
1272 } catch (RemoteException ex) {
1273 // the phone process is restarting.
1275 } catch (NullPointerException ex) {
1281 * Returns the CDMA ERI icon mode,
1287 public int getCdmaEriIconMode() {
1289 return getITelephony().getCdmaEriIconMode();
1290 } catch (RemoteException ex) {
1291 // the phone process is restarting.
1293 } catch (NullPointerException ex) {
1299 * Returns the CDMA ERI text,
1303 public String getCdmaEriText() {
1305 return getITelephony().getCdmaEriText();
1306 } catch (RemoteException ex) {
1307 // the phone process is restarting.
1309 } catch (NullPointerException ex) {
1315 * @return true if the current device is "voice capable".
1317 * "Voice capable" means that this device supports circuit-switched
1318 * (i.e. voice) phone calls over the telephony network, and is allowed
1319 * to display the in-call UI while a cellular voice call is active.
1320 * This will be false on "data only" devices which can't make voice
1321 * calls and don't support any in-call UI.
1323 * Note: the meaning of this flag is subtly different from the
1324 * PackageManager.FEATURE_TELEPHONY system feature, which is available
1325 * on any device with a telephony radio, even if the device is
1328 * @hide pending API review
1330 public boolean isVoiceCapable() {
1331 if (mContext == null) return true;
1332 return mContext.getResources().getBoolean(
1333 com.android.internal.R.bool.config_voice_capable);
1337 * @return true if the current device supports sms service.
1339 * If true, this means that the device supports both sending and
1340 * receiving sms via the telephony network.
1342 * Note: Voicemail waiting sms, cell broadcasting sms, and MMS are
1343 * disabled when device doesn't support sms.
1345 * @hide pending API review
1347 public boolean isSmsCapable() {
1348 if (mContext == null) return true;
1349 return mContext.getResources().getBoolean(
1350 com.android.internal.R.bool.config_sms_capable);
1354 * Returns all observed cell information from all radios on the
1355 * device including the primary and neighboring cells. This does
1356 * not cause or change the rate of PhoneStateListner#onCellInfoChanged.
1358 * The list can include one or more of {@link android.telephony.CellInfoGsm CellInfoGsm},
1359 * {@link android.telephony.CellInfoCdma CellInfoCdma},
1360 * {@link android.telephony.CellInfoLte CellInfoLte} and
1361 * {@link android.telephony.CellInfoWcdma CellInfoCdma} in any combination.
1362 * Specifically on devices with multiple radios it is typical to see instances of
1363 * one or more of any these in the list. In addition 0, 1 or more CellInfo
1364 * objects may return isRegistered() true.
1366 * This is preferred over using getCellLocation although for older
1367 * devices this may return null in which case getCellLocation should
1370 * @return List of CellInfo or null if info unavailable.
1372 * <p>Requires Permission: {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}
1374 public List<CellInfo> getAllCellInfo() {
1376 return getITelephony().getAllCellInfo();
1377 } catch (RemoteException ex) {
1379 } catch (NullPointerException ex) {
1385 * Sets the minimum time in milli-seconds between {@link PhoneStateListener#onCellInfoChanged
1386 * PhoneStateListener.onCellInfoChanged} will be invoked.
1388 * The default, 0, means invoke onCellInfoChanged when any of the reported
1389 * information changes. Setting the value to INT_MAX(0x7fffffff) means never issue
1390 * A onCellInfoChanged.
1392 * @param rateInMillis the rate
1396 public void setCellInfoListRate(int rateInMillis) {
1398 getITelephony().setCellInfoListRate(rateInMillis);
1399 } catch (RemoteException ex) {
1400 } catch (NullPointerException ex) {
1405 * Returns the MMS user agent.
1407 public String getMmsUserAgent() {
1408 if (mContext == null) return null;
1409 return mContext.getResources().getString(
1410 com.android.internal.R.string.config_mms_user_agent);
1414 * Returns the MMS user agent profile URL.
1416 public String getMmsUAProfUrl() {
1417 if (mContext == null) return null;
1418 return mContext.getResources().getString(
1419 com.android.internal.R.string.config_mms_user_agent_profile_url);