TelephonyManager.java revision 60b41e6aa7a93bf90cb1b7715a2429dbe7c975fe
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.Nullable;
20import android.annotation.SystemApi;
21import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
23import android.content.ContentResolver;
24import android.content.Context;
25import android.content.Intent;
26import android.provider.Settings;
27import android.provider.Settings.SettingNotFoundException;
28import android.os.Bundle;
29import android.os.RemoteException;
30import android.os.ServiceManager;
31import android.os.SystemProperties;
32import android.util.Log;
33
34import com.android.internal.telecom.ITelecomService;
35import com.android.internal.telephony.IPhoneSubInfo;
36import com.android.internal.telephony.ITelephony;
37import com.android.internal.telephony.ITelephonyRegistry;
38import com.android.internal.telephony.PhoneConstants;
39import com.android.internal.telephony.RILConstants;
40import com.android.internal.telephony.TelephonyProperties;
41
42import java.io.FileInputStream;
43import java.io.IOException;
44import java.util.Arrays;
45import java.util.List;
46import java.util.Set;
47import java.util.regex.Matcher;
48import java.util.regex.Pattern;
49
50/**
51 * Provides access to information about the telephony services on
52 * the device. Applications can use the methods in this class to
53 * determine telephony services and states, as well as to access some
54 * types of subscriber information. Applications can also register
55 * a listener to receive notification of telephony state changes.
56 * <p>
57 * You do not instantiate this class directly; instead, you retrieve
58 * a reference to an instance through
59 * {@link android.content.Context#getSystemService
60 * Context.getSystemService(Context.TELEPHONY_SERVICE)}.
61 * <p>
62 * Note that access to some telephony information is
63 * permission-protected. Your application cannot access the protected
64 * information unless it has the appropriate permissions declared in
65 * its manifest file. Where permissions apply, they are noted in the
66 * the methods through which you access the protected information.
67 */
68public class TelephonyManager {
69    private static final String TAG = "TelephonyManager";
70
71    private static ITelephonyRegistry sRegistry;
72
73    /**
74     * The allowed states of Wi-Fi calling.
75     *
76     * @hide
77     */
78    public interface WifiCallingChoices {
79        /** Always use Wi-Fi calling */
80        static final int ALWAYS_USE = 0;
81        /** Ask the user whether to use Wi-Fi on every call */
82        static final int ASK_EVERY_TIME = 1;
83        /** Never use Wi-Fi calling */
84        static final int NEVER_USE = 2;
85    }
86
87    private final Context mContext;
88    private SubscriptionManager mSubscriptionManager;
89
90    private static String multiSimConfig =
91            SystemProperties.get(TelephonyProperties.PROPERTY_MULTI_SIM_CONFIG);
92
93    /** Enum indicating multisim variants
94     *  DSDS - Dual SIM Dual Standby
95     *  DSDA - Dual SIM Dual Active
96     *  TSTS - Triple SIM Triple Standby
97     **/
98    /** @hide */
99    public enum MultiSimVariants {
100        DSDS,
101        DSDA,
102        TSTS,
103        UNKNOWN
104    };
105
106    /** @hide */
107    public TelephonyManager(Context context) {
108        Context appContext = context.getApplicationContext();
109        if (appContext != null) {
110            mContext = appContext;
111        } else {
112            mContext = context;
113        }
114        mSubscriptionManager = SubscriptionManager.from(mContext);
115
116        if (sRegistry == null) {
117            sRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
118                    "telephony.registry"));
119        }
120    }
121
122    /** @hide */
123    private TelephonyManager() {
124        mContext = null;
125    }
126
127    private static TelephonyManager sInstance = new TelephonyManager();
128
129    /** @hide
130    /* @deprecated - use getSystemService as described above */
131    public static TelephonyManager getDefault() {
132        return sInstance;
133    }
134
135
136    /**
137     * Returns the multi SIM variant
138     * Returns DSDS for Dual SIM Dual Standby
139     * Returns DSDA for Dual SIM Dual Active
140     * Returns TSTS for Triple SIM Triple Standby
141     * Returns UNKNOWN for others
142     */
143    /** {@hide} */
144    public MultiSimVariants getMultiSimConfiguration() {
145        String mSimConfig =
146            SystemProperties.get(TelephonyProperties.PROPERTY_MULTI_SIM_CONFIG);
147        if (mSimConfig.equals("dsds")) {
148            return MultiSimVariants.DSDS;
149        } else if (mSimConfig.equals("dsda")) {
150            return MultiSimVariants.DSDA;
151        } else if (mSimConfig.equals("tsts")) {
152            return MultiSimVariants.TSTS;
153        } else {
154            return MultiSimVariants.UNKNOWN;
155        }
156    }
157
158
159    /**
160     * Returns the number of phones available.
161     * Returns 1 for Single standby mode (Single SIM functionality)
162     * Returns 2 for Dual standby mode.(Dual SIM functionality)
163     */
164    /** {@hide} */
165    public int getPhoneCount() {
166        int phoneCount = 1;
167        switch (getMultiSimConfiguration()) {
168            case UNKNOWN:
169                phoneCount = 1;
170                break;
171            case DSDS:
172            case DSDA:
173                phoneCount = PhoneConstants.MAX_PHONE_COUNT_DUAL_SIM;
174                break;
175            case TSTS:
176                phoneCount = PhoneConstants.MAX_PHONE_COUNT_TRI_SIM;
177                break;
178        }
179        return phoneCount;
180    }
181
182    /** {@hide} */
183    public static TelephonyManager from(Context context) {
184        return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
185    }
186
187    /** {@hide} */
188    public boolean isMultiSimEnabled() {
189        return (multiSimConfig.equals("dsds") || multiSimConfig.equals("dsda") ||
190            multiSimConfig.equals("tsts"));
191    }
192
193    //
194    // Broadcast Intent actions
195    //
196
197    /**
198     * Broadcast intent action indicating that the call state (cellular)
199     * on the device has changed.
200     *
201     * <p>
202     * The {@link #EXTRA_STATE} extra indicates the new call state.
203     * If the new state is RINGING, a second extra
204     * {@link #EXTRA_INCOMING_NUMBER} provides the incoming phone number as
205     * a String.
206     *
207     * <p class="note">
208     * Requires the READ_PHONE_STATE permission.
209     *
210     * <p class="note">
211     * This was a {@link android.content.Context#sendStickyBroadcast sticky}
212     * broadcast in version 1.0, but it is no longer sticky.
213     * Instead, use {@link #getCallState} to synchronously query the current call state.
214     *
215     * @see #EXTRA_STATE
216     * @see #EXTRA_INCOMING_NUMBER
217     * @see #getCallState
218     */
219    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
220    public static final String ACTION_PHONE_STATE_CHANGED =
221            "android.intent.action.PHONE_STATE";
222
223    /**
224     * The Phone app sends this intent when a user opts to respond-via-message during an incoming
225     * call. By default, the device's default SMS app consumes this message and sends a text message
226     * to the caller. A third party app can also provide this functionality by consuming this Intent
227     * with a {@link android.app.Service} and sending the message using its own messaging system.
228     * <p>The intent contains a URI (available from {@link android.content.Intent#getData})
229     * describing the recipient, using either the {@code sms:}, {@code smsto:}, {@code mms:},
230     * or {@code mmsto:} URI schema. Each of these URI schema carry the recipient information the
231     * same way: the path part of the URI contains the recipient's phone number or a comma-separated
232     * set of phone numbers if there are multiple recipients. For example, {@code
233     * smsto:2065551234}.</p>
234     *
235     * <p>The intent may also contain extras for the message text (in {@link
236     * android.content.Intent#EXTRA_TEXT}) and a message subject
237     * (in {@link android.content.Intent#EXTRA_SUBJECT}).</p>
238     *
239     * <p class="note"><strong>Note:</strong>
240     * The intent-filter that consumes this Intent needs to be in a {@link android.app.Service}
241     * that requires the
242     * permission {@link android.Manifest.permission#SEND_RESPOND_VIA_MESSAGE}.</p>
243     * <p>For example, the service that receives this intent can be declared in the manifest file
244     * with an intent filter like this:</p>
245     * <pre>
246     * &lt;!-- Service that delivers SMS messages received from the phone "quick response" -->
247     * &lt;service android:name=".HeadlessSmsSendService"
248     *          android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
249     *          android:exported="true" >
250     *   &lt;intent-filter>
251     *     &lt;action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
252     *     &lt;category android:name="android.intent.category.DEFAULT" />
253     *     &lt;data android:scheme="sms" />
254     *     &lt;data android:scheme="smsto" />
255     *     &lt;data android:scheme="mms" />
256     *     &lt;data android:scheme="mmsto" />
257     *   &lt;/intent-filter>
258     * &lt;/service></pre>
259     * <p>
260     * Output: nothing.
261     */
262    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
263    public static final String ACTION_RESPOND_VIA_MESSAGE =
264            "android.intent.action.RESPOND_VIA_MESSAGE";
265
266    /**
267     * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
268     * for a String containing the new call state.
269     *
270     * @see #EXTRA_STATE_IDLE
271     * @see #EXTRA_STATE_RINGING
272     * @see #EXTRA_STATE_OFFHOOK
273     *
274     * <p class="note">
275     * Retrieve with
276     * {@link android.content.Intent#getStringExtra(String)}.
277     */
278    public static final String EXTRA_STATE = PhoneConstants.STATE_KEY;
279
280    /**
281     * Value used with {@link #EXTRA_STATE} corresponding to
282     * {@link #CALL_STATE_IDLE}.
283     */
284    public static final String EXTRA_STATE_IDLE = PhoneConstants.State.IDLE.toString();
285
286    /**
287     * Value used with {@link #EXTRA_STATE} corresponding to
288     * {@link #CALL_STATE_RINGING}.
289     */
290    public static final String EXTRA_STATE_RINGING = PhoneConstants.State.RINGING.toString();
291
292    /**
293     * Value used with {@link #EXTRA_STATE} corresponding to
294     * {@link #CALL_STATE_OFFHOOK}.
295     */
296    public static final String EXTRA_STATE_OFFHOOK = PhoneConstants.State.OFFHOOK.toString();
297
298    /**
299     * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
300     * for a String containing the incoming phone number.
301     * Only valid when the new call state is RINGING.
302     *
303     * <p class="note">
304     * Retrieve with
305     * {@link android.content.Intent#getStringExtra(String)}.
306     */
307    public static final String EXTRA_INCOMING_NUMBER = "incoming_number";
308
309    /**
310     * Broadcast intent action indicating that a precise call state
311     * (cellular) on the device has changed.
312     *
313     * <p>
314     * The {@link #EXTRA_RINGING_CALL_STATE} extra indicates the ringing call state.
315     * The {@link #EXTRA_FOREGROUND_CALL_STATE} extra indicates the foreground call state.
316     * The {@link #EXTRA_BACKGROUND_CALL_STATE} extra indicates the background call state.
317     * The {@link #EXTRA_DISCONNECT_CAUSE} extra indicates the disconnect cause.
318     * The {@link #EXTRA_PRECISE_DISCONNECT_CAUSE} extra indicates the precise disconnect cause.
319     *
320     * <p class="note">
321     * Requires the READ_PRECISE_PHONE_STATE permission.
322     *
323     * @see #EXTRA_RINGING_CALL_STATE
324     * @see #EXTRA_FOREGROUND_CALL_STATE
325     * @see #EXTRA_BACKGROUND_CALL_STATE
326     * @see #EXTRA_DISCONNECT_CAUSE
327     * @see #EXTRA_PRECISE_DISCONNECT_CAUSE
328     *
329     * <p class="note">
330     * Requires the READ_PRECISE_PHONE_STATE permission.
331     *
332     * @hide
333     */
334    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
335    public static final String ACTION_PRECISE_CALL_STATE_CHANGED =
336            "android.intent.action.PRECISE_CALL_STATE";
337
338    /**
339     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
340     * for an integer containing the state of the current ringing call.
341     *
342     * @see PreciseCallState#PRECISE_CALL_STATE_NOT_VALID
343     * @see PreciseCallState#PRECISE_CALL_STATE_IDLE
344     * @see PreciseCallState#PRECISE_CALL_STATE_ACTIVE
345     * @see PreciseCallState#PRECISE_CALL_STATE_HOLDING
346     * @see PreciseCallState#PRECISE_CALL_STATE_DIALING
347     * @see PreciseCallState#PRECISE_CALL_STATE_ALERTING
348     * @see PreciseCallState#PRECISE_CALL_STATE_INCOMING
349     * @see PreciseCallState#PRECISE_CALL_STATE_WAITING
350     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTED
351     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTING
352     *
353     * <p class="note">
354     * Retrieve with
355     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
356     *
357     * @hide
358     */
359    public static final String EXTRA_RINGING_CALL_STATE = "ringing_state";
360
361    /**
362     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
363     * for an integer containing the state of the current foreground call.
364     *
365     * @see PreciseCallState#PRECISE_CALL_STATE_NOT_VALID
366     * @see PreciseCallState#PRECISE_CALL_STATE_IDLE
367     * @see PreciseCallState#PRECISE_CALL_STATE_ACTIVE
368     * @see PreciseCallState#PRECISE_CALL_STATE_HOLDING
369     * @see PreciseCallState#PRECISE_CALL_STATE_DIALING
370     * @see PreciseCallState#PRECISE_CALL_STATE_ALERTING
371     * @see PreciseCallState#PRECISE_CALL_STATE_INCOMING
372     * @see PreciseCallState#PRECISE_CALL_STATE_WAITING
373     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTED
374     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTING
375     *
376     * <p class="note">
377     * Retrieve with
378     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
379     *
380     * @hide
381     */
382    public static final String EXTRA_FOREGROUND_CALL_STATE = "foreground_state";
383
384    /**
385     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
386     * for an integer containing the state of the current background call.
387     *
388     * @see PreciseCallState#PRECISE_CALL_STATE_NOT_VALID
389     * @see PreciseCallState#PRECISE_CALL_STATE_IDLE
390     * @see PreciseCallState#PRECISE_CALL_STATE_ACTIVE
391     * @see PreciseCallState#PRECISE_CALL_STATE_HOLDING
392     * @see PreciseCallState#PRECISE_CALL_STATE_DIALING
393     * @see PreciseCallState#PRECISE_CALL_STATE_ALERTING
394     * @see PreciseCallState#PRECISE_CALL_STATE_INCOMING
395     * @see PreciseCallState#PRECISE_CALL_STATE_WAITING
396     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTED
397     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTING
398     *
399     * <p class="note">
400     * Retrieve with
401     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
402     *
403     * @hide
404     */
405    public static final String EXTRA_BACKGROUND_CALL_STATE = "background_state";
406
407    /**
408     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
409     * for an integer containing the disconnect cause.
410     *
411     * @see DisconnectCause
412     *
413     * <p class="note">
414     * Retrieve with
415     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
416     *
417     * @hide
418     */
419    public static final String EXTRA_DISCONNECT_CAUSE = "disconnect_cause";
420
421    /**
422     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
423     * for an integer containing the disconnect cause provided by the RIL.
424     *
425     * @see PreciseDisconnectCause
426     *
427     * <p class="note">
428     * Retrieve with
429     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
430     *
431     * @hide
432     */
433    public static final String EXTRA_PRECISE_DISCONNECT_CAUSE = "precise_disconnect_cause";
434
435    /**
436     * Broadcast intent action indicating a data connection has changed,
437     * providing precise information about the connection.
438     *
439     * <p>
440     * The {@link #EXTRA_DATA_STATE} extra indicates the connection state.
441     * The {@link #EXTRA_DATA_NETWORK_TYPE} extra indicates the connection network type.
442     * The {@link #EXTRA_DATA_APN_TYPE} extra indicates the APN type.
443     * The {@link #EXTRA_DATA_APN} extra indicates the APN.
444     * The {@link #EXTRA_DATA_CHANGE_REASON} extra indicates the connection change reason.
445     * The {@link #EXTRA_DATA_IFACE_PROPERTIES} extra indicates the connection interface.
446     * The {@link #EXTRA_DATA_FAILURE_CAUSE} extra indicates the connection fail cause.
447     *
448     * <p class="note">
449     * Requires the READ_PRECISE_PHONE_STATE permission.
450     *
451     * @see #EXTRA_DATA_STATE
452     * @see #EXTRA_DATA_NETWORK_TYPE
453     * @see #EXTRA_DATA_APN_TYPE
454     * @see #EXTRA_DATA_APN
455     * @see #EXTRA_DATA_CHANGE_REASON
456     * @see #EXTRA_DATA_IFACE
457     * @see #EXTRA_DATA_FAILURE_CAUSE
458     * @hide
459     */
460    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
461    public static final String ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED =
462            "android.intent.action.PRECISE_DATA_CONNECTION_STATE_CHANGED";
463
464    /**
465     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
466     * for an integer containing the state of the current data connection.
467     *
468     * @see TelephonyManager#DATA_UNKNOWN
469     * @see TelephonyManager#DATA_DISCONNECTED
470     * @see TelephonyManager#DATA_CONNECTING
471     * @see TelephonyManager#DATA_CONNECTED
472     * @see TelephonyManager#DATA_SUSPENDED
473     *
474     * <p class="note">
475     * Retrieve with
476     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
477     *
478     * @hide
479     */
480    public static final String EXTRA_DATA_STATE = PhoneConstants.STATE_KEY;
481
482    /**
483     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
484     * for an integer containing the network type.
485     *
486     * @see TelephonyManager#NETWORK_TYPE_UNKNOWN
487     * @see TelephonyManager#NETWORK_TYPE_GPRS
488     * @see TelephonyManager#NETWORK_TYPE_EDGE
489     * @see TelephonyManager#NETWORK_TYPE_UMTS
490     * @see TelephonyManager#NETWORK_TYPE_CDMA
491     * @see TelephonyManager#NETWORK_TYPE_EVDO_0
492     * @see TelephonyManager#NETWORK_TYPE_EVDO_A
493     * @see TelephonyManager#NETWORK_TYPE_1xRTT
494     * @see TelephonyManager#NETWORK_TYPE_HSDPA
495     * @see TelephonyManager#NETWORK_TYPE_HSUPA
496     * @see TelephonyManager#NETWORK_TYPE_HSPA
497     * @see TelephonyManager#NETWORK_TYPE_IDEN
498     * @see TelephonyManager#NETWORK_TYPE_EVDO_B
499     * @see TelephonyManager#NETWORK_TYPE_LTE
500     * @see TelephonyManager#NETWORK_TYPE_EHRPD
501     * @see TelephonyManager#NETWORK_TYPE_HSPAP
502     *
503     * <p class="note">
504     * Retrieve with
505     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
506     *
507     * @hide
508     */
509    public static final String EXTRA_DATA_NETWORK_TYPE = PhoneConstants.DATA_NETWORK_TYPE_KEY;
510
511    /**
512     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
513     * for an String containing the data APN type.
514     *
515     * <p class="note">
516     * Retrieve with
517     * {@link android.content.Intent#getStringExtra(String name)}.
518     *
519     * @hide
520     */
521    public static final String EXTRA_DATA_APN_TYPE = PhoneConstants.DATA_APN_TYPE_KEY;
522
523    /**
524     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
525     * for an String containing the data APN.
526     *
527     * <p class="note">
528     * Retrieve with
529     * {@link android.content.Intent#getStringExtra(String name)}.
530     *
531     * @hide
532     */
533    public static final String EXTRA_DATA_APN = PhoneConstants.DATA_APN_KEY;
534
535    /**
536     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
537     * for an String representation of the change reason.
538     *
539     * <p class="note">
540     * Retrieve with
541     * {@link android.content.Intent#getStringExtra(String name)}.
542     *
543     * @hide
544     */
545    public static final String EXTRA_DATA_CHANGE_REASON = PhoneConstants.STATE_CHANGE_REASON_KEY;
546
547    /**
548     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
549     * for an String representation of the data interface.
550     *
551     * <p class="note">
552     * Retrieve with
553     * {@link android.content.Intent#getParcelableExtra(String name)}.
554     *
555     * @hide
556     */
557    public static final String EXTRA_DATA_LINK_PROPERTIES_KEY = PhoneConstants.DATA_LINK_PROPERTIES_KEY;
558
559    /**
560     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
561     * for the data connection fail cause.
562     *
563     * <p class="note">
564     * Retrieve with
565     * {@link android.content.Intent#getStringExtra(String name)}.
566     *
567     * @hide
568     */
569    public static final String EXTRA_DATA_FAILURE_CAUSE = PhoneConstants.DATA_FAILURE_CAUSE_KEY;
570
571    //
572    //
573    // Device Info
574    //
575    //
576
577    /**
578     * Returns the software version number for the device, for example,
579     * the IMEI/SV for GSM phones. Return null if the software version is
580     * not available.
581     *
582     * <p>Requires Permission:
583     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
584     */
585    public String getDeviceSoftwareVersion() {
586        return getDeviceSoftwareVersion(getDefaultSim());
587    }
588
589    /**
590     * Returns the software version number for the device, for example,
591     * the IMEI/SV for GSM phones. Return null if the software version is
592     * not available.
593     *
594     * <p>Requires Permission:
595     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
596     *
597     * @param slotId of which deviceID is returned
598     */
599    /** {@hide} */
600    public String getDeviceSoftwareVersion(int slotId) {
601        // FIXME methods taking slot id should not use subscription, instead us Uicc directly
602        int[] subId = SubscriptionManager.getSubId(slotId);
603        if (subId == null || subId.length == 0) {
604            return null;
605        }
606        try {
607            return getSubscriberInfo().getDeviceSvnUsingSubId(subId[0]);
608        } catch (RemoteException ex) {
609            return null;
610        } catch (NullPointerException ex) {
611            return null;
612        }
613    }
614
615    /**
616     * Returns the unique device ID, for example, the IMEI for GSM and the MEID
617     * or ESN for CDMA phones. Return null if device ID is not available.
618     *
619     * <p>Requires Permission:
620     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
621     */
622    public String getDeviceId() {
623        try {
624            return getITelephony().getDeviceId();
625        } catch (RemoteException ex) {
626            return null;
627        }
628    }
629
630    /**
631     * Returns the unique device ID of a subscription, for example, the IMEI for
632     * GSM and the MEID for CDMA phones. Return null if device ID is not available.
633     *
634     * <p>Requires Permission:
635     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
636     *
637     * @param slotId of which deviceID is returned
638     */
639    /** {@hide} */
640    public String getDeviceId(int slotId) {
641        // FIXME methods taking slot id should not use subscription, instead us Uicc directly
642        int[] subId = SubscriptionManager.getSubId(slotId);
643        if (subId == null || subId.length == 0) {
644            return null;
645        }
646        try {
647            return getSubscriberInfo().getDeviceIdForSubscriber(subId[0]);
648        } catch (RemoteException ex) {
649            return null;
650        } catch (NullPointerException ex) {
651            return null;
652        }
653    }
654
655    /**
656     * Returns the IMEI. Return null if IMEI is not available.
657     *
658     * <p>Requires Permission:
659     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
660     */
661    /** {@hide} */
662    public String getImei() {
663        return getImei(getDefaultSim());
664    }
665
666    /**
667     * Returns the IMEI. Return null if IMEI is not available.
668     *
669     * <p>Requires Permission:
670     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
671     *
672     * @param slotId of which deviceID is returned
673     */
674    /** {@hide} */
675    public String getImei(int slotId) {
676        int[] subId = SubscriptionManager.getSubId(slotId);
677        try {
678            return getSubscriberInfo().getImeiForSubscriber(subId[0]);
679        } catch (RemoteException ex) {
680            return null;
681        } catch (NullPointerException ex) {
682            return null;
683        }
684    }
685
686    /**
687     * Returns the NAI. Return null if NAI is not available.
688     *
689     */
690    /** {@hide}*/
691    public String getNai() {
692        return getNai(getDefaultSim());
693    }
694
695    /**
696     * Returns the NAI. Return null if NAI is not available.
697     *
698     *  @param slotId of which Nai is returned
699     */
700    /** {@hide}*/
701    public String getNai(int slotId) {
702        int[] subId = SubscriptionManager.getSubId(slotId);
703        try {
704            return getSubscriberInfo().getNaiForSubscriber(subId[0]);
705        } catch (RemoteException ex) {
706            return null;
707        } catch (NullPointerException ex) {
708            return null;
709        }
710    }
711
712    /**
713     * Returns the current location of the device.
714     *<p>
715     * If there is only one radio in the device and that radio has an LTE connection,
716     * this method will return null. The implementation must not to try add LTE
717     * identifiers into the existing cdma/gsm classes.
718     *<p>
719     * In the future this call will be deprecated.
720     *<p>
721     * @return Current location of the device or null if not available.
722     *
723     * <p>Requires Permission:
724     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} or
725     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION}.
726     */
727    public CellLocation getCellLocation() {
728        try {
729            Bundle bundle = getITelephony().getCellLocation();
730            if (bundle.isEmpty()) return null;
731            CellLocation cl = CellLocation.newFromBundle(bundle);
732            if (cl.isEmpty())
733                return null;
734            return cl;
735        } catch (RemoteException ex) {
736            return null;
737        } catch (NullPointerException ex) {
738            return null;
739        }
740    }
741
742    /**
743     * Enables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
744     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
745     *
746     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
747     * CONTROL_LOCATION_UPDATES}
748     *
749     * @hide
750     */
751    public void enableLocationUpdates() {
752            enableLocationUpdates(getDefaultSubscription());
753    }
754
755    /**
756     * Enables location update notifications for a subscription.
757     * {@link PhoneStateListener#onCellLocationChanged
758     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
759     *
760     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
761     * CONTROL_LOCATION_UPDATES}
762     *
763     * @param subId for which the location updates are enabled
764     */
765    /** @hide */
766    public void enableLocationUpdates(int subId) {
767        try {
768            getITelephony().enableLocationUpdatesForSubscriber(subId);
769        } catch (RemoteException ex) {
770        } catch (NullPointerException ex) {
771        }
772    }
773
774    /**
775     * Disables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
776     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
777     *
778     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
779     * CONTROL_LOCATION_UPDATES}
780     *
781     * @hide
782     */
783    public void disableLocationUpdates() {
784            disableLocationUpdates(getDefaultSubscription());
785    }
786
787    /** @hide */
788    public void disableLocationUpdates(int subId) {
789        try {
790            getITelephony().disableLocationUpdatesForSubscriber(subId);
791        } catch (RemoteException ex) {
792        } catch (NullPointerException ex) {
793        }
794    }
795
796    /**
797     * Returns the neighboring cell information of the device. The getAllCellInfo is preferred
798     * and use this only if getAllCellInfo return nulls or an empty list.
799     *<p>
800     * In the future this call will be deprecated.
801     *<p>
802     * @return List of NeighboringCellInfo or null if info unavailable.
803     *
804     * <p>Requires Permission:
805     * (@link android.Manifest.permission#ACCESS_COARSE_UPDATES}
806     */
807    public List<NeighboringCellInfo> getNeighboringCellInfo() {
808        try {
809            return getITelephony().getNeighboringCellInfo(mContext.getOpPackageName());
810        } catch (RemoteException ex) {
811            return null;
812        } catch (NullPointerException ex) {
813            return null;
814        }
815    }
816
817    /** No phone radio. */
818    public static final int PHONE_TYPE_NONE = PhoneConstants.PHONE_TYPE_NONE;
819    /** Phone radio is GSM. */
820    public static final int PHONE_TYPE_GSM = PhoneConstants.PHONE_TYPE_GSM;
821    /** Phone radio is CDMA. */
822    public static final int PHONE_TYPE_CDMA = PhoneConstants.PHONE_TYPE_CDMA;
823    /** Phone is via SIP. */
824    public static final int PHONE_TYPE_SIP = PhoneConstants.PHONE_TYPE_SIP;
825
826    /**
827     * Returns the current phone type.
828     * TODO: This is a last minute change and hence hidden.
829     *
830     * @see #PHONE_TYPE_NONE
831     * @see #PHONE_TYPE_GSM
832     * @see #PHONE_TYPE_CDMA
833     * @see #PHONE_TYPE_SIP
834     *
835     * {@hide}
836     */
837    @SystemApi
838    public int getCurrentPhoneType() {
839        return getCurrentPhoneType(getDefaultSubscription());
840    }
841
842    /**
843     * Returns a constant indicating the device phone type for a subscription.
844     *
845     * @see #PHONE_TYPE_NONE
846     * @see #PHONE_TYPE_GSM
847     * @see #PHONE_TYPE_CDMA
848     *
849     * @param subId for which phone type is returned
850     */
851    /** {@hide} */
852    @SystemApi
853    public int getCurrentPhoneType(int subId) {
854        int phoneId = SubscriptionManager.getPhoneId(subId);
855        try{
856            ITelephony telephony = getITelephony();
857            if (telephony != null) {
858                return telephony.getActivePhoneTypeForSubscriber(subId);
859            } else {
860                // This can happen when the ITelephony interface is not up yet.
861                return getPhoneTypeFromProperty(phoneId);
862            }
863        } catch (RemoteException ex) {
864            // This shouldn't happen in the normal case, as a backup we
865            // read from the system property.
866            return getPhoneTypeFromProperty(phoneId);
867        } catch (NullPointerException ex) {
868            // This shouldn't happen in the normal case, as a backup we
869            // read from the system property.
870            return getPhoneTypeFromProperty(phoneId);
871        }
872    }
873
874    /**
875     * Returns a constant indicating the device phone type.  This
876     * indicates the type of radio used to transmit voice calls.
877     *
878     * @see #PHONE_TYPE_NONE
879     * @see #PHONE_TYPE_GSM
880     * @see #PHONE_TYPE_CDMA
881     * @see #PHONE_TYPE_SIP
882     */
883    public int getPhoneType() {
884        if (!isVoiceCapable()) {
885            return PHONE_TYPE_NONE;
886        }
887        return getCurrentPhoneType();
888    }
889
890    private int getPhoneTypeFromProperty() {
891        return getPhoneTypeFromProperty(getDefaultPhone());
892    }
893
894    /** {@hide} */
895    private int getPhoneTypeFromProperty(int phoneId) {
896        String type = getTelephonyProperty(phoneId,
897                TelephonyProperties.CURRENT_ACTIVE_PHONE, null);
898        if (type == null || type.equals("")) {
899            return getPhoneTypeFromNetworkType(phoneId);
900        }
901        return Integer.parseInt(type);
902    }
903
904    private int getPhoneTypeFromNetworkType() {
905        return getPhoneTypeFromNetworkType(getDefaultPhone());
906    }
907
908    /** {@hide} */
909    private int getPhoneTypeFromNetworkType(int phoneId) {
910        // When the system property CURRENT_ACTIVE_PHONE, has not been set,
911        // use the system property for default network type.
912        // This is a fail safe, and can only happen at first boot.
913        String mode = getTelephonyProperty(phoneId, "ro.telephony.default_network", null);
914        if (mode != null) {
915            return TelephonyManager.getPhoneType(Integer.parseInt(mode));
916        }
917        return TelephonyManager.PHONE_TYPE_NONE;
918    }
919
920    /**
921     * This function returns the type of the phone, depending
922     * on the network mode.
923     *
924     * @param networkMode
925     * @return Phone Type
926     *
927     * @hide
928     */
929    public static int getPhoneType(int networkMode) {
930        switch(networkMode) {
931        case RILConstants.NETWORK_MODE_CDMA:
932        case RILConstants.NETWORK_MODE_CDMA_NO_EVDO:
933        case RILConstants.NETWORK_MODE_EVDO_NO_CDMA:
934            return PhoneConstants.PHONE_TYPE_CDMA;
935
936        case RILConstants.NETWORK_MODE_WCDMA_PREF:
937        case RILConstants.NETWORK_MODE_GSM_ONLY:
938        case RILConstants.NETWORK_MODE_WCDMA_ONLY:
939        case RILConstants.NETWORK_MODE_GSM_UMTS:
940        case RILConstants.NETWORK_MODE_LTE_GSM_WCDMA:
941        case RILConstants.NETWORK_MODE_LTE_WCDMA:
942        case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
943            return PhoneConstants.PHONE_TYPE_GSM;
944
945        // Use CDMA Phone for the global mode including CDMA
946        case RILConstants.NETWORK_MODE_GLOBAL:
947        case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO:
948            return PhoneConstants.PHONE_TYPE_CDMA;
949
950        case RILConstants.NETWORK_MODE_LTE_ONLY:
951            if (getLteOnCdmaModeStatic() == PhoneConstants.LTE_ON_CDMA_TRUE) {
952                return PhoneConstants.PHONE_TYPE_CDMA;
953            } else {
954                return PhoneConstants.PHONE_TYPE_GSM;
955            }
956        default:
957            return PhoneConstants.PHONE_TYPE_GSM;
958        }
959    }
960
961    /**
962     * The contents of the /proc/cmdline file
963     */
964    private static String getProcCmdLine()
965    {
966        String cmdline = "";
967        FileInputStream is = null;
968        try {
969            is = new FileInputStream("/proc/cmdline");
970            byte [] buffer = new byte[2048];
971            int count = is.read(buffer);
972            if (count > 0) {
973                cmdline = new String(buffer, 0, count);
974            }
975        } catch (IOException e) {
976            Rlog.d(TAG, "No /proc/cmdline exception=" + e);
977        } finally {
978            if (is != null) {
979                try {
980                    is.close();
981                } catch (IOException e) {
982                }
983            }
984        }
985        Rlog.d(TAG, "/proc/cmdline=" + cmdline);
986        return cmdline;
987    }
988
989    /** Kernel command line */
990    private static final String sKernelCmdLine = getProcCmdLine();
991
992    /** Pattern for selecting the product type from the kernel command line */
993    private static final Pattern sProductTypePattern =
994        Pattern.compile("\\sproduct_type\\s*=\\s*(\\w+)");
995
996    /** The ProductType used for LTE on CDMA devices */
997    private static final String sLteOnCdmaProductType =
998        SystemProperties.get(TelephonyProperties.PROPERTY_LTE_ON_CDMA_PRODUCT_TYPE, "");
999
1000    /**
1001     * Return if the current radio is LTE on CDMA. This
1002     * is a tri-state return value as for a period of time
1003     * the mode may be unknown.
1004     *
1005     * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE}
1006     * or {@link PhoneConstants#LTE_ON_CDMA_TRUE}
1007     *
1008     * @hide
1009     */
1010    public static int getLteOnCdmaModeStatic() {
1011        int retVal;
1012        int curVal;
1013        String productType = "";
1014
1015        curVal = SystemProperties.getInt(TelephonyProperties.PROPERTY_LTE_ON_CDMA_DEVICE,
1016                    PhoneConstants.LTE_ON_CDMA_UNKNOWN);
1017        retVal = curVal;
1018        if (retVal == PhoneConstants.LTE_ON_CDMA_UNKNOWN) {
1019            Matcher matcher = sProductTypePattern.matcher(sKernelCmdLine);
1020            if (matcher.find()) {
1021                productType = matcher.group(1);
1022                if (sLteOnCdmaProductType.equals(productType)) {
1023                    retVal = PhoneConstants.LTE_ON_CDMA_TRUE;
1024                } else {
1025                    retVal = PhoneConstants.LTE_ON_CDMA_FALSE;
1026                }
1027            } else {
1028                retVal = PhoneConstants.LTE_ON_CDMA_FALSE;
1029            }
1030        }
1031
1032        Rlog.d(TAG, "getLteOnCdmaMode=" + retVal + " curVal=" + curVal +
1033                " product_type='" + productType +
1034                "' lteOnCdmaProductType='" + sLteOnCdmaProductType + "'");
1035        return retVal;
1036    }
1037
1038    //
1039    //
1040    // Current Network
1041    //
1042    //
1043
1044    /**
1045     * Returns the alphabetic name of current registered operator.
1046     * <p>
1047     * Availability: Only when user is registered to a network. Result may be
1048     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1049     * on a CDMA network).
1050     */
1051    public String getNetworkOperatorName() {
1052        return getNetworkOperatorName(getDefaultSubscription());
1053    }
1054
1055    /**
1056     * Returns the alphabetic name of current registered operator
1057     * for a particular subscription.
1058     * <p>
1059     * Availability: Only when user is registered to a network. Result may be
1060     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1061     * on a CDMA network).
1062     * @param subId
1063     */
1064    /** {@hide} */
1065    public String getNetworkOperatorName(int subId) {
1066        int phoneId = SubscriptionManager.getPhoneId(subId);
1067        return getTelephonyProperty(phoneId, TelephonyProperties.PROPERTY_OPERATOR_ALPHA, "");
1068    }
1069
1070    /**
1071     * Returns the numeric name (MCC+MNC) of current registered operator.
1072     * <p>
1073     * Availability: Only when user is registered to a network. Result may be
1074     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1075     * on a CDMA network).
1076     */
1077    public String getNetworkOperator() {
1078        return getNetworkOperator(getDefaultSubscription());
1079    }
1080
1081    /**
1082     * Returns the numeric name (MCC+MNC) of current registered operator
1083     * for a particular subscription.
1084     * <p>
1085     * Availability: Only when user is registered to a network. Result may be
1086     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1087     * on a CDMA network).
1088     *
1089     * @param subId
1090     */
1091    /** {@hide} */
1092   public String getNetworkOperator(int subId) {
1093        int phoneId = SubscriptionManager.getPhoneId(subId);
1094        return getTelephonyProperty(phoneId, TelephonyProperties.PROPERTY_OPERATOR_NUMERIC, "");
1095     }
1096
1097    /**
1098     * Returns true if the device is considered roaming on the current
1099     * network, for GSM purposes.
1100     * <p>
1101     * Availability: Only when user registered to a network.
1102     */
1103    public boolean isNetworkRoaming() {
1104        return isNetworkRoaming(getDefaultSubscription());
1105    }
1106
1107    /**
1108     * Returns true if the device is considered roaming on the current
1109     * network for a subscription.
1110     * <p>
1111     * Availability: Only when user registered to a network.
1112     *
1113     * @param subId
1114     */
1115    /** {@hide} */
1116    public boolean isNetworkRoaming(int subId) {
1117        int phoneId = SubscriptionManager.getPhoneId(subId);
1118        return Boolean.parseBoolean(getTelephonyProperty(phoneId,
1119                TelephonyProperties.PROPERTY_OPERATOR_ISROAMING, null));
1120    }
1121
1122    /**
1123     * Returns the ISO country code equivalent of the current registered
1124     * operator's MCC (Mobile Country Code).
1125     * <p>
1126     * Availability: Only when user is registered to a network. Result may be
1127     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1128     * on a CDMA network).
1129     */
1130    public String getNetworkCountryIso() {
1131        return getNetworkCountryIso(getDefaultSubscription());
1132    }
1133
1134    /**
1135     * Returns the ISO country code equivalent of the current registered
1136     * operator's MCC (Mobile Country Code) of a subscription.
1137     * <p>
1138     * Availability: Only when user is registered to a network. Result may be
1139     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1140     * on a CDMA network).
1141     *
1142     * @param subId for which Network CountryIso is returned
1143     */
1144    /** {@hide} */
1145    public String getNetworkCountryIso(int subId) {
1146        int phoneId = SubscriptionManager.getPhoneId(subId);
1147        return getTelephonyProperty(phoneId, TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY, "");
1148    }
1149
1150    /** Network type is unknown */
1151    public static final int NETWORK_TYPE_UNKNOWN = 0;
1152    /** Current network is GPRS */
1153    public static final int NETWORK_TYPE_GPRS = 1;
1154    /** Current network is EDGE */
1155    public static final int NETWORK_TYPE_EDGE = 2;
1156    /** Current network is UMTS */
1157    public static final int NETWORK_TYPE_UMTS = 3;
1158    /** Current network is CDMA: Either IS95A or IS95B*/
1159    public static final int NETWORK_TYPE_CDMA = 4;
1160    /** Current network is EVDO revision 0*/
1161    public static final int NETWORK_TYPE_EVDO_0 = 5;
1162    /** Current network is EVDO revision A*/
1163    public static final int NETWORK_TYPE_EVDO_A = 6;
1164    /** Current network is 1xRTT*/
1165    public static final int NETWORK_TYPE_1xRTT = 7;
1166    /** Current network is HSDPA */
1167    public static final int NETWORK_TYPE_HSDPA = 8;
1168    /** Current network is HSUPA */
1169    public static final int NETWORK_TYPE_HSUPA = 9;
1170    /** Current network is HSPA */
1171    public static final int NETWORK_TYPE_HSPA = 10;
1172    /** Current network is iDen */
1173    public static final int NETWORK_TYPE_IDEN = 11;
1174    /** Current network is EVDO revision B*/
1175    public static final int NETWORK_TYPE_EVDO_B = 12;
1176    /** Current network is LTE */
1177    public static final int NETWORK_TYPE_LTE = 13;
1178    /** Current network is eHRPD */
1179    public static final int NETWORK_TYPE_EHRPD = 14;
1180    /** Current network is HSPA+ */
1181    public static final int NETWORK_TYPE_HSPAP = 15;
1182    /** Current network is GSM {@hide} */
1183    public static final int NETWORK_TYPE_GSM = 16;
1184
1185    /**
1186     * @return the NETWORK_TYPE_xxxx for current data connection.
1187     */
1188    public int getNetworkType() {
1189        return getDataNetworkType();
1190    }
1191
1192    /**
1193     * Returns a constant indicating the radio technology (network type)
1194     * currently in use on the device for a subscription.
1195     * @return the network type
1196     *
1197     * @param subId for which network type is returned
1198     *
1199     * @see #NETWORK_TYPE_UNKNOWN
1200     * @see #NETWORK_TYPE_GPRS
1201     * @see #NETWORK_TYPE_EDGE
1202     * @see #NETWORK_TYPE_UMTS
1203     * @see #NETWORK_TYPE_HSDPA
1204     * @see #NETWORK_TYPE_HSUPA
1205     * @see #NETWORK_TYPE_HSPA
1206     * @see #NETWORK_TYPE_CDMA
1207     * @see #NETWORK_TYPE_EVDO_0
1208     * @see #NETWORK_TYPE_EVDO_A
1209     * @see #NETWORK_TYPE_EVDO_B
1210     * @see #NETWORK_TYPE_1xRTT
1211     * @see #NETWORK_TYPE_IDEN
1212     * @see #NETWORK_TYPE_LTE
1213     * @see #NETWORK_TYPE_EHRPD
1214     * @see #NETWORK_TYPE_HSPAP
1215     */
1216    /** {@hide} */
1217   public int getNetworkType(int subId) {
1218       try {
1219           ITelephony telephony = getITelephony();
1220           if (telephony != null) {
1221               return telephony.getNetworkTypeForSubscriber(subId);
1222           } else {
1223               // This can happen when the ITelephony interface is not up yet.
1224               return NETWORK_TYPE_UNKNOWN;
1225           }
1226       } catch(RemoteException ex) {
1227           // This shouldn't happen in the normal case
1228           return NETWORK_TYPE_UNKNOWN;
1229       } catch (NullPointerException ex) {
1230           // This could happen before phone restarts due to crashing
1231           return NETWORK_TYPE_UNKNOWN;
1232       }
1233   }
1234
1235    /**
1236     * Returns a constant indicating the radio technology (network type)
1237     * currently in use on the device for data transmission.
1238     * @return the network type
1239     *
1240     * @see #NETWORK_TYPE_UNKNOWN
1241     * @see #NETWORK_TYPE_GPRS
1242     * @see #NETWORK_TYPE_EDGE
1243     * @see #NETWORK_TYPE_UMTS
1244     * @see #NETWORK_TYPE_HSDPA
1245     * @see #NETWORK_TYPE_HSUPA
1246     * @see #NETWORK_TYPE_HSPA
1247     * @see #NETWORK_TYPE_CDMA
1248     * @see #NETWORK_TYPE_EVDO_0
1249     * @see #NETWORK_TYPE_EVDO_A
1250     * @see #NETWORK_TYPE_EVDO_B
1251     * @see #NETWORK_TYPE_1xRTT
1252     * @see #NETWORK_TYPE_IDEN
1253     * @see #NETWORK_TYPE_LTE
1254     * @see #NETWORK_TYPE_EHRPD
1255     * @see #NETWORK_TYPE_HSPAP
1256     *
1257     * @hide
1258     */
1259    public int getDataNetworkType() {
1260        return getDataNetworkType(getDefaultSubscription());
1261    }
1262
1263    /**
1264     * Returns a constant indicating the radio technology (network type)
1265     * currently in use on the device for data transmission for a subscription
1266     * @return the network type
1267     *
1268     * @param subId for which network type is returned
1269     */
1270    /** {@hide} */
1271    public int getDataNetworkType(int subId) {
1272        try{
1273            ITelephony telephony = getITelephony();
1274            if (telephony != null) {
1275                return telephony.getDataNetworkTypeForSubscriber(subId);
1276            } else {
1277                // This can happen when the ITelephony interface is not up yet.
1278                return NETWORK_TYPE_UNKNOWN;
1279            }
1280        } catch(RemoteException ex) {
1281            // This shouldn't happen in the normal case
1282            return NETWORK_TYPE_UNKNOWN;
1283        } catch (NullPointerException ex) {
1284            // This could happen before phone restarts due to crashing
1285            return NETWORK_TYPE_UNKNOWN;
1286        }
1287    }
1288
1289    /**
1290     * Returns the NETWORK_TYPE_xxxx for voice
1291     *
1292     * @hide
1293     */
1294    public int getVoiceNetworkType() {
1295        return getVoiceNetworkType(getDefaultSubscription());
1296    }
1297
1298    /**
1299     * Returns the NETWORK_TYPE_xxxx for voice for a subId
1300     *
1301     */
1302    /** {@hide} */
1303    public int getVoiceNetworkType(int subId) {
1304        try{
1305            ITelephony telephony = getITelephony();
1306            if (telephony != null) {
1307                return telephony.getVoiceNetworkTypeForSubscriber(subId);
1308            } else {
1309                // This can happen when the ITelephony interface is not up yet.
1310                return NETWORK_TYPE_UNKNOWN;
1311            }
1312        } catch(RemoteException ex) {
1313            // This shouldn't happen in the normal case
1314            return NETWORK_TYPE_UNKNOWN;
1315        } catch (NullPointerException ex) {
1316            // This could happen before phone restarts due to crashing
1317            return NETWORK_TYPE_UNKNOWN;
1318        }
1319    }
1320
1321    /** Unknown network class. {@hide} */
1322    public static final int NETWORK_CLASS_UNKNOWN = 0;
1323    /** Class of broadly defined "2G" networks. {@hide} */
1324    public static final int NETWORK_CLASS_2_G = 1;
1325    /** Class of broadly defined "3G" networks. {@hide} */
1326    public static final int NETWORK_CLASS_3_G = 2;
1327    /** Class of broadly defined "4G" networks. {@hide} */
1328    public static final int NETWORK_CLASS_4_G = 3;
1329
1330    /**
1331     * Return general class of network type, such as "3G" or "4G". In cases
1332     * where classification is contentious, this method is conservative.
1333     *
1334     * @hide
1335     */
1336    public static int getNetworkClass(int networkType) {
1337        switch (networkType) {
1338            case NETWORK_TYPE_GPRS:
1339            case NETWORK_TYPE_GSM:
1340            case NETWORK_TYPE_EDGE:
1341            case NETWORK_TYPE_CDMA:
1342            case NETWORK_TYPE_1xRTT:
1343            case NETWORK_TYPE_IDEN:
1344                return NETWORK_CLASS_2_G;
1345            case NETWORK_TYPE_UMTS:
1346            case NETWORK_TYPE_EVDO_0:
1347            case NETWORK_TYPE_EVDO_A:
1348            case NETWORK_TYPE_HSDPA:
1349            case NETWORK_TYPE_HSUPA:
1350            case NETWORK_TYPE_HSPA:
1351            case NETWORK_TYPE_EVDO_B:
1352            case NETWORK_TYPE_EHRPD:
1353            case NETWORK_TYPE_HSPAP:
1354                return NETWORK_CLASS_3_G;
1355            case NETWORK_TYPE_LTE:
1356                return NETWORK_CLASS_4_G;
1357            default:
1358                return NETWORK_CLASS_UNKNOWN;
1359        }
1360    }
1361
1362    /**
1363     * Returns a string representation of the radio technology (network type)
1364     * currently in use on the device.
1365     * @return the name of the radio technology
1366     *
1367     * @hide pending API council review
1368     */
1369    public String getNetworkTypeName() {
1370        return getNetworkTypeName(getNetworkType());
1371    }
1372
1373    /**
1374     * Returns a string representation of the radio technology (network type)
1375     * currently in use on the device.
1376     * @param subId for which network type is returned
1377     * @return the name of the radio technology
1378     *
1379     */
1380    /** {@hide} */
1381    public static String getNetworkTypeName(int type) {
1382        switch (type) {
1383            case NETWORK_TYPE_GPRS:
1384                return "GPRS";
1385            case NETWORK_TYPE_EDGE:
1386                return "EDGE";
1387            case NETWORK_TYPE_UMTS:
1388                return "UMTS";
1389            case NETWORK_TYPE_HSDPA:
1390                return "HSDPA";
1391            case NETWORK_TYPE_HSUPA:
1392                return "HSUPA";
1393            case NETWORK_TYPE_HSPA:
1394                return "HSPA";
1395            case NETWORK_TYPE_CDMA:
1396                return "CDMA";
1397            case NETWORK_TYPE_EVDO_0:
1398                return "CDMA - EvDo rev. 0";
1399            case NETWORK_TYPE_EVDO_A:
1400                return "CDMA - EvDo rev. A";
1401            case NETWORK_TYPE_EVDO_B:
1402                return "CDMA - EvDo rev. B";
1403            case NETWORK_TYPE_1xRTT:
1404                return "CDMA - 1xRTT";
1405            case NETWORK_TYPE_LTE:
1406                return "LTE";
1407            case NETWORK_TYPE_EHRPD:
1408                return "CDMA - eHRPD";
1409            case NETWORK_TYPE_IDEN:
1410                return "iDEN";
1411            case NETWORK_TYPE_HSPAP:
1412                return "HSPA+";
1413            case NETWORK_TYPE_GSM:
1414                return "GSM";
1415            default:
1416                return "UNKNOWN";
1417        }
1418    }
1419
1420    //
1421    //
1422    // SIM Card
1423    //
1424    //
1425
1426    /**
1427     * SIM card state: Unknown. Signifies that the SIM is in transition
1428     * between states. For example, when the user inputs the SIM pin
1429     * under PIN_REQUIRED state, a query for sim status returns
1430     * this state before turning to SIM_STATE_READY.
1431     *
1432     * These are the ordinal value of IccCardConstants.State.
1433     */
1434    public static final int SIM_STATE_UNKNOWN = 0;
1435    /** SIM card state: no SIM card is available in the device */
1436    public static final int SIM_STATE_ABSENT = 1;
1437    /** SIM card state: Locked: requires the user's SIM PIN to unlock */
1438    public static final int SIM_STATE_PIN_REQUIRED = 2;
1439    /** SIM card state: Locked: requires the user's SIM PUK to unlock */
1440    public static final int SIM_STATE_PUK_REQUIRED = 3;
1441    /** SIM card state: Locked: requires a network PIN to unlock */
1442    public static final int SIM_STATE_NETWORK_LOCKED = 4;
1443    /** SIM card state: Ready */
1444    public static final int SIM_STATE_READY = 5;
1445    /** SIM card state: SIM Card is NOT READY
1446     *@hide
1447     */
1448    public static final int SIM_STATE_NOT_READY = 6;
1449    /** SIM card state: SIM Card Error, permanently disabled
1450     *@hide
1451     */
1452    public static final int SIM_STATE_PERM_DISABLED = 7;
1453    /** SIM card state: SIM Card Error, present but faulty
1454     *@hide
1455     */
1456    public static final int SIM_STATE_CARD_IO_ERROR = 8;
1457
1458    /**
1459     * @return true if a ICC card is present
1460     */
1461    public boolean hasIccCard() {
1462        return hasIccCard(getDefaultSim());
1463    }
1464
1465    /**
1466     * @return true if a ICC card is present for a subscription
1467     *
1468     * @param slotId for which icc card presence is checked
1469     */
1470    /** {@hide} */
1471    // FIXME Input argument slotId should be of type int
1472    public boolean hasIccCard(int slotId) {
1473
1474        try {
1475            return getITelephony().hasIccCardUsingSlotId(slotId);
1476        } catch (RemoteException ex) {
1477            // Assume no ICC card if remote exception which shouldn't happen
1478            return false;
1479        } catch (NullPointerException ex) {
1480            // This could happen before phone restarts due to crashing
1481            return false;
1482        }
1483    }
1484
1485    /**
1486     * Returns a constant indicating the state of the default SIM card.
1487     *
1488     * @see #SIM_STATE_UNKNOWN
1489     * @see #SIM_STATE_ABSENT
1490     * @see #SIM_STATE_PIN_REQUIRED
1491     * @see #SIM_STATE_PUK_REQUIRED
1492     * @see #SIM_STATE_NETWORK_LOCKED
1493     * @see #SIM_STATE_READY
1494     * @see #SIM_STATE_NOT_READY
1495     * @see #SIM_STATE_PERM_DISABLED
1496     * @see #SIM_STATE_CARD_IO_ERROR
1497     */
1498    public int getSimState() {
1499        return getSimState(getDefaultSim());
1500    }
1501
1502    /**
1503     * Returns a constant indicating the state of the device SIM card in a slot.
1504     *
1505     * @param slotIdx
1506     *
1507     * @see #SIM_STATE_UNKNOWN
1508     * @see #SIM_STATE_ABSENT
1509     * @see #SIM_STATE_PIN_REQUIRED
1510     * @see #SIM_STATE_PUK_REQUIRED
1511     * @see #SIM_STATE_NETWORK_LOCKED
1512     * @see #SIM_STATE_READY
1513     * @see #SIM_STATE_NOT_READY
1514     * @see #SIM_STATE_PERM_DISABLED
1515     * @see #SIM_STATE_CARD_IO_ERROR
1516     */
1517    /** {@hide} */
1518    public int getSimState(int slotIdx) {
1519        int[] subId = SubscriptionManager.getSubId(slotIdx);
1520        if (subId == null || subId.length == 0) {
1521            Rlog.d(TAG, "getSimState:- empty subId return SIM_STATE_ABSENT");
1522            return SIM_STATE_UNKNOWN;
1523        }
1524        int simState = SubscriptionManager.getSimStateForSubscriber(subId[0]);
1525        Rlog.d(TAG, "getSimState: simState=" + simState + " slotIdx=" + slotIdx);
1526        return simState;
1527    }
1528
1529    /**
1530     * Returns the MCC+MNC (mobile country code + mobile network code) of the
1531     * provider of the SIM. 5 or 6 decimal digits.
1532     * <p>
1533     * Availability: SIM state must be {@link #SIM_STATE_READY}
1534     *
1535     * @see #getSimState
1536     */
1537    public String getSimOperator() {
1538        int subId = SubscriptionManager.getDefaultDataSubId();
1539        if (!SubscriptionManager.isUsableSubIdValue(subId)) {
1540            subId = SubscriptionManager.getDefaultSmsSubId();
1541            if (!SubscriptionManager.isUsableSubIdValue(subId)) {
1542                subId = SubscriptionManager.getDefaultVoiceSubId();
1543                if (!SubscriptionManager.isUsableSubIdValue(subId)) {
1544                    subId = SubscriptionManager.getDefaultSubId();
1545                }
1546            }
1547        }
1548        Rlog.d(TAG, "getSimOperator(): default subId=" + subId);
1549        return getSimOperator(subId);
1550    }
1551
1552    /**
1553     * Returns the MCC+MNC (mobile country code + mobile network code) of the
1554     * provider of the SIM for a particular subscription. 5 or 6 decimal digits.
1555     * <p>
1556     * Availability: SIM state must be {@link #SIM_STATE_READY}
1557     *
1558     * @see #getSimState
1559     *
1560     * @param subId for which SimOperator is returned
1561     */
1562    /** {@hide} */
1563    public String getSimOperator(int subId) {
1564        int phoneId = SubscriptionManager.getPhoneId(subId);
1565        String operator = getTelephonyProperty(phoneId,
1566                TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC, "");
1567        Rlog.d(TAG, "getSimOperator: subId=" + subId + " operator=" + operator);
1568        return operator;
1569    }
1570
1571    /**
1572     * Returns the Service Provider Name (SPN).
1573     * <p>
1574     * Availability: SIM state must be {@link #SIM_STATE_READY}
1575     *
1576     * @see #getSimState
1577     */
1578    public String getSimOperatorName() {
1579        return getSimOperatorName(getDefaultSubscription());
1580    }
1581
1582    /**
1583     * Returns the Service Provider Name (SPN).
1584     * <p>
1585     * Availability: SIM state must be {@link #SIM_STATE_READY}
1586     *
1587     * @see #getSimState
1588     *
1589     * @param subId for which SimOperatorName is returned
1590     */
1591    /** {@hide} */
1592    public String getSimOperatorName(int subId) {
1593        int phoneId = SubscriptionManager.getPhoneId(subId);
1594        return getTelephonyProperty(phoneId, TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA, "");
1595    }
1596
1597    /**
1598     * Returns the ISO country code equivalent for the SIM provider's country code.
1599     */
1600    public String getSimCountryIso() {
1601        return getSimCountryIso(getDefaultSubscription());
1602    }
1603
1604    /**
1605     * Returns the ISO country code equivalent for the SIM provider's country code.
1606     *
1607     * @param subId for which SimCountryIso is returned
1608     */
1609    /** {@hide} */
1610    public String getSimCountryIso(int subId) {
1611        int phoneId = SubscriptionManager.getPhoneId(subId);
1612        return getTelephonyProperty(phoneId, TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY,
1613                "");
1614    }
1615
1616    /**
1617     * Returns the serial number of the SIM, if applicable. Return null if it is
1618     * unavailable.
1619     * <p>
1620     * Requires Permission:
1621     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1622     */
1623    public String getSimSerialNumber() {
1624         return getSimSerialNumber(getDefaultSubscription());
1625    }
1626
1627    /**
1628     * Returns the serial number for the given subscription, if applicable. Return null if it is
1629     * unavailable.
1630     * <p>
1631     * @param subId for which Sim Serial number is returned
1632     * Requires Permission:
1633     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1634     */
1635    /** {@hide} */
1636    public String getSimSerialNumber(int subId) {
1637        try {
1638            return getSubscriberInfo().getIccSerialNumberForSubscriber(subId);
1639        } catch (RemoteException ex) {
1640            return null;
1641        } catch (NullPointerException ex) {
1642            // This could happen before phone restarts due to crashing
1643            return null;
1644        }
1645    }
1646
1647    /**
1648     * Return if the current radio is LTE on CDMA. This
1649     * is a tri-state return value as for a period of time
1650     * the mode may be unknown.
1651     *
1652     * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE}
1653     * or {@link PhoneConstants#LTE_ON_CDMA_TRUE}
1654     *
1655     * @hide
1656     */
1657    public int getLteOnCdmaMode() {
1658        return getLteOnCdmaMode(getDefaultSubscription());
1659    }
1660
1661    /**
1662     * Return if the current radio is LTE on CDMA for Subscription. This
1663     * is a tri-state return value as for a period of time
1664     * the mode may be unknown.
1665     *
1666     * @param subId for which radio is LTE on CDMA is returned
1667     * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE}
1668     * or {@link PhoneConstants#LTE_ON_CDMA_TRUE}
1669     *
1670     */
1671    /** {@hide} */
1672    public int getLteOnCdmaMode(int subId) {
1673        try {
1674            return getITelephony().getLteOnCdmaModeForSubscriber(subId);
1675        } catch (RemoteException ex) {
1676            // Assume no ICC card if remote exception which shouldn't happen
1677            return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1678        } catch (NullPointerException ex) {
1679            // This could happen before phone restarts due to crashing
1680            return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1681        }
1682    }
1683
1684    //
1685    //
1686    // Subscriber Info
1687    //
1688    //
1689
1690    /**
1691     * Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
1692     * Return null if it is unavailable.
1693     * <p>
1694     * Requires Permission:
1695     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1696     */
1697    public String getSubscriberId() {
1698        return getSubscriberId(getDefaultSubscription());
1699    }
1700
1701    /**
1702     * Returns the unique subscriber ID, for example, the IMSI for a GSM phone
1703     * for a subscription.
1704     * Return null if it is unavailable.
1705     * <p>
1706     * Requires Permission:
1707     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1708     *
1709     * @param subId whose subscriber id is returned
1710     */
1711    /** {@hide} */
1712    public String getSubscriberId(int subId) {
1713        try {
1714            return getSubscriberInfo().getSubscriberIdForSubscriber(subId);
1715        } catch (RemoteException ex) {
1716            return null;
1717        } catch (NullPointerException ex) {
1718            // This could happen before phone restarts due to crashing
1719            return null;
1720        }
1721    }
1722
1723    /**
1724     * Returns the Group Identifier Level1 for a GSM phone.
1725     * Return null if it is unavailable.
1726     * <p>
1727     * Requires Permission:
1728     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1729     */
1730    public String getGroupIdLevel1() {
1731        try {
1732            return getSubscriberInfo().getGroupIdLevel1();
1733        } catch (RemoteException ex) {
1734            return null;
1735        } catch (NullPointerException ex) {
1736            // This could happen before phone restarts due to crashing
1737            return null;
1738        }
1739    }
1740
1741    /**
1742     * Returns the Group Identifier Level1 for a GSM phone for a particular subscription.
1743     * Return null if it is unavailable.
1744     * <p>
1745     * Requires Permission:
1746     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1747     *
1748     * @param subscription whose subscriber id is returned
1749     */
1750    /** {@hide} */
1751    public String getGroupIdLevel1(int subId) {
1752        try {
1753            return getSubscriberInfo().getGroupIdLevel1ForSubscriber(subId);
1754        } catch (RemoteException ex) {
1755            return null;
1756        } catch (NullPointerException ex) {
1757            // This could happen before phone restarts due to crashing
1758            return null;
1759        }
1760    }
1761
1762    /**
1763     * Returns the phone number string for line 1, for example, the MSISDN
1764     * for a GSM phone. Return null if it is unavailable.
1765     * <p>
1766     * Requires Permission:
1767     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1768     */
1769    public String getLine1Number() {
1770        return getLine1NumberForSubscriber(getDefaultSubscription());
1771    }
1772
1773    /**
1774     * Returns the phone number string for line 1, for example, the MSISDN
1775     * for a GSM phone for a particular subscription. Return null if it is unavailable.
1776     * <p>
1777     * Requires Permission:
1778     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1779     *
1780     * @param subId whose phone number for line 1 is returned
1781     */
1782    /** {@hide} */
1783    public String getLine1NumberForSubscriber(int subId) {
1784        String number = null;
1785        try {
1786            number = getITelephony().getLine1NumberForDisplay(subId);
1787        } catch (RemoteException ex) {
1788        } catch (NullPointerException ex) {
1789        }
1790        if (number != null) {
1791            return number;
1792        }
1793        try {
1794            return getSubscriberInfo().getLine1NumberForSubscriber(subId);
1795        } catch (RemoteException ex) {
1796            return null;
1797        } catch (NullPointerException ex) {
1798            // This could happen before phone restarts due to crashing
1799            return null;
1800        }
1801    }
1802
1803    /**
1804     * Set the line 1 phone number string and its alphatag for the current ICCID
1805     * for display purpose only, for example, displayed in Phone Status. It won't
1806     * change the actual MSISDN/MDN. To unset alphatag or number, pass in a null
1807     * value.
1808     *
1809     * <p>Requires that the calling app has carrier privileges.
1810     * @see #hasCarrierPrivileges
1811     *
1812     * @param alphaTag alpha-tagging of the dailing nubmer
1813     * @param number The dialing number
1814     * @return true if the operation was executed correctly.
1815     */
1816    public boolean setLine1NumberForDisplay(String alphaTag, String number) {
1817        return setLine1NumberForDisplayForSubscriber(getDefaultSubscription(), alphaTag, number);
1818    }
1819
1820    /**
1821     * Set the line 1 phone number string and its alphatag for the current ICCID
1822     * for display purpose only, for example, displayed in Phone Status. It won't
1823     * change the actual MSISDN/MDN. To unset alphatag or number, pass in a null
1824     * value.
1825     *
1826     * <p>Requires that the calling app has carrier privileges.
1827     * @see #hasCarrierPrivileges
1828     *
1829     * @param subId the subscriber that the alphatag and dialing number belongs to.
1830     * @param alphaTag alpha-tagging of the dailing nubmer
1831     * @param number The dialing number
1832     * @return true if the operation was executed correctly.
1833     * @hide
1834     */
1835    public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag, String number) {
1836        try {
1837            return getITelephony().setLine1NumberForDisplayForSubscriber(subId, alphaTag, number);
1838        } catch (RemoteException ex) {
1839        } catch (NullPointerException ex) {
1840        }
1841        return false;
1842    }
1843
1844    /**
1845     * Returns the alphabetic identifier associated with the line 1 number.
1846     * Return null if it is unavailable.
1847     * <p>
1848     * Requires Permission:
1849     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1850     * @hide
1851     * nobody seems to call this.
1852     */
1853    public String getLine1AlphaTag() {
1854        return getLine1AlphaTagForSubscriber(getDefaultSubscription());
1855    }
1856
1857    /**
1858     * Returns the alphabetic identifier associated with the line 1 number
1859     * for a subscription.
1860     * Return null if it is unavailable.
1861     * <p>
1862     * Requires Permission:
1863     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1864     * @param subId whose alphabetic identifier associated with line 1 is returned
1865     * nobody seems to call this.
1866     */
1867    /** {@hide} */
1868    public String getLine1AlphaTagForSubscriber(int subId) {
1869        String alphaTag = null;
1870        try {
1871            alphaTag = getITelephony().getLine1AlphaTagForDisplay(subId);
1872        } catch (RemoteException ex) {
1873        } catch (NullPointerException ex) {
1874        }
1875        if (alphaTag != null) {
1876            return alphaTag;
1877        }
1878        try {
1879            return getSubscriberInfo().getLine1AlphaTagForSubscriber(subId);
1880        } catch (RemoteException ex) {
1881            return null;
1882        } catch (NullPointerException ex) {
1883            // This could happen before phone restarts due to crashing
1884            return null;
1885        }
1886    }
1887
1888    /**
1889     * Return the set of subscriber IDs that should be considered as "merged
1890     * together" for data usage purposes. This is commonly {@code null} to
1891     * indicate no merging is required. Any returned subscribers are sorted in a
1892     * deterministic order.
1893     *
1894     * @hide
1895     */
1896    public @Nullable String[] getMergedSubscriberIds() {
1897        try {
1898            return getITelephony().getMergedSubscriberIds();
1899        } catch (RemoteException ex) {
1900        } catch (NullPointerException ex) {
1901        }
1902        return null;
1903    }
1904
1905    /**
1906     * Returns the MSISDN string.
1907     * for a GSM phone. Return null if it is unavailable.
1908     * <p>
1909     * Requires Permission:
1910     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1911     *
1912     * @hide
1913     */
1914    public String getMsisdn() {
1915        return getMsisdn(getDefaultSubscription());
1916    }
1917
1918    /**
1919     * Returns the MSISDN string.
1920     * for a GSM phone. Return null if it is unavailable.
1921     * <p>
1922     * Requires Permission:
1923     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1924     *
1925     * @param subId for which msisdn is returned
1926     */
1927    /** {@hide} */
1928    public String getMsisdn(int subId) {
1929        try {
1930            return getSubscriberInfo().getMsisdnForSubscriber(subId);
1931        } catch (RemoteException ex) {
1932            return null;
1933        } catch (NullPointerException ex) {
1934            // This could happen before phone restarts due to crashing
1935            return null;
1936        }
1937    }
1938
1939    /**
1940     * Returns the voice mail number. Return null if it is unavailable.
1941     * <p>
1942     * Requires Permission:
1943     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1944     */
1945    public String getVoiceMailNumber() {
1946        return getVoiceMailNumber(getDefaultSubscription());
1947    }
1948
1949    /**
1950     * Returns the voice mail number for a subscription.
1951     * Return null if it is unavailable.
1952     * <p>
1953     * Requires Permission:
1954     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1955     * @param subId whose voice mail number is returned
1956     */
1957    /** {@hide} */
1958    public String getVoiceMailNumber(int subId) {
1959        try {
1960            return getSubscriberInfo().getVoiceMailNumberForSubscriber(subId);
1961        } catch (RemoteException ex) {
1962            return null;
1963        } catch (NullPointerException ex) {
1964            // This could happen before phone restarts due to crashing
1965            return null;
1966        }
1967    }
1968
1969    /**
1970     * Returns the complete voice mail number. Return null if it is unavailable.
1971     * <p>
1972     * Requires Permission:
1973     *   {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED}
1974     *
1975     * @hide
1976     */
1977    public String getCompleteVoiceMailNumber() {
1978        return getCompleteVoiceMailNumber(getDefaultSubscription());
1979    }
1980
1981    /**
1982     * Returns the complete voice mail number. Return null if it is unavailable.
1983     * <p>
1984     * Requires Permission:
1985     *   {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED}
1986     *
1987     * @param subId
1988     */
1989    /** {@hide} */
1990    public String getCompleteVoiceMailNumber(int subId) {
1991        try {
1992            return getSubscriberInfo().getCompleteVoiceMailNumberForSubscriber(subId);
1993        } catch (RemoteException ex) {
1994            return null;
1995        } catch (NullPointerException ex) {
1996            // This could happen before phone restarts due to crashing
1997            return null;
1998        }
1999    }
2000
2001    /**
2002     * Sets the voice mail number.
2003     *
2004     * <p>Requires that the calling app has carrier privileges.
2005     * @see #hasCarrierPrivileges
2006     *
2007     * @param alphaTag The alpha tag to display.
2008     * @param number The voicemail number.
2009     */
2010    public boolean setVoiceMailNumber(String alphaTag, String number) {
2011        return setVoiceMailNumber(getDefaultSubscription(), alphaTag, number);
2012    }
2013
2014    /**
2015     * Sets the voicemail number for the given subscriber.
2016     *
2017     * <p>Requires that the calling app has carrier privileges.
2018     * @see #hasCarrierPrivileges
2019     *
2020     * @param subId The subscription id.
2021     * @param alphaTag The alpha tag to display.
2022     * @param number The voicemail number.
2023     */
2024    /** {@hide} */
2025    public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
2026        try {
2027            return getITelephony().setVoiceMailNumber(subId, alphaTag, number);
2028        } catch (RemoteException ex) {
2029        } catch (NullPointerException ex) {
2030        }
2031        return false;
2032    }
2033
2034    /**
2035     * Returns the voice mail count. Return 0 if unavailable.
2036     * <p>
2037     * Requires Permission:
2038     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2039     * @hide
2040     */
2041    public int getVoiceMessageCount() {
2042        return getVoiceMessageCount(getDefaultSubscription());
2043    }
2044
2045    /**
2046     * Returns the voice mail count for a subscription. Return 0 if unavailable.
2047     * <p>
2048     * Requires Permission:
2049     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2050     * @param subId whose voice message count is returned
2051     */
2052    /** {@hide} */
2053    public int getVoiceMessageCount(int subId) {
2054        try {
2055            return getITelephony().getVoiceMessageCountForSubscriber(subId);
2056        } catch (RemoteException ex) {
2057            return 0;
2058        } catch (NullPointerException ex) {
2059            // This could happen before phone restarts due to crashing
2060            return 0;
2061        }
2062    }
2063
2064    /**
2065     * Retrieves the alphabetic identifier associated with the voice
2066     * mail number.
2067     * <p>
2068     * Requires Permission:
2069     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2070     */
2071    public String getVoiceMailAlphaTag() {
2072        return getVoiceMailAlphaTag(getDefaultSubscription());
2073    }
2074
2075    /**
2076     * Retrieves the alphabetic identifier associated with the voice
2077     * mail number for a subscription.
2078     * <p>
2079     * Requires Permission:
2080     * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2081     * @param subId whose alphabetic identifier associated with the
2082     * voice mail number is returned
2083     */
2084    /** {@hide} */
2085    public String getVoiceMailAlphaTag(int subId) {
2086        try {
2087            return getSubscriberInfo().getVoiceMailAlphaTagForSubscriber(subId);
2088        } catch (RemoteException ex) {
2089            return null;
2090        } catch (NullPointerException ex) {
2091            // This could happen before phone restarts due to crashing
2092            return null;
2093        }
2094    }
2095
2096    /**
2097     * Returns the IMS private user identity (IMPI) that was loaded from the ISIM.
2098     * @return the IMPI, or null if not present or not loaded
2099     * @hide
2100     */
2101    public String getIsimImpi() {
2102        try {
2103            return getSubscriberInfo().getIsimImpi();
2104        } catch (RemoteException ex) {
2105            return null;
2106        } catch (NullPointerException ex) {
2107            // This could happen before phone restarts due to crashing
2108            return null;
2109        }
2110    }
2111
2112    /**
2113     * Returns the IMS home network domain name that was loaded from the ISIM.
2114     * @return the IMS domain name, or null if not present or not loaded
2115     * @hide
2116     */
2117    public String getIsimDomain() {
2118        try {
2119            return getSubscriberInfo().getIsimDomain();
2120        } catch (RemoteException ex) {
2121            return null;
2122        } catch (NullPointerException ex) {
2123            // This could happen before phone restarts due to crashing
2124            return null;
2125        }
2126    }
2127
2128    /**
2129     * Returns the IMS public user identities (IMPU) that were loaded from the ISIM.
2130     * @return an array of IMPU strings, with one IMPU per string, or null if
2131     *      not present or not loaded
2132     * @hide
2133     */
2134    public String[] getIsimImpu() {
2135        try {
2136            return getSubscriberInfo().getIsimImpu();
2137        } catch (RemoteException ex) {
2138            return null;
2139        } catch (NullPointerException ex) {
2140            // This could happen before phone restarts due to crashing
2141            return null;
2142        }
2143    }
2144
2145   /**
2146    * @hide
2147    */
2148    private IPhoneSubInfo getSubscriberInfo() {
2149        // get it each time because that process crashes a lot
2150        return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
2151    }
2152
2153    /** Device call state: No activity. */
2154    public static final int CALL_STATE_IDLE = 0;
2155    /** Device call state: Ringing. A new call arrived and is
2156     *  ringing or waiting. In the latter case, another call is
2157     *  already active. */
2158    public static final int CALL_STATE_RINGING = 1;
2159    /** Device call state: Off-hook. At least one call exists
2160      * that is dialing, active, or on hold, and no calls are ringing
2161      * or waiting. */
2162    public static final int CALL_STATE_OFFHOOK = 2;
2163
2164    /**
2165     * Returns a constant indicating the call state (cellular) on the device.
2166     */
2167    public int getCallState() {
2168        return getCallState(getDefaultSubscription());
2169    }
2170
2171    /**
2172     * Returns a constant indicating the call state (cellular) on the device
2173     * for a subscription.
2174     *
2175     * @param subId whose call state is returned
2176     */
2177    /** {@hide} */
2178    public int getCallState(int subId) {
2179        try {
2180            return getITelephony().getCallStateForSubscriber(subId);
2181        } catch (RemoteException ex) {
2182            // the phone process is restarting.
2183            return CALL_STATE_IDLE;
2184        } catch (NullPointerException ex) {
2185          // the phone process is restarting.
2186          return CALL_STATE_IDLE;
2187      }
2188    }
2189
2190    /** Data connection activity: No traffic. */
2191    public static final int DATA_ACTIVITY_NONE = 0x00000000;
2192    /** Data connection activity: Currently receiving IP PPP traffic. */
2193    public static final int DATA_ACTIVITY_IN = 0x00000001;
2194    /** Data connection activity: Currently sending IP PPP traffic. */
2195    public static final int DATA_ACTIVITY_OUT = 0x00000002;
2196    /** Data connection activity: Currently both sending and receiving
2197     *  IP PPP traffic. */
2198    public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT;
2199    /**
2200     * Data connection is active, but physical link is down
2201     */
2202    public static final int DATA_ACTIVITY_DORMANT = 0x00000004;
2203
2204    /**
2205     * Returns a constant indicating the type of activity on a data connection
2206     * (cellular).
2207     *
2208     * @see #DATA_ACTIVITY_NONE
2209     * @see #DATA_ACTIVITY_IN
2210     * @see #DATA_ACTIVITY_OUT
2211     * @see #DATA_ACTIVITY_INOUT
2212     * @see #DATA_ACTIVITY_DORMANT
2213     */
2214    public int getDataActivity() {
2215        try {
2216            return getITelephony().getDataActivity();
2217        } catch (RemoteException ex) {
2218            // the phone process is restarting.
2219            return DATA_ACTIVITY_NONE;
2220        } catch (NullPointerException ex) {
2221          // the phone process is restarting.
2222          return DATA_ACTIVITY_NONE;
2223      }
2224    }
2225
2226    /** Data connection state: Unknown.  Used before we know the state.
2227     * @hide
2228     */
2229    public static final int DATA_UNKNOWN        = -1;
2230    /** Data connection state: Disconnected. IP traffic not available. */
2231    public static final int DATA_DISCONNECTED   = 0;
2232    /** Data connection state: Currently setting up a data connection. */
2233    public static final int DATA_CONNECTING     = 1;
2234    /** Data connection state: Connected. IP traffic should be available. */
2235    public static final int DATA_CONNECTED      = 2;
2236    /** Data connection state: Suspended. The connection is up, but IP
2237     * traffic is temporarily unavailable. For example, in a 2G network,
2238     * data activity may be suspended when a voice call arrives. */
2239    public static final int DATA_SUSPENDED      = 3;
2240
2241    /**
2242     * Returns a constant indicating the current data connection state
2243     * (cellular).
2244     *
2245     * @see #DATA_DISCONNECTED
2246     * @see #DATA_CONNECTING
2247     * @see #DATA_CONNECTED
2248     * @see #DATA_SUSPENDED
2249     */
2250    public int getDataState() {
2251        try {
2252            return getITelephony().getDataState();
2253        } catch (RemoteException ex) {
2254            // the phone process is restarting.
2255            return DATA_DISCONNECTED;
2256        } catch (NullPointerException ex) {
2257            return DATA_DISCONNECTED;
2258        }
2259    }
2260
2261   /**
2262    * @hide
2263    */
2264    private ITelephony getITelephony() {
2265        return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
2266    }
2267
2268    /**
2269    * @hide
2270    */
2271    private ITelecomService getTelecomService() {
2272        return ITelecomService.Stub.asInterface(ServiceManager.getService(Context.TELECOM_SERVICE));
2273    }
2274
2275    //
2276    //
2277    // PhoneStateListener
2278    //
2279    //
2280
2281    /**
2282     * Registers a listener object to receive notification of changes
2283     * in specified telephony states.
2284     * <p>
2285     * To register a listener, pass a {@link PhoneStateListener}
2286     * and specify at least one telephony state of interest in
2287     * the events argument.
2288     *
2289     * At registration, and when a specified telephony state
2290     * changes, the telephony manager invokes the appropriate
2291     * callback method on the listener object and passes the
2292     * current (updated) values.
2293     * <p>
2294     * To unregister a listener, pass the listener object and set the
2295     * events argument to
2296     * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0).
2297     *
2298     * @param listener The {@link PhoneStateListener} object to register
2299     *                 (or unregister)
2300     * @param events The telephony state(s) of interest to the listener,
2301     *               as a bitwise-OR combination of {@link PhoneStateListener}
2302     *               LISTEN_ flags.
2303     */
2304    public void listen(PhoneStateListener listener, int events) {
2305        String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>";
2306        try {
2307            Boolean notifyNow = (getITelephony() != null);
2308            sRegistry.listenForSubscriber(listener.mSubId, pkgForDebug, listener.callback, events, notifyNow);
2309        } catch (RemoteException ex) {
2310            // system process dead
2311        } catch (NullPointerException ex) {
2312            // system process dead
2313        }
2314    }
2315
2316    /**
2317     * Returns the CDMA ERI icon index to display
2318     *
2319     * @hide
2320     */
2321    public int getCdmaEriIconIndex() {
2322        return getCdmaEriIconIndex(getDefaultSubscription());
2323    }
2324
2325    /**
2326     * Returns the CDMA ERI icon index to display for a subscription
2327     */
2328    /** {@hide} */
2329    public int getCdmaEriIconIndex(int subId) {
2330        try {
2331            return getITelephony().getCdmaEriIconIndexForSubscriber(subId);
2332        } catch (RemoteException ex) {
2333            // the phone process is restarting.
2334            return -1;
2335        } catch (NullPointerException ex) {
2336            return -1;
2337        }
2338    }
2339
2340    /**
2341     * Returns the CDMA ERI icon mode,
2342     * 0 - ON
2343     * 1 - FLASHING
2344     *
2345     * @hide
2346     */
2347    public int getCdmaEriIconMode() {
2348        return getCdmaEriIconMode(getDefaultSubscription());
2349    }
2350
2351    /**
2352     * Returns the CDMA ERI icon mode for a subscription.
2353     * 0 - ON
2354     * 1 - FLASHING
2355     */
2356    /** {@hide} */
2357    public int getCdmaEriIconMode(int subId) {
2358        try {
2359            return getITelephony().getCdmaEriIconModeForSubscriber(subId);
2360        } catch (RemoteException ex) {
2361            // the phone process is restarting.
2362            return -1;
2363        } catch (NullPointerException ex) {
2364            return -1;
2365        }
2366    }
2367
2368    /**
2369     * Returns the CDMA ERI text,
2370     *
2371     * @hide
2372     */
2373    public String getCdmaEriText() {
2374        return getCdmaEriText(getDefaultSubscription());
2375    }
2376
2377    /**
2378     * Returns the CDMA ERI text, of a subscription
2379     *
2380     */
2381    /** {@hide} */
2382    public String getCdmaEriText(int subId) {
2383        try {
2384            return getITelephony().getCdmaEriTextForSubscriber(subId);
2385        } catch (RemoteException ex) {
2386            // the phone process is restarting.
2387            return null;
2388        } catch (NullPointerException ex) {
2389            return null;
2390        }
2391    }
2392
2393    /**
2394     * @return true if the current device is "voice capable".
2395     * <p>
2396     * "Voice capable" means that this device supports circuit-switched
2397     * (i.e. voice) phone calls over the telephony network, and is allowed
2398     * to display the in-call UI while a cellular voice call is active.
2399     * This will be false on "data only" devices which can't make voice
2400     * calls and don't support any in-call UI.
2401     * <p>
2402     * Note: the meaning of this flag is subtly different from the
2403     * PackageManager.FEATURE_TELEPHONY system feature, which is available
2404     * on any device with a telephony radio, even if the device is
2405     * data-only.
2406     */
2407    public boolean isVoiceCapable() {
2408        if (mContext == null) return true;
2409        return mContext.getResources().getBoolean(
2410                com.android.internal.R.bool.config_voice_capable);
2411    }
2412
2413    /**
2414     * @return true if the current device supports sms service.
2415     * <p>
2416     * If true, this means that the device supports both sending and
2417     * receiving sms via the telephony network.
2418     * <p>
2419     * Note: Voicemail waiting sms, cell broadcasting sms, and MMS are
2420     *       disabled when device doesn't support sms.
2421     */
2422    public boolean isSmsCapable() {
2423        if (mContext == null) return true;
2424        return mContext.getResources().getBoolean(
2425                com.android.internal.R.bool.config_sms_capable);
2426    }
2427
2428    /**
2429     * Returns all observed cell information from all radios on the
2430     * device including the primary and neighboring cells. This does
2431     * not cause or change the rate of PhoneStateListner#onCellInfoChanged.
2432     *<p>
2433     * The list can include one or more of {@link android.telephony.CellInfoGsm CellInfoGsm},
2434     * {@link android.telephony.CellInfoCdma CellInfoCdma},
2435     * {@link android.telephony.CellInfoLte CellInfoLte} and
2436     * {@link android.telephony.CellInfoWcdma CellInfoCdma} in any combination.
2437     * Specifically on devices with multiple radios it is typical to see instances of
2438     * one or more of any these in the list. In addition 0, 1 or more CellInfo
2439     * objects may return isRegistered() true.
2440     *<p>
2441     * This is preferred over using getCellLocation although for older
2442     * devices this may return null in which case getCellLocation should
2443     * be called.
2444     *<p>
2445     * @return List of CellInfo or null if info unavailable.
2446     *
2447     * <p>Requires Permission: {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}
2448     */
2449    public List<CellInfo> getAllCellInfo() {
2450        try {
2451            return getITelephony().getAllCellInfo();
2452        } catch (RemoteException ex) {
2453            return null;
2454        } catch (NullPointerException ex) {
2455            return null;
2456        }
2457    }
2458
2459    /**
2460     * Sets the minimum time in milli-seconds between {@link PhoneStateListener#onCellInfoChanged
2461     * PhoneStateListener.onCellInfoChanged} will be invoked.
2462     *<p>
2463     * The default, 0, means invoke onCellInfoChanged when any of the reported
2464     * information changes. Setting the value to INT_MAX(0x7fffffff) means never issue
2465     * A onCellInfoChanged.
2466     *<p>
2467     * @param rateInMillis the rate
2468     *
2469     * @hide
2470     */
2471    public void setCellInfoListRate(int rateInMillis) {
2472        try {
2473            getITelephony().setCellInfoListRate(rateInMillis);
2474        } catch (RemoteException ex) {
2475        } catch (NullPointerException ex) {
2476        }
2477    }
2478
2479    /**
2480     * Returns the MMS user agent.
2481     */
2482    public String getMmsUserAgent() {
2483        if (mContext == null) return null;
2484        return mContext.getResources().getString(
2485                com.android.internal.R.string.config_mms_user_agent);
2486    }
2487
2488    /**
2489     * Returns the MMS user agent profile URL.
2490     */
2491    public String getMmsUAProfUrl() {
2492        if (mContext == null) return null;
2493        return mContext.getResources().getString(
2494                com.android.internal.R.string.config_mms_user_agent_profile_url);
2495    }
2496
2497    /**
2498     * Opens a logical channel to the ICC card.
2499     *
2500     * Input parameters equivalent to TS 27.007 AT+CCHO command.
2501     *
2502     * <p>Requires Permission:
2503     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2504     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
2505     *
2506     * @param AID Application id. See ETSI 102.221 and 101.220.
2507     * @return an IccOpenLogicalChannelResponse object.
2508     */
2509    public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
2510        try {
2511            return getITelephony().iccOpenLogicalChannel(AID);
2512        } catch (RemoteException ex) {
2513        } catch (NullPointerException ex) {
2514        }
2515        return null;
2516    }
2517
2518    /**
2519     * Closes a previously opened logical channel to the ICC card.
2520     *
2521     * Input parameters equivalent to TS 27.007 AT+CCHC command.
2522     *
2523     * <p>Requires Permission:
2524     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2525     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
2526     *
2527     * @param channel is the channel id to be closed as retruned by a successful
2528     *            iccOpenLogicalChannel.
2529     * @return true if the channel was closed successfully.
2530     */
2531    public boolean iccCloseLogicalChannel(int channel) {
2532        try {
2533            return getITelephony().iccCloseLogicalChannel(channel);
2534        } catch (RemoteException ex) {
2535        } catch (NullPointerException ex) {
2536        }
2537        return false;
2538    }
2539
2540    /**
2541     * Transmit an APDU to the ICC card over a logical channel.
2542     *
2543     * Input parameters equivalent to TS 27.007 AT+CGLA command.
2544     *
2545     * <p>Requires Permission:
2546     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2547     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
2548     *
2549     * @param channel is the channel id to be closed as returned by a successful
2550     *            iccOpenLogicalChannel.
2551     * @param cla Class of the APDU command.
2552     * @param instruction Instruction of the APDU command.
2553     * @param p1 P1 value of the APDU command.
2554     * @param p2 P2 value of the APDU command.
2555     * @param p3 P3 value of the APDU command. If p3 is negative a 4 byte APDU
2556     *            is sent to the SIM.
2557     * @param data Data to be sent with the APDU.
2558     * @return The APDU response from the ICC card with the status appended at
2559     *            the end.
2560     */
2561    public String iccTransmitApduLogicalChannel(int channel, int cla,
2562            int instruction, int p1, int p2, int p3, String data) {
2563        try {
2564            return getITelephony().iccTransmitApduLogicalChannel(channel, cla,
2565                    instruction, p1, p2, p3, data);
2566        } catch (RemoteException ex) {
2567        } catch (NullPointerException ex) {
2568        }
2569        return "";
2570    }
2571
2572    /**
2573     * Transmit an APDU to the ICC card over the basic channel.
2574     *
2575     * Input parameters equivalent to TS 27.007 AT+CSIM command.
2576     *
2577     * <p>Requires Permission:
2578     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2579     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
2580     *
2581     * @param cla Class of the APDU command.
2582     * @param instruction Instruction of the APDU command.
2583     * @param p1 P1 value of the APDU command.
2584     * @param p2 P2 value of the APDU command.
2585     * @param p3 P3 value of the APDU command. If p3 is negative a 4 byte APDU
2586     *            is sent to the SIM.
2587     * @param data Data to be sent with the APDU.
2588     * @return The APDU response from the ICC card with the status appended at
2589     *            the end.
2590     */
2591    public String iccTransmitApduBasicChannel(int cla,
2592            int instruction, int p1, int p2, int p3, String data) {
2593        try {
2594            return getITelephony().iccTransmitApduBasicChannel(cla,
2595                    instruction, p1, p2, p3, data);
2596        } catch (RemoteException ex) {
2597        } catch (NullPointerException ex) {
2598        }
2599        return "";
2600    }
2601
2602    /**
2603     * Returns the response APDU for a command APDU sent through SIM_IO.
2604     *
2605     * <p>Requires Permission:
2606     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2607     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
2608     *
2609     * @param fileID
2610     * @param command
2611     * @param p1 P1 value of the APDU command.
2612     * @param p2 P2 value of the APDU command.
2613     * @param p3 P3 value of the APDU command.
2614     * @param filePath
2615     * @return The APDU response.
2616     */
2617    public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
2618            String filePath) {
2619        try {
2620            return getITelephony().iccExchangeSimIO(fileID, command, p1, p2,
2621                p3, filePath);
2622        } catch (RemoteException ex) {
2623        } catch (NullPointerException ex) {
2624        }
2625        return null;
2626    }
2627
2628    /**
2629     * Send ENVELOPE to the SIM and return the response.
2630     *
2631     * <p>Requires Permission:
2632     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2633     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
2634     *
2635     * @param content String containing SAT/USAT response in hexadecimal
2636     *                format starting with command tag. See TS 102 223 for
2637     *                details.
2638     * @return The APDU response from the ICC card in hexadecimal format
2639     *         with the last 4 bytes being the status word. If the command fails,
2640     *         returns an empty string.
2641     */
2642    public String sendEnvelopeWithStatus(String content) {
2643        try {
2644            return getITelephony().sendEnvelopeWithStatus(content);
2645        } catch (RemoteException ex) {
2646        } catch (NullPointerException ex) {
2647        }
2648        return "";
2649    }
2650
2651    /**
2652     * Read one of the NV items defined in com.android.internal.telephony.RadioNVItems.
2653     * Used for device configuration by some CDMA operators.
2654     * <p>
2655     * Requires Permission:
2656     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2657     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
2658     *
2659     * @param itemID the ID of the item to read.
2660     * @return the NV item as a String, or null on any failure.
2661     *
2662     * @hide
2663     */
2664    public String nvReadItem(int itemID) {
2665        try {
2666            return getITelephony().nvReadItem(itemID);
2667        } catch (RemoteException ex) {
2668            Rlog.e(TAG, "nvReadItem RemoteException", ex);
2669        } catch (NullPointerException ex) {
2670            Rlog.e(TAG, "nvReadItem NPE", ex);
2671        }
2672        return "";
2673    }
2674
2675    /**
2676     * Write one of the NV items defined in com.android.internal.telephony.RadioNVItems.
2677     * Used for device configuration by some CDMA operators.
2678     * <p>
2679     * Requires Permission:
2680     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2681     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
2682     *
2683     * @param itemID the ID of the item to read.
2684     * @param itemValue the value to write, as a String.
2685     * @return true on success; false on any failure.
2686     *
2687     * @hide
2688     */
2689    public boolean nvWriteItem(int itemID, String itemValue) {
2690        try {
2691            return getITelephony().nvWriteItem(itemID, itemValue);
2692        } catch (RemoteException ex) {
2693            Rlog.e(TAG, "nvWriteItem RemoteException", ex);
2694        } catch (NullPointerException ex) {
2695            Rlog.e(TAG, "nvWriteItem NPE", ex);
2696        }
2697        return false;
2698    }
2699
2700    /**
2701     * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2702     * Used for device configuration by some CDMA operators.
2703     * <p>
2704     * Requires Permission:
2705     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2706     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
2707     *
2708     * @param preferredRoamingList byte array containing the new PRL.
2709     * @return true on success; false on any failure.
2710     *
2711     * @hide
2712     */
2713    public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
2714        try {
2715            return getITelephony().nvWriteCdmaPrl(preferredRoamingList);
2716        } catch (RemoteException ex) {
2717            Rlog.e(TAG, "nvWriteCdmaPrl RemoteException", ex);
2718        } catch (NullPointerException ex) {
2719            Rlog.e(TAG, "nvWriteCdmaPrl NPE", ex);
2720        }
2721        return false;
2722    }
2723
2724    /**
2725     * Perform the specified type of NV config reset. The radio will be taken offline
2726     * and the device must be rebooted after the operation. Used for device
2727     * configuration by some CDMA operators.
2728     * <p>
2729     * Requires Permission:
2730     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2731     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
2732     *
2733     * @param resetType reset type: 1: reload NV reset, 2: erase NV reset, 3: factory NV reset
2734     * @return true on success; false on any failure.
2735     *
2736     * @hide
2737     */
2738    public boolean nvResetConfig(int resetType) {
2739        try {
2740            return getITelephony().nvResetConfig(resetType);
2741        } catch (RemoteException ex) {
2742            Rlog.e(TAG, "nvResetConfig RemoteException", ex);
2743        } catch (NullPointerException ex) {
2744            Rlog.e(TAG, "nvResetConfig NPE", ex);
2745        }
2746        return false;
2747    }
2748
2749    /**
2750     * Returns Default subscription.
2751     */
2752    private static int getDefaultSubscription() {
2753        return SubscriptionManager.getDefaultSubId();
2754    }
2755
2756    /**
2757     * Returns Default phone.
2758     */
2759    private static int getDefaultPhone() {
2760        return SubscriptionManager.getPhoneId(SubscriptionManager.getDefaultSubId());
2761    }
2762
2763    /** {@hide} */
2764    public int getDefaultSim() {
2765        return SubscriptionManager.getSlotId(SubscriptionManager.getDefaultSubId());
2766    }
2767
2768    /**
2769     * Sets the telephony property with the value specified.
2770     *
2771     * @hide
2772     */
2773    public static void setTelephonyProperty(int phoneId, String property, String value) {
2774        String propVal = "";
2775        String p[] = null;
2776        String prop = SystemProperties.get(property);
2777
2778        if (value == null) {
2779            value = "";
2780        }
2781
2782        if (prop != null) {
2783            p = prop.split(",");
2784        }
2785
2786        if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2787            Rlog.d(TAG, "setTelephonyProperty: invalid phoneId=" + phoneId +
2788                    " property=" + property + " value: " + value + " prop=" + prop);
2789            return;
2790        }
2791
2792        for (int i = 0; i < phoneId; i++) {
2793            String str = "";
2794            if ((p != null) && (i < p.length)) {
2795                str = p[i];
2796            }
2797            propVal = propVal + str + ",";
2798        }
2799
2800        propVal = propVal + value;
2801        if (p != null) {
2802            for (int i = phoneId + 1; i < p.length; i++) {
2803                propVal = propVal + "," + p[i];
2804            }
2805        }
2806
2807        if (property.length() > SystemProperties.PROP_NAME_MAX
2808                || propVal.length() > SystemProperties.PROP_VALUE_MAX) {
2809            Rlog.d(TAG, "setTelephonyProperty: property to long phoneId=" + phoneId +
2810                    " property=" + property + " value: " + value + " propVal=" + propVal);
2811            return;
2812        }
2813
2814        Rlog.d(TAG, "setTelephonyProperty: success phoneId=" + phoneId +
2815                " property=" + property + " value: " + value + " propVal=" + propVal);
2816        SystemProperties.set(property, propVal);
2817    }
2818
2819    /**
2820     * Convenience function for retrieving a value from the secure settings
2821     * value list as an integer.  Note that internally setting values are
2822     * always stored as strings; this function converts the string to an
2823     * integer for you.
2824     * <p>
2825     * This version does not take a default value.  If the setting has not
2826     * been set, or the string value is not a number,
2827     * it throws {@link SettingNotFoundException}.
2828     *
2829     * @param cr The ContentResolver to access.
2830     * @param name The name of the setting to retrieve.
2831     * @param index The index of the list
2832     *
2833     * @throws SettingNotFoundException Thrown if a setting by the given
2834     * name can't be found or the setting value is not an integer.
2835     *
2836     * @return The value at the given index of settings.
2837     * @hide
2838     */
2839    public static int getIntAtIndex(android.content.ContentResolver cr,
2840            String name, int index)
2841            throws android.provider.Settings.SettingNotFoundException {
2842        String v = android.provider.Settings.Global.getString(cr, name);
2843        if (v != null) {
2844            String valArray[] = v.split(",");
2845            if ((index >= 0) && (index < valArray.length) && (valArray[index] != null)) {
2846                try {
2847                    return Integer.parseInt(valArray[index]);
2848                } catch (NumberFormatException e) {
2849                    //Log.e(TAG, "Exception while parsing Integer: ", e);
2850                }
2851            }
2852        }
2853        throw new android.provider.Settings.SettingNotFoundException(name);
2854    }
2855
2856    /**
2857     * Convenience function for updating settings value as coma separated
2858     * integer values. This will either create a new entry in the table if the
2859     * given name does not exist, or modify the value of the existing row
2860     * with that name.  Note that internally setting values are always
2861     * stored as strings, so this function converts the given value to a
2862     * string before storing it.
2863     *
2864     * @param cr The ContentResolver to access.
2865     * @param name The name of the setting to modify.
2866     * @param index The index of the list
2867     * @param value The new value for the setting to be added to the list.
2868     * @return true if the value was set, false on database errors
2869     * @hide
2870     */
2871    public static boolean putIntAtIndex(android.content.ContentResolver cr,
2872            String name, int index, int value) {
2873        String data = "";
2874        String valArray[] = null;
2875        String v = android.provider.Settings.Global.getString(cr, name);
2876
2877        if (index == Integer.MAX_VALUE) {
2878            throw new RuntimeException("putIntAtIndex index == MAX_VALUE index=" + index);
2879        }
2880        if (index < 0) {
2881            throw new RuntimeException("putIntAtIndex index < 0 index=" + index);
2882        }
2883        if (v != null) {
2884            valArray = v.split(",");
2885        }
2886
2887        // Copy the elements from valArray till index
2888        for (int i = 0; i < index; i++) {
2889            String str = "";
2890            if ((valArray != null) && (i < valArray.length)) {
2891                str = valArray[i];
2892            }
2893            data = data + str + ",";
2894        }
2895
2896        data = data + value;
2897
2898        // Copy the remaining elements from valArray if any.
2899        if (valArray != null) {
2900            for (int i = index+1; i < valArray.length; i++) {
2901                data = data + "," + valArray[i];
2902            }
2903        }
2904        return android.provider.Settings.Global.putString(cr, name, data);
2905    }
2906
2907    /**
2908     * Gets the telephony property.
2909     *
2910     * @hide
2911     */
2912    public static String getTelephonyProperty(int phoneId, String property, String defaultVal) {
2913        String propVal = null;
2914        String prop = SystemProperties.get(property);
2915        if ((prop != null) && (prop.length() > 0)) {
2916            String values[] = prop.split(",");
2917            if ((phoneId >= 0) && (phoneId < values.length) && (values[phoneId] != null)) {
2918                propVal = values[phoneId];
2919            }
2920        }
2921        Rlog.d(TAG, "getTelephonyProperty: return propVal='" + propVal + "' phoneId=" + phoneId
2922                + " property='" + property + "' defaultVal='" + defaultVal + "' prop=" + prop);
2923        return propVal == null ? defaultVal : propVal;
2924    }
2925
2926    /** @hide */
2927    public int getSimCount() {
2928        // FIXME Need to get it from Telephony Dev Controller when that gets implemented!
2929        // and then this method shouldn't be used at all!
2930        if(isMultiSimEnabled()) {
2931            return 2;
2932        } else {
2933            return 1;
2934        }
2935    }
2936
2937    /**
2938     * Returns the IMS Service Table (IST) that was loaded from the ISIM.
2939     * @return IMS Service Table or null if not present or not loaded
2940     * @hide
2941     */
2942    public String getIsimIst() {
2943        try {
2944            return getSubscriberInfo().getIsimIst();
2945        } catch (RemoteException ex) {
2946            return null;
2947        } catch (NullPointerException ex) {
2948            // This could happen before phone restarts due to crashing
2949            return null;
2950        }
2951    }
2952
2953    /**
2954     * Returns the IMS Proxy Call Session Control Function(PCSCF) that were loaded from the ISIM.
2955     * @return an array of PCSCF strings with one PCSCF per string, or null if
2956     *         not present or not loaded
2957     * @hide
2958     */
2959    public String[] getIsimPcscf() {
2960        try {
2961            return getSubscriberInfo().getIsimPcscf();
2962        } catch (RemoteException ex) {
2963            return null;
2964        } catch (NullPointerException ex) {
2965            // This could happen before phone restarts due to crashing
2966            return null;
2967        }
2968    }
2969
2970    /**
2971     * Returns the response of ISIM Authetification through RIL.
2972     * Returns null if the Authentification hasn't been successed or isn't present iphonesubinfo.
2973     * @return the response of ISIM Authetification, or null if not available
2974     * @hide
2975     * @deprecated
2976     * @see getIccSimChallengeResponse with appType=PhoneConstants.APPTYPE_ISIM
2977     */
2978    public String getIsimChallengeResponse(String nonce){
2979        try {
2980            return getSubscriberInfo().getIsimChallengeResponse(nonce);
2981        } catch (RemoteException ex) {
2982            return null;
2983        } catch (NullPointerException ex) {
2984            // This could happen before phone restarts due to crashing
2985            return null;
2986        }
2987    }
2988
2989    /**
2990     * Returns the response of SIM Authentication through RIL.
2991     * Returns null if the Authentication hasn't been successful
2992     * @param subId subscription ID to be queried
2993     * @param appType ICC application type (@see com.android.internal.telephony.PhoneConstants#APPTYPE_xxx)
2994     * @param data authentication challenge data
2995     * @return the response of SIM Authentication, or null if not available
2996     * @hide
2997     */
2998    public String getIccSimChallengeResponse(int subId, int appType, String data) {
2999        try {
3000            return getSubscriberInfo().getIccSimChallengeResponse(subId, appType, data);
3001        } catch (RemoteException ex) {
3002            return null;
3003        } catch (NullPointerException ex) {
3004            // This could happen before phone starts
3005            return null;
3006        }
3007    }
3008
3009    /**
3010     * Returns the response of SIM Authentication through RIL for the default subscription.
3011     * Returns null if the Authentication hasn't been successful
3012     * @param appType ICC application type (@see com.android.internal.telephony.PhoneConstants#APPTYPE_xxx)
3013     * @param data authentication challenge data
3014     * @return the response of SIM Authentication, or null if not available
3015     * @hide
3016     */
3017    public String getIccSimChallengeResponse(int appType, String data) {
3018        return getIccSimChallengeResponse(getDefaultSubscription(), appType, data);
3019    }
3020
3021    /**
3022     * Get P-CSCF address from PCO after data connection is established or modified.
3023     * @param apnType the apnType, "ims" for IMS APN, "emergency" for EMERGENCY APN
3024     * @return array of P-CSCF address
3025     * @hide
3026     */
3027    public String[] getPcscfAddress(String apnType) {
3028        try {
3029            return getITelephony().getPcscfAddress(apnType);
3030        } catch (RemoteException e) {
3031            return new String[0];
3032        }
3033    }
3034
3035    /**
3036     * Set IMS registration state
3037     *
3038     * @param Registration state
3039     * @hide
3040     */
3041    public void setImsRegistrationState(boolean registered) {
3042        try {
3043            getITelephony().setImsRegistrationState(registered);
3044        } catch (RemoteException e) {
3045        }
3046    }
3047
3048    /**
3049     * Get the preferred network type.
3050     * Used for device configuration by some CDMA operators.
3051     * <p>
3052     * Requires Permission:
3053     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3054     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3055     *
3056     * @return the preferred network type, defined in RILConstants.java.
3057     * @hide
3058     */
3059    public int getPreferredNetworkType() {
3060        try {
3061            return getITelephony().getPreferredNetworkType();
3062        } catch (RemoteException ex) {
3063            Rlog.e(TAG, "getPreferredNetworkType RemoteException", ex);
3064        } catch (NullPointerException ex) {
3065            Rlog.e(TAG, "getPreferredNetworkType NPE", ex);
3066        }
3067        return -1;
3068    }
3069
3070    /**
3071     * Set the preferred network type.
3072     * Used for device configuration by some CDMA operators.
3073     * <p>
3074     * Requires Permission:
3075     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3076     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3077     *
3078     * @param networkType the preferred network type, defined in RILConstants.java.
3079     * @return true on success; false on any failure.
3080     * @hide
3081     */
3082    public boolean setPreferredNetworkType(int networkType) {
3083        try {
3084            return getITelephony().setPreferredNetworkType(networkType);
3085        } catch (RemoteException ex) {
3086            Rlog.e(TAG, "setPreferredNetworkType RemoteException", ex);
3087        } catch (NullPointerException ex) {
3088            Rlog.e(TAG, "setPreferredNetworkType NPE", ex);
3089        }
3090        return false;
3091    }
3092
3093    /**
3094     * Set the preferred network type to global mode which includes LTE, CDMA, EvDo and GSM/WCDMA.
3095     *
3096     * <p>
3097     * Requires that the calling app has carrier privileges.
3098     * @see #hasCarrierPrivileges
3099     *
3100     * @return true on success; false on any failure.
3101     */
3102    public boolean setPreferredNetworkTypeToGlobal() {
3103        return setPreferredNetworkType(RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA);
3104    }
3105
3106    /**
3107     * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
3108     * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
3109     * tethering.
3110     *
3111     * @return 0: Not required. 1: required. 2: Not set.
3112     * @hide
3113     */
3114    public int getTetherApnRequired() {
3115        try {
3116            return getITelephony().getTetherApnRequired();
3117        } catch (RemoteException ex) {
3118            Rlog.e(TAG, "hasMatchedTetherApnSetting RemoteException", ex);
3119        } catch (NullPointerException ex) {
3120            Rlog.e(TAG, "hasMatchedTetherApnSetting NPE", ex);
3121        }
3122        return 2;
3123    }
3124
3125
3126    /**
3127     * Values used to return status for hasCarrierPrivileges call.
3128     */
3129    /** @hide */
3130    public static final int CARRIER_PRIVILEGE_STATUS_HAS_ACCESS = 1;
3131    /** @hide */
3132    public static final int CARRIER_PRIVILEGE_STATUS_NO_ACCESS = 0;
3133    /** @hide */
3134    public static final int CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED = -1;
3135    /** @hide */
3136    public static final int CARRIER_PRIVILEGE_STATUS_ERROR_LOADING_RULES = -2;
3137
3138    /**
3139     * Has the calling application been granted carrier privileges by the carrier.
3140     *
3141     * If any of the packages in the calling UID has carrier privileges, the
3142     * call will return true. This access is granted by the owner of the UICC
3143     * card and does not depend on the registered carrier.
3144     *
3145     * @return true if the app has carrier privileges.
3146     */
3147    public boolean hasCarrierPrivileges() {
3148        try {
3149            return getITelephony().getCarrierPrivilegeStatus() ==
3150                CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
3151        } catch (RemoteException ex) {
3152            Rlog.e(TAG, "hasCarrierPrivileges RemoteException", ex);
3153        } catch (NullPointerException ex) {
3154            Rlog.e(TAG, "hasCarrierPrivileges NPE", ex);
3155        }
3156        return false;
3157    }
3158
3159    /**
3160     * Override the branding for the current ICCID.
3161     *
3162     * Once set, whenever the SIM is present in the device, the service
3163     * provider name (SPN) and the operator name will both be replaced by the
3164     * brand value input. To unset the value, the same function should be
3165     * called with a null brand value.
3166     *
3167     * <p>Requires that the calling app has carrier privileges.
3168     * @see #hasCarrierPrivileges
3169     *
3170     * @param brand The brand name to display/set.
3171     * @return true if the operation was executed correctly.
3172     */
3173    public boolean setOperatorBrandOverride(String brand) {
3174        try {
3175            return getITelephony().setOperatorBrandOverride(brand);
3176        } catch (RemoteException ex) {
3177            Rlog.e(TAG, "setOperatorBrandOverride RemoteException", ex);
3178        } catch (NullPointerException ex) {
3179            Rlog.e(TAG, "setOperatorBrandOverride NPE", ex);
3180        }
3181        return false;
3182    }
3183
3184    /**
3185     * Override the roaming preference for the current ICCID.
3186     *
3187     * Using this call, the carrier app (see #hasCarrierPrivileges) can override
3188     * the platform's notion of a network operator being considered roaming or not.
3189     * The change only affects the ICCID that was active when this call was made.
3190     *
3191     * If null is passed as any of the input, the corresponding value is deleted.
3192     *
3193     * <p>Requires that the caller have carrier privilege. See #hasCarrierPrivileges.
3194     *
3195     * @param gsmRoamingList - List of MCCMNCs to be considered roaming for 3GPP RATs.
3196     * @param gsmNonRoamingList - List of MCCMNCs to be considered not roaming for 3GPP RATs.
3197     * @param cdmaRoamingList - List of SIDs to be considered roaming for 3GPP2 RATs.
3198     * @param cdmaNonRoamingList - List of SIDs to be considered not roaming for 3GPP2 RATs.
3199     * @return true if the operation was executed correctly.
3200     *
3201     * @hide
3202     */
3203    public boolean setRoamingOverride(List<String> gsmRoamingList,
3204            List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3205            List<String> cdmaNonRoamingList) {
3206        try {
3207            return getITelephony().setRoamingOverride(gsmRoamingList, gsmNonRoamingList,
3208                    cdmaRoamingList, cdmaNonRoamingList);
3209        } catch (RemoteException ex) {
3210            Rlog.e(TAG, "setRoamingOverride RemoteException", ex);
3211        } catch (NullPointerException ex) {
3212            Rlog.e(TAG, "setRoamingOverride NPE", ex);
3213        }
3214        return false;
3215    }
3216
3217    /**
3218     * Expose the rest of ITelephony to @SystemApi
3219     */
3220
3221    /** @hide */
3222    @SystemApi
3223    public String getCdmaMdn() {
3224        return getCdmaMdn(getDefaultSubscription());
3225    }
3226
3227    /** @hide */
3228    @SystemApi
3229    public String getCdmaMdn(int subId) {
3230        try {
3231            return getITelephony().getCdmaMdn(subId);
3232        } catch (RemoteException ex) {
3233            return null;
3234        } catch (NullPointerException ex) {
3235            return null;
3236        }
3237    }
3238
3239    /** @hide */
3240    @SystemApi
3241    public String getCdmaMin() {
3242        return getCdmaMin(getDefaultSubscription());
3243    }
3244
3245    /** @hide */
3246    @SystemApi
3247    public String getCdmaMin(int subId) {
3248        try {
3249            return getITelephony().getCdmaMin(subId);
3250        } catch (RemoteException ex) {
3251            return null;
3252        } catch (NullPointerException ex) {
3253            return null;
3254        }
3255    }
3256
3257    /** @hide */
3258    @SystemApi
3259    public int checkCarrierPrivilegesForPackage(String pkgname) {
3260        try {
3261            return getITelephony().checkCarrierPrivilegesForPackage(pkgname);
3262        } catch (RemoteException ex) {
3263            Rlog.e(TAG, "checkCarrierPrivilegesForPackage RemoteException", ex);
3264        } catch (NullPointerException ex) {
3265            Rlog.e(TAG, "checkCarrierPrivilegesForPackage NPE", ex);
3266        }
3267        return CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
3268    }
3269
3270    /** @hide */
3271    @SystemApi
3272    public List<String> getCarrierPackageNamesForIntent(Intent intent) {
3273        try {
3274            return getITelephony().getCarrierPackageNamesForIntent(intent);
3275        } catch (RemoteException ex) {
3276            Rlog.e(TAG, "getCarrierPackageNamesForIntent RemoteException", ex);
3277        } catch (NullPointerException ex) {
3278            Rlog.e(TAG, "getCarrierPackageNamesForIntent NPE", ex);
3279        }
3280        return null;
3281    }
3282
3283    /** @hide */
3284    @SystemApi
3285    public void dial(String number) {
3286        try {
3287            getITelephony().dial(number);
3288        } catch (RemoteException e) {
3289            Log.e(TAG, "Error calling ITelephony#dial", e);
3290        }
3291    }
3292
3293    /** @hide */
3294    @SystemApi
3295    public void call(String callingPackage, String number) {
3296        try {
3297            getITelephony().call(callingPackage, number);
3298        } catch (RemoteException e) {
3299            Log.e(TAG, "Error calling ITelephony#call", e);
3300        }
3301    }
3302
3303    /** @hide */
3304    @SystemApi
3305    public boolean endCall() {
3306        try {
3307            return getITelephony().endCall();
3308        } catch (RemoteException e) {
3309            Log.e(TAG, "Error calling ITelephony#endCall", e);
3310        }
3311        return false;
3312    }
3313
3314    /** @hide */
3315    @SystemApi
3316    public void answerRingingCall() {
3317        try {
3318            getITelephony().answerRingingCall();
3319        } catch (RemoteException e) {
3320            Log.e(TAG, "Error calling ITelephony#answerRingingCall", e);
3321        }
3322    }
3323
3324    /** @hide */
3325    @SystemApi
3326    public void silenceRinger() {
3327        try {
3328            getTelecomService().silenceRinger();
3329        } catch (RemoteException e) {
3330            Log.e(TAG, "Error calling ITelecomService#silenceRinger", e);
3331        }
3332    }
3333
3334    /** @hide */
3335    @SystemApi
3336    public boolean isOffhook() {
3337        try {
3338            return getITelephony().isOffhook();
3339        } catch (RemoteException e) {
3340            Log.e(TAG, "Error calling ITelephony#isOffhook", e);
3341        }
3342        return false;
3343    }
3344
3345    /** @hide */
3346    @SystemApi
3347    public boolean isRinging() {
3348        try {
3349            return getITelephony().isRinging();
3350        } catch (RemoteException e) {
3351            Log.e(TAG, "Error calling ITelephony#isRinging", e);
3352        }
3353        return false;
3354    }
3355
3356    /** @hide */
3357    @SystemApi
3358    public boolean isIdle() {
3359        try {
3360            return getITelephony().isIdle();
3361        } catch (RemoteException e) {
3362            Log.e(TAG, "Error calling ITelephony#isIdle", e);
3363        }
3364        return true;
3365    }
3366
3367    /** @hide */
3368    @SystemApi
3369    public boolean isRadioOn() {
3370        try {
3371            return getITelephony().isRadioOn();
3372        } catch (RemoteException e) {
3373            Log.e(TAG, "Error calling ITelephony#isRadioOn", e);
3374        }
3375        return false;
3376    }
3377
3378    /** @hide */
3379    @SystemApi
3380    public boolean isSimPinEnabled() {
3381        try {
3382            return getITelephony().isSimPinEnabled();
3383        } catch (RemoteException e) {
3384            Log.e(TAG, "Error calling ITelephony#isSimPinEnabled", e);
3385        }
3386        return false;
3387    }
3388
3389    /** @hide */
3390    @SystemApi
3391    public boolean supplyPin(String pin) {
3392        try {
3393            return getITelephony().supplyPin(pin);
3394        } catch (RemoteException e) {
3395            Log.e(TAG, "Error calling ITelephony#supplyPin", e);
3396        }
3397        return false;
3398    }
3399
3400    /** @hide */
3401    @SystemApi
3402    public boolean supplyPuk(String puk, String pin) {
3403        try {
3404            return getITelephony().supplyPuk(puk, pin);
3405        } catch (RemoteException e) {
3406            Log.e(TAG, "Error calling ITelephony#supplyPuk", e);
3407        }
3408        return false;
3409    }
3410
3411    /** @hide */
3412    @SystemApi
3413    public int[] supplyPinReportResult(String pin) {
3414        try {
3415            return getITelephony().supplyPinReportResult(pin);
3416        } catch (RemoteException e) {
3417            Log.e(TAG, "Error calling ITelephony#supplyPinReportResult", e);
3418        }
3419        return new int[0];
3420    }
3421
3422    /** @hide */
3423    @SystemApi
3424    public int[] supplyPukReportResult(String puk, String pin) {
3425        try {
3426            return getITelephony().supplyPukReportResult(puk, pin);
3427        } catch (RemoteException e) {
3428            Log.e(TAG, "Error calling ITelephony#]", e);
3429        }
3430        return new int[0];
3431    }
3432
3433    /** @hide */
3434    @SystemApi
3435    public boolean handlePinMmi(String dialString) {
3436        try {
3437            return getITelephony().handlePinMmi(dialString);
3438        } catch (RemoteException e) {
3439            Log.e(TAG, "Error calling ITelephony#handlePinMmi", e);
3440        }
3441        return false;
3442    }
3443
3444    /** @hide */
3445    @SystemApi
3446    public boolean handlePinMmiForSubscriber(int subId, String dialString) {
3447        try {
3448            return getITelephony().handlePinMmiForSubscriber(subId, dialString);
3449        } catch (RemoteException e) {
3450            Log.e(TAG, "Error calling ITelephony#handlePinMmi", e);
3451        }
3452        return false;
3453    }
3454
3455    /** @hide */
3456    @SystemApi
3457    public void toggleRadioOnOff() {
3458        try {
3459            getITelephony().toggleRadioOnOff();
3460        } catch (RemoteException e) {
3461            Log.e(TAG, "Error calling ITelephony#toggleRadioOnOff", e);
3462        }
3463    }
3464
3465    /** @hide */
3466    @SystemApi
3467    public boolean setRadio(boolean turnOn) {
3468        try {
3469            return getITelephony().setRadio(turnOn);
3470        } catch (RemoteException e) {
3471            Log.e(TAG, "Error calling ITelephony#setRadio", e);
3472        }
3473        return false;
3474    }
3475
3476    /** @hide */
3477    @SystemApi
3478    public boolean setRadioPower(boolean turnOn) {
3479        try {
3480            return getITelephony().setRadioPower(turnOn);
3481        } catch (RemoteException e) {
3482            Log.e(TAG, "Error calling ITelephony#setRadioPower", e);
3483        }
3484        return false;
3485    }
3486
3487    /** @hide */
3488    @SystemApi
3489    public void updateServiceLocation() {
3490        try {
3491            getITelephony().updateServiceLocation();
3492        } catch (RemoteException e) {
3493            Log.e(TAG, "Error calling ITelephony#updateServiceLocation", e);
3494        }
3495    }
3496
3497    /** @hide */
3498    @SystemApi
3499    public boolean enableDataConnectivity() {
3500        try {
3501            return getITelephony().enableDataConnectivity();
3502        } catch (RemoteException e) {
3503            Log.e(TAG, "Error calling ITelephony#enableDataConnectivity", e);
3504        }
3505        return false;
3506    }
3507
3508    /** @hide */
3509    @SystemApi
3510    public boolean disableDataConnectivity() {
3511        try {
3512            return getITelephony().disableDataConnectivity();
3513        } catch (RemoteException e) {
3514            Log.e(TAG, "Error calling ITelephony#disableDataConnectivity", e);
3515        }
3516        return false;
3517    }
3518
3519    /** @hide */
3520    @SystemApi
3521    public boolean isDataConnectivityPossible() {
3522        try {
3523            return getITelephony().isDataConnectivityPossible();
3524        } catch (RemoteException e) {
3525            Log.e(TAG, "Error calling ITelephony#isDataConnectivityPossible", e);
3526        }
3527        return false;
3528    }
3529
3530    /** @hide */
3531    @SystemApi
3532    public boolean needsOtaServiceProvisioning() {
3533        try {
3534            return getITelephony().needsOtaServiceProvisioning();
3535        } catch (RemoteException e) {
3536            Log.e(TAG, "Error calling ITelephony#needsOtaServiceProvisioning", e);
3537        }
3538        return false;
3539    }
3540
3541    /** @hide */
3542    @SystemApi
3543    public void setDataEnabled(boolean enable) {
3544        setDataEnabled(SubscriptionManager.getDefaultDataSubId(), enable);
3545    }
3546
3547    /** @hide */
3548    @SystemApi
3549    public void setDataEnabled(int subId, boolean enable) {
3550        try {
3551            Log.d(TAG, "setDataEnabled: enabled=" + enable);
3552            getITelephony().setDataEnabled(subId, enable);
3553        } catch (RemoteException e) {
3554            Log.e(TAG, "Error calling ITelephony#setDataEnabled", e);
3555        }
3556    }
3557
3558    /** @hide */
3559    @SystemApi
3560    public boolean getDataEnabled() {
3561        return getDataEnabled(SubscriptionManager.getDefaultDataSubId());
3562    }
3563
3564    /** @hide */
3565    @SystemApi
3566    public boolean getDataEnabled(int subId) {
3567        boolean retVal;
3568        try {
3569            retVal = getITelephony().getDataEnabled(subId);
3570        } catch (RemoteException e) {
3571            Log.e(TAG, "Error calling ITelephony#getDataEnabled", e);
3572            retVal = false;
3573        }
3574        Log.d(TAG, "getDataEnabled: retVal=" + retVal);
3575        return retVal;
3576    }
3577
3578    /**
3579     * Returns the result and response from RIL for oem request
3580     *
3581     * @param oemReq the data is sent to ril.
3582     * @param oemResp the respose data from RIL.
3583     * @return negative value request was not handled or get error
3584     *         0 request was handled succesfully, but no response data
3585     *         positive value success, data length of response
3586     * @hide
3587     */
3588    public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3589        try {
3590            return getITelephony().invokeOemRilRequestRaw(oemReq, oemResp);
3591        } catch (RemoteException ex) {
3592        } catch (NullPointerException ex) {
3593        }
3594        return -1;
3595    }
3596
3597    /** @hide */
3598    @SystemApi
3599    public void enableVideoCalling(boolean enable) {
3600        try {
3601            getITelephony().enableVideoCalling(enable);
3602        } catch (RemoteException e) {
3603            Log.e(TAG, "Error calling ITelephony#enableVideoCalling", e);
3604        }
3605    }
3606
3607    /** @hide */
3608    @SystemApi
3609    public boolean isVideoCallingEnabled() {
3610        try {
3611            return getITelephony().isVideoCallingEnabled();
3612        } catch (RemoteException e) {
3613            Log.e(TAG, "Error calling ITelephony#isVideoCallingEnabled", e);
3614        }
3615        return false;
3616    }
3617
3618    /**
3619     * This function retrieves value for setting "name+subId", and if that is not found
3620     * retrieves value for setting "name", and if that is not found uses def as default
3621     *
3622     * @hide */
3623    public static int getIntWithSubId(ContentResolver cr, String name, int subId, int def) {
3624        return Settings.Global.getInt(cr, name + subId, Settings.Global.getInt(cr, name, def));
3625    }
3626
3627    /**
3628     * This function retrieves value for setting "name+subId", and if that is not found
3629     * retrieves value for setting "name", and if that is not found throws
3630     * SettingNotFoundException
3631     *
3632     * @hide */
3633    public static int getIntWithSubId(ContentResolver cr, String name, int subId)
3634            throws SettingNotFoundException {
3635        try {
3636            return Settings.Global.getInt(cr, name + subId);
3637        } catch (SettingNotFoundException e) {
3638            try {
3639                int val = Settings.Global.getInt(cr, name);
3640                Settings.Global.putInt(cr, name + subId, val);
3641
3642                /* We are now moving from 'setting' to 'setting+subId', and using the value stored
3643                 * for 'setting' as default. Reset the default (since it may have a user set
3644                 * value). */
3645                int default_val = val;
3646                if (name.equals(Settings.Global.MOBILE_DATA)) {
3647                    default_val = "true".equalsIgnoreCase(
3648                            SystemProperties.get("ro.com.android.mobiledata", "true")) ? 1 : 0;
3649                } else if (name.equals(Settings.Global.DATA_ROAMING)) {
3650                    default_val = "true".equalsIgnoreCase(
3651                            SystemProperties.get("ro.com.android.dataroaming", "false")) ? 1 : 0;
3652                }
3653
3654                if (default_val != val) {
3655                    Settings.Global.putInt(cr, name, default_val);
3656                }
3657
3658                return val;
3659            } catch (SettingNotFoundException exc) {
3660                throw new SettingNotFoundException(name);
3661            }
3662        }
3663    }
3664
3665   /**
3666    * Returns the IMS Registration Status
3667    *@hide
3668    */
3669   public boolean isImsRegistered() {
3670       try {
3671           return getITelephony().isImsRegistered();
3672       } catch (RemoteException ex) {
3673           return false;
3674       } catch (NullPointerException ex) {
3675           return false;
3676       }
3677   }
3678}
3679