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