TelephonyManager.java revision b307c8945d4bf8d843445f3cc6d727f4e43d90f0
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.telephony;
18
19import android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
21import android.content.Context;
22import android.os.Bundle;
23import android.os.RemoteException;
24import android.os.ServiceManager;
25import android.os.SystemProperties;
26
27import com.android.internal.telephony.IPhoneSubInfo;
28import com.android.internal.telephony.ITelephony;
29import com.android.internal.telephony.ITelephonyRegistry;
30import com.android.internal.telephony.Phone;
31import com.android.internal.telephony.PhoneFactory;
32import com.android.internal.telephony.RILConstants;
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 acess 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 Context mContext;
59    private ITelephonyRegistry mRegistry;
60
61    /** @hide */
62    public TelephonyManager(Context context) {
63        mContext = context;
64        mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
65                    "telephony.registry"));
66    }
67
68    /** @hide */
69    private TelephonyManager() {
70    }
71
72    private static TelephonyManager sInstance = new TelephonyManager();
73
74    /** @hide */
75    public static TelephonyManager getDefault() {
76        return sInstance;
77    }
78
79
80    //
81    // Broadcast Intent actions
82    //
83
84    /**
85     * Broadcast intent action indicating that the call state (cellular)
86     * on the device has changed.
87     *
88     * <p>
89     * The {@link #EXTRA_STATE} extra indicates the new call state.
90     * If the new state is RINGING, a second extra
91     * {@link #EXTRA_INCOMING_NUMBER} provides the incoming phone number as
92     * a String.
93     *
94     * <p class="note">
95     * Requires the READ_PHONE_STATE permission.
96     *
97     * <p class="note">
98     * This was a {@link android.content.Context#sendStickyBroadcast sticky}
99     * broadcast in version 1.0, but it is no longer sticky.
100     * Instead, use {@link #getCallState} to synchronously query the current call state.
101     *
102     * @see #EXTRA_STATE
103     * @see #EXTRA_INCOMING_NUMBER
104     * @see #getCallState
105     */
106    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
107    public static final String ACTION_PHONE_STATE_CHANGED =
108            "android.intent.action.PHONE_STATE";
109
110    /**
111     * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
112     * for a String containing the new call state.
113     *
114     * @see #EXTRA_STATE_IDLE
115     * @see #EXTRA_STATE_RINGING
116     * @see #EXTRA_STATE_OFFHOOK
117     *
118     * <p class="note">
119     * Retrieve with
120     * {@link android.content.Intent#getStringExtra(String)}.
121     */
122    public static final String EXTRA_STATE = Phone.STATE_KEY;
123
124    /**
125     * Value used with {@link #EXTRA_STATE} corresponding to
126     * {@link #CALL_STATE_IDLE}.
127     */
128    public static final String EXTRA_STATE_IDLE = Phone.State.IDLE.toString();
129
130    /**
131     * Value used with {@link #EXTRA_STATE} corresponding to
132     * {@link #CALL_STATE_RINGING}.
133     */
134    public static final String EXTRA_STATE_RINGING = Phone.State.RINGING.toString();
135
136    /**
137     * Value used with {@link #EXTRA_STATE} corresponding to
138     * {@link #CALL_STATE_OFFHOOK}.
139     */
140    public static final String EXTRA_STATE_OFFHOOK = Phone.State.OFFHOOK.toString();
141
142    /**
143     * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
144     * for a String containing the incoming phone number.
145     * Only valid when the new call state is RINGING.
146     *
147     * <p class="note">
148     * Retrieve with
149     * {@link android.content.Intent#getStringExtra(String)}.
150     */
151    public static final String EXTRA_INCOMING_NUMBER = "incoming_number";
152
153
154    //
155    //
156    // Device Info
157    //
158    //
159
160    /**
161     * Returns the software version number for the device, for example,
162     * the IMEI/SV for GSM phones.
163     *
164     * <p>Requires Permission:
165     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
166     */
167    public String getDeviceSoftwareVersion() {
168        try {
169            return getSubscriberInfo().getDeviceSvn();
170        } catch (RemoteException ex) {
171        }
172        return null;
173    }
174
175    /**
176     * Returns the unique device ID, for example, the IMEI for GSM and the MEID for CDMA
177     * phones.
178     *
179     * <p>Requires Permission:
180     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
181     */
182    public String getDeviceId() {
183        try {
184            return getSubscriberInfo().getDeviceId();
185        } catch (RemoteException ex) {
186        }
187        return null;
188    }
189
190    /**
191     * Returns the current location of the device.
192     *
193     * <p>Requires Permission:
194     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} or
195     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION}.
196     */
197    public CellLocation getCellLocation() {
198        try {
199            Bundle bundle = getITelephony().getCellLocation();
200            return CellLocation.newFromBundle(bundle);
201        } catch (RemoteException ex) {
202        }
203        return null;
204    }
205
206    /**
207     * Enables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
208     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
209     *
210     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
211     * CONTROL_LOCATION_UPDATES}
212     *
213     * @hide
214     */
215    public void enableLocationUpdates() {
216        try {
217            getITelephony().enableLocationUpdates();
218        } catch (RemoteException ex) {
219        }
220    }
221
222    /**
223     * Disables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
224     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
225     *
226     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
227     * CONTROL_LOCATION_UPDATES}
228     *
229     * @hide
230     */
231    public void disableLocationUpdates() {
232        try {
233            getITelephony().disableLocationUpdates();
234        } catch (RemoteException ex) {
235        }
236    }
237
238    /**
239     * Returns the neighboring cell information of the device.
240     *
241     * @return List of NeighboringCellInfo or null if info unavailable.
242     *
243     * <p>Requires Permission:
244     * (@link android.Manifest.permission#ACCESS_COARSE_UPDATES}
245     */
246    public List<NeighboringCellInfo> getNeighboringCellInfo() {
247       try {
248           return getITelephony().getNeighboringCellInfo();
249       } catch (RemoteException ex) {
250       }
251       return null;
252
253    }
254
255    /**
256     * No phone module
257     *
258     */
259    public static final int PHONE_TYPE_NONE = RILConstants.NO_PHONE;
260
261    /**
262     * GSM phone
263     */
264    public static final int PHONE_TYPE_GSM = RILConstants.GSM_PHONE;
265
266    /**
267     * CDMA phone
268     * @hide
269     */
270    public static final int PHONE_TYPE_CDMA = RILConstants.CDMA_PHONE;
271
272    /**
273     * Returns a constant indicating the device phone type.
274     *
275     * @see #PHONE_TYPE_NONE
276     * @see #PHONE_TYPE_GSM
277     * @see #PHONE_TYPE_CDMA
278     */
279    public int getPhoneType() {
280        try{
281            ITelephony telephony = getITelephony();
282            if (telephony != null) {
283                if(telephony.getActivePhoneType() == RILConstants.CDMA_PHONE) {
284                    return PHONE_TYPE_CDMA;
285                } else {
286                    return PHONE_TYPE_GSM;
287                }
288            } else {
289                // This can happen when the ITelephony interface is not up yet.
290                return getPhoneTypeFromProperty();
291            }
292        } catch(RemoteException ex){
293            // This shouldn't happen in the normal case, as a backup we
294            // read from the system property.
295            return getPhoneTypeFromProperty();
296        }
297    }
298
299
300    private int getPhoneTypeFromProperty() {
301        int type =
302            SystemProperties.getInt(TelephonyProperties.CURRENT_ACTIVE_PHONE,
303                    getPhoneTypeFromNetworkType());
304        return type;
305    }
306
307    private int getPhoneTypeFromNetworkType() {
308        // When the system property CURRENT_ACTIVE_PHONE, has not been set,
309        // use the system property for default network type.
310        // This is a fail safe, and can only happen at first boot.
311        int mode = SystemProperties.getInt("ro.telephony.default_network", -1);
312        if (mode == -1)
313            return PHONE_TYPE_NONE;
314        return PhoneFactory.getPhoneType(mode);
315    }
316    //
317    //
318    // Current Network
319    //
320    //
321
322    /**
323     * Returns the alphabetic name of current registered operator.
324     * <p>
325     * Availability: Only when user is registered to a network. Result may be
326     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
327     * on a CDMA network).
328     */
329    public String getNetworkOperatorName() {
330        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ALPHA);
331    }
332
333    /**
334     * Returns the numeric name (MCC+MNC) of current registered operator.
335     * <p>
336     * Availability: Only when user is registered to a network. Result may be
337     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
338     * on a CDMA network).
339     */
340    public String getNetworkOperator() {
341        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_NUMERIC);
342    }
343
344    /**
345     * Returns true if the device is considered roaming on the current
346     * network, for GSM purposes.
347     * <p>
348     * Availability: Only when user registered to a network.
349     */
350    public boolean isNetworkRoaming() {
351        return "true".equals(SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING));
352    }
353
354    /**
355     * Returns the ISO country code equivilent of the current registered
356     * operator's MCC (Mobile Country Code).
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 getNetworkCountryIso() {
363        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY);
364    }
365
366    /** Network type is unknown */
367    public static final int NETWORK_TYPE_UNKNOWN = 0;
368    /** Current network is GPRS */
369    public static final int NETWORK_TYPE_GPRS = 1;
370    /** Current network is EDGE */
371    public static final int NETWORK_TYPE_EDGE = 2;
372    /** Current network is UMTS */
373    public static final int NETWORK_TYPE_UMTS = 3;
374    /** Current network is CDMA: Either IS95A or IS95B*/
375    /** @hide */
376    public static final int NETWORK_TYPE_CDMA = 4;
377    /** Current network is EVDO revision 0 or revision A*/
378    /** @hide */
379    public static final int NETWORK_TYPE_EVDO_0 = 5;
380    /** @hide */
381    public static final int NETWORK_TYPE_EVDO_A = 6;
382    /** Current network is 1xRTT*/
383    /** @hide */
384    public static final int NETWORK_TYPE_1xRTT = 7;
385
386    /**
387     * Returns a constant indicating the radio technology (network type)
388     * currently in use on the device.
389     * @return the network type
390     *
391     * @see #NETWORK_TYPE_UNKNOWN
392     * @see #NETWORK_TYPE_GPRS
393     * @see #NETWORK_TYPE_EDGE
394     * @see #NETWORK_TYPE_UMTS
395     * @see #NETWORK_TYPE_CDMA
396     * @see #NETWORK_TYPE_EVDO_0
397     * @see #NETWORK_TYPE_EVDO_A
398     * @see #NETWORK_TYPE_1xRTT
399     */
400    public int getNetworkType() {
401        String prop = SystemProperties.get(TelephonyProperties.PROPERTY_DATA_NETWORK_TYPE);
402        if ("GPRS".equals(prop)) {
403            return NETWORK_TYPE_GPRS;
404        }
405        else if ("EDGE".equals(prop)) {
406            return NETWORK_TYPE_EDGE;
407        }
408        else if ("UMTS".equals(prop)) {
409            return NETWORK_TYPE_UMTS;
410        }
411        else if ("CDMA".equals(prop)) {
412            return NETWORK_TYPE_CDMA;
413                }
414        else if ("CDMA - EvDo rev. 0".equals(prop)) {
415            return NETWORK_TYPE_EVDO_0;
416            }
417        else if ("CDMA - EvDo rev. A".equals(prop)) {
418            return NETWORK_TYPE_EVDO_A;
419            }
420        else if ("CDMA - 1xRTT".equals(prop)) {
421            return NETWORK_TYPE_1xRTT;
422            }
423        else {
424            return NETWORK_TYPE_UNKNOWN;
425        }
426    }
427
428    /**
429     * Returns a string representation of the radio technology (network type)
430     * currently in use on the device.
431     * @return the name of the radio technology
432     *
433     * @hide pending API council review
434     */
435    public String getNetworkTypeName() {
436        switch (getNetworkType()) {
437            case NETWORK_TYPE_GPRS:
438                return "GPRS";
439            case NETWORK_TYPE_EDGE:
440                return "EDGE";
441            case NETWORK_TYPE_UMTS:
442                return "UMTS";
443            case NETWORK_TYPE_CDMA:
444                return "CDMA";
445            case NETWORK_TYPE_EVDO_0:
446                return "CDMA - EvDo rev. 0";
447            case NETWORK_TYPE_EVDO_A:
448                return "CDMA - EvDo rev. A";
449            case NETWORK_TYPE_1xRTT:
450                return "CDMA - 1xRTT";
451            default:
452                return "UNKNOWN";
453        }
454    }
455
456    //
457    //
458    // SIM Card
459    //
460    //
461
462    /** SIM card state: Unknown. Signifies that the SIM is in transition
463     *  between states. For example, when the user inputs the SIM pin
464     *  under PIN_REQUIRED state, a query for sim status returns
465     *  this state before turning to SIM_STATE_READY. */
466    public static final int SIM_STATE_UNKNOWN = 0;
467    /** SIM card state: no SIM card is available in the device */
468    public static final int SIM_STATE_ABSENT = 1;
469    /** SIM card state: Locked: requires the user's SIM PIN to unlock */
470    public static final int SIM_STATE_PIN_REQUIRED = 2;
471    /** SIM card state: Locked: requires the user's SIM PUK to unlock */
472    public static final int SIM_STATE_PUK_REQUIRED = 3;
473    /** SIM card state: Locked: requries a network PIN to unlock */
474    public static final int SIM_STATE_NETWORK_LOCKED = 4;
475    /** SIM card state: Ready */
476    public static final int SIM_STATE_READY = 5;
477
478    /**
479     * Returns a constant indicating the state of the
480     * device SIM card.
481     *
482     * @see #SIM_STATE_UNKNOWN
483     * @see #SIM_STATE_ABSENT
484     * @see #SIM_STATE_PIN_REQUIRED
485     * @see #SIM_STATE_PUK_REQUIRED
486     * @see #SIM_STATE_NETWORK_LOCKED
487     * @see #SIM_STATE_READY
488     */
489    public int getSimState() {
490        String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE);
491        if ("ABSENT".equals(prop)) {
492            return SIM_STATE_ABSENT;
493        }
494        else if ("PIN_REQUIRED".equals(prop)) {
495            return SIM_STATE_PIN_REQUIRED;
496        }
497        else if ("PUK_REQUIRED".equals(prop)) {
498            return SIM_STATE_PUK_REQUIRED;
499        }
500        else if ("NETWORK_LOCKED".equals(prop)) {
501            return SIM_STATE_NETWORK_LOCKED;
502        }
503        else if ("READY".equals(prop)) {
504            return SIM_STATE_READY;
505        }
506        else {
507            return SIM_STATE_UNKNOWN;
508        }
509    }
510
511    /**
512     * Returns the MCC+MNC (mobile country code + mobile network code) of the
513     * provider of the SIM. 5 or 6 decimal digits.
514     * <p>
515     * Availability: SIM state must be {@link #SIM_STATE_READY}
516     *
517     * @see #getSimState
518     */
519    public String getSimOperator() {
520        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC);
521    }
522
523    /**
524     * Returns the Service Provider Name (SPN).
525     * <p>
526     * Availability: SIM state must be {@link #SIM_STATE_READY}
527     *
528     * @see #getSimState
529     */
530    public String getSimOperatorName() {
531        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA);
532    }
533
534    /**
535     * Returns the ISO country code equivalent for the SIM provider's country code.
536     */
537    public String getSimCountryIso() {
538        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY);
539    }
540
541    /**
542     * Returns the serial number of the SIM, if applicable.
543     * <p>
544     * Requires Permission:
545     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
546     */
547    public String getSimSerialNumber() {
548        try {
549            return getSubscriberInfo().getIccSerialNumber();
550        } catch (RemoteException ex) {
551        }
552        return null;
553    }
554
555    //
556    //
557    // Subscriber Info
558    //
559    //
560
561    /**
562     * Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
563     * <p>
564     * Requires Permission:
565     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
566     */
567    public String getSubscriberId() {
568        try {
569            return getSubscriberInfo().getSubscriberId();
570        } catch (RemoteException ex) {
571        }
572        return null;
573    }
574
575    /**
576     * Returns the phone number string for line 1, for example, the MSISDN
577     * for a GSM phone.
578     * <p>
579     * Requires Permission:
580     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
581     */
582    public String getLine1Number() {
583        try {
584            return getSubscriberInfo().getLine1Number();
585        } catch (RemoteException ex) {
586        }
587        return null;
588    }
589
590    /**
591     * Returns the alphabetic identifier associated with the line 1 number.
592     * <p>
593     * Requires Permission:
594     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
595     * @hide
596     * nobody seems to call this.
597     */
598    public String getLine1AlphaTag() {
599        try {
600            return getSubscriberInfo().getLine1AlphaTag();
601        } catch (RemoteException ex) {
602        }
603        return null;
604    }
605
606    /**
607     * Returns the voice mail number.
608     * <p>
609     * Requires Permission:
610     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
611     */
612    public String getVoiceMailNumber() {
613        try {
614            return getSubscriberInfo().getVoiceMailNumber();
615        } catch (RemoteException ex) {
616        }
617        return null;
618    }
619
620    /**
621     * Returns the voice mail count.
622     * <p>
623     * Requires Permission:
624     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
625     * @hide
626     */
627    public int getVoiceMessageCount() {
628        try {
629            return getITelephony().getVoiceMessageCount();
630        } catch (RemoteException ex) {
631        }
632        return 0;
633    }
634
635    /**
636     * Retrieves the alphabetic identifier associated with the voice
637     * mail number.
638     * <p>
639     * Requires Permission:
640     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
641     */
642    public String getVoiceMailAlphaTag() {
643        try {
644            return getSubscriberInfo().getVoiceMailAlphaTag();
645        } catch (RemoteException ex) {
646        }
647        return null;
648    }
649
650    private IPhoneSubInfo getSubscriberInfo() {
651        // get it each time because that process crashes a lot
652        return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
653    }
654
655
656    /** Device call state: No activity. */
657    public static final int CALL_STATE_IDLE = 0;
658    /** Device call state: Ringing. A new call arrived and is
659     *  ringing or waiting. In the latter case, another call is
660     *  already active. */
661    public static final int CALL_STATE_RINGING = 1;
662    /** Device call state: Off-hook. At least one call exists
663      * that is dialing, active, or on hold, and no calls are ringing
664      * or waiting. */
665    public static final int CALL_STATE_OFFHOOK = 2;
666
667    /**
668     * Returns a constant indicating the call state (cellular) on the device.
669     */
670    public int getCallState() {
671        try {
672            return getITelephony().getCallState();
673        } catch (RemoteException ex) {
674            // the phone process is restarting.
675            return CALL_STATE_IDLE;
676        } catch (NullPointerException ex) {
677          // the phone process is restarting.
678          return CALL_STATE_IDLE;
679      }
680    }
681
682    /** Data connection activity: No traffic. */
683    public static final int DATA_ACTIVITY_NONE = 0x00000000;
684    /** Data connection activity: Currently receiving IP PPP traffic. */
685    public static final int DATA_ACTIVITY_IN = 0x00000001;
686    /** Data connection activity: Currently sending IP PPP traffic. */
687    public static final int DATA_ACTIVITY_OUT = 0x00000002;
688    /** Data connection activity: Currently both sending and receiving
689     *  IP PPP traffic. */
690    public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT;
691    /**
692     * Data connection is active, but physical link is down
693     * @hide
694     */
695    public static final int DATA_ACTIVITY_DORMANT = 0x00000004;
696
697    /**
698     * Returns a constant indicating the type of activity on a data connection
699     * (cellular).
700     *
701     * @see #DATA_ACTIVITY_NONE
702     * @see #DATA_ACTIVITY_IN
703     * @see #DATA_ACTIVITY_OUT
704     * @see #DATA_ACTIVITY_INOUT
705     * @see #DATA_ACTIVITY_DORMANT
706     */
707    public int getDataActivity() {
708        try {
709            return getITelephony().getDataActivity();
710        } catch (RemoteException ex) {
711            // the phone process is restarting.
712            return DATA_ACTIVITY_NONE;
713        } catch (NullPointerException ex) {
714          // the phone process is restarting.
715          return DATA_ACTIVITY_NONE;
716      }
717    }
718
719    /** Data connection state: Disconnected. IP traffic not available. */
720    public static final int DATA_DISCONNECTED   = 0;
721    /** Data connection state: Currently setting up a data connection. */
722    public static final int DATA_CONNECTING     = 1;
723    /** Data connection state: Connected. IP traffic should be available. */
724    public static final int DATA_CONNECTED      = 2;
725    /** Data connection state: Suspended. The connection is up, but IP
726     * traffic is temporarily unavailable. For example, in a 2G network,
727     * data activity may be suspended when a voice call arrives. */
728    public static final int DATA_SUSPENDED      = 3;
729
730    /**
731     * Returns a constant indicating the current data connection state
732     * (cellular).
733     *
734     * @see #DATA_DISCONNECTED
735     * @see #DATA_CONNECTING
736     * @see #DATA_CONNECTED
737     * @see #DATA_SUSPENDED
738     */
739    public int getDataState() {
740        try {
741            return getITelephony().getDataState();
742        } catch (RemoteException ex) {
743            // the phone process is restarting.
744            return DATA_DISCONNECTED;
745        }
746    }
747
748    private ITelephony getITelephony() {
749        return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
750    }
751
752    //
753    //
754    // PhoneStateListener
755    //
756    //
757
758    /**
759     * Registers a listener object to receive notification of changes
760     * in specified telephony states.
761     * <p>
762     * To register a listener, pass a {@link PhoneStateListener}
763     * and specify at least one telephony state of interest in
764     * the events argument.
765     *
766     * At registration, and when a specified telephony state
767     * changes, the telephony manager invokes the appropriate
768     * callback method on the listener object and passes the
769     * current (udpated) values.
770     * <p>
771     * To unregister a listener, pass the listener object and set the
772     * events argument to
773     * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0).
774     *
775     * @param listener The {@link PhoneStateListener} object to register
776     *                 (or unregister)
777     * @param events The telephony state(s) of interest to the listener,
778     *               as a bitwise-OR combination of {@link PhoneStateListener}
779     *               LISTEN_ flags.
780     */
781    public void listen(PhoneStateListener listener, int events) {
782        String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>";
783        try {
784            Boolean notifyNow = (getITelephony() != null);
785            mRegistry.listen(pkgForDebug, listener.callback, events, notifyNow);
786        } catch (RemoteException ex) {
787            // system process dead
788        }
789    }
790
791    /**
792     * Returns the CDMA ERI icon index to display
793     *
794     * @hide
795     */
796    public int getCdmaEriIconIndex() {
797        try {
798            return getITelephony().getCdmaEriIconIndex();
799        } catch (RemoteException ex) {
800            // the phone process is restarting.
801            return -1;
802        }
803    }
804
805    /**
806     * Returns the CDMA ERI icon mode,
807     * 0 - ON
808     * 1 - FLASHING
809     *
810     * @hide
811     */
812    public int getCdmaEriIconMode() {
813        try {
814            return getITelephony().getCdmaEriIconMode();
815        } catch (RemoteException ex) {
816            // the phone process is restarting.
817            return -1;
818        }
819    }
820
821    /**
822     * Returns the CDMA ERI text,
823     *
824     * @hide
825     */
826    public String getCdmaEriText() {
827        try {
828            return getITelephony().getCdmaEriText();
829        } catch (RemoteException ex) {
830            // the phone process is restarting.
831            return null;
832        }
833    }
834}
835