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