TelephonyManager.java revision 3fa5f373467054c673c5fd6296adba4c6de41520
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            sContext = context;
65
66            sRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
67                    "telephony.registry"));
68        } else {
69            Log.e(TAG, "Hidden constructor called more than once per process!");
70            Log.e(TAG, "Original: " + sContext.getPackageName() + ", new: " +
71                    context.getPackageName());
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
88    //
89    // Broadcast Intent actions
90    //
91
92    /**
93     * Broadcast intent action indicating that the call state (cellular)
94     * on the device has changed.
95     *
96     * <p>
97     * The {@link #EXTRA_STATE} extra indicates the new call state.
98     * If the new state is RINGING, a second extra
99     * {@link #EXTRA_INCOMING_NUMBER} provides the incoming phone number as
100     * a String.
101     *
102     * <p class="note">
103     * Requires the READ_PHONE_STATE permission.
104     *
105     * <p class="note">
106     * This was a {@link android.content.Context#sendStickyBroadcast sticky}
107     * broadcast in version 1.0, but it is no longer sticky.
108     * Instead, use {@link #getCallState} to synchronously query the current call state.
109     *
110     * @see #EXTRA_STATE
111     * @see #EXTRA_INCOMING_NUMBER
112     * @see #getCallState
113     */
114    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
115    public static final String ACTION_PHONE_STATE_CHANGED =
116            "android.intent.action.PHONE_STATE";
117
118    /**
119     * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
120     * for a String containing the new call state.
121     *
122     * @see #EXTRA_STATE_IDLE
123     * @see #EXTRA_STATE_RINGING
124     * @see #EXTRA_STATE_OFFHOOK
125     *
126     * <p class="note">
127     * Retrieve with
128     * {@link android.content.Intent#getStringExtra(String)}.
129     */
130    public static final String EXTRA_STATE = Phone.STATE_KEY;
131
132    /**
133     * Value used with {@link #EXTRA_STATE} corresponding to
134     * {@link #CALL_STATE_IDLE}.
135     */
136    public static final String EXTRA_STATE_IDLE = Phone.State.IDLE.toString();
137
138    /**
139     * Value used with {@link #EXTRA_STATE} corresponding to
140     * {@link #CALL_STATE_RINGING}.
141     */
142    public static final String EXTRA_STATE_RINGING = Phone.State.RINGING.toString();
143
144    /**
145     * Value used with {@link #EXTRA_STATE} corresponding to
146     * {@link #CALL_STATE_OFFHOOK}.
147     */
148    public static final String EXTRA_STATE_OFFHOOK = Phone.State.OFFHOOK.toString();
149
150    /**
151     * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
152     * for a String containing the incoming phone number.
153     * Only valid when the new call state is RINGING.
154     *
155     * <p class="note">
156     * Retrieve with
157     * {@link android.content.Intent#getStringExtra(String)}.
158     */
159    public static final String EXTRA_INCOMING_NUMBER = "incoming_number";
160
161
162    //
163    //
164    // Device Info
165    //
166    //
167
168    /**
169     * Returns the software version number for the device, for example,
170     * the IMEI/SV for GSM phones. Return null if the software version is
171     * not available.
172     *
173     * <p>Requires Permission:
174     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
175     */
176    public String getDeviceSoftwareVersion() {
177        try {
178            return getSubscriberInfo().getDeviceSvn();
179        } catch (RemoteException ex) {
180            return null;
181        } catch (NullPointerException ex) {
182            return null;
183        }
184    }
185
186    /**
187     * Returns the unique device ID, for example, the IMEI for GSM and the MEID
188     * or ESN for CDMA phones. Return null if device ID is not available.
189     *
190     * <p>Requires Permission:
191     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
192     */
193    public String getDeviceId() {
194        if (!isVoiceCapable()) {
195            return null;
196        }
197
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 voice mail number. Return null if it is unavailable.
769     * <p>
770     * Requires Permission:
771     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
772     */
773    public String getVoiceMailNumber() {
774        try {
775            return getSubscriberInfo().getVoiceMailNumber();
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 complete voice mail number. Return null if it is unavailable.
786     * <p>
787     * Requires Permission:
788     *   {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED}
789     *
790     * @hide
791     */
792    public String getCompleteVoiceMailNumber() {
793        try {
794            return getSubscriberInfo().getCompleteVoiceMailNumber();
795        } catch (RemoteException ex) {
796            return null;
797        } catch (NullPointerException ex) {
798            // This could happen before phone restarts due to crashing
799            return null;
800        }
801    }
802
803    /**
804     * Returns the voice mail count. Return 0 if unavailable.
805     * <p>
806     * Requires Permission:
807     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
808     * @hide
809     */
810    public int getVoiceMessageCount() {
811        try {
812            return getITelephony().getVoiceMessageCount();
813        } catch (RemoteException ex) {
814            return 0;
815        } catch (NullPointerException ex) {
816            // This could happen before phone restarts due to crashing
817            return 0;
818        }
819    }
820
821    /**
822     * Retrieves the alphabetic identifier associated with the voice
823     * mail number.
824     * <p>
825     * Requires Permission:
826     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
827     */
828    public String getVoiceMailAlphaTag() {
829        try {
830            return getSubscriberInfo().getVoiceMailAlphaTag();
831        } catch (RemoteException ex) {
832            return null;
833        } catch (NullPointerException ex) {
834            // This could happen before phone restarts due to crashing
835            return null;
836        }
837    }
838
839    private IPhoneSubInfo getSubscriberInfo() {
840        // get it each time because that process crashes a lot
841        return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
842    }
843
844
845    /** Device call state: No activity. */
846    public static final int CALL_STATE_IDLE = 0;
847    /** Device call state: Ringing. A new call arrived and is
848     *  ringing or waiting. In the latter case, another call is
849     *  already active. */
850    public static final int CALL_STATE_RINGING = 1;
851    /** Device call state: Off-hook. At least one call exists
852      * that is dialing, active, or on hold, and no calls are ringing
853      * or waiting. */
854    public static final int CALL_STATE_OFFHOOK = 2;
855
856    /**
857     * Returns a constant indicating the call state (cellular) on the device.
858     */
859    public int getCallState() {
860        try {
861            return getITelephony().getCallState();
862        } catch (RemoteException ex) {
863            // the phone process is restarting.
864            return CALL_STATE_IDLE;
865        } catch (NullPointerException ex) {
866          // the phone process is restarting.
867          return CALL_STATE_IDLE;
868      }
869    }
870
871    /** Data connection activity: No traffic. */
872    public static final int DATA_ACTIVITY_NONE = 0x00000000;
873    /** Data connection activity: Currently receiving IP PPP traffic. */
874    public static final int DATA_ACTIVITY_IN = 0x00000001;
875    /** Data connection activity: Currently sending IP PPP traffic. */
876    public static final int DATA_ACTIVITY_OUT = 0x00000002;
877    /** Data connection activity: Currently both sending and receiving
878     *  IP PPP traffic. */
879    public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT;
880    /**
881     * Data connection is active, but physical link is down
882     */
883    public static final int DATA_ACTIVITY_DORMANT = 0x00000004;
884
885    /**
886     * Returns a constant indicating the type of activity on a data connection
887     * (cellular).
888     *
889     * @see #DATA_ACTIVITY_NONE
890     * @see #DATA_ACTIVITY_IN
891     * @see #DATA_ACTIVITY_OUT
892     * @see #DATA_ACTIVITY_INOUT
893     * @see #DATA_ACTIVITY_DORMANT
894     */
895    public int getDataActivity() {
896        try {
897            return getITelephony().getDataActivity();
898        } catch (RemoteException ex) {
899            // the phone process is restarting.
900            return DATA_ACTIVITY_NONE;
901        } catch (NullPointerException ex) {
902          // the phone process is restarting.
903          return DATA_ACTIVITY_NONE;
904      }
905    }
906
907    /** Data connection state: Unknown.  Used before we know the state.
908     * @hide
909     */
910    public static final int DATA_UNKNOWN        = -1;
911    /** Data connection state: Disconnected. IP traffic not available. */
912    public static final int DATA_DISCONNECTED   = 0;
913    /** Data connection state: Currently setting up a data connection. */
914    public static final int DATA_CONNECTING     = 1;
915    /** Data connection state: Connected. IP traffic should be available. */
916    public static final int DATA_CONNECTED      = 2;
917    /** Data connection state: Suspended. The connection is up, but IP
918     * traffic is temporarily unavailable. For example, in a 2G network,
919     * data activity may be suspended when a voice call arrives. */
920    public static final int DATA_SUSPENDED      = 3;
921
922    /**
923     * Returns a constant indicating the current data connection state
924     * (cellular).
925     *
926     * @see #DATA_DISCONNECTED
927     * @see #DATA_CONNECTING
928     * @see #DATA_CONNECTED
929     * @see #DATA_SUSPENDED
930     */
931    public int getDataState() {
932        try {
933            return getITelephony().getDataState();
934        } catch (RemoteException ex) {
935            // the phone process is restarting.
936            return DATA_DISCONNECTED;
937        } catch (NullPointerException ex) {
938            return DATA_DISCONNECTED;
939        }
940    }
941
942    private ITelephony getITelephony() {
943        return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
944    }
945
946    //
947    //
948    // PhoneStateListener
949    //
950    //
951
952    /**
953     * Registers a listener object to receive notification of changes
954     * in specified telephony states.
955     * <p>
956     * To register a listener, pass a {@link PhoneStateListener}
957     * and specify at least one telephony state of interest in
958     * the events argument.
959     *
960     * At registration, and when a specified telephony state
961     * changes, the telephony manager invokes the appropriate
962     * callback method on the listener object and passes the
963     * current (udpated) values.
964     * <p>
965     * To unregister a listener, pass the listener object and set the
966     * events argument to
967     * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0).
968     *
969     * @param listener The {@link PhoneStateListener} object to register
970     *                 (or unregister)
971     * @param events The telephony state(s) of interest to the listener,
972     *               as a bitwise-OR combination of {@link PhoneStateListener}
973     *               LISTEN_ flags.
974     */
975    public void listen(PhoneStateListener listener, int events) {
976        String pkgForDebug = sContext != null ? sContext.getPackageName() : "<unknown>";
977        try {
978            Boolean notifyNow = (getITelephony() != null);
979            sRegistry.listen(pkgForDebug, listener.callback, events, notifyNow);
980        } catch (RemoteException ex) {
981            // system process dead
982        } catch (NullPointerException ex) {
983            // system process dead
984        }
985    }
986
987    /**
988     * Returns the CDMA ERI icon index to display
989     *
990     * @hide
991     */
992    public int getCdmaEriIconIndex() {
993        try {
994            return getITelephony().getCdmaEriIconIndex();
995        } catch (RemoteException ex) {
996            // the phone process is restarting.
997            return -1;
998        } catch (NullPointerException ex) {
999            return -1;
1000        }
1001    }
1002
1003    /**
1004     * Returns the CDMA ERI icon mode,
1005     * 0 - ON
1006     * 1 - FLASHING
1007     *
1008     * @hide
1009     */
1010    public int getCdmaEriIconMode() {
1011        try {
1012            return getITelephony().getCdmaEriIconMode();
1013        } catch (RemoteException ex) {
1014            // the phone process is restarting.
1015            return -1;
1016        } catch (NullPointerException ex) {
1017            return -1;
1018        }
1019    }
1020
1021    /**
1022     * Returns the CDMA ERI text,
1023     *
1024     * @hide
1025     */
1026    public String getCdmaEriText() {
1027        try {
1028            return getITelephony().getCdmaEriText();
1029        } catch (RemoteException ex) {
1030            // the phone process is restarting.
1031            return null;
1032        } catch (NullPointerException ex) {
1033            return null;
1034        }
1035    }
1036
1037    /**
1038     * @return true if the current device is "voice capable".
1039     * <p>
1040     * "Voice capable" means that this device supports circuit-switched
1041     * (i.e. voice) phone calls over the telephony network, and is allowed
1042     * to display the in-call UI while a cellular voice call is active.
1043     * This will be false on "data only" devices which can't make voice
1044     * calls and don't support any in-call UI.
1045     * <p>
1046     * Note: the meaning of this flag is subtly different from the
1047     * PackageManager.FEATURE_TELEPHONY system feature, which is available
1048     * on any device with a telephony radio, even if the device is
1049     * data-only.
1050     *
1051     * @hide pending API review
1052     */
1053    public boolean isVoiceCapable() {
1054        if (sContext == null) return true;
1055        return sContext.getResources().getBoolean(
1056                com.android.internal.R.bool.config_voice_capable);
1057    }
1058
1059    /**
1060     * @return true if the current device supports sms service.
1061     * <p>
1062     * If true, this means that the device supports both sending and
1063     * receiving sms via the telephony network.
1064     * <p>
1065     * Note: Voicemail waiting sms, cell broadcasting sms, and MMS are
1066     *       disabled when device doesn't support sms.
1067     *
1068     * @hide pending API review
1069     */
1070    public boolean isSmsCapable() {
1071        if (sContext == null) return true;
1072        return sContext.getResources().getBoolean(
1073                com.android.internal.R.bool.config_sms_capable);
1074    }
1075}
1076