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