TelephonyManager.java revision 97ab2d4f86ee9c2b661a00688e934fc7030df22e
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    /** Current network is HSPA+ */
431    public static final int NETWORK_TYPE_HSPAP = 15;
432
433    /**
434     * Returns a constant indicating the radio technology (network type)
435     * currently in use on the device for data transmission.
436     * @return the network type
437     *
438     * @see #NETWORK_TYPE_UNKNOWN
439     * @see #NETWORK_TYPE_GPRS
440     * @see #NETWORK_TYPE_EDGE
441     * @see #NETWORK_TYPE_UMTS
442     * @see #NETWORK_TYPE_HSDPA
443     * @see #NETWORK_TYPE_HSUPA
444     * @see #NETWORK_TYPE_HSPA
445     * @see #NETWORK_TYPE_CDMA
446     * @see #NETWORK_TYPE_EVDO_0
447     * @see #NETWORK_TYPE_EVDO_A
448     * @see #NETWORK_TYPE_EVDO_B
449     * @see #NETWORK_TYPE_1xRTT
450     * @see #NETWORK_TYPE_IDEN
451     * @see #NETWORK_TYPE_LTE
452     * @see #NETWORK_TYPE_EHRPD
453     * @see #NETWORK_TYPE_HSPAP
454     */
455    public int getNetworkType() {
456        try{
457            ITelephony telephony = getITelephony();
458            if (telephony != null) {
459                return telephony.getNetworkType();
460            } else {
461                // This can happen when the ITelephony interface is not up yet.
462                return NETWORK_TYPE_UNKNOWN;
463            }
464        } catch(RemoteException ex) {
465            // This shouldn't happen in the normal case
466            return NETWORK_TYPE_UNKNOWN;
467        } catch (NullPointerException ex) {
468            // This could happen before phone restarts due to crashing
469            return NETWORK_TYPE_UNKNOWN;
470        }
471    }
472
473    /**
474     * Returns a string representation of the radio technology (network type)
475     * currently in use on the device.
476     * @return the name of the radio technology
477     *
478     * @hide pending API council review
479     */
480    public String getNetworkTypeName() {
481        switch (getNetworkType()) {
482            case NETWORK_TYPE_GPRS:
483                return "GPRS";
484            case NETWORK_TYPE_EDGE:
485                return "EDGE";
486            case NETWORK_TYPE_UMTS:
487                return "UMTS";
488            case NETWORK_TYPE_HSDPA:
489                return "HSDPA";
490            case NETWORK_TYPE_HSUPA:
491                return "HSUPA";
492            case NETWORK_TYPE_HSPA:
493                return "HSPA";
494            case NETWORK_TYPE_CDMA:
495                return "CDMA";
496            case NETWORK_TYPE_EVDO_0:
497                return "CDMA - EvDo rev. 0";
498            case NETWORK_TYPE_EVDO_A:
499                return "CDMA - EvDo rev. A";
500            case NETWORK_TYPE_EVDO_B:
501                return "CDMA - EvDo rev. B";
502            case NETWORK_TYPE_1xRTT:
503                return "CDMA - 1xRTT";
504            case NETWORK_TYPE_LTE:
505                return "LTE";
506            case NETWORK_TYPE_EHRPD:
507                return "CDMA - eHRPD";
508            case NETWORK_TYPE_IDEN:
509                return "iDEN";
510            case NETWORK_TYPE_HSPAP:
511                return "HSPA+";
512            default:
513                return "UNKNOWN";
514        }
515    }
516
517    //
518    //
519    // SIM Card
520    //
521    //
522
523    /** SIM card state: Unknown. Signifies that the SIM is in transition
524     *  between states. For example, when the user inputs the SIM pin
525     *  under PIN_REQUIRED state, a query for sim status returns
526     *  this state before turning to SIM_STATE_READY. */
527    public static final int SIM_STATE_UNKNOWN = 0;
528    /** SIM card state: no SIM card is available in the device */
529    public static final int SIM_STATE_ABSENT = 1;
530    /** SIM card state: Locked: requires the user's SIM PIN to unlock */
531    public static final int SIM_STATE_PIN_REQUIRED = 2;
532    /** SIM card state: Locked: requires the user's SIM PUK to unlock */
533    public static final int SIM_STATE_PUK_REQUIRED = 3;
534    /** SIM card state: Locked: requries a network PIN to unlock */
535    public static final int SIM_STATE_NETWORK_LOCKED = 4;
536    /** SIM card state: Ready */
537    public static final int SIM_STATE_READY = 5;
538
539    /**
540     * @return true if a ICC card is present
541     */
542    public boolean hasIccCard() {
543        try {
544            return getITelephony().hasIccCard();
545        } catch (RemoteException ex) {
546            // Assume no ICC card if remote exception which shouldn't happen
547            return false;
548        } catch (NullPointerException ex) {
549            // This could happen before phone restarts due to crashing
550            return false;
551        }
552    }
553
554    /**
555     * Returns a constant indicating the state of the
556     * device SIM card.
557     *
558     * @see #SIM_STATE_UNKNOWN
559     * @see #SIM_STATE_ABSENT
560     * @see #SIM_STATE_PIN_REQUIRED
561     * @see #SIM_STATE_PUK_REQUIRED
562     * @see #SIM_STATE_NETWORK_LOCKED
563     * @see #SIM_STATE_READY
564     */
565    public int getSimState() {
566        String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE);
567        if ("ABSENT".equals(prop)) {
568            return SIM_STATE_ABSENT;
569        }
570        else if ("PIN_REQUIRED".equals(prop)) {
571            return SIM_STATE_PIN_REQUIRED;
572        }
573        else if ("PUK_REQUIRED".equals(prop)) {
574            return SIM_STATE_PUK_REQUIRED;
575        }
576        else if ("NETWORK_LOCKED".equals(prop)) {
577            return SIM_STATE_NETWORK_LOCKED;
578        }
579        else if ("READY".equals(prop)) {
580            return SIM_STATE_READY;
581        }
582        else {
583            return SIM_STATE_UNKNOWN;
584        }
585    }
586
587    /**
588     * Returns the MCC+MNC (mobile country code + mobile network code) of the
589     * provider of the SIM. 5 or 6 decimal digits.
590     * <p>
591     * Availability: SIM state must be {@link #SIM_STATE_READY}
592     *
593     * @see #getSimState
594     */
595    public String getSimOperator() {
596        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC);
597    }
598
599    /**
600     * Returns the Service Provider Name (SPN).
601     * <p>
602     * Availability: SIM state must be {@link #SIM_STATE_READY}
603     *
604     * @see #getSimState
605     */
606    public String getSimOperatorName() {
607        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA);
608    }
609
610    /**
611     * Returns the ISO country code equivalent for the SIM provider's country code.
612     */
613    public String getSimCountryIso() {
614        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY);
615    }
616
617    /**
618     * Returns the serial number of the SIM, if applicable. Return null if it is
619     * unavailable.
620     * <p>
621     * Requires Permission:
622     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
623     */
624    public String getSimSerialNumber() {
625        try {
626            return getSubscriberInfo().getIccSerialNumber();
627        } catch (RemoteException ex) {
628            return null;
629        } catch (NullPointerException ex) {
630            // This could happen before phone restarts due to crashing
631            return null;
632        }
633    }
634
635    //
636    //
637    // Subscriber Info
638    //
639    //
640
641    /**
642     * Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
643     * Return null if it is unavailable.
644     * <p>
645     * Requires Permission:
646     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
647     */
648    public String getSubscriberId() {
649        try {
650            return getSubscriberInfo().getSubscriberId();
651        } catch (RemoteException ex) {
652            return null;
653        } catch (NullPointerException ex) {
654            // This could happen before phone restarts due to crashing
655            return null;
656        }
657    }
658
659    /**
660     * Returns the phone number string for line 1, for example, the MSISDN
661     * for a GSM phone. Return null if it is unavailable.
662     * <p>
663     * Requires Permission:
664     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
665     */
666    public String getLine1Number() {
667        try {
668            return getSubscriberInfo().getLine1Number();
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     * Returns the alphabetic identifier associated with the line 1 number.
679     * Return null if it is unavailable.
680     * <p>
681     * Requires Permission:
682     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
683     * @hide
684     * nobody seems to call this.
685     */
686    public String getLine1AlphaTag() {
687        try {
688            return getSubscriberInfo().getLine1AlphaTag();
689        } catch (RemoteException ex) {
690            return null;
691        } catch (NullPointerException ex) {
692            // This could happen before phone restarts due to crashing
693            return null;
694        }
695    }
696
697    /**
698     * Returns the voice mail number. Return null if it is unavailable.
699     * <p>
700     * Requires Permission:
701     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
702     */
703    public String getVoiceMailNumber() {
704        try {
705            return getSubscriberInfo().getVoiceMailNumber();
706        } catch (RemoteException ex) {
707            return null;
708        } catch (NullPointerException ex) {
709            // This could happen before phone restarts due to crashing
710            return null;
711        }
712    }
713
714    /**
715     * Returns the complete voice mail number. Return null if it is unavailable.
716     * <p>
717     * Requires Permission:
718     *   {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED}
719     *
720     * @hide
721     */
722    public String getCompleteVoiceMailNumber() {
723        try {
724            return getSubscriberInfo().getCompleteVoiceMailNumber();
725        } catch (RemoteException ex) {
726            return null;
727        } catch (NullPointerException ex) {
728            // This could happen before phone restarts due to crashing
729            return null;
730        }
731    }
732
733    /**
734     * Returns the voice mail count. Return 0 if unavailable.
735     * <p>
736     * Requires Permission:
737     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
738     * @hide
739     */
740    public int getVoiceMessageCount() {
741        try {
742            return getITelephony().getVoiceMessageCount();
743        } catch (RemoteException ex) {
744            return 0;
745        } catch (NullPointerException ex) {
746            // This could happen before phone restarts due to crashing
747            return 0;
748        }
749    }
750
751    /**
752     * Retrieves the alphabetic identifier associated with the voice
753     * mail number.
754     * <p>
755     * Requires Permission:
756     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
757     */
758    public String getVoiceMailAlphaTag() {
759        try {
760            return getSubscriberInfo().getVoiceMailAlphaTag();
761        } catch (RemoteException ex) {
762            return null;
763        } catch (NullPointerException ex) {
764            // This could happen before phone restarts due to crashing
765            return null;
766        }
767    }
768
769    private IPhoneSubInfo getSubscriberInfo() {
770        // get it each time because that process crashes a lot
771        return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
772    }
773
774
775    /** Device call state: No activity. */
776    public static final int CALL_STATE_IDLE = 0;
777    /** Device call state: Ringing. A new call arrived and is
778     *  ringing or waiting. In the latter case, another call is
779     *  already active. */
780    public static final int CALL_STATE_RINGING = 1;
781    /** Device call state: Off-hook. At least one call exists
782      * that is dialing, active, or on hold, and no calls are ringing
783      * or waiting. */
784    public static final int CALL_STATE_OFFHOOK = 2;
785
786    /**
787     * Returns a constant indicating the call state (cellular) on the device.
788     */
789    public int getCallState() {
790        try {
791            return getITelephony().getCallState();
792        } catch (RemoteException ex) {
793            // the phone process is restarting.
794            return CALL_STATE_IDLE;
795        } catch (NullPointerException ex) {
796          // the phone process is restarting.
797          return CALL_STATE_IDLE;
798      }
799    }
800
801    /** Data connection activity: No traffic. */
802    public static final int DATA_ACTIVITY_NONE = 0x00000000;
803    /** Data connection activity: Currently receiving IP PPP traffic. */
804    public static final int DATA_ACTIVITY_IN = 0x00000001;
805    /** Data connection activity: Currently sending IP PPP traffic. */
806    public static final int DATA_ACTIVITY_OUT = 0x00000002;
807    /** Data connection activity: Currently both sending and receiving
808     *  IP PPP traffic. */
809    public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT;
810    /**
811     * Data connection is active, but physical link is down
812     */
813    public static final int DATA_ACTIVITY_DORMANT = 0x00000004;
814
815    /**
816     * Returns a constant indicating the type of activity on a data connection
817     * (cellular).
818     *
819     * @see #DATA_ACTIVITY_NONE
820     * @see #DATA_ACTIVITY_IN
821     * @see #DATA_ACTIVITY_OUT
822     * @see #DATA_ACTIVITY_INOUT
823     * @see #DATA_ACTIVITY_DORMANT
824     */
825    public int getDataActivity() {
826        try {
827            return getITelephony().getDataActivity();
828        } catch (RemoteException ex) {
829            // the phone process is restarting.
830            return DATA_ACTIVITY_NONE;
831        } catch (NullPointerException ex) {
832          // the phone process is restarting.
833          return DATA_ACTIVITY_NONE;
834      }
835    }
836
837    /** Data connection state: Unknown.  Used before we know the state.
838     * @hide
839     */
840    public static final int DATA_UNKNOWN        = -1;
841    /** Data connection state: Disconnected. IP traffic not available. */
842    public static final int DATA_DISCONNECTED   = 0;
843    /** Data connection state: Currently setting up a data connection. */
844    public static final int DATA_CONNECTING     = 1;
845    /** Data connection state: Connected. IP traffic should be available. */
846    public static final int DATA_CONNECTED      = 2;
847    /** Data connection state: Suspended. The connection is up, but IP
848     * traffic is temporarily unavailable. For example, in a 2G network,
849     * data activity may be suspended when a voice call arrives. */
850    public static final int DATA_SUSPENDED      = 3;
851
852    /**
853     * Returns a constant indicating the current data connection state
854     * (cellular).
855     *
856     * @see #DATA_DISCONNECTED
857     * @see #DATA_CONNECTING
858     * @see #DATA_CONNECTED
859     * @see #DATA_SUSPENDED
860     */
861    public int getDataState() {
862        try {
863            return getITelephony().getDataState();
864        } catch (RemoteException ex) {
865            // the phone process is restarting.
866            return DATA_DISCONNECTED;
867        } catch (NullPointerException ex) {
868            return DATA_DISCONNECTED;
869        }
870    }
871
872    private ITelephony getITelephony() {
873        return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
874    }
875
876    //
877    //
878    // PhoneStateListener
879    //
880    //
881
882    /**
883     * Registers a listener object to receive notification of changes
884     * in specified telephony states.
885     * <p>
886     * To register a listener, pass a {@link PhoneStateListener}
887     * and specify at least one telephony state of interest in
888     * the events argument.
889     *
890     * At registration, and when a specified telephony state
891     * changes, the telephony manager invokes the appropriate
892     * callback method on the listener object and passes the
893     * current (udpated) values.
894     * <p>
895     * To unregister a listener, pass the listener object and set the
896     * events argument to
897     * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0).
898     *
899     * @param listener The {@link PhoneStateListener} object to register
900     *                 (or unregister)
901     * @param events The telephony state(s) of interest to the listener,
902     *               as a bitwise-OR combination of {@link PhoneStateListener}
903     *               LISTEN_ flags.
904     */
905    public void listen(PhoneStateListener listener, int events) {
906        String pkgForDebug = sContext != null ? sContext.getPackageName() : "<unknown>";
907        try {
908            Boolean notifyNow = (getITelephony() != null);
909            sRegistry.listen(pkgForDebug, listener.callback, events, notifyNow);
910        } catch (RemoteException ex) {
911            // system process dead
912        } catch (NullPointerException ex) {
913            // system process dead
914        }
915    }
916
917    /**
918     * Returns the CDMA ERI icon index to display
919     *
920     * @hide
921     */
922    public int getCdmaEriIconIndex() {
923        try {
924            return getITelephony().getCdmaEriIconIndex();
925        } catch (RemoteException ex) {
926            // the phone process is restarting.
927            return -1;
928        } catch (NullPointerException ex) {
929            return -1;
930        }
931    }
932
933    /**
934     * Returns the CDMA ERI icon mode,
935     * 0 - ON
936     * 1 - FLASHING
937     *
938     * @hide
939     */
940    public int getCdmaEriIconMode() {
941        try {
942            return getITelephony().getCdmaEriIconMode();
943        } catch (RemoteException ex) {
944            // the phone process is restarting.
945            return -1;
946        } catch (NullPointerException ex) {
947            return -1;
948        }
949    }
950
951    /**
952     * Returns the CDMA ERI text,
953     *
954     * @hide
955     */
956    public String getCdmaEriText() {
957        try {
958            return getITelephony().getCdmaEriText();
959        } catch (RemoteException ex) {
960            // the phone process is restarting.
961            return null;
962        } catch (NullPointerException ex) {
963            return null;
964        }
965    }
966
967    /**
968     * @return true if the current device is "voice capable".
969     * <p>
970     * "Voice capable" means that this device supports circuit-switched
971     * (i.e. voice) phone calls over the telephony network, and is allowed
972     * to display the in-call UI while a cellular voice call is active.
973     * This will be false on "data only" devices which can't make voice
974     * calls and don't support any in-call UI.
975     * <p>
976     * Note: the meaning of this flag is subtly different from the
977     * PackageManager.FEATURE_TELEPHONY system feature, which is available
978     * on any device with a telephony radio, even if the device is
979     * data-only.
980     *
981     * @hide pending API review
982     */
983    public boolean isVoiceCapable() {
984        if (sContext == null) return true;
985        return sContext.getResources().getBoolean(
986                com.android.internal.R.bool.config_voice_capable);
987    }
988
989    /**
990     * @return true if the current device supports sms service.
991     * <p>
992     * If true, this means that the device supports both sending and
993     * receiving sms via the telephony network.
994     * <p>
995     * Note: Voicemail waiting sms, cell broadcasting sms, and MMS are
996     *       disabled when device doesn't support sms.
997     *
998     * @hide pending API review
999     */
1000    public boolean isSmsCapable() {
1001        if (sContext == null) return true;
1002        return sContext.getResources().getBoolean(
1003                com.android.internal.R.bool.config_sms_capable);
1004    }
1005}
1006