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