TelephonyManager.java revision b690ac3b27d9b782a23a5f9a82bb3421e88716e3
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.PhoneConstants;
32import com.android.internal.telephony.RILConstants;
33import com.android.internal.telephony.TelephonyProperties;
34
35import java.io.FileInputStream;
36import java.io.IOException;
37import java.util.List;
38import java.util.regex.Matcher;
39import java.util.regex.Pattern;
40
41/**
42 * Provides access to information about the telephony services on
43 * the device. Applications can use the methods in this class to
44 * determine telephony services and states, as well as to access some
45 * types of subscriber information. Applications can also register
46 * a listener to receive notification of telephony state changes.
47 * <p>
48 * You do not instantiate this class directly; instead, you retrieve
49 * a reference to an instance through
50 * {@link android.content.Context#getSystemService
51 * Context.getSystemService(Context.TELEPHONY_SERVICE)}.
52 * <p>
53 * Note that access to some telephony information is
54 * permission-protected. Your application cannot access the protected
55 * information unless it has the appropriate permissions declared in
56 * its manifest file. Where permissions apply, they are noted in the
57 * the methods through which you access the protected information.
58 */
59public class TelephonyManager {
60    private static final String TAG = "TelephonyManager";
61
62    private static Context sContext;
63    private static ITelephonyRegistry sRegistry;
64
65    /** @hide */
66    public TelephonyManager(Context context) {
67        if (sContext == null) {
68            Context appContext = context.getApplicationContext();
69            if (appContext != null) {
70                sContext = appContext;
71            } else {
72                sContext = context;
73            }
74
75            sRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
76                    "telephony.registry"));
77        }
78    }
79
80    /** @hide */
81    private TelephonyManager() {
82    }
83
84    private static TelephonyManager sInstance = new TelephonyManager();
85
86    /** @hide
87    /* @deprecated - use getSystemService as described above */
88    public static TelephonyManager getDefault() {
89        return sInstance;
90    }
91
92    /** {@hide} */
93    public static TelephonyManager from(Context context) {
94        return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
95    }
96
97    //
98    // Broadcast Intent actions
99    //
100
101    /**
102     * Broadcast intent action indicating that the call state (cellular)
103     * on the device has changed.
104     *
105     * <p>
106     * The {@link #EXTRA_STATE} extra indicates the new call state.
107     * If the new state is RINGING, a second extra
108     * {@link #EXTRA_INCOMING_NUMBER} provides the incoming phone number as
109     * a String.
110     *
111     * <p class="note">
112     * Requires the READ_PHONE_STATE permission.
113     *
114     * <p class="note">
115     * This was a {@link android.content.Context#sendStickyBroadcast sticky}
116     * broadcast in version 1.0, but it is no longer sticky.
117     * Instead, use {@link #getCallState} to synchronously query the current call state.
118     *
119     * @see #EXTRA_STATE
120     * @see #EXTRA_INCOMING_NUMBER
121     * @see #getCallState
122     */
123    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
124    public static final String ACTION_PHONE_STATE_CHANGED =
125            "android.intent.action.PHONE_STATE";
126
127    /**
128     * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
129     * for a String containing the new call state.
130     *
131     * @see #EXTRA_STATE_IDLE
132     * @see #EXTRA_STATE_RINGING
133     * @see #EXTRA_STATE_OFFHOOK
134     *
135     * <p class="note">
136     * Retrieve with
137     * {@link android.content.Intent#getStringExtra(String)}.
138     */
139    public static final String EXTRA_STATE = PhoneConstants.STATE_KEY;
140
141    /**
142     * Value used with {@link #EXTRA_STATE} corresponding to
143     * {@link #CALL_STATE_IDLE}.
144     */
145    public static final String EXTRA_STATE_IDLE = PhoneConstants.State.IDLE.toString();
146
147    /**
148     * Value used with {@link #EXTRA_STATE} corresponding to
149     * {@link #CALL_STATE_RINGING}.
150     */
151    public static final String EXTRA_STATE_RINGING = PhoneConstants.State.RINGING.toString();
152
153    /**
154     * Value used with {@link #EXTRA_STATE} corresponding to
155     * {@link #CALL_STATE_OFFHOOK}.
156     */
157    public static final String EXTRA_STATE_OFFHOOK = PhoneConstants.State.OFFHOOK.toString();
158
159    /**
160     * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
161     * for a String containing the incoming phone number.
162     * Only valid when the new call state is RINGING.
163     *
164     * <p class="note">
165     * Retrieve with
166     * {@link android.content.Intent#getStringExtra(String)}.
167     */
168    public static final String EXTRA_INCOMING_NUMBER = "incoming_number";
169
170
171    //
172    //
173    // Device Info
174    //
175    //
176
177    /**
178     * Returns the software version number for the device, for example,
179     * the IMEI/SV for GSM phones. Return null if the software version is
180     * not available.
181     *
182     * <p>Requires Permission:
183     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
184     */
185    public String getDeviceSoftwareVersion() {
186        try {
187            return getSubscriberInfo().getDeviceSvn();
188        } catch (RemoteException ex) {
189            return null;
190        } catch (NullPointerException ex) {
191            return null;
192        }
193    }
194
195    /**
196     * Returns the unique device ID, for example, the IMEI for GSM and the MEID
197     * or ESN for CDMA phones. Return null if device ID is not available.
198     *
199     * <p>Requires Permission:
200     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
201     */
202    public String getDeviceId() {
203        try {
204            return getSubscriberInfo().getDeviceId();
205        } catch (RemoteException ex) {
206            return null;
207        } catch (NullPointerException ex) {
208            return null;
209        }
210    }
211
212    /**
213     * Returns the current location of the device.
214     * Return null if current location is not available.
215     *
216     * <p>Requires Permission:
217     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} or
218     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION}.
219     */
220    public CellLocation getCellLocation() {
221        try {
222            Bundle bundle = getITelephony().getCellLocation();
223            if (bundle.isEmpty()) return null;
224            CellLocation cl = CellLocation.newFromBundle(bundle);
225            if (cl.isEmpty())
226                return null;
227            return cl;
228        } catch (RemoteException ex) {
229            return null;
230        } catch (NullPointerException ex) {
231            return null;
232        }
233    }
234
235    /**
236     * Enables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
237     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
238     *
239     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
240     * CONTROL_LOCATION_UPDATES}
241     *
242     * @hide
243     */
244    public void enableLocationUpdates() {
245        try {
246            getITelephony().enableLocationUpdates();
247        } catch (RemoteException ex) {
248        } catch (NullPointerException ex) {
249        }
250    }
251
252    /**
253     * Disables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
254     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
255     *
256     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
257     * CONTROL_LOCATION_UPDATES}
258     *
259     * @hide
260     */
261    public void disableLocationUpdates() {
262        try {
263            getITelephony().disableLocationUpdates();
264        } catch (RemoteException ex) {
265        } catch (NullPointerException ex) {
266        }
267    }
268
269    /**
270     * Returns the neighboring cell information of the device.
271     *
272     * @return List of NeighboringCellInfo or null if info unavailable.
273     *
274     * <p>Requires Permission:
275     * (@link android.Manifest.permission#ACCESS_COARSE_UPDATES}
276     */
277    public List<NeighboringCellInfo> getNeighboringCellInfo() {
278        try {
279            return getITelephony().getNeighboringCellInfo();
280        } catch (RemoteException ex) {
281            return null;
282        } catch (NullPointerException ex) {
283            return null;
284        }
285    }
286
287    /** No phone radio. */
288    public static final int PHONE_TYPE_NONE = PhoneConstants.PHONE_TYPE_NONE;
289    /** Phone radio is GSM. */
290    public static final int PHONE_TYPE_GSM = PhoneConstants.PHONE_TYPE_GSM;
291    /** Phone radio is CDMA. */
292    public static final int PHONE_TYPE_CDMA = PhoneConstants.PHONE_TYPE_CDMA;
293    /** Phone is via SIP. */
294    public static final int PHONE_TYPE_SIP = PhoneConstants.PHONE_TYPE_SIP;
295
296    /**
297     * Returns the current phone type.
298     * TODO: This is a last minute change and hence hidden.
299     *
300     * @see #PHONE_TYPE_NONE
301     * @see #PHONE_TYPE_GSM
302     * @see #PHONE_TYPE_CDMA
303     * @see #PHONE_TYPE_SIP
304     *
305     * {@hide}
306     */
307    public int getCurrentPhoneType() {
308        try{
309            ITelephony telephony = getITelephony();
310            if (telephony != null) {
311                return telephony.getActivePhoneType();
312            } else {
313                // This can happen when the ITelephony interface is not up yet.
314                return getPhoneTypeFromProperty();
315            }
316        } catch (RemoteException ex) {
317            // This shouldn't happen in the normal case, as a backup we
318            // read from the system property.
319            return getPhoneTypeFromProperty();
320        } catch (NullPointerException ex) {
321            // This shouldn't happen in the normal case, as a backup we
322            // read from the system property.
323            return getPhoneTypeFromProperty();
324        }
325    }
326
327    /**
328     * Returns a constant indicating the device phone type.  This
329     * indicates the type of radio used to transmit voice calls.
330     *
331     * @see #PHONE_TYPE_NONE
332     * @see #PHONE_TYPE_GSM
333     * @see #PHONE_TYPE_CDMA
334     * @see #PHONE_TYPE_SIP
335     */
336    public int getPhoneType() {
337        if (!isVoiceCapable()) {
338            return PHONE_TYPE_NONE;
339        }
340        return getCurrentPhoneType();
341    }
342
343    private int getPhoneTypeFromProperty() {
344        int type =
345            SystemProperties.getInt(TelephonyProperties.CURRENT_ACTIVE_PHONE,
346                    getPhoneTypeFromNetworkType());
347        return type;
348    }
349
350    private int getPhoneTypeFromNetworkType() {
351        // When the system property CURRENT_ACTIVE_PHONE, has not been set,
352        // use the system property for default network type.
353        // This is a fail safe, and can only happen at first boot.
354        int mode = SystemProperties.getInt("ro.telephony.default_network", -1);
355        if (mode == -1)
356            return PHONE_TYPE_NONE;
357        return getPhoneType(mode);
358    }
359
360    /**
361     * This function returns the type of the phone, depending
362     * on the network mode.
363     *
364     * @param network mode
365     * @return Phone Type
366     *
367     * @hide
368     */
369    public static int getPhoneType(int networkMode) {
370        switch(networkMode) {
371        case RILConstants.NETWORK_MODE_CDMA:
372        case RILConstants.NETWORK_MODE_CDMA_NO_EVDO:
373        case RILConstants.NETWORK_MODE_EVDO_NO_CDMA:
374            return PhoneConstants.PHONE_TYPE_CDMA;
375
376        case RILConstants.NETWORK_MODE_WCDMA_PREF:
377        case RILConstants.NETWORK_MODE_GSM_ONLY:
378        case RILConstants.NETWORK_MODE_WCDMA_ONLY:
379        case RILConstants.NETWORK_MODE_GSM_UMTS:
380            return PhoneConstants.PHONE_TYPE_GSM;
381
382        // Use CDMA Phone for the global mode including CDMA
383        case RILConstants.NETWORK_MODE_GLOBAL:
384        case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO:
385        case RILConstants.NETWORK_MODE_LTE_CMDA_EVDO_GSM_WCDMA:
386            return PhoneConstants.PHONE_TYPE_CDMA;
387
388        case RILConstants.NETWORK_MODE_LTE_ONLY:
389            if (getLteOnCdmaModeStatic() == PhoneConstants.LTE_ON_CDMA_TRUE) {
390                return PhoneConstants.PHONE_TYPE_CDMA;
391            } else {
392                return PhoneConstants.PHONE_TYPE_GSM;
393            }
394        default:
395            return PhoneConstants.PHONE_TYPE_GSM;
396        }
397    }
398
399    /**
400     * The contents of the /proc/cmdline file
401     */
402    private static String getProcCmdLine()
403    {
404        String cmdline = "";
405        FileInputStream is = null;
406        try {
407            is = new FileInputStream("/proc/cmdline");
408            byte [] buffer = new byte[2048];
409            int count = is.read(buffer);
410            if (count > 0) {
411                cmdline = new String(buffer, 0, count);
412            }
413        } catch (IOException e) {
414            Log.d(TAG, "No /proc/cmdline exception=" + e);
415        } finally {
416            if (is != null) {
417                try {
418                    is.close();
419                } catch (IOException e) {
420                }
421            }
422        }
423        Log.d(TAG, "/proc/cmdline=" + cmdline);
424        return cmdline;
425    }
426
427    /** Kernel command line */
428    private static final String sKernelCmdLine = getProcCmdLine();
429
430    /** Pattern for selecting the product type from the kernel command line */
431    private static final Pattern sProductTypePattern =
432        Pattern.compile("\\sproduct_type\\s*=\\s*(\\w+)");
433
434    /** The ProductType used for LTE on CDMA devices */
435    private static final String sLteOnCdmaProductType =
436        SystemProperties.get(TelephonyProperties.PROPERTY_LTE_ON_CDMA_PRODUCT_TYPE, "");
437
438    /**
439     * Return if the current radio is LTE on CDMA. This
440     * is a tri-state return value as for a period of time
441     * the mode may be unknown.
442     *
443     * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE}
444     * or {@link PhoneConstants#LTE_ON_CDMA_TRUE}
445     *
446     * @hide
447     */
448    public static int getLteOnCdmaModeStatic() {
449        int retVal;
450        int curVal;
451        String productType = "";
452
453        curVal = SystemProperties.getInt(TelephonyProperties.PROPERTY_LTE_ON_CDMA_DEVICE,
454                    PhoneConstants.LTE_ON_CDMA_UNKNOWN);
455        retVal = curVal;
456        if (retVal == PhoneConstants.LTE_ON_CDMA_UNKNOWN) {
457            Matcher matcher = sProductTypePattern.matcher(sKernelCmdLine);
458            if (matcher.find()) {
459                productType = matcher.group(1);
460                if (sLteOnCdmaProductType.equals(productType)) {
461                    retVal = PhoneConstants.LTE_ON_CDMA_TRUE;
462                } else {
463                    retVal = PhoneConstants.LTE_ON_CDMA_FALSE;
464                }
465            } else {
466                retVal = PhoneConstants.LTE_ON_CDMA_FALSE;
467            }
468        }
469
470        Log.d(TAG, "getLteOnCdmaMode=" + retVal + " curVal=" + curVal +
471                " product_type='" + productType +
472                "' lteOnCdmaProductType='" + sLteOnCdmaProductType + "'");
473        return retVal;
474    }
475
476    //
477    //
478    // Current Network
479    //
480    //
481
482    /**
483     * Returns the alphabetic name of current registered operator.
484     * <p>
485     * Availability: Only when user is registered to a network. Result may be
486     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
487     * on a CDMA network).
488     */
489    public String getNetworkOperatorName() {
490        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ALPHA);
491    }
492
493    /**
494     * Returns the numeric name (MCC+MNC) of current registered operator.
495     * <p>
496     * Availability: Only when user is registered to a network. Result may be
497     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
498     * on a CDMA network).
499     */
500    public String getNetworkOperator() {
501        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_NUMERIC);
502    }
503
504    /**
505     * Returns true if the device is considered roaming on the current
506     * network, for GSM purposes.
507     * <p>
508     * Availability: Only when user registered to a network.
509     */
510    public boolean isNetworkRoaming() {
511        return "true".equals(SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING));
512    }
513
514    /**
515     * Returns the ISO country code equivalent of the current registered
516     * operator's MCC (Mobile Country Code).
517     * <p>
518     * Availability: Only when user is registered to a network. Result may be
519     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
520     * on a CDMA network).
521     */
522    public String getNetworkCountryIso() {
523        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY);
524    }
525
526    /** Network type is unknown */
527    public static final int NETWORK_TYPE_UNKNOWN = 0;
528    /** Current network is GPRS */
529    public static final int NETWORK_TYPE_GPRS = 1;
530    /** Current network is EDGE */
531    public static final int NETWORK_TYPE_EDGE = 2;
532    /** Current network is UMTS */
533    public static final int NETWORK_TYPE_UMTS = 3;
534    /** Current network is CDMA: Either IS95A or IS95B*/
535    public static final int NETWORK_TYPE_CDMA = 4;
536    /** Current network is EVDO revision 0*/
537    public static final int NETWORK_TYPE_EVDO_0 = 5;
538    /** Current network is EVDO revision A*/
539    public static final int NETWORK_TYPE_EVDO_A = 6;
540    /** Current network is 1xRTT*/
541    public static final int NETWORK_TYPE_1xRTT = 7;
542    /** Current network is HSDPA */
543    public static final int NETWORK_TYPE_HSDPA = 8;
544    /** Current network is HSUPA */
545    public static final int NETWORK_TYPE_HSUPA = 9;
546    /** Current network is HSPA */
547    public static final int NETWORK_TYPE_HSPA = 10;
548    /** Current network is iDen */
549    public static final int NETWORK_TYPE_IDEN = 11;
550    /** Current network is EVDO revision B*/
551    public static final int NETWORK_TYPE_EVDO_B = 12;
552    /** Current network is LTE */
553    public static final int NETWORK_TYPE_LTE = 13;
554    /** Current network is eHRPD */
555    public static final int NETWORK_TYPE_EHRPD = 14;
556    /** Current network is HSPA+ */
557    public static final int NETWORK_TYPE_HSPAP = 15;
558
559    /**
560     * Returns the NETWORK_TYPE_xxxx for data transmission
561     *
562     * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
563     */
564    public int getNetworkType() {
565        return getDataNetworkType();
566    }
567
568    /**
569     * Returns a constant indicating the radio technology (network type)
570     * currently in use on the device for data transmission.
571     * @return the network type
572     *
573     * @see #NETWORK_TYPE_UNKNOWN
574     * @see #NETWORK_TYPE_GPRS
575     * @see #NETWORK_TYPE_EDGE
576     * @see #NETWORK_TYPE_UMTS
577     * @see #NETWORK_TYPE_HSDPA
578     * @see #NETWORK_TYPE_HSUPA
579     * @see #NETWORK_TYPE_HSPA
580     * @see #NETWORK_TYPE_CDMA
581     * @see #NETWORK_TYPE_EVDO_0
582     * @see #NETWORK_TYPE_EVDO_A
583     * @see #NETWORK_TYPE_EVDO_B
584     * @see #NETWORK_TYPE_1xRTT
585     * @see #NETWORK_TYPE_IDEN
586     * @see #NETWORK_TYPE_LTE
587     * @see #NETWORK_TYPE_EHRPD
588     * @see #NETWORK_TYPE_HSPAP
589     */
590    public int getDataNetworkType() {
591        try{
592            ITelephony telephony = getITelephony();
593            if (telephony != null) {
594                return telephony.getDataNetworkType();
595            } else {
596                // This can happen when the ITelephony interface is not up yet.
597                return NETWORK_TYPE_UNKNOWN;
598            }
599        } catch(RemoteException ex) {
600            // This shouldn't happen in the normal case
601            return NETWORK_TYPE_UNKNOWN;
602        } catch (NullPointerException ex) {
603            // This could happen before phone restarts due to crashing
604            return NETWORK_TYPE_UNKNOWN;
605        }
606    }
607
608    /**
609     * Returns the NETWORK_TYPE_xxxx for voice
610     */
611    public int getVoiceNetworkType() {
612        try{
613            ITelephony telephony = getITelephony();
614            if (telephony != null) {
615                return telephony.getVoiceNetworkType();
616            } else {
617                // This can happen when the ITelephony interface is not up yet.
618                return NETWORK_TYPE_UNKNOWN;
619            }
620        } catch(RemoteException ex) {
621            // This shouldn't happen in the normal case
622            return NETWORK_TYPE_UNKNOWN;
623        } catch (NullPointerException ex) {
624            // This could happen before phone restarts due to crashing
625            return NETWORK_TYPE_UNKNOWN;
626        }
627    }
628
629    /** Unknown network class. {@hide} */
630    public static final int NETWORK_CLASS_UNKNOWN = 0;
631    /** Class of broadly defined "2G" networks. {@hide} */
632    public static final int NETWORK_CLASS_2_G = 1;
633    /** Class of broadly defined "3G" networks. {@hide} */
634    public static final int NETWORK_CLASS_3_G = 2;
635    /** Class of broadly defined "4G" networks. {@hide} */
636    public static final int NETWORK_CLASS_4_G = 3;
637
638    /**
639     * Return general class of network type, such as "3G" or "4G". In cases
640     * where classification is contentious, this method is conservative.
641     *
642     * @hide
643     */
644    public static int getNetworkClass(int networkType) {
645        switch (networkType) {
646            case NETWORK_TYPE_GPRS:
647            case NETWORK_TYPE_EDGE:
648            case NETWORK_TYPE_CDMA:
649            case NETWORK_TYPE_1xRTT:
650            case NETWORK_TYPE_IDEN:
651                return NETWORK_CLASS_2_G;
652            case NETWORK_TYPE_UMTS:
653            case NETWORK_TYPE_EVDO_0:
654            case NETWORK_TYPE_EVDO_A:
655            case NETWORK_TYPE_HSDPA:
656            case NETWORK_TYPE_HSUPA:
657            case NETWORK_TYPE_HSPA:
658            case NETWORK_TYPE_EVDO_B:
659            case NETWORK_TYPE_EHRPD:
660            case NETWORK_TYPE_HSPAP:
661                return NETWORK_CLASS_3_G;
662            case NETWORK_TYPE_LTE:
663                return NETWORK_CLASS_4_G;
664            default:
665                return NETWORK_CLASS_UNKNOWN;
666        }
667    }
668
669    /**
670     * Returns a string representation of the radio technology (network type)
671     * currently in use on the device.
672     * @return the name of the radio technology
673     *
674     * @hide pending API council review
675     */
676    public String getNetworkTypeName() {
677        return getNetworkTypeName(getNetworkType());
678    }
679
680    /** {@hide} */
681    public static String getNetworkTypeName(int type) {
682        switch (type) {
683            case NETWORK_TYPE_GPRS:
684                return "GPRS";
685            case NETWORK_TYPE_EDGE:
686                return "EDGE";
687            case NETWORK_TYPE_UMTS:
688                return "UMTS";
689            case NETWORK_TYPE_HSDPA:
690                return "HSDPA";
691            case NETWORK_TYPE_HSUPA:
692                return "HSUPA";
693            case NETWORK_TYPE_HSPA:
694                return "HSPA";
695            case NETWORK_TYPE_CDMA:
696                return "CDMA";
697            case NETWORK_TYPE_EVDO_0:
698                return "CDMA - EvDo rev. 0";
699            case NETWORK_TYPE_EVDO_A:
700                return "CDMA - EvDo rev. A";
701            case NETWORK_TYPE_EVDO_B:
702                return "CDMA - EvDo rev. B";
703            case NETWORK_TYPE_1xRTT:
704                return "CDMA - 1xRTT";
705            case NETWORK_TYPE_LTE:
706                return "LTE";
707            case NETWORK_TYPE_EHRPD:
708                return "CDMA - eHRPD";
709            case NETWORK_TYPE_IDEN:
710                return "iDEN";
711            case NETWORK_TYPE_HSPAP:
712                return "HSPA+";
713            default:
714                return "UNKNOWN";
715        }
716    }
717
718    //
719    //
720    // SIM Card
721    //
722    //
723
724    /** SIM card state: Unknown. Signifies that the SIM is in transition
725     *  between states. For example, when the user inputs the SIM pin
726     *  under PIN_REQUIRED state, a query for sim status returns
727     *  this state before turning to SIM_STATE_READY. */
728    public static final int SIM_STATE_UNKNOWN = 0;
729    /** SIM card state: no SIM card is available in the device */
730    public static final int SIM_STATE_ABSENT = 1;
731    /** SIM card state: Locked: requires the user's SIM PIN to unlock */
732    public static final int SIM_STATE_PIN_REQUIRED = 2;
733    /** SIM card state: Locked: requires the user's SIM PUK to unlock */
734    public static final int SIM_STATE_PUK_REQUIRED = 3;
735    /** SIM card state: Locked: requries a network PIN to unlock */
736    public static final int SIM_STATE_NETWORK_LOCKED = 4;
737    /** SIM card state: Ready */
738    public static final int SIM_STATE_READY = 5;
739
740    /**
741     * @return true if a ICC card is present
742     */
743    public boolean hasIccCard() {
744        try {
745            return getITelephony().hasIccCard();
746        } catch (RemoteException ex) {
747            // Assume no ICC card if remote exception which shouldn't happen
748            return false;
749        } catch (NullPointerException ex) {
750            // This could happen before phone restarts due to crashing
751            return false;
752        }
753    }
754
755    /**
756     * Returns a constant indicating the state of the
757     * device SIM card.
758     *
759     * @see #SIM_STATE_UNKNOWN
760     * @see #SIM_STATE_ABSENT
761     * @see #SIM_STATE_PIN_REQUIRED
762     * @see #SIM_STATE_PUK_REQUIRED
763     * @see #SIM_STATE_NETWORK_LOCKED
764     * @see #SIM_STATE_READY
765     */
766    public int getSimState() {
767        String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE);
768        if ("ABSENT".equals(prop)) {
769            return SIM_STATE_ABSENT;
770        }
771        else if ("PIN_REQUIRED".equals(prop)) {
772            return SIM_STATE_PIN_REQUIRED;
773        }
774        else if ("PUK_REQUIRED".equals(prop)) {
775            return SIM_STATE_PUK_REQUIRED;
776        }
777        else if ("NETWORK_LOCKED".equals(prop)) {
778            return SIM_STATE_NETWORK_LOCKED;
779        }
780        else if ("READY".equals(prop)) {
781            return SIM_STATE_READY;
782        }
783        else {
784            return SIM_STATE_UNKNOWN;
785        }
786    }
787
788    /**
789     * Returns the MCC+MNC (mobile country code + mobile network code) of the
790     * provider of the SIM. 5 or 6 decimal digits.
791     * <p>
792     * Availability: SIM state must be {@link #SIM_STATE_READY}
793     *
794     * @see #getSimState
795     */
796    public String getSimOperator() {
797        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC);
798    }
799
800    /**
801     * Returns the Service Provider Name (SPN).
802     * <p>
803     * Availability: SIM state must be {@link #SIM_STATE_READY}
804     *
805     * @see #getSimState
806     */
807    public String getSimOperatorName() {
808        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA);
809    }
810
811    /**
812     * Returns the ISO country code equivalent for the SIM provider's country code.
813     */
814    public String getSimCountryIso() {
815        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY);
816    }
817
818    /**
819     * Returns the serial number of the SIM, if applicable. Return null if it is
820     * unavailable.
821     * <p>
822     * Requires Permission:
823     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
824     */
825    public String getSimSerialNumber() {
826        try {
827            return getSubscriberInfo().getIccSerialNumber();
828        } catch (RemoteException ex) {
829            return null;
830        } catch (NullPointerException ex) {
831            // This could happen before phone restarts due to crashing
832            return null;
833        }
834    }
835
836    /**
837     * Return if the current radio is LTE on CDMA. This
838     * is a tri-state return value as for a period of time
839     * the mode may be unknown.
840     *
841     * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
842     * or {@link Phone#LTE_ON_CDMA_TRUE}
843     *
844     * @hide
845     */
846    public int getLteOnCdmaMode() {
847        try {
848            return getITelephony().getLteOnCdmaMode();
849        } catch (RemoteException ex) {
850            // Assume no ICC card if remote exception which shouldn't happen
851            return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
852        } catch (NullPointerException ex) {
853            // This could happen before phone restarts due to crashing
854            return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
855        }
856    }
857
858    //
859    //
860    // Subscriber Info
861    //
862    //
863
864    /**
865     * Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
866     * Return null if it is unavailable.
867     * <p>
868     * Requires Permission:
869     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
870     */
871    public String getSubscriberId() {
872        try {
873            return getSubscriberInfo().getSubscriberId();
874        } catch (RemoteException ex) {
875            return null;
876        } catch (NullPointerException ex) {
877            // This could happen before phone restarts due to crashing
878            return null;
879        }
880    }
881
882    /**
883     * Returns the phone number string for line 1, for example, the MSISDN
884     * for a GSM phone. Return null if it is unavailable.
885     * <p>
886     * Requires Permission:
887     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
888     */
889    public String getLine1Number() {
890        try {
891            return getSubscriberInfo().getLine1Number();
892        } catch (RemoteException ex) {
893            return null;
894        } catch (NullPointerException ex) {
895            // This could happen before phone restarts due to crashing
896            return null;
897        }
898    }
899
900    /**
901     * Returns the alphabetic identifier associated with the line 1 number.
902     * Return null if it is unavailable.
903     * <p>
904     * Requires Permission:
905     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
906     * @hide
907     * nobody seems to call this.
908     */
909    public String getLine1AlphaTag() {
910        try {
911            return getSubscriberInfo().getLine1AlphaTag();
912        } catch (RemoteException ex) {
913            return null;
914        } catch (NullPointerException ex) {
915            // This could happen before phone restarts due to crashing
916            return null;
917        }
918    }
919
920    /**
921     * Returns the MSISDN string.
922     * for a GSM phone. Return null if it is unavailable.
923     * <p>
924     * Requires Permission:
925     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
926     *
927     * @hide
928     */
929    public String getMsisdn() {
930        try {
931            return getSubscriberInfo().getMsisdn();
932        } catch (RemoteException ex) {
933            return null;
934        } catch (NullPointerException ex) {
935            // This could happen before phone restarts due to crashing
936            return null;
937        }
938    }
939
940    /**
941     * Returns the voice mail number. Return null if it is unavailable.
942     * <p>
943     * Requires Permission:
944     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
945     */
946    public String getVoiceMailNumber() {
947        try {
948            return getSubscriberInfo().getVoiceMailNumber();
949        } catch (RemoteException ex) {
950            return null;
951        } catch (NullPointerException ex) {
952            // This could happen before phone restarts due to crashing
953            return null;
954        }
955    }
956
957    /**
958     * Returns the complete voice mail number. Return null if it is unavailable.
959     * <p>
960     * Requires Permission:
961     *   {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED}
962     *
963     * @hide
964     */
965    public String getCompleteVoiceMailNumber() {
966        try {
967            return getSubscriberInfo().getCompleteVoiceMailNumber();
968        } catch (RemoteException ex) {
969            return null;
970        } catch (NullPointerException ex) {
971            // This could happen before phone restarts due to crashing
972            return null;
973        }
974    }
975
976    /**
977     * Returns the voice mail count. Return 0 if unavailable.
978     * <p>
979     * Requires Permission:
980     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
981     * @hide
982     */
983    public int getVoiceMessageCount() {
984        try {
985            return getITelephony().getVoiceMessageCount();
986        } catch (RemoteException ex) {
987            return 0;
988        } catch (NullPointerException ex) {
989            // This could happen before phone restarts due to crashing
990            return 0;
991        }
992    }
993
994    /**
995     * Retrieves the alphabetic identifier associated with the voice
996     * mail number.
997     * <p>
998     * Requires Permission:
999     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1000     */
1001    public String getVoiceMailAlphaTag() {
1002        try {
1003            return getSubscriberInfo().getVoiceMailAlphaTag();
1004        } catch (RemoteException ex) {
1005            return null;
1006        } catch (NullPointerException ex) {
1007            // This could happen before phone restarts due to crashing
1008            return null;
1009        }
1010    }
1011
1012    /**
1013     * Returns the IMS private user identity (IMPI) that was loaded from the ISIM.
1014     * @return the IMPI, or null if not present or not loaded
1015     * @hide
1016     */
1017    public String getIsimImpi() {
1018        try {
1019            return getSubscriberInfo().getIsimImpi();
1020        } catch (RemoteException ex) {
1021            return null;
1022        } catch (NullPointerException ex) {
1023            // This could happen before phone restarts due to crashing
1024            return null;
1025        }
1026    }
1027
1028    /**
1029     * Returns the IMS home network domain name that was loaded from the ISIM.
1030     * @return the IMS domain name, or null if not present or not loaded
1031     * @hide
1032     */
1033    public String getIsimDomain() {
1034        try {
1035            return getSubscriberInfo().getIsimDomain();
1036        } catch (RemoteException ex) {
1037            return null;
1038        } catch (NullPointerException ex) {
1039            // This could happen before phone restarts due to crashing
1040            return null;
1041        }
1042    }
1043
1044    /**
1045     * Returns the IMS public user identities (IMPU) that were loaded from the ISIM.
1046     * @return an array of IMPU strings, with one IMPU per string, or null if
1047     *      not present or not loaded
1048     * @hide
1049     */
1050    public String[] getIsimImpu() {
1051        try {
1052            return getSubscriberInfo().getIsimImpu();
1053        } catch (RemoteException ex) {
1054            return null;
1055        } catch (NullPointerException ex) {
1056            // This could happen before phone restarts due to crashing
1057            return null;
1058        }
1059    }
1060
1061    private IPhoneSubInfo getSubscriberInfo() {
1062        // get it each time because that process crashes a lot
1063        return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
1064    }
1065
1066
1067    /** Device call state: No activity. */
1068    public static final int CALL_STATE_IDLE = 0;
1069    /** Device call state: Ringing. A new call arrived and is
1070     *  ringing or waiting. In the latter case, another call is
1071     *  already active. */
1072    public static final int CALL_STATE_RINGING = 1;
1073    /** Device call state: Off-hook. At least one call exists
1074      * that is dialing, active, or on hold, and no calls are ringing
1075      * or waiting. */
1076    public static final int CALL_STATE_OFFHOOK = 2;
1077
1078    /**
1079     * Returns a constant indicating the call state (cellular) on the device.
1080     */
1081    public int getCallState() {
1082        try {
1083            return getITelephony().getCallState();
1084        } catch (RemoteException ex) {
1085            // the phone process is restarting.
1086            return CALL_STATE_IDLE;
1087        } catch (NullPointerException ex) {
1088          // the phone process is restarting.
1089          return CALL_STATE_IDLE;
1090      }
1091    }
1092
1093    /** Data connection activity: No traffic. */
1094    public static final int DATA_ACTIVITY_NONE = 0x00000000;
1095    /** Data connection activity: Currently receiving IP PPP traffic. */
1096    public static final int DATA_ACTIVITY_IN = 0x00000001;
1097    /** Data connection activity: Currently sending IP PPP traffic. */
1098    public static final int DATA_ACTIVITY_OUT = 0x00000002;
1099    /** Data connection activity: Currently both sending and receiving
1100     *  IP PPP traffic. */
1101    public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT;
1102    /**
1103     * Data connection is active, but physical link is down
1104     */
1105    public static final int DATA_ACTIVITY_DORMANT = 0x00000004;
1106
1107    /**
1108     * Returns a constant indicating the type of activity on a data connection
1109     * (cellular).
1110     *
1111     * @see #DATA_ACTIVITY_NONE
1112     * @see #DATA_ACTIVITY_IN
1113     * @see #DATA_ACTIVITY_OUT
1114     * @see #DATA_ACTIVITY_INOUT
1115     * @see #DATA_ACTIVITY_DORMANT
1116     */
1117    public int getDataActivity() {
1118        try {
1119            return getITelephony().getDataActivity();
1120        } catch (RemoteException ex) {
1121            // the phone process is restarting.
1122            return DATA_ACTIVITY_NONE;
1123        } catch (NullPointerException ex) {
1124          // the phone process is restarting.
1125          return DATA_ACTIVITY_NONE;
1126      }
1127    }
1128
1129    /** Data connection state: Unknown.  Used before we know the state.
1130     * @hide
1131     */
1132    public static final int DATA_UNKNOWN        = -1;
1133    /** Data connection state: Disconnected. IP traffic not available. */
1134    public static final int DATA_DISCONNECTED   = 0;
1135    /** Data connection state: Currently setting up a data connection. */
1136    public static final int DATA_CONNECTING     = 1;
1137    /** Data connection state: Connected. IP traffic should be available. */
1138    public static final int DATA_CONNECTED      = 2;
1139    /** Data connection state: Suspended. The connection is up, but IP
1140     * traffic is temporarily unavailable. For example, in a 2G network,
1141     * data activity may be suspended when a voice call arrives. */
1142    public static final int DATA_SUSPENDED      = 3;
1143
1144    /**
1145     * Returns a constant indicating the current data connection state
1146     * (cellular).
1147     *
1148     * @see #DATA_DISCONNECTED
1149     * @see #DATA_CONNECTING
1150     * @see #DATA_CONNECTED
1151     * @see #DATA_SUSPENDED
1152     */
1153    public int getDataState() {
1154        try {
1155            return getITelephony().getDataState();
1156        } catch (RemoteException ex) {
1157            // the phone process is restarting.
1158            return DATA_DISCONNECTED;
1159        } catch (NullPointerException ex) {
1160            return DATA_DISCONNECTED;
1161        }
1162    }
1163
1164    private ITelephony getITelephony() {
1165        return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
1166    }
1167
1168    //
1169    //
1170    // PhoneStateListener
1171    //
1172    //
1173
1174    /**
1175     * Registers a listener object to receive notification of changes
1176     * in specified telephony states.
1177     * <p>
1178     * To register a listener, pass a {@link PhoneStateListener}
1179     * and specify at least one telephony state of interest in
1180     * the events argument.
1181     *
1182     * At registration, and when a specified telephony state
1183     * changes, the telephony manager invokes the appropriate
1184     * callback method on the listener object and passes the
1185     * current (udpated) values.
1186     * <p>
1187     * To unregister a listener, pass the listener object and set the
1188     * events argument to
1189     * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0).
1190     *
1191     * @param listener The {@link PhoneStateListener} object to register
1192     *                 (or unregister)
1193     * @param events The telephony state(s) of interest to the listener,
1194     *               as a bitwise-OR combination of {@link PhoneStateListener}
1195     *               LISTEN_ flags.
1196     */
1197    public void listen(PhoneStateListener listener, int events) {
1198        String pkgForDebug = sContext != null ? sContext.getPackageName() : "<unknown>";
1199        try {
1200            Boolean notifyNow = (getITelephony() != null);
1201            sRegistry.listen(pkgForDebug, listener.callback, events, notifyNow);
1202        } catch (RemoteException ex) {
1203            // system process dead
1204        } catch (NullPointerException ex) {
1205            // system process dead
1206        }
1207    }
1208
1209    /**
1210     * Returns the CDMA ERI icon index to display
1211     *
1212     * @hide
1213     */
1214    public int getCdmaEriIconIndex() {
1215        try {
1216            return getITelephony().getCdmaEriIconIndex();
1217        } catch (RemoteException ex) {
1218            // the phone process is restarting.
1219            return -1;
1220        } catch (NullPointerException ex) {
1221            return -1;
1222        }
1223    }
1224
1225    /**
1226     * Returns the CDMA ERI icon mode,
1227     * 0 - ON
1228     * 1 - FLASHING
1229     *
1230     * @hide
1231     */
1232    public int getCdmaEriIconMode() {
1233        try {
1234            return getITelephony().getCdmaEriIconMode();
1235        } catch (RemoteException ex) {
1236            // the phone process is restarting.
1237            return -1;
1238        } catch (NullPointerException ex) {
1239            return -1;
1240        }
1241    }
1242
1243    /**
1244     * Returns the CDMA ERI text,
1245     *
1246     * @hide
1247     */
1248    public String getCdmaEriText() {
1249        try {
1250            return getITelephony().getCdmaEriText();
1251        } catch (RemoteException ex) {
1252            // the phone process is restarting.
1253            return null;
1254        } catch (NullPointerException ex) {
1255            return null;
1256        }
1257    }
1258
1259    /**
1260     * @return true if the current device is "voice capable".
1261     * <p>
1262     * "Voice capable" means that this device supports circuit-switched
1263     * (i.e. voice) phone calls over the telephony network, and is allowed
1264     * to display the in-call UI while a cellular voice call is active.
1265     * This will be false on "data only" devices which can't make voice
1266     * calls and don't support any in-call UI.
1267     * <p>
1268     * Note: the meaning of this flag is subtly different from the
1269     * PackageManager.FEATURE_TELEPHONY system feature, which is available
1270     * on any device with a telephony radio, even if the device is
1271     * data-only.
1272     *
1273     * @hide pending API review
1274     */
1275    public boolean isVoiceCapable() {
1276        if (sContext == null) return true;
1277        return sContext.getResources().getBoolean(
1278                com.android.internal.R.bool.config_voice_capable);
1279    }
1280
1281    /**
1282     * @return true if the current device supports sms service.
1283     * <p>
1284     * If true, this means that the device supports both sending and
1285     * receiving sms via the telephony network.
1286     * <p>
1287     * Note: Voicemail waiting sms, cell broadcasting sms, and MMS are
1288     *       disabled when device doesn't support sms.
1289     *
1290     * @hide pending API review
1291     */
1292    public boolean isSmsCapable() {
1293        if (sContext == null) return true;
1294        return sContext.getResources().getBoolean(
1295                com.android.internal.R.bool.config_sms_capable);
1296    }
1297
1298    /**
1299     * Returns all observed cell information of the device.
1300     *
1301     * @return List of CellInfo or null if info unavailable.
1302     *
1303     * <p>Requires Permission:
1304     * (@link android.Manifest.permission#ACCESS_COARSE_UPDATES}
1305     */
1306    public List<CellInfo> getAllCellInfo() {
1307        try {
1308            return getITelephony().getAllCellInfo();
1309        } catch (RemoteException ex) {
1310            return null;
1311        } catch (NullPointerException ex) {
1312            return null;
1313        }
1314    }
1315}
1316