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