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