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