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