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