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