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