TelephonyManager.java revision ef5a401b7e3a68b0411886572c1a18b948f00c3d
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.IntDef;
20import android.annotation.Nullable;
21import android.annotation.SystemApi;
22import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
24import android.app.ActivityThread;
25import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
28import android.net.ConnectivityManager;
29import android.net.Uri;
30import android.os.BatteryStats;
31import android.os.ResultReceiver;
32import android.provider.Settings;
33import android.provider.Settings.SettingNotFoundException;
34import android.os.Bundle;
35import android.os.PersistableBundle;
36import android.os.RemoteException;
37import android.os.ServiceManager;
38import android.os.SystemProperties;
39import android.service.carrier.CarrierIdentifier;
40import android.telecom.PhoneAccount;
41import android.telecom.PhoneAccountHandle;
42import android.telephony.ClientRequestStats;
43import android.telephony.TelephonyHistogram;
44import android.telephony.ims.feature.ImsFeature;
45import android.util.Log;
46
47import com.android.ims.internal.IImsServiceController;
48import com.android.ims.internal.IImsServiceFeatureListener;
49import com.android.internal.telecom.ITelecomService;
50import com.android.internal.telephony.CellNetworkScanResult;
51import com.android.internal.telephony.IPhoneSubInfo;
52import com.android.internal.telephony.ITelephony;
53import com.android.internal.telephony.ITelephonyRegistry;
54import com.android.internal.telephony.OperatorInfo;
55import com.android.internal.telephony.PhoneConstants;
56import com.android.internal.telephony.RILConstants;
57import com.android.internal.telephony.TelephonyProperties;
58
59import java.io.FileInputStream;
60import java.io.IOException;
61import java.lang.annotation.Retention;
62import java.lang.annotation.RetentionPolicy;
63import java.util.ArrayList;
64import java.util.Collections;
65import java.util.List;
66import java.util.regex.Matcher;
67import java.util.regex.Pattern;
68
69/**
70 * Provides access to information about the telephony services on
71 * the device. Applications can use the methods in this class to
72 * determine telephony services and states, as well as to access some
73 * types of subscriber information. Applications can also register
74 * a listener to receive notification of telephony state changes.
75 * <p>
76 * You do not instantiate this class directly; instead, you retrieve
77 * a reference to an instance through
78 * {@link android.content.Context#getSystemService
79 * Context.getSystemService(Context.TELEPHONY_SERVICE)}.
80 *
81 * The returned TelephonyManager will use the default subscription for all calls.
82 * To call an API for a specific subscription, use {@link #createForSubscriptionId(int)}. e.g.
83 * <code>
84 *   telephonyManager = defaultSubTelephonyManager.createForSubscriptionId(subId);
85 * </code>
86 * <p>
87 * Note that access to some telephony information is
88 * permission-protected. Your application cannot access the protected
89 * information unless it has the appropriate permissions declared in
90 * its manifest file. Where permissions apply, they are noted in the
91 * the methods through which you access the protected information.
92 */
93public class TelephonyManager {
94    private static final String TAG = "TelephonyManager";
95
96    /**
97     * The key to use when placing the result of {@link #requestModemActivityInfo(ResultReceiver)}
98     * into the ResultReceiver Bundle.
99     * @hide
100     */
101    public static final String MODEM_ACTIVITY_RESULT_KEY =
102            BatteryStats.RESULT_RECEIVER_CONTROLLER_KEY;
103
104    private static ITelephonyRegistry sRegistry;
105
106    /**
107     * The allowed states of Wi-Fi calling.
108     *
109     * @hide
110     */
111    public interface WifiCallingChoices {
112        /** Always use Wi-Fi calling */
113        static final int ALWAYS_USE = 0;
114        /** Ask the user whether to use Wi-Fi on every call */
115        static final int ASK_EVERY_TIME = 1;
116        /** Never use Wi-Fi calling */
117        static final int NEVER_USE = 2;
118    }
119
120    private final Context mContext;
121    private final int mSubId;
122    private SubscriptionManager mSubscriptionManager;
123
124    private static String multiSimConfig =
125            SystemProperties.get(TelephonyProperties.PROPERTY_MULTI_SIM_CONFIG);
126
127    /** Enum indicating multisim variants
128     *  DSDS - Dual SIM Dual Standby
129     *  DSDA - Dual SIM Dual Active
130     *  TSTS - Triple SIM Triple Standby
131     **/
132    /** @hide */
133    public enum MultiSimVariants {
134        DSDS,
135        DSDA,
136        TSTS,
137        UNKNOWN
138    };
139
140    /** @hide */
141    public TelephonyManager(Context context) {
142      this(context, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
143    }
144
145    /** @hide */
146    public TelephonyManager(Context context, int subId) {
147        mSubId = subId;
148        Context appContext = context.getApplicationContext();
149        if (appContext != null) {
150            mContext = appContext;
151        } else {
152            mContext = context;
153        }
154        mSubscriptionManager = SubscriptionManager.from(mContext);
155
156        if (sRegistry == null) {
157            sRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
158                    "telephony.registry"));
159        }
160    }
161
162    /** @hide */
163    private TelephonyManager() {
164        mContext = null;
165        mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
166    }
167
168    private static TelephonyManager sInstance = new TelephonyManager();
169
170    /** @hide
171    /* @deprecated - use getSystemService as described above */
172    public static TelephonyManager getDefault() {
173        return sInstance;
174    }
175
176    private String getOpPackageName() {
177        // For legacy reasons the TelephonyManager has API for getting
178        // a static instance with no context set preventing us from
179        // getting the op package name. As a workaround we do a best
180        // effort and get the context from the current activity thread.
181        if (mContext != null) {
182            return mContext.getOpPackageName();
183        }
184        return ActivityThread.currentOpPackageName();
185    }
186
187    /**
188     * Returns the multi SIM variant
189     * Returns DSDS for Dual SIM Dual Standby
190     * Returns DSDA for Dual SIM Dual Active
191     * Returns TSTS for Triple SIM Triple Standby
192     * Returns UNKNOWN for others
193     */
194    /** {@hide} */
195    public MultiSimVariants getMultiSimConfiguration() {
196        String mSimConfig =
197            SystemProperties.get(TelephonyProperties.PROPERTY_MULTI_SIM_CONFIG);
198        if (mSimConfig.equals("dsds")) {
199            return MultiSimVariants.DSDS;
200        } else if (mSimConfig.equals("dsda")) {
201            return MultiSimVariants.DSDA;
202        } else if (mSimConfig.equals("tsts")) {
203            return MultiSimVariants.TSTS;
204        } else {
205            return MultiSimVariants.UNKNOWN;
206        }
207    }
208
209
210    /**
211     * Returns the number of phones available.
212     * Returns 0 if none of voice, sms, data is not supported
213     * Returns 1 for Single standby mode (Single SIM functionality)
214     * Returns 2 for Dual standby mode.(Dual SIM functionality)
215     */
216    public int getPhoneCount() {
217        int phoneCount = 1;
218        switch (getMultiSimConfiguration()) {
219            case UNKNOWN:
220                // if voice or sms or data is supported, return 1 otherwise 0
221                if (isVoiceCapable() || isSmsCapable()) {
222                    phoneCount = 1;
223                } else {
224                    // todo: try to clean this up further by getting rid of the nested conditions
225                    if (mContext == null) {
226                        phoneCount = 1;
227                    } else {
228                        // check for data support
229                        ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(
230                                Context.CONNECTIVITY_SERVICE);
231                        if (cm == null) {
232                            phoneCount = 1;
233                        } else {
234                            if (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) {
235                                phoneCount = 1;
236                            } else {
237                                phoneCount = 0;
238                            }
239                        }
240                    }
241                }
242                break;
243            case DSDS:
244            case DSDA:
245                phoneCount = PhoneConstants.MAX_PHONE_COUNT_DUAL_SIM;
246                break;
247            case TSTS:
248                phoneCount = PhoneConstants.MAX_PHONE_COUNT_TRI_SIM;
249                break;
250        }
251        return phoneCount;
252    }
253
254    /** {@hide} */
255    public static TelephonyManager from(Context context) {
256        return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
257    }
258
259    /**
260     * Create a new TelephonyManager object pinned to the given subscription ID.
261     *
262     * @return a TelephonyManager that uses the given subId for all calls.
263     */
264    public TelephonyManager createForSubscriptionId(int subId) {
265      // Don't reuse any TelephonyManager objects.
266      return new TelephonyManager(mContext, subId);
267    }
268
269    /**
270     * Create a new TelephonyManager object pinned to the subscription ID associated with the given
271     * phone account.
272     *
273     * @return a TelephonyManager that uses the given phone account for all calls, or {@code null}
274     * if the phone account does not correspond to a valid subscription ID.
275     */
276    @Nullable
277    public TelephonyManager createForPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle) {
278        int subId = getSubIdForPhoneAccountHandle(phoneAccountHandle);
279        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
280            return null;
281        }
282        return new TelephonyManager(mContext, subId);
283    }
284
285    /** {@hide} */
286    public boolean isMultiSimEnabled() {
287        return (multiSimConfig.equals("dsds") || multiSimConfig.equals("dsda") ||
288            multiSimConfig.equals("tsts"));
289    }
290
291    //
292    // Broadcast Intent actions
293    //
294
295    /**
296     * Broadcast intent action indicating that the call state
297     * on the device has changed.
298     *
299     * <p>
300     * The {@link #EXTRA_STATE} extra indicates the new call state.
301     * If the new state is RINGING, a second extra
302     * {@link #EXTRA_INCOMING_NUMBER} provides the incoming phone number as
303     * a String.
304     *
305     * <p class="note">
306     * Requires the READ_PHONE_STATE permission.
307     *
308     * <p class="note">
309     * This was a {@link android.content.Context#sendStickyBroadcast sticky}
310     * broadcast in version 1.0, but it is no longer sticky.
311     * Instead, use {@link #getCallState} to synchronously query the current call state.
312     *
313     * @see #EXTRA_STATE
314     * @see #EXTRA_INCOMING_NUMBER
315     * @see #getCallState
316     */
317    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
318    public static final String ACTION_PHONE_STATE_CHANGED =
319            "android.intent.action.PHONE_STATE";
320
321    /**
322     * The Phone app sends this intent when a user opts to respond-via-message during an incoming
323     * call. By default, the device's default SMS app consumes this message and sends a text message
324     * to the caller. A third party app can also provide this functionality by consuming this Intent
325     * with a {@link android.app.Service} and sending the message using its own messaging system.
326     * <p>The intent contains a URI (available from {@link android.content.Intent#getData})
327     * describing the recipient, using either the {@code sms:}, {@code smsto:}, {@code mms:},
328     * or {@code mmsto:} URI schema. Each of these URI schema carry the recipient information the
329     * same way: the path part of the URI contains the recipient's phone number or a comma-separated
330     * set of phone numbers if there are multiple recipients. For example, {@code
331     * smsto:2065551234}.</p>
332     *
333     * <p>The intent may also contain extras for the message text (in {@link
334     * android.content.Intent#EXTRA_TEXT}) and a message subject
335     * (in {@link android.content.Intent#EXTRA_SUBJECT}).</p>
336     *
337     * <p class="note"><strong>Note:</strong>
338     * The intent-filter that consumes this Intent needs to be in a {@link android.app.Service}
339     * that requires the
340     * permission {@link android.Manifest.permission#SEND_RESPOND_VIA_MESSAGE}.</p>
341     * <p>For example, the service that receives this intent can be declared in the manifest file
342     * with an intent filter like this:</p>
343     * <pre>
344     * &lt;!-- Service that delivers SMS messages received from the phone "quick response" -->
345     * &lt;service android:name=".HeadlessSmsSendService"
346     *          android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
347     *          android:exported="true" >
348     *   &lt;intent-filter>
349     *     &lt;action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
350     *     &lt;category android:name="android.intent.category.DEFAULT" />
351     *     &lt;data android:scheme="sms" />
352     *     &lt;data android:scheme="smsto" />
353     *     &lt;data android:scheme="mms" />
354     *     &lt;data android:scheme="mmsto" />
355     *   &lt;/intent-filter>
356     * &lt;/service></pre>
357     * <p>
358     * Output: nothing.
359     */
360    @SdkConstant(SdkConstantType.SERVICE_ACTION)
361    public static final String ACTION_RESPOND_VIA_MESSAGE =
362            "android.intent.action.RESPOND_VIA_MESSAGE";
363
364    /**
365     * The emergency dialer may choose to present activities with intent filters for this
366     * action as emergency assistance buttons that launch the activity when clicked.
367     *
368     * @hide
369     */
370    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
371    public static final String ACTION_EMERGENCY_ASSISTANCE =
372            "android.telephony.action.EMERGENCY_ASSISTANCE";
373
374    /**
375     * Open the voicemail settings activity to make changes to voicemail configuration.
376     */
377    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
378    public static final String ACTION_CONFIGURE_VOICEMAIL =
379            "android.telephony.action.CONFIGURE_VOICEMAIL";
380
381    /**
382     * @hide
383     */
384    public static final boolean EMERGENCY_ASSISTANCE_ENABLED = true;
385
386    /**
387     * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
388     * for a String containing the new call state.
389     *
390     * @see #EXTRA_STATE_IDLE
391     * @see #EXTRA_STATE_RINGING
392     * @see #EXTRA_STATE_OFFHOOK
393     *
394     * <p class="note">
395     * Retrieve with
396     * {@link android.content.Intent#getStringExtra(String)}.
397     */
398    public static final String EXTRA_STATE = PhoneConstants.STATE_KEY;
399
400    /**
401     * Value used with {@link #EXTRA_STATE} corresponding to
402     * {@link #CALL_STATE_IDLE}.
403     */
404    public static final String EXTRA_STATE_IDLE = PhoneConstants.State.IDLE.toString();
405
406    /**
407     * Value used with {@link #EXTRA_STATE} corresponding to
408     * {@link #CALL_STATE_RINGING}.
409     */
410    public static final String EXTRA_STATE_RINGING = PhoneConstants.State.RINGING.toString();
411
412    /**
413     * Value used with {@link #EXTRA_STATE} corresponding to
414     * {@link #CALL_STATE_OFFHOOK}.
415     */
416    public static final String EXTRA_STATE_OFFHOOK = PhoneConstants.State.OFFHOOK.toString();
417
418    /**
419     * The lookup key used with the {@link #ACTION_PHONE_STATE_CHANGED} broadcast
420     * for a String containing the incoming phone number.
421     * Only valid when the new call state is RINGING.
422     *
423     * <p class="note">
424     * Retrieve with
425     * {@link android.content.Intent#getStringExtra(String)}.
426     */
427    public static final String EXTRA_INCOMING_NUMBER = "incoming_number";
428
429    /**
430     * Broadcast intent action indicating that a precise call state
431     * (cellular) on the device has changed.
432     *
433     * <p>
434     * The {@link #EXTRA_RINGING_CALL_STATE} extra indicates the ringing call state.
435     * The {@link #EXTRA_FOREGROUND_CALL_STATE} extra indicates the foreground call state.
436     * The {@link #EXTRA_BACKGROUND_CALL_STATE} extra indicates the background call state.
437     * The {@link #EXTRA_DISCONNECT_CAUSE} extra indicates the disconnect cause.
438     * The {@link #EXTRA_PRECISE_DISCONNECT_CAUSE} extra indicates the precise disconnect cause.
439     *
440     * <p class="note">
441     * Requires the READ_PRECISE_PHONE_STATE permission.
442     *
443     * @see #EXTRA_RINGING_CALL_STATE
444     * @see #EXTRA_FOREGROUND_CALL_STATE
445     * @see #EXTRA_BACKGROUND_CALL_STATE
446     * @see #EXTRA_DISCONNECT_CAUSE
447     * @see #EXTRA_PRECISE_DISCONNECT_CAUSE
448     *
449     * <p class="note">
450     * Requires the READ_PRECISE_PHONE_STATE permission.
451     *
452     * @hide
453     */
454    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
455    public static final String ACTION_PRECISE_CALL_STATE_CHANGED =
456            "android.intent.action.PRECISE_CALL_STATE";
457
458    /**
459     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
460     * for an integer containing the state of the current ringing call.
461     *
462     * @see PreciseCallState#PRECISE_CALL_STATE_NOT_VALID
463     * @see PreciseCallState#PRECISE_CALL_STATE_IDLE
464     * @see PreciseCallState#PRECISE_CALL_STATE_ACTIVE
465     * @see PreciseCallState#PRECISE_CALL_STATE_HOLDING
466     * @see PreciseCallState#PRECISE_CALL_STATE_DIALING
467     * @see PreciseCallState#PRECISE_CALL_STATE_ALERTING
468     * @see PreciseCallState#PRECISE_CALL_STATE_INCOMING
469     * @see PreciseCallState#PRECISE_CALL_STATE_WAITING
470     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTED
471     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTING
472     *
473     * <p class="note">
474     * Retrieve with
475     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
476     *
477     * @hide
478     */
479    public static final String EXTRA_RINGING_CALL_STATE = "ringing_state";
480
481    /**
482     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
483     * for an integer containing the state of the current foreground call.
484     *
485     * @see PreciseCallState#PRECISE_CALL_STATE_NOT_VALID
486     * @see PreciseCallState#PRECISE_CALL_STATE_IDLE
487     * @see PreciseCallState#PRECISE_CALL_STATE_ACTIVE
488     * @see PreciseCallState#PRECISE_CALL_STATE_HOLDING
489     * @see PreciseCallState#PRECISE_CALL_STATE_DIALING
490     * @see PreciseCallState#PRECISE_CALL_STATE_ALERTING
491     * @see PreciseCallState#PRECISE_CALL_STATE_INCOMING
492     * @see PreciseCallState#PRECISE_CALL_STATE_WAITING
493     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTED
494     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTING
495     *
496     * <p class="note">
497     * Retrieve with
498     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
499     *
500     * @hide
501     */
502    public static final String EXTRA_FOREGROUND_CALL_STATE = "foreground_state";
503
504    /**
505     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
506     * for an integer containing the state of the current background call.
507     *
508     * @see PreciseCallState#PRECISE_CALL_STATE_NOT_VALID
509     * @see PreciseCallState#PRECISE_CALL_STATE_IDLE
510     * @see PreciseCallState#PRECISE_CALL_STATE_ACTIVE
511     * @see PreciseCallState#PRECISE_CALL_STATE_HOLDING
512     * @see PreciseCallState#PRECISE_CALL_STATE_DIALING
513     * @see PreciseCallState#PRECISE_CALL_STATE_ALERTING
514     * @see PreciseCallState#PRECISE_CALL_STATE_INCOMING
515     * @see PreciseCallState#PRECISE_CALL_STATE_WAITING
516     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTED
517     * @see PreciseCallState#PRECISE_CALL_STATE_DISCONNECTING
518     *
519     * <p class="note">
520     * Retrieve with
521     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
522     *
523     * @hide
524     */
525    public static final String EXTRA_BACKGROUND_CALL_STATE = "background_state";
526
527    /**
528     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
529     * for an integer containing the disconnect cause.
530     *
531     * @see DisconnectCause
532     *
533     * <p class="note">
534     * Retrieve with
535     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
536     *
537     * @hide
538     */
539    public static final String EXTRA_DISCONNECT_CAUSE = "disconnect_cause";
540
541    /**
542     * The lookup key used with the {@link #ACTION_PRECISE_CALL_STATE_CHANGED} broadcast
543     * for an integer containing the disconnect cause provided by the RIL.
544     *
545     * @see PreciseDisconnectCause
546     *
547     * <p class="note">
548     * Retrieve with
549     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
550     *
551     * @hide
552     */
553    public static final String EXTRA_PRECISE_DISCONNECT_CAUSE = "precise_disconnect_cause";
554
555    /**
556     * Broadcast intent action indicating a data connection has changed,
557     * providing precise information about the connection.
558     *
559     * <p>
560     * The {@link #EXTRA_DATA_STATE} extra indicates the connection state.
561     * The {@link #EXTRA_DATA_NETWORK_TYPE} extra indicates the connection network type.
562     * The {@link #EXTRA_DATA_APN_TYPE} extra indicates the APN type.
563     * The {@link #EXTRA_DATA_APN} extra indicates the APN.
564     * The {@link #EXTRA_DATA_CHANGE_REASON} extra indicates the connection change reason.
565     * The {@link #EXTRA_DATA_IFACE_PROPERTIES} extra indicates the connection interface.
566     * The {@link #EXTRA_DATA_FAILURE_CAUSE} extra indicates the connection fail cause.
567     *
568     * <p class="note">
569     * Requires the READ_PRECISE_PHONE_STATE permission.
570     *
571     * @see #EXTRA_DATA_STATE
572     * @see #EXTRA_DATA_NETWORK_TYPE
573     * @see #EXTRA_DATA_APN_TYPE
574     * @see #EXTRA_DATA_APN
575     * @see #EXTRA_DATA_CHANGE_REASON
576     * @see #EXTRA_DATA_IFACE
577     * @see #EXTRA_DATA_FAILURE_CAUSE
578     * @hide
579     */
580    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
581    public static final String ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED =
582            "android.intent.action.PRECISE_DATA_CONNECTION_STATE_CHANGED";
583
584    /**
585     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
586     * for an integer containing the state of the current data connection.
587     *
588     * @see TelephonyManager#DATA_UNKNOWN
589     * @see TelephonyManager#DATA_DISCONNECTED
590     * @see TelephonyManager#DATA_CONNECTING
591     * @see TelephonyManager#DATA_CONNECTED
592     * @see TelephonyManager#DATA_SUSPENDED
593     *
594     * <p class="note">
595     * Retrieve with
596     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
597     *
598     * @hide
599     */
600    public static final String EXTRA_DATA_STATE = PhoneConstants.STATE_KEY;
601
602    /**
603     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
604     * for an integer containing the network type.
605     *
606     * @see TelephonyManager#NETWORK_TYPE_UNKNOWN
607     * @see TelephonyManager#NETWORK_TYPE_GPRS
608     * @see TelephonyManager#NETWORK_TYPE_EDGE
609     * @see TelephonyManager#NETWORK_TYPE_UMTS
610     * @see TelephonyManager#NETWORK_TYPE_CDMA
611     * @see TelephonyManager#NETWORK_TYPE_EVDO_0
612     * @see TelephonyManager#NETWORK_TYPE_EVDO_A
613     * @see TelephonyManager#NETWORK_TYPE_1xRTT
614     * @see TelephonyManager#NETWORK_TYPE_HSDPA
615     * @see TelephonyManager#NETWORK_TYPE_HSUPA
616     * @see TelephonyManager#NETWORK_TYPE_HSPA
617     * @see TelephonyManager#NETWORK_TYPE_IDEN
618     * @see TelephonyManager#NETWORK_TYPE_EVDO_B
619     * @see TelephonyManager#NETWORK_TYPE_LTE
620     * @see TelephonyManager#NETWORK_TYPE_EHRPD
621     * @see TelephonyManager#NETWORK_TYPE_HSPAP
622     *
623     * <p class="note">
624     * Retrieve with
625     * {@link android.content.Intent#getIntExtra(String name, int defaultValue)}.
626     *
627     * @hide
628     */
629    public static final String EXTRA_DATA_NETWORK_TYPE = PhoneConstants.DATA_NETWORK_TYPE_KEY;
630
631    /**
632     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
633     * for an String containing the data APN type.
634     *
635     * <p class="note">
636     * Retrieve with
637     * {@link android.content.Intent#getStringExtra(String name)}.
638     *
639     * @hide
640     */
641    public static final String EXTRA_DATA_APN_TYPE = PhoneConstants.DATA_APN_TYPE_KEY;
642
643    /**
644     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
645     * for an String containing the data APN.
646     *
647     * <p class="note">
648     * Retrieve with
649     * {@link android.content.Intent#getStringExtra(String name)}.
650     *
651     * @hide
652     */
653    public static final String EXTRA_DATA_APN = PhoneConstants.DATA_APN_KEY;
654
655    /**
656     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
657     * for an String representation of the change reason.
658     *
659     * <p class="note">
660     * Retrieve with
661     * {@link android.content.Intent#getStringExtra(String name)}.
662     *
663     * @hide
664     */
665    public static final String EXTRA_DATA_CHANGE_REASON = PhoneConstants.STATE_CHANGE_REASON_KEY;
666
667    /**
668     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
669     * for an String representation of the data interface.
670     *
671     * <p class="note">
672     * Retrieve with
673     * {@link android.content.Intent#getParcelableExtra(String name)}.
674     *
675     * @hide
676     */
677    public static final String EXTRA_DATA_LINK_PROPERTIES_KEY = PhoneConstants.DATA_LINK_PROPERTIES_KEY;
678
679    /**
680     * The lookup key used with the {@link #ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED} broadcast
681     * for the data connection fail cause.
682     *
683     * <p class="note">
684     * Retrieve with
685     * {@link android.content.Intent#getStringExtra(String name)}.
686     *
687     * @hide
688     */
689    public static final String EXTRA_DATA_FAILURE_CAUSE = PhoneConstants.DATA_FAILURE_CAUSE_KEY;
690
691    /**
692     * Broadcast intent action for letting the default dialer to know to show voicemail
693     * notification.
694     *
695     * <p>
696     * The {@link #EXTRA_NOTIFICATION_COUNT} extra indicates the total numbers of unheard
697     * voicemails.
698     * The {@link #EXTRA_VOICEMAIL_NUMBER} extra indicates the voicemail number if available.
699     * The {@link #EXTRA_CALL_VOICEMAIL_INTENT} extra is a {@link android.app.PendingIntent} that
700     * will call the voicemail number when sent. This extra will be empty if the voicemail number
701     * is not set, and {@link #EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT} will be set instead.
702     * The {@link #EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT} extra is a
703     * {@link android.app.PendingIntent} that will launch the voicemail settings. This extra is only
704     * available when the voicemail number is not set.
705     *
706     * @see #EXTRA_NOTIFICATION_COUNT
707     * @see #EXTRA_VOICEMAIL_NUMBER
708     * @see #EXTRA_CALL_VOICEMAIL_INTENT
709     * @see #EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
710     */
711    public static final String ACTION_SHOW_VOICEMAIL_NOTIFICATION =
712            "android.telephony.action.SHOW_VOICEMAIL_NOTIFICATION";
713
714    /**
715     * The number of voice messages associated with the notification.
716     */
717    public static final String EXTRA_NOTIFICATION_COUNT =
718            "android.telephony.extra.NOTIFICATION_COUNT";
719
720    /**
721     * The voicemail number.
722     */
723    public static final String EXTRA_VOICEMAIL_NUMBER =
724            "android.telephony.extra.VOICEMAIL_NUMBER";
725
726    /**
727     * The intent to call voicemail.
728     */
729    public static final String EXTRA_CALL_VOICEMAIL_INTENT =
730            "android.telephony.extra.CALL_VOICEMAIL_INTENT";
731
732    /**
733     * The intent to launch voicemail settings.
734     */
735    public static final String EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT =
736            "android.telephony.extra.LAUNCH_VOICEMAIL_SETTINGS_INTENT";
737
738    /**
739     * {@link android.telecom.Connection} event used to indicate that an IMS call has be
740     * successfully handed over from WIFI to LTE.
741     * <p>
742     * Sent via {@link android.telecom.Connection#sendConnectionEvent(String, Bundle)}.
743     * The {@link Bundle} parameter is expected to be null when this connection event is used.
744     * @hide
745     */
746    public static final String EVENT_HANDOVER_VIDEO_FROM_WIFI_TO_LTE =
747            "android.telephony.event.EVENT_HANDOVER_VIDEO_FROM_WIFI_TO_LTE";
748
749    /**
750     * {@link android.telecom.Connection} event used to indicate that an IMS call failed to be
751     * handed over from LTE to WIFI.
752     * <p>
753     * Sent via {@link android.telecom.Connection#sendConnectionEvent(String, Bundle)}.
754     * The {@link Bundle} parameter is expected to be null when this connection event is used.
755     * @hide
756     */
757    public static final String EVENT_HANDOVER_TO_WIFI_FAILED =
758            "android.telephony.event.EVENT_HANDOVER_TO_WIFI_FAILED";
759
760    /**
761     * {@link android.telecom.Connection} event used to indicate that a video call was downgraded to
762     * audio because the data limit was reached.
763     * <p>
764     * Sent via {@link android.telecom.Connection#sendConnectionEvent(String, Bundle)}.
765     * The {@link Bundle} parameter is expected to be null when this connection event is used.
766     * @hide
767     */
768    public static final String EVENT_DOWNGRADE_DATA_LIMIT_REACHED =
769            "android.telephony.event.EVENT_DOWNGRADE_DATA_LIMIT_REACHED";
770
771    /**
772     * {@link android.telecom.Connection} event used to indicate that a video call was downgraded to
773     * audio because the data was disabled.
774     * <p>
775     * Sent via {@link android.telecom.Connection#sendConnectionEvent(String, Bundle)}.
776     * The {@link Bundle} parameter is expected to be null when this connection event is used.
777     * @hide
778     */
779    public static final String EVENT_DOWNGRADE_DATA_DISABLED =
780            "android.telephony.event.EVENT_DOWNGRADE_DATA_DISABLED";
781
782    /**
783     * {@link android.telecom.Connection} event used to indicate that the InCall UI should notify
784     * the user when an international call is placed while on WFC only.
785     * <p>
786     * Used when the carrier config value
787     * {@link CarrierConfigManager#KEY_NOTIFY_INTERNATIONAL_CALL_ON_WFC_BOOL} is true, the device
788     * is on WFC (VoLTE not available) and an international number is dialed.
789     * <p>
790     * Sent via {@link android.telecom.Connection#sendConnectionEvent(String, Bundle)}.
791     * The {@link Bundle} parameter is expected to be null when this connection event is used.
792     * @hide
793     */
794    public static final String EVENT_NOTIFY_INTERNATIONAL_CALL_ON_WFC =
795            "android.telephony.event.EVENT_NOTIFY_INTERNATIONAL_CALL_ON_WFC";
796
797    /**
798     * Response codes for sim activation. Activation completed successfully.
799     * @hide
800     */
801    @SystemApi
802    public static final int SIM_ACTIVATION_RESULT_COMPLETE = 0;
803    /**
804     * Response codes for sim activation. Activation not supported (device has no SIM).
805     * @hide
806     */
807    @SystemApi
808    public static final int SIM_ACTIVATION_RESULT_NOT_SUPPORTED = 1;
809    /**
810     * Response codes for sim activation. Activation is in progress.
811     * @hide
812     */
813    @SystemApi
814    public static final int SIM_ACTIVATION_RESULT_IN_PROGRESS = 2;
815    /**
816     * Response codes for sim activation. Activation failed to complete.
817     * @hide
818     */
819    @SystemApi
820    public static final int SIM_ACTIVATION_RESULT_FAILED = 3;
821    /**
822     * Response codes for sim activation. Activation canceled by user.
823     * @hide
824     */
825    @SystemApi
826    public static final int SIM_ACTIVATION_RESULT_CANCELED = 4;
827
828    /* Visual voicemail protocols */
829
830    /**
831     * The OMTP protocol.
832     */
833    public static final String VVM_TYPE_OMTP = "vvm_type_omtp";
834
835    /**
836     * A flavor of OMTP protocol with a different mobile originated (MO) format
837     */
838    public static final String VVM_TYPE_CVVM = "vvm_type_cvvm";
839
840    //
841    //
842    // Device Info
843    //
844    //
845
846    /**
847     * Returns the software version number for the device, for example,
848     * the IMEI/SV for GSM phones. Return null if the software version is
849     * not available.
850     *
851     * <p>Requires Permission:
852     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
853     */
854    public String getDeviceSoftwareVersion() {
855        return getDeviceSoftwareVersion(getDefaultSim());
856    }
857
858    /**
859     * Returns the software version number for the device, for example,
860     * the IMEI/SV for GSM phones. Return null if the software version is
861     * not available.
862     *
863     * <p>Requires Permission:
864     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
865     *
866     * @param slotId of which deviceID is returned
867     */
868    /** {@hide} */
869    public String getDeviceSoftwareVersion(int slotId) {
870        ITelephony telephony = getITelephony();
871        if (telephony == null) return null;
872
873        try {
874            return telephony.getDeviceSoftwareVersionForSlot(slotId, getOpPackageName());
875        } catch (RemoteException ex) {
876            return null;
877        } catch (NullPointerException ex) {
878            return null;
879        }
880    }
881
882    /**
883     * Returns the unique device ID, for example, the IMEI for GSM and the MEID
884     * or ESN for CDMA phones. Return null if device ID is not available.
885     *
886     * <p>Requires Permission:
887     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
888     */
889    public String getDeviceId() {
890        try {
891            ITelephony telephony = getITelephony();
892            if (telephony == null)
893                return null;
894            return telephony.getDeviceId(mContext.getOpPackageName());
895        } catch (RemoteException ex) {
896            return null;
897        } catch (NullPointerException ex) {
898            return null;
899        }
900    }
901
902    /**
903     * Returns the unique device ID of a subscription, for example, the IMEI for
904     * GSM and the MEID for CDMA phones. Return null if device ID is not available.
905     *
906     * <p>Requires Permission:
907     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
908     *
909     * @param slotId of which deviceID is returned
910     */
911    public String getDeviceId(int slotId) {
912        // FIXME this assumes phoneId == slotId
913        try {
914            IPhoneSubInfo info = getSubscriberInfo();
915            if (info == null)
916                return null;
917            return info.getDeviceIdForPhone(slotId, mContext.getOpPackageName());
918        } catch (RemoteException ex) {
919            return null;
920        } catch (NullPointerException ex) {
921            return null;
922        }
923    }
924
925    /**
926     * Returns the IMEI. Return null if IMEI is not available.
927     *
928     * <p>Requires Permission:
929     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
930     *
931     * @hide
932     */
933    @SystemApi
934    public String getImei() {
935        return getImei(getDefaultSim());
936    }
937
938    /**
939     * Returns the IMEI. Return null if IMEI is not available.
940     *
941     * <p>Requires Permission:
942     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
943     *
944     * @param slotId of which deviceID is returned
945     *
946     * @hide
947     */
948    @SystemApi
949    public String getImei(int slotId) {
950        ITelephony telephony = getITelephony();
951        if (telephony == null) return null;
952
953        try {
954            return telephony.getImeiForSlot(slotId, getOpPackageName());
955        } catch (RemoteException ex) {
956            return null;
957        } catch (NullPointerException ex) {
958            return null;
959        }
960    }
961
962    /**
963     * Returns the NAI. Return null if NAI is not available.
964     *
965     */
966    /** {@hide}*/
967    public String getNai() {
968        return getNai(getDefaultSim());
969    }
970
971    /**
972     * Returns the NAI. Return null if NAI is not available.
973     *
974     *  @param slotId of which Nai is returned
975     */
976    /** {@hide}*/
977    public String getNai(int slotId) {
978        int[] subId = SubscriptionManager.getSubId(slotId);
979        try {
980            IPhoneSubInfo info = getSubscriberInfo();
981            if (info == null)
982                return null;
983            String nai = info.getNaiForSubscriber(subId[0], mContext.getOpPackageName());
984            if (Log.isLoggable(TAG, Log.VERBOSE)) {
985                Rlog.v(TAG, "Nai = " + nai);
986            }
987            return nai;
988        } catch (RemoteException ex) {
989            return null;
990        } catch (NullPointerException ex) {
991            return null;
992        }
993    }
994
995    /**
996     * Returns the current location of the device.
997     *<p>
998     * If there is only one radio in the device and that radio has an LTE connection,
999     * this method will return null. The implementation must not to try add LTE
1000     * identifiers into the existing cdma/gsm classes.
1001     *<p>
1002     * In the future this call will be deprecated.
1003     *<p>
1004     * @return Current location of the device or null if not available.
1005     *
1006     * <p>Requires Permission:
1007     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} or
1008     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION}.
1009     */
1010    public CellLocation getCellLocation() {
1011        try {
1012            ITelephony telephony = getITelephony();
1013            if (telephony == null) {
1014                Rlog.d(TAG, "getCellLocation returning null because telephony is null");
1015                return null;
1016            }
1017            Bundle bundle = telephony.getCellLocation(mContext.getOpPackageName());
1018            if (bundle.isEmpty()) {
1019                Rlog.d(TAG, "getCellLocation returning null because bundle is empty");
1020                return null;
1021            }
1022            CellLocation cl = CellLocation.newFromBundle(bundle);
1023            if (cl.isEmpty()) {
1024                Rlog.d(TAG, "getCellLocation returning null because CellLocation is empty");
1025                return null;
1026            }
1027            return cl;
1028        } catch (RemoteException ex) {
1029            Rlog.d(TAG, "getCellLocation returning null due to RemoteException " + ex);
1030            return null;
1031        } catch (NullPointerException ex) {
1032            Rlog.d(TAG, "getCellLocation returning null due to NullPointerException " + ex);
1033            return null;
1034        }
1035    }
1036
1037    /**
1038     * Enables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
1039     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
1040     *
1041     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
1042     * CONTROL_LOCATION_UPDATES}
1043     *
1044     * @hide
1045     */
1046    public void enableLocationUpdates() {
1047        enableLocationUpdates(getSubId());
1048    }
1049
1050    /**
1051     * Enables location update notifications for a subscription.
1052     * {@link PhoneStateListener#onCellLocationChanged
1053     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
1054     *
1055     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
1056     * CONTROL_LOCATION_UPDATES}
1057     *
1058     * @param subId for which the location updates are enabled
1059     * @hide
1060     */
1061    public void enableLocationUpdates(int subId) {
1062        try {
1063            ITelephony telephony = getITelephony();
1064            if (telephony != null)
1065                telephony.enableLocationUpdatesForSubscriber(subId);
1066        } catch (RemoteException ex) {
1067        } catch (NullPointerException ex) {
1068        }
1069    }
1070
1071    /**
1072     * Disables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
1073     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
1074     *
1075     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
1076     * CONTROL_LOCATION_UPDATES}
1077     *
1078     * @hide
1079     */
1080    public void disableLocationUpdates() {
1081        disableLocationUpdates(getSubId());
1082    }
1083
1084    /** @hide */
1085    public void disableLocationUpdates(int subId) {
1086        try {
1087            ITelephony telephony = getITelephony();
1088            if (telephony != null)
1089                telephony.disableLocationUpdatesForSubscriber(subId);
1090        } catch (RemoteException ex) {
1091        } catch (NullPointerException ex) {
1092        }
1093    }
1094
1095    /**
1096     * Returns the neighboring cell information of the device.
1097     *
1098     * @return List of NeighboringCellInfo or null if info unavailable.
1099     *
1100     * <p>Requires Permission:
1101     * (@link android.Manifest.permission#ACCESS_COARSE_UPDATES}
1102     *
1103     * @deprecated Use (@link getAllCellInfo} which returns a superset of the information
1104     *             from NeighboringCellInfo.
1105     */
1106    @Deprecated
1107    public List<NeighboringCellInfo> getNeighboringCellInfo() {
1108        try {
1109            ITelephony telephony = getITelephony();
1110            if (telephony == null)
1111                return null;
1112            return telephony.getNeighboringCellInfo(mContext.getOpPackageName());
1113        } catch (RemoteException ex) {
1114            return null;
1115        } catch (NullPointerException ex) {
1116            return null;
1117        }
1118    }
1119
1120    /** No phone radio. */
1121    public static final int PHONE_TYPE_NONE = PhoneConstants.PHONE_TYPE_NONE;
1122    /** Phone radio is GSM. */
1123    public static final int PHONE_TYPE_GSM = PhoneConstants.PHONE_TYPE_GSM;
1124    /** Phone radio is CDMA. */
1125    public static final int PHONE_TYPE_CDMA = PhoneConstants.PHONE_TYPE_CDMA;
1126    /** Phone is via SIP. */
1127    public static final int PHONE_TYPE_SIP = PhoneConstants.PHONE_TYPE_SIP;
1128
1129    /**
1130     * Returns the current phone type.
1131     * TODO: This is a last minute change and hence hidden.
1132     *
1133     * @see #PHONE_TYPE_NONE
1134     * @see #PHONE_TYPE_GSM
1135     * @see #PHONE_TYPE_CDMA
1136     * @see #PHONE_TYPE_SIP
1137     *
1138     * {@hide}
1139     */
1140    @SystemApi
1141    public int getCurrentPhoneType() {
1142        return getCurrentPhoneType(getSubId());
1143    }
1144
1145    /**
1146     * Returns a constant indicating the device phone type for a subscription.
1147     *
1148     * @see #PHONE_TYPE_NONE
1149     * @see #PHONE_TYPE_GSM
1150     * @see #PHONE_TYPE_CDMA
1151     *
1152     * @param subId for which phone type is returned
1153     * @hide
1154     */
1155    @SystemApi
1156    public int getCurrentPhoneType(int subId) {
1157        int phoneId;
1158        if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
1159            // if we don't have any sims, we don't have subscriptions, but we
1160            // still may want to know what type of phone we've got.
1161            phoneId = 0;
1162        } else {
1163            phoneId = SubscriptionManager.getPhoneId(subId);
1164        }
1165
1166        return getCurrentPhoneTypeForSlot(phoneId);
1167    }
1168
1169    /**
1170     * See getCurrentPhoneType.
1171     *
1172     * @hide
1173     */
1174    public int getCurrentPhoneTypeForSlot(int slotId) {
1175        try{
1176            ITelephony telephony = getITelephony();
1177            if (telephony != null) {
1178                return telephony.getActivePhoneTypeForSlot(slotId);
1179            } else {
1180                // This can happen when the ITelephony interface is not up yet.
1181                return getPhoneTypeFromProperty(slotId);
1182            }
1183        } catch (RemoteException ex) {
1184            // This shouldn't happen in the normal case, as a backup we
1185            // read from the system property.
1186            return getPhoneTypeFromProperty(slotId);
1187        } catch (NullPointerException ex) {
1188            // This shouldn't happen in the normal case, as a backup we
1189            // read from the system property.
1190            return getPhoneTypeFromProperty(slotId);
1191        }
1192    }
1193
1194    /**
1195     * Returns a constant indicating the device phone type.  This
1196     * indicates the type of radio used to transmit voice calls.
1197     *
1198     * @see #PHONE_TYPE_NONE
1199     * @see #PHONE_TYPE_GSM
1200     * @see #PHONE_TYPE_CDMA
1201     * @see #PHONE_TYPE_SIP
1202     */
1203    public int getPhoneType() {
1204        if (!isVoiceCapable()) {
1205            return PHONE_TYPE_NONE;
1206        }
1207        return getCurrentPhoneType();
1208    }
1209
1210    private int getPhoneTypeFromProperty() {
1211        return getPhoneTypeFromProperty(getDefaultPhone());
1212    }
1213
1214    /** {@hide} */
1215    private int getPhoneTypeFromProperty(int phoneId) {
1216        String type = getTelephonyProperty(phoneId,
1217                TelephonyProperties.CURRENT_ACTIVE_PHONE, null);
1218        if (type == null || type.isEmpty()) {
1219            return getPhoneTypeFromNetworkType(phoneId);
1220        }
1221        return Integer.parseInt(type);
1222    }
1223
1224    private int getPhoneTypeFromNetworkType() {
1225        return getPhoneTypeFromNetworkType(getDefaultPhone());
1226    }
1227
1228    /** {@hide} */
1229    private int getPhoneTypeFromNetworkType(int phoneId) {
1230        // When the system property CURRENT_ACTIVE_PHONE, has not been set,
1231        // use the system property for default network type.
1232        // This is a fail safe, and can only happen at first boot.
1233        String mode = getTelephonyProperty(phoneId, "ro.telephony.default_network", null);
1234        if (mode != null && !mode.isEmpty()) {
1235            return TelephonyManager.getPhoneType(Integer.parseInt(mode));
1236        }
1237        return TelephonyManager.PHONE_TYPE_NONE;
1238    }
1239
1240    /**
1241     * This function returns the type of the phone, depending
1242     * on the network mode.
1243     *
1244     * @param networkMode
1245     * @return Phone Type
1246     *
1247     * @hide
1248     */
1249    public static int getPhoneType(int networkMode) {
1250        switch(networkMode) {
1251        case RILConstants.NETWORK_MODE_CDMA:
1252        case RILConstants.NETWORK_MODE_CDMA_NO_EVDO:
1253        case RILConstants.NETWORK_MODE_EVDO_NO_CDMA:
1254            return PhoneConstants.PHONE_TYPE_CDMA;
1255
1256        case RILConstants.NETWORK_MODE_WCDMA_PREF:
1257        case RILConstants.NETWORK_MODE_GSM_ONLY:
1258        case RILConstants.NETWORK_MODE_WCDMA_ONLY:
1259        case RILConstants.NETWORK_MODE_GSM_UMTS:
1260        case RILConstants.NETWORK_MODE_LTE_GSM_WCDMA:
1261        case RILConstants.NETWORK_MODE_LTE_WCDMA:
1262        case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
1263        case RILConstants.NETWORK_MODE_TDSCDMA_ONLY:
1264        case RILConstants.NETWORK_MODE_TDSCDMA_WCDMA:
1265        case RILConstants.NETWORK_MODE_LTE_TDSCDMA:
1266        case RILConstants.NETWORK_MODE_TDSCDMA_GSM:
1267        case RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM:
1268        case RILConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA:
1269        case RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA:
1270        case RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA:
1271        case RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
1272            return PhoneConstants.PHONE_TYPE_GSM;
1273
1274        // Use CDMA Phone for the global mode including CDMA
1275        case RILConstants.NETWORK_MODE_GLOBAL:
1276        case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO:
1277        case RILConstants.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
1278            return PhoneConstants.PHONE_TYPE_CDMA;
1279
1280        case RILConstants.NETWORK_MODE_LTE_ONLY:
1281            if (getLteOnCdmaModeStatic() == PhoneConstants.LTE_ON_CDMA_TRUE) {
1282                return PhoneConstants.PHONE_TYPE_CDMA;
1283            } else {
1284                return PhoneConstants.PHONE_TYPE_GSM;
1285            }
1286        default:
1287            return PhoneConstants.PHONE_TYPE_GSM;
1288        }
1289    }
1290
1291    /**
1292     * The contents of the /proc/cmdline file
1293     */
1294    private static String getProcCmdLine()
1295    {
1296        String cmdline = "";
1297        FileInputStream is = null;
1298        try {
1299            is = new FileInputStream("/proc/cmdline");
1300            byte [] buffer = new byte[2048];
1301            int count = is.read(buffer);
1302            if (count > 0) {
1303                cmdline = new String(buffer, 0, count);
1304            }
1305        } catch (IOException e) {
1306            Rlog.d(TAG, "No /proc/cmdline exception=" + e);
1307        } finally {
1308            if (is != null) {
1309                try {
1310                    is.close();
1311                } catch (IOException e) {
1312                }
1313            }
1314        }
1315        Rlog.d(TAG, "/proc/cmdline=" + cmdline);
1316        return cmdline;
1317    }
1318
1319    /** Kernel command line */
1320    private static final String sKernelCmdLine = getProcCmdLine();
1321
1322    /** Pattern for selecting the product type from the kernel command line */
1323    private static final Pattern sProductTypePattern =
1324        Pattern.compile("\\sproduct_type\\s*=\\s*(\\w+)");
1325
1326    /** The ProductType used for LTE on CDMA devices */
1327    private static final String sLteOnCdmaProductType =
1328        SystemProperties.get(TelephonyProperties.PROPERTY_LTE_ON_CDMA_PRODUCT_TYPE, "");
1329
1330    /**
1331     * Return if the current radio is LTE on CDMA. This
1332     * is a tri-state return value as for a period of time
1333     * the mode may be unknown.
1334     *
1335     * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE}
1336     * or {@link PhoneConstants#LTE_ON_CDMA_TRUE}
1337     *
1338     * @hide
1339     */
1340    public static int getLteOnCdmaModeStatic() {
1341        int retVal;
1342        int curVal;
1343        String productType = "";
1344
1345        curVal = SystemProperties.getInt(TelephonyProperties.PROPERTY_LTE_ON_CDMA_DEVICE,
1346                    PhoneConstants.LTE_ON_CDMA_UNKNOWN);
1347        retVal = curVal;
1348        if (retVal == PhoneConstants.LTE_ON_CDMA_UNKNOWN) {
1349            Matcher matcher = sProductTypePattern.matcher(sKernelCmdLine);
1350            if (matcher.find()) {
1351                productType = matcher.group(1);
1352                if (sLteOnCdmaProductType.equals(productType)) {
1353                    retVal = PhoneConstants.LTE_ON_CDMA_TRUE;
1354                } else {
1355                    retVal = PhoneConstants.LTE_ON_CDMA_FALSE;
1356                }
1357            } else {
1358                retVal = PhoneConstants.LTE_ON_CDMA_FALSE;
1359            }
1360        }
1361
1362        Rlog.d(TAG, "getLteOnCdmaMode=" + retVal + " curVal=" + curVal +
1363                " product_type='" + productType +
1364                "' lteOnCdmaProductType='" + sLteOnCdmaProductType + "'");
1365        return retVal;
1366    }
1367
1368    //
1369    //
1370    // Current Network
1371    //
1372    //
1373
1374    /**
1375     * Returns the alphabetic name of current registered operator.
1376     * <p>
1377     * Availability: Only when user is registered to a network. Result may be
1378     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1379     * on a CDMA network).
1380     */
1381    public String getNetworkOperatorName() {
1382        return getNetworkOperatorName(getSubId());
1383    }
1384
1385    /**
1386     * Returns the alphabetic name of current registered operator
1387     * for a particular subscription.
1388     * <p>
1389     * Availability: Only when user is registered to a network. Result may be
1390     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1391     * on a CDMA network).
1392     * @param subId
1393     * @hide
1394     */
1395    public String getNetworkOperatorName(int subId) {
1396        int phoneId = SubscriptionManager.getPhoneId(subId);
1397        return getTelephonyProperty(phoneId, TelephonyProperties.PROPERTY_OPERATOR_ALPHA, "");
1398    }
1399
1400    /**
1401     * Returns the numeric name (MCC+MNC) of current registered operator.
1402     * <p>
1403     * Availability: Only when user is registered to a network. Result may be
1404     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1405     * on a CDMA network).
1406     */
1407    public String getNetworkOperator() {
1408        return getNetworkOperatorForPhone(getDefaultPhone());
1409    }
1410
1411    /**
1412     * Returns the numeric name (MCC+MNC) of current registered operator
1413     * for a particular subscription.
1414     * <p>
1415     * Availability: Only when user is registered to a network. Result may be
1416     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1417     * on a CDMA network).
1418     *
1419     * @param subId
1420     * @hide
1421     */
1422    public String getNetworkOperator(int subId) {
1423        int phoneId = SubscriptionManager.getPhoneId(subId);
1424        return getNetworkOperatorForPhone(phoneId);
1425     }
1426
1427    /**
1428     * Returns the numeric name (MCC+MNC) of current registered operator
1429     * for a particular subscription.
1430     * <p>
1431     * Availability: Only when user is registered to a network. Result may be
1432     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1433     * on a CDMA network).
1434     *
1435     * @param phoneId
1436     * @hide
1437     **/
1438    public String getNetworkOperatorForPhone(int phoneId) {
1439        return getTelephonyProperty(phoneId, TelephonyProperties.PROPERTY_OPERATOR_NUMERIC, "");
1440     }
1441
1442
1443    /**
1444     * Returns the network specifier of the subscription ID pinned to the TelephonyManager.
1445     *
1446     * @see android.net.NetworkRequest.Builder#setNetworkSpecifier(String)
1447     * @see #createForSubscriptionId(int)
1448     * @see #createForPhoneAccountHandle(PhoneAccountHandle)
1449     */
1450    public String getNetworkSpecifier() {
1451        return String.valueOf(mSubId);
1452    }
1453
1454    /**
1455     * Returns the carrier config of the subscription ID pinned to the TelephonyManager.
1456     *
1457     * <p>Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE
1458     * READ_PHONE_STATE}
1459     *
1460     * @see CarrierConfigManager#getConfigForSubId(int)
1461     * @see #createForSubscriptionId(int)
1462     * @see #createForPhoneAccountHandle(PhoneAccountHandle)
1463     */
1464    public PersistableBundle getCarrierConfig() {
1465        CarrierConfigManager carrierConfigManager = mContext
1466                .getSystemService(CarrierConfigManager.class);
1467        return carrierConfigManager.getConfigForSubId(mSubId);
1468    }
1469
1470    /**
1471     * Returns true if the device is considered roaming on the current
1472     * network, for GSM purposes.
1473     * <p>
1474     * Availability: Only when user registered to a network.
1475     */
1476    public boolean isNetworkRoaming() {
1477        return isNetworkRoaming(getSubId());
1478    }
1479
1480    /**
1481     * Returns true if the device is considered roaming on the current
1482     * network for a subscription.
1483     * <p>
1484     * Availability: Only when user registered to a network.
1485     *
1486     * @param subId
1487     * @hide
1488     */
1489    public boolean isNetworkRoaming(int subId) {
1490        int phoneId = SubscriptionManager.getPhoneId(subId);
1491        return Boolean.parseBoolean(getTelephonyProperty(phoneId,
1492                TelephonyProperties.PROPERTY_OPERATOR_ISROAMING, null));
1493    }
1494
1495    /**
1496     * Returns the ISO country code equivalent of the current registered
1497     * operator's MCC (Mobile Country Code).
1498     * <p>
1499     * Availability: Only when user is registered to a network. Result may be
1500     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1501     * on a CDMA network).
1502     */
1503    public String getNetworkCountryIso() {
1504        return getNetworkCountryIsoForPhone(getDefaultPhone());
1505    }
1506
1507    /**
1508     * Returns the ISO country code equivalent of the current registered
1509     * operator's MCC (Mobile Country Code) of a subscription.
1510     * <p>
1511     * Availability: Only when user is registered to a network. Result may be
1512     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1513     * on a CDMA network).
1514     *
1515     * @param subId for which Network CountryIso is returned
1516     * @hide
1517     */
1518    public String getNetworkCountryIso(int subId) {
1519        int phoneId = SubscriptionManager.getPhoneId(subId);
1520        return getNetworkCountryIsoForPhone(phoneId);
1521    }
1522
1523    /**
1524     * Returns the ISO country code equivalent of the current registered
1525     * operator's MCC (Mobile Country Code) of a subscription.
1526     * <p>
1527     * Availability: Only when user is registered to a network. Result may be
1528     * unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
1529     * on a CDMA network).
1530     *
1531     * @param phoneId for which Network CountryIso is returned
1532     */
1533    /** {@hide} */
1534    public String getNetworkCountryIsoForPhone(int phoneId) {
1535        return getTelephonyProperty(phoneId, TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY, "");
1536    }
1537
1538    /** Network type is unknown */
1539    public static final int NETWORK_TYPE_UNKNOWN = 0;
1540    /** Current network is GPRS */
1541    public static final int NETWORK_TYPE_GPRS = 1;
1542    /** Current network is EDGE */
1543    public static final int NETWORK_TYPE_EDGE = 2;
1544    /** Current network is UMTS */
1545    public static final int NETWORK_TYPE_UMTS = 3;
1546    /** Current network is CDMA: Either IS95A or IS95B*/
1547    public static final int NETWORK_TYPE_CDMA = 4;
1548    /** Current network is EVDO revision 0*/
1549    public static final int NETWORK_TYPE_EVDO_0 = 5;
1550    /** Current network is EVDO revision A*/
1551    public static final int NETWORK_TYPE_EVDO_A = 6;
1552    /** Current network is 1xRTT*/
1553    public static final int NETWORK_TYPE_1xRTT = 7;
1554    /** Current network is HSDPA */
1555    public static final int NETWORK_TYPE_HSDPA = 8;
1556    /** Current network is HSUPA */
1557    public static final int NETWORK_TYPE_HSUPA = 9;
1558    /** Current network is HSPA */
1559    public static final int NETWORK_TYPE_HSPA = 10;
1560    /** Current network is iDen */
1561    public static final int NETWORK_TYPE_IDEN = 11;
1562    /** Current network is EVDO revision B*/
1563    public static final int NETWORK_TYPE_EVDO_B = 12;
1564    /** Current network is LTE */
1565    public static final int NETWORK_TYPE_LTE = 13;
1566    /** Current network is eHRPD */
1567    public static final int NETWORK_TYPE_EHRPD = 14;
1568    /** Current network is HSPA+ */
1569    public static final int NETWORK_TYPE_HSPAP = 15;
1570    /** Current network is GSM */
1571    public static final int NETWORK_TYPE_GSM = 16;
1572    /** Current network is TD_SCDMA */
1573    public static final int NETWORK_TYPE_TD_SCDMA = 17;
1574    /** Current network is IWLAN */
1575    public static final int NETWORK_TYPE_IWLAN = 18;
1576    /** Current network is LTE_CA {@hide} */
1577    public static final int NETWORK_TYPE_LTE_CA = 19;
1578    /**
1579     * @return the NETWORK_TYPE_xxxx for current data connection.
1580     */
1581    public int getNetworkType() {
1582       try {
1583           ITelephony telephony = getITelephony();
1584           if (telephony != null) {
1585               return telephony.getNetworkType();
1586            } else {
1587                // This can happen when the ITelephony interface is not up yet.
1588                return NETWORK_TYPE_UNKNOWN;
1589            }
1590        } catch(RemoteException ex) {
1591            // This shouldn't happen in the normal case
1592            return NETWORK_TYPE_UNKNOWN;
1593        } catch (NullPointerException ex) {
1594            // This could happen before phone restarts due to crashing
1595            return NETWORK_TYPE_UNKNOWN;
1596        }
1597    }
1598
1599    /**
1600     * Returns a constant indicating the radio technology (network type)
1601     * currently in use on the device for a subscription.
1602     * @return the network type
1603     *
1604     * @param subId for which network type is returned
1605     *
1606     * @see #NETWORK_TYPE_UNKNOWN
1607     * @see #NETWORK_TYPE_GPRS
1608     * @see #NETWORK_TYPE_EDGE
1609     * @see #NETWORK_TYPE_UMTS
1610     * @see #NETWORK_TYPE_HSDPA
1611     * @see #NETWORK_TYPE_HSUPA
1612     * @see #NETWORK_TYPE_HSPA
1613     * @see #NETWORK_TYPE_CDMA
1614     * @see #NETWORK_TYPE_EVDO_0
1615     * @see #NETWORK_TYPE_EVDO_A
1616     * @see #NETWORK_TYPE_EVDO_B
1617     * @see #NETWORK_TYPE_1xRTT
1618     * @see #NETWORK_TYPE_IDEN
1619     * @see #NETWORK_TYPE_LTE
1620     * @see #NETWORK_TYPE_EHRPD
1621     * @see #NETWORK_TYPE_HSPAP
1622     *
1623     * <p>
1624     * Requires Permission:
1625     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1626     * @hide
1627     */
1628   public int getNetworkType(int subId) {
1629       try {
1630           ITelephony telephony = getITelephony();
1631           if (telephony != null) {
1632               return telephony.getNetworkTypeForSubscriber(subId, getOpPackageName());
1633           } else {
1634               // This can happen when the ITelephony interface is not up yet.
1635               return NETWORK_TYPE_UNKNOWN;
1636           }
1637       } catch(RemoteException ex) {
1638           // This shouldn't happen in the normal case
1639           return NETWORK_TYPE_UNKNOWN;
1640       } catch (NullPointerException ex) {
1641           // This could happen before phone restarts due to crashing
1642           return NETWORK_TYPE_UNKNOWN;
1643       }
1644   }
1645
1646    /**
1647     * Returns a constant indicating the radio technology (network type)
1648     * currently in use on the device for data transmission.
1649     * @return the network type
1650     *
1651     * @see #NETWORK_TYPE_UNKNOWN
1652     * @see #NETWORK_TYPE_GPRS
1653     * @see #NETWORK_TYPE_EDGE
1654     * @see #NETWORK_TYPE_UMTS
1655     * @see #NETWORK_TYPE_HSDPA
1656     * @see #NETWORK_TYPE_HSUPA
1657     * @see #NETWORK_TYPE_HSPA
1658     * @see #NETWORK_TYPE_CDMA
1659     * @see #NETWORK_TYPE_EVDO_0
1660     * @see #NETWORK_TYPE_EVDO_A
1661     * @see #NETWORK_TYPE_EVDO_B
1662     * @see #NETWORK_TYPE_1xRTT
1663     * @see #NETWORK_TYPE_IDEN
1664     * @see #NETWORK_TYPE_LTE
1665     * @see #NETWORK_TYPE_EHRPD
1666     * @see #NETWORK_TYPE_HSPAP
1667     *
1668     * <p>
1669     * Requires Permission:
1670     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1671     */
1672    public int getDataNetworkType() {
1673        return getDataNetworkType(getSubId());
1674    }
1675
1676    /**
1677     * Returns a constant indicating the radio technology (network type)
1678     * currently in use on the device for data transmission for a subscription
1679     * @return the network type
1680     *
1681     * @param subId for which network type is returned
1682     *
1683     * <p>
1684     * Requires Permission:
1685     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1686     * @hide
1687     */
1688    public int getDataNetworkType(int subId) {
1689        try{
1690            ITelephony telephony = getITelephony();
1691            if (telephony != null) {
1692                return telephony.getDataNetworkTypeForSubscriber(subId, getOpPackageName());
1693            } else {
1694                // This can happen when the ITelephony interface is not up yet.
1695                return NETWORK_TYPE_UNKNOWN;
1696            }
1697        } catch(RemoteException ex) {
1698            // This shouldn't happen in the normal case
1699            return NETWORK_TYPE_UNKNOWN;
1700        } catch (NullPointerException ex) {
1701            // This could happen before phone restarts due to crashing
1702            return NETWORK_TYPE_UNKNOWN;
1703        }
1704    }
1705
1706    /**
1707     * Returns the NETWORK_TYPE_xxxx for voice
1708     *
1709     * <p>
1710     * Requires Permission:
1711     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1712     */
1713    public int getVoiceNetworkType() {
1714        return getVoiceNetworkType(getSubId());
1715    }
1716
1717    /**
1718     * Returns the NETWORK_TYPE_xxxx for voice for a subId
1719     *
1720     * <p>
1721     * Requires Permission:
1722     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
1723     * @hide
1724     */
1725    public int getVoiceNetworkType(int subId) {
1726        try{
1727            ITelephony telephony = getITelephony();
1728            if (telephony != null) {
1729                return telephony.getVoiceNetworkTypeForSubscriber(subId, getOpPackageName());
1730            } else {
1731                // This can happen when the ITelephony interface is not up yet.
1732                return NETWORK_TYPE_UNKNOWN;
1733            }
1734        } catch(RemoteException ex) {
1735            // This shouldn't happen in the normal case
1736            return NETWORK_TYPE_UNKNOWN;
1737        } catch (NullPointerException ex) {
1738            // This could happen before phone restarts due to crashing
1739            return NETWORK_TYPE_UNKNOWN;
1740        }
1741    }
1742
1743    /**
1744     * Network Class Definitions.
1745     * Do not change this order, it is used for sorting during emergency calling in
1746     * {@link TelephonyConnectionService#getFirstPhoneForEmergencyCall()}. Any newer technologies
1747     * should be added after the current definitions.
1748     */
1749    /** Unknown network class. {@hide} */
1750    public static final int NETWORK_CLASS_UNKNOWN = 0;
1751    /** Class of broadly defined "2G" networks. {@hide} */
1752    public static final int NETWORK_CLASS_2_G = 1;
1753    /** Class of broadly defined "3G" networks. {@hide} */
1754    public static final int NETWORK_CLASS_3_G = 2;
1755    /** Class of broadly defined "4G" networks. {@hide} */
1756    public static final int NETWORK_CLASS_4_G = 3;
1757
1758    /**
1759     * Return general class of network type, such as "3G" or "4G". In cases
1760     * where classification is contentious, this method is conservative.
1761     *
1762     * @hide
1763     */
1764    public static int getNetworkClass(int networkType) {
1765        switch (networkType) {
1766            case NETWORK_TYPE_GPRS:
1767            case NETWORK_TYPE_GSM:
1768            case NETWORK_TYPE_EDGE:
1769            case NETWORK_TYPE_CDMA:
1770            case NETWORK_TYPE_1xRTT:
1771            case NETWORK_TYPE_IDEN:
1772                return NETWORK_CLASS_2_G;
1773            case NETWORK_TYPE_UMTS:
1774            case NETWORK_TYPE_EVDO_0:
1775            case NETWORK_TYPE_EVDO_A:
1776            case NETWORK_TYPE_HSDPA:
1777            case NETWORK_TYPE_HSUPA:
1778            case NETWORK_TYPE_HSPA:
1779            case NETWORK_TYPE_EVDO_B:
1780            case NETWORK_TYPE_EHRPD:
1781            case NETWORK_TYPE_HSPAP:
1782            case NETWORK_TYPE_TD_SCDMA:
1783                return NETWORK_CLASS_3_G;
1784            case NETWORK_TYPE_LTE:
1785            case NETWORK_TYPE_IWLAN:
1786            case NETWORK_TYPE_LTE_CA:
1787                return NETWORK_CLASS_4_G;
1788            default:
1789                return NETWORK_CLASS_UNKNOWN;
1790        }
1791    }
1792
1793    /**
1794     * Returns a string representation of the radio technology (network type)
1795     * currently in use on the device.
1796     * @return the name of the radio technology
1797     *
1798     * @hide pending API council review
1799     */
1800    public String getNetworkTypeName() {
1801        return getNetworkTypeName(getNetworkType());
1802    }
1803
1804    /**
1805     * Returns a string representation of the radio technology (network type)
1806     * currently in use on the device.
1807     * @param subId for which network type is returned
1808     * @return the name of the radio technology
1809     *
1810     */
1811    /** {@hide} */
1812    public static String getNetworkTypeName(int type) {
1813        switch (type) {
1814            case NETWORK_TYPE_GPRS:
1815                return "GPRS";
1816            case NETWORK_TYPE_EDGE:
1817                return "EDGE";
1818            case NETWORK_TYPE_UMTS:
1819                return "UMTS";
1820            case NETWORK_TYPE_HSDPA:
1821                return "HSDPA";
1822            case NETWORK_TYPE_HSUPA:
1823                return "HSUPA";
1824            case NETWORK_TYPE_HSPA:
1825                return "HSPA";
1826            case NETWORK_TYPE_CDMA:
1827                return "CDMA";
1828            case NETWORK_TYPE_EVDO_0:
1829                return "CDMA - EvDo rev. 0";
1830            case NETWORK_TYPE_EVDO_A:
1831                return "CDMA - EvDo rev. A";
1832            case NETWORK_TYPE_EVDO_B:
1833                return "CDMA - EvDo rev. B";
1834            case NETWORK_TYPE_1xRTT:
1835                return "CDMA - 1xRTT";
1836            case NETWORK_TYPE_LTE:
1837                return "LTE";
1838            case NETWORK_TYPE_EHRPD:
1839                return "CDMA - eHRPD";
1840            case NETWORK_TYPE_IDEN:
1841                return "iDEN";
1842            case NETWORK_TYPE_HSPAP:
1843                return "HSPA+";
1844            case NETWORK_TYPE_GSM:
1845                return "GSM";
1846            case NETWORK_TYPE_TD_SCDMA:
1847                return "TD_SCDMA";
1848            case NETWORK_TYPE_IWLAN:
1849                return "IWLAN";
1850            case NETWORK_TYPE_LTE_CA:
1851                return "LTE_CA";
1852            default:
1853                return "UNKNOWN";
1854        }
1855    }
1856
1857    //
1858    //
1859    // SIM Card
1860    //
1861    //
1862
1863    /**
1864     * SIM card state: Unknown. Signifies that the SIM is in transition
1865     * between states. For example, when the user inputs the SIM pin
1866     * under PIN_REQUIRED state, a query for sim status returns
1867     * this state before turning to SIM_STATE_READY.
1868     *
1869     * These are the ordinal value of IccCardConstants.State.
1870     */
1871    public static final int SIM_STATE_UNKNOWN = 0;
1872    /** SIM card state: no SIM card is available in the device */
1873    public static final int SIM_STATE_ABSENT = 1;
1874    /** SIM card state: Locked: requires the user's SIM PIN to unlock */
1875    public static final int SIM_STATE_PIN_REQUIRED = 2;
1876    /** SIM card state: Locked: requires the user's SIM PUK to unlock */
1877    public static final int SIM_STATE_PUK_REQUIRED = 3;
1878    /** SIM card state: Locked: requires a network PIN to unlock */
1879    public static final int SIM_STATE_NETWORK_LOCKED = 4;
1880    /** SIM card state: Ready */
1881    public static final int SIM_STATE_READY = 5;
1882    /** SIM card state: SIM Card is NOT READY */
1883    public static final int SIM_STATE_NOT_READY = 6;
1884    /** SIM card state: SIM Card Error, permanently disabled */
1885    public static final int SIM_STATE_PERM_DISABLED = 7;
1886    /** SIM card state: SIM Card Error, present but faulty */
1887    public static final int SIM_STATE_CARD_IO_ERROR = 8;
1888    /** SIM card state: SIM Card restricted, present but not usable due to
1889     * carrier restrictions.
1890     */
1891    public static final int SIM_STATE_CARD_RESTRICTED = 9;
1892
1893    /**
1894     * @return true if a ICC card is present
1895     */
1896    public boolean hasIccCard() {
1897        return hasIccCard(getDefaultSim());
1898    }
1899
1900    /**
1901     * @return true if a ICC card is present for a subscription
1902     *
1903     * @param slotId for which icc card presence is checked
1904     */
1905    /** {@hide} */
1906    // FIXME Input argument slotId should be of type int
1907    public boolean hasIccCard(int slotId) {
1908
1909        try {
1910            ITelephony telephony = getITelephony();
1911            if (telephony == null)
1912                return false;
1913            return telephony.hasIccCardUsingSlotId(slotId);
1914        } catch (RemoteException ex) {
1915            // Assume no ICC card if remote exception which shouldn't happen
1916            return false;
1917        } catch (NullPointerException ex) {
1918            // This could happen before phone restarts due to crashing
1919            return false;
1920        }
1921    }
1922
1923    /**
1924     * Returns a constant indicating the state of the default SIM card.
1925     *
1926     * @see #SIM_STATE_UNKNOWN
1927     * @see #SIM_STATE_ABSENT
1928     * @see #SIM_STATE_PIN_REQUIRED
1929     * @see #SIM_STATE_PUK_REQUIRED
1930     * @see #SIM_STATE_NETWORK_LOCKED
1931     * @see #SIM_STATE_READY
1932     * @see #SIM_STATE_NOT_READY
1933     * @see #SIM_STATE_PERM_DISABLED
1934     * @see #SIM_STATE_CARD_IO_ERROR
1935     * @see #SIM_STATE_CARD_RESTRICTED
1936     */
1937    public int getSimState() {
1938        int slotIdx = getDefaultSim();
1939        // slotIdx may be invalid due to sim being absent. In that case query all slots to get
1940        // sim state
1941        if (slotIdx < 0) {
1942            // query for all slots and return absent if all sim states are absent, otherwise
1943            // return unknown
1944            for (int i = 0; i < getPhoneCount(); i++) {
1945                int simState = getSimState(i);
1946                if (simState != SIM_STATE_ABSENT) {
1947                    Rlog.d(TAG, "getSimState: default sim:" + slotIdx + ", sim state for " +
1948                            "slotIdx=" + i + " is " + simState + ", return state as unknown");
1949                    return SIM_STATE_UNKNOWN;
1950                }
1951            }
1952            Rlog.d(TAG, "getSimState: default sim:" + slotIdx + ", all SIMs absent, return " +
1953                    "state as absent");
1954            return SIM_STATE_ABSENT;
1955        }
1956        return getSimState(slotIdx);
1957    }
1958
1959    /**
1960     * Returns a constant indicating the state of the device SIM card in a slot.
1961     *
1962     * @param slotIdx
1963     *
1964     * @see #SIM_STATE_UNKNOWN
1965     * @see #SIM_STATE_ABSENT
1966     * @see #SIM_STATE_PIN_REQUIRED
1967     * @see #SIM_STATE_PUK_REQUIRED
1968     * @see #SIM_STATE_NETWORK_LOCKED
1969     * @see #SIM_STATE_READY
1970     * @see #SIM_STATE_NOT_READY
1971     * @see #SIM_STATE_PERM_DISABLED
1972     * @see #SIM_STATE_CARD_IO_ERROR
1973     * @see #SIM_STATE_CARD_RESTRICTED
1974     */
1975    public int getSimState(int slotIdx) {
1976        int simState = SubscriptionManager.getSimStateForSlotIdx(slotIdx);
1977        return simState;
1978    }
1979
1980    /**
1981     * Returns the MCC+MNC (mobile country code + mobile network code) of the
1982     * provider of the SIM. 5 or 6 decimal digits.
1983     * <p>
1984     * Availability: SIM state must be {@link #SIM_STATE_READY}
1985     *
1986     * @see #getSimState
1987     */
1988    public String getSimOperator() {
1989        return getSimOperatorNumeric();
1990    }
1991
1992    /**
1993     * Returns the MCC+MNC (mobile country code + mobile network code) of the
1994     * provider of the SIM. 5 or 6 decimal digits.
1995     * <p>
1996     * Availability: SIM state must be {@link #SIM_STATE_READY}
1997     *
1998     * @see #getSimState
1999     *
2000     * @param subId for which SimOperator is returned
2001     * @hide
2002     */
2003    public String getSimOperator(int subId) {
2004        return getSimOperatorNumeric(subId);
2005    }
2006
2007    /**
2008     * Returns the MCC+MNC (mobile country code + mobile network code) of the
2009     * provider of the SIM. 5 or 6 decimal digits.
2010     * <p>
2011     * Availability: SIM state must be {@link #SIM_STATE_READY}
2012     *
2013     * @see #getSimState
2014     * @hide
2015     */
2016    public String getSimOperatorNumeric() {
2017        int subId = SubscriptionManager.getDefaultDataSubscriptionId();
2018        if (!SubscriptionManager.isUsableSubIdValue(subId)) {
2019            subId = SubscriptionManager.getDefaultSmsSubscriptionId();
2020            if (!SubscriptionManager.isUsableSubIdValue(subId)) {
2021                subId = SubscriptionManager.getDefaultVoiceSubscriptionId();
2022                if (!SubscriptionManager.isUsableSubIdValue(subId)) {
2023                    subId = SubscriptionManager.getDefaultSubscriptionId();
2024                }
2025            }
2026        }
2027        return getSimOperatorNumeric(subId);
2028    }
2029
2030    /**
2031     * Returns the MCC+MNC (mobile country code + mobile network code) of the
2032     * provider of the SIM for a particular subscription. 5 or 6 decimal digits.
2033     * <p>
2034     * Availability: SIM state must be {@link #SIM_STATE_READY}
2035     *
2036     * @see #getSimState
2037     *
2038     * @param subId for which SimOperator is returned
2039     * @hide
2040     */
2041    public String getSimOperatorNumeric(int subId) {
2042        int phoneId = SubscriptionManager.getPhoneId(subId);
2043        return getSimOperatorNumericForPhone(phoneId);
2044    }
2045
2046    /**
2047     * Returns the MCC+MNC (mobile country code + mobile network code) of the
2048     * provider of the SIM for a particular subscription. 5 or 6 decimal digits.
2049     * <p>
2050     *
2051     * @param phoneId for which SimOperator is returned
2052     * @hide
2053     */
2054    public String getSimOperatorNumericForPhone(int phoneId) {
2055        return getTelephonyProperty(phoneId,
2056                TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC, "");
2057    }
2058
2059    /**
2060     * Returns the Service Provider Name (SPN).
2061     * <p>
2062     * Availability: SIM state must be {@link #SIM_STATE_READY}
2063     *
2064     * @see #getSimState
2065     */
2066    public String getSimOperatorName() {
2067        return getSimOperatorNameForPhone(getDefaultPhone());
2068    }
2069
2070    /**
2071     * Returns the Service Provider Name (SPN).
2072     * <p>
2073     * Availability: SIM state must be {@link #SIM_STATE_READY}
2074     *
2075     * @see #getSimState
2076     *
2077     * @param subId for which SimOperatorName is returned
2078     * @hide
2079     */
2080    public String getSimOperatorName(int subId) {
2081        int phoneId = SubscriptionManager.getPhoneId(subId);
2082        return getSimOperatorNameForPhone(phoneId);
2083    }
2084
2085    /**
2086     * Returns the Service Provider Name (SPN).
2087     *
2088     * @hide
2089     */
2090    public String getSimOperatorNameForPhone(int phoneId) {
2091         return getTelephonyProperty(phoneId,
2092                TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA, "");
2093    }
2094
2095    /**
2096     * Returns the ISO country code equivalent for the SIM provider's country code.
2097     */
2098    public String getSimCountryIso() {
2099        return getSimCountryIsoForPhone(getDefaultPhone());
2100    }
2101
2102    /**
2103     * Returns the ISO country code equivalent for the SIM provider's country code.
2104     *
2105     * @param subId for which SimCountryIso is returned
2106     * @hide
2107     */
2108    public String getSimCountryIso(int subId) {
2109        int phoneId = SubscriptionManager.getPhoneId(subId);
2110        return getSimCountryIsoForPhone(phoneId);
2111    }
2112
2113    /**
2114     * Returns the ISO country code equivalent for the SIM provider's country code.
2115     *
2116     * @hide
2117     */
2118    public String getSimCountryIsoForPhone(int phoneId) {
2119        return getTelephonyProperty(phoneId,
2120                TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY, "");
2121    }
2122
2123    /**
2124     * Returns the serial number of the SIM, if applicable. Return null if it is
2125     * unavailable.
2126     * <p>
2127     * Requires Permission:
2128     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2129     */
2130    public String getSimSerialNumber() {
2131         return getSimSerialNumber(getSubId());
2132    }
2133
2134    /**
2135     * Returns the serial number for the given subscription, if applicable. Return null if it is
2136     * unavailable.
2137     * <p>
2138     * @param subId for which Sim Serial number is returned
2139     * Requires Permission:
2140     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2141     * @hide
2142     */
2143    public String getSimSerialNumber(int subId) {
2144        try {
2145            IPhoneSubInfo info = getSubscriberInfo();
2146            if (info == null)
2147                return null;
2148            return info.getIccSerialNumberForSubscriber(subId, mContext.getOpPackageName());
2149        } catch (RemoteException ex) {
2150            return null;
2151        } catch (NullPointerException ex) {
2152            // This could happen before phone restarts due to crashing
2153            return null;
2154        }
2155    }
2156
2157    /**
2158     * Return if the current radio is LTE on CDMA. This
2159     * is a tri-state return value as for a period of time
2160     * the mode may be unknown.
2161     *
2162     * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE}
2163     * or {@link PhoneConstants#LTE_ON_CDMA_TRUE}
2164     *
2165     * <p>
2166     * Requires Permission:
2167     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2168     *
2169     * @hide
2170     */
2171    public int getLteOnCdmaMode() {
2172        return getLteOnCdmaMode(getSubId());
2173    }
2174
2175    /**
2176     * Return if the current radio is LTE on CDMA for Subscription. This
2177     * is a tri-state return value as for a period of time
2178     * the mode may be unknown.
2179     *
2180     * @param subId for which radio is LTE on CDMA is returned
2181     * @return {@link PhoneConstants#LTE_ON_CDMA_UNKNOWN}, {@link PhoneConstants#LTE_ON_CDMA_FALSE}
2182     * or {@link PhoneConstants#LTE_ON_CDMA_TRUE}
2183     *
2184     * <p>
2185     * Requires Permission:
2186     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2187     * @hide
2188     */
2189    public int getLteOnCdmaMode(int subId) {
2190        try {
2191            ITelephony telephony = getITelephony();
2192            if (telephony == null)
2193                return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2194            return telephony.getLteOnCdmaModeForSubscriber(subId, getOpPackageName());
2195        } catch (RemoteException ex) {
2196            // Assume no ICC card if remote exception which shouldn't happen
2197            return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2198        } catch (NullPointerException ex) {
2199            // This could happen before phone restarts due to crashing
2200            return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2201        }
2202    }
2203
2204    //
2205    //
2206    // Subscriber Info
2207    //
2208    //
2209
2210    /**
2211     * Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
2212     * Return null if it is unavailable.
2213     * <p>
2214     * Requires Permission:
2215     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2216     */
2217    public String getSubscriberId() {
2218        return getSubscriberId(getSubId());
2219    }
2220
2221    /**
2222     * Returns the unique subscriber ID, for example, the IMSI for a GSM phone
2223     * for a subscription.
2224     * Return null if it is unavailable.
2225     * <p>
2226     * Requires Permission:
2227     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2228     *
2229     * @param subId whose subscriber id is returned
2230     * @hide
2231     */
2232    public String getSubscriberId(int subId) {
2233        try {
2234            IPhoneSubInfo info = getSubscriberInfo();
2235            if (info == null)
2236                return null;
2237            return info.getSubscriberIdForSubscriber(subId, mContext.getOpPackageName());
2238        } catch (RemoteException ex) {
2239            return null;
2240        } catch (NullPointerException ex) {
2241            // This could happen before phone restarts due to crashing
2242            return null;
2243        }
2244    }
2245
2246    /**
2247     * Returns the Group Identifier Level1 for a GSM phone.
2248     * Return null if it is unavailable.
2249     * <p>
2250     * Requires Permission:
2251     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2252     */
2253    public String getGroupIdLevel1() {
2254        try {
2255            IPhoneSubInfo info = getSubscriberInfo();
2256            if (info == null)
2257                return null;
2258            return info.getGroupIdLevel1(mContext.getOpPackageName());
2259        } catch (RemoteException ex) {
2260            return null;
2261        } catch (NullPointerException ex) {
2262            // This could happen before phone restarts due to crashing
2263            return null;
2264        }
2265    }
2266
2267    /**
2268     * Returns the Group Identifier Level1 for a GSM phone for a particular subscription.
2269     * Return null if it is unavailable.
2270     * <p>
2271     * Requires Permission:
2272     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2273     *
2274     * @param subId whose subscriber id is returned
2275     * @hide
2276     */
2277    public String getGroupIdLevel1(int subId) {
2278        try {
2279            IPhoneSubInfo info = getSubscriberInfo();
2280            if (info == null)
2281                return null;
2282            return info.getGroupIdLevel1ForSubscriber(subId, mContext.getOpPackageName());
2283        } catch (RemoteException ex) {
2284            return null;
2285        } catch (NullPointerException ex) {
2286            // This could happen before phone restarts due to crashing
2287            return null;
2288        }
2289    }
2290
2291    /**
2292     * Returns the phone number string for line 1, for example, the MSISDN
2293     * for a GSM phone. Return null if it is unavailable.
2294     * <p>
2295     * Requires Permission:
2296     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2297     *   OR
2298     *   {@link android.Manifest.permission#READ_SMS}
2299     * <p>
2300     * The default SMS app can also use this.
2301     */
2302    public String getLine1Number() {
2303        return getLine1Number(getSubId());
2304    }
2305
2306    /**
2307     * Returns the phone number string for line 1, for example, the MSISDN
2308     * for a GSM phone for a particular subscription. Return null if it is unavailable.
2309     * <p>
2310     * Requires Permission:
2311     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2312     *   OR
2313     *   {@link android.Manifest.permission#READ_SMS}
2314     * <p>
2315     * The default SMS app can also use this.
2316     *
2317     * @param subId whose phone number for line 1 is returned
2318     * @hide
2319     */
2320    public String getLine1Number(int subId) {
2321        String number = null;
2322        try {
2323            ITelephony telephony = getITelephony();
2324            if (telephony != null)
2325                number = telephony.getLine1NumberForDisplay(subId, mContext.getOpPackageName());
2326        } catch (RemoteException ex) {
2327        } catch (NullPointerException ex) {
2328        }
2329        if (number != null) {
2330            return number;
2331        }
2332        try {
2333            IPhoneSubInfo info = getSubscriberInfo();
2334            if (info == null)
2335                return null;
2336            return info.getLine1NumberForSubscriber(subId, mContext.getOpPackageName());
2337        } catch (RemoteException ex) {
2338            return null;
2339        } catch (NullPointerException ex) {
2340            // This could happen before phone restarts due to crashing
2341            return null;
2342        }
2343    }
2344
2345    /**
2346     * Set the line 1 phone number string and its alphatag for the current ICCID
2347     * for display purpose only, for example, displayed in Phone Status. It won't
2348     * change the actual MSISDN/MDN. To unset alphatag or number, pass in a null
2349     * value.
2350     *
2351     * <p>Requires that the calling app has carrier privileges.
2352     * @see #hasCarrierPrivileges
2353     *
2354     * @param alphaTag alpha-tagging of the dailing nubmer
2355     * @param number The dialing number
2356     * @return true if the operation was executed correctly.
2357     */
2358    public boolean setLine1NumberForDisplay(String alphaTag, String number) {
2359        return setLine1NumberForDisplay(getSubId(), alphaTag, number);
2360    }
2361
2362    /**
2363     * Set the line 1 phone number string and its alphatag for the current ICCID
2364     * for display purpose only, for example, displayed in Phone Status. It won't
2365     * change the actual MSISDN/MDN. To unset alphatag or number, pass in a null
2366     * value.
2367     *
2368     * <p>Requires that the calling app has carrier privileges.
2369     * @see #hasCarrierPrivileges
2370     *
2371     * @param subId the subscriber that the alphatag and dialing number belongs to.
2372     * @param alphaTag alpha-tagging of the dailing nubmer
2373     * @param number The dialing number
2374     * @return true if the operation was executed correctly.
2375     * @hide
2376     */
2377    public boolean setLine1NumberForDisplay(int subId, String alphaTag, String number) {
2378        try {
2379            ITelephony telephony = getITelephony();
2380            if (telephony != null)
2381                return telephony.setLine1NumberForDisplayForSubscriber(subId, alphaTag, number);
2382        } catch (RemoteException ex) {
2383        } catch (NullPointerException ex) {
2384        }
2385        return false;
2386    }
2387
2388    /**
2389     * Returns the alphabetic identifier associated with the line 1 number.
2390     * Return null if it is unavailable.
2391     * <p>
2392     * Requires Permission:
2393     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2394     * @hide
2395     * nobody seems to call this.
2396     */
2397    public String getLine1AlphaTag() {
2398        return getLine1AlphaTag(getSubId());
2399    }
2400
2401    /**
2402     * Returns the alphabetic identifier associated with the line 1 number
2403     * for a subscription.
2404     * Return null if it is unavailable.
2405     * <p>
2406     * Requires Permission:
2407     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2408     * @param subId whose alphabetic identifier associated with line 1 is returned
2409     * nobody seems to call this.
2410     * @hide
2411     */
2412    public String getLine1AlphaTag(int subId) {
2413        String alphaTag = null;
2414        try {
2415            ITelephony telephony = getITelephony();
2416            if (telephony != null)
2417                alphaTag = telephony.getLine1AlphaTagForDisplay(subId,
2418                        getOpPackageName());
2419        } catch (RemoteException ex) {
2420        } catch (NullPointerException ex) {
2421        }
2422        if (alphaTag != null) {
2423            return alphaTag;
2424        }
2425        try {
2426            IPhoneSubInfo info = getSubscriberInfo();
2427            if (info == null)
2428                return null;
2429            return info.getLine1AlphaTagForSubscriber(subId, getOpPackageName());
2430        } catch (RemoteException ex) {
2431            return null;
2432        } catch (NullPointerException ex) {
2433            // This could happen before phone restarts due to crashing
2434            return null;
2435        }
2436    }
2437
2438    /**
2439     * Return the set of subscriber IDs that should be considered as "merged
2440     * together" for data usage purposes. This is commonly {@code null} to
2441     * indicate no merging is required. Any returned subscribers are sorted in a
2442     * deterministic order.
2443     *
2444     * @hide
2445     */
2446    public @Nullable String[] getMergedSubscriberIds() {
2447        try {
2448            ITelephony telephony = getITelephony();
2449            if (telephony != null)
2450                return telephony.getMergedSubscriberIds(getOpPackageName());
2451        } catch (RemoteException ex) {
2452        } catch (NullPointerException ex) {
2453        }
2454        return null;
2455    }
2456
2457    /**
2458     * Returns the MSISDN string.
2459     * for a GSM phone. Return null if it is unavailable.
2460     * <p>
2461     * Requires Permission:
2462     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2463     *
2464     * @hide
2465     */
2466    public String getMsisdn() {
2467        return getMsisdn(getSubId());
2468    }
2469
2470    /**
2471     * Returns the MSISDN string.
2472     * for a GSM phone. Return null if it is unavailable.
2473     * <p>
2474     * Requires Permission:
2475     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2476     *
2477     * @param subId for which msisdn is returned
2478     * @hide
2479     */
2480    public String getMsisdn(int subId) {
2481        try {
2482            IPhoneSubInfo info = getSubscriberInfo();
2483            if (info == null)
2484                return null;
2485            return info.getMsisdnForSubscriber(subId, getOpPackageName());
2486        } catch (RemoteException ex) {
2487            return null;
2488        } catch (NullPointerException ex) {
2489            // This could happen before phone restarts due to crashing
2490            return null;
2491        }
2492    }
2493
2494    /**
2495     * Returns the voice mail number. Return null if it is unavailable.
2496     * <p>
2497     * Requires Permission:
2498     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2499     */
2500    public String getVoiceMailNumber() {
2501        return getVoiceMailNumber(getSubId());
2502    }
2503
2504    /**
2505     * Returns the voice mail number for a subscription.
2506     * Return null if it is unavailable.
2507     * <p>
2508     * Requires Permission:
2509     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2510     * @param subId whose voice mail number is returned
2511     * @hide
2512     */
2513    public String getVoiceMailNumber(int subId) {
2514        try {
2515            IPhoneSubInfo info = getSubscriberInfo();
2516            if (info == null)
2517                return null;
2518            return info.getVoiceMailNumberForSubscriber(subId, getOpPackageName());
2519        } catch (RemoteException ex) {
2520            return null;
2521        } catch (NullPointerException ex) {
2522            // This could happen before phone restarts due to crashing
2523            return null;
2524        }
2525    }
2526
2527    /**
2528     * Returns the complete voice mail number. Return null if it is unavailable.
2529     * <p>
2530     * Requires Permission:
2531     *   {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED}
2532     *
2533     * @hide
2534     */
2535    public String getCompleteVoiceMailNumber() {
2536        return getCompleteVoiceMailNumber(getSubId());
2537    }
2538
2539    /**
2540     * Returns the complete voice mail number. Return null if it is unavailable.
2541     * <p>
2542     * Requires Permission:
2543     *   {@link android.Manifest.permission#CALL_PRIVILEGED CALL_PRIVILEGED}
2544     *
2545     * @param subId
2546     * @hide
2547     */
2548    public String getCompleteVoiceMailNumber(int subId) {
2549        try {
2550            IPhoneSubInfo info = getSubscriberInfo();
2551            if (info == null)
2552                return null;
2553            return info.getCompleteVoiceMailNumberForSubscriber(subId);
2554        } catch (RemoteException ex) {
2555            return null;
2556        } catch (NullPointerException ex) {
2557            // This could happen before phone restarts due to crashing
2558            return null;
2559        }
2560    }
2561
2562    /**
2563     * Sets the voice mail number.
2564     *
2565     * <p>Requires that the calling app has carrier privileges.
2566     * @see #hasCarrierPrivileges
2567     *
2568     * @param alphaTag The alpha tag to display.
2569     * @param number The voicemail number.
2570     */
2571    public boolean setVoiceMailNumber(String alphaTag, String number) {
2572        return setVoiceMailNumber(getSubId(), alphaTag, number);
2573    }
2574
2575    /**
2576     * Sets the voicemail number for the given subscriber.
2577     *
2578     * <p>Requires that the calling app has carrier privileges.
2579     * @see #hasCarrierPrivileges
2580     *
2581     * @param subId The subscription id.
2582     * @param alphaTag The alpha tag to display.
2583     * @param number The voicemail number.
2584     * @hide
2585     */
2586    public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
2587        try {
2588            ITelephony telephony = getITelephony();
2589            if (telephony != null)
2590                return telephony.setVoiceMailNumber(subId, alphaTag, number);
2591        } catch (RemoteException ex) {
2592        } catch (NullPointerException ex) {
2593        }
2594        return false;
2595    }
2596
2597    /**
2598     * Enables or disables the visual voicemail client for a phone account.
2599     *
2600     * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
2601     * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
2602     * @see #hasCarrierPrivileges
2603     *
2604     * @param phoneAccountHandle the phone account to change the client state
2605     * @param enabled the new state of the client
2606     * @hide
2607     */
2608    @SystemApi
2609    public void setVisualVoicemailEnabled(PhoneAccountHandle phoneAccountHandle, boolean enabled){
2610        try {
2611            ITelephony telephony = getITelephony();
2612            if (telephony != null) {
2613                telephony.setVisualVoicemailEnabled(mContext.getOpPackageName(), phoneAccountHandle,
2614                    enabled);
2615            }
2616        } catch (RemoteException ex) {
2617        } catch (NullPointerException ex) {
2618            // This could happen before phone restarts due to crashing
2619        }
2620    }
2621
2622    /**
2623     * Returns whether the visual voicemail client is enabled.
2624     *
2625     * <p>Requires Permission:
2626     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2627     *
2628     * @param phoneAccountHandle the phone account to check for.
2629     * @return {@code true} when the visual voicemail client is enabled for this client
2630     * @hide
2631     */
2632    @SystemApi
2633    public boolean isVisualVoicemailEnabled(PhoneAccountHandle phoneAccountHandle){
2634        try {
2635            ITelephony telephony = getITelephony();
2636            if (telephony != null) {
2637                return telephony.isVisualVoicemailEnabled(
2638                    mContext.getOpPackageName(), phoneAccountHandle);
2639            }
2640        } catch (RemoteException ex) {
2641        } catch (NullPointerException ex) {
2642            // This could happen before phone restarts due to crashing
2643        }
2644        return false;
2645    }
2646
2647    /**
2648     * Enables the visual voicemail SMS filter for a phone account. When the filter is
2649     * enabled, Incoming SMS messages matching the OMTP VVM SMS interface will be redirected to the
2650     * visual voicemail client with
2651     * {@link android.provider.VoicemailContract.ACTION_VOICEMAIL_SMS_RECEIVED}.
2652     *
2653     * <p>This takes effect only when the caller is the default dialer. The enabled status and
2654     * settings persist through default dialer changes, but the filter will only honor the setting
2655     * set by the current default dialer.
2656     *
2657     *
2658     * @param subId The subscription id of the phone account.
2659     * @param settings The settings for the filter.
2660     */
2661    /** @hide */
2662    public void enableVisualVoicemailSmsFilter(int subId,
2663            VisualVoicemailSmsFilterSettings settings) {
2664        if(settings == null){
2665            throw new IllegalArgumentException("Settings cannot be null");
2666        }
2667        try {
2668            ITelephony telephony = getITelephony();
2669            if (telephony != null) {
2670                telephony.enableVisualVoicemailSmsFilter(mContext.getOpPackageName(), subId,
2671                        settings);
2672            }
2673        } catch (RemoteException ex) {
2674        } catch (NullPointerException ex) {
2675        }
2676    }
2677
2678    /**
2679     * Disables the visual voicemail SMS filter for a phone account.
2680     *
2681     * <p>This takes effect only when the caller is the default dialer. The enabled status and
2682     * settings persist through default dialer changes, but the filter will only honor the setting
2683     * set by the current default dialer.
2684     */
2685    /** @hide */
2686    public void disableVisualVoicemailSmsFilter(int subId) {
2687        try {
2688            ITelephony telephony = getITelephony();
2689            if (telephony != null) {
2690                telephony.disableVisualVoicemailSmsFilter(mContext.getOpPackageName(), subId);
2691            }
2692        } catch (RemoteException ex) {
2693        } catch (NullPointerException ex) {
2694        }
2695    }
2696
2697    /**
2698     * @returns the settings of the visual voicemail SMS filter for a phone account, or {@code null}
2699     * if the filter is disabled.
2700     *
2701     * <p>This takes effect only when the caller is the default dialer. The enabled status and
2702     * settings persist through default dialer changes, but the filter will only honor the setting
2703     * set by the current default dialer.
2704     */
2705    /** @hide */
2706    @Nullable
2707    public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(int subId) {
2708        try {
2709            ITelephony telephony = getITelephony();
2710            if (telephony != null) {
2711                return telephony
2712                        .getVisualVoicemailSmsFilterSettings(mContext.getOpPackageName(), subId);
2713            }
2714        } catch (RemoteException ex) {
2715        } catch (NullPointerException ex) {
2716        }
2717
2718        return null;
2719    }
2720
2721    /**
2722     * @returns the settings of the visual voicemail SMS filter for a phone account set by the
2723     * package, or {@code null} if the filter is disabled.
2724     *
2725     * <p>Requires the calling app to have READ_PRIVILEGED_PHONE_STATE permission.
2726     */
2727    /** @hide */
2728    @Nullable
2729    public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(String packageName,
2730            int subId) {
2731        try {
2732            ITelephony telephony = getITelephony();
2733            if (telephony != null) {
2734                return telephony.getSystemVisualVoicemailSmsFilterSettings(packageName, subId);
2735            }
2736        } catch (RemoteException ex) {
2737        } catch (NullPointerException ex) {
2738        }
2739
2740        return null;
2741    }
2742
2743    /**
2744     * Initial SIM activation state, unknown. Not set by any carrier apps.
2745     * @hide
2746     */
2747    public static final int SIM_ACTIVATION_STATE_UNKNOWN = 0;
2748
2749    /**
2750     * indicate SIM is under activation procedure now.
2751     * intermediate state followed by another state update with activation procedure result:
2752     * @see #SIM_ACTIVATION_STATE_ACTIVATED
2753     * @see #SIM_ACTIVATION_STATE_DEACTIVATED
2754     * @see #SIM_ACTIVATION_STATE_RESTRICTED
2755     * @hide
2756     */
2757    public static final int SIM_ACTIVATION_STATE_ACTIVATING = 1;
2758
2759    /**
2760     * Indicate SIM has been successfully activated with full service
2761     * @hide
2762     */
2763    public static final int SIM_ACTIVATION_STATE_ACTIVATED = 2;
2764
2765    /**
2766     * Indicate SIM has been deactivated by the carrier so that service is not available
2767     * and requires activation service to enable services.
2768     * Carrier apps could be signalled to set activation state to deactivated if detected
2769     * deactivated sim state and set it back to activated after successfully run activation service.
2770     * @hide
2771     */
2772    public static final int SIM_ACTIVATION_STATE_DEACTIVATED = 3;
2773
2774    /**
2775     * Restricted state indicate SIM has been activated but service are restricted.
2776     * note this is currently available for data activation state. For example out of byte sim.
2777     * @hide
2778     */
2779    public static final int SIM_ACTIVATION_STATE_RESTRICTED = 4;
2780
2781    /**
2782     * Sets the voice activation state for the given subscriber.
2783     *
2784     * <p>Requires Permission:
2785     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2786     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
2787     *
2788     * @param subId The subscription id.
2789     * @param activationState The voice activation state of the given subscriber.
2790     * @see #SIM_ACTIVATION_STATE_UNKNOWN
2791     * @see #SIM_ACTIVATION_STATE_ACTIVATING
2792     * @see #SIM_ACTIVATION_STATE_ACTIVATED
2793     * @see #SIM_ACTIVATION_STATE_DEACTIVATED
2794     * @hide
2795     */
2796    public void setVoiceActivationState(int subId, int activationState) {
2797        try {
2798            ITelephony telephony = getITelephony();
2799            if (telephony != null)
2800                telephony.setVoiceActivationState(subId, activationState);
2801        } catch (RemoteException ex) {
2802        } catch (NullPointerException ex) {
2803        }
2804    }
2805
2806    /**
2807     * Sets the data activation state for the given subscriber.
2808     *
2809     * <p>Requires Permission:
2810     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE}
2811     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
2812     *
2813     * @param subId The subscription id.
2814     * @param activationState The data activation state of the given subscriber.
2815     * @see #SIM_ACTIVATION_STATE_UNKNOWN
2816     * @see #SIM_ACTIVATION_STATE_ACTIVATING
2817     * @see #SIM_ACTIVATION_STATE_ACTIVATED
2818     * @see #SIM_ACTIVATION_STATE_DEACTIVATED
2819     * @see #SIM_ACTIVATION_STATE_RESTRICTED
2820     * @hide
2821     */
2822    public void setDataActivationState(int subId, int activationState) {
2823        try {
2824            ITelephony telephony = getITelephony();
2825            if (telephony != null)
2826                telephony.setDataActivationState(subId, activationState);
2827        } catch (RemoteException ex) {
2828        } catch (NullPointerException ex) {
2829        }
2830    }
2831
2832    /**
2833     * Returns the voice activation state for the given subscriber.
2834     *
2835     * <p>Requires Permission:
2836     *   {@link android.Manifest.permission#READ_PHONE_STATE}
2837     *
2838     * @param subId The subscription id.
2839     *
2840     * @return voiceActivationState for the given subscriber
2841     * @see #SIM_ACTIVATION_STATE_UNKNOWN
2842     * @see #SIM_ACTIVATION_STATE_ACTIVATING
2843     * @see #SIM_ACTIVATION_STATE_ACTIVATED
2844     * @see #SIM_ACTIVATION_STATE_DEACTIVATED
2845     * @hide
2846     */
2847    public int getVoiceActivationState(int subId) {
2848        try {
2849            ITelephony telephony = getITelephony();
2850            if (telephony != null)
2851                return telephony.getVoiceActivationState(subId, getOpPackageName());
2852        } catch (RemoteException ex) {
2853        } catch (NullPointerException ex) {
2854        }
2855        return SIM_ACTIVATION_STATE_UNKNOWN;
2856    }
2857
2858    /**
2859     * Returns the data activation state for the given subscriber.
2860     *
2861     * <p>Requires Permission:
2862     *   {@link android.Manifest.permission#READ_PHONE_STATE}
2863     *
2864     * @param subId The subscription id.
2865     *
2866     * @return dataActivationState for the given subscriber
2867     * @see #SIM_ACTIVATION_STATE_UNKNOWN
2868     * @see #SIM_ACTIVATION_STATE_ACTIVATING
2869     * @see #SIM_ACTIVATION_STATE_ACTIVATED
2870     * @see #SIM_ACTIVATION_STATE_DEACTIVATED
2871     * @see #SIM_ACTIVATION_STATE_RESTRICTED
2872     * @hide
2873     */
2874    public int getDataActivationState(int subId) {
2875        try {
2876            ITelephony telephony = getITelephony();
2877            if (telephony != null)
2878                return telephony.getDataActivationState(subId, getOpPackageName());
2879        } catch (RemoteException ex) {
2880        } catch (NullPointerException ex) {
2881        }
2882        return SIM_ACTIVATION_STATE_UNKNOWN;
2883    }
2884
2885    /**
2886     * Returns the voice mail count. Return 0 if unavailable, -1 if there are unread voice messages
2887     * but the count is unknown.
2888     * <p>
2889     * Requires Permission:
2890     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2891     * @hide
2892     */
2893    public int getVoiceMessageCount() {
2894        return getVoiceMessageCount(getSubId());
2895    }
2896
2897    /**
2898     * Returns the voice mail count for a subscription. Return 0 if unavailable.
2899     * <p>
2900     * Requires Permission:
2901     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2902     * @param subId whose voice message count is returned
2903     * @hide
2904     */
2905    public int getVoiceMessageCount(int subId) {
2906        try {
2907            ITelephony telephony = getITelephony();
2908            if (telephony == null)
2909                return 0;
2910            return telephony.getVoiceMessageCountForSubscriber(subId);
2911        } catch (RemoteException ex) {
2912            return 0;
2913        } catch (NullPointerException ex) {
2914            // This could happen before phone restarts due to crashing
2915            return 0;
2916        }
2917    }
2918
2919    /**
2920     * Retrieves the alphabetic identifier associated with the voice
2921     * mail number.
2922     * <p>
2923     * Requires Permission:
2924     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2925     */
2926    public String getVoiceMailAlphaTag() {
2927        return getVoiceMailAlphaTag(getSubId());
2928    }
2929
2930    /**
2931     * Retrieves the alphabetic identifier associated with the voice
2932     * mail number for a subscription.
2933     * <p>
2934     * Requires Permission:
2935     * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2936     * @param subId whose alphabetic identifier associated with the
2937     * voice mail number is returned
2938     * @hide
2939     */
2940    public String getVoiceMailAlphaTag(int subId) {
2941        try {
2942            IPhoneSubInfo info = getSubscriberInfo();
2943            if (info == null)
2944                return null;
2945            return info.getVoiceMailAlphaTagForSubscriber(subId, getOpPackageName());
2946        } catch (RemoteException ex) {
2947            return null;
2948        } catch (NullPointerException ex) {
2949            // This could happen before phone restarts due to crashing
2950            return null;
2951        }
2952    }
2953
2954    /**
2955     * Returns the IMS private user identity (IMPI) that was loaded from the ISIM.
2956     * @return the IMPI, or null if not present or not loaded
2957     * @hide
2958     */
2959    public String getIsimImpi() {
2960        try {
2961            IPhoneSubInfo info = getSubscriberInfo();
2962            if (info == null)
2963                return null;
2964            return info.getIsimImpi();
2965        } catch (RemoteException ex) {
2966            return null;
2967        } catch (NullPointerException ex) {
2968            // This could happen before phone restarts due to crashing
2969            return null;
2970        }
2971    }
2972
2973    /**
2974     * Returns the IMS home network domain name that was loaded from the ISIM.
2975     * @return the IMS domain name, or null if not present or not loaded
2976     * @hide
2977     */
2978    public String getIsimDomain() {
2979        try {
2980            IPhoneSubInfo info = getSubscriberInfo();
2981            if (info == null)
2982                return null;
2983            return info.getIsimDomain();
2984        } catch (RemoteException ex) {
2985            return null;
2986        } catch (NullPointerException ex) {
2987            // This could happen before phone restarts due to crashing
2988            return null;
2989        }
2990    }
2991
2992    /**
2993     * Returns the IMS public user identities (IMPU) that were loaded from the ISIM.
2994     * @return an array of IMPU strings, with one IMPU per string, or null if
2995     *      not present or not loaded
2996     * @hide
2997     */
2998    public String[] getIsimImpu() {
2999        try {
3000            IPhoneSubInfo info = getSubscriberInfo();
3001            if (info == null)
3002                return null;
3003            return info.getIsimImpu();
3004        } catch (RemoteException ex) {
3005            return null;
3006        } catch (NullPointerException ex) {
3007            // This could happen before phone restarts due to crashing
3008            return null;
3009        }
3010    }
3011
3012   /**
3013    * @hide
3014    */
3015    private IPhoneSubInfo getSubscriberInfo() {
3016        // get it each time because that process crashes a lot
3017        return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
3018    }
3019
3020    /** Device call state: No activity. */
3021    public static final int CALL_STATE_IDLE = 0;
3022    /** Device call state: Ringing. A new call arrived and is
3023     *  ringing or waiting. In the latter case, another call is
3024     *  already active. */
3025    public static final int CALL_STATE_RINGING = 1;
3026    /** Device call state: Off-hook. At least one call exists
3027      * that is dialing, active, or on hold, and no calls are ringing
3028      * or waiting. */
3029    public static final int CALL_STATE_OFFHOOK = 2;
3030
3031    /**
3032     * Returns one of the following constants that represents the current state of all
3033     * phone calls.
3034     *
3035     * {@link TelephonyManager#CALL_STATE_RINGING}
3036     * {@link TelephonyManager#CALL_STATE_OFFHOOK}
3037     * {@link TelephonyManager#CALL_STATE_IDLE}
3038     */
3039    public int getCallState() {
3040        try {
3041            ITelecomService telecom = getTelecomService();
3042            if (telecom != null) {
3043                return telecom.getCallState();
3044            }
3045        } catch (RemoteException e) {
3046            Log.e(TAG, "Error calling ITelecomService#getCallState", e);
3047        }
3048        return CALL_STATE_IDLE;
3049    }
3050
3051    /**
3052     * Returns a constant indicating the call state (cellular) on the device
3053     * for a subscription.
3054     *
3055     * @param subId whose call state is returned
3056     * @hide
3057     */
3058    public int getCallState(int subId) {
3059        int phoneId = SubscriptionManager.getPhoneId(subId);
3060        return getCallStateForSlot(phoneId);
3061    }
3062
3063    /**
3064     * See getCallState.
3065     *
3066     * @hide
3067     */
3068    public int getCallStateForSlot(int slotId) {
3069        try {
3070            ITelephony telephony = getITelephony();
3071            if (telephony == null)
3072                return CALL_STATE_IDLE;
3073            return telephony.getCallStateForSlot(slotId);
3074        } catch (RemoteException ex) {
3075            // the phone process is restarting.
3076            return CALL_STATE_IDLE;
3077        } catch (NullPointerException ex) {
3078          // the phone process is restarting.
3079          return CALL_STATE_IDLE;
3080        }
3081    }
3082
3083
3084    /** Data connection activity: No traffic. */
3085    public static final int DATA_ACTIVITY_NONE = 0x00000000;
3086    /** Data connection activity: Currently receiving IP PPP traffic. */
3087    public static final int DATA_ACTIVITY_IN = 0x00000001;
3088    /** Data connection activity: Currently sending IP PPP traffic. */
3089    public static final int DATA_ACTIVITY_OUT = 0x00000002;
3090    /** Data connection activity: Currently both sending and receiving
3091     *  IP PPP traffic. */
3092    public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT;
3093    /**
3094     * Data connection is active, but physical link is down
3095     */
3096    public static final int DATA_ACTIVITY_DORMANT = 0x00000004;
3097
3098    /**
3099     * Returns a constant indicating the type of activity on a data connection
3100     * (cellular).
3101     *
3102     * @see #DATA_ACTIVITY_NONE
3103     * @see #DATA_ACTIVITY_IN
3104     * @see #DATA_ACTIVITY_OUT
3105     * @see #DATA_ACTIVITY_INOUT
3106     * @see #DATA_ACTIVITY_DORMANT
3107     */
3108    public int getDataActivity() {
3109        try {
3110            ITelephony telephony = getITelephony();
3111            if (telephony == null)
3112                return DATA_ACTIVITY_NONE;
3113            return telephony.getDataActivity();
3114        } catch (RemoteException ex) {
3115            // the phone process is restarting.
3116            return DATA_ACTIVITY_NONE;
3117        } catch (NullPointerException ex) {
3118          // the phone process is restarting.
3119          return DATA_ACTIVITY_NONE;
3120      }
3121    }
3122
3123    /** Data connection state: Unknown.  Used before we know the state.
3124     * @hide
3125     */
3126    public static final int DATA_UNKNOWN        = -1;
3127    /** Data connection state: Disconnected. IP traffic not available. */
3128    public static final int DATA_DISCONNECTED   = 0;
3129    /** Data connection state: Currently setting up a data connection. */
3130    public static final int DATA_CONNECTING     = 1;
3131    /** Data connection state: Connected. IP traffic should be available. */
3132    public static final int DATA_CONNECTED      = 2;
3133    /** Data connection state: Suspended. The connection is up, but IP
3134     * traffic is temporarily unavailable. For example, in a 2G network,
3135     * data activity may be suspended when a voice call arrives. */
3136    public static final int DATA_SUSPENDED      = 3;
3137
3138    /**
3139     * Returns a constant indicating the current data connection state
3140     * (cellular).
3141     *
3142     * @see #DATA_DISCONNECTED
3143     * @see #DATA_CONNECTING
3144     * @see #DATA_CONNECTED
3145     * @see #DATA_SUSPENDED
3146     */
3147    public int getDataState() {
3148        try {
3149            ITelephony telephony = getITelephony();
3150            if (telephony == null)
3151                return DATA_DISCONNECTED;
3152            return telephony.getDataState();
3153        } catch (RemoteException ex) {
3154            // the phone process is restarting.
3155            return DATA_DISCONNECTED;
3156        } catch (NullPointerException ex) {
3157            return DATA_DISCONNECTED;
3158        }
3159    }
3160
3161   /**
3162    * @hide
3163    */
3164    private ITelephony getITelephony() {
3165        return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
3166    }
3167
3168    /**
3169    * @hide
3170    */
3171    private ITelecomService getTelecomService() {
3172        return ITelecomService.Stub.asInterface(ServiceManager.getService(Context.TELECOM_SERVICE));
3173    }
3174
3175    //
3176    //
3177    // PhoneStateListener
3178    //
3179    //
3180
3181    /**
3182     * Registers a listener object to receive notification of changes
3183     * in specified telephony states.
3184     * <p>
3185     * To register a listener, pass a {@link PhoneStateListener}
3186     * and specify at least one telephony state of interest in
3187     * the events argument.
3188     *
3189     * At registration, and when a specified telephony state
3190     * changes, the telephony manager invokes the appropriate
3191     * callback method on the listener object and passes the
3192     * current (updated) values.
3193     * <p>
3194     * To unregister a listener, pass the listener object and set the
3195     * events argument to
3196     * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0).
3197     *
3198     * @param listener The {@link PhoneStateListener} object to register
3199     *                 (or unregister)
3200     * @param events The telephony state(s) of interest to the listener,
3201     *               as a bitwise-OR combination of {@link PhoneStateListener}
3202     *               LISTEN_ flags.
3203     */
3204    public void listen(PhoneStateListener listener, int events) {
3205        if (mContext == null) return;
3206        try {
3207            Boolean notifyNow = (getITelephony() != null);
3208            // If the listener has not explicitly set the subId (for example, created with the
3209            // default constructor), replace the subId so it will listen to the account the
3210            // telephony manager is created with.
3211            if (listener.mSubId == null) {
3212                listener.mSubId = mSubId;
3213            }
3214            sRegistry.listenForSubscriber(listener.mSubId, getOpPackageName(),
3215                    listener.callback, events, notifyNow);
3216        } catch (RemoteException ex) {
3217            // system process dead
3218        } catch (NullPointerException ex) {
3219            // system process dead
3220        }
3221    }
3222
3223    /**
3224     * Returns the CDMA ERI icon index to display
3225     *
3226     * <p>
3227     * Requires Permission:
3228     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3229     * @hide
3230     */
3231    public int getCdmaEriIconIndex() {
3232        return getCdmaEriIconIndex(getSubId());
3233    }
3234
3235    /**
3236     * Returns the CDMA ERI icon index to display for a subscription
3237     * <p>
3238     * Requires Permission:
3239     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3240     * @hide
3241     */
3242    public int getCdmaEriIconIndex(int subId) {
3243        try {
3244            ITelephony telephony = getITelephony();
3245            if (telephony == null)
3246                return -1;
3247            return telephony.getCdmaEriIconIndexForSubscriber(subId, getOpPackageName());
3248        } catch (RemoteException ex) {
3249            // the phone process is restarting.
3250            return -1;
3251        } catch (NullPointerException ex) {
3252            return -1;
3253        }
3254    }
3255
3256    /**
3257     * Returns the CDMA ERI icon mode,
3258     * 0 - ON
3259     * 1 - FLASHING
3260     *
3261     * <p>
3262     * Requires Permission:
3263     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3264     * @hide
3265     */
3266    public int getCdmaEriIconMode() {
3267        return getCdmaEriIconMode(getSubId());
3268    }
3269
3270    /**
3271     * Returns the CDMA ERI icon mode for a subscription.
3272     * 0 - ON
3273     * 1 - FLASHING
3274     *
3275     * <p>
3276     * Requires Permission:
3277     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3278     * @hide
3279     */
3280    public int getCdmaEriIconMode(int subId) {
3281        try {
3282            ITelephony telephony = getITelephony();
3283            if (telephony == null)
3284                return -1;
3285            return telephony.getCdmaEriIconModeForSubscriber(subId, getOpPackageName());
3286        } catch (RemoteException ex) {
3287            // the phone process is restarting.
3288            return -1;
3289        } catch (NullPointerException ex) {
3290            return -1;
3291        }
3292    }
3293
3294    /**
3295     * Returns the CDMA ERI text,
3296     *
3297     * <p>
3298     * Requires Permission:
3299     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3300     * @hide
3301     */
3302    public String getCdmaEriText() {
3303        return getCdmaEriText(getSubId());
3304    }
3305
3306    /**
3307     * Returns the CDMA ERI text, of a subscription
3308     *
3309     * <p>
3310     * Requires Permission:
3311     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3312     * @hide
3313     */
3314    public String getCdmaEriText(int subId) {
3315        try {
3316            ITelephony telephony = getITelephony();
3317            if (telephony == null)
3318                return null;
3319            return telephony.getCdmaEriTextForSubscriber(subId, getOpPackageName());
3320        } catch (RemoteException ex) {
3321            // the phone process is restarting.
3322            return null;
3323        } catch (NullPointerException ex) {
3324            return null;
3325        }
3326    }
3327
3328    /**
3329     * @return true if the current device is "voice capable".
3330     * <p>
3331     * "Voice capable" means that this device supports circuit-switched
3332     * (i.e. voice) phone calls over the telephony network, and is allowed
3333     * to display the in-call UI while a cellular voice call is active.
3334     * This will be false on "data only" devices which can't make voice
3335     * calls and don't support any in-call UI.
3336     * <p>
3337     * Note: the meaning of this flag is subtly different from the
3338     * PackageManager.FEATURE_TELEPHONY system feature, which is available
3339     * on any device with a telephony radio, even if the device is
3340     * data-only.
3341     */
3342    public boolean isVoiceCapable() {
3343        if (mContext == null) return true;
3344        return mContext.getResources().getBoolean(
3345                com.android.internal.R.bool.config_voice_capable);
3346    }
3347
3348    /**
3349     * @return true if the current device supports sms service.
3350     * <p>
3351     * If true, this means that the device supports both sending and
3352     * receiving sms via the telephony network.
3353     * <p>
3354     * Note: Voicemail waiting sms, cell broadcasting sms, and MMS are
3355     *       disabled when device doesn't support sms.
3356     */
3357    public boolean isSmsCapable() {
3358        if (mContext == null) return true;
3359        return mContext.getResources().getBoolean(
3360                com.android.internal.R.bool.config_sms_capable);
3361    }
3362
3363    /**
3364     * Returns all observed cell information from all radios on the
3365     * device including the primary and neighboring cells. Calling this method does
3366     * not trigger a call to {@link android.telephony.PhoneStateListener#onCellInfoChanged
3367     * onCellInfoChanged()}, or change the rate at which
3368     * {@link android.telephony.PhoneStateListener#onCellInfoChanged
3369     * onCellInfoChanged()} is called.
3370     *
3371     *<p>
3372     * The list can include one or more {@link android.telephony.CellInfoGsm CellInfoGsm},
3373     * {@link android.telephony.CellInfoCdma CellInfoCdma},
3374     * {@link android.telephony.CellInfoLte CellInfoLte}, and
3375     * {@link android.telephony.CellInfoWcdma CellInfoWcdma} objects, in any combination.
3376     * On devices with multiple radios it is typical to see instances of
3377     * one or more of any these in the list. In addition, zero, one, or more
3378     * of the returned objects may be considered registered; that is, their
3379     * {@link android.telephony.CellInfo#isRegistered CellInfo.isRegistered()}
3380     * methods may return true.
3381     *
3382     * <p>This method returns valid data for registered cells on devices with
3383     * {@link android.content.pm.PackageManager#FEATURE_TELEPHONY}. In cases where only
3384     * partial information is available for a particular CellInfo entry, unavailable fields
3385     * will be reported as Integer.MAX_VALUE. All reported cells will include at least a
3386     * valid set of technology-specific identification info and a power level measurement.
3387     *
3388     *<p>
3389     * This method is preferred over using {@link
3390     * android.telephony.TelephonyManager#getCellLocation getCellLocation()}.
3391     * However, for older devices, <code>getAllCellInfo()</code> may return
3392     * null. In these cases, you should call {@link
3393     * android.telephony.TelephonyManager#getCellLocation getCellLocation()}
3394     * instead.
3395     *
3396     * <p>Requires permission:
3397     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}
3398     *
3399     * @return List of {@link android.telephony.CellInfo}; null if cell
3400     * information is unavailable.
3401     *
3402     */
3403    public List<CellInfo> getAllCellInfo() {
3404        try {
3405            ITelephony telephony = getITelephony();
3406            if (telephony == null)
3407                return null;
3408            return telephony.getAllCellInfo(getOpPackageName());
3409        } catch (RemoteException ex) {
3410            return null;
3411        } catch (NullPointerException ex) {
3412            return null;
3413        }
3414    }
3415
3416    /**
3417     * Sets the minimum time in milli-seconds between {@link PhoneStateListener#onCellInfoChanged
3418     * PhoneStateListener.onCellInfoChanged} will be invoked.
3419     *<p>
3420     * The default, 0, means invoke onCellInfoChanged when any of the reported
3421     * information changes. Setting the value to INT_MAX(0x7fffffff) means never issue
3422     * A onCellInfoChanged.
3423     *<p>
3424     * @param rateInMillis the rate
3425     *
3426     * @hide
3427     */
3428    public void setCellInfoListRate(int rateInMillis) {
3429        try {
3430            ITelephony telephony = getITelephony();
3431            if (telephony != null)
3432                telephony.setCellInfoListRate(rateInMillis);
3433        } catch (RemoteException ex) {
3434        } catch (NullPointerException ex) {
3435        }
3436    }
3437
3438    /**
3439     * Returns the MMS user agent.
3440     */
3441    public String getMmsUserAgent() {
3442        if (mContext == null) return null;
3443        return mContext.getResources().getString(
3444                com.android.internal.R.string.config_mms_user_agent);
3445    }
3446
3447    /**
3448     * Returns the MMS user agent profile URL.
3449     */
3450    public String getMmsUAProfUrl() {
3451        if (mContext == null) return null;
3452        return mContext.getResources().getString(
3453                com.android.internal.R.string.config_mms_user_agent_profile_url);
3454    }
3455
3456    /**
3457     * Opens a logical channel to the ICC card.
3458     *
3459     * Input parameters equivalent to TS 27.007 AT+CCHO command.
3460     *
3461     * <p>Requires Permission:
3462     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3463     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3464     *
3465     * @param AID Application id. See ETSI 102.221 and 101.220.
3466     * @return an IccOpenLogicalChannelResponse object.
3467     */
3468    public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
3469        return iccOpenLogicalChannel(getSubId(), AID);
3470    }
3471
3472    /**
3473     * Opens a logical channel to the ICC card.
3474     *
3475     * Input parameters equivalent to TS 27.007 AT+CCHO command.
3476     *
3477     * <p>Requires Permission:
3478     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3479     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3480     *
3481     * @param subId The subscription to use.
3482     * @param AID Application id. See ETSI 102.221 and 101.220.
3483     * @return an IccOpenLogicalChannelResponse object.
3484     * @hide
3485     */
3486    public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
3487        try {
3488            ITelephony telephony = getITelephony();
3489            if (telephony != null)
3490                return telephony.iccOpenLogicalChannel(subId, AID);
3491        } catch (RemoteException ex) {
3492        } catch (NullPointerException ex) {
3493        }
3494        return null;
3495    }
3496
3497    /**
3498     * Closes a previously opened logical channel to the ICC card.
3499     *
3500     * Input parameters equivalent to TS 27.007 AT+CCHC command.
3501     *
3502     * <p>Requires Permission:
3503     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3504     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3505     *
3506     * @param channel is the channel id to be closed as retruned by a successful
3507     *            iccOpenLogicalChannel.
3508     * @return true if the channel was closed successfully.
3509     */
3510    public boolean iccCloseLogicalChannel(int channel) {
3511        return iccCloseLogicalChannel(getSubId(), channel);
3512    }
3513
3514    /**
3515     * Closes a previously opened logical channel to the ICC card.
3516     *
3517     * Input parameters equivalent to TS 27.007 AT+CCHC command.
3518     *
3519     * <p>Requires Permission:
3520     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3521     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3522     *
3523     * @param subId The subscription to use.
3524     * @param channel is the channel id to be closed as retruned by a successful
3525     *            iccOpenLogicalChannel.
3526     * @return true if the channel was closed successfully.
3527     * @hide
3528     */
3529    public boolean iccCloseLogicalChannel(int subId, int channel) {
3530        try {
3531            ITelephony telephony = getITelephony();
3532            if (telephony != null)
3533                return telephony.iccCloseLogicalChannel(subId, channel);
3534        } catch (RemoteException ex) {
3535        } catch (NullPointerException ex) {
3536        }
3537        return false;
3538    }
3539
3540    /**
3541     * Transmit an APDU to the ICC card over a logical channel.
3542     *
3543     * Input parameters equivalent to TS 27.007 AT+CGLA command.
3544     *
3545     * <p>Requires Permission:
3546     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3547     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3548     *
3549     * @param channel is the channel id to be closed as returned by a successful
3550     *            iccOpenLogicalChannel.
3551     * @param cla Class of the APDU command.
3552     * @param instruction Instruction of the APDU command.
3553     * @param p1 P1 value of the APDU command.
3554     * @param p2 P2 value of the APDU command.
3555     * @param p3 P3 value of the APDU command. If p3 is negative a 4 byte APDU
3556     *            is sent to the SIM.
3557     * @param data Data to be sent with the APDU.
3558     * @return The APDU response from the ICC card with the status appended at
3559     *            the end.
3560     */
3561    public String iccTransmitApduLogicalChannel(int channel, int cla,
3562            int instruction, int p1, int p2, int p3, String data) {
3563        return iccTransmitApduLogicalChannel(getSubId(), channel, cla,
3564                    instruction, p1, p2, p3, data);
3565    }
3566
3567    /**
3568     * Transmit an APDU to the ICC card over a logical channel.
3569     *
3570     * Input parameters equivalent to TS 27.007 AT+CGLA command.
3571     *
3572     * <p>Requires Permission:
3573     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3574     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3575     *
3576     * @param subId The subscription to use.
3577     * @param channel is the channel id to be closed as returned by a successful
3578     *            iccOpenLogicalChannel.
3579     * @param cla Class of the APDU command.
3580     * @param instruction Instruction of the APDU command.
3581     * @param p1 P1 value of the APDU command.
3582     * @param p2 P2 value of the APDU command.
3583     * @param p3 P3 value of the APDU command. If p3 is negative a 4 byte APDU
3584     *            is sent to the SIM.
3585     * @param data Data to be sent with the APDU.
3586     * @return The APDU response from the ICC card with the status appended at
3587     *            the end.
3588     * @hide
3589     */
3590    public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
3591            int instruction, int p1, int p2, int p3, String data) {
3592        try {
3593            ITelephony telephony = getITelephony();
3594            if (telephony != null)
3595                return telephony.iccTransmitApduLogicalChannel(subId, channel, cla,
3596                    instruction, p1, p2, p3, data);
3597        } catch (RemoteException ex) {
3598        } catch (NullPointerException ex) {
3599        }
3600        return "";
3601    }
3602
3603    /**
3604     * Transmit an APDU to the ICC card over the basic channel.
3605     *
3606     * Input parameters equivalent to TS 27.007 AT+CSIM command.
3607     *
3608     * <p>Requires Permission:
3609     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3610     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3611     *
3612     * @param cla Class of the APDU command.
3613     * @param instruction Instruction of the APDU command.
3614     * @param p1 P1 value of the APDU command.
3615     * @param p2 P2 value of the APDU command.
3616     * @param p3 P3 value of the APDU command. If p3 is negative a 4 byte APDU
3617     *            is sent to the SIM.
3618     * @param data Data to be sent with the APDU.
3619     * @return The APDU response from the ICC card with the status appended at
3620     *            the end.
3621     */
3622    public String iccTransmitApduBasicChannel(int cla,
3623            int instruction, int p1, int p2, int p3, String data) {
3624        return iccTransmitApduBasicChannel(getSubId(), cla,
3625                    instruction, p1, p2, p3, data);
3626    }
3627
3628    /**
3629     * Transmit an APDU to the ICC card over the basic channel.
3630     *
3631     * Input parameters equivalent to TS 27.007 AT+CSIM command.
3632     *
3633     * <p>Requires Permission:
3634     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3635     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3636     *
3637     * @param subId The subscription to use.
3638     * @param cla Class of the APDU command.
3639     * @param instruction Instruction of the APDU command.
3640     * @param p1 P1 value of the APDU command.
3641     * @param p2 P2 value of the APDU command.
3642     * @param p3 P3 value of the APDU command. If p3 is negative a 4 byte APDU
3643     *            is sent to the SIM.
3644     * @param data Data to be sent with the APDU.
3645     * @return The APDU response from the ICC card with the status appended at
3646     *            the end.
3647     * @hide
3648     */
3649    public String iccTransmitApduBasicChannel(int subId, int cla,
3650            int instruction, int p1, int p2, int p3, String data) {
3651        try {
3652            ITelephony telephony = getITelephony();
3653            if (telephony != null)
3654                return telephony.iccTransmitApduBasicChannel(subId, cla,
3655                    instruction, p1, p2, p3, data);
3656        } catch (RemoteException ex) {
3657        } catch (NullPointerException ex) {
3658        }
3659        return "";
3660    }
3661
3662    /**
3663     * Returns the response APDU for a command APDU sent through SIM_IO.
3664     *
3665     * <p>Requires Permission:
3666     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3667     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3668     *
3669     * @param fileID
3670     * @param command
3671     * @param p1 P1 value of the APDU command.
3672     * @param p2 P2 value of the APDU command.
3673     * @param p3 P3 value of the APDU command.
3674     * @param filePath
3675     * @return The APDU response.
3676     */
3677    public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
3678            String filePath) {
3679        return iccExchangeSimIO(getSubId(), fileID, command, p1, p2, p3, filePath);
3680    }
3681
3682    /**
3683     * Returns the response APDU for a command APDU sent through SIM_IO.
3684     *
3685     * <p>Requires Permission:
3686     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3687     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3688     *
3689     * @param subId The subscription to use.
3690     * @param fileID
3691     * @param command
3692     * @param p1 P1 value of the APDU command.
3693     * @param p2 P2 value of the APDU command.
3694     * @param p3 P3 value of the APDU command.
3695     * @param filePath
3696     * @return The APDU response.
3697     * @hide
3698     */
3699    public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2,
3700            int p3, String filePath) {
3701        try {
3702            ITelephony telephony = getITelephony();
3703            if (telephony != null)
3704                return telephony.iccExchangeSimIO(subId, fileID, command, p1, p2, p3, filePath);
3705        } catch (RemoteException ex) {
3706        } catch (NullPointerException ex) {
3707        }
3708        return null;
3709    }
3710
3711    /**
3712     * Send ENVELOPE to the SIM and return the response.
3713     *
3714     * <p>Requires Permission:
3715     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3716     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3717     *
3718     * @param content String containing SAT/USAT response in hexadecimal
3719     *                format starting with command tag. See TS 102 223 for
3720     *                details.
3721     * @return The APDU response from the ICC card in hexadecimal format
3722     *         with the last 4 bytes being the status word. If the command fails,
3723     *         returns an empty string.
3724     */
3725    public String sendEnvelopeWithStatus(String content) {
3726        return sendEnvelopeWithStatus(getSubId(), content);
3727    }
3728
3729    /**
3730     * Send ENVELOPE to the SIM and return the response.
3731     *
3732     * <p>Requires Permission:
3733     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3734     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3735     *
3736     * @param subId The subscription to use.
3737     * @param content String containing SAT/USAT response in hexadecimal
3738     *                format starting with command tag. See TS 102 223 for
3739     *                details.
3740     * @return The APDU response from the ICC card in hexadecimal format
3741     *         with the last 4 bytes being the status word. If the command fails,
3742     *         returns an empty string.
3743     * @hide
3744     */
3745    public String sendEnvelopeWithStatus(int subId, String content) {
3746        try {
3747            ITelephony telephony = getITelephony();
3748            if (telephony != null)
3749                return telephony.sendEnvelopeWithStatus(subId, content);
3750        } catch (RemoteException ex) {
3751        } catch (NullPointerException ex) {
3752        }
3753        return "";
3754    }
3755
3756    /**
3757     * Read one of the NV items defined in com.android.internal.telephony.RadioNVItems.
3758     * Used for device configuration by some CDMA operators.
3759     * <p>
3760     * Requires Permission:
3761     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3762     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3763     *
3764     * @param itemID the ID of the item to read.
3765     * @return the NV item as a String, or null on any failure.
3766     *
3767     * @hide
3768     */
3769    public String nvReadItem(int itemID) {
3770        try {
3771            ITelephony telephony = getITelephony();
3772            if (telephony != null)
3773                return telephony.nvReadItem(itemID);
3774        } catch (RemoteException ex) {
3775            Rlog.e(TAG, "nvReadItem RemoteException", ex);
3776        } catch (NullPointerException ex) {
3777            Rlog.e(TAG, "nvReadItem NPE", ex);
3778        }
3779        return "";
3780    }
3781
3782    /**
3783     * Write one of the NV items defined in com.android.internal.telephony.RadioNVItems.
3784     * Used for device configuration by some CDMA operators.
3785     * <p>
3786     * Requires Permission:
3787     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3788     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3789     *
3790     * @param itemID the ID of the item to read.
3791     * @param itemValue the value to write, as a String.
3792     * @return true on success; false on any failure.
3793     *
3794     * @hide
3795     */
3796    public boolean nvWriteItem(int itemID, String itemValue) {
3797        try {
3798            ITelephony telephony = getITelephony();
3799            if (telephony != null)
3800                return telephony.nvWriteItem(itemID, itemValue);
3801        } catch (RemoteException ex) {
3802            Rlog.e(TAG, "nvWriteItem RemoteException", ex);
3803        } catch (NullPointerException ex) {
3804            Rlog.e(TAG, "nvWriteItem NPE", ex);
3805        }
3806        return false;
3807    }
3808
3809    /**
3810     * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
3811     * Used for device configuration by some CDMA operators.
3812     * <p>
3813     * Requires Permission:
3814     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3815     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3816     *
3817     * @param preferredRoamingList byte array containing the new PRL.
3818     * @return true on success; false on any failure.
3819     *
3820     * @hide
3821     */
3822    public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
3823        try {
3824            ITelephony telephony = getITelephony();
3825            if (telephony != null)
3826                return telephony.nvWriteCdmaPrl(preferredRoamingList);
3827        } catch (RemoteException ex) {
3828            Rlog.e(TAG, "nvWriteCdmaPrl RemoteException", ex);
3829        } catch (NullPointerException ex) {
3830            Rlog.e(TAG, "nvWriteCdmaPrl NPE", ex);
3831        }
3832        return false;
3833    }
3834
3835    /**
3836     * Perform the specified type of NV config reset. The radio will be taken offline
3837     * and the device must be rebooted after the operation. Used for device
3838     * configuration by some CDMA operators.
3839     * <p>
3840     * Requires Permission:
3841     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
3842     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
3843     *
3844     * @param resetType reset type: 1: reload NV reset, 2: erase NV reset, 3: factory NV reset
3845     * @return true on success; false on any failure.
3846     *
3847     * @hide
3848     */
3849    public boolean nvResetConfig(int resetType) {
3850        try {
3851            ITelephony telephony = getITelephony();
3852            if (telephony != null)
3853                return telephony.nvResetConfig(resetType);
3854        } catch (RemoteException ex) {
3855            Rlog.e(TAG, "nvResetConfig RemoteException", ex);
3856        } catch (NullPointerException ex) {
3857            Rlog.e(TAG, "nvResetConfig NPE", ex);
3858        }
3859        return false;
3860    }
3861
3862    /**
3863     * Return an appropriate subscription ID for any situation.
3864     *
3865     * If this object has been created with {@link #createForSubscriptionId}, then the provided
3866     * subId is returned. Otherwise, the default subId will be returned.
3867     */
3868    private int getSubId() {
3869      if (mSubId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
3870        return getDefaultSubscription();
3871      }
3872      return mSubId;
3873    }
3874
3875    /**
3876     * Returns Default subscription.
3877     */
3878    private static int getDefaultSubscription() {
3879        return SubscriptionManager.getDefaultSubscriptionId();
3880    }
3881
3882    /**
3883     * Returns Default phone.
3884     */
3885    private static int getDefaultPhone() {
3886        return SubscriptionManager.getPhoneId(SubscriptionManager.getDefaultSubscriptionId());
3887    }
3888
3889    /** {@hide} */
3890    public int getDefaultSim() {
3891        return SubscriptionManager.getSlotId(SubscriptionManager.getDefaultSubscriptionId());
3892    }
3893
3894    /**
3895     * Sets the telephony property with the value specified.
3896     *
3897     * @hide
3898     */
3899    public static void setTelephonyProperty(int phoneId, String property, String value) {
3900        String propVal = "";
3901        String p[] = null;
3902        String prop = SystemProperties.get(property);
3903
3904        if (value == null) {
3905            value = "";
3906        }
3907
3908        if (prop != null) {
3909            p = prop.split(",");
3910        }
3911
3912        if (!SubscriptionManager.isValidPhoneId(phoneId)) {
3913            Rlog.d(TAG, "setTelephonyProperty: invalid phoneId=" + phoneId +
3914                    " property=" + property + " value: " + value + " prop=" + prop);
3915            return;
3916        }
3917
3918        for (int i = 0; i < phoneId; i++) {
3919            String str = "";
3920            if ((p != null) && (i < p.length)) {
3921                str = p[i];
3922            }
3923            propVal = propVal + str + ",";
3924        }
3925
3926        propVal = propVal + value;
3927        if (p != null) {
3928            for (int i = phoneId + 1; i < p.length; i++) {
3929                propVal = propVal + "," + p[i];
3930            }
3931        }
3932
3933        if (property.length() > SystemProperties.PROP_NAME_MAX
3934                || propVal.length() > SystemProperties.PROP_VALUE_MAX) {
3935            Rlog.d(TAG, "setTelephonyProperty: property to long phoneId=" + phoneId +
3936                    " property=" + property + " value: " + value + " propVal=" + propVal);
3937            return;
3938        }
3939
3940        Rlog.d(TAG, "setTelephonyProperty: success phoneId=" + phoneId +
3941                " property=" + property + " value: " + value + " propVal=" + propVal);
3942        SystemProperties.set(property, propVal);
3943    }
3944
3945    /**
3946     * Convenience function for retrieving a value from the secure settings
3947     * value list as an integer.  Note that internally setting values are
3948     * always stored as strings; this function converts the string to an
3949     * integer for you.
3950     * <p>
3951     * This version does not take a default value.  If the setting has not
3952     * been set, or the string value is not a number,
3953     * it throws {@link SettingNotFoundException}.
3954     *
3955     * @param cr The ContentResolver to access.
3956     * @param name The name of the setting to retrieve.
3957     * @param index The index of the list
3958     *
3959     * @throws SettingNotFoundException Thrown if a setting by the given
3960     * name can't be found or the setting value is not an integer.
3961     *
3962     * @return The value at the given index of settings.
3963     * @hide
3964     */
3965    public static int getIntAtIndex(android.content.ContentResolver cr,
3966            String name, int index)
3967            throws android.provider.Settings.SettingNotFoundException {
3968        String v = android.provider.Settings.Global.getString(cr, name);
3969        if (v != null) {
3970            String valArray[] = v.split(",");
3971            if ((index >= 0) && (index < valArray.length) && (valArray[index] != null)) {
3972                try {
3973                    return Integer.parseInt(valArray[index]);
3974                } catch (NumberFormatException e) {
3975                    //Log.e(TAG, "Exception while parsing Integer: ", e);
3976                }
3977            }
3978        }
3979        throw new android.provider.Settings.SettingNotFoundException(name);
3980    }
3981
3982    /**
3983     * Convenience function for updating settings value as coma separated
3984     * integer values. This will either create a new entry in the table if the
3985     * given name does not exist, or modify the value of the existing row
3986     * with that name.  Note that internally setting values are always
3987     * stored as strings, so this function converts the given value to a
3988     * string before storing it.
3989     *
3990     * @param cr The ContentResolver to access.
3991     * @param name The name of the setting to modify.
3992     * @param index The index of the list
3993     * @param value The new value for the setting to be added to the list.
3994     * @return true if the value was set, false on database errors
3995     * @hide
3996     */
3997    public static boolean putIntAtIndex(android.content.ContentResolver cr,
3998            String name, int index, int value) {
3999        String data = "";
4000        String valArray[] = null;
4001        String v = android.provider.Settings.Global.getString(cr, name);
4002
4003        if (index == Integer.MAX_VALUE) {
4004            throw new RuntimeException("putIntAtIndex index == MAX_VALUE index=" + index);
4005        }
4006        if (index < 0) {
4007            throw new RuntimeException("putIntAtIndex index < 0 index=" + index);
4008        }
4009        if (v != null) {
4010            valArray = v.split(",");
4011        }
4012
4013        // Copy the elements from valArray till index
4014        for (int i = 0; i < index; i++) {
4015            String str = "";
4016            if ((valArray != null) && (i < valArray.length)) {
4017                str = valArray[i];
4018            }
4019            data = data + str + ",";
4020        }
4021
4022        data = data + value;
4023
4024        // Copy the remaining elements from valArray if any.
4025        if (valArray != null) {
4026            for (int i = index+1; i < valArray.length; i++) {
4027                data = data + "," + valArray[i];
4028            }
4029        }
4030        return android.provider.Settings.Global.putString(cr, name, data);
4031    }
4032
4033    /**
4034     * Gets the telephony property.
4035     *
4036     * @hide
4037     */
4038    public static String getTelephonyProperty(int phoneId, String property, String defaultVal) {
4039        String propVal = null;
4040        String prop = SystemProperties.get(property);
4041        if ((prop != null) && (prop.length() > 0)) {
4042            String values[] = prop.split(",");
4043            if ((phoneId >= 0) && (phoneId < values.length) && (values[phoneId] != null)) {
4044                propVal = values[phoneId];
4045            }
4046        }
4047        return propVal == null ? defaultVal : propVal;
4048    }
4049
4050    /** @hide */
4051    public int getSimCount() {
4052        // FIXME Need to get it from Telephony Dev Controller when that gets implemented!
4053        // and then this method shouldn't be used at all!
4054        if(isMultiSimEnabled()) {
4055            return 2;
4056        } else {
4057            return 1;
4058        }
4059    }
4060
4061    /**
4062     * Returns the IMS Service Table (IST) that was loaded from the ISIM.
4063     * @return IMS Service Table or null if not present or not loaded
4064     * @hide
4065     */
4066    public String getIsimIst() {
4067        try {
4068            IPhoneSubInfo info = getSubscriberInfo();
4069            if (info == null)
4070                return null;
4071            return info.getIsimIst();
4072        } catch (RemoteException ex) {
4073            return null;
4074        } catch (NullPointerException ex) {
4075            // This could happen before phone restarts due to crashing
4076            return null;
4077        }
4078    }
4079
4080    /**
4081     * Returns the IMS Proxy Call Session Control Function(PCSCF) that were loaded from the ISIM.
4082     * @return an array of PCSCF strings with one PCSCF per string, or null if
4083     *         not present or not loaded
4084     * @hide
4085     */
4086    public String[] getIsimPcscf() {
4087        try {
4088            IPhoneSubInfo info = getSubscriberInfo();
4089            if (info == null)
4090                return null;
4091            return info.getIsimPcscf();
4092        } catch (RemoteException ex) {
4093            return null;
4094        } catch (NullPointerException ex) {
4095            // This could happen before phone restarts due to crashing
4096            return null;
4097        }
4098    }
4099
4100    /**
4101     * Returns the response of ISIM Authetification through RIL.
4102     * Returns null if the Authentification hasn't been successed or isn't present iphonesubinfo.
4103     * @return the response of ISIM Authetification, or null if not available
4104     * @hide
4105     * @deprecated
4106     * @see getIccAuthentication with appType=PhoneConstants.APPTYPE_ISIM
4107     */
4108    public String getIsimChallengeResponse(String nonce){
4109        try {
4110            IPhoneSubInfo info = getSubscriberInfo();
4111            if (info == null)
4112                return null;
4113            return info.getIsimChallengeResponse(nonce);
4114        } catch (RemoteException ex) {
4115            return null;
4116        } catch (NullPointerException ex) {
4117            // This could happen before phone restarts due to crashing
4118            return null;
4119        }
4120    }
4121
4122    // ICC SIM Application Types
4123    /** UICC application type is SIM */
4124    public static final int APPTYPE_SIM = PhoneConstants.APPTYPE_SIM;
4125    /** UICC application type is USIM */
4126    public static final int APPTYPE_USIM = PhoneConstants.APPTYPE_USIM;
4127    /** UICC application type is RUIM */
4128    public static final int APPTYPE_RUIM = PhoneConstants.APPTYPE_RUIM;
4129    /** UICC application type is CSIM */
4130    public static final int APPTYPE_CSIM = PhoneConstants.APPTYPE_CSIM;
4131    /** UICC application type is ISIM */
4132    public static final int APPTYPE_ISIM = PhoneConstants.APPTYPE_ISIM;
4133    // authContext (parameter P2) when doing UICC challenge,
4134    // per 3GPP TS 31.102 (Section 7.1.2)
4135    /** Authentication type for UICC challenge is EAP SIM. See RFC 4186 for details. */
4136    public static final int AUTHTYPE_EAP_SIM = PhoneConstants.AUTH_CONTEXT_EAP_SIM;
4137    /** Authentication type for UICC challenge is EAP AKA. See RFC 4187 for details. */
4138    public static final int AUTHTYPE_EAP_AKA = PhoneConstants.AUTH_CONTEXT_EAP_AKA;
4139
4140    /**
4141     * Returns the response of authentication for the default subscription.
4142     * Returns null if the authentication hasn't been successful
4143     *
4144     * <p>Requires that the calling app has carrier privileges or READ_PRIVILEGED_PHONE_STATE
4145     * permission.
4146     *
4147     * @param appType the icc application type, like {@link #APPTYPE_USIM}
4148     * @param authType the authentication type, {@link #AUTHTYPE_EAP_AKA} or
4149     * {@link #AUTHTYPE_EAP_SIM}
4150     * @param data authentication challenge data, base64 encoded.
4151     * See 3GPP TS 31.102 7.1.2 for more details.
4152     * @return the response of authentication, or null if not available
4153     *
4154     * @see #hasCarrierPrivileges
4155     */
4156    public String getIccAuthentication(int appType, int authType, String data) {
4157        return getIccAuthentication(getSubId(), appType, authType, data);
4158    }
4159
4160    /**
4161     * Returns the response of USIM Authentication for specified subId.
4162     * Returns null if the authentication hasn't been successful
4163     *
4164     * <p>Requires that the calling app has carrier privileges.
4165     *
4166     * @param subId subscription ID used for authentication
4167     * @param appType the icc application type, like {@link #APPTYPE_USIM}
4168     * @param authType the authentication type, {@link #AUTHTYPE_EAP_AKA} or
4169     * {@link #AUTHTYPE_EAP_SIM}
4170     * @param data authentication challenge data, base64 encoded.
4171     * See 3GPP TS 31.102 7.1.2 for more details.
4172     * @return the response of authentication, or null if not available
4173     *
4174     * @see #hasCarrierPrivileges
4175     * @hide
4176     */
4177    public String getIccAuthentication(int subId, int appType, int authType, String data) {
4178        try {
4179            IPhoneSubInfo info = getSubscriberInfo();
4180            if (info == null)
4181                return null;
4182            return info.getIccSimChallengeResponse(subId, appType, authType, data);
4183        } catch (RemoteException ex) {
4184            return null;
4185        } catch (NullPointerException ex) {
4186            // This could happen before phone starts
4187            return null;
4188        }
4189    }
4190
4191    /**
4192     * Get P-CSCF address from PCO after data connection is established or modified.
4193     * @param apnType the apnType, "ims" for IMS APN, "emergency" for EMERGENCY APN
4194     * @return array of P-CSCF address
4195     * @hide
4196     */
4197    public String[] getPcscfAddress(String apnType) {
4198        try {
4199            ITelephony telephony = getITelephony();
4200            if (telephony == null)
4201                return new String[0];
4202            return telephony.getPcscfAddress(apnType, getOpPackageName());
4203        } catch (RemoteException e) {
4204            return new String[0];
4205        }
4206    }
4207
4208    /** @hide */
4209    @IntDef({ImsFeature.EMERGENCY_MMTEL, ImsFeature.MMTEL, ImsFeature.RCS})
4210    @Retention(RetentionPolicy.SOURCE)
4211    public @interface Feature {}
4212
4213    /**
4214     * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
4215     * feature or {@link null} if the service is not available. If an ImsServiceController is
4216     * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
4217     * feature updates.
4218     * @param slotId The SIM slot that we are requesting the {@link IImsServiceController} for.
4219     * @param feature The IMS Feature we are requesting, corresponding to {@link ImsFeature}.
4220     * @param callback Listener that will send updates to ImsManager when there are updates to
4221     * ImsServiceController.
4222     * @return {@link IImsServiceController} interface for the feature specified or {@link null} if
4223     * it is unavailable.
4224     * @hide
4225     */
4226    public IImsServiceController getImsServiceControllerAndListen(int slotId, @Feature int feature,
4227            IImsServiceFeatureListener callback) {
4228        try {
4229            ITelephony telephony = getITelephony();
4230            if (telephony != null) {
4231                return telephony.getImsServiceControllerAndListen(slotId, feature, callback);
4232            }
4233        } catch (RemoteException e) {
4234            Rlog.e(TAG, "getImsServiceControllerAndListen, RemoteException: " + e.getMessage());
4235        }
4236        return null;
4237    }
4238
4239    /**
4240     * Set IMS registration state
4241     *
4242     * @param Registration state
4243     * @hide
4244     */
4245    public void setImsRegistrationState(boolean registered) {
4246        try {
4247            ITelephony telephony = getITelephony();
4248            if (telephony != null)
4249                telephony.setImsRegistrationState(registered);
4250        } catch (RemoteException e) {
4251        }
4252    }
4253
4254    /**
4255     * Get the preferred network type.
4256     * Used for device configuration by some CDMA operators.
4257     * <p>
4258     * Requires Permission:
4259     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
4260     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
4261     *
4262     * @return the preferred network type, defined in RILConstants.java.
4263     * @hide
4264     */
4265    public int getPreferredNetworkType(int subId) {
4266        try {
4267            ITelephony telephony = getITelephony();
4268            if (telephony != null)
4269                return telephony.getPreferredNetworkType(subId);
4270        } catch (RemoteException ex) {
4271            Rlog.e(TAG, "getPreferredNetworkType RemoteException", ex);
4272        } catch (NullPointerException ex) {
4273            Rlog.e(TAG, "getPreferredNetworkType NPE", ex);
4274        }
4275        return -1;
4276    }
4277
4278    /**
4279     * Sets the network selection mode to automatic.
4280     * <p>
4281     * Requires Permission:
4282     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
4283     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
4284     *
4285     * @hide
4286     * TODO: Add an overload that takes no args.
4287     */
4288    public void setNetworkSelectionModeAutomatic(int subId) {
4289        try {
4290            ITelephony telephony = getITelephony();
4291            if (telephony != null)
4292                telephony.setNetworkSelectionModeAutomatic(subId);
4293        } catch (RemoteException ex) {
4294            Rlog.e(TAG, "setNetworkSelectionModeAutomatic RemoteException", ex);
4295        } catch (NullPointerException ex) {
4296            Rlog.e(TAG, "setNetworkSelectionModeAutomatic NPE", ex);
4297        }
4298    }
4299
4300    /**
4301     * Perform a radio scan and return the list of avialble networks.
4302     *
4303     * The return value is a list of the OperatorInfo of the networks found. Note that this
4304     * scan can take a long time (sometimes minutes) to happen.
4305     *
4306     * <p>
4307     * Requires Permission:
4308     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
4309     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
4310     *
4311     * @hide
4312     * TODO: Add an overload that takes no args.
4313     */
4314    public CellNetworkScanResult getCellNetworkScanResults(int subId) {
4315        try {
4316            ITelephony telephony = getITelephony();
4317            if (telephony != null)
4318                return telephony.getCellNetworkScanResults(subId);
4319        } catch (RemoteException ex) {
4320            Rlog.e(TAG, "getCellNetworkScanResults RemoteException", ex);
4321        } catch (NullPointerException ex) {
4322            Rlog.e(TAG, "getCellNetworkScanResults NPE", ex);
4323        }
4324        return null;
4325    }
4326
4327    /**
4328     * Ask the radio to connect to the input network and change selection mode to manual.
4329     *
4330     * <p>
4331     * Requires Permission:
4332     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
4333     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
4334     *
4335     * @hide
4336     * TODO: Add an overload that takes no args.
4337     */
4338    public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
4339            boolean persistSelection) {
4340        try {
4341            ITelephony telephony = getITelephony();
4342            if (telephony != null)
4343                return telephony.setNetworkSelectionModeManual(subId, operator, persistSelection);
4344        } catch (RemoteException ex) {
4345            Rlog.e(TAG, "setNetworkSelectionModeManual RemoteException", ex);
4346        } catch (NullPointerException ex) {
4347            Rlog.e(TAG, "setNetworkSelectionModeManual NPE", ex);
4348        }
4349        return false;
4350    }
4351
4352    /**
4353     * Set the preferred network type.
4354     * Used for device configuration by some CDMA operators.
4355     * <p>
4356     * Requires Permission:
4357     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
4358     * Or the calling app has carrier privileges. @see #hasCarrierPrivileges
4359     *
4360     * @param subId the id of the subscription to set the preferred network type for.
4361     * @param networkType the preferred network type, defined in RILConstants.java.
4362     * @return true on success; false on any failure.
4363     * @hide
4364     */
4365    public boolean setPreferredNetworkType(int subId, int networkType) {
4366        try {
4367            ITelephony telephony = getITelephony();
4368            if (telephony != null)
4369                return telephony.setPreferredNetworkType(subId, networkType);
4370        } catch (RemoteException ex) {
4371            Rlog.e(TAG, "setPreferredNetworkType RemoteException", ex);
4372        } catch (NullPointerException ex) {
4373            Rlog.e(TAG, "setPreferredNetworkType NPE", ex);
4374        }
4375        return false;
4376    }
4377
4378    /**
4379     * Set the preferred network type to global mode which includes LTE, CDMA, EvDo and GSM/WCDMA.
4380     *
4381     * <p>
4382     * Requires that the calling app has carrier privileges.
4383     * @see #hasCarrierPrivileges
4384     *
4385     * @return true on success; false on any failure.
4386     */
4387    public boolean setPreferredNetworkTypeToGlobal() {
4388        return setPreferredNetworkTypeToGlobal(getSubId());
4389    }
4390
4391    /**
4392     * Set the preferred network type to global mode which includes LTE, CDMA, EvDo and GSM/WCDMA.
4393     *
4394     * <p>
4395     * Requires that the calling app has carrier privileges.
4396     * @see #hasCarrierPrivileges
4397     *
4398     * @return true on success; false on any failure.
4399     * @hide
4400     */
4401    public boolean setPreferredNetworkTypeToGlobal(int subId) {
4402        return setPreferredNetworkType(subId, RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA);
4403    }
4404
4405    /**
4406     * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
4407     * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
4408     * tethering.
4409     *
4410     * @return 0: Not required. 1: required. 2: Not set.
4411     * @hide
4412     */
4413    public int getTetherApnRequired() {
4414        try {
4415            ITelephony telephony = getITelephony();
4416            if (telephony != null)
4417                return telephony.getTetherApnRequired();
4418        } catch (RemoteException ex) {
4419            Rlog.e(TAG, "hasMatchedTetherApnSetting RemoteException", ex);
4420        } catch (NullPointerException ex) {
4421            Rlog.e(TAG, "hasMatchedTetherApnSetting NPE", ex);
4422        }
4423        return 2;
4424    }
4425
4426
4427    /**
4428     * Values used to return status for hasCarrierPrivileges call.
4429     */
4430    /** @hide */ @SystemApi
4431    public static final int CARRIER_PRIVILEGE_STATUS_HAS_ACCESS = 1;
4432    /** @hide */ @SystemApi
4433    public static final int CARRIER_PRIVILEGE_STATUS_NO_ACCESS = 0;
4434    /** @hide */ @SystemApi
4435    public static final int CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED = -1;
4436    /** @hide */ @SystemApi
4437    public static final int CARRIER_PRIVILEGE_STATUS_ERROR_LOADING_RULES = -2;
4438
4439    /**
4440     * Has the calling application been granted carrier privileges by the carrier.
4441     *
4442     * If any of the packages in the calling UID has carrier privileges, the
4443     * call will return true. This access is granted by the owner of the UICC
4444     * card and does not depend on the registered carrier.
4445     *
4446     * @return true if the app has carrier privileges.
4447     */
4448    public boolean hasCarrierPrivileges() {
4449        return hasCarrierPrivileges(getSubId());
4450    }
4451
4452    /**
4453     * Has the calling application been granted carrier privileges by the carrier.
4454     *
4455     * If any of the packages in the calling UID has carrier privileges, the
4456     * call will return true. This access is granted by the owner of the UICC
4457     * card and does not depend on the registered carrier.
4458     *
4459     * @param subId The subscription to use.
4460     * @return true if the app has carrier privileges.
4461     * @hide
4462     */
4463    public boolean hasCarrierPrivileges(int subId) {
4464        try {
4465            ITelephony telephony = getITelephony();
4466            if (telephony != null) {
4467                return telephony.getCarrierPrivilegeStatus(mSubId) ==
4468                    CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
4469            }
4470        } catch (RemoteException ex) {
4471            Rlog.e(TAG, "hasCarrierPrivileges RemoteException", ex);
4472        } catch (NullPointerException ex) {
4473            Rlog.e(TAG, "hasCarrierPrivileges NPE", ex);
4474        }
4475        return false;
4476    }
4477
4478    /**
4479     * Override the branding for the current ICCID.
4480     *
4481     * Once set, whenever the SIM is present in the device, the service
4482     * provider name (SPN) and the operator name will both be replaced by the
4483     * brand value input. To unset the value, the same function should be
4484     * called with a null brand value.
4485     *
4486     * <p>Requires that the calling app has carrier privileges.
4487     * @see #hasCarrierPrivileges
4488     *
4489     * @param brand The brand name to display/set.
4490     * @return true if the operation was executed correctly.
4491     */
4492    public boolean setOperatorBrandOverride(String brand) {
4493        return setOperatorBrandOverride(getSubId(), brand);
4494    }
4495
4496    /**
4497     * Override the branding for the current ICCID.
4498     *
4499     * Once set, whenever the SIM is present in the device, the service
4500     * provider name (SPN) and the operator name will both be replaced by the
4501     * brand value input. To unset the value, the same function should be
4502     * called with a null brand value.
4503     *
4504     * <p>Requires that the calling app has carrier privileges.
4505     * @see #hasCarrierPrivileges
4506     *
4507     * @param subId The subscription to use.
4508     * @param brand The brand name to display/set.
4509     * @return true if the operation was executed correctly.
4510     * @hide
4511     */
4512    public boolean setOperatorBrandOverride(int subId, String brand) {
4513        try {
4514            ITelephony telephony = getITelephony();
4515            if (telephony != null)
4516                return telephony.setOperatorBrandOverride(subId, brand);
4517        } catch (RemoteException ex) {
4518            Rlog.e(TAG, "setOperatorBrandOverride RemoteException", ex);
4519        } catch (NullPointerException ex) {
4520            Rlog.e(TAG, "setOperatorBrandOverride NPE", ex);
4521        }
4522        return false;
4523    }
4524
4525    /**
4526     * Override the roaming preference for the current ICCID.
4527     *
4528     * Using this call, the carrier app (see #hasCarrierPrivileges) can override
4529     * the platform's notion of a network operator being considered roaming or not.
4530     * The change only affects the ICCID that was active when this call was made.
4531     *
4532     * If null is passed as any of the input, the corresponding value is deleted.
4533     *
4534     * <p>Requires that the caller have carrier privilege. See #hasCarrierPrivileges.
4535     *
4536     * @param gsmRoamingList - List of MCCMNCs to be considered roaming for 3GPP RATs.
4537     * @param gsmNonRoamingList - List of MCCMNCs to be considered not roaming for 3GPP RATs.
4538     * @param cdmaRoamingList - List of SIDs to be considered roaming for 3GPP2 RATs.
4539     * @param cdmaNonRoamingList - List of SIDs to be considered not roaming for 3GPP2 RATs.
4540     * @return true if the operation was executed correctly.
4541     *
4542     * @hide
4543     */
4544    public boolean setRoamingOverride(List<String> gsmRoamingList,
4545            List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
4546            List<String> cdmaNonRoamingList) {
4547        return setRoamingOverride(getSubId(), gsmRoamingList, gsmNonRoamingList,
4548                cdmaRoamingList, cdmaNonRoamingList);
4549    }
4550
4551    /**
4552     * Override the roaming preference for the current ICCID.
4553     *
4554     * Using this call, the carrier app (see #hasCarrierPrivileges) can override
4555     * the platform's notion of a network operator being considered roaming or not.
4556     * The change only affects the ICCID that was active when this call was made.
4557     *
4558     * If null is passed as any of the input, the corresponding value is deleted.
4559     *
4560     * <p>Requires that the caller have carrier privilege. See #hasCarrierPrivileges.
4561     *
4562     * @param subId for which the roaming overrides apply.
4563     * @param gsmRoamingList - List of MCCMNCs to be considered roaming for 3GPP RATs.
4564     * @param gsmNonRoamingList - List of MCCMNCs to be considered not roaming for 3GPP RATs.
4565     * @param cdmaRoamingList - List of SIDs to be considered roaming for 3GPP2 RATs.
4566     * @param cdmaNonRoamingList - List of SIDs to be considered not roaming for 3GPP2 RATs.
4567     * @return true if the operation was executed correctly.
4568     *
4569     * @hide
4570     */
4571    public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
4572            List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
4573            List<String> cdmaNonRoamingList) {
4574        try {
4575            ITelephony telephony = getITelephony();
4576            if (telephony != null)
4577                return telephony.setRoamingOverride(subId, gsmRoamingList, gsmNonRoamingList,
4578                        cdmaRoamingList, cdmaNonRoamingList);
4579        } catch (RemoteException ex) {
4580            Rlog.e(TAG, "setRoamingOverride RemoteException", ex);
4581        } catch (NullPointerException ex) {
4582            Rlog.e(TAG, "setRoamingOverride NPE", ex);
4583        }
4584        return false;
4585    }
4586
4587    /**
4588     * Expose the rest of ITelephony to @SystemApi
4589     */
4590
4591    /** @hide */
4592    @SystemApi
4593    public String getCdmaMdn() {
4594        return getCdmaMdn(getSubId());
4595    }
4596
4597    /** @hide */
4598    @SystemApi
4599    public String getCdmaMdn(int subId) {
4600        try {
4601            ITelephony telephony = getITelephony();
4602            if (telephony == null)
4603                return null;
4604            return telephony.getCdmaMdn(subId);
4605        } catch (RemoteException ex) {
4606            return null;
4607        } catch (NullPointerException ex) {
4608            return null;
4609        }
4610    }
4611
4612    /** @hide */
4613    @SystemApi
4614    public String getCdmaMin() {
4615        return getCdmaMin(getSubId());
4616    }
4617
4618    /** @hide */
4619    @SystemApi
4620    public String getCdmaMin(int subId) {
4621        try {
4622            ITelephony telephony = getITelephony();
4623            if (telephony == null)
4624                return null;
4625            return telephony.getCdmaMin(subId);
4626        } catch (RemoteException ex) {
4627            return null;
4628        } catch (NullPointerException ex) {
4629            return null;
4630        }
4631    }
4632
4633    /** @hide */
4634    @SystemApi
4635    public int checkCarrierPrivilegesForPackage(String pkgName) {
4636        try {
4637            ITelephony telephony = getITelephony();
4638            if (telephony != null)
4639                return telephony.checkCarrierPrivilegesForPackage(pkgName);
4640        } catch (RemoteException ex) {
4641            Rlog.e(TAG, "checkCarrierPrivilegesForPackage RemoteException", ex);
4642        } catch (NullPointerException ex) {
4643            Rlog.e(TAG, "checkCarrierPrivilegesForPackage NPE", ex);
4644        }
4645        return CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
4646    }
4647
4648    /** @hide */
4649    @SystemApi
4650    public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
4651        try {
4652            ITelephony telephony = getITelephony();
4653            if (telephony != null)
4654                return telephony.checkCarrierPrivilegesForPackageAnyPhone(pkgName);
4655        } catch (RemoteException ex) {
4656            Rlog.e(TAG, "checkCarrierPrivilegesForPackageAnyPhone RemoteException", ex);
4657        } catch (NullPointerException ex) {
4658            Rlog.e(TAG, "checkCarrierPrivilegesForPackageAnyPhone NPE", ex);
4659        }
4660        return CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
4661    }
4662
4663    /** @hide */
4664    @SystemApi
4665    public List<String> getCarrierPackageNamesForIntent(Intent intent) {
4666        return getCarrierPackageNamesForIntentAndPhone(intent, getDefaultPhone());
4667    }
4668
4669    /** @hide */
4670    @SystemApi
4671    public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
4672        try {
4673            ITelephony telephony = getITelephony();
4674            if (telephony != null)
4675                return telephony.getCarrierPackageNamesForIntentAndPhone(intent, phoneId);
4676        } catch (RemoteException ex) {
4677            Rlog.e(TAG, "getCarrierPackageNamesForIntentAndPhone RemoteException", ex);
4678        } catch (NullPointerException ex) {
4679            Rlog.e(TAG, "getCarrierPackageNamesForIntentAndPhone NPE", ex);
4680        }
4681        return null;
4682    }
4683
4684    /** @hide */
4685    public List<String> getPackagesWithCarrierPrivileges() {
4686        try {
4687            ITelephony telephony = getITelephony();
4688            if (telephony != null) {
4689                return telephony.getPackagesWithCarrierPrivileges();
4690            }
4691        } catch (RemoteException ex) {
4692            Rlog.e(TAG, "getPackagesWithCarrierPrivileges RemoteException", ex);
4693        } catch (NullPointerException ex) {
4694            Rlog.e(TAG, "getPackagesWithCarrierPrivileges NPE", ex);
4695        }
4696        return Collections.EMPTY_LIST;
4697    }
4698
4699    /** @hide */
4700    @SystemApi
4701    public void dial(String number) {
4702        try {
4703            ITelephony telephony = getITelephony();
4704            if (telephony != null)
4705                telephony.dial(number);
4706        } catch (RemoteException e) {
4707            Log.e(TAG, "Error calling ITelephony#dial", e);
4708        }
4709    }
4710
4711    /** @hide */
4712    @SystemApi
4713    public void call(String callingPackage, String number) {
4714        try {
4715            ITelephony telephony = getITelephony();
4716            if (telephony != null)
4717                telephony.call(callingPackage, number);
4718        } catch (RemoteException e) {
4719            Log.e(TAG, "Error calling ITelephony#call", e);
4720        }
4721    }
4722
4723    /** @hide */
4724    @SystemApi
4725    public boolean endCall() {
4726        try {
4727            ITelephony telephony = getITelephony();
4728            if (telephony != null)
4729                return telephony.endCall();
4730        } catch (RemoteException e) {
4731            Log.e(TAG, "Error calling ITelephony#endCall", e);
4732        }
4733        return false;
4734    }
4735
4736    /** @hide */
4737    @SystemApi
4738    public void answerRingingCall() {
4739        try {
4740            ITelephony telephony = getITelephony();
4741            if (telephony != null)
4742                telephony.answerRingingCall();
4743        } catch (RemoteException e) {
4744            Log.e(TAG, "Error calling ITelephony#answerRingingCall", e);
4745        }
4746    }
4747
4748    /** @hide */
4749    @SystemApi
4750    public void silenceRinger() {
4751        try {
4752            getTelecomService().silenceRinger(getOpPackageName());
4753        } catch (RemoteException e) {
4754            Log.e(TAG, "Error calling ITelecomService#silenceRinger", e);
4755        }
4756    }
4757
4758    /** @hide */
4759    @SystemApi
4760    public boolean isOffhook() {
4761        try {
4762            ITelephony telephony = getITelephony();
4763            if (telephony != null)
4764                return telephony.isOffhook(getOpPackageName());
4765        } catch (RemoteException e) {
4766            Log.e(TAG, "Error calling ITelephony#isOffhook", e);
4767        }
4768        return false;
4769    }
4770
4771    /** @hide */
4772    @SystemApi
4773    public boolean isRinging() {
4774        try {
4775            ITelephony telephony = getITelephony();
4776            if (telephony != null)
4777                return telephony.isRinging(getOpPackageName());
4778        } catch (RemoteException e) {
4779            Log.e(TAG, "Error calling ITelephony#isRinging", e);
4780        }
4781        return false;
4782    }
4783
4784    /** @hide */
4785    @SystemApi
4786    public boolean isIdle() {
4787        try {
4788            ITelephony telephony = getITelephony();
4789            if (telephony != null)
4790                return telephony.isIdle(getOpPackageName());
4791        } catch (RemoteException e) {
4792            Log.e(TAG, "Error calling ITelephony#isIdle", e);
4793        }
4794        return true;
4795    }
4796
4797    /** @hide */
4798    @SystemApi
4799    public boolean isRadioOn() {
4800        try {
4801            ITelephony telephony = getITelephony();
4802            if (telephony != null)
4803                return telephony.isRadioOn(getOpPackageName());
4804        } catch (RemoteException e) {
4805            Log.e(TAG, "Error calling ITelephony#isRadioOn", e);
4806        }
4807        return false;
4808    }
4809
4810    /** @hide */
4811    @SystemApi
4812    public boolean supplyPin(String pin) {
4813        try {
4814            ITelephony telephony = getITelephony();
4815            if (telephony != null)
4816                return telephony.supplyPin(pin);
4817        } catch (RemoteException e) {
4818            Log.e(TAG, "Error calling ITelephony#supplyPin", e);
4819        }
4820        return false;
4821    }
4822
4823    /** @hide */
4824    @SystemApi
4825    public boolean supplyPuk(String puk, String pin) {
4826        try {
4827            ITelephony telephony = getITelephony();
4828            if (telephony != null)
4829                return telephony.supplyPuk(puk, pin);
4830        } catch (RemoteException e) {
4831            Log.e(TAG, "Error calling ITelephony#supplyPuk", e);
4832        }
4833        return false;
4834    }
4835
4836    /** @hide */
4837    @SystemApi
4838    public int[] supplyPinReportResult(String pin) {
4839        try {
4840            ITelephony telephony = getITelephony();
4841            if (telephony != null)
4842                return telephony.supplyPinReportResult(pin);
4843        } catch (RemoteException e) {
4844            Log.e(TAG, "Error calling ITelephony#supplyPinReportResult", e);
4845        }
4846        return new int[0];
4847    }
4848
4849    /** @hide */
4850    @SystemApi
4851    public int[] supplyPukReportResult(String puk, String pin) {
4852        try {
4853            ITelephony telephony = getITelephony();
4854            if (telephony != null)
4855                return telephony.supplyPukReportResult(puk, pin);
4856        } catch (RemoteException e) {
4857            Log.e(TAG, "Error calling ITelephony#]", e);
4858        }
4859        return new int[0];
4860    }
4861
4862    /** @hide */
4863    @SystemApi
4864    public boolean handlePinMmi(String dialString) {
4865        try {
4866            ITelephony telephony = getITelephony();
4867            if (telephony != null)
4868                return telephony.handlePinMmi(dialString);
4869        } catch (RemoteException e) {
4870            Log.e(TAG, "Error calling ITelephony#handlePinMmi", e);
4871        }
4872        return false;
4873    }
4874
4875    /** @hide */
4876    @SystemApi
4877    public boolean handlePinMmiForSubscriber(int subId, String dialString) {
4878        try {
4879            ITelephony telephony = getITelephony();
4880            if (telephony != null)
4881                return telephony.handlePinMmiForSubscriber(subId, dialString);
4882        } catch (RemoteException e) {
4883            Log.e(TAG, "Error calling ITelephony#handlePinMmi", e);
4884        }
4885        return false;
4886    }
4887
4888    /** @hide */
4889    @SystemApi
4890    public void toggleRadioOnOff() {
4891        try {
4892            ITelephony telephony = getITelephony();
4893            if (telephony != null)
4894                telephony.toggleRadioOnOff();
4895        } catch (RemoteException e) {
4896            Log.e(TAG, "Error calling ITelephony#toggleRadioOnOff", e);
4897        }
4898    }
4899
4900    /** @hide */
4901    @SystemApi
4902    public boolean setRadio(boolean turnOn) {
4903        try {
4904            ITelephony telephony = getITelephony();
4905            if (telephony != null)
4906                return telephony.setRadio(turnOn);
4907        } catch (RemoteException e) {
4908            Log.e(TAG, "Error calling ITelephony#setRadio", e);
4909        }
4910        return false;
4911    }
4912
4913    /** @hide */
4914    @SystemApi
4915    public boolean setRadioPower(boolean turnOn) {
4916        try {
4917            ITelephony telephony = getITelephony();
4918            if (telephony != null)
4919                return telephony.setRadioPower(turnOn);
4920        } catch (RemoteException e) {
4921            Log.e(TAG, "Error calling ITelephony#setRadioPower", e);
4922        }
4923        return false;
4924    }
4925
4926    /** @hide */
4927    @SystemApi
4928    public void updateServiceLocation() {
4929        try {
4930            ITelephony telephony = getITelephony();
4931            if (telephony != null)
4932                telephony.updateServiceLocation();
4933        } catch (RemoteException e) {
4934            Log.e(TAG, "Error calling ITelephony#updateServiceLocation", e);
4935        }
4936    }
4937
4938    /** @hide */
4939    @SystemApi
4940    public boolean enableDataConnectivity() {
4941        try {
4942            ITelephony telephony = getITelephony();
4943            if (telephony != null)
4944                return telephony.enableDataConnectivity();
4945        } catch (RemoteException e) {
4946            Log.e(TAG, "Error calling ITelephony#enableDataConnectivity", e);
4947        }
4948        return false;
4949    }
4950
4951    /** @hide */
4952    @SystemApi
4953    public boolean disableDataConnectivity() {
4954        try {
4955            ITelephony telephony = getITelephony();
4956            if (telephony != null)
4957                return telephony.disableDataConnectivity();
4958        } catch (RemoteException e) {
4959            Log.e(TAG, "Error calling ITelephony#disableDataConnectivity", e);
4960        }
4961        return false;
4962    }
4963
4964    /** @hide */
4965    @SystemApi
4966    public boolean isDataConnectivityPossible() {
4967        try {
4968            ITelephony telephony = getITelephony();
4969            if (telephony != null)
4970                return telephony.isDataConnectivityPossible();
4971        } catch (RemoteException e) {
4972            Log.e(TAG, "Error calling ITelephony#isDataConnectivityPossible", e);
4973        }
4974        return false;
4975    }
4976
4977    /** @hide */
4978    @SystemApi
4979    public boolean needsOtaServiceProvisioning() {
4980        try {
4981            ITelephony telephony = getITelephony();
4982            if (telephony != null)
4983                return telephony.needsOtaServiceProvisioning();
4984        } catch (RemoteException e) {
4985            Log.e(TAG, "Error calling ITelephony#needsOtaServiceProvisioning", e);
4986        }
4987        return false;
4988    }
4989
4990    /**
4991     * Turns mobile data on or off.
4992     *
4993     * <p>Requires Permission:
4994     *     {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE} or that the
4995     *     calling app has carrier privileges.
4996     *
4997     * @param enable Whether to enable mobile data.
4998     *
4999     * @see #hasCarrierPrivileges
5000     */
5001    public void setDataEnabled(boolean enable) {
5002        setDataEnabled(getSubId(), enable);
5003    }
5004
5005    /** @hide */
5006    @SystemApi
5007    public void setDataEnabled(int subId, boolean enable) {
5008        try {
5009            Log.d(TAG, "setDataEnabled: enabled=" + enable);
5010            ITelephony telephony = getITelephony();
5011            if (telephony != null)
5012                telephony.setDataEnabled(subId, enable);
5013        } catch (RemoteException e) {
5014            Log.e(TAG, "Error calling ITelephony#setDataEnabled", e);
5015        }
5016    }
5017
5018    /**
5019     * Returns whether mobile data is enabled or not.
5020     *
5021     * <p>Requires Permission:
5022     *     {@link android.Manifest.permission#ACCESS_NETWORK_STATE ACCESS_NETWORK_STATE},
5023     *     {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}, or that the
5024     *     calling app has carrier privileges.
5025     *
5026     * @return true if mobile data is enabled.
5027     *
5028     * @see #hasCarrierPrivileges
5029     */
5030    public boolean getDataEnabled() {
5031        return getDataEnabled(getSubId());
5032    }
5033
5034    /** @hide */
5035    @SystemApi
5036    public boolean getDataEnabled(int subId) {
5037        boolean retVal = false;
5038        try {
5039            ITelephony telephony = getITelephony();
5040            if (telephony != null)
5041                retVal = telephony.getDataEnabled(subId);
5042        } catch (RemoteException e) {
5043            Log.e(TAG, "Error calling ITelephony#getDataEnabled", e);
5044        } catch (NullPointerException e) {
5045        }
5046        return retVal;
5047    }
5048
5049    /**
5050     * Returns the result and response from RIL for oem request
5051     *
5052     * @param oemReq the data is sent to ril.
5053     * @param oemResp the respose data from RIL.
5054     * @return negative value request was not handled or get error
5055     *         0 request was handled succesfully, but no response data
5056     *         positive value success, data length of response
5057     * @hide
5058     * @deprecated OEM needs a vendor-extension hal and their apps should use that instead
5059     */
5060    @Deprecated
5061    public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
5062        try {
5063            ITelephony telephony = getITelephony();
5064            if (telephony != null)
5065                return telephony.invokeOemRilRequestRaw(oemReq, oemResp);
5066        } catch (RemoteException ex) {
5067        } catch (NullPointerException ex) {
5068        }
5069        return -1;
5070    }
5071
5072    /** @hide */
5073    @SystemApi
5074    public void enableVideoCalling(boolean enable) {
5075        try {
5076            ITelephony telephony = getITelephony();
5077            if (telephony != null)
5078                telephony.enableVideoCalling(enable);
5079        } catch (RemoteException e) {
5080            Log.e(TAG, "Error calling ITelephony#enableVideoCalling", e);
5081        }
5082    }
5083
5084    /** @hide */
5085    @SystemApi
5086    public boolean isVideoCallingEnabled() {
5087        try {
5088            ITelephony telephony = getITelephony();
5089            if (telephony != null)
5090                return telephony.isVideoCallingEnabled(getOpPackageName());
5091        } catch (RemoteException e) {
5092            Log.e(TAG, "Error calling ITelephony#isVideoCallingEnabled", e);
5093        }
5094        return false;
5095    }
5096
5097    /**
5098     * Whether the device supports configuring the DTMF tone length.
5099     *
5100     * @return {@code true} if the DTMF tone length can be changed, and {@code false} otherwise.
5101     */
5102    public boolean canChangeDtmfToneLength() {
5103        try {
5104            ITelephony telephony = getITelephony();
5105            if (telephony != null) {
5106                return telephony.canChangeDtmfToneLength();
5107            }
5108        } catch (RemoteException e) {
5109            Log.e(TAG, "Error calling ITelephony#canChangeDtmfToneLength", e);
5110        } catch (SecurityException e) {
5111            Log.e(TAG, "Permission error calling ITelephony#canChangeDtmfToneLength", e);
5112        }
5113        return false;
5114    }
5115
5116    /**
5117     * Whether the device is a world phone.
5118     *
5119     * @return {@code true} if the device is a world phone, and {@code false} otherwise.
5120     */
5121    public boolean isWorldPhone() {
5122        try {
5123            ITelephony telephony = getITelephony();
5124            if (telephony != null) {
5125                return telephony.isWorldPhone();
5126            }
5127        } catch (RemoteException e) {
5128            Log.e(TAG, "Error calling ITelephony#isWorldPhone", e);
5129        } catch (SecurityException e) {
5130            Log.e(TAG, "Permission error calling ITelephony#isWorldPhone", e);
5131        }
5132        return false;
5133    }
5134
5135    /**
5136     * Whether the phone supports TTY mode.
5137     *
5138     * @return {@code true} if the device supports TTY mode, and {@code false} otherwise.
5139     */
5140    public boolean isTtyModeSupported() {
5141        try {
5142            ITelephony telephony = getITelephony();
5143            if (telephony != null) {
5144                return telephony.isTtyModeSupported();
5145            }
5146        } catch (RemoteException e) {
5147            Log.e(TAG, "Error calling ITelephony#isTtyModeSupported", e);
5148        } catch (SecurityException e) {
5149            Log.e(TAG, "Permission error calling ITelephony#isTtyModeSupported", e);
5150        }
5151        return false;
5152    }
5153
5154    /**
5155     * Whether the phone supports hearing aid compatibility.
5156     *
5157     * @return {@code true} if the device supports hearing aid compatibility, and {@code false}
5158     * otherwise.
5159     */
5160    public boolean isHearingAidCompatibilitySupported() {
5161        try {
5162            ITelephony telephony = getITelephony();
5163            if (telephony != null) {
5164                return telephony.isHearingAidCompatibilitySupported();
5165            }
5166        } catch (RemoteException e) {
5167            Log.e(TAG, "Error calling ITelephony#isHearingAidCompatibilitySupported", e);
5168        } catch (SecurityException e) {
5169            Log.e(TAG, "Permission error calling ITelephony#isHearingAidCompatibilitySupported", e);
5170        }
5171        return false;
5172    }
5173
5174    /**
5175     * This function retrieves value for setting "name+subId", and if that is not found
5176     * retrieves value for setting "name", and if that is not found throws
5177     * SettingNotFoundException
5178     *
5179     * @hide
5180     */
5181    public static int getIntWithSubId(ContentResolver cr, String name, int subId)
5182            throws SettingNotFoundException {
5183        try {
5184            return Settings.Global.getInt(cr, name + subId);
5185        } catch (SettingNotFoundException e) {
5186            try {
5187                int val = Settings.Global.getInt(cr, name);
5188                Settings.Global.putInt(cr, name + subId, val);
5189
5190                /* We are now moving from 'setting' to 'setting+subId', and using the value stored
5191                 * for 'setting' as default. Reset the default (since it may have a user set
5192                 * value). */
5193                int default_val = val;
5194                if (name.equals(Settings.Global.MOBILE_DATA)) {
5195                    default_val = "true".equalsIgnoreCase(
5196                            SystemProperties.get("ro.com.android.mobiledata", "true")) ? 1 : 0;
5197                } else if (name.equals(Settings.Global.DATA_ROAMING)) {
5198                    default_val = "true".equalsIgnoreCase(
5199                            SystemProperties.get("ro.com.android.dataroaming", "false")) ? 1 : 0;
5200                }
5201
5202                if (default_val != val) {
5203                    Settings.Global.putInt(cr, name, default_val);
5204                }
5205
5206                return val;
5207            } catch (SettingNotFoundException exc) {
5208                throw new SettingNotFoundException(name);
5209            }
5210        }
5211    }
5212
5213   /**
5214    * Returns the IMS Registration Status
5215    * @hide
5216    */
5217   public boolean isImsRegistered() {
5218       try {
5219           ITelephony telephony = getITelephony();
5220           if (telephony == null)
5221               return false;
5222           return telephony.isImsRegistered();
5223       } catch (RemoteException ex) {
5224           return false;
5225       } catch (NullPointerException ex) {
5226           return false;
5227       }
5228   }
5229
5230    /**
5231     * Returns the Status of Volte
5232     * @hide
5233     */
5234    public boolean isVolteAvailable() {
5235       try {
5236           return getITelephony().isVolteAvailable();
5237       } catch (RemoteException ex) {
5238           return false;
5239       } catch (NullPointerException ex) {
5240           return false;
5241       }
5242   }
5243
5244    /**
5245     * Returns the Status of video telephony (VT)
5246     * @hide
5247     */
5248    public boolean isVideoTelephonyAvailable() {
5249        try {
5250            return getITelephony().isVideoTelephonyAvailable();
5251        } catch (RemoteException ex) {
5252            return false;
5253        } catch (NullPointerException ex) {
5254            return false;
5255        }
5256    }
5257
5258    /**
5259     * Returns the Status of Wi-Fi Calling
5260     * @hide
5261     */
5262    public boolean isWifiCallingAvailable() {
5263       try {
5264           return getITelephony().isWifiCallingAvailable();
5265       } catch (RemoteException ex) {
5266           return false;
5267       } catch (NullPointerException ex) {
5268           return false;
5269       }
5270   }
5271
5272   /**
5273    * Set TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC for the default phone.
5274    *
5275    * @hide
5276    */
5277    public void setSimOperatorNumeric(String numeric) {
5278        int phoneId = getDefaultPhone();
5279        setSimOperatorNumericForPhone(phoneId, numeric);
5280    }
5281
5282   /**
5283    * Set TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC for the given phone.
5284    *
5285    * @hide
5286    */
5287    public void setSimOperatorNumericForPhone(int phoneId, String numeric) {
5288        setTelephonyProperty(phoneId,
5289                TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC, numeric);
5290    }
5291
5292    /**
5293     * Set TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC for the default phone.
5294     *
5295     * @hide
5296     */
5297    public void setSimOperatorName(String name) {
5298        int phoneId = getDefaultPhone();
5299        setSimOperatorNameForPhone(phoneId, name);
5300    }
5301
5302    /**
5303     * Set TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC for the given phone.
5304     *
5305     * @hide
5306     */
5307    public void setSimOperatorNameForPhone(int phoneId, String name) {
5308        setTelephonyProperty(phoneId,
5309                TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA, name);
5310    }
5311
5312   /**
5313    * Set TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY for the default phone.
5314    *
5315    * @hide
5316    */
5317    public void setSimCountryIso(String iso) {
5318        int phoneId = getDefaultPhone();
5319        setSimCountryIsoForPhone(phoneId, iso);
5320    }
5321
5322   /**
5323    * Set TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY for the given phone.
5324    *
5325    * @hide
5326    */
5327    public void setSimCountryIsoForPhone(int phoneId, String iso) {
5328        setTelephonyProperty(phoneId,
5329                TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY, iso);
5330    }
5331
5332    /**
5333     * Set TelephonyProperties.PROPERTY_SIM_STATE for the default phone.
5334     *
5335     * @hide
5336     */
5337    public void setSimState(String state) {
5338        int phoneId = getDefaultPhone();
5339        setSimStateForPhone(phoneId, state);
5340    }
5341
5342    /**
5343     * Set TelephonyProperties.PROPERTY_SIM_STATE for the given phone.
5344     *
5345     * @hide
5346     */
5347    public void setSimStateForPhone(int phoneId, String state) {
5348        setTelephonyProperty(phoneId,
5349                TelephonyProperties.PROPERTY_SIM_STATE, state);
5350    }
5351
5352    /**
5353     * Set SIM card power state. Request is equivalent to inserting or removing the card.
5354     *
5355     * @param powerUp True if powering up the SIM, otherwise powering down
5356     *
5357     * <p>Requires Permission:
5358     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
5359     *
5360     * @hide
5361     **/
5362    public void setSimPowerState(boolean powerUp) {
5363        setSimPowerStateForSlot(getDefaultSim(), powerUp);
5364    }
5365
5366    /**
5367     * Set SIM card power state. Request is equivalent to inserting or removing the card.
5368     *
5369     * @param slotId SIM slot id
5370     * @param powerUp True if powering up the SIM, otherwise powering down
5371     *
5372     * <p>Requires Permission:
5373     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
5374     *
5375     * @hide
5376     **/
5377    public void setSimPowerStateForSlot(int slotId, boolean powerUp) {
5378        try {
5379            ITelephony telephony = getITelephony();
5380            if (telephony != null) {
5381                telephony.setSimPowerStateForSlot(slotId, powerUp);
5382            }
5383        } catch (RemoteException e) {
5384            Log.e(TAG, "Error calling ITelephony#setSimPowerStateForSlot", e);
5385        } catch (SecurityException e) {
5386            Log.e(TAG, "Permission error calling ITelephony#setSimPowerStateForSlot", e);
5387        }
5388    }
5389
5390    /**
5391     * Set baseband version for the default phone.
5392     *
5393     * @param version baseband version
5394     * @hide
5395     */
5396    public void setBasebandVersion(String version) {
5397        int phoneId = getDefaultPhone();
5398        setBasebandVersionForPhone(phoneId, version);
5399    }
5400
5401    /**
5402     * Set baseband version by phone id.
5403     *
5404     * @param phoneId for which baseband version is set
5405     * @param version baseband version
5406     * @hide
5407     */
5408    public void setBasebandVersionForPhone(int phoneId, String version) {
5409        if (SubscriptionManager.isValidPhoneId(phoneId)) {
5410            String prop = TelephonyProperties.PROPERTY_BASEBAND_VERSION +
5411                    ((phoneId == 0) ? "" : Integer.toString(phoneId));
5412            SystemProperties.set(prop, version);
5413        }
5414    }
5415
5416    /**
5417     * Set phone type for the default phone.
5418     *
5419     * @param type phone type
5420     *
5421     * @hide
5422     */
5423    public void setPhoneType(int type) {
5424        int phoneId = getDefaultPhone();
5425        setPhoneType(phoneId, type);
5426    }
5427
5428    /**
5429     * Set phone type by phone id.
5430     *
5431     * @param phoneId for which phone type is set
5432     * @param type phone type
5433     *
5434     * @hide
5435     */
5436    public void setPhoneType(int phoneId, int type) {
5437        if (SubscriptionManager.isValidPhoneId(phoneId)) {
5438            TelephonyManager.setTelephonyProperty(phoneId,
5439                    TelephonyProperties.CURRENT_ACTIVE_PHONE, String.valueOf(type));
5440        }
5441    }
5442
5443    /**
5444     * Get OTASP number schema for the default phone.
5445     *
5446     * @param defaultValue default value
5447     * @return OTA SP number schema
5448     *
5449     * @hide
5450     */
5451    public String getOtaSpNumberSchema(String defaultValue) {
5452        int phoneId = getDefaultPhone();
5453        return getOtaSpNumberSchemaForPhone(phoneId, defaultValue);
5454    }
5455
5456    /**
5457     * Get OTASP number schema by phone id.
5458     *
5459     * @param phoneId for which OTA SP number schema is get
5460     * @param defaultValue default value
5461     * @return OTA SP number schema
5462     *
5463     * @hide
5464     */
5465    public String getOtaSpNumberSchemaForPhone(int phoneId, String defaultValue) {
5466        if (SubscriptionManager.isValidPhoneId(phoneId)) {
5467            return TelephonyManager.getTelephonyProperty(phoneId,
5468                    TelephonyProperties.PROPERTY_OTASP_NUM_SCHEMA, defaultValue);
5469        }
5470
5471        return defaultValue;
5472    }
5473
5474    /**
5475     * Get SMS receive capable from system property for the default phone.
5476     *
5477     * @param defaultValue default value
5478     * @return SMS receive capable
5479     *
5480     * @hide
5481     */
5482    public boolean getSmsReceiveCapable(boolean defaultValue) {
5483        int phoneId = getDefaultPhone();
5484        return getSmsReceiveCapableForPhone(phoneId, defaultValue);
5485    }
5486
5487    /**
5488     * Get SMS receive capable from system property by phone id.
5489     *
5490     * @param phoneId for which SMS receive capable is get
5491     * @param defaultValue default value
5492     * @return SMS receive capable
5493     *
5494     * @hide
5495     */
5496    public boolean getSmsReceiveCapableForPhone(int phoneId, boolean defaultValue) {
5497        if (SubscriptionManager.isValidPhoneId(phoneId)) {
5498            return Boolean.parseBoolean(TelephonyManager.getTelephonyProperty(phoneId,
5499                    TelephonyProperties.PROPERTY_SMS_RECEIVE, String.valueOf(defaultValue)));
5500        }
5501
5502        return defaultValue;
5503    }
5504
5505    /**
5506     * Get SMS send capable from system property for the default phone.
5507     *
5508     * @param defaultValue default value
5509     * @return SMS send capable
5510     *
5511     * @hide
5512     */
5513    public boolean getSmsSendCapable(boolean defaultValue) {
5514        int phoneId = getDefaultPhone();
5515        return getSmsSendCapableForPhone(phoneId, defaultValue);
5516    }
5517
5518    /**
5519     * Get SMS send capable from system property by phone id.
5520     *
5521     * @param phoneId for which SMS send capable is get
5522     * @param defaultValue default value
5523     * @return SMS send capable
5524     *
5525     * @hide
5526     */
5527    public boolean getSmsSendCapableForPhone(int phoneId, boolean defaultValue) {
5528        if (SubscriptionManager.isValidPhoneId(phoneId)) {
5529            return Boolean.parseBoolean(TelephonyManager.getTelephonyProperty(phoneId,
5530                    TelephonyProperties.PROPERTY_SMS_SEND, String.valueOf(defaultValue)));
5531        }
5532
5533        return defaultValue;
5534    }
5535
5536    /**
5537     * Set the alphabetic name of current registered operator.
5538     * @param name the alphabetic name of current registered operator.
5539     * @hide
5540     */
5541    public void setNetworkOperatorName(String name) {
5542        int phoneId = getDefaultPhone();
5543        setNetworkOperatorNameForPhone(phoneId, name);
5544    }
5545
5546    /**
5547     * Set the alphabetic name of current registered operator.
5548     * @param phoneId which phone you want to set
5549     * @param name the alphabetic name of current registered operator.
5550     * @hide
5551     */
5552    public void setNetworkOperatorNameForPhone(int phoneId, String name) {
5553        if (SubscriptionManager.isValidPhoneId(phoneId)) {
5554            setTelephonyProperty(phoneId, TelephonyProperties.PROPERTY_OPERATOR_ALPHA, name);
5555        }
5556    }
5557
5558    /**
5559     * Set the numeric name (MCC+MNC) of current registered operator.
5560     * @param operator the numeric name (MCC+MNC) of current registered operator
5561     * @hide
5562     */
5563    public void setNetworkOperatorNumeric(String numeric) {
5564        int phoneId = getDefaultPhone();
5565        setNetworkOperatorNumericForPhone(phoneId, numeric);
5566    }
5567
5568    /**
5569     * Set the numeric name (MCC+MNC) of current registered operator.
5570     * @param phoneId for which phone type is set
5571     * @param operator the numeric name (MCC+MNC) of current registered operator
5572     * @hide
5573     */
5574    public void setNetworkOperatorNumericForPhone(int phoneId, String numeric) {
5575        setTelephonyProperty(phoneId, TelephonyProperties.PROPERTY_OPERATOR_NUMERIC, numeric);
5576    }
5577
5578    /**
5579     * Set roaming state of the current network, for GSM purposes.
5580     * @param isRoaming is network in romaing state or not
5581     * @hide
5582     */
5583    public void setNetworkRoaming(boolean isRoaming) {
5584        int phoneId = getDefaultPhone();
5585        setNetworkRoamingForPhone(phoneId, isRoaming);
5586    }
5587
5588    /**
5589     * Set roaming state of the current network, for GSM purposes.
5590     * @param phoneId which phone you want to set
5591     * @param isRoaming is network in romaing state or not
5592     * @hide
5593     */
5594    public void setNetworkRoamingForPhone(int phoneId, boolean isRoaming) {
5595        if (SubscriptionManager.isValidPhoneId(phoneId)) {
5596            setTelephonyProperty(phoneId, TelephonyProperties.PROPERTY_OPERATOR_ISROAMING,
5597                    isRoaming ? "true" : "false");
5598        }
5599    }
5600
5601    /**
5602     * Set the ISO country code equivalent of the current registered
5603     * operator's MCC (Mobile Country Code).
5604     * @param iso the ISO country code equivalent of the current registered
5605     * @hide
5606     */
5607    public void setNetworkCountryIso(String iso) {
5608        int phoneId = getDefaultPhone();
5609        setNetworkCountryIsoForPhone(phoneId, iso);
5610    }
5611
5612    /**
5613     * Set the ISO country code equivalent of the current registered
5614     * operator's MCC (Mobile Country Code).
5615     * @param phoneId which phone you want to set
5616     * @param iso the ISO country code equivalent of the current registered
5617     * @hide
5618     */
5619    public void setNetworkCountryIsoForPhone(int phoneId, String iso) {
5620        if (SubscriptionManager.isValidPhoneId(phoneId)) {
5621            setTelephonyProperty(phoneId,
5622                    TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY, iso);
5623        }
5624    }
5625
5626    /**
5627     * Set the network type currently in use on the device for data transmission.
5628     * @param type the network type currently in use on the device for data transmission
5629     * @hide
5630     */
5631    public void setDataNetworkType(int type) {
5632        int phoneId = getDefaultPhone();
5633        setDataNetworkTypeForPhone(phoneId, type);
5634    }
5635
5636    /**
5637     * Set the network type currently in use on the device for data transmission.
5638     * @param phoneId which phone you want to set
5639     * @param type the network type currently in use on the device for data transmission
5640     * @hide
5641     */
5642    public void setDataNetworkTypeForPhone(int phoneId, int type) {
5643        if (SubscriptionManager.isValidPhoneId(phoneId)) {
5644            setTelephonyProperty(phoneId,
5645                    TelephonyProperties.PROPERTY_DATA_NETWORK_TYPE,
5646                    ServiceState.rilRadioTechnologyToString(type));
5647        }
5648    }
5649
5650    /**
5651     * Returns the subscription ID for the given phone account.
5652     * @hide
5653     */
5654    public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
5655        int retval = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
5656        try {
5657            ITelephony service = getITelephony();
5658            if (service != null) {
5659                retval = service.getSubIdForPhoneAccount(phoneAccount);
5660            }
5661        } catch (RemoteException e) {
5662        }
5663
5664        return retval;
5665    }
5666
5667    private int getSubIdForPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle) {
5668        int retval = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
5669        try {
5670            ITelecomService service = getTelecomService();
5671            if (service != null) {
5672                retval = getSubIdForPhoneAccount(service.getPhoneAccount(phoneAccountHandle));
5673            }
5674        } catch (RemoteException e) {
5675        }
5676
5677        return retval;
5678    }
5679
5680    /**
5681     * Resets telephony manager settings back to factory defaults.
5682     *
5683     * @hide
5684     */
5685    public void factoryReset(int subId) {
5686        try {
5687            Log.d(TAG, "factoryReset: subId=" + subId);
5688            ITelephony telephony = getITelephony();
5689            if (telephony != null)
5690                telephony.factoryReset(subId);
5691        } catch (RemoteException e) {
5692        }
5693    }
5694
5695
5696    /** @hide */
5697    public String getLocaleFromDefaultSim() {
5698        try {
5699            final ITelephony telephony = getITelephony();
5700            if (telephony != null) {
5701                return telephony.getLocaleFromDefaultSim();
5702            }
5703        } catch (RemoteException ex) {
5704        }
5705        return null;
5706    }
5707
5708    /**
5709     * Requests the modem activity info. The recipient will place the result
5710     * in `result`.
5711     * @param result The object on which the recipient will send the resulting
5712     * {@link android.telephony.ModemActivityInfo} object.
5713     * @hide
5714     */
5715    public void requestModemActivityInfo(ResultReceiver result) {
5716        try {
5717            ITelephony service = getITelephony();
5718            if (service != null) {
5719                service.requestModemActivityInfo(result);
5720                return;
5721            }
5722        } catch (RemoteException e) {
5723            Log.e(TAG, "Error calling ITelephony#getModemActivityInfo", e);
5724        }
5725        result.send(0, null);
5726    }
5727
5728    /**
5729     * Returns the current {@link ServiceState} information.
5730     *
5731     * <p>Requires Permission:
5732     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
5733     */
5734    public ServiceState getServiceState() {
5735        return getServiceStateForSubscriber(getSubId());
5736    }
5737
5738    /**
5739     * Returns the service state information on specified subscription. Callers require
5740     * either READ_PRIVILEGED_PHONE_STATE or READ_PHONE_STATE to retrieve the information.
5741     * @hide
5742     */
5743    public ServiceState getServiceStateForSubscriber(int subId) {
5744        try {
5745            ITelephony service = getITelephony();
5746            if (service != null) {
5747                return service.getServiceStateForSubscriber(subId, getOpPackageName());
5748            }
5749        } catch (RemoteException e) {
5750            Log.e(TAG, "Error calling ITelephony#getServiceStateForSubscriber", e);
5751        }
5752        return null;
5753    }
5754
5755    /**
5756     * Returns the URI for the per-account voicemail ringtone set in Phone settings.
5757     *
5758     * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
5759     * voicemail ringtone.
5760     * @return The URI for the ringtone to play when receiving a voicemail from a specific
5761     * PhoneAccount.
5762     */
5763    public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
5764        try {
5765            ITelephony service = getITelephony();
5766            if (service != null) {
5767                return service.getVoicemailRingtoneUri(accountHandle);
5768            }
5769        } catch (RemoteException e) {
5770            Log.e(TAG, "Error calling ITelephony#getVoicemailRingtoneUri", e);
5771        }
5772        return null;
5773    }
5774
5775    /**
5776     * Returns whether vibration is set for voicemail notification in Phone settings.
5777     *
5778     * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
5779     * voicemail vibration setting.
5780     * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
5781     */
5782    public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
5783        try {
5784            ITelephony service = getITelephony();
5785            if (service != null) {
5786                return service.isVoicemailVibrationEnabled(accountHandle);
5787            }
5788        } catch (RemoteException e) {
5789            Log.e(TAG, "Error calling ITelephony#isVoicemailVibrationEnabled", e);
5790        }
5791        return false;
5792    }
5793
5794    /**
5795     * Return the application ID for the app type like {@link APPTYPE_CSIM}.
5796     *
5797     * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
5798     *
5799     * @param appType the uicc app type like {@link APPTYPE_CSIM}
5800     * @return Application ID for specificied app type or null if no uicc or error.
5801     * @hide
5802     */
5803    public String getAidForAppType(int appType) {
5804        return getAidForAppType(getDefaultSubscription(), appType);
5805    }
5806
5807    /**
5808     * Return the application ID for the app type like {@link APPTYPE_CSIM}.
5809     *
5810     * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
5811     *
5812     * @param subId the subscription ID that this request applies to.
5813     * @param appType the uicc app type, like {@link APPTYPE_CSIM}
5814     * @return Application ID for specificied app type or null if no uicc or error.
5815     * @hide
5816     */
5817    public String getAidForAppType(int subId, int appType) {
5818        try {
5819            ITelephony service = getITelephony();
5820            if (service != null) {
5821                return service.getAidForAppType(subId, appType);
5822            }
5823        } catch (RemoteException e) {
5824            Log.e(TAG, "Error calling ITelephony#getAidForAppType", e);
5825        }
5826        return null;
5827    }
5828
5829    /**
5830     * Return the Electronic Serial Number.
5831     *
5832     * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
5833     *
5834     * @return ESN or null if error.
5835     * @hide
5836     */
5837    public String getEsn() {
5838        return getEsn(getDefaultSubscription());
5839    }
5840
5841    /**
5842     * Return the Electronic Serial Number.
5843     *
5844     * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
5845     *
5846     * @param subId the subscription ID that this request applies to.
5847     * @return ESN or null if error.
5848     * @hide
5849     */
5850    public String getEsn(int subId) {
5851        try {
5852            ITelephony service = getITelephony();
5853            if (service != null) {
5854                return service.getEsn(subId);
5855            }
5856        } catch (RemoteException e) {
5857            Log.e(TAG, "Error calling ITelephony#getEsn", e);
5858        }
5859        return null;
5860    }
5861
5862    /**
5863     * Return the Preferred Roaming List Version
5864     *
5865     * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
5866     *
5867     * @return PRLVersion or null if error.
5868     * @hide
5869     */
5870    public String getCdmaPrlVersion() {
5871        return getCdmaPrlVersion(getDefaultSubscription());
5872    }
5873
5874    /**
5875     * Return the Preferred Roaming List Version
5876     *
5877     * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
5878     *
5879     * @param subId the subscription ID that this request applies to.
5880     * @return PRLVersion or null if error.
5881     * @hide
5882     */
5883    public String getCdmaPrlVersion(int subId) {
5884        try {
5885            ITelephony service = getITelephony();
5886            if (service != null) {
5887                return service.getCdmaPrlVersion(subId);
5888            }
5889        } catch (RemoteException e) {
5890            Log.e(TAG, "Error calling ITelephony#getCdmaPrlVersion", e);
5891        }
5892        return null;
5893    }
5894
5895    /**
5896     * Get snapshot of Telephony histograms
5897     * @return List of Telephony histograms
5898     * Requires Permission:
5899     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
5900     * Or the calling app has carrier privileges.
5901     * @hide
5902     */
5903    @SystemApi
5904    public List<TelephonyHistogram> getTelephonyHistograms() {
5905        try {
5906            ITelephony service = getITelephony();
5907            if (service != null) {
5908                return service.getTelephonyHistograms();
5909            }
5910        } catch (RemoteException e) {
5911            Log.e(TAG, "Error calling ITelephony#getTelephonyHistograms", e);
5912        }
5913        return null;
5914    }
5915
5916    /**
5917     * Set the allowed carrier list for slotId
5918     * Require system privileges. In the future we may add this to carrier APIs.
5919     *
5920     * <p>Requires Permission:
5921     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE}
5922     *
5923     * <p>This method works only on devices with {@link
5924     * android.content.pm.PackageManager#FEATURE_TELEPHONY_CARRIERLOCK} enabled.
5925     *
5926     * @return The number of carriers set successfully. Should be length of
5927     * carrierList on success; -1 on error.
5928     * @hide
5929     */
5930    @SystemApi
5931    public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
5932        try {
5933            ITelephony service = getITelephony();
5934            if (service != null) {
5935                return service.setAllowedCarriers(slotId, carriers);
5936            }
5937        } catch (RemoteException e) {
5938            Log.e(TAG, "Error calling ITelephony#setAllowedCarriers", e);
5939        } catch (NullPointerException e) {
5940            Log.e(TAG, "Error calling ITelephony#setAllowedCarriers", e);
5941        }
5942        return -1;
5943    }
5944
5945    /**
5946     * Get the allowed carrier list for slotId.
5947     * Require system privileges. In the future we may add this to carrier APIs.
5948     *
5949     * <p>Requires Permission:
5950     *   {@link android.Manifest.permission#READ_PRIVILEGED_PHONE_STATE}
5951     *
5952     * <p>This method returns valid data on devices with {@link
5953     * android.content.pm.PackageManager#FEATURE_TELEPHONY_CARRIERLOCK} enabled.
5954     *
5955     * @return List of {@link android.telephony.CarrierIdentifier}; empty list
5956     * means all carriers are allowed.
5957     * @hide
5958     */
5959    @SystemApi
5960    public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
5961        try {
5962            ITelephony service = getITelephony();
5963            if (service != null) {
5964                return service.getAllowedCarriers(slotId);
5965            }
5966        } catch (RemoteException e) {
5967            Log.e(TAG, "Error calling ITelephony#getAllowedCarriers", e);
5968        } catch (NullPointerException e) {
5969            Log.e(TAG, "Error calling ITelephony#setAllowedCarriers", e);
5970        }
5971        return new ArrayList<CarrierIdentifier>(0);
5972    }
5973
5974    /**
5975     * Action set from carrier signalling broadcast receivers to enable/disable metered apns
5976     * Permissions android.Manifest.permission.MODIFY_PHONE_STATE is required
5977     * @param subId the subscription ID that this action applies to.
5978     * @param enabled control enable or disable metered apns.
5979     * @hide
5980     */
5981    public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
5982        try {
5983            ITelephony service = getITelephony();
5984            if (service != null) {
5985                service.carrierActionSetMeteredApnsEnabled(subId, enabled);
5986            }
5987        } catch (RemoteException e) {
5988            Log.e(TAG, "Error calling ITelephony#carrierActionSetMeteredApnsEnabled", e);
5989        }
5990    }
5991
5992    /**
5993     * Action set from carrier signalling broadcast receivers to enable/disable radio
5994     * Permissions android.Manifest.permission.MODIFY_PHONE_STATE is required
5995     * @param subId the subscription ID that this action applies to.
5996     * @param enabled control enable or disable radio.
5997     * @hide
5998     */
5999    public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
6000        try {
6001            ITelephony service = getITelephony();
6002            if (service != null) {
6003                service.carrierActionSetRadioEnabled(subId, enabled);
6004            }
6005        } catch (RemoteException e) {
6006            Log.e(TAG, "Error calling ITelephony#carrierActionSetRadioEnabled", e);
6007        }
6008    }
6009
6010    /**
6011     * Get aggregated video call data usage since boot.
6012     * Permissions android.Manifest.permission.READ_NETWORK_USAGE_HISTORY is required.
6013     * @return total data usage in bytes
6014     * @hide
6015     */
6016    public long getVtDataUsage() {
6017
6018        try {
6019            ITelephony service = getITelephony();
6020            if (service != null) {
6021                return service.getVtDataUsage();
6022            }
6023        } catch (RemoteException e) {
6024            Log.e(TAG, "Error calling getVtDataUsage", e);
6025        }
6026        return 0;
6027    }
6028
6029    /**
6030     * Policy control of data connection. Usually used when data limit is passed.
6031     * @param enabled True if enabling the data, otherwise disabling.
6032     * @param subId sub id
6033     * @hide
6034     */
6035    public void setPolicyDataEnabled(boolean enabled, int subId) {
6036        try {
6037            ITelephony service = getITelephony();
6038            if (service != null) {
6039                service.setPolicyDataEnabled(enabled, subId);
6040            }
6041        } catch (RemoteException e) {
6042            Log.e(TAG, "Error calling ITelephony#setPolicyDataEnabled", e);
6043        }
6044    }
6045
6046    /**
6047     * Get Client request stats which will contain statistical information
6048     * on each request made by client.
6049     * Callers require either READ_PRIVILEGED_PHONE_STATE or
6050     * READ_PHONE_STATE to retrieve the information.
6051     * @param subId sub id
6052     * @return List of Client Request Stats
6053     * @hide
6054     */
6055    public List<ClientRequestStats> getClientRequestStats(int subId) {
6056        try {
6057            ITelephony service = getITelephony();
6058            if (service != null) {
6059                return service.getClientRequestStats(getOpPackageName(), subId);
6060            }
6061        } catch (RemoteException e) {
6062            Log.e(TAG, "Error calling ITelephony#getClientRequestStats", e);
6063        }
6064
6065        return null;
6066    }
6067}
6068
6069