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