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