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