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