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