TelephonyManager.java revision 5f1f26a7ffb3e1130108bc670960cb5368038a1b
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. Return null if the software version is
163     * not available.
164     *
165     * <p>Requires Permission:
166     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
167     */
168    public String getDeviceSoftwareVersion() {
169        try {
170            return getSubscriberInfo().getDeviceSvn();
171        } catch (RemoteException ex) {
172            return null;
173        } catch (NullPointerException ex) {
174            return null;
175        }
176    }
177
178    /**
179     * Returns the unique device ID, for example, the IMEI for GSM and the MEID
180     * for CDMA phones. Return null if device ID is not available.
181     *
182     * <p>Requires Permission:
183     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
184     */
185    public String getDeviceId() {
186        try {
187            return getSubscriberInfo().getDeviceId();
188        } catch (RemoteException ex) {
189            return null;
190        } catch (NullPointerException ex) {
191            return null;
192        }
193    }
194
195    /**
196     * Returns the current location of the device.
197     * Return null if current location is not available.
198     *
199     * <p>Requires Permission:
200     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} or
201     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION}.
202     */
203    public CellLocation getCellLocation() {
204        try {
205            Bundle bundle = getITelephony().getCellLocation();
206            return CellLocation.newFromBundle(bundle);
207        } catch (RemoteException ex) {
208            return null;
209        } catch (NullPointerException ex) {
210            return null;
211        }
212    }
213
214    /**
215     * Enables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
216     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
217     *
218     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
219     * CONTROL_LOCATION_UPDATES}
220     *
221     * @hide
222     */
223    public void enableLocationUpdates() {
224        try {
225            getITelephony().enableLocationUpdates();
226        } catch (RemoteException ex) {
227        } catch (NullPointerException ex) {
228        }
229    }
230
231    /**
232     * Disables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
233     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
234     *
235     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
236     * CONTROL_LOCATION_UPDATES}
237     *
238     * @hide
239     */
240    public void disableLocationUpdates() {
241        try {
242            getITelephony().disableLocationUpdates();
243        } catch (RemoteException ex) {
244        } catch (NullPointerException ex) {
245        }
246    }
247
248    /**
249     * Returns the neighboring cell information of the device.
250     *
251     * @return List of NeighboringCellInfo or null if info unavailable.
252     *
253     * <p>Requires Permission:
254     * (@link android.Manifest.permission#ACCESS_COARSE_UPDATES}
255     */
256    public List<NeighboringCellInfo> getNeighboringCellInfo() {
257       try {
258           return getITelephony().getNeighboringCellInfo();
259       } catch (RemoteException ex) {
260           return null;
261       } catch (NullPointerException ex) {
262           return null;
263       }
264    }
265
266    /**
267     * No phone module
268     *
269     */
270    public static final int PHONE_TYPE_NONE = RILConstants.NO_PHONE;
271
272    /**
273     * GSM phone
274     */
275    public static final int PHONE_TYPE_GSM = RILConstants.GSM_PHONE;
276
277    /**
278     * CDMA phone
279     * @hide
280     */
281    public static final int PHONE_TYPE_CDMA = RILConstants.CDMA_PHONE;
282
283    /**
284     * Returns a constant indicating the device phone type.
285     *
286     * @see #PHONE_TYPE_NONE
287     * @see #PHONE_TYPE_GSM
288     * @see #PHONE_TYPE_CDMA
289     */
290    public int getPhoneType() {
291        try{
292            ITelephony telephony = getITelephony();
293            if (telephony != null) {
294                if(telephony.getActivePhoneType() == RILConstants.CDMA_PHONE) {
295                    return PHONE_TYPE_CDMA;
296                } else {
297                    return PHONE_TYPE_GSM;
298                }
299            } else {
300                // This can happen when the ITelephony interface is not up yet.
301                return getPhoneTypeFromProperty();
302            }
303        } catch (RemoteException ex) {
304            // This shouldn't happen in the normal case, as a backup we
305            // read from the system property.
306            return getPhoneTypeFromProperty();
307        } catch (NullPointerException ex) {
308            // This shouldn't happen in the normal case, as a backup we
309            // read from the system property.
310            return getPhoneTypeFromProperty();
311        }
312    }
313
314
315    private int getPhoneTypeFromProperty() {
316        int type =
317            SystemProperties.getInt(TelephonyProperties.CURRENT_ACTIVE_PHONE,
318                    getPhoneTypeFromNetworkType());
319        return type;
320    }
321
322    private int getPhoneTypeFromNetworkType() {
323        // When the system property CURRENT_ACTIVE_PHONE, has not been set,
324        // use the system property for default network type.
325        // This is a fail safe, and can only happen at first boot.
326        int mode = SystemProperties.getInt("ro.telephony.default_network", -1);
327        if (mode == -1)
328            return PHONE_TYPE_NONE;
329        return PhoneFactory.getPhoneType(mode);
330    }
331    //
332    //
333    // Current Network
334    //
335    //
336
337    /**
338     * Returns the alphabetic name of current registered operator.
339     * <p>
340     * Availability: Only when user is registered to a network. Result may be
341     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
342     * on a CDMA network).
343     */
344    public String getNetworkOperatorName() {
345        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ALPHA);
346    }
347
348    /**
349     * Returns the numeric name (MCC+MNC) of current registered operator.
350     * <p>
351     * Availability: Only when user is registered to a network. Result may be
352     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
353     * on a CDMA network).
354     */
355    public String getNetworkOperator() {
356        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_NUMERIC);
357    }
358
359    /**
360     * Returns true if the device is considered roaming on the current
361     * network, for GSM purposes.
362     * <p>
363     * Availability: Only when user registered to a network.
364     */
365    public boolean isNetworkRoaming() {
366        return "true".equals(SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING));
367    }
368
369    /**
370     * Returns the ISO country code equivilent of the current registered
371     * operator's MCC (Mobile Country Code).
372     * <p>
373     * Availability: Only when user is registered to a network. Result may be
374     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
375     * on a CDMA network).
376     */
377    public String getNetworkCountryIso() {
378        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY);
379    }
380
381    /** Network type is unknown */
382    public static final int NETWORK_TYPE_UNKNOWN = 0;
383    /** Current network is GPRS */
384    public static final int NETWORK_TYPE_GPRS = 1;
385    /** Current network is EDGE */
386    public static final int NETWORK_TYPE_EDGE = 2;
387    /** Current network is UMTS */
388    public static final int NETWORK_TYPE_UMTS = 3;
389    /** Current network is CDMA: Either IS95A or IS95B*/
390    /** @hide */
391    public static final int NETWORK_TYPE_CDMA = 4;
392    /** Current network is EVDO revision 0 or revision A*/
393    /** @hide */
394    public static final int NETWORK_TYPE_EVDO_0 = 5;
395    /** @hide */
396    public static final int NETWORK_TYPE_EVDO_A = 6;
397    /** Current network is 1xRTT*/
398    /** @hide */
399    public static final int NETWORK_TYPE_1xRTT = 7;
400    /** Current network is HSDPA */
401    /** @hide */
402    public static final int NETWORK_TYPE_HSDPA = 8;
403    /** Current network is HSUPA */
404    /** @hide */
405    public static final int NETWORK_TYPE_HSUPA = 9;
406    /** Current network is HSPA */
407    /** @hide */
408    public static final int NETWORK_TYPE_HSPA = 10;
409
410    /**
411     * Returns a constant indicating the radio technology (network type)
412     * currently in use on the device.
413     * @return the network type
414     *
415     * @see #NETWORK_TYPE_UNKNOWN
416     * @see #NETWORK_TYPE_GPRS
417     * @see #NETWORK_TYPE_EDGE
418     * @see #NETWORK_TYPE_UMTS
419     * @see #NETWORK_TYPE_HSDPA
420     * @see #NETWORK_TYPE_HSUPA
421     * @see #NETWORK_TYPE_HSPA
422     * @see #NETWORK_TYPE_CDMA
423     * @see #NETWORK_TYPE_EVDO_0
424     * @see #NETWORK_TYPE_EVDO_A
425     * @see #NETWORK_TYPE_1xRTT
426     */
427    public int getNetworkType() {
428        try{
429            ITelephony telephony = getITelephony();
430            if (telephony != null) {
431                return telephony.getNetworkType();
432            } else {
433                // This can happen when the ITelephony interface is not up yet.
434                return NETWORK_TYPE_UNKNOWN;
435            }
436        } catch(RemoteException ex) {
437            // This shouldn't happen in the normal case
438            return NETWORK_TYPE_UNKNOWN;
439        } catch (NullPointerException ex) {
440            // This could happen before phone restarts due to crashing
441            return NETWORK_TYPE_UNKNOWN;
442        }
443    }
444
445    /**
446     * Returns a string representation of the radio technology (network type)
447     * currently in use on the device.
448     * @return the name of the radio technology
449     *
450     * @hide pending API council review
451     */
452    public String getNetworkTypeName() {
453        switch (getNetworkType()) {
454            case NETWORK_TYPE_GPRS:
455                return "GPRS";
456            case NETWORK_TYPE_EDGE:
457                return "EDGE";
458            case NETWORK_TYPE_UMTS:
459                return "UMTS";
460            case NETWORK_TYPE_HSDPA:
461                return "HSDPA";
462            case NETWORK_TYPE_HSUPA:
463                return "HSUPA";
464            case NETWORK_TYPE_HSPA:
465                return "HSPA";
466            case NETWORK_TYPE_CDMA:
467                return "CDMA";
468            case NETWORK_TYPE_EVDO_0:
469                return "CDMA - EvDo rev. 0";
470            case NETWORK_TYPE_EVDO_A:
471                return "CDMA - EvDo rev. A";
472            case NETWORK_TYPE_1xRTT:
473                return "CDMA - 1xRTT";
474            default:
475                return "UNKNOWN";
476        }
477    }
478
479    //
480    //
481    // SIM Card
482    //
483    //
484
485    /** SIM card state: Unknown. Signifies that the SIM is in transition
486     *  between states. For example, when the user inputs the SIM pin
487     *  under PIN_REQUIRED state, a query for sim status returns
488     *  this state before turning to SIM_STATE_READY. */
489    public static final int SIM_STATE_UNKNOWN = 0;
490    /** SIM card state: no SIM card is available in the device */
491    public static final int SIM_STATE_ABSENT = 1;
492    /** SIM card state: Locked: requires the user's SIM PIN to unlock */
493    public static final int SIM_STATE_PIN_REQUIRED = 2;
494    /** SIM card state: Locked: requires the user's SIM PUK to unlock */
495    public static final int SIM_STATE_PUK_REQUIRED = 3;
496    /** SIM card state: Locked: requries a network PIN to unlock */
497    public static final int SIM_STATE_NETWORK_LOCKED = 4;
498    /** SIM card state: Ready */
499    public static final int SIM_STATE_READY = 5;
500
501    /**
502     * @return true if a ICC card is present
503     */
504    public boolean hasIccCard() {
505        try {
506            return getITelephony().hasIccCard();
507        } catch (RemoteException ex) {
508            // Assume no ICC card if remote exception which shouldn't happen
509            return false;
510        } catch (NullPointerException ex) {
511            // This could happen before phone restarts due to crashing
512            return false;
513        }
514    }
515
516    /**
517     * Returns a constant indicating the state of the
518     * device SIM card.
519     *
520     * @see #SIM_STATE_UNKNOWN
521     * @see #SIM_STATE_ABSENT
522     * @see #SIM_STATE_PIN_REQUIRED
523     * @see #SIM_STATE_PUK_REQUIRED
524     * @see #SIM_STATE_NETWORK_LOCKED
525     * @see #SIM_STATE_READY
526     */
527    public int getSimState() {
528        String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE);
529        if ("ABSENT".equals(prop)) {
530            return SIM_STATE_ABSENT;
531        }
532        else if ("PIN_REQUIRED".equals(prop)) {
533            return SIM_STATE_PIN_REQUIRED;
534        }
535        else if ("PUK_REQUIRED".equals(prop)) {
536            return SIM_STATE_PUK_REQUIRED;
537        }
538        else if ("NETWORK_LOCKED".equals(prop)) {
539            return SIM_STATE_NETWORK_LOCKED;
540        }
541        else if ("READY".equals(prop)) {
542            return SIM_STATE_READY;
543        }
544        else {
545            return SIM_STATE_UNKNOWN;
546        }
547    }
548
549    /**
550     * Returns the MCC+MNC (mobile country code + mobile network code) of the
551     * provider of the SIM. 5 or 6 decimal digits.
552     * <p>
553     * Availability: SIM state must be {@link #SIM_STATE_READY}
554     *
555     * @see #getSimState
556     */
557    public String getSimOperator() {
558        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC);
559    }
560
561    /**
562     * Returns the Service Provider Name (SPN).
563     * <p>
564     * Availability: SIM state must be {@link #SIM_STATE_READY}
565     *
566     * @see #getSimState
567     */
568    public String getSimOperatorName() {
569        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA);
570    }
571
572    /**
573     * Returns the ISO country code equivalent for the SIM provider's country code.
574     */
575    public String getSimCountryIso() {
576        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY);
577    }
578
579    /**
580     * Returns the serial number of the SIM, if applicable. Return null if it is
581     * unavailable.
582     * <p>
583     * Requires Permission:
584     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
585     */
586    public String getSimSerialNumber() {
587        try {
588            return getSubscriberInfo().getIccSerialNumber();
589        } catch (RemoteException ex) {
590            return null;
591        } catch (NullPointerException ex) {
592            // This could happen before phone restarts due to crashing
593            return null;
594        }
595    }
596
597    //
598    //
599    // Subscriber Info
600    //
601    //
602
603    /**
604     * Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
605     * Return null if it is unavailable.
606     * <p>
607     * Requires Permission:
608     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
609     */
610    public String getSubscriberId() {
611        try {
612            return getSubscriberInfo().getSubscriberId();
613        } catch (RemoteException ex) {
614            return null;
615        } catch (NullPointerException ex) {
616            // This could happen before phone restarts due to crashing
617            return null;
618        }
619    }
620
621    /**
622     * Returns the phone number string for line 1, for example, the MSISDN
623     * for a GSM phone. Return null if it is unavailable.
624     * <p>
625     * Requires Permission:
626     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
627     */
628    public String getLine1Number() {
629        try {
630            return getSubscriberInfo().getLine1Number();
631        } catch (RemoteException ex) {
632            return null;
633        } catch (NullPointerException ex) {
634            // This could happen before phone restarts due to crashing
635            return null;
636        }
637    }
638
639    /**
640     * Returns the alphabetic identifier associated with the line 1 number.
641     * Return null if it is unavailable.
642     * <p>
643     * Requires Permission:
644     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
645     * @hide
646     * nobody seems to call this.
647     */
648    public String getLine1AlphaTag() {
649        try {
650            return getSubscriberInfo().getLine1AlphaTag();
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 voice mail number. Return null if it is unavailable.
661     * <p>
662     * Requires Permission:
663     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
664     */
665    public String getVoiceMailNumber() {
666        try {
667            return getSubscriberInfo().getVoiceMailNumber();
668        } catch (RemoteException ex) {
669            return null;
670        } catch (NullPointerException ex) {
671            // This could happen before phone restarts due to crashing
672            return null;
673        }
674    }
675
676    /**
677     * Returns the voice mail count. Return 0 if unavailable.
678     * <p>
679     * Requires Permission:
680     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
681     * @hide
682     */
683    public int getVoiceMessageCount() {
684        try {
685            return getITelephony().getVoiceMessageCount();
686        } catch (RemoteException ex) {
687            return 0;
688        } catch (NullPointerException ex) {
689            // This could happen before phone restarts due to crashing
690            return 0;
691        }
692    }
693
694    /**
695     * Retrieves the alphabetic identifier associated with the voice
696     * mail number.
697     * <p>
698     * Requires Permission:
699     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
700     */
701    public String getVoiceMailAlphaTag() {
702        try {
703            return getSubscriberInfo().getVoiceMailAlphaTag();
704        } catch (RemoteException ex) {
705            return null;
706        } catch (NullPointerException ex) {
707            // This could happen before phone restarts due to crashing
708            return null;
709        }
710    }
711
712    private IPhoneSubInfo getSubscriberInfo() {
713        // get it each time because that process crashes a lot
714        return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
715    }
716
717
718    /** Device call state: No activity. */
719    public static final int CALL_STATE_IDLE = 0;
720    /** Device call state: Ringing. A new call arrived and is
721     *  ringing or waiting. In the latter case, another call is
722     *  already active. */
723    public static final int CALL_STATE_RINGING = 1;
724    /** Device call state: Off-hook. At least one call exists
725      * that is dialing, active, or on hold, and no calls are ringing
726      * or waiting. */
727    public static final int CALL_STATE_OFFHOOK = 2;
728
729    /**
730     * Returns a constant indicating the call state (cellular) on the device.
731     */
732    public int getCallState() {
733        try {
734            return getITelephony().getCallState();
735        } catch (RemoteException ex) {
736            // the phone process is restarting.
737            return CALL_STATE_IDLE;
738        } catch (NullPointerException ex) {
739          // the phone process is restarting.
740          return CALL_STATE_IDLE;
741      }
742    }
743
744    /** Data connection activity: No traffic. */
745    public static final int DATA_ACTIVITY_NONE = 0x00000000;
746    /** Data connection activity: Currently receiving IP PPP traffic. */
747    public static final int DATA_ACTIVITY_IN = 0x00000001;
748    /** Data connection activity: Currently sending IP PPP traffic. */
749    public static final int DATA_ACTIVITY_OUT = 0x00000002;
750    /** Data connection activity: Currently both sending and receiving
751     *  IP PPP traffic. */
752    public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT;
753    /**
754     * Data connection is active, but physical link is down
755     * @hide
756     */
757    public static final int DATA_ACTIVITY_DORMANT = 0x00000004;
758
759    /**
760     * Returns a constant indicating the type of activity on a data connection
761     * (cellular).
762     *
763     * @see #DATA_ACTIVITY_NONE
764     * @see #DATA_ACTIVITY_IN
765     * @see #DATA_ACTIVITY_OUT
766     * @see #DATA_ACTIVITY_INOUT
767     * @see #DATA_ACTIVITY_DORMANT
768     */
769    public int getDataActivity() {
770        try {
771            return getITelephony().getDataActivity();
772        } catch (RemoteException ex) {
773            // the phone process is restarting.
774            return DATA_ACTIVITY_NONE;
775        } catch (NullPointerException ex) {
776          // the phone process is restarting.
777          return DATA_ACTIVITY_NONE;
778      }
779    }
780
781    /** Data connection state: Disconnected. IP traffic not available. */
782    public static final int DATA_DISCONNECTED   = 0;
783    /** Data connection state: Currently setting up a data connection. */
784    public static final int DATA_CONNECTING     = 1;
785    /** Data connection state: Connected. IP traffic should be available. */
786    public static final int DATA_CONNECTED      = 2;
787    /** Data connection state: Suspended. The connection is up, but IP
788     * traffic is temporarily unavailable. For example, in a 2G network,
789     * data activity may be suspended when a voice call arrives. */
790    public static final int DATA_SUSPENDED      = 3;
791
792    /**
793     * Returns a constant indicating the current data connection state
794     * (cellular).
795     *
796     * @see #DATA_DISCONNECTED
797     * @see #DATA_CONNECTING
798     * @see #DATA_CONNECTED
799     * @see #DATA_SUSPENDED
800     */
801    public int getDataState() {
802        try {
803            return getITelephony().getDataState();
804        } catch (RemoteException ex) {
805            // the phone process is restarting.
806            return DATA_DISCONNECTED;
807        } catch (NullPointerException ex) {
808            return DATA_DISCONNECTED;
809        }
810    }
811
812    private ITelephony getITelephony() {
813        return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
814    }
815
816    //
817    //
818    // PhoneStateListener
819    //
820    //
821
822    /**
823     * Registers a listener object to receive notification of changes
824     * in specified telephony states.
825     * <p>
826     * To register a listener, pass a {@link PhoneStateListener}
827     * and specify at least one telephony state of interest in
828     * the events argument.
829     *
830     * At registration, and when a specified telephony state
831     * changes, the telephony manager invokes the appropriate
832     * callback method on the listener object and passes the
833     * current (udpated) values.
834     * <p>
835     * To unregister a listener, pass the listener object and set the
836     * events argument to
837     * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0).
838     *
839     * @param listener The {@link PhoneStateListener} object to register
840     *                 (or unregister)
841     * @param events The telephony state(s) of interest to the listener,
842     *               as a bitwise-OR combination of {@link PhoneStateListener}
843     *               LISTEN_ flags.
844     */
845    public void listen(PhoneStateListener listener, int events) {
846        String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>";
847        try {
848            Boolean notifyNow = (getITelephony() != null);
849            mRegistry.listen(pkgForDebug, listener.callback, events, notifyNow);
850        } catch (RemoteException ex) {
851            // system process dead
852        } catch (NullPointerException ex) {
853            // system process dead
854        }
855    }
856
857    /**
858     * Returns the CDMA ERI icon index to display
859     *
860     * @hide
861     */
862    public int getCdmaEriIconIndex() {
863        try {
864            return getITelephony().getCdmaEriIconIndex();
865        } catch (RemoteException ex) {
866            // the phone process is restarting.
867            return -1;
868        } catch (NullPointerException ex) {
869            return -1;
870        }
871    }
872
873    /**
874     * Returns the CDMA ERI icon mode,
875     * 0 - ON
876     * 1 - FLASHING
877     *
878     * @hide
879     */
880    public int getCdmaEriIconMode() {
881        try {
882            return getITelephony().getCdmaEriIconMode();
883        } catch (RemoteException ex) {
884            // the phone process is restarting.
885            return -1;
886        } catch (NullPointerException ex) {
887            return -1;
888        }
889    }
890
891    /**
892     * Returns the CDMA ERI text,
893     *
894     * @hide
895     */
896    public String getCdmaEriText() {
897        try {
898            return getITelephony().getCdmaEriText();
899        } catch (RemoteException ex) {
900            // the phone process is restarting.
901            return null;
902        } catch (NullPointerException ex) {
903            return null;
904        }
905    }
906}
907