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