TelephonyManager.java revision d2a458750e5a3d490af09cecb5c28370baf0a913
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        try {
195            return getSubscriberInfo().getDeviceId();
196        } catch (RemoteException ex) {
197            return null;
198        } catch (NullPointerException ex) {
199            return null;
200        }
201    }
202
203    /**
204     * Returns the current location of the device.
205     * Return null if current location is not available.
206     *
207     * <p>Requires Permission:
208     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} or
209     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION}.
210     */
211    public CellLocation getCellLocation() {
212        try {
213            Bundle bundle = getITelephony().getCellLocation();
214            CellLocation cl = CellLocation.newFromBundle(bundle);
215            if (cl.isEmpty())
216                return null;
217            return cl;
218        } catch (RemoteException ex) {
219            return null;
220        } catch (NullPointerException ex) {
221            return null;
222        }
223    }
224
225    /**
226     * Enables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
227     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
228     *
229     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
230     * CONTROL_LOCATION_UPDATES}
231     *
232     * @hide
233     */
234    public void enableLocationUpdates() {
235        try {
236            getITelephony().enableLocationUpdates();
237        } catch (RemoteException ex) {
238        } catch (NullPointerException ex) {
239        }
240    }
241
242    /**
243     * Disables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
244     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
245     *
246     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
247     * CONTROL_LOCATION_UPDATES}
248     *
249     * @hide
250     */
251    public void disableLocationUpdates() {
252        try {
253            getITelephony().disableLocationUpdates();
254        } catch (RemoteException ex) {
255        } catch (NullPointerException ex) {
256        }
257    }
258
259    /**
260     * Returns the neighboring cell information of the device.
261     *
262     * @return List of NeighboringCellInfo or null if info unavailable.
263     *
264     * <p>Requires Permission:
265     * (@link android.Manifest.permission#ACCESS_COARSE_UPDATES}
266     */
267    public List<NeighboringCellInfo> getNeighboringCellInfo() {
268        try {
269            return getITelephony().getNeighboringCellInfo();
270        } catch (RemoteException ex) {
271            return null;
272        } catch (NullPointerException ex) {
273            return null;
274        }
275    }
276
277    /** No phone radio. */
278    public static final int PHONE_TYPE_NONE = Phone.PHONE_TYPE_NONE;
279    /** Phone radio is GSM. */
280    public static final int PHONE_TYPE_GSM = Phone.PHONE_TYPE_GSM;
281    /** Phone radio is CDMA. */
282    public static final int PHONE_TYPE_CDMA = Phone.PHONE_TYPE_CDMA;
283    /** Phone is via SIP. */
284    public static final int PHONE_TYPE_SIP = Phone.PHONE_TYPE_SIP;
285
286    /**
287     * Returns the current phone type.
288     * TODO: This is a last minute change and hence hidden.
289     *
290     * @see #PHONE_TYPE_NONE
291     * @see #PHONE_TYPE_GSM
292     * @see #PHONE_TYPE_CDMA
293     * @see #PHONE_TYPE_SIP
294     *
295     * {@hide}
296     */
297    public int getCurrentPhoneType() {
298        try{
299            ITelephony telephony = getITelephony();
300            if (telephony != null) {
301                return telephony.getActivePhoneType();
302            } else {
303                // This can happen when the ITelephony interface is not up yet.
304                return getPhoneTypeFromProperty();
305            }
306        } catch (RemoteException ex) {
307            // This shouldn't happen in the normal case, as a backup we
308            // read from the system property.
309            return getPhoneTypeFromProperty();
310        } catch (NullPointerException ex) {
311            // This shouldn't happen in the normal case, as a backup we
312            // read from the system property.
313            return getPhoneTypeFromProperty();
314        }
315    }
316
317    /**
318     * Returns a constant indicating the device phone type.  This
319     * indicates the type of radio used to transmit voice calls.
320     *
321     * @see #PHONE_TYPE_NONE
322     * @see #PHONE_TYPE_GSM
323     * @see #PHONE_TYPE_CDMA
324     * @see #PHONE_TYPE_SIP
325     */
326    public int getPhoneType() {
327        if (!isVoiceCapable()) {
328            return PHONE_TYPE_NONE;
329        }
330        return getCurrentPhoneType();
331    }
332
333    private int getPhoneTypeFromProperty() {
334        int type =
335            SystemProperties.getInt(TelephonyProperties.CURRENT_ACTIVE_PHONE,
336                    getPhoneTypeFromNetworkType());
337        return type;
338    }
339
340    private int getPhoneTypeFromNetworkType() {
341        // When the system property CURRENT_ACTIVE_PHONE, has not been set,
342        // use the system property for default network type.
343        // This is a fail safe, and can only happen at first boot.
344        int mode = SystemProperties.getInt("ro.telephony.default_network", -1);
345        if (mode == -1)
346            return PHONE_TYPE_NONE;
347        return PhoneFactory.getPhoneType(mode);
348    }
349    //
350    //
351    // Current Network
352    //
353    //
354
355    /**
356     * Returns the alphabetic name of current registered operator.
357     * <p>
358     * Availability: Only when user is registered to a network. Result may be
359     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
360     * on a CDMA network).
361     */
362    public String getNetworkOperatorName() {
363        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ALPHA);
364    }
365
366    /**
367     * Returns the numeric name (MCC+MNC) of current registered operator.
368     * <p>
369     * Availability: Only when user is registered to a network. Result may be
370     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
371     * on a CDMA network).
372     */
373    public String getNetworkOperator() {
374        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_NUMERIC);
375    }
376
377    /**
378     * Returns true if the device is considered roaming on the current
379     * network, for GSM purposes.
380     * <p>
381     * Availability: Only when user registered to a network.
382     */
383    public boolean isNetworkRoaming() {
384        return "true".equals(SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING));
385    }
386
387    /**
388     * Returns the ISO country code equivalent of the current registered
389     * operator's MCC (Mobile Country Code).
390     * <p>
391     * Availability: Only when user is registered to a network. Result may be
392     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
393     * on a CDMA network).
394     */
395    public String getNetworkCountryIso() {
396        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY);
397    }
398
399    /** Network type is unknown */
400    public static final int NETWORK_TYPE_UNKNOWN = 0;
401    /** Current network is GPRS */
402    public static final int NETWORK_TYPE_GPRS = 1;
403    /** Current network is EDGE */
404    public static final int NETWORK_TYPE_EDGE = 2;
405    /** Current network is UMTS */
406    public static final int NETWORK_TYPE_UMTS = 3;
407    /** Current network is CDMA: Either IS95A or IS95B*/
408    public static final int NETWORK_TYPE_CDMA = 4;
409    /** Current network is EVDO revision 0*/
410    public static final int NETWORK_TYPE_EVDO_0 = 5;
411    /** Current network is EVDO revision A*/
412    public static final int NETWORK_TYPE_EVDO_A = 6;
413    /** Current network is 1xRTT*/
414    public static final int NETWORK_TYPE_1xRTT = 7;
415    /** Current network is HSDPA */
416    public static final int NETWORK_TYPE_HSDPA = 8;
417    /** Current network is HSUPA */
418    public static final int NETWORK_TYPE_HSUPA = 9;
419    /** Current network is HSPA */
420    public static final int NETWORK_TYPE_HSPA = 10;
421    /** Current network is iDen */
422    public static final int NETWORK_TYPE_IDEN = 11;
423    /** Current network is EVDO revision B*/
424    public static final int NETWORK_TYPE_EVDO_B = 12;
425    /** Current network is LTE */
426    public static final int NETWORK_TYPE_LTE = 13;
427    /** Current network is eHRPD */
428    public static final int NETWORK_TYPE_EHRPD = 14;
429    /** Current network is HSPA+
430     * @hide */
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     */
454    public int getNetworkType() {
455        try{
456            ITelephony telephony = getITelephony();
457            if (telephony != null) {
458                return telephony.getNetworkType();
459            } else {
460                // This can happen when the ITelephony interface is not up yet.
461                return NETWORK_TYPE_UNKNOWN;
462            }
463        } catch(RemoteException ex) {
464            // This shouldn't happen in the normal case
465            return NETWORK_TYPE_UNKNOWN;
466        } catch (NullPointerException ex) {
467            // This could happen before phone restarts due to crashing
468            return NETWORK_TYPE_UNKNOWN;
469        }
470    }
471
472    /** Unknown network class. {@hide} */
473    public static final int NETWORK_CLASS_UNKNOWN = 0;
474    /** Class of broadly defined "2G" networks. {@hide} */
475    public static final int NETWORK_CLASS_2_G = 1;
476    /** Class of broadly defined "3G" networks. {@hide} */
477    public static final int NETWORK_CLASS_3_G = 2;
478    /** Class of broadly defined "4G" networks. {@hide} */
479    public static final int NETWORK_CLASS_4_G = 3;
480
481    /**
482     * Return general class of network type, such as "3G" or "4G". In cases
483     * where classification is contentious, this method is conservative.
484     *
485     * @hide
486     */
487    public static int getNetworkClass(int networkType) {
488        switch (networkType) {
489            case NETWORK_TYPE_GPRS:
490            case NETWORK_TYPE_EDGE:
491            case NETWORK_TYPE_CDMA:
492            case NETWORK_TYPE_1xRTT:
493            case NETWORK_TYPE_IDEN:
494                return NETWORK_CLASS_2_G;
495            case NETWORK_TYPE_UMTS:
496            case NETWORK_TYPE_EVDO_0:
497            case NETWORK_TYPE_EVDO_A:
498            case NETWORK_TYPE_HSDPA:
499            case NETWORK_TYPE_HSUPA:
500            case NETWORK_TYPE_HSPA:
501            case NETWORK_TYPE_EVDO_B:
502            case NETWORK_TYPE_EHRPD:
503            case NETWORK_TYPE_HSPAP:
504                return NETWORK_CLASS_3_G;
505            case NETWORK_TYPE_LTE:
506                return NETWORK_CLASS_4_G;
507            default:
508                return NETWORK_CLASS_UNKNOWN;
509        }
510    }
511
512    /**
513     * Returns a string representation of the radio technology (network type)
514     * currently in use on the device.
515     * @return the name of the radio technology
516     *
517     * @hide pending API council review
518     */
519    public String getNetworkTypeName() {
520        return getNetworkTypeName(getNetworkType());
521    }
522
523    /** {@hide} */
524    public static String getNetworkTypeName(int type) {
525        switch (type) {
526            case NETWORK_TYPE_GPRS:
527                return "GPRS";
528            case NETWORK_TYPE_EDGE:
529                return "EDGE";
530            case NETWORK_TYPE_UMTS:
531                return "UMTS";
532            case NETWORK_TYPE_HSDPA:
533                return "HSDPA";
534            case NETWORK_TYPE_HSUPA:
535                return "HSUPA";
536            case NETWORK_TYPE_HSPA:
537                return "HSPA";
538            case NETWORK_TYPE_CDMA:
539                return "CDMA";
540            case NETWORK_TYPE_EVDO_0:
541                return "CDMA - EvDo rev. 0";
542            case NETWORK_TYPE_EVDO_A:
543                return "CDMA - EvDo rev. A";
544            case NETWORK_TYPE_EVDO_B:
545                return "CDMA - EvDo rev. B";
546            case NETWORK_TYPE_1xRTT:
547                return "CDMA - 1xRTT";
548            case NETWORK_TYPE_LTE:
549                return "LTE";
550            case NETWORK_TYPE_EHRPD:
551                return "CDMA - eHRPD";
552            case NETWORK_TYPE_IDEN:
553                return "iDEN";
554            default:
555                return "UNKNOWN";
556        }
557    }
558
559    //
560    //
561    // SIM Card
562    //
563    //
564
565    /** SIM card state: Unknown. Signifies that the SIM is in transition
566     *  between states. For example, when the user inputs the SIM pin
567     *  under PIN_REQUIRED state, a query for sim status returns
568     *  this state before turning to SIM_STATE_READY. */
569    public static final int SIM_STATE_UNKNOWN = 0;
570    /** SIM card state: no SIM card is available in the device */
571    public static final int SIM_STATE_ABSENT = 1;
572    /** SIM card state: Locked: requires the user's SIM PIN to unlock */
573    public static final int SIM_STATE_PIN_REQUIRED = 2;
574    /** SIM card state: Locked: requires the user's SIM PUK to unlock */
575    public static final int SIM_STATE_PUK_REQUIRED = 3;
576    /** SIM card state: Locked: requries a network PIN to unlock */
577    public static final int SIM_STATE_NETWORK_LOCKED = 4;
578    /** SIM card state: Ready */
579    public static final int SIM_STATE_READY = 5;
580
581    /**
582     * @return true if a ICC card is present
583     */
584    public boolean hasIccCard() {
585        try {
586            return getITelephony().hasIccCard();
587        } catch (RemoteException ex) {
588            // Assume no ICC card if remote exception which shouldn't happen
589            return false;
590        } catch (NullPointerException ex) {
591            // This could happen before phone restarts due to crashing
592            return false;
593        }
594    }
595
596    /**
597     * Returns a constant indicating the state of the
598     * device SIM card.
599     *
600     * @see #SIM_STATE_UNKNOWN
601     * @see #SIM_STATE_ABSENT
602     * @see #SIM_STATE_PIN_REQUIRED
603     * @see #SIM_STATE_PUK_REQUIRED
604     * @see #SIM_STATE_NETWORK_LOCKED
605     * @see #SIM_STATE_READY
606     */
607    public int getSimState() {
608        String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE);
609        if ("ABSENT".equals(prop)) {
610            return SIM_STATE_ABSENT;
611        }
612        else if ("PIN_REQUIRED".equals(prop)) {
613            return SIM_STATE_PIN_REQUIRED;
614        }
615        else if ("PUK_REQUIRED".equals(prop)) {
616            return SIM_STATE_PUK_REQUIRED;
617        }
618        else if ("NETWORK_LOCKED".equals(prop)) {
619            return SIM_STATE_NETWORK_LOCKED;
620        }
621        else if ("READY".equals(prop)) {
622            return SIM_STATE_READY;
623        }
624        else {
625            return SIM_STATE_UNKNOWN;
626        }
627    }
628
629    /**
630     * Returns the MCC+MNC (mobile country code + mobile network code) of the
631     * provider of the SIM. 5 or 6 decimal digits.
632     * <p>
633     * Availability: SIM state must be {@link #SIM_STATE_READY}
634     *
635     * @see #getSimState
636     */
637    public String getSimOperator() {
638        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC);
639    }
640
641    /**
642     * Returns the Service Provider Name (SPN).
643     * <p>
644     * Availability: SIM state must be {@link #SIM_STATE_READY}
645     *
646     * @see #getSimState
647     */
648    public String getSimOperatorName() {
649        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA);
650    }
651
652    /**
653     * Returns the ISO country code equivalent for the SIM provider's country code.
654     */
655    public String getSimCountryIso() {
656        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY);
657    }
658
659    /**
660     * Returns the serial number of the SIM, if applicable. Return null if it is
661     * unavailable.
662     * <p>
663     * Requires Permission:
664     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
665     */
666    public String getSimSerialNumber() {
667        try {
668            return getSubscriberInfo().getIccSerialNumber();
669        } catch (RemoteException ex) {
670            return null;
671        } catch (NullPointerException ex) {
672            // This could happen before phone restarts due to crashing
673            return null;
674        }
675    }
676
677    /**
678     * Return if the current radio is LTE on CDMA. This
679     * is a tri-state return value as for a period of time
680     * the mode may be unknown.
681     *
682     * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
683     * or {@link Phone#LTE_ON_CDMA_TRUE}
684     *
685     * @hide
686     */
687    public int getLteOnCdmaMode() {
688        try {
689            return getITelephony().getLteOnCdmaMode();
690        } catch (RemoteException ex) {
691            // Assume no ICC card if remote exception which shouldn't happen
692            return Phone.LTE_ON_CDMA_UNKNOWN;
693        } catch (NullPointerException ex) {
694            // This could happen before phone restarts due to crashing
695            return Phone.LTE_ON_CDMA_UNKNOWN;
696        }
697    }
698
699    //
700    //
701    // Subscriber Info
702    //
703    //
704
705    /**
706     * Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
707     * Return null if it is unavailable.
708     * <p>
709     * Requires Permission:
710     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
711     */
712    public String getSubscriberId() {
713        try {
714            return getSubscriberInfo().getSubscriberId();
715        } catch (RemoteException ex) {
716            return null;
717        } catch (NullPointerException ex) {
718            // This could happen before phone restarts due to crashing
719            return null;
720        }
721    }
722
723    /**
724     * Returns the phone number string for line 1, for example, the MSISDN
725     * for a GSM phone. Return null if it is unavailable.
726     * <p>
727     * Requires Permission:
728     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
729     */
730    public String getLine1Number() {
731        try {
732            return getSubscriberInfo().getLine1Number();
733        } catch (RemoteException ex) {
734            return null;
735        } catch (NullPointerException ex) {
736            // This could happen before phone restarts due to crashing
737            return null;
738        }
739    }
740
741    /**
742     * Returns the alphabetic identifier associated with the line 1 number.
743     * Return null if it is unavailable.
744     * <p>
745     * Requires Permission:
746     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
747     * @hide
748     * nobody seems to call this.
749     */
750    public String getLine1AlphaTag() {
751        try {
752            return getSubscriberInfo().getLine1AlphaTag();
753        } catch (RemoteException ex) {
754            return null;
755        } catch (NullPointerException ex) {
756            // This could happen before phone restarts due to crashing
757            return null;
758        }
759    }
760
761    /**
762     * Returns the voice mail number. Return null if it is unavailable.
763     * <p>
764     * Requires Permission:
765     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
766     */
767    public String getVoiceMailNumber() {
768        try {
769            return getSubscriberInfo().getVoiceMailNumber();
770        } catch (RemoteException ex) {
771            return null;
772        } catch (NullPointerException ex) {
773            // This could happen before phone restarts due to crashing
774            return null;
775        }
776    }
777
778    /**
779     * Returns the complete voice mail number. Return null if it is unavailable.
780     * <p>
781     * Requires Permission:
782     *   {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED}
783     *
784     * @hide
785     */
786    public String getCompleteVoiceMailNumber() {
787        try {
788            return getSubscriberInfo().getCompleteVoiceMailNumber();
789        } catch (RemoteException ex) {
790            return null;
791        } catch (NullPointerException ex) {
792            // This could happen before phone restarts due to crashing
793            return null;
794        }
795    }
796
797    /**
798     * Returns the voice mail count. Return 0 if unavailable.
799     * <p>
800     * Requires Permission:
801     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
802     * @hide
803     */
804    public int getVoiceMessageCount() {
805        try {
806            return getITelephony().getVoiceMessageCount();
807        } catch (RemoteException ex) {
808            return 0;
809        } catch (NullPointerException ex) {
810            // This could happen before phone restarts due to crashing
811            return 0;
812        }
813    }
814
815    /**
816     * Retrieves the alphabetic identifier associated with the voice
817     * mail number.
818     * <p>
819     * Requires Permission:
820     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
821     */
822    public String getVoiceMailAlphaTag() {
823        try {
824            return getSubscriberInfo().getVoiceMailAlphaTag();
825        } catch (RemoteException ex) {
826            return null;
827        } catch (NullPointerException ex) {
828            // This could happen before phone restarts due to crashing
829            return null;
830        }
831    }
832
833    private IPhoneSubInfo getSubscriberInfo() {
834        // get it each time because that process crashes a lot
835        return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
836    }
837
838
839    /** Device call state: No activity. */
840    public static final int CALL_STATE_IDLE = 0;
841    /** Device call state: Ringing. A new call arrived and is
842     *  ringing or waiting. In the latter case, another call is
843     *  already active. */
844    public static final int CALL_STATE_RINGING = 1;
845    /** Device call state: Off-hook. At least one call exists
846      * that is dialing, active, or on hold, and no calls are ringing
847      * or waiting. */
848    public static final int CALL_STATE_OFFHOOK = 2;
849
850    /**
851     * Returns a constant indicating the call state (cellular) on the device.
852     */
853    public int getCallState() {
854        try {
855            return getITelephony().getCallState();
856        } catch (RemoteException ex) {
857            // the phone process is restarting.
858            return CALL_STATE_IDLE;
859        } catch (NullPointerException ex) {
860          // the phone process is restarting.
861          return CALL_STATE_IDLE;
862      }
863    }
864
865    /** Data connection activity: No traffic. */
866    public static final int DATA_ACTIVITY_NONE = 0x00000000;
867    /** Data connection activity: Currently receiving IP PPP traffic. */
868    public static final int DATA_ACTIVITY_IN = 0x00000001;
869    /** Data connection activity: Currently sending IP PPP traffic. */
870    public static final int DATA_ACTIVITY_OUT = 0x00000002;
871    /** Data connection activity: Currently both sending and receiving
872     *  IP PPP traffic. */
873    public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT;
874    /**
875     * Data connection is active, but physical link is down
876     */
877    public static final int DATA_ACTIVITY_DORMANT = 0x00000004;
878
879    /**
880     * Returns a constant indicating the type of activity on a data connection
881     * (cellular).
882     *
883     * @see #DATA_ACTIVITY_NONE
884     * @see #DATA_ACTIVITY_IN
885     * @see #DATA_ACTIVITY_OUT
886     * @see #DATA_ACTIVITY_INOUT
887     * @see #DATA_ACTIVITY_DORMANT
888     */
889    public int getDataActivity() {
890        try {
891            return getITelephony().getDataActivity();
892        } catch (RemoteException ex) {
893            // the phone process is restarting.
894            return DATA_ACTIVITY_NONE;
895        } catch (NullPointerException ex) {
896          // the phone process is restarting.
897          return DATA_ACTIVITY_NONE;
898      }
899    }
900
901    /** Data connection state: Unknown.  Used before we know the state.
902     * @hide
903     */
904    public static final int DATA_UNKNOWN        = -1;
905    /** Data connection state: Disconnected. IP traffic not available. */
906    public static final int DATA_DISCONNECTED   = 0;
907    /** Data connection state: Currently setting up a data connection. */
908    public static final int DATA_CONNECTING     = 1;
909    /** Data connection state: Connected. IP traffic should be available. */
910    public static final int DATA_CONNECTED      = 2;
911    /** Data connection state: Suspended. The connection is up, but IP
912     * traffic is temporarily unavailable. For example, in a 2G network,
913     * data activity may be suspended when a voice call arrives. */
914    public static final int DATA_SUSPENDED      = 3;
915
916    /**
917     * Returns a constant indicating the current data connection state
918     * (cellular).
919     *
920     * @see #DATA_DISCONNECTED
921     * @see #DATA_CONNECTING
922     * @see #DATA_CONNECTED
923     * @see #DATA_SUSPENDED
924     */
925    public int getDataState() {
926        try {
927            return getITelephony().getDataState();
928        } catch (RemoteException ex) {
929            // the phone process is restarting.
930            return DATA_DISCONNECTED;
931        } catch (NullPointerException ex) {
932            return DATA_DISCONNECTED;
933        }
934    }
935
936    private ITelephony getITelephony() {
937        return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
938    }
939
940    //
941    //
942    // PhoneStateListener
943    //
944    //
945
946    /**
947     * Registers a listener object to receive notification of changes
948     * in specified telephony states.
949     * <p>
950     * To register a listener, pass a {@link PhoneStateListener}
951     * and specify at least one telephony state of interest in
952     * the events argument.
953     *
954     * At registration, and when a specified telephony state
955     * changes, the telephony manager invokes the appropriate
956     * callback method on the listener object and passes the
957     * current (udpated) values.
958     * <p>
959     * To unregister a listener, pass the listener object and set the
960     * events argument to
961     * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0).
962     *
963     * @param listener The {@link PhoneStateListener} object to register
964     *                 (or unregister)
965     * @param events The telephony state(s) of interest to the listener,
966     *               as a bitwise-OR combination of {@link PhoneStateListener}
967     *               LISTEN_ flags.
968     */
969    public void listen(PhoneStateListener listener, int events) {
970        String pkgForDebug = sContext != null ? sContext.getPackageName() : "<unknown>";
971        try {
972            Boolean notifyNow = (getITelephony() != null);
973            sRegistry.listen(pkgForDebug, listener.callback, events, notifyNow);
974        } catch (RemoteException ex) {
975            // system process dead
976        } catch (NullPointerException ex) {
977            // system process dead
978        }
979    }
980
981    /**
982     * Returns the CDMA ERI icon index to display
983     *
984     * @hide
985     */
986    public int getCdmaEriIconIndex() {
987        try {
988            return getITelephony().getCdmaEriIconIndex();
989        } catch (RemoteException ex) {
990            // the phone process is restarting.
991            return -1;
992        } catch (NullPointerException ex) {
993            return -1;
994        }
995    }
996
997    /**
998     * Returns the CDMA ERI icon mode,
999     * 0 - ON
1000     * 1 - FLASHING
1001     *
1002     * @hide
1003     */
1004    public int getCdmaEriIconMode() {
1005        try {
1006            return getITelephony().getCdmaEriIconMode();
1007        } catch (RemoteException ex) {
1008            // the phone process is restarting.
1009            return -1;
1010        } catch (NullPointerException ex) {
1011            return -1;
1012        }
1013    }
1014
1015    /**
1016     * Returns the CDMA ERI text,
1017     *
1018     * @hide
1019     */
1020    public String getCdmaEriText() {
1021        try {
1022            return getITelephony().getCdmaEriText();
1023        } catch (RemoteException ex) {
1024            // the phone process is restarting.
1025            return null;
1026        } catch (NullPointerException ex) {
1027            return null;
1028        }
1029    }
1030
1031    /**
1032     * @return true if the current device is "voice capable".
1033     * <p>
1034     * "Voice capable" means that this device supports circuit-switched
1035     * (i.e. voice) phone calls over the telephony network, and is allowed
1036     * to display the in-call UI while a cellular voice call is active.
1037     * This will be false on "data only" devices which can't make voice
1038     * calls and don't support any in-call UI.
1039     * <p>
1040     * Note: the meaning of this flag is subtly different from the
1041     * PackageManager.FEATURE_TELEPHONY system feature, which is available
1042     * on any device with a telephony radio, even if the device is
1043     * data-only.
1044     *
1045     * @hide pending API review
1046     */
1047    public boolean isVoiceCapable() {
1048        if (sContext == null) return true;
1049        return sContext.getResources().getBoolean(
1050                com.android.internal.R.bool.config_voice_capable);
1051    }
1052
1053    /**
1054     * @return true if the current device supports sms service.
1055     * <p>
1056     * If true, this means that the device supports both sending and
1057     * receiving sms via the telephony network.
1058     * <p>
1059     * Note: Voicemail waiting sms, cell broadcasting sms, and MMS are
1060     *       disabled when device doesn't support sms.
1061     *
1062     * @hide pending API review
1063     */
1064    public boolean isSmsCapable() {
1065        if (sContext == null) return true;
1066        return sContext.getResources().getBoolean(
1067                com.android.internal.R.bool.config_sms_capable);
1068    }
1069}
1070