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