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