TelephonyManager.java revision 786e71ab11f2d89afffc9db7473f16206395c813
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.PhoneConstants;
31import com.android.internal.telephony.RILConstants;
32import com.android.internal.telephony.TelephonyProperties;
33
34import java.io.FileInputStream;
35import java.io.IOException;
36import java.util.List;
37import java.util.regex.Matcher;
38import java.util.regex.Pattern;
39
40/**
41 * Provides access to information about the telephony services on
42 * the device. Applications can use the methods in this class to
43 * determine telephony services and states, as well as to access some
44 * types of subscriber information. Applications can also register
45 * a listener to receive notification of telephony state changes.
46 * <p>
47 * You do not instantiate this class directly; instead, you retrieve
48 * a reference to an instance through
49 * {@link android.content.Context#getSystemService
50 * Context.getSystemService(Context.TELEPHONY_SERVICE)}.
51 * <p>
52 * Note that access to some telephony information is
53 * permission-protected. Your application cannot access the protected
54 * information unless it has the appropriate permissions declared in
55 * its manifest file. Where permissions apply, they are noted in the
56 * the methods through which you access the protected information.
57 */
58public class TelephonyManager {
59    private static final String TAG = "TelephonyManager";
60
61    private static ITelephonyRegistry sRegistry;
62    private final Context mContext;
63
64    /** @hide */
65    public TelephonyManager(Context context) {
66        Context appContext = context.getApplicationContext();
67        if (appContext != null) {
68            mContext = appContext;
69        } else {
70            mContext = context;
71        }
72
73        if (sRegistry == null) {
74            sRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
75                    "telephony.registry"));
76        }
77    }
78
79    /** @hide */
80    private TelephonyManager() {
81        mContext = null;
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 Phone app sends this intent when a user opts to respond-via-message during an incoming
129     * call. By default, the device's default SMS app consumes this message and sends a text message
130     * to the caller. A third party app can also provide this functionality by consuming this Intent
131     * with a {@link android.app.Service} and sending the message using its own messaging system.
132     * <p>The intent contains a URI (available from {@link android.content.Intent#getData})
133     * describing the recipient, using either the {@code sms:}, {@code smsto:}, {@code mms:},
134     * or {@code mmsto:} URI schema. Each of these URI schema carry the recipient information the
135     * same way: the path part of the URI contains the recipient's phone number or a comma-separated
136     * set of phone numbers if there are multiple recipients. For example, {@code
137     * smsto:2065551234}.</p>
138     *
139     * <p>The intent may also contain extras for the message text (in {@link
140     * android.content.Intent#EXTRA_TEXT}) and a message subject
141     * (in {@link android.content.Intent#EXTRA_SUBJECT}).</p>
142     *
143     * <p class="note"><strong>Note:</strong>
144     * The intent-filter that consumes this Intent needs to be in a {@link android.app.Service}
145     * that requires the
146     * permission {@link android.Manifest.permission#SEND_RESPOND_VIA_MESSAGE}.</p>
147     * <p>For example, the service that receives this intent can be declared in the manifest file
148     * with an intent filter like this:</p>
149     * <pre>
150     * &lt;!-- Service that delivers SMS messages received from the phone "quick response" -->
151     * &lt;service android:name=".HeadlessSmsSendService"
152     *          android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
153     *          android:exported="true" >
154     *   &lt;intent-filter>
155     *     &lt;action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
156     *     &lt;category android:name="android.intent.category.DEFAULT" />
157     *     &lt;data android:scheme="sms" />
158     *     &lt;data android:scheme="smsto" />
159     *     &lt;data android:scheme="mms" />
160     *     &lt;data android:scheme="mmsto" />
161     *   &lt;/intent-filter>
162     * &lt;/service></pre>
163     * <p>
164     * Output: nothing.
165     */
166    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
167    public static final String ACTION_RESPOND_VIA_MESSAGE =
168            "android.intent.action.RESPOND_VIA_MESSAGE";
169
170    /**
171     * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
172     * for a String containing the new call state.
173     *
174     * @see #EXTRA_STATE_IDLE
175     * @see #EXTRA_STATE_RINGING
176     * @see #EXTRA_STATE_OFFHOOK
177     *
178     * <p class="note">
179     * Retrieve with
180     * {@link android.content.Intent#getStringExtra(String)}.
181     */
182    public static final String EXTRA_STATE = PhoneConstants.STATE_KEY;
183
184    /**
185     * Value used with {@link #EXTRA_STATE} corresponding to
186     * {@link #CALL_STATE_IDLE}.
187     */
188    public static final String EXTRA_STATE_IDLE = PhoneConstants.State.IDLE.toString();
189
190    /**
191     * Value used with {@link #EXTRA_STATE} corresponding to
192     * {@link #CALL_STATE_RINGING}.
193     */
194    public static final String EXTRA_STATE_RINGING = PhoneConstants.State.RINGING.toString();
195
196    /**
197     * Value used with {@link #EXTRA_STATE} corresponding to
198     * {@link #CALL_STATE_OFFHOOK}.
199     */
200    public static final String EXTRA_STATE_OFFHOOK = PhoneConstants.State.OFFHOOK.toString();
201
202    /**
203     * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
204     * for a String containing the incoming phone number.
205     * Only valid when the new call state is RINGING.
206     *
207     * <p class="note">
208     * Retrieve with
209     * {@link android.content.Intent#getStringExtra(String)}.
210     */
211    public static final String EXTRA_INCOMING_NUMBER = "incoming_number";
212
213    /**
214     * Broadcast intent action indicating that a precise call state
215     * (cellular) on the device has changed.
216     *
217     * <p>
218     * The {@link #EXTRA_RINGING_CALL_STATE} extra indicates the ringing call state.
219     * The {@link #EXTRA_FOREGROUND_CALL_STATE} extra indicates the foreground call state.
220     * The {@link #EXTRA_BACKGROUND_CALL_STATE} extra indicates the background call state.
221     * The {@link #EXTRA_DISCONNECT_CAUSE} extra indicates the disconnect cause.
222     * The {@link #EXTRA_PRECISE_DISCONNECT_CAUSE} extra indicates the precise disconnect cause.
223     *
224     * <p class="note">
225     * Requires the READ_PRECISE_PHONE_STATE permission.
226     *
227     * @see #EXTRA_RINGING_CALL_STATE
228     * @see #EXTRA_FOREGROUND_CALL_STATE
229     * @see #EXTRA_BACKGROUND_CALL_STATE
230     * @see #EXTRA_DISCONNECT_CAUSE
231     * @see #EXTRA_PRECISE_DISCONNECT_CAUSE
232     *
233     * <p class="note">
234     * Requires the READ_PRECISE_PHONE_STATE permission.
235     *
236     * @hide
237     */
238    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
239    public static final String ACTION_PRECISE_CALL_STATE_CHANGED =
240            "android.intent.action.PRECISE_CALL_STATE";
241
242    /**
243     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
244     * for an integer containing the state of the current ringing call.
245     *
246     * @see PreciseCallState#PRECISE_CALL_STATE_NOT_VALID
247     * @see PreciseCallState#PRECISE_CALL_STATE_IDLE
248     * @see PreciseCallState#PRECISE_CALL_STATE_ACTIVE
249     * @see PreciseCallState#PRECISE_CALL_STATE_HOLDING
250     * @see PreciseCallState#PRECISE_CALL_STATE_DIALING
251     * @see PreciseCallState#PRECISE_CALL_STATE_ALERTING
252     * @see PreciseCallState#PRECISE_CALL_STATE_INCOMING
253     * @see PreciseCallState#PRECISE_CALL_STATE_WAITING
254     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTED
255     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTING
256     *
257     * <p class="note">
258     * Retrieve with
259     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
260     *
261     * @hide
262     */
263    public static final String EXTRA_RINGING_CALL_STATE = "ringing_state";
264
265    /**
266     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
267     * for an integer containing the state of the current foreground call.
268     *
269     * @see PreciseCallState#PRECISE_CALL_STATE_NOT_VALID
270     * @see PreciseCallState#PRECISE_CALL_STATE_IDLE
271     * @see PreciseCallState#PRECISE_CALL_STATE_ACTIVE
272     * @see PreciseCallState#PRECISE_CALL_STATE_HOLDING
273     * @see PreciseCallState#PRECISE_CALL_STATE_DIALING
274     * @see PreciseCallState#PRECISE_CALL_STATE_ALERTING
275     * @see PreciseCallState#PRECISE_CALL_STATE_INCOMING
276     * @see PreciseCallState#PRECISE_CALL_STATE_WAITING
277     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTED
278     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTING
279     *
280     * <p class="note">
281     * Retrieve with
282     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
283     *
284     * @hide
285     */
286    public static final String EXTRA_FOREGROUND_CALL_STATE = "foreground_state";
287
288    /**
289     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
290     * for an integer containing the state of the current background call.
291     *
292     * @see PreciseCallState#PRECISE_CALL_STATE_NOT_VALID
293     * @see PreciseCallState#PRECISE_CALL_STATE_IDLE
294     * @see PreciseCallState#PRECISE_CALL_STATE_ACTIVE
295     * @see PreciseCallState#PRECISE_CALL_STATE_HOLDING
296     * @see PreciseCallState#PRECISE_CALL_STATE_DIALING
297     * @see PreciseCallState#PRECISE_CALL_STATE_ALERTING
298     * @see PreciseCallState#PRECISE_CALL_STATE_INCOMING
299     * @see PreciseCallState#PRECISE_CALL_STATE_WAITING
300     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTED
301     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTING
302     *
303     * <p class="note">
304     * Retrieve with
305     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
306     *
307     * @hide
308     */
309    public static final String EXTRA_BACKGROUND_CALL_STATE = "background_state";
310
311    /**
312     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
313     * for an integer containing the disconnect cause.
314     *
315     * @see DisconnectCause
316     *
317     * <p class="note">
318     * Retrieve with
319     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
320     *
321     * @hide
322     */
323    public static final String EXTRA_DISCONNECT_CAUSE = "disconnect_cause";
324
325    /**
326     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
327     * for an integer containing the disconnect cause provided by the RIL.
328     *
329     * @see PreciseDisconnectCause
330     *
331     * <p class="note">
332     * Retrieve with
333     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
334     *
335     * @hide
336     */
337    public static final String EXTRA_PRECISE_DISCONNECT_CAUSE = "precise_disconnect_cause";
338
339    /**
340     * Broadcast intent action indicating a data connection has changed,
341     * providing precise information about the connection.
342     *
343     * <p>
344     * The {@link #EXTRA_DATA_STATE} extra indicates the connection state.
345     * The {@link #EXTRA_DATA_NETWORK_TYPE} extra indicates the connection network type.
346     * The {@link #EXTRA_DATA_APN_TYPE} extra indicates the APN type.
347     * The {@link #EXTRA_DATA_APN} extra indicates the APN.
348     * The {@link #EXTRA_DATA_CHANGE_REASON} extra indicates the connection change reason.
349     * The {@link #EXTRA_DATA_IFACE_PROPERTIES} extra indicates the connection interface.
350     * The {@link #EXTRA_DATA_FAILURE_CAUSE} extra indicates the connection fail cause.
351     *
352     * <p class="note">
353     * Requires the READ_PRECISE_PHONE_STATE permission.
354     *
355     * @see #EXTRA_DATA_STATE
356     * @see #EXTRA_DATA_NETWORK_TYPE
357     * @see #EXTRA_DATA_APN_TYPE
358     * @see #EXTRA_DATA_APN
359     * @see #EXTRA_DATA_CHANGE_REASON
360     * @see #EXTRA_DATA_IFACE
361     * @see #EXTRA_DATA_FAILURE_CAUSE
362     * @hide
363     */
364    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
365    public static final String ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED =
366            "android.intent.action.PRECISE_DATA_CONNECTION_STATE_CHANGED";
367
368    /**
369     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
370     * for an integer containing the state of the current data connection.
371     *
372     * @see TelephonyManager#DATA_UNKNOWN
373     * @see TelephonyManager#DATA_DISCONNECTED
374     * @see TelephonyManager#DATA_CONNECTING
375     * @see TelephonyManager#DATA_CONNECTED
376     * @see TelephonyManager#DATA_SUSPENDED
377     *
378     * <p class="note">
379     * Retrieve with
380     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
381     *
382     * @hide
383     */
384    public static final String EXTRA_DATA_STATE = PhoneConstants.STATE_KEY;
385
386    /**
387     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
388     * for an integer containing the network type.
389     *
390     * @see TelephonyManager#NETWORK_TYPE_UNKNOWN
391     * @see TelephonyManager#NETWORK_TYPE_GPRS
392     * @see TelephonyManager#NETWORK_TYPE_EDGE
393     * @see TelephonyManager#NETWORK_TYPE_UMTS
394     * @see TelephonyManager#NETWORK_TYPE_CDMA
395     * @see TelephonyManager#NETWORK_TYPE_EVDO_0
396     * @see TelephonyManager#NETWORK_TYPE_EVDO_A
397     * @see TelephonyManager#NETWORK_TYPE_1xRTT
398     * @see TelephonyManager#NETWORK_TYPE_HSDPA
399     * @see TelephonyManager#NETWORK_TYPE_HSUPA
400     * @see TelephonyManager#NETWORK_TYPE_HSPA
401     * @see TelephonyManager#NETWORK_TYPE_IDEN
402     * @see TelephonyManager#NETWORK_TYPE_EVDO_B
403     * @see TelephonyManager#NETWORK_TYPE_LTE
404     * @see TelephonyManager#NETWORK_TYPE_EHRPD
405     * @see TelephonyManager#NETWORK_TYPE_HSPAP
406     *
407     * <p class="note">
408     * Retrieve with
409     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
410     *
411     * @hide
412     */
413    public static final String EXTRA_DATA_NETWORK_TYPE = PhoneConstants.DATA_NETWORK_TYPE_KEY;
414
415    /**
416     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
417     * for an String containing the data APN type.
418     *
419     * <p class="note">
420     * Retrieve with
421     * {@link android.content.Intent#getStringExtra(String name)}.
422     *
423     * @hide
424     */
425    public static final String EXTRA_DATA_APN_TYPE = PhoneConstants.DATA_APN_TYPE_KEY;
426
427    /**
428     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
429     * for an String containing the data APN.
430     *
431     * <p class="note">
432     * Retrieve with
433     * {@link android.content.Intent#getStringExtra(String name)}.
434     *
435     * @hide
436     */
437    public static final String EXTRA_DATA_APN = PhoneConstants.DATA_APN_KEY;
438
439    /**
440     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
441     * for an String representation of the change reason.
442     *
443     * <p class="note">
444     * Retrieve with
445     * {@link android.content.Intent#getStringExtra(String name)}.
446     *
447     * @hide
448     */
449    public static final String EXTRA_DATA_CHANGE_REASON = PhoneConstants.STATE_CHANGE_REASON_KEY;
450
451    /**
452     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
453     * for an String representation of the data interface.
454     *
455     * <p class="note">
456     * Retrieve with
457     * {@link android.content.Intent#getParcelableExtra(String name)}.
458     *
459     * @hide
460     */
461    public static final String EXTRA_DATA_LINK_PROPERTIES_KEY = PhoneConstants.DATA_LINK_PROPERTIES_KEY;
462
463    /**
464     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
465     * for the data connection fail cause.
466     *
467     * <p class="note">
468     * Retrieve with
469     * {@link android.content.Intent#getStringExtra(String name)}.
470     *
471     * @hide
472     */
473    public static final String EXTRA_DATA_FAILURE_CAUSE = PhoneConstants.DATA_FAILURE_CAUSE_KEY;
474
475    //
476    //
477    // Device Info
478    //
479    //
480
481    /**
482     * Returns the software version number for the device, for example,
483     * the IMEI/SV for GSM phones. Return null if the software version is
484     * not available.
485     *
486     * <p>Requires Permission:
487     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
488     */
489    public String getDeviceSoftwareVersion() {
490        try {
491            return getSubscriberInfo().getDeviceSvn();
492        } catch (RemoteException ex) {
493            return null;
494        } catch (NullPointerException ex) {
495            return null;
496        }
497    }
498
499    /**
500     * Returns the unique device ID, for example, the IMEI for GSM and the MEID
501     * or ESN for CDMA phones. Return null if device ID is not available.
502     *
503     * <p>Requires Permission:
504     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
505     */
506    public String getDeviceId() {
507        try {
508            return getSubscriberInfo().getDeviceId();
509        } catch (RemoteException ex) {
510            return null;
511        } catch (NullPointerException ex) {
512            return null;
513        }
514    }
515
516    /**
517     * Returns the current location of the device.
518     *<p>
519     * If there is only one radio in the device and that radio has an LTE connection,
520     * this method will return null. The implementation must not to try add LTE
521     * identifiers into the existing cdma/gsm classes.
522     *<p>
523     * In the future this call will be deprecated.
524     *<p>
525     * @return Current location of the device or null if not available.
526     *
527     * <p>Requires Permission:
528     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} or
529     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION}.
530     */
531    public CellLocation getCellLocation() {
532        try {
533            Bundle bundle = getITelephony().getCellLocation();
534            if (bundle.isEmpty()) return null;
535            CellLocation cl = CellLocation.newFromBundle(bundle);
536            if (cl.isEmpty())
537                return null;
538            return cl;
539        } catch (RemoteException ex) {
540            return null;
541        } catch (NullPointerException ex) {
542            return null;
543        }
544    }
545
546    /**
547     * Enables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
548     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
549     *
550     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
551     * CONTROL_LOCATION_UPDATES}
552     *
553     * @hide
554     */
555    public void enableLocationUpdates() {
556        try {
557            getITelephony().enableLocationUpdates();
558        } catch (RemoteException ex) {
559        } catch (NullPointerException ex) {
560        }
561    }
562
563    /**
564     * Disables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
565     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
566     *
567     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
568     * CONTROL_LOCATION_UPDATES}
569     *
570     * @hide
571     */
572    public void disableLocationUpdates() {
573        try {
574            getITelephony().disableLocationUpdates();
575        } catch (RemoteException ex) {
576        } catch (NullPointerException ex) {
577        }
578    }
579
580    /**
581     * Returns the neighboring cell information of the device. The getAllCellInfo is preferred
582     * and use this only if getAllCellInfo return nulls or an empty list.
583     *<p>
584     * In the future this call will be deprecated.
585     *<p>
586     * @return List of NeighboringCellInfo or null if info unavailable.
587     *
588     * <p>Requires Permission:
589     * (@link android.Manifest.permission#ACCESS_COARSE_UPDATES}
590     */
591    public List<NeighboringCellInfo> getNeighboringCellInfo() {
592        try {
593            return getITelephony().getNeighboringCellInfo(mContext.getOpPackageName());
594        } catch (RemoteException ex) {
595            return null;
596        } catch (NullPointerException ex) {
597            return null;
598        }
599    }
600
601    /** No phone radio. */
602    public static final int PHONE_TYPE_NONE = PhoneConstants.PHONE_TYPE_NONE;
603    /** Phone radio is GSM. */
604    public static final int PHONE_TYPE_GSM = PhoneConstants.PHONE_TYPE_GSM;
605    /** Phone radio is CDMA. */
606    public static final int PHONE_TYPE_CDMA = PhoneConstants.PHONE_TYPE_CDMA;
607    /** Phone is via SIP. */
608    public static final int PHONE_TYPE_SIP = PhoneConstants.PHONE_TYPE_SIP;
609
610    /**
611     * Returns the current phone type.
612     * TODO: This is a last minute change and hence hidden.
613     *
614     * @see #PHONE_TYPE_NONE
615     * @see #PHONE_TYPE_GSM
616     * @see #PHONE_TYPE_CDMA
617     * @see #PHONE_TYPE_SIP
618     *
619     * {@hide}
620     */
621    public int getCurrentPhoneType() {
622        try{
623            ITelephony telephony = getITelephony();
624            if (telephony != null) {
625                return telephony.getActivePhoneType();
626            } else {
627                // This can happen when the ITelephony interface is not up yet.
628                return getPhoneTypeFromProperty();
629            }
630        } catch (RemoteException ex) {
631            // This shouldn't happen in the normal case, as a backup we
632            // read from the system property.
633            return getPhoneTypeFromProperty();
634        } catch (NullPointerException ex) {
635            // This shouldn't happen in the normal case, as a backup we
636            // read from the system property.
637            return getPhoneTypeFromProperty();
638        }
639    }
640
641    /**
642     * Returns a constant indicating the device phone type.  This
643     * indicates the type of radio used to transmit voice calls.
644     *
645     * @see #PHONE_TYPE_NONE
646     * @see #PHONE_TYPE_GSM
647     * @see #PHONE_TYPE_CDMA
648     * @see #PHONE_TYPE_SIP
649     */
650    public int getPhoneType() {
651        if (!isVoiceCapable()) {
652            return PHONE_TYPE_NONE;
653        }
654        return getCurrentPhoneType();
655    }
656
657    private int getPhoneTypeFromProperty() {
658        int type =
659            SystemProperties.getInt(TelephonyProperties.CURRENT_ACTIVE_PHONE,
660                    getPhoneTypeFromNetworkType());
661        return type;
662    }
663
664    private int getPhoneTypeFromNetworkType() {
665        // When the system property CURRENT_ACTIVE_PHONE, has not been set,
666        // use the system property for default network type.
667        // This is a fail safe, and can only happen at first boot.
668        int mode = SystemProperties.getInt("ro.telephony.default_network", -1);
669        if (mode == -1)
670            return PHONE_TYPE_NONE;
671        return getPhoneType(mode);
672    }
673
674    /**
675     * This function returns the type of the phone, depending
676     * on the network mode.
677     *
678     * @param networkMode
679     * @return Phone Type
680     *
681     * @hide
682     */
683    public static int getPhoneType(int networkMode) {
684        switch(networkMode) {
685        case RILConstants.NETWORK_MODE_CDMA:
686        case RILConstants.NETWORK_MODE_CDMA_NO_EVDO:
687        case RILConstants.NETWORK_MODE_EVDO_NO_CDMA:
688            return PhoneConstants.PHONE_TYPE_CDMA;
689
690        case RILConstants.NETWORK_MODE_WCDMA_PREF:
691        case RILConstants.NETWORK_MODE_GSM_ONLY:
692        case RILConstants.NETWORK_MODE_WCDMA_ONLY:
693        case RILConstants.NETWORK_MODE_GSM_UMTS:
694        case RILConstants.NETWORK_MODE_LTE_GSM_WCDMA:
695        case RILConstants.NETWORK_MODE_LTE_WCDMA:
696        case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
697            return PhoneConstants.PHONE_TYPE_GSM;
698
699        // Use CDMA Phone for the global mode including CDMA
700        case RILConstants.NETWORK_MODE_GLOBAL:
701        case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO:
702            return PhoneConstants.PHONE_TYPE_CDMA;
703
704        case RILConstants.NETWORK_MODE_LTE_ONLY:
705            if (getLteOnCdmaModeStatic() == PhoneConstants.LTE_ON_CDMA_TRUE) {
706                return PhoneConstants.PHONE_TYPE_CDMA;
707            } else {
708                return PhoneConstants.PHONE_TYPE_GSM;
709            }
710        default:
711            return PhoneConstants.PHONE_TYPE_GSM;
712        }
713    }
714
715    /**
716     * The contents of the /proc/cmdline file
717     */
718    private static String getProcCmdLine()
719    {
720        String cmdline = "";
721        FileInputStream is = null;
722        try {
723            is = new FileInputStream("/proc/cmdline");
724            byte [] buffer = new byte[2048];
725            int count = is.read(buffer);
726            if (count > 0) {
727                cmdline = new String(buffer, 0, count);
728            }
729        } catch (IOException e) {
730            Rlog.d(TAG, "No /proc/cmdline exception=" + e);
731        } finally {
732            if (is != null) {
733                try {
734                    is.close();
735                } catch (IOException e) {
736                }
737            }
738        }
739        Rlog.d(TAG, "/proc/cmdline=" + cmdline);
740        return cmdline;
741    }
742
743    /** Kernel command line */
744    private static final String sKernelCmdLine = getProcCmdLine();
745
746    /** Pattern for selecting the product type from the kernel command line */
747    private static final Pattern sProductTypePattern =
748        Pattern.compile("\\sproduct_type\\s*=\\s*(\\w+)");
749
750    /** The ProductType used for LTE on CDMA devices */
751    private static final String sLteOnCdmaProductType =
752        SystemProperties.get(TelephonyProperties.PROPERTY_LTE_ON_CDMA_PRODUCT_TYPE, "");
753
754    /**
755     * Return if the current radio is LTE on CDMA. This
756     * is a tri-state return value as for a period of time
757     * the mode may be unknown.
758     *
759     * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE}
760     * or {@link PhoneConstants#LTE_ON_CDMA_TRUE}
761     *
762     * @hide
763     */
764    public static int getLteOnCdmaModeStatic() {
765        int retVal;
766        int curVal;
767        String productType = "";
768
769        curVal = SystemProperties.getInt(TelephonyProperties.PROPERTY_LTE_ON_CDMA_DEVICE,
770                    PhoneConstants.LTE_ON_CDMA_UNKNOWN);
771        retVal = curVal;
772        if (retVal == PhoneConstants.LTE_ON_CDMA_UNKNOWN) {
773            Matcher matcher = sProductTypePattern.matcher(sKernelCmdLine);
774            if (matcher.find()) {
775                productType = matcher.group(1);
776                if (sLteOnCdmaProductType.equals(productType)) {
777                    retVal = PhoneConstants.LTE_ON_CDMA_TRUE;
778                } else {
779                    retVal = PhoneConstants.LTE_ON_CDMA_FALSE;
780                }
781            } else {
782                retVal = PhoneConstants.LTE_ON_CDMA_FALSE;
783            }
784        }
785
786        Rlog.d(TAG, "getLteOnCdmaMode=" + retVal + " curVal=" + curVal +
787                " product_type='" + productType +
788                "' lteOnCdmaProductType='" + sLteOnCdmaProductType + "'");
789        return retVal;
790    }
791
792    //
793    //
794    // Current Network
795    //
796    //
797
798    /**
799     * Returns the alphabetic name of current registered operator.
800     * <p>
801     * Availability: Only when user is registered to a network. Result may be
802     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
803     * on a CDMA network).
804     */
805    public String getNetworkOperatorName() {
806        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ALPHA);
807    }
808
809    /**
810     * Returns the numeric name (MCC+MNC) of current registered operator.
811     * <p>
812     * Availability: Only when user is registered to a network. Result may be
813     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
814     * on a CDMA network).
815     */
816    public String getNetworkOperator() {
817        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_NUMERIC);
818    }
819
820    /**
821     * Returns true if the device is considered roaming on the current
822     * network, for GSM purposes.
823     * <p>
824     * Availability: Only when user registered to a network.
825     */
826    public boolean isNetworkRoaming() {
827        return "true".equals(SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING));
828    }
829
830    /**
831     * Returns the ISO country code equivalent of the current registered
832     * operator's MCC (Mobile Country Code).
833     * <p>
834     * Availability: Only when user is registered to a network. Result may be
835     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
836     * on a CDMA network).
837     */
838    public String getNetworkCountryIso() {
839        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY);
840    }
841
842    /** Network type is unknown */
843    public static final int NETWORK_TYPE_UNKNOWN = 0;
844    /** Current network is GPRS */
845    public static final int NETWORK_TYPE_GPRS = 1;
846    /** Current network is EDGE */
847    public static final int NETWORK_TYPE_EDGE = 2;
848    /** Current network is UMTS */
849    public static final int NETWORK_TYPE_UMTS = 3;
850    /** Current network is CDMA: Either IS95A or IS95B*/
851    public static final int NETWORK_TYPE_CDMA = 4;
852    /** Current network is EVDO revision 0*/
853    public static final int NETWORK_TYPE_EVDO_0 = 5;
854    /** Current network is EVDO revision A*/
855    public static final int NETWORK_TYPE_EVDO_A = 6;
856    /** Current network is 1xRTT*/
857    public static final int NETWORK_TYPE_1xRTT = 7;
858    /** Current network is HSDPA */
859    public static final int NETWORK_TYPE_HSDPA = 8;
860    /** Current network is HSUPA */
861    public static final int NETWORK_TYPE_HSUPA = 9;
862    /** Current network is HSPA */
863    public static final int NETWORK_TYPE_HSPA = 10;
864    /** Current network is iDen */
865    public static final int NETWORK_TYPE_IDEN = 11;
866    /** Current network is EVDO revision B*/
867    public static final int NETWORK_TYPE_EVDO_B = 12;
868    /** Current network is LTE */
869    public static final int NETWORK_TYPE_LTE = 13;
870    /** Current network is eHRPD */
871    public static final int NETWORK_TYPE_EHRPD = 14;
872    /** Current network is HSPA+ */
873    public static final int NETWORK_TYPE_HSPAP = 15;
874
875    /**
876     * @return the NETWORK_TYPE_xxxx for current data connection.
877     */
878    public int getNetworkType() {
879        return getDataNetworkType();
880    }
881
882    /**
883     * Returns a constant indicating the radio technology (network type)
884     * currently in use on the device for data transmission.
885     * @return the network type
886     *
887     * @see #NETWORK_TYPE_UNKNOWN
888     * @see #NETWORK_TYPE_GPRS
889     * @see #NETWORK_TYPE_EDGE
890     * @see #NETWORK_TYPE_UMTS
891     * @see #NETWORK_TYPE_HSDPA
892     * @see #NETWORK_TYPE_HSUPA
893     * @see #NETWORK_TYPE_HSPA
894     * @see #NETWORK_TYPE_CDMA
895     * @see #NETWORK_TYPE_EVDO_0
896     * @see #NETWORK_TYPE_EVDO_A
897     * @see #NETWORK_TYPE_EVDO_B
898     * @see #NETWORK_TYPE_1xRTT
899     * @see #NETWORK_TYPE_IDEN
900     * @see #NETWORK_TYPE_LTE
901     * @see #NETWORK_TYPE_EHRPD
902     * @see #NETWORK_TYPE_HSPAP
903     *
904     * @hide
905     */
906    public int getDataNetworkType() {
907        try{
908            ITelephony telephony = getITelephony();
909            if (telephony != null) {
910                return telephony.getDataNetworkType();
911            } else {
912                // This can happen when the ITelephony interface is not up yet.
913                return NETWORK_TYPE_UNKNOWN;
914            }
915        } catch(RemoteException ex) {
916            // This shouldn't happen in the normal case
917            return NETWORK_TYPE_UNKNOWN;
918        } catch (NullPointerException ex) {
919            // This could happen before phone restarts due to crashing
920            return NETWORK_TYPE_UNKNOWN;
921        }
922    }
923
924    /**
925     * Returns the NETWORK_TYPE_xxxx for voice
926     *
927     * @hide
928     */
929    public int getVoiceNetworkType() {
930        try{
931            ITelephony telephony = getITelephony();
932            if (telephony != null) {
933                return telephony.getVoiceNetworkType();
934            } else {
935                // This can happen when the ITelephony interface is not up yet.
936                return NETWORK_TYPE_UNKNOWN;
937            }
938        } catch(RemoteException ex) {
939            // This shouldn't happen in the normal case
940            return NETWORK_TYPE_UNKNOWN;
941        } catch (NullPointerException ex) {
942            // This could happen before phone restarts due to crashing
943            return NETWORK_TYPE_UNKNOWN;
944        }
945    }
946
947    /** Unknown network class. {@hide} */
948    public static final int NETWORK_CLASS_UNKNOWN = 0;
949    /** Class of broadly defined "2G" networks. {@hide} */
950    public static final int NETWORK_CLASS_2_G = 1;
951    /** Class of broadly defined "3G" networks. {@hide} */
952    public static final int NETWORK_CLASS_3_G = 2;
953    /** Class of broadly defined "4G" networks. {@hide} */
954    public static final int NETWORK_CLASS_4_G = 3;
955
956    /**
957     * Return general class of network type, such as "3G" or "4G". In cases
958     * where classification is contentious, this method is conservative.
959     *
960     * @hide
961     */
962    public static int getNetworkClass(int networkType) {
963        switch (networkType) {
964            case NETWORK_TYPE_GPRS:
965            case NETWORK_TYPE_EDGE:
966            case NETWORK_TYPE_CDMA:
967            case NETWORK_TYPE_1xRTT:
968            case NETWORK_TYPE_IDEN:
969                return NETWORK_CLASS_2_G;
970            case NETWORK_TYPE_UMTS:
971            case NETWORK_TYPE_EVDO_0:
972            case NETWORK_TYPE_EVDO_A:
973            case NETWORK_TYPE_HSDPA:
974            case NETWORK_TYPE_HSUPA:
975            case NETWORK_TYPE_HSPA:
976            case NETWORK_TYPE_EVDO_B:
977            case NETWORK_TYPE_EHRPD:
978            case NETWORK_TYPE_HSPAP:
979                return NETWORK_CLASS_3_G;
980            case NETWORK_TYPE_LTE:
981                return NETWORK_CLASS_4_G;
982            default:
983                return NETWORK_CLASS_UNKNOWN;
984        }
985    }
986
987    /**
988     * Returns a string representation of the radio technology (network type)
989     * currently in use on the device.
990     * @return the name of the radio technology
991     *
992     * @hide pending API council review
993     */
994    public String getNetworkTypeName() {
995        return getNetworkTypeName(getNetworkType());
996    }
997
998    /** {@hide} */
999    public static String getNetworkTypeName(int type) {
1000        switch (type) {
1001            case NETWORK_TYPE_GPRS:
1002                return "GPRS";
1003            case NETWORK_TYPE_EDGE:
1004                return "EDGE";
1005            case NETWORK_TYPE_UMTS:
1006                return "UMTS";
1007            case NETWORK_TYPE_HSDPA:
1008                return "HSDPA";
1009            case NETWORK_TYPE_HSUPA:
1010                return "HSUPA";
1011            case NETWORK_TYPE_HSPA:
1012                return "HSPA";
1013            case NETWORK_TYPE_CDMA:
1014                return "CDMA";
1015            case NETWORK_TYPE_EVDO_0:
1016                return "CDMA - EvDo rev. 0";
1017            case NETWORK_TYPE_EVDO_A:
1018                return "CDMA - EvDo rev. A";
1019            case NETWORK_TYPE_EVDO_B:
1020                return "CDMA - EvDo rev. B";
1021            case NETWORK_TYPE_1xRTT:
1022                return "CDMA - 1xRTT";
1023            case NETWORK_TYPE_LTE:
1024                return "LTE";
1025            case NETWORK_TYPE_EHRPD:
1026                return "CDMA - eHRPD";
1027            case NETWORK_TYPE_IDEN:
1028                return "iDEN";
1029            case NETWORK_TYPE_HSPAP:
1030                return "HSPA+";
1031            default:
1032                return "UNKNOWN";
1033        }
1034    }
1035
1036    //
1037    //
1038    // SIM Card
1039    //
1040    //
1041
1042    /** SIM card state: Unknown. Signifies that the SIM is in transition
1043     *  between states. For example, when the user inputs the SIM pin
1044     *  under PIN_REQUIRED state, a query for sim status returns
1045     *  this state before turning to SIM_STATE_READY. */
1046    public static final int SIM_STATE_UNKNOWN = 0;
1047    /** SIM card state: no SIM card is available in the device */
1048    public static final int SIM_STATE_ABSENT = 1;
1049    /** SIM card state: Locked: requires the user's SIM PIN to unlock */
1050    public static final int SIM_STATE_PIN_REQUIRED = 2;
1051    /** SIM card state: Locked: requires the user's SIM PUK to unlock */
1052    public static final int SIM_STATE_PUK_REQUIRED = 3;
1053    /** SIM card state: Locked: requries a network PIN to unlock */
1054    public static final int SIM_STATE_NETWORK_LOCKED = 4;
1055    /** SIM card state: Ready */
1056    public static final int SIM_STATE_READY = 5;
1057
1058    /**
1059     * @return true if a ICC card is present
1060     */
1061    public boolean hasIccCard() {
1062        try {
1063            return getITelephony().hasIccCard();
1064        } catch (RemoteException ex) {
1065            // Assume no ICC card if remote exception which shouldn't happen
1066            return false;
1067        } catch (NullPointerException ex) {
1068            // This could happen before phone restarts due to crashing
1069            return false;
1070        }
1071    }
1072
1073    /**
1074     * Returns a constant indicating the state of the
1075     * device SIM card.
1076     *
1077     * @see #SIM_STATE_UNKNOWN
1078     * @see #SIM_STATE_ABSENT
1079     * @see #SIM_STATE_PIN_REQUIRED
1080     * @see #SIM_STATE_PUK_REQUIRED
1081     * @see #SIM_STATE_NETWORK_LOCKED
1082     * @see #SIM_STATE_READY
1083     */
1084    public int getSimState() {
1085        String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE);
1086        if ("ABSENT".equals(prop)) {
1087            return SIM_STATE_ABSENT;
1088        }
1089        else if ("PIN_REQUIRED".equals(prop)) {
1090            return SIM_STATE_PIN_REQUIRED;
1091        }
1092        else if ("PUK_REQUIRED".equals(prop)) {
1093            return SIM_STATE_PUK_REQUIRED;
1094        }
1095        else if ("NETWORK_LOCKED".equals(prop)) {
1096            return SIM_STATE_NETWORK_LOCKED;
1097        }
1098        else if ("READY".equals(prop)) {
1099            return SIM_STATE_READY;
1100        }
1101        else {
1102            return SIM_STATE_UNKNOWN;
1103        }
1104    }
1105
1106    /**
1107     * Returns the MCC+MNC (mobile country code + mobile network code) of the
1108     * provider of the SIM. 5 or 6 decimal digits.
1109     * <p>
1110     * Availability: SIM state must be {@link #SIM_STATE_READY}
1111     *
1112     * @see #getSimState
1113     */
1114    public String getSimOperator() {
1115        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC);
1116    }
1117
1118    /**
1119     * Returns the Service Provider Name (SPN).
1120     * <p>
1121     * Availability: SIM state must be {@link #SIM_STATE_READY}
1122     *
1123     * @see #getSimState
1124     */
1125    public String getSimOperatorName() {
1126        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA);
1127    }
1128
1129    /**
1130     * Returns the ISO country code equivalent for the SIM provider's country code.
1131     */
1132    public String getSimCountryIso() {
1133        return SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY);
1134    }
1135
1136    /**
1137     * Returns the serial number of the SIM, if applicable. Return null if it is
1138     * unavailable.
1139     * <p>
1140     * Requires Permission:
1141     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1142     */
1143    public String getSimSerialNumber() {
1144        try {
1145            return getSubscriberInfo().getIccSerialNumber();
1146        } catch (RemoteException ex) {
1147            return null;
1148        } catch (NullPointerException ex) {
1149            // This could happen before phone restarts due to crashing
1150            return null;
1151        }
1152    }
1153
1154    /**
1155     * Return if the current radio is LTE on CDMA. This
1156     * is a tri-state return value as for a period of time
1157     * the mode may be unknown.
1158     *
1159     * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE}
1160     * or {@link PhoneConstants#LTE_ON_CDMA_TRUE}
1161     *
1162     * @hide
1163     */
1164    public int getLteOnCdmaMode() {
1165        try {
1166            return getITelephony().getLteOnCdmaMode();
1167        } catch (RemoteException ex) {
1168            // Assume no ICC card if remote exception which shouldn't happen
1169            return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1170        } catch (NullPointerException ex) {
1171            // This could happen before phone restarts due to crashing
1172            return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1173        }
1174    }
1175
1176    //
1177    //
1178    // Subscriber Info
1179    //
1180    //
1181
1182    /**
1183     * Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
1184     * Return null if it is unavailable.
1185     * <p>
1186     * Requires Permission:
1187     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1188     */
1189    public String getSubscriberId() {
1190        try {
1191            return getSubscriberInfo().getSubscriberId();
1192        } catch (RemoteException ex) {
1193            return null;
1194        } catch (NullPointerException ex) {
1195            // This could happen before phone restarts due to crashing
1196            return null;
1197        }
1198    }
1199
1200    /**
1201     * Returns the Group Identifier Level1 for a GSM phone.
1202     * Return null if it is unavailable.
1203     * <p>
1204     * Requires Permission:
1205     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1206     */
1207    public String getGroupIdLevel1() {
1208        try {
1209            return getSubscriberInfo().getGroupIdLevel1();
1210        } catch (RemoteException ex) {
1211            return null;
1212        } catch (NullPointerException ex) {
1213            // This could happen before phone restarts due to crashing
1214            return null;
1215        }
1216    }
1217
1218    /**
1219     * Returns the phone number string for line 1, for example, the MSISDN
1220     * for a GSM phone. Return null if it is unavailable.
1221     * <p>
1222     * Requires Permission:
1223     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1224     */
1225    public String getLine1Number() {
1226        try {
1227            return getSubscriberInfo().getLine1Number();
1228        } catch (RemoteException ex) {
1229            return null;
1230        } catch (NullPointerException ex) {
1231            // This could happen before phone restarts due to crashing
1232            return null;
1233        }
1234    }
1235
1236    /**
1237     * Returns the alphabetic identifier associated with the line 1 number.
1238     * Return null if it is unavailable.
1239     * <p>
1240     * Requires Permission:
1241     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1242     * @hide
1243     * nobody seems to call this.
1244     */
1245    public String getLine1AlphaTag() {
1246        try {
1247            return getSubscriberInfo().getLine1AlphaTag();
1248        } catch (RemoteException ex) {
1249            return null;
1250        } catch (NullPointerException ex) {
1251            // This could happen before phone restarts due to crashing
1252            return null;
1253        }
1254    }
1255
1256    /**
1257     * Returns the MSISDN string.
1258     * for a GSM phone. Return null if it is unavailable.
1259     * <p>
1260     * Requires Permission:
1261     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1262     *
1263     * @hide
1264     */
1265    public String getMsisdn() {
1266        try {
1267            return getSubscriberInfo().getMsisdn();
1268        } catch (RemoteException ex) {
1269            return null;
1270        } catch (NullPointerException ex) {
1271            // This could happen before phone restarts due to crashing
1272            return null;
1273        }
1274    }
1275
1276    /**
1277     * Returns the voice mail number. Return null if it is unavailable.
1278     * <p>
1279     * Requires Permission:
1280     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1281     */
1282    public String getVoiceMailNumber() {
1283        try {
1284            return getSubscriberInfo().getVoiceMailNumber();
1285        } catch (RemoteException ex) {
1286            return null;
1287        } catch (NullPointerException ex) {
1288            // This could happen before phone restarts due to crashing
1289            return null;
1290        }
1291    }
1292
1293    /**
1294     * Returns the complete voice mail number. Return null if it is unavailable.
1295     * <p>
1296     * Requires Permission:
1297     *   {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED}
1298     *
1299     * @hide
1300     */
1301    public String getCompleteVoiceMailNumber() {
1302        try {
1303            return getSubscriberInfo().getCompleteVoiceMailNumber();
1304        } catch (RemoteException ex) {
1305            return null;
1306        } catch (NullPointerException ex) {
1307            // This could happen before phone restarts due to crashing
1308            return null;
1309        }
1310    }
1311
1312    /**
1313     * Returns the voice mail count. Return 0 if unavailable.
1314     * <p>
1315     * Requires Permission:
1316     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1317     * @hide
1318     */
1319    public int getVoiceMessageCount() {
1320        try {
1321            return getITelephony().getVoiceMessageCount();
1322        } catch (RemoteException ex) {
1323            return 0;
1324        } catch (NullPointerException ex) {
1325            // This could happen before phone restarts due to crashing
1326            return 0;
1327        }
1328    }
1329
1330    /**
1331     * Retrieves the alphabetic identifier associated with the voice
1332     * mail number.
1333     * <p>
1334     * Requires Permission:
1335     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1336     */
1337    public String getVoiceMailAlphaTag() {
1338        try {
1339            return getSubscriberInfo().getVoiceMailAlphaTag();
1340        } catch (RemoteException ex) {
1341            return null;
1342        } catch (NullPointerException ex) {
1343            // This could happen before phone restarts due to crashing
1344            return null;
1345        }
1346    }
1347
1348    /**
1349     * Returns the IMS private user identity (IMPI) that was loaded from the ISIM.
1350     * @return the IMPI, or null if not present or not loaded
1351     * @hide
1352     */
1353    public String getIsimImpi() {
1354        try {
1355            return getSubscriberInfo().getIsimImpi();
1356        } catch (RemoteException ex) {
1357            return null;
1358        } catch (NullPointerException ex) {
1359            // This could happen before phone restarts due to crashing
1360            return null;
1361        }
1362    }
1363
1364    /**
1365     * Returns the IMS home network domain name that was loaded from the ISIM.
1366     * @return the IMS domain name, or null if not present or not loaded
1367     * @hide
1368     */
1369    public String getIsimDomain() {
1370        try {
1371            return getSubscriberInfo().getIsimDomain();
1372        } catch (RemoteException ex) {
1373            return null;
1374        } catch (NullPointerException ex) {
1375            // This could happen before phone restarts due to crashing
1376            return null;
1377        }
1378    }
1379
1380    /**
1381     * Returns the IMS public user identities (IMPU) that were loaded from the ISIM.
1382     * @return an array of IMPU strings, with one IMPU per string, or null if
1383     *      not present or not loaded
1384     * @hide
1385     */
1386    public String[] getIsimImpu() {
1387        try {
1388            return getSubscriberInfo().getIsimImpu();
1389        } catch (RemoteException ex) {
1390            return null;
1391        } catch (NullPointerException ex) {
1392            // This could happen before phone restarts due to crashing
1393            return null;
1394        }
1395    }
1396
1397    private IPhoneSubInfo getSubscriberInfo() {
1398        // get it each time because that process crashes a lot
1399        return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
1400    }
1401
1402
1403    /** Device call state: No activity. */
1404    public static final int CALL_STATE_IDLE = 0;
1405    /** Device call state: Ringing. A new call arrived and is
1406     *  ringing or waiting. In the latter case, another call is
1407     *  already active. */
1408    public static final int CALL_STATE_RINGING = 1;
1409    /** Device call state: Off-hook. At least one call exists
1410      * that is dialing, active, or on hold, and no calls are ringing
1411      * or waiting. */
1412    public static final int CALL_STATE_OFFHOOK = 2;
1413
1414    /**
1415     * Returns a constant indicating the call state (cellular) on the device.
1416     */
1417    public int getCallState() {
1418        try {
1419            return getITelephony().getCallState();
1420        } catch (RemoteException ex) {
1421            // the phone process is restarting.
1422            return CALL_STATE_IDLE;
1423        } catch (NullPointerException ex) {
1424          // the phone process is restarting.
1425          return CALL_STATE_IDLE;
1426      }
1427    }
1428
1429    /** Data connection activity: No traffic. */
1430    public static final int DATA_ACTIVITY_NONE = 0x00000000;
1431    /** Data connection activity: Currently receiving IP PPP traffic. */
1432    public static final int DATA_ACTIVITY_IN = 0x00000001;
1433    /** Data connection activity: Currently sending IP PPP traffic. */
1434    public static final int DATA_ACTIVITY_OUT = 0x00000002;
1435    /** Data connection activity: Currently both sending and receiving
1436     *  IP PPP traffic. */
1437    public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT;
1438    /**
1439     * Data connection is active, but physical link is down
1440     */
1441    public static final int DATA_ACTIVITY_DORMANT = 0x00000004;
1442
1443    /**
1444     * Returns a constant indicating the type of activity on a data connection
1445     * (cellular).
1446     *
1447     * @see #DATA_ACTIVITY_NONE
1448     * @see #DATA_ACTIVITY_IN
1449     * @see #DATA_ACTIVITY_OUT
1450     * @see #DATA_ACTIVITY_INOUT
1451     * @see #DATA_ACTIVITY_DORMANT
1452     */
1453    public int getDataActivity() {
1454        try {
1455            return getITelephony().getDataActivity();
1456        } catch (RemoteException ex) {
1457            // the phone process is restarting.
1458            return DATA_ACTIVITY_NONE;
1459        } catch (NullPointerException ex) {
1460          // the phone process is restarting.
1461          return DATA_ACTIVITY_NONE;
1462      }
1463    }
1464
1465    /** Data connection state: Unknown.  Used before we know the state.
1466     * @hide
1467     */
1468    public static final int DATA_UNKNOWN        = -1;
1469    /** Data connection state: Disconnected. IP traffic not available. */
1470    public static final int DATA_DISCONNECTED   = 0;
1471    /** Data connection state: Currently setting up a data connection. */
1472    public static final int DATA_CONNECTING     = 1;
1473    /** Data connection state: Connected. IP traffic should be available. */
1474    public static final int DATA_CONNECTED      = 2;
1475    /** Data connection state: Suspended. The connection is up, but IP
1476     * traffic is temporarily unavailable. For example, in a 2G network,
1477     * data activity may be suspended when a voice call arrives. */
1478    public static final int DATA_SUSPENDED      = 3;
1479
1480    /**
1481     * Returns a constant indicating the current data connection state
1482     * (cellular).
1483     *
1484     * @see #DATA_DISCONNECTED
1485     * @see #DATA_CONNECTING
1486     * @see #DATA_CONNECTED
1487     * @see #DATA_SUSPENDED
1488     */
1489    public int getDataState() {
1490        try {
1491            return getITelephony().getDataState();
1492        } catch (RemoteException ex) {
1493            // the phone process is restarting.
1494            return DATA_DISCONNECTED;
1495        } catch (NullPointerException ex) {
1496            return DATA_DISCONNECTED;
1497        }
1498    }
1499
1500    private ITelephony getITelephony() {
1501        return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
1502    }
1503
1504    //
1505    //
1506    // PhoneStateListener
1507    //
1508    //
1509
1510    /**
1511     * Registers a listener object to receive notification of changes
1512     * in specified telephony states.
1513     * <p>
1514     * To register a listener, pass a {@link PhoneStateListener}
1515     * and specify at least one telephony state of interest in
1516     * the events argument.
1517     *
1518     * At registration, and when a specified telephony state
1519     * changes, the telephony manager invokes the appropriate
1520     * callback method on the listener object and passes the
1521     * current (updated) values.
1522     * <p>
1523     * To unregister a listener, pass the listener object and set the
1524     * events argument to
1525     * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0).
1526     *
1527     * @param listener The {@link PhoneStateListener} object to register
1528     *                 (or unregister)
1529     * @param events The telephony state(s) of interest to the listener,
1530     *               as a bitwise-OR combination of {@link PhoneStateListener}
1531     *               LISTEN_ flags.
1532     */
1533    public void listen(PhoneStateListener listener, int events) {
1534        String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>";
1535        try {
1536            Boolean notifyNow = true;
1537            sRegistry.listen(pkgForDebug, listener.callback, events, notifyNow);
1538        } catch (RemoteException ex) {
1539            // system process dead
1540        } catch (NullPointerException ex) {
1541            // system process dead
1542        }
1543    }
1544
1545    /**
1546     * Returns the CDMA ERI icon index to display
1547     *
1548     * @hide
1549     */
1550    public int getCdmaEriIconIndex() {
1551        try {
1552            return getITelephony().getCdmaEriIconIndex();
1553        } catch (RemoteException ex) {
1554            // the phone process is restarting.
1555            return -1;
1556        } catch (NullPointerException ex) {
1557            return -1;
1558        }
1559    }
1560
1561    /**
1562     * Returns the CDMA ERI icon mode,
1563     * 0 - ON
1564     * 1 - FLASHING
1565     *
1566     * @hide
1567     */
1568    public int getCdmaEriIconMode() {
1569        try {
1570            return getITelephony().getCdmaEriIconMode();
1571        } catch (RemoteException ex) {
1572            // the phone process is restarting.
1573            return -1;
1574        } catch (NullPointerException ex) {
1575            return -1;
1576        }
1577    }
1578
1579    /**
1580     * Returns the CDMA ERI text,
1581     *
1582     * @hide
1583     */
1584    public String getCdmaEriText() {
1585        try {
1586            return getITelephony().getCdmaEriText();
1587        } catch (RemoteException ex) {
1588            // the phone process is restarting.
1589            return null;
1590        } catch (NullPointerException ex) {
1591            return null;
1592        }
1593    }
1594
1595    /**
1596     * @return true if the current device is "voice capable".
1597     * <p>
1598     * "Voice capable" means that this device supports circuit-switched
1599     * (i.e. voice) phone calls over the telephony network, and is allowed
1600     * to display the in-call UI while a cellular voice call is active.
1601     * This will be false on "data only" devices which can't make voice
1602     * calls and don't support any in-call UI.
1603     * <p>
1604     * Note: the meaning of this flag is subtly different from the
1605     * PackageManager.FEATURE_TELEPHONY system feature, which is available
1606     * on any device with a telephony radio, even if the device is
1607     * data-only.
1608     *
1609     * @hide pending API review
1610     */
1611    public boolean isVoiceCapable() {
1612        if (mContext == null) return true;
1613        return mContext.getResources().getBoolean(
1614                com.android.internal.R.bool.config_voice_capable);
1615    }
1616
1617    /**
1618     * @return true if the current device supports sms service.
1619     * <p>
1620     * If true, this means that the device supports both sending and
1621     * receiving sms via the telephony network.
1622     * <p>
1623     * Note: Voicemail waiting sms, cell broadcasting sms, and MMS are
1624     *       disabled when device doesn't support sms.
1625     *
1626     * @hide pending API review
1627     */
1628    public boolean isSmsCapable() {
1629        if (mContext == null) return true;
1630        return mContext.getResources().getBoolean(
1631                com.android.internal.R.bool.config_sms_capable);
1632    }
1633
1634    /**
1635     * Returns all observed cell information from all radios on the
1636     * device including the primary and neighboring cells. This does
1637     * not cause or change the rate of PhoneStateListner#onCellInfoChanged.
1638     *<p>
1639     * The list can include one or more of {@link android.telephony.CellInfoGsm CellInfoGsm},
1640     * {@link android.telephony.CellInfoCdma CellInfoCdma},
1641     * {@link android.telephony.CellInfoLte CellInfoLte} and
1642     * {@link android.telephony.CellInfoWcdma CellInfoCdma} in any combination.
1643     * Specifically on devices with multiple radios it is typical to see instances of
1644     * one or more of any these in the list. In addition 0, 1 or more CellInfo
1645     * objects may return isRegistered() true.
1646     *<p>
1647     * This is preferred over using getCellLocation although for older
1648     * devices this may return null in which case getCellLocation should
1649     * be called.
1650     *<p>
1651     * @return List of CellInfo or null if info unavailable.
1652     *
1653     * <p>Requires Permission: {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}
1654     */
1655    public List<CellInfo> getAllCellInfo() {
1656        try {
1657            return getITelephony().getAllCellInfo();
1658        } catch (RemoteException ex) {
1659            return null;
1660        } catch (NullPointerException ex) {
1661            return null;
1662        }
1663    }
1664
1665    /**
1666     * Sets the minimum time in milli-seconds between {@link PhoneStateListener#onCellInfoChanged
1667     * PhoneStateListener.onCellInfoChanged} will be invoked.
1668     *<p>
1669     * The default, 0, means invoke onCellInfoChanged when any of the reported
1670     * information changes. Setting the value to INT_MAX(0x7fffffff) means never issue
1671     * A onCellInfoChanged.
1672     *<p>
1673     * @param rateInMillis the rate
1674     *
1675     * @hide
1676     */
1677    public void setCellInfoListRate(int rateInMillis) {
1678        try {
1679            getITelephony().setCellInfoListRate(rateInMillis);
1680        } catch (RemoteException ex) {
1681        } catch (NullPointerException ex) {
1682        }
1683    }
1684
1685    /**
1686     * Returns the MMS user agent.
1687     */
1688    public String getMmsUserAgent() {
1689        if (mContext == null) return null;
1690        return mContext.getResources().getString(
1691                com.android.internal.R.string.config_mms_user_agent);
1692    }
1693
1694    /**
1695     * Returns the MMS user agent profile URL.
1696     */
1697    public String getMmsUAProfUrl() {
1698        if (mContext == null) return null;
1699        return mContext.getResources().getString(
1700                com.android.internal.R.string.config_mms_user_agent_profile_url);
1701    }
1702
1703    /**
1704     * Opens a logical channel to the ICC card.
1705     *
1706     * Input parameters equivalent to TS 27.007 AT+CCHO command.
1707     *
1708     * @param AID Application id. See ETSI 102.221 and 101.220.
1709     * @return The logical channel id which is negative on error.
1710     */
1711    public int iccOpenLogicalChannel(String AID) {
1712        try {
1713          return getITelephony().iccOpenLogicalChannel(AID);
1714        } catch (RemoteException ex) {
1715        } catch (NullPointerException ex) {
1716        }
1717        return -1;
1718    }
1719
1720    /**
1721     * Closes a previously opened logical channel to the ICC card.
1722     *
1723     * Input parameters equivalent to TS 27.007 AT+CCHC command.
1724     *
1725     * @param channel is the channel id to be closed as retruned by a successful
1726     *            iccOpenLogicalChannel.
1727     * @return true if the channel was closed successfully.
1728     */
1729    public boolean iccCloseLogicalChannel(int channel) {
1730        try {
1731          return getITelephony().iccCloseLogicalChannel(channel);
1732        } catch (RemoteException ex) {
1733        } catch (NullPointerException ex) {
1734        }
1735        return false;
1736    }
1737
1738    /**
1739     * Transmit an APDU to the ICC card over a logical channel.
1740     *
1741     * Input parameters equivalent to TS 27.007 AT+CGLA command.
1742     *
1743     * @param channel is the channel id to be closed as returned by a successful
1744     *            iccOpenLogicalChannel.
1745     * @param cla Class of the APDU command.
1746     * @param instruction Instruction of the APDU command.
1747     * @param p1 P1 value of the APDU command.
1748     * @param p2 P2 value of the APDU command.
1749     * @param p3 P3 value of the APDU command. If p3 is negative a 4 byte APDU
1750     *            is sent to the SIM.
1751     * @param data Data to be sent with the APDU.
1752     * @return The APDU response from the ICC card with the status appended at
1753     *            the end. If an error occurs, an empty string is returned.
1754     */
1755    public String iccTransmitApduLogicalChannel(int channel, int cla,
1756            int instruction, int p1, int p2, int p3, String data) {
1757        try {
1758          return getITelephony().iccTransmitApduLogicalChannel(channel, cla,
1759                  instruction, p1, p2, p3, data);
1760        } catch (RemoteException ex) {
1761        } catch (NullPointerException ex) {
1762        }
1763        return "";
1764    }
1765
1766    /**
1767     * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}.
1768     * Used for device configuration by some CDMA operators.
1769     *
1770     * @param itemID the ID of the item to read.
1771     * @return the NV item as a String, or null on any failure.
1772     * @hide
1773     */
1774    public String nvReadItem(int itemID) {
1775        try {
1776            return getITelephony().nvReadItem(itemID);
1777        } catch (RemoteException ex) {
1778            Rlog.e(TAG, "nvReadItem RemoteException", ex);
1779        } catch (NullPointerException ex) {
1780            Rlog.e(TAG, "nvReadItem NPE", ex);
1781        }
1782        return "";
1783    }
1784
1785
1786    /**
1787     * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}.
1788     * Used for device configuration by some CDMA operators.
1789     *
1790     * @param itemID the ID of the item to read.
1791     * @param itemValue the value to write, as a String.
1792     * @return true on success; false on any failure.
1793     * @hide
1794     */
1795    public boolean nvWriteItem(int itemID, String itemValue) {
1796        try {
1797            return getITelephony().nvWriteItem(itemID, itemValue);
1798        } catch (RemoteException ex) {
1799            Rlog.e(TAG, "nvWriteItem RemoteException", ex);
1800        } catch (NullPointerException ex) {
1801            Rlog.e(TAG, "nvWriteItem NPE", ex);
1802        }
1803        return false;
1804    }
1805
1806    /**
1807     * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1808     * Used for device configuration by some CDMA operators.
1809     *
1810     * @param preferredRoamingList byte array containing the new PRL.
1811     * @return true on success; false on any failure.
1812     * @hide
1813     */
1814    public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
1815        try {
1816            return getITelephony().nvWriteCdmaPrl(preferredRoamingList);
1817        } catch (RemoteException ex) {
1818            Rlog.e(TAG, "nvWriteCdmaPrl RemoteException", ex);
1819        } catch (NullPointerException ex) {
1820            Rlog.e(TAG, "nvWriteCdmaPrl NPE", ex);
1821        }
1822        return false;
1823    }
1824
1825    /**
1826     * Perform the specified type of NV config reset. The radio will be taken offline
1827     * and the device must be rebooted after the operation. Used for device
1828     * configuration by some CDMA operators.
1829     *
1830     * @param resetType reset type: 1: reload NV reset, 2: erase NV reset, 3: factory NV reset
1831     * @return true on success; false on any failure.
1832     * @hide
1833     */
1834    public boolean nvResetConfig(int resetType) {
1835        try {
1836            return getITelephony().nvResetConfig(resetType);
1837        } catch (RemoteException ex) {
1838            Rlog.e(TAG, "nvResetConfig RemoteException", ex);
1839        } catch (NullPointerException ex) {
1840            Rlog.e(TAG, "nvResetConfig NPE", ex);
1841        }
1842        return false;
1843    }
1844}
1845