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