1 /*
2  * Copyright (C) 2018 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 package android.telephony.data;
17 
18 import android.annotation.IntDef;
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.content.ContentValues;
22 import android.database.Cursor;
23 import android.hardware.radio.V1_5.ApnTypes;
24 import android.net.Uri;
25 import android.os.Parcel;
26 import android.os.Parcelable;
27 import android.provider.Telephony;
28 import android.provider.Telephony.Carriers;
29 import android.telephony.Annotation.ApnType;
30 import android.telephony.Annotation.NetworkType;
31 import android.telephony.ServiceState;
32 import android.telephony.TelephonyManager;
33 import android.text.TextUtils;
34 import android.util.ArrayMap;
35 import android.util.Log;
36 
37 import com.android.telephony.Rlog;
38 
39 import java.lang.annotation.Retention;
40 import java.lang.annotation.RetentionPolicy;
41 import java.net.InetAddress;
42 import java.net.UnknownHostException;
43 import java.util.ArrayList;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.Objects;
47 
48 /**
49  * An Access Point Name (APN) configuration for a carrier data connection.
50  *
51  * <p>The APN provides configuration to connect a cellular network device to an IP data network. A
52  * carrier uses the name, type and other configuration in an {@code APNSetting} to decide which IP
53  * address to assign, any security methods to apply, and how the device might be connected to
54  * private networks.
55  *
56  * <p>Use {@link ApnSetting.Builder} to create new instances.
57  */
58 public class ApnSetting implements Parcelable {
59 
60     private static final String LOG_TAG = "ApnSetting";
61     private static final boolean VDBG = false;
62 
63     private static final String V2_FORMAT_REGEX = "^\\[ApnSettingV2\\]\\s*";
64     private static final String V3_FORMAT_REGEX = "^\\[ApnSettingV3\\]\\s*";
65     private static final String V4_FORMAT_REGEX = "^\\[ApnSettingV4\\]\\s*";
66     private static final String V5_FORMAT_REGEX = "^\\[ApnSettingV5\\]\\s*";
67     private static final String V6_FORMAT_REGEX = "^\\[ApnSettingV6\\]\\s*";
68     private static final String V7_FORMAT_REGEX = "^\\[ApnSettingV7\\]\\s*";
69 
70     /**
71      * Default value for mtu if it's not set. Moved from PhoneConstants.
72      * @hide
73      */
74     public static final int UNSET_MTU = 0;
75     private static final int UNSPECIFIED_INT = -1;
76     private static final String UNSPECIFIED_STRING = "";
77 
78     /**
79      * APN type for none. Should only be used for initialization.
80      * @hide
81      */
82     public static final int TYPE_NONE = ApnTypes.NONE;
83     /**
84      * APN type for all APNs (except wild-cardable types).
85      * @hide
86      */
87     public static final int TYPE_ALL = ApnTypes.DEFAULT | ApnTypes.HIPRI | ApnTypes.MMS
88             | ApnTypes.SUPL | ApnTypes.DUN | ApnTypes.FOTA | ApnTypes.IMS | ApnTypes.CBS;
89     /** APN type for default data traffic. */
90     public static final int TYPE_DEFAULT = ApnTypes.DEFAULT | ApnTypes.HIPRI;
91     /** APN type for MMS traffic. */
92     public static final int TYPE_MMS = ApnTypes.MMS;
93     /** APN type for SUPL assisted GPS. */
94     public static final int TYPE_SUPL = ApnTypes.SUPL;
95     /** APN type for DUN traffic. */
96     public static final int TYPE_DUN = ApnTypes.DUN;
97     /** APN type for HiPri traffic. */
98     public static final int TYPE_HIPRI = ApnTypes.HIPRI;
99     /** APN type for accessing the carrier's FOTA portal, used for over the air updates. */
100     public static final int TYPE_FOTA = ApnTypes.FOTA;
101     /** APN type for IMS. */
102     public static final int TYPE_IMS = ApnTypes.IMS;
103     /** APN type for CBS. */
104     public static final int TYPE_CBS = ApnTypes.CBS;
105     /** APN type for IA Initial Attach APN. */
106     public static final int TYPE_IA = ApnTypes.IA;
107     /**
108      * APN type for Emergency PDN. This is not an IA apn, but is used
109      * for access to carrier services in an emergency call situation.
110      */
111     public static final int TYPE_EMERGENCY = ApnTypes.EMERGENCY;
112     /** APN type for MCX (Mission Critical Service) where X can be PTT/Video/Data */
113     public static final int TYPE_MCX = ApnTypes.MCX;
114     /** APN type for XCAP. */
115     public static final int TYPE_XCAP = ApnTypes.XCAP;
116 
117     // Possible values for authentication types.
118     /** No authentication type. */
119     public static final int AUTH_TYPE_NONE = 0;
120     /** Authentication type for PAP. */
121     public static final int AUTH_TYPE_PAP = 1;
122     /** Authentication type for CHAP. */
123     public static final int AUTH_TYPE_CHAP = 2;
124     /** Authentication type for PAP or CHAP. */
125     public static final int AUTH_TYPE_PAP_OR_CHAP = 3;
126 
127     /** @hide */
128     @IntDef({
129             Telephony.Carriers.SKIP_464XLAT_DEFAULT,
130             Telephony.Carriers.SKIP_464XLAT_DISABLE,
131             Telephony.Carriers.SKIP_464XLAT_ENABLE,
132     })
133     @Retention(RetentionPolicy.SOURCE)
134     public @interface Skip464XlatStatus {}
135 
136     /**
137      * APN types for data connections.  These are usage categories for an APN
138      * entry.  One APN entry may support multiple APN types, eg, a single APN
139      * may service regular internet traffic ("default") as well as MMS-specific
140      * connections.<br/>
141      * APN_TYPE_ALL is a special type to indicate that this APN entry can
142      * service all data connections.
143      * <p>
144      * Note: The goal is to deprecate this.  Due to the Carrier Table being used
145      * directly, this isn't feasible right now.
146      *
147      * @hide
148      */
149     public static final String TYPE_ALL_STRING = "*";
150 
151     /**
152      * APN type for default data traffic
153      *
154      * @hide
155      */
156     public static final String TYPE_DEFAULT_STRING = "default";
157 
158 
159     /**
160      * APN type for MMS traffic
161      *
162      * @hide
163      */
164     public static final String TYPE_MMS_STRING = "mms";
165 
166 
167     /**
168      * APN type for SUPL assisted GPS
169      *
170      * @hide
171      */
172     public static final String TYPE_SUPL_STRING = "supl";
173 
174     /**
175      * APN type for DUN traffic
176      *
177      * @hide
178      */
179     public static final String TYPE_DUN_STRING = "dun";
180 
181     /**
182      * APN type for HiPri traffic
183      *
184      * @hide
185      */
186     public static final String TYPE_HIPRI_STRING = "hipri";
187 
188     /**
189      * APN type for FOTA
190      *
191      * @hide
192      */
193     public static final String TYPE_FOTA_STRING = "fota";
194 
195     /**
196      * APN type for IMS
197      *
198      * @hide
199      */
200     public static final String TYPE_IMS_STRING = "ims";
201 
202     /**
203      * APN type for CBS
204      *
205      * @hide
206      */
207     public static final String TYPE_CBS_STRING = "cbs";
208 
209     /**
210      * APN type for IA Initial Attach APN
211      *
212      * @hide
213      */
214     public static final String TYPE_IA_STRING = "ia";
215 
216     /**
217      * APN type for Emergency PDN. This is not an IA apn, but is used
218      * for access to carrier services in an emergency call situation.
219      *
220      * @hide
221      */
222     public static final String TYPE_EMERGENCY_STRING = "emergency";
223 
224     /**
225      * APN type for Mission Critical Services
226      *
227      * @hide
228      */
229     public static final String TYPE_MCX_STRING = "mcx";
230 
231     /**
232      * APN type for XCAP
233      *
234      * @hide
235      */
236     public static final String TYPE_XCAP_STRING = "xcap";
237 
238 
239     /** @hide */
240     @IntDef(prefix = { "AUTH_TYPE_" }, value = {
241         AUTH_TYPE_NONE,
242         AUTH_TYPE_PAP,
243         AUTH_TYPE_CHAP,
244         AUTH_TYPE_PAP_OR_CHAP,
245     })
246     @Retention(RetentionPolicy.SOURCE)
247     public @interface AuthType {}
248 
249     // Possible values for protocol which is defined in TS 27.007 section 10.1.1.
250     /** Internet protocol. */
251     public static final int PROTOCOL_IP = 0;
252     /** Internet protocol, version 6. */
253     public static final int PROTOCOL_IPV6 = 1;
254     /** Virtual PDP type introduced to handle dual IP stack UE capability. */
255     public static final int PROTOCOL_IPV4V6 = 2;
256     /** Point to point protocol. */
257     public static final int PROTOCOL_PPP = 3;
258     /** Transfer of Non-IP data to external packet data network. */
259     public static final int PROTOCOL_NON_IP = 4;
260     /** Transfer of Unstructured data to the Data Network via N6. */
261     public static final int PROTOCOL_UNSTRUCTURED = 5;
262 
263     /** @hide */
264     @IntDef(prefix = { "PROTOCOL_" }, value = {
265         PROTOCOL_IP,
266         PROTOCOL_IPV6,
267         PROTOCOL_IPV4V6,
268         PROTOCOL_PPP,
269         PROTOCOL_NON_IP,
270         PROTOCOL_UNSTRUCTURED,
271     })
272     @Retention(RetentionPolicy.SOURCE)
273     public @interface ProtocolType {}
274 
275     // Possible values for MVNO type.
276     /** MVNO type for service provider name. */
277     public static final int MVNO_TYPE_SPN = 0;
278     /** MVNO type for IMSI. */
279     public static final int MVNO_TYPE_IMSI = 1;
280     /** MVNO type for group identifier level 1. */
281     public static final int MVNO_TYPE_GID = 2;
282     /** MVNO type for ICCID. */
283     public static final int MVNO_TYPE_ICCID = 3;
284 
285     /** @hide */
286     @IntDef(prefix = { "MVNO_TYPE_" }, value = {
287         MVNO_TYPE_SPN,
288         MVNO_TYPE_IMSI,
289         MVNO_TYPE_GID,
290         MVNO_TYPE_ICCID,
291     })
292     @Retention(RetentionPolicy.SOURCE)
293     public @interface MvnoType {}
294 
295     private static final Map<String, Integer> APN_TYPE_STRING_MAP;
296     private static final Map<Integer, String> APN_TYPE_INT_MAP;
297     private static final Map<String, Integer> PROTOCOL_STRING_MAP;
298     private static final Map<Integer, String> PROTOCOL_INT_MAP;
299     private static final Map<String, Integer> MVNO_TYPE_STRING_MAP;
300     private static final Map<Integer, String> MVNO_TYPE_INT_MAP;
301 
302     static {
303         APN_TYPE_STRING_MAP = new ArrayMap<>();
APN_TYPE_STRING_MAP.put(TYPE_ALL_STRING, TYPE_ALL)304         APN_TYPE_STRING_MAP.put(TYPE_ALL_STRING, TYPE_ALL);
APN_TYPE_STRING_MAP.put(TYPE_DEFAULT_STRING, TYPE_DEFAULT)305         APN_TYPE_STRING_MAP.put(TYPE_DEFAULT_STRING, TYPE_DEFAULT);
APN_TYPE_STRING_MAP.put(TYPE_MMS_STRING, TYPE_MMS)306         APN_TYPE_STRING_MAP.put(TYPE_MMS_STRING, TYPE_MMS);
APN_TYPE_STRING_MAP.put(TYPE_SUPL_STRING, TYPE_SUPL)307         APN_TYPE_STRING_MAP.put(TYPE_SUPL_STRING, TYPE_SUPL);
APN_TYPE_STRING_MAP.put(TYPE_DUN_STRING, TYPE_DUN)308         APN_TYPE_STRING_MAP.put(TYPE_DUN_STRING, TYPE_DUN);
APN_TYPE_STRING_MAP.put(TYPE_HIPRI_STRING, TYPE_HIPRI)309         APN_TYPE_STRING_MAP.put(TYPE_HIPRI_STRING, TYPE_HIPRI);
APN_TYPE_STRING_MAP.put(TYPE_FOTA_STRING, TYPE_FOTA)310         APN_TYPE_STRING_MAP.put(TYPE_FOTA_STRING, TYPE_FOTA);
APN_TYPE_STRING_MAP.put(TYPE_IMS_STRING, TYPE_IMS)311         APN_TYPE_STRING_MAP.put(TYPE_IMS_STRING, TYPE_IMS);
APN_TYPE_STRING_MAP.put(TYPE_CBS_STRING, TYPE_CBS)312         APN_TYPE_STRING_MAP.put(TYPE_CBS_STRING, TYPE_CBS);
APN_TYPE_STRING_MAP.put(TYPE_IA_STRING, TYPE_IA)313         APN_TYPE_STRING_MAP.put(TYPE_IA_STRING, TYPE_IA);
APN_TYPE_STRING_MAP.put(TYPE_EMERGENCY_STRING, TYPE_EMERGENCY)314         APN_TYPE_STRING_MAP.put(TYPE_EMERGENCY_STRING, TYPE_EMERGENCY);
APN_TYPE_STRING_MAP.put(TYPE_MCX_STRING, TYPE_MCX)315         APN_TYPE_STRING_MAP.put(TYPE_MCX_STRING, TYPE_MCX);
APN_TYPE_STRING_MAP.put(TYPE_XCAP_STRING, TYPE_XCAP)316         APN_TYPE_STRING_MAP.put(TYPE_XCAP_STRING, TYPE_XCAP);
317 
318         APN_TYPE_INT_MAP = new ArrayMap<>();
APN_TYPE_INT_MAP.put(TYPE_DEFAULT, TYPE_DEFAULT_STRING)319         APN_TYPE_INT_MAP.put(TYPE_DEFAULT, TYPE_DEFAULT_STRING);
APN_TYPE_INT_MAP.put(TYPE_MMS, TYPE_MMS_STRING)320         APN_TYPE_INT_MAP.put(TYPE_MMS, TYPE_MMS_STRING);
APN_TYPE_INT_MAP.put(TYPE_SUPL, TYPE_SUPL_STRING)321         APN_TYPE_INT_MAP.put(TYPE_SUPL, TYPE_SUPL_STRING);
APN_TYPE_INT_MAP.put(TYPE_DUN, TYPE_DUN_STRING)322         APN_TYPE_INT_MAP.put(TYPE_DUN, TYPE_DUN_STRING);
APN_TYPE_INT_MAP.put(TYPE_HIPRI, TYPE_HIPRI_STRING)323         APN_TYPE_INT_MAP.put(TYPE_HIPRI, TYPE_HIPRI_STRING);
APN_TYPE_INT_MAP.put(TYPE_FOTA, TYPE_FOTA_STRING)324         APN_TYPE_INT_MAP.put(TYPE_FOTA, TYPE_FOTA_STRING);
APN_TYPE_INT_MAP.put(TYPE_IMS, TYPE_IMS_STRING)325         APN_TYPE_INT_MAP.put(TYPE_IMS, TYPE_IMS_STRING);
APN_TYPE_INT_MAP.put(TYPE_CBS, TYPE_CBS_STRING)326         APN_TYPE_INT_MAP.put(TYPE_CBS, TYPE_CBS_STRING);
APN_TYPE_INT_MAP.put(TYPE_IA, TYPE_IA_STRING)327         APN_TYPE_INT_MAP.put(TYPE_IA, TYPE_IA_STRING);
APN_TYPE_INT_MAP.put(TYPE_EMERGENCY, TYPE_EMERGENCY_STRING)328         APN_TYPE_INT_MAP.put(TYPE_EMERGENCY, TYPE_EMERGENCY_STRING);
APN_TYPE_INT_MAP.put(TYPE_MCX, TYPE_MCX_STRING)329         APN_TYPE_INT_MAP.put(TYPE_MCX, TYPE_MCX_STRING);
APN_TYPE_INT_MAP.put(TYPE_XCAP, TYPE_XCAP_STRING)330         APN_TYPE_INT_MAP.put(TYPE_XCAP, TYPE_XCAP_STRING);
331 
332         PROTOCOL_STRING_MAP = new ArrayMap<>();
333         PROTOCOL_STRING_MAP.put("IP", PROTOCOL_IP);
334         PROTOCOL_STRING_MAP.put("IPV6", PROTOCOL_IPV6);
335         PROTOCOL_STRING_MAP.put("IPV4V6", PROTOCOL_IPV4V6);
336         PROTOCOL_STRING_MAP.put("PPP", PROTOCOL_PPP);
337         PROTOCOL_STRING_MAP.put("NON-IP", PROTOCOL_NON_IP);
338         PROTOCOL_STRING_MAP.put("UNSTRUCTURED", PROTOCOL_UNSTRUCTURED);
339 
340         PROTOCOL_INT_MAP = new ArrayMap<>();
PROTOCOL_INT_MAP.put(PROTOCOL_IP, "IP")341         PROTOCOL_INT_MAP.put(PROTOCOL_IP, "IP");
PROTOCOL_INT_MAP.put(PROTOCOL_IPV6, "IPV6")342         PROTOCOL_INT_MAP.put(PROTOCOL_IPV6, "IPV6");
PROTOCOL_INT_MAP.put(PROTOCOL_IPV4V6, "IPV4V6")343         PROTOCOL_INT_MAP.put(PROTOCOL_IPV4V6, "IPV4V6");
PROTOCOL_INT_MAP.put(PROTOCOL_PPP, "PPP")344         PROTOCOL_INT_MAP.put(PROTOCOL_PPP, "PPP");
PROTOCOL_INT_MAP.put(PROTOCOL_NON_IP, "NON-IP")345         PROTOCOL_INT_MAP.put(PROTOCOL_NON_IP, "NON-IP");
PROTOCOL_INT_MAP.put(PROTOCOL_UNSTRUCTURED, "UNSTRUCTURED")346         PROTOCOL_INT_MAP.put(PROTOCOL_UNSTRUCTURED, "UNSTRUCTURED");
347 
348         MVNO_TYPE_STRING_MAP = new ArrayMap<>();
349         MVNO_TYPE_STRING_MAP.put("spn", MVNO_TYPE_SPN);
350         MVNO_TYPE_STRING_MAP.put("imsi", MVNO_TYPE_IMSI);
351         MVNO_TYPE_STRING_MAP.put("gid", MVNO_TYPE_GID);
352         MVNO_TYPE_STRING_MAP.put("iccid", MVNO_TYPE_ICCID);
353 
354         MVNO_TYPE_INT_MAP = new ArrayMap<>();
MVNO_TYPE_INT_MAP.put(MVNO_TYPE_SPN, "spn")355         MVNO_TYPE_INT_MAP.put(MVNO_TYPE_SPN, "spn");
MVNO_TYPE_INT_MAP.put(MVNO_TYPE_IMSI, "imsi")356         MVNO_TYPE_INT_MAP.put(MVNO_TYPE_IMSI, "imsi");
MVNO_TYPE_INT_MAP.put(MVNO_TYPE_GID, "gid")357         MVNO_TYPE_INT_MAP.put(MVNO_TYPE_GID, "gid");
MVNO_TYPE_INT_MAP.put(MVNO_TYPE_ICCID, "iccid")358         MVNO_TYPE_INT_MAP.put(MVNO_TYPE_ICCID, "iccid");
359     }
360 
361     private final String mEntryName;
362     private final String mApnName;
363     private final String mProxyAddress;
364     private final int mProxyPort;
365     private final Uri mMmsc;
366     private final String mMmsProxyAddress;
367     private final int mMmsProxyPort;
368     private final String mUser;
369     private final String mPassword;
370     private final int mAuthType;
371     private final int mApnTypeBitmask;
372     private final int mId;
373     private final String mOperatorNumeric;
374     private final int mProtocol;
375     private final int mRoamingProtocol;
376     private final int mMtu;
377 
378     private final boolean mCarrierEnabled;
379 
380     private final int mNetworkTypeBitmask;
381 
382     private final int mProfileId;
383 
384     private final boolean mPersistent;
385     private final int mMaxConns;
386     private final int mWaitTime;
387     private final int mMaxConnsTime;
388 
389     private final int mMvnoType;
390     private final String mMvnoMatchData;
391 
392     private final int mApnSetId;
393 
394     private boolean mPermanentFailed = false;
395     private final int mCarrierId;
396 
397     private final int mSkip464Xlat;
398 
399     /**
400      * Returns the MTU size of the mobile interface to which the APN connected.
401      *
402      * @return the MTU size of the APN
403      * @hide
404      */
getMtu()405     public int getMtu() {
406         return mMtu;
407     }
408 
409     /**
410      * Returns the profile id to which the APN saved in modem.
411      *
412      * @return the profile id of the APN
413      * @hide
414      */
getProfileId()415     public int getProfileId() {
416         return mProfileId;
417     }
418 
419     /**
420      * Returns if the APN setting is persistent on the modem.
421      *
422      * @return is the APN setting to be set in modem
423      * @hide
424      */
isPersistent()425     public boolean isPersistent() {
426         return mPersistent;
427     }
428 
429     /**
430      * Returns the max connections of this APN.
431      *
432      * @return the max connections of this APN
433      * @hide
434      */
getMaxConns()435     public int getMaxConns() {
436         return mMaxConns;
437     }
438 
439     /**
440      * Returns the wait time for retry of the APN.
441      *
442      * @return the wait time for retry of the APN
443      * @hide
444      */
getWaitTime()445     public int getWaitTime() {
446         return mWaitTime;
447     }
448 
449     /**
450      * Returns the time to limit max connection for the APN.
451      *
452      * @return the time to limit max connection for the APN
453      * @hide
454      */
getMaxConnsTime()455     public int getMaxConnsTime() {
456         return mMaxConnsTime;
457     }
458 
459     /**
460      * Returns the MVNO data. Examples:
461      *   "spn": A MOBILE, BEN NL
462      *   "imsi": 302720x94, 2060188
463      *   "gid": 4E, 33
464      *   "iccid": 898603 etc..
465      *
466      * @return the mvno match data
467      * @hide
468      */
getMvnoMatchData()469     public String getMvnoMatchData() {
470         return mMvnoMatchData;
471     }
472 
473     /**
474      * Returns the APN set id.
475      *
476      * APNs that are part of the same set should be preferred together, e.g. if the
477      * user selects a default APN with apnSetId=1, then we will prefer all APNs with apnSetId = 1.
478      *
479      * If the apnSetId = Carriers.NO_SET_SET(=0) then the APN is not part of a set.
480      *
481      * @return the APN set id
482      * @hide
483      */
getApnSetId()484     public int getApnSetId() {
485         return mApnSetId;
486     }
487 
488     /**
489      * Indicates this APN setting is permanently failed and cannot be
490      * retried by the retry manager anymore.
491      *
492      * @return if this APN setting is permanently failed
493      * @hide
494      */
getPermanentFailed()495     public boolean getPermanentFailed() {
496         return mPermanentFailed;
497     }
498 
499     /**
500      * Sets if this APN setting is permanently failed.
501      *
502      * @param permanentFailed if this APN setting is permanently failed
503      * @hide
504      */
setPermanentFailed(boolean permanentFailed)505     public void setPermanentFailed(boolean permanentFailed) {
506         mPermanentFailed = permanentFailed;
507     }
508 
509     /**
510      * Gets the human-readable name that describes the APN.
511      *
512      * @return the entry name for the APN
513      */
getEntryName()514     public String getEntryName() {
515         return mEntryName;
516     }
517 
518     /**
519      * Returns the name of the APN.
520      *
521      * @return APN name
522      */
getApnName()523     public String getApnName() {
524         return mApnName;
525     }
526 
527     /**
528      * Gets the HTTP proxy address configured for the APN. The proxy address might be an IP address
529      * or hostname. This method returns {@code null} if system networking (typically DNS) isn’t
530      * available to resolve a hostname value—values set as IP addresses don’t have this restriction.
531      * This is a known problem and will be addressed in a future release.
532      *
533      * @return the HTTP proxy address or {@code null} if DNS isn’t available to resolve a hostname
534      * @deprecated use {@link #getProxyAddressAsString()} instead.
535      */
536     @Deprecated
getProxyAddress()537     public InetAddress getProxyAddress() {
538         return inetAddressFromString(mProxyAddress);
539     }
540 
541     /**
542      * Returns the proxy address of the APN.
543      *
544      * @return proxy address.
545      */
getProxyAddressAsString()546     public String getProxyAddressAsString() {
547         return mProxyAddress;
548     }
549 
550     /**
551      * Returns the proxy address of the APN.
552      *
553      * @return proxy address.
554      */
getProxyPort()555     public int getProxyPort() {
556         return mProxyPort;
557     }
558     /**
559      * Returns the MMSC Uri of the APN.
560      *
561      * @return MMSC Uri.
562      */
getMmsc()563     public Uri getMmsc() {
564         return mMmsc;
565     }
566 
567     /**
568      * Gets the MMS proxy address configured for the APN. The MMS proxy address might be an IP
569      * address or hostname. This method returns {@code null} if system networking (typically DNS)
570      * isn’t available to resolve a hostname value—values set as IP addresses don’t have this
571      * restriction. This is a known problem and will be addressed in a future release.
572      *
573      * @return the MMS proxy address or {@code null} if DNS isn’t available to resolve a hostname
574      * @deprecated use {@link #getMmsProxyAddressAsString()} instead.
575      */
576     @Deprecated
getMmsProxyAddress()577     public InetAddress getMmsProxyAddress() {
578         return inetAddressFromString(mMmsProxyAddress);
579     }
580 
581     /**
582      * Returns the MMS proxy address of the APN.
583      *
584      * @return MMS proxy address.
585      */
getMmsProxyAddressAsString()586     public String getMmsProxyAddressAsString() {
587         return mMmsProxyAddress;
588     }
589 
590     /**
591      * Returns the MMS proxy port of the APN.
592      *
593      * @return MMS proxy port
594      */
getMmsProxyPort()595     public int getMmsProxyPort() {
596         return mMmsProxyPort;
597     }
598 
599     /**
600      * Returns the APN username of the APN.
601      *
602      * @return APN username
603      */
getUser()604     public String getUser() {
605         return mUser;
606     }
607 
608     /**
609      * Returns the APN password of the APN.
610      *
611      * @return APN password
612      */
getPassword()613     public String getPassword() {
614         return mPassword;
615     }
616 
617     /**
618      * Returns the authentication type of the APN.
619      *
620      * @return authentication type
621      */
622     @AuthType
getAuthType()623     public int getAuthType() {
624         return mAuthType;
625     }
626 
627     /**
628      * Returns the bitmask of APN types.
629      *
630      * <p>Apn types are usage categories for an APN entry. One APN entry may support multiple
631      * APN types, eg, a single APN may service regular internet traffic ("default") as well as
632      * MMS-specific connections.
633      *
634      * <p>The bitmask of APN types is calculated from APN types defined in {@link ApnSetting}.
635      *
636      * @see Builder#setApnTypeBitmask(int)
637      * @return a bitmask describing the types of the APN
638      */
getApnTypeBitmask()639     public @ApnType int getApnTypeBitmask() {
640         return mApnTypeBitmask;
641     }
642 
643     /**
644      * Returns the unique database id for this entry.
645      *
646      * @return the unique database id
647      */
getId()648     public int getId() {
649         return mId;
650     }
651 
652     /**
653      * Returns the numeric operator ID for the APN. Numeric operator ID is defined as
654      * {@link android.provider.Telephony.Carriers#MCC} +
655      * {@link android.provider.Telephony.Carriers#MNC}.
656      *
657      * @return the numeric operator ID
658      */
getOperatorNumeric()659     public String getOperatorNumeric() {
660         return mOperatorNumeric;
661     }
662 
663     /**
664      * Returns the protocol to use to connect to this APN.
665      *
666      * <p>Protocol is one of the {@code PDP_type} values in TS 27.007 section 10.1.1.
667      *
668      * @see Builder#setProtocol(int)
669      * @return the protocol
670      */
671     @ProtocolType
getProtocol()672     public int getProtocol() {
673         return mProtocol;
674     }
675 
676     /**
677      * Returns the protocol to use to connect to this APN while the device is roaming.
678      *
679      * <p>Roaming protocol is one of the {@code PDP_type} values in TS 27.007 section 10.1.1.
680      *
681      * @see Builder#setRoamingProtocol(int)
682      * @return the roaming protocol
683      */
684     @ProtocolType
getRoamingProtocol()685     public int getRoamingProtocol() {
686         return mRoamingProtocol;
687     }
688 
689     /**
690      * Returns the current status of APN.
691      *
692      * {@code true} : enabled APN.
693      * {@code false} : disabled APN.
694      *
695      * @return the current status
696      */
isEnabled()697     public boolean isEnabled() {
698         return mCarrierEnabled;
699     }
700 
701     /**
702      * Returns a bitmask describing the Radio Technologies(Network Types) which this APN may use.
703      *
704      * NetworkType bitmask is calculated from NETWORK_TYPE defined in {@link TelephonyManager}.
705      *
706      * Examples of Network Types include {@link TelephonyManager#NETWORK_TYPE_UNKNOWN},
707      * {@link TelephonyManager#NETWORK_TYPE_GPRS}, {@link TelephonyManager#NETWORK_TYPE_EDGE}.
708      *
709      * @return a bitmask describing the Radio Technologies(Network Types)
710      */
getNetworkTypeBitmask()711     public int getNetworkTypeBitmask() {
712         return mNetworkTypeBitmask;
713     }
714 
715     /**
716      * Returns the MVNO match type for this APN.
717      *
718      * @see Builder#setMvnoType(int)
719      * @return the MVNO match type
720      */
721     @MvnoType
getMvnoType()722     public int getMvnoType() {
723         return mMvnoType;
724     }
725 
726     /**
727      * Returns the carrier id for this APN.
728      *
729      * @see Builder#setCarrierId(int)
730      * @return the carrier id
731      */
getCarrierId()732     public int getCarrierId() {
733         return mCarrierId;
734     }
735 
736     /**
737      * Returns the skip464xlat flag for this APN.
738      *
739      * @return SKIP_464XLAT_DEFAULT, SKIP_464XLAT_DISABLE or SKIP_464XLAT_ENABLE
740      * @hide
741      */
742     @Skip464XlatStatus
getSkip464Xlat()743     public int getSkip464Xlat() {
744         return mSkip464Xlat;
745     }
746 
ApnSetting(Builder builder)747     private ApnSetting(Builder builder) {
748         this.mEntryName = builder.mEntryName;
749         this.mApnName = builder.mApnName;
750         this.mProxyAddress = builder.mProxyAddress;
751         this.mProxyPort = builder.mProxyPort;
752         this.mMmsc = builder.mMmsc;
753         this.mMmsProxyAddress = builder.mMmsProxyAddress;
754         this.mMmsProxyPort = builder.mMmsProxyPort;
755         this.mUser = builder.mUser;
756         this.mPassword = builder.mPassword;
757         this.mAuthType = builder.mAuthType;
758         this.mApnTypeBitmask = builder.mApnTypeBitmask;
759         this.mId = builder.mId;
760         this.mOperatorNumeric = builder.mOperatorNumeric;
761         this.mProtocol = builder.mProtocol;
762         this.mRoamingProtocol = builder.mRoamingProtocol;
763         this.mMtu = builder.mMtu;
764         this.mCarrierEnabled = builder.mCarrierEnabled;
765         this.mNetworkTypeBitmask = builder.mNetworkTypeBitmask;
766         this.mProfileId = builder.mProfileId;
767         this.mPersistent = builder.mModemCognitive;
768         this.mMaxConns = builder.mMaxConns;
769         this.mWaitTime = builder.mWaitTime;
770         this.mMaxConnsTime = builder.mMaxConnsTime;
771         this.mMvnoType = builder.mMvnoType;
772         this.mMvnoMatchData = builder.mMvnoMatchData;
773         this.mApnSetId = builder.mApnSetId;
774         this.mCarrierId = builder.mCarrierId;
775         this.mSkip464Xlat = builder.mSkip464Xlat;
776     }
777 
778     /**
779      * @hide
780      */
makeApnSetting(int id, String operatorNumeric, String entryName, String apnName, String proxyAddress, int proxyPort, Uri mmsc, String mmsProxyAddress, int mmsProxyPort, String user, String password, int authType, int mApnTypeBitmask, int protocol, int roamingProtocol, boolean carrierEnabled, int networkTypeBitmask, int profileId, boolean modemCognitive, int maxConns, int waitTime, int maxConnsTime, int mtu, int mvnoType, String mvnoMatchData, int apnSetId, int carrierId, int skip464xlat)781     public static ApnSetting makeApnSetting(int id, String operatorNumeric, String entryName,
782             String apnName, String proxyAddress, int proxyPort, Uri mmsc,
783             String mmsProxyAddress, int mmsProxyPort, String user, String password,
784             int authType, int mApnTypeBitmask, int protocol, int roamingProtocol,
785             boolean carrierEnabled, int networkTypeBitmask, int profileId,
786             boolean modemCognitive, int maxConns, int waitTime, int maxConnsTime, int mtu,
787             int mvnoType, String mvnoMatchData, int apnSetId, int carrierId, int skip464xlat) {
788         return new Builder()
789             .setId(id)
790             .setOperatorNumeric(operatorNumeric)
791             .setEntryName(entryName)
792             .setApnName(apnName)
793             .setProxyAddress(proxyAddress)
794             .setProxyPort(proxyPort)
795             .setMmsc(mmsc)
796             .setMmsProxyAddress(mmsProxyAddress)
797             .setMmsProxyPort(mmsProxyPort)
798             .setUser(user)
799             .setPassword(password)
800             .setAuthType(authType)
801             .setApnTypeBitmask(mApnTypeBitmask)
802             .setProtocol(protocol)
803             .setRoamingProtocol(roamingProtocol)
804             .setCarrierEnabled(carrierEnabled)
805             .setNetworkTypeBitmask(networkTypeBitmask)
806             .setProfileId(profileId)
807             .setModemCognitive(modemCognitive)
808             .setMaxConns(maxConns)
809             .setWaitTime(waitTime)
810             .setMaxConnsTime(maxConnsTime)
811             .setMtu(mtu)
812             .setMvnoType(mvnoType)
813             .setMvnoMatchData(mvnoMatchData)
814             .setApnSetId(apnSetId)
815             .setCarrierId(carrierId)
816             .setSkip464Xlat(skip464xlat)
817             .buildWithoutCheck();
818     }
819 
820     /**
821      * @hide
822      */
makeApnSetting(int id, String operatorNumeric, String entryName, String apnName, String proxyAddress, int proxyPort, Uri mmsc, String mmsProxyAddress, int mmsProxyPort, String user, String password, int authType, int mApnTypeBitmask, int protocol, int roamingProtocol, boolean carrierEnabled, int networkTypeBitmask, int profileId, boolean modemCognitive, int maxConns, int waitTime, int maxConnsTime, int mtu, int mvnoType, String mvnoMatchData)823     public static ApnSetting makeApnSetting(int id, String operatorNumeric, String entryName,
824             String apnName, String proxyAddress, int proxyPort, Uri mmsc,
825             String mmsProxyAddress, int mmsProxyPort, String user, String password,
826             int authType, int mApnTypeBitmask, int protocol, int roamingProtocol,
827             boolean carrierEnabled, int networkTypeBitmask, int profileId, boolean modemCognitive,
828             int maxConns, int waitTime, int maxConnsTime, int mtu, int mvnoType,
829             String mvnoMatchData) {
830         return makeApnSetting(id, operatorNumeric, entryName, apnName, proxyAddress, proxyPort,
831             mmsc, mmsProxyAddress, mmsProxyPort, user, password, authType, mApnTypeBitmask,
832             protocol, roamingProtocol, carrierEnabled, networkTypeBitmask, profileId,
833             modemCognitive, maxConns, waitTime, maxConnsTime, mtu, mvnoType, mvnoMatchData,
834             Carriers.NO_APN_SET_ID, TelephonyManager.UNKNOWN_CARRIER_ID,
835             Carriers.SKIP_464XLAT_DEFAULT);
836     }
837 
838     /**
839      * @hide
840      */
makeApnSetting(Cursor cursor)841     public static ApnSetting makeApnSetting(Cursor cursor) {
842         final int apnTypesBitmask = getApnTypesBitmaskFromString(
843                 cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.TYPE)));
844         int networkTypeBitmask = cursor.getInt(
845                 cursor.getColumnIndexOrThrow(Telephony.Carriers.NETWORK_TYPE_BITMASK));
846         if (networkTypeBitmask == 0) {
847             final int bearerBitmask = cursor.getInt(cursor.getColumnIndexOrThrow(
848                     Telephony.Carriers.BEARER_BITMASK));
849             networkTypeBitmask =
850                 ServiceState.convertBearerBitmaskToNetworkTypeBitmask(bearerBitmask);
851         }
852 
853         return makeApnSetting(
854             cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers._ID)),
855             cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.NUMERIC)),
856             cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.NAME)),
857             cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.APN)),
858             cursor.getString(
859                 cursor.getColumnIndexOrThrow(Telephony.Carriers.PROXY)),
860             portFromString(cursor.getString(
861                 cursor.getColumnIndexOrThrow(Telephony.Carriers.PORT))),
862             UriFromString(cursor.getString(
863                 cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSC))),
864             cursor.getString(
865                 cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSPROXY)),
866             portFromString(cursor.getString(
867                 cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSPORT))),
868             cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.USER)),
869             cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PASSWORD)),
870             cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.AUTH_TYPE)),
871             apnTypesBitmask,
872             getProtocolIntFromString(
873                 cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PROTOCOL))),
874             getProtocolIntFromString(
875                 cursor.getString(cursor.getColumnIndexOrThrow(
876                     Telephony.Carriers.ROAMING_PROTOCOL))),
877             cursor.getInt(cursor.getColumnIndexOrThrow(
878                 Telephony.Carriers.CARRIER_ENABLED)) == 1,
879             networkTypeBitmask,
880             cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.PROFILE_ID)),
881             cursor.getInt(cursor.getColumnIndexOrThrow(
882                 Telephony.Carriers.MODEM_PERSIST)) == 1,
883             cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.MAX_CONNECTIONS)),
884             cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.WAIT_TIME_RETRY)),
885             cursor.getInt(cursor.getColumnIndexOrThrow(
886                 Telephony.Carriers.TIME_LIMIT_FOR_MAX_CONNECTIONS)),
887             cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.MTU)),
888             getMvnoTypeIntFromString(
889                 cursor.getString(cursor.getColumnIndexOrThrow(
890                     Telephony.Carriers.MVNO_TYPE))),
891             cursor.getString(cursor.getColumnIndexOrThrow(
892                 Telephony.Carriers.MVNO_MATCH_DATA)),
893             cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.APN_SET_ID)),
894             cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.CARRIER_ID)),
895             cursor.getInt(cursor.getColumnIndexOrThrow(Carriers.SKIP_464XLAT)));
896     }
897 
898     /**
899      * @hide
900      */
makeApnSetting(ApnSetting apn)901     public static ApnSetting makeApnSetting(ApnSetting apn) {
902         return makeApnSetting(apn.mId, apn.mOperatorNumeric, apn.mEntryName, apn.mApnName,
903             apn.mProxyAddress, apn.mProxyPort, apn.mMmsc, apn.mMmsProxyAddress,
904             apn.mMmsProxyPort, apn.mUser, apn.mPassword, apn.mAuthType, apn.mApnTypeBitmask,
905             apn.mProtocol, apn.mRoamingProtocol, apn.mCarrierEnabled, apn.mNetworkTypeBitmask,
906             apn.mProfileId, apn.mPersistent, apn.mMaxConns, apn.mWaitTime,
907             apn.mMaxConnsTime, apn.mMtu, apn.mMvnoType, apn.mMvnoMatchData, apn.mApnSetId,
908             apn.mCarrierId, apn.mSkip464Xlat);
909     }
910 
911     /**
912      * Creates an ApnSetting object from a string.
913      *
914      * @param data the string to read.
915      *
916      * The string must be in one of two formats (newlines added for clarity,
917      * spaces are optional):
918      *
919      * v1 format:
920      *   <carrier>, <apn>, <proxy>, <port>, <user>, <password>, <server>,
921      *   <mmsc>, <mmsproxy>, <mmsport>, <mcc>, <mnc>, <authtype>,
922      *   <type>[| <type>...],
923      *
924      * v2 format:
925      *   [ApnSettingV2] <carrier>, <apn>, <proxy>, <port>, <user>, <password>, <server>,
926      *   <mmsc>, <mmsproxy>, <mmsport>, <mcc>, <mnc>, <authtype>,
927      *   <type>[| <type>...], <protocol>, <roaming_protocol>, <carrierEnabled>, <bearerBitmask>,
928      *
929      * v3 format:
930      *   [ApnSettingV3] <carrier>, <apn>, <proxy>, <port>, <user>, <password>, <server>,
931      *   <mmsc>, <mmsproxy>, <mmsport>, <mcc>, <mnc>, <authtype>,
932      *   <type>[| <type>...], <protocol>, <roaming_protocol>, <carrierEnabled>, <bearerBitmask>,
933      *   <profileId>, <modemCognitive>, <maxConns>, <waitTime>, <maxConnsTime>, <mtu>,
934      *   <mvnoType>, <mvnoMatchData>
935      *
936      * v4 format:
937      *   [ApnSettingV4] <carrier>, <apn>, <proxy>, <port>, <user>, <password>, <server>,
938      *   <mmsc>, <mmsproxy>, <mmsport>, <mcc>, <mnc>, <authtype>,
939      *   <type>[| <type>...], <protocol>, <roaming_protocol>, <carrierEnabled>, <bearerBitmask>,
940      *   <profileId>, <modemCognitive>, <maxConns>, <waitTime>, <maxConnsTime>, <mtu>,
941      *   <mvnoType>, <mvnoMatchData>, <networkTypeBitmask>
942      *
943      * v5 format:
944      *   [ApnSettingV5] <carrier>, <apn>, <proxy>, <port>, <user>, <password>, <server>,
945      *   <mmsc>, <mmsproxy>, <mmsport>, <mcc>, <mnc>, <authtype>,
946      *   <type>[| <type>...], <protocol>, <roaming_protocol>, <carrierEnabled>, <bearerBitmask>,
947      *   <profileId>, <modemCognitive>, <maxConns>, <waitTime>, <maxConnsTime>, <mtu>,
948      *   <mvnoType>, <mvnoMatchData>, <networkTypeBitmask>, <apnSetId>
949      *
950      * v6 format:
951      *   [ApnSettingV6] <carrier>, <apn>, <proxy>, <port>, <user>, <password>, <server>,
952      *   <mmsc>, <mmsproxy>, <mmsport>, <mcc>, <mnc>, <authtype>,
953      *   <type>[| <type>...], <protocol>, <roaming_protocol>, <carrierEnabled>, <bearerBitmask>,
954      *   <profileId>, <modemCognitive>, <maxConns>, <waitTime>, <maxConnsTime>, <mtu>,
955      *   <mvnoType>, <mvnoMatchData>, <networkTypeBitmask>, <apnSetId>, <carrierId>
956      *
957      * v7 format:
958      *   [ApnSettingV7] <carrier>, <apn>, <proxy>, <port>, <user>, <password>, <server>,
959      *   <mmsc>, <mmsproxy>, <mmsport>, <mcc>, <mnc>, <authtype>,
960      *   <type>[| <type>...], <protocol>, <roaming_protocol>, <carrierEnabled>, <bearerBitmask>,
961      *   <profileId>, <modemCognitive>, <maxConns>, <waitTime>, <maxConnsTime>, <mtu>,
962      *   <mvnoType>, <mvnoMatchData>, <networkTypeBitmask>, <apnSetId>, <carrierId>, <skip464xlat>
963      *
964      * Note that the strings generated by {@link #toString()} do not contain the username
965      * and password and thus cannot be read by this method.
966      *
967      * This method may return {@code null} if the input string is invalid.
968      *
969      * @hide
970      */
fromString(String data)971     public static ApnSetting fromString(String data) {
972         if (data == null) return null;
973 
974         int version;
975         // matches() operates on the whole string, so append .* to the regex.
976         if (data.matches(V7_FORMAT_REGEX + ".*")) {
977             version = 7;
978             data = data.replaceFirst(V7_FORMAT_REGEX, "");
979         } else if (data.matches(V6_FORMAT_REGEX + ".*")) {
980             version = 6;
981             data = data.replaceFirst(V6_FORMAT_REGEX, "");
982         } else if (data.matches(V5_FORMAT_REGEX + ".*")) {
983             version = 5;
984             data = data.replaceFirst(V5_FORMAT_REGEX, "");
985         } else if (data.matches(V4_FORMAT_REGEX + ".*")) {
986             version = 4;
987             data = data.replaceFirst(V4_FORMAT_REGEX, "");
988         } else if (data.matches(V3_FORMAT_REGEX + ".*")) {
989             version = 3;
990             data = data.replaceFirst(V3_FORMAT_REGEX, "");
991         } else if (data.matches(V2_FORMAT_REGEX + ".*")) {
992             version = 2;
993             data = data.replaceFirst(V2_FORMAT_REGEX, "");
994         } else {
995             version = 1;
996         }
997 
998         String[] a = data.split("\\s*,\\s*", -1);
999         if (a.length < 14) {
1000             return null;
1001         }
1002 
1003         int authType;
1004         try {
1005             authType = Integer.parseInt(a[12]);
1006         } catch (NumberFormatException e) {
1007             authType = 0;
1008         }
1009 
1010         String[] typeArray;
1011         String protocol, roamingProtocol;
1012         boolean carrierEnabled;
1013         int bearerBitmask = 0;
1014         int networkTypeBitmask = 0;
1015         int profileId = 0;
1016         boolean modemCognitive = false;
1017         int maxConns = 0;
1018         int waitTime = 0;
1019         int maxConnsTime = 0;
1020         int mtu = UNSET_MTU;
1021         String mvnoType = "";
1022         String mvnoMatchData = "";
1023         int apnSetId = Carriers.NO_APN_SET_ID;
1024         int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
1025         int skip464xlat = Carriers.SKIP_464XLAT_DEFAULT;
1026         if (version == 1) {
1027             typeArray = new String[a.length - 13];
1028             System.arraycopy(a, 13, typeArray, 0, a.length - 13);
1029             protocol = PROTOCOL_INT_MAP.get(PROTOCOL_IP);
1030             roamingProtocol = PROTOCOL_INT_MAP.get(PROTOCOL_IP);
1031             carrierEnabled = true;
1032         } else {
1033             if (a.length < 18) {
1034                 return null;
1035             }
1036             typeArray = a[13].split("\\s*\\|\\s*");
1037             protocol = a[14];
1038             roamingProtocol = a[15];
1039             carrierEnabled = Boolean.parseBoolean(a[16]);
1040 
1041             bearerBitmask = ServiceState.getBitmaskFromString(a[17]);
1042 
1043             if (a.length > 22) {
1044                 modemCognitive = Boolean.parseBoolean(a[19]);
1045                 try {
1046                     profileId = Integer.parseInt(a[18]);
1047                     maxConns = Integer.parseInt(a[20]);
1048                     waitTime = Integer.parseInt(a[21]);
1049                     maxConnsTime = Integer.parseInt(a[22]);
1050                 } catch (NumberFormatException e) {
1051                 }
1052             }
1053             if (a.length > 23) {
1054                 try {
1055                     mtu = Integer.parseInt(a[23]);
1056                 } catch (NumberFormatException e) {
1057                 }
1058             }
1059             if (a.length > 25) {
1060                 mvnoType = a[24];
1061                 mvnoMatchData = a[25];
1062             }
1063             if (a.length > 26) {
1064                 networkTypeBitmask = ServiceState.getBitmaskFromString(a[26]);
1065             }
1066             if (a.length > 27) {
1067                 apnSetId = Integer.parseInt(a[27]);
1068             }
1069             if (a.length > 28) {
1070                 carrierId = Integer.parseInt(a[28]);
1071             }
1072             if (a.length > 29) {
1073                 try {
1074                     skip464xlat = Integer.parseInt(a[29]);
1075                 } catch (NumberFormatException e) {
1076                 }
1077             }
1078         }
1079 
1080         // If both bearerBitmask and networkTypeBitmask were specified, bearerBitmask would be
1081         // ignored.
1082         if (networkTypeBitmask == 0) {
1083             networkTypeBitmask =
1084                 ServiceState.convertBearerBitmaskToNetworkTypeBitmask(bearerBitmask);
1085         }
1086         return makeApnSetting(-1, a[10] + a[11], a[0], a[1], a[2],
1087             portFromString(a[3]), UriFromString(a[7]), a[8],
1088             portFromString(a[9]), a[4], a[5], authType,
1089             getApnTypesBitmaskFromString(TextUtils.join(",", typeArray)),
1090             getProtocolIntFromString(protocol), getProtocolIntFromString(roamingProtocol),
1091             carrierEnabled, networkTypeBitmask, profileId, modemCognitive, maxConns, waitTime,
1092             maxConnsTime, mtu, getMvnoTypeIntFromString(mvnoType), mvnoMatchData, apnSetId,
1093             carrierId, skip464xlat);
1094     }
1095 
1096     /**
1097      * Creates an array of ApnSetting objects from a string.
1098      *
1099      * @param data the string to read.
1100      *
1101      * Builds on top of the same format used by fromString, but allows for multiple entries
1102      * separated by ";".
1103      *
1104      * @hide
1105      */
arrayFromString(String data)1106     public static List<ApnSetting> arrayFromString(String data) {
1107         List<ApnSetting> retVal = new ArrayList<ApnSetting>();
1108         if (TextUtils.isEmpty(data)) {
1109             return retVal;
1110         }
1111         String[] apnStrings = data.split("\\s*;\\s*");
1112         for (String apnString : apnStrings) {
1113             ApnSetting apn = fromString(apnString);
1114             if (apn != null) {
1115                 retVal.add(apn);
1116             }
1117         }
1118         return retVal;
1119     }
1120 
1121     /**
1122      * Returns the string representation of ApnSetting.
1123      *
1124      * This method prints null for unset elements. The output doesn't contain password or user.
1125      * @hide
1126      */
toString()1127     public String toString() {
1128         StringBuilder sb = new StringBuilder();
1129         sb.append("[ApnSettingV7] ")
1130                 .append(mEntryName)
1131                 .append(", ").append(mId)
1132                 .append(", ").append(mOperatorNumeric)
1133                 .append(", ").append(mApnName)
1134                 .append(", ").append(mProxyAddress)
1135                 .append(", ").append(UriToString(mMmsc))
1136                 .append(", ").append(mMmsProxyAddress)
1137                 .append(", ").append(portToString(mMmsProxyPort))
1138                 .append(", ").append(portToString(mProxyPort))
1139                 .append(", ").append(mAuthType).append(", ");
1140         final String[] types = getApnTypesStringFromBitmask(mApnTypeBitmask).split(",");
1141         sb.append(TextUtils.join(" | ", types));
1142         sb.append(", ").append(PROTOCOL_INT_MAP.get(mProtocol));
1143         sb.append(", ").append(PROTOCOL_INT_MAP.get(mRoamingProtocol));
1144         sb.append(", ").append(mCarrierEnabled);
1145         sb.append(", ").append(mProfileId);
1146         sb.append(", ").append(mPersistent);
1147         sb.append(", ").append(mMaxConns);
1148         sb.append(", ").append(mWaitTime);
1149         sb.append(", ").append(mMaxConnsTime);
1150         sb.append(", ").append(mMtu);
1151         sb.append(", ").append(MVNO_TYPE_INT_MAP.get(mMvnoType));
1152         sb.append(", ").append(mMvnoMatchData);
1153         sb.append(", ").append(mPermanentFailed);
1154         sb.append(", ").append(mNetworkTypeBitmask);
1155         sb.append(", ").append(mApnSetId);
1156         sb.append(", ").append(mCarrierId);
1157         sb.append(", ").append(mSkip464Xlat);
1158         return sb.toString();
1159     }
1160 
1161     /**
1162      * Returns true if there are MVNO params specified.
1163      * @hide
1164      */
hasMvnoParams()1165     public boolean hasMvnoParams() {
1166         return !TextUtils.isEmpty(getMvnoTypeStringFromInt(mMvnoType))
1167             && !TextUtils.isEmpty(mMvnoMatchData);
1168     }
1169 
hasApnType(int type)1170     private boolean hasApnType(int type) {
1171         return (mApnTypeBitmask & type) == type;
1172     }
1173 
1174     /** @hide */
isEmergencyApn()1175     public boolean isEmergencyApn() {
1176         return hasApnType(TYPE_EMERGENCY);
1177     }
1178 
1179     /** @hide */
canHandleType(@pnType int type)1180     public boolean canHandleType(@ApnType int type) {
1181         if (!mCarrierEnabled) {
1182             return false;
1183         }
1184         // DEFAULT can handle HIPRI.
1185         if (hasApnType(type)) {
1186             return true;
1187         }
1188         return false;
1189     }
1190 
1191     // Check whether the types of two APN same (even only one type of each APN is same).
typeSameAny(ApnSetting first, ApnSetting second)1192     private boolean typeSameAny(ApnSetting first, ApnSetting second) {
1193         if (VDBG) {
1194             StringBuilder apnType1 = new StringBuilder(first.mApnName + ": ");
1195             apnType1.append(getApnTypesStringFromBitmask(first.mApnTypeBitmask));
1196 
1197             StringBuilder apnType2 = new StringBuilder(second.mApnName + ": ");
1198             apnType2.append(getApnTypesStringFromBitmask(second.mApnTypeBitmask));
1199 
1200             Rlog.d(LOG_TAG, "APN1: is " + apnType1);
1201             Rlog.d(LOG_TAG, "APN2: is " + apnType2);
1202         }
1203 
1204         if ((first.mApnTypeBitmask & second.mApnTypeBitmask) != 0) {
1205             if (VDBG) {
1206                 Rlog.d(LOG_TAG, "typeSameAny: return true");
1207             }
1208             return true;
1209         }
1210 
1211         if (VDBG) {
1212             Rlog.d(LOG_TAG, "typeSameAny: return false");
1213         }
1214         return false;
1215     }
1216 
1217     // TODO - if we have this function we should also have hashCode.
1218     // Also should handle changes in type order and perhaps case-insensitivity.
1219 
1220     /**
1221      * @hide
1222      */
equals(Object o)1223     public boolean equals(Object o) {
1224         if (o instanceof ApnSetting == false) {
1225             return false;
1226         }
1227 
1228         ApnSetting other = (ApnSetting) o;
1229 
1230         return mEntryName.equals(other.mEntryName)
1231             && Objects.equals(mId, other.mId)
1232             && Objects.equals(mOperatorNumeric, other.mOperatorNumeric)
1233             && Objects.equals(mApnName, other.mApnName)
1234             && Objects.equals(mProxyAddress, other.mProxyAddress)
1235             && Objects.equals(mMmsc, other.mMmsc)
1236             && Objects.equals(mMmsProxyAddress, other.mMmsProxyAddress)
1237             && Objects.equals(mMmsProxyPort, other.mMmsProxyPort)
1238             && Objects.equals(mProxyPort, other.mProxyPort)
1239             && Objects.equals(mUser, other.mUser)
1240             && Objects.equals(mPassword, other.mPassword)
1241             && Objects.equals(mAuthType, other.mAuthType)
1242             && Objects.equals(mApnTypeBitmask, other.mApnTypeBitmask)
1243             && Objects.equals(mProtocol, other.mProtocol)
1244             && Objects.equals(mRoamingProtocol, other.mRoamingProtocol)
1245             && Objects.equals(mCarrierEnabled, other.mCarrierEnabled)
1246             && Objects.equals(mProfileId, other.mProfileId)
1247             && Objects.equals(mPersistent, other.mPersistent)
1248             && Objects.equals(mMaxConns, other.mMaxConns)
1249             && Objects.equals(mWaitTime, other.mWaitTime)
1250             && Objects.equals(mMaxConnsTime, other.mMaxConnsTime)
1251             && Objects.equals(mMtu, other.mMtu)
1252             && Objects.equals(mMvnoType, other.mMvnoType)
1253             && Objects.equals(mMvnoMatchData, other.mMvnoMatchData)
1254             && Objects.equals(mNetworkTypeBitmask, other.mNetworkTypeBitmask)
1255             && Objects.equals(mApnSetId, other.mApnSetId)
1256             && Objects.equals(mCarrierId, other.mCarrierId)
1257             && Objects.equals(mSkip464Xlat, other.mSkip464Xlat);
1258     }
1259 
1260     /**
1261      * Compare two APN settings
1262      *
1263      * Note: This method does not compare 'mId', 'mNetworkTypeBitmask'. We only use this for
1264      * determining if tearing a data call is needed when conditions change. See
1265      * cleanUpConnectionsOnUpdatedApns in DcTracker.
1266      *
1267      * @param o the other object to compare
1268      * @param isDataRoaming True if the device is on data roaming
1269      * @return True if the two APN settings are same
1270      * @hide
1271      */
equals(Object o, boolean isDataRoaming)1272     public boolean equals(Object o, boolean isDataRoaming) {
1273         if (!(o instanceof ApnSetting)) {
1274             return false;
1275         }
1276 
1277         ApnSetting other = (ApnSetting) o;
1278 
1279         return mEntryName.equals(other.mEntryName)
1280             && Objects.equals(mOperatorNumeric, other.mOperatorNumeric)
1281             && Objects.equals(mApnName, other.mApnName)
1282             && Objects.equals(mProxyAddress, other.mProxyAddress)
1283             && Objects.equals(mMmsc, other.mMmsc)
1284             && Objects.equals(mMmsProxyAddress, other.mMmsProxyAddress)
1285             && Objects.equals(mMmsProxyPort, other.mMmsProxyPort)
1286             && Objects.equals(mProxyPort, other.mProxyPort)
1287             && Objects.equals(mUser, other.mUser)
1288             && Objects.equals(mPassword, other.mPassword)
1289             && Objects.equals(mAuthType, other.mAuthType)
1290             && Objects.equals(mApnTypeBitmask, other.mApnTypeBitmask)
1291             && (isDataRoaming || Objects.equals(mProtocol, other.mProtocol))
1292             && (!isDataRoaming || Objects.equals(mRoamingProtocol, other.mRoamingProtocol))
1293             && Objects.equals(mCarrierEnabled, other.mCarrierEnabled)
1294             && Objects.equals(mProfileId, other.mProfileId)
1295             && Objects.equals(mPersistent, other.mPersistent)
1296             && Objects.equals(mMaxConns, other.mMaxConns)
1297             && Objects.equals(mWaitTime, other.mWaitTime)
1298             && Objects.equals(mMaxConnsTime, other.mMaxConnsTime)
1299             && Objects.equals(mMtu, other.mMtu)
1300             && Objects.equals(mMvnoType, other.mMvnoType)
1301             && Objects.equals(mMvnoMatchData, other.mMvnoMatchData)
1302             && Objects.equals(mApnSetId, other.mApnSetId)
1303             && Objects.equals(mCarrierId, other.mCarrierId)
1304             && Objects.equals(mSkip464Xlat, other.mSkip464Xlat);
1305     }
1306 
1307     /**
1308      * Check if neither mention DUN and are substantially similar
1309      *
1310      * @param other The other APN settings to compare
1311      * @return True if two APN settings are similar
1312      * @hide
1313      */
similar(ApnSetting other)1314     public boolean similar(ApnSetting other) {
1315         return (!this.canHandleType(TYPE_DUN)
1316             && !other.canHandleType(TYPE_DUN)
1317             && Objects.equals(this.mApnName, other.mApnName)
1318             && !typeSameAny(this, other)
1319             && xorEqualsString(this.mProxyAddress, other.mProxyAddress)
1320             && xorEqualsInt(this.mProxyPort, other.mProxyPort)
1321             && xorEquals(this.mProtocol, other.mProtocol)
1322             && xorEquals(this.mRoamingProtocol, other.mRoamingProtocol)
1323             && Objects.equals(this.mCarrierEnabled, other.mCarrierEnabled)
1324             && Objects.equals(this.mProfileId, other.mProfileId)
1325             && Objects.equals(this.mMvnoType, other.mMvnoType)
1326             && Objects.equals(this.mMvnoMatchData, other.mMvnoMatchData)
1327             && xorEquals(this.mMmsc, other.mMmsc)
1328             && xorEqualsString(this.mMmsProxyAddress, other.mMmsProxyAddress)
1329             && xorEqualsInt(this.mMmsProxyPort, other.mMmsProxyPort))
1330             && Objects.equals(this.mNetworkTypeBitmask, other.mNetworkTypeBitmask)
1331             && Objects.equals(mApnSetId, other.mApnSetId)
1332             && Objects.equals(mCarrierId, other.mCarrierId)
1333             && Objects.equals(mSkip464Xlat, other.mSkip464Xlat);
1334     }
1335 
1336     // Equal or one is null.
xorEquals(Object first, Object second)1337     private boolean xorEquals(Object first, Object second) {
1338         return first == null || second == null || first.equals(second);
1339     }
1340 
1341     // Equal or one is null.
xorEqualsString(String first, String second)1342     private boolean xorEqualsString(String first, String second) {
1343         return TextUtils.isEmpty(first) || TextUtils.isEmpty(second) || first.equals(second);
1344     }
1345 
1346     // Equal or one is not specified.
xorEqualsInt(int first, int second)1347     private boolean xorEqualsInt(int first, int second) {
1348         return first == UNSPECIFIED_INT || second == UNSPECIFIED_INT
1349             || Objects.equals(first, second);
1350     }
1351 
nullToEmpty(String stringValue)1352     private String nullToEmpty(String stringValue) {
1353         return stringValue == null ? UNSPECIFIED_STRING : stringValue;
1354     }
1355 
1356     /**
1357      * @hide
1358      * Called by {@link android.app.admin.DevicePolicyManager} to convert this APN into
1359      * ContentValue. If a field is not specified then we put "" instead of null.
1360      */
toContentValues()1361     public ContentValues toContentValues() {
1362         ContentValues apnValue = new ContentValues();
1363         apnValue.put(Telephony.Carriers.NUMERIC, nullToEmpty(mOperatorNumeric));
1364         apnValue.put(Telephony.Carriers.NAME, nullToEmpty(mEntryName));
1365         apnValue.put(Telephony.Carriers.APN, nullToEmpty(mApnName));
1366         apnValue.put(Telephony.Carriers.PROXY, nullToEmpty(mProxyAddress));
1367         apnValue.put(Telephony.Carriers.PORT, nullToEmpty(portToString(mProxyPort)));
1368         apnValue.put(Telephony.Carriers.MMSC, nullToEmpty(UriToString(mMmsc)));
1369         apnValue.put(Telephony.Carriers.MMSPORT, nullToEmpty(portToString(mMmsProxyPort)));
1370         apnValue.put(Telephony.Carriers.MMSPROXY, nullToEmpty(
1371                 mMmsProxyAddress));
1372         apnValue.put(Telephony.Carriers.USER, nullToEmpty(mUser));
1373         apnValue.put(Telephony.Carriers.PASSWORD, nullToEmpty(mPassword));
1374         apnValue.put(Telephony.Carriers.AUTH_TYPE, mAuthType);
1375         String apnType = getApnTypesStringFromBitmask(mApnTypeBitmask);
1376         apnValue.put(Telephony.Carriers.TYPE, nullToEmpty(apnType));
1377         apnValue.put(Telephony.Carriers.PROTOCOL,
1378                 getProtocolStringFromInt(mProtocol));
1379         apnValue.put(Telephony.Carriers.ROAMING_PROTOCOL,
1380                 getProtocolStringFromInt(mRoamingProtocol));
1381         apnValue.put(Telephony.Carriers.CARRIER_ENABLED, mCarrierEnabled);
1382         apnValue.put(Telephony.Carriers.MVNO_TYPE, getMvnoTypeStringFromInt(mMvnoType));
1383         apnValue.put(Telephony.Carriers.NETWORK_TYPE_BITMASK, mNetworkTypeBitmask);
1384         apnValue.put(Telephony.Carriers.CARRIER_ID, mCarrierId);
1385         apnValue.put(Telephony.Carriers.SKIP_464XLAT, mSkip464Xlat);
1386 
1387         return apnValue;
1388     }
1389 
1390     /**
1391      * Get supported APN types
1392      *
1393      * @return list of APN types
1394      * @hide
1395      */
1396     @ApnType
getApnTypes()1397     public List<Integer> getApnTypes() {
1398         List<Integer> types = new ArrayList<>();
1399         for (Integer type : APN_TYPE_INT_MAP.keySet()) {
1400             if ((mApnTypeBitmask & type) == type) {
1401                 types.add(type);
1402             }
1403         }
1404         return types;
1405     }
1406 
1407     /**
1408      * Converts the integer value of an APN type to the string version.
1409      * @param apnTypeBitmask bitmask of APN types.
1410      * @return comma delimited list of APN types.
1411      * @hide
1412      */
1413     @NonNull
getApnTypesStringFromBitmask(int apnTypeBitmask)1414     public static String getApnTypesStringFromBitmask(int apnTypeBitmask) {
1415         List<String> types = new ArrayList<>();
1416         for (Integer type : APN_TYPE_INT_MAP.keySet()) {
1417             if ((apnTypeBitmask & type) == type) {
1418                 types.add(APN_TYPE_INT_MAP.get(type));
1419             }
1420         }
1421         return TextUtils.join(",", types);
1422     }
1423 
1424     /**
1425      * @param apnType APN type
1426      * @return APN type in string format
1427      * @hide
1428      */
getApnTypeString(int apnType)1429     public static String getApnTypeString(int apnType) {
1430         if (apnType == TYPE_ALL) {
1431             return "*";
1432         }
1433         String apnTypeString = APN_TYPE_INT_MAP.get(apnType);
1434         return apnTypeString == null ? "Unknown" : apnTypeString;
1435     }
1436 
1437     /**
1438      * @param types comma delimited list of APN types.
1439      * @return bitmask of APN types.
1440      * @hide
1441      */
getApnTypesBitmaskFromString(String types)1442     public static int getApnTypesBitmaskFromString(String types) {
1443         // If unset, set to ALL.
1444         if (TextUtils.isEmpty(types)) {
1445             return TYPE_ALL;
1446         } else {
1447             int result = 0;
1448             for (String str : types.split(",")) {
1449                 Integer type = APN_TYPE_STRING_MAP.get(str.toLowerCase());
1450                 if (type != null) {
1451                     result |= type;
1452                 }
1453             }
1454             return result;
1455         }
1456     }
1457 
1458     /** @hide */
getMvnoTypeIntFromString(String mvnoType)1459     public static int getMvnoTypeIntFromString(String mvnoType) {
1460         String mvnoTypeString = TextUtils.isEmpty(mvnoType) ? mvnoType : mvnoType.toLowerCase();
1461         Integer mvnoTypeInt = MVNO_TYPE_STRING_MAP.get(mvnoTypeString);
1462         return  mvnoTypeInt == null ? UNSPECIFIED_INT : mvnoTypeInt;
1463     }
1464 
1465     /** @hide */
getMvnoTypeStringFromInt(int mvnoType)1466     public static String getMvnoTypeStringFromInt(int mvnoType) {
1467         String mvnoTypeString = MVNO_TYPE_INT_MAP.get(mvnoType);
1468         return  mvnoTypeString == null ? UNSPECIFIED_STRING : mvnoTypeString;
1469     }
1470 
1471     /** @hide */
getProtocolIntFromString(String protocol)1472     public static int getProtocolIntFromString(String protocol) {
1473         Integer protocolInt = PROTOCOL_STRING_MAP.get(protocol);
1474         return  protocolInt == null ? UNSPECIFIED_INT : protocolInt;
1475     }
1476 
1477     /** @hide */
getProtocolStringFromInt(int protocol)1478     public static String getProtocolStringFromInt(int protocol) {
1479         String protocolString = PROTOCOL_INT_MAP.get(protocol);
1480         return  protocolString == null ? UNSPECIFIED_STRING : protocolString;
1481     }
1482 
UriFromString(String uri)1483     private static Uri UriFromString(String uri) {
1484         return TextUtils.isEmpty(uri) ? null : Uri.parse(uri);
1485     }
1486 
UriToString(Uri uri)1487     private static String UriToString(Uri uri) {
1488         return uri == null ? null : uri.toString();
1489     }
1490 
1491     /** @hide */
inetAddressFromString(String inetAddress)1492     public static InetAddress inetAddressFromString(String inetAddress) {
1493         if (TextUtils.isEmpty(inetAddress)) {
1494             return null;
1495         }
1496         try {
1497             return InetAddress.getByName(inetAddress);
1498         } catch (UnknownHostException e) {
1499             Log.e(LOG_TAG, "Can't parse InetAddress from string: unknown host.");
1500             return null;
1501         }
1502     }
1503 
1504     /** @hide */
inetAddressToString(InetAddress inetAddress)1505     public static String inetAddressToString(InetAddress inetAddress) {
1506         if (inetAddress == null) {
1507             return null;
1508         }
1509         final String inetAddressString = inetAddress.toString();
1510         if (TextUtils.isEmpty(inetAddressString)) {
1511             return null;
1512         }
1513         final String hostName = inetAddressString.substring(0, inetAddressString.indexOf("/"));
1514         final String address = inetAddressString.substring(inetAddressString.indexOf("/") + 1);
1515         if (TextUtils.isEmpty(hostName) && TextUtils.isEmpty(address)) {
1516             return null;
1517         }
1518         return TextUtils.isEmpty(hostName) ? address : hostName;
1519     }
1520 
portFromString(String strPort)1521     private static int portFromString(String strPort) {
1522         int port = UNSPECIFIED_INT;
1523         if (!TextUtils.isEmpty(strPort)) {
1524             try {
1525                 port = Integer.parseInt(strPort);
1526             } catch (NumberFormatException e) {
1527                 Log.e(LOG_TAG, "Can't parse port from String");
1528             }
1529         }
1530         return port;
1531     }
1532 
portToString(int port)1533     private static String portToString(int port) {
1534         return port == UNSPECIFIED_INT ? null : Integer.toString(port);
1535     }
1536 
1537     /**
1538      * Check if this APN setting can support the given network
1539      *
1540      * @param networkType The network type
1541      * @return {@code true} if this APN setting can support the given network.
1542      *
1543      * @hide
1544      */
canSupportNetworkType(@etworkType int networkType)1545     public boolean canSupportNetworkType(@NetworkType int networkType) {
1546         // Do a special checking for GSM. In reality, GSM is a voice only network type and can never
1547         // be used for data. We allow it here because in some DSDS corner cases, on the non-DDS
1548         // sub, modem reports data rat unknown. In that case if voice is GSM and this APN supports
1549         // GPRS or EDGE, this APN setting should be selected.
1550         if (networkType == TelephonyManager.NETWORK_TYPE_GSM
1551                 && (mNetworkTypeBitmask & (TelephonyManager.NETWORK_TYPE_BITMASK_GPRS
1552                 | TelephonyManager.NETWORK_TYPE_BITMASK_EDGE)) != 0) {
1553             return true;
1554         }
1555 
1556         return ServiceState.bitmaskHasTech(mNetworkTypeBitmask, networkType);
1557     }
1558 
1559     // Implement Parcelable.
1560     @Override
1561     /** @hide */
describeContents()1562     public int describeContents() {
1563         return 0;
1564     }
1565 
1566     @Override
1567     /** @hide */
writeToParcel(@onNull Parcel dest, int flags)1568     public void writeToParcel(@NonNull Parcel dest, int flags) {
1569         dest.writeInt(mId);
1570         dest.writeString(mOperatorNumeric);
1571         dest.writeString(mEntryName);
1572         dest.writeString(mApnName);
1573         dest.writeString(mProxyAddress);
1574         dest.writeInt(mProxyPort);
1575         dest.writeValue(mMmsc);
1576         dest.writeString(mMmsProxyAddress);
1577         dest.writeInt(mMmsProxyPort);
1578         dest.writeString(mUser);
1579         dest.writeString(mPassword);
1580         dest.writeInt(mAuthType);
1581         dest.writeInt(mApnTypeBitmask);
1582         dest.writeInt(mProtocol);
1583         dest.writeInt(mRoamingProtocol);
1584         dest.writeBoolean(mCarrierEnabled);
1585         dest.writeInt(mMvnoType);
1586         dest.writeInt(mNetworkTypeBitmask);
1587         dest.writeInt(mApnSetId);
1588         dest.writeInt(mCarrierId);
1589         dest.writeInt(mSkip464Xlat);
1590     }
1591 
readFromParcel(Parcel in)1592     private static ApnSetting readFromParcel(Parcel in) {
1593         final int id = in.readInt();
1594         final String operatorNumeric = in.readString();
1595         final String entryName = in.readString();
1596         final String apnName = in.readString();
1597         final String proxy = in.readString();
1598         final int port = in.readInt();
1599         final Uri mmsc = (Uri) in.readValue(Uri.class.getClassLoader());
1600         final String mmsProxy = in.readString();
1601         final int mmsPort = in.readInt();
1602         final String user = in.readString();
1603         final String password = in.readString();
1604         final int authType = in.readInt();
1605         final int apnTypesBitmask = in.readInt();
1606         final int protocol = in.readInt();
1607         final int roamingProtocol = in.readInt();
1608         final boolean carrierEnabled = in.readBoolean();
1609         final int mvnoType = in.readInt();
1610         final int networkTypeBitmask = in.readInt();
1611         final int apnSetId = in.readInt();
1612         final int carrierId = in.readInt();
1613         final int skip464xlat = in.readInt();
1614 
1615         return makeApnSetting(id, operatorNumeric, entryName, apnName,
1616                 proxy, port, mmsc, mmsProxy, mmsPort, user, password, authType, apnTypesBitmask,
1617                 protocol, roamingProtocol, carrierEnabled, networkTypeBitmask, 0, false,
1618                 0, 0, 0, 0, mvnoType, null, apnSetId, carrierId, skip464xlat);
1619     }
1620 
1621     public static final @android.annotation.NonNull Parcelable.Creator<ApnSetting> CREATOR =
1622             new Parcelable.Creator<ApnSetting>() {
1623                 @Override
1624                 public ApnSetting createFromParcel(Parcel in) {
1625                     return readFromParcel(in);
1626                 }
1627 
1628                 @Override
1629                 public ApnSetting[] newArray(int size) {
1630                     return new ApnSetting[size];
1631                 }
1632             };
1633 
1634     /**
1635      * Provides a convenient way to set the fields of a {@link ApnSetting} when creating a new
1636      * instance. The following settings are required to build an {@code ApnSetting}:
1637      *
1638      * <ul><li>apnTypeBitmask</li>
1639      * <li>apnName</li>
1640      * <li>entryName</li></ul>
1641      *
1642      * <p>The example below shows how you might create a new {@code ApnSetting}:
1643      *
1644      * <pre><code>
1645      * // Create an MMS proxy address with a hostname. A network might not be
1646      * // available, so supply a dummy (0.0.0.0) IPv4 address to avoid DNS lookup.
1647      * String host = "mms.example.com";
1648      * byte[] ipAddress = new byte[4];
1649      * InetAddress mmsProxy;
1650      * try {
1651      *   mmsProxy = InetAddress.getByAddress(host, ipAddress);
1652      * } catch (UnknownHostException e) {
1653      *   e.printStackTrace();
1654      *   return;
1655      * }
1656      *
1657      * ApnSetting apn = new ApnSetting.Builder()
1658      *     .setApnTypeBitmask(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS)
1659      *     .setApnName("apn.example.com")
1660      *     .setEntryName("Example Carrier APN")
1661      *     .setMmsc(Uri.parse("http://mms.example.com:8002"))
1662      *     .setMmsProxyAddress(mmsProxy)
1663      *     .setMmsProxyPort(8799)
1664      *     .build();
1665      * </code></pre>
1666      */
1667     public static class Builder{
1668         private String mEntryName;
1669         private String mApnName;
1670         private String mProxyAddress;
1671         private int mProxyPort = UNSPECIFIED_INT;
1672         private Uri mMmsc;
1673         private String mMmsProxyAddress;
1674         private int mMmsProxyPort = UNSPECIFIED_INT;
1675         private String mUser;
1676         private String mPassword;
1677         private int mAuthType;
1678         private int mApnTypeBitmask;
1679         private int mId;
1680         private String mOperatorNumeric;
1681         private int mProtocol = UNSPECIFIED_INT;
1682         private int mRoamingProtocol = UNSPECIFIED_INT;
1683         private int mMtu;
1684         private int mNetworkTypeBitmask;
1685         private boolean mCarrierEnabled;
1686         private int mProfileId;
1687         private boolean mModemCognitive;
1688         private int mMaxConns;
1689         private int mWaitTime;
1690         private int mMaxConnsTime;
1691         private int mMvnoType = UNSPECIFIED_INT;
1692         private String mMvnoMatchData;
1693         private int mApnSetId;
1694         private int mCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
1695         private int mSkip464Xlat = Carriers.SKIP_464XLAT_DEFAULT;
1696 
1697         /**
1698          * Default constructor for Builder.
1699          */
Builder()1700         public Builder() {}
1701 
1702         /**
1703          * Sets the unique database id for this entry.
1704          *
1705          * @param id the unique database id to set for this entry
1706          */
setId(int id)1707         private Builder setId(int id) {
1708             this.mId = id;
1709             return this;
1710         }
1711 
1712         /**
1713          * Set the MTU size of the mobile interface to which the APN connected.
1714          *
1715          * @param mtu the MTU size to set for the APN
1716          * @hide
1717          */
setMtu(int mtu)1718         public Builder setMtu(int mtu) {
1719             this.mMtu = mtu;
1720             return this;
1721         }
1722 
1723         /**
1724          * Sets the profile id to which the APN saved in modem.
1725          *
1726          * @param profileId the profile id to set for the APN
1727          * @hide
1728          */
setProfileId(int profileId)1729         public Builder setProfileId(int profileId) {
1730             this.mProfileId = profileId;
1731             return this;
1732         }
1733 
1734         /**
1735          * Sets if the APN setting is to be set in modem.
1736          *
1737          * @param modemCognitive if the APN setting is to be set in modem
1738          * @hide
1739          */
setModemCognitive(boolean modemCognitive)1740         public Builder setModemCognitive(boolean modemCognitive) {
1741             this.mModemCognitive = modemCognitive;
1742             return this;
1743         }
1744 
1745         /**
1746          * Sets the max connections of this APN.
1747          *
1748          * @param maxConns the max connections of this APN
1749          * @hide
1750          */
setMaxConns(int maxConns)1751         public Builder setMaxConns(int maxConns) {
1752             this.mMaxConns = maxConns;
1753             return this;
1754         }
1755 
1756         /**
1757          * Sets the wait time for retry of the APN.
1758          *
1759          * @param waitTime the wait time for retry of the APN
1760          * @hide
1761          */
setWaitTime(int waitTime)1762         public Builder setWaitTime(int waitTime) {
1763             this.mWaitTime = waitTime;
1764             return this;
1765         }
1766 
1767         /**
1768          * Sets the time to limit max connection for the APN.
1769          *
1770          * @param maxConnsTime the time to limit max connection for the APN
1771          * @hide
1772          */
setMaxConnsTime(int maxConnsTime)1773         public Builder setMaxConnsTime(int maxConnsTime) {
1774             this.mMaxConnsTime = maxConnsTime;
1775             return this;
1776         }
1777 
1778         /**
1779          * Sets the MVNO match data for the APN.
1780          *
1781          * @param mvnoMatchData the MVNO match data for the APN
1782          * @hide
1783          */
setMvnoMatchData(@ullable String mvnoMatchData)1784         public Builder setMvnoMatchData(@Nullable String mvnoMatchData) {
1785             this.mMvnoMatchData = mvnoMatchData;
1786             return this;
1787         }
1788 
1789         /**
1790          * Sets the APN set id for the APN.
1791          *
1792          * @param apnSetId the set id for the APN
1793          * @hide
1794          */
setApnSetId(int apnSetId)1795         public Builder setApnSetId(int apnSetId) {
1796             this.mApnSetId = apnSetId;
1797             return this;
1798         }
1799 
1800         /**
1801          * Sets a human-readable name that describes the APN.
1802          *
1803          * @param entryName the entry name to set for the APN
1804          */
1805         @NonNull
setEntryName(@ullable String entryName)1806         public Builder setEntryName(@Nullable String entryName) {
1807             this.mEntryName = entryName;
1808             return this;
1809         }
1810 
1811         /**
1812          * Sets the name of the APN.
1813          *
1814          * @param apnName the name to set for the APN
1815          */
1816         @NonNull
setApnName(@ullable String apnName)1817         public Builder setApnName(@Nullable String apnName) {
1818             this.mApnName = apnName;
1819             return this;
1820         }
1821 
1822         /**
1823          * Sets the address of an HTTP proxy for the APN. The proxy address can be an IP address or
1824          * hostname. If {@code proxy} contains both an IP address and hostname, this method ignores
1825          * the IP address.
1826          *
1827          * <p>The {@link java.net.InetAddress} methods
1828          * {@link java.net.InetAddress#getAllByName getAllByName()} require DNS for hostname
1829          * resolution. To avoid this requirement when setting a hostname, call
1830          * {@link java.net.InetAddress#getByAddress(java.lang.String, byte[])} with both the
1831          * hostname and a dummy IP address. See {@link ApnSetting.Builder above} for an example.
1832          *
1833          * @param proxy the proxy address to set for the APN
1834          * @deprecated use {@link #setProxyAddress(String)} instead.
1835          */
1836         @Deprecated
setProxyAddress(InetAddress proxy)1837         public Builder setProxyAddress(InetAddress proxy) {
1838             this.mProxyAddress = inetAddressToString(proxy);
1839             return this;
1840         }
1841 
1842         /**
1843          * Sets the proxy address of the APN.
1844          *
1845          * @param proxy the proxy address to set for the APN
1846          */
1847         @NonNull
setProxyAddress(@ullable String proxy)1848         public Builder setProxyAddress(@Nullable String proxy) {
1849             this.mProxyAddress = proxy;
1850             return this;
1851         }
1852 
1853         /**
1854          * Sets the proxy port of the APN.
1855          *
1856          * @param port the proxy port to set for the APN
1857          */
1858         @NonNull
setProxyPort(int port)1859         public Builder setProxyPort(int port) {
1860             this.mProxyPort = port;
1861             return this;
1862         }
1863 
1864         /**
1865          * Sets the MMSC Uri of the APN.
1866          *
1867          * @param mmsc the MMSC Uri to set for the APN
1868          */
1869         @NonNull
setMmsc(@ullable Uri mmsc)1870         public Builder setMmsc(@Nullable Uri mmsc) {
1871             this.mMmsc = mmsc;
1872             return this;
1873         }
1874 
1875         /**
1876          * Sets the address of an MMS proxy for the APN. The MMS proxy address can be an IP address
1877          * or hostname. If {@code mmsProxy} contains both an IP address and hostname, this method
1878          * ignores the IP address.
1879          *
1880          * <p>The {@link java.net.InetAddress} methods
1881          * {@link java.net.InetAddress#getByName getByName()} and
1882          * {@link java.net.InetAddress#getAllByName getAllByName()} require DNS for hostname
1883          * resolution. To avoid this requirement when setting a hostname, call
1884          * {@link java.net.InetAddress#getByAddress(java.lang.String, byte[])} with both the
1885          * hostname and a dummy IP address. See {@link ApnSetting.Builder above} for an example.
1886          *
1887          * @param mmsProxy the MMS proxy address to set for the APN
1888          * @deprecated use {@link #setMmsProxyAddress(String)} instead.
1889          */
1890         @Deprecated
setMmsProxyAddress(InetAddress mmsProxy)1891         public Builder setMmsProxyAddress(InetAddress mmsProxy) {
1892             this.mMmsProxyAddress = inetAddressToString(mmsProxy);
1893             return this;
1894         }
1895 
1896         /**
1897          * Sets the MMS proxy address of the APN.
1898          *
1899          * @param mmsProxy the MMS proxy address to set for the APN
1900          */
1901         @NonNull
setMmsProxyAddress(@ullable String mmsProxy)1902         public Builder setMmsProxyAddress(@Nullable String mmsProxy) {
1903             this.mMmsProxyAddress = mmsProxy;
1904             return this;
1905         }
1906 
1907         /**
1908          * Sets the MMS proxy port of the APN.
1909          *
1910          * @param mmsPort the MMS proxy port to set for the APN
1911          */
1912         @NonNull
setMmsProxyPort(int mmsPort)1913         public Builder setMmsProxyPort(int mmsPort) {
1914             this.mMmsProxyPort = mmsPort;
1915             return this;
1916         }
1917 
1918         /**
1919          * Sets the APN username of the APN.
1920          *
1921          * @param user the APN username to set for the APN
1922          */
1923         @NonNull
setUser(@ullable String user)1924         public Builder setUser(@Nullable String user) {
1925             this.mUser = user;
1926             return this;
1927         }
1928 
1929         /**
1930          * Sets the APN password of the APN.
1931          *
1932          * @see android.provider.Telephony.Carriers#PASSWORD
1933          * @param password the APN password to set for the APN
1934          */
1935         @NonNull
setPassword(@ullable String password)1936         public Builder setPassword(@Nullable String password) {
1937             this.mPassword = password;
1938             return this;
1939         }
1940 
1941         /**
1942          * Sets the authentication type of the APN.
1943          *
1944          * @param authType the authentication type to set for the APN
1945          */
1946         @NonNull
setAuthType(@uthType int authType)1947         public Builder setAuthType(@AuthType int authType) {
1948             this.mAuthType = authType;
1949             return this;
1950         }
1951 
1952         /**
1953          * Sets the bitmask of APN types.
1954          *
1955          * <p>Apn types are usage categories for an APN entry. One APN entry may support multiple
1956          * APN types, eg, a single APN may service regular internet traffic ("default") as well as
1957          * MMS-specific connections.
1958          *
1959          * <p>The bitmask of APN types is calculated from APN types defined in {@link ApnSetting}.
1960          *
1961          * @param apnTypeBitmask a bitmask describing the types of the APN
1962          */
1963         @NonNull
setApnTypeBitmask(@pnType int apnTypeBitmask)1964         public Builder setApnTypeBitmask(@ApnType int apnTypeBitmask) {
1965             this.mApnTypeBitmask = apnTypeBitmask;
1966             return this;
1967         }
1968 
1969         /**
1970          * Sets the numeric operator ID for the APN. Numeric operator ID is defined as
1971          * {@link android.provider.Telephony.Carriers#MCC} +
1972          * {@link android.provider.Telephony.Carriers#MNC}.
1973          *
1974          * @param operatorNumeric the numeric operator ID to set for this entry
1975          */
1976         @NonNull
setOperatorNumeric(@ullable String operatorNumeric)1977         public Builder setOperatorNumeric(@Nullable String operatorNumeric) {
1978             this.mOperatorNumeric = operatorNumeric;
1979             return this;
1980         }
1981 
1982         /**
1983          * Sets the protocol to use to connect to this APN.
1984          *
1985          * <p>Protocol is one of the {@code PDP_type} values in TS 27.007 section 10.1.1.
1986          *
1987          * @param protocol the protocol to set to use to connect to this APN
1988          */
1989         @NonNull
setProtocol(@rotocolType int protocol)1990         public Builder setProtocol(@ProtocolType int protocol) {
1991             this.mProtocol = protocol;
1992             return this;
1993         }
1994 
1995         /**
1996          * Sets the protocol to use to connect to this APN when the device is roaming.
1997          *
1998          * <p>Roaming protocol is one of the {@code PDP_type} values in TS 27.007 section 10.1.1.
1999          *
2000          * @param roamingProtocol the protocol to set to use to connect to this APN when roaming
2001          */
2002         @NonNull
setRoamingProtocol(@rotocolType int roamingProtocol)2003         public Builder setRoamingProtocol(@ProtocolType  int roamingProtocol) {
2004             this.mRoamingProtocol = roamingProtocol;
2005             return this;
2006         }
2007 
2008         /**
2009          * Sets the current status for this APN.
2010          *
2011          * @param carrierEnabled the current status to set for this APN
2012          */
2013         @NonNull
setCarrierEnabled(boolean carrierEnabled)2014         public Builder setCarrierEnabled(boolean carrierEnabled) {
2015             this.mCarrierEnabled = carrierEnabled;
2016             return this;
2017         }
2018 
2019         /**
2020          * Sets Radio Technology (Network Type) info for this APN.
2021          *
2022          * @param networkTypeBitmask the Radio Technology (Network Type) info
2023          */
2024         @NonNull
setNetworkTypeBitmask(int networkTypeBitmask)2025         public Builder setNetworkTypeBitmask(int networkTypeBitmask) {
2026             this.mNetworkTypeBitmask = networkTypeBitmask;
2027             return this;
2028         }
2029 
2030         /**
2031          * Sets the MVNO match type for this APN.
2032          *
2033          * @param mvnoType the MVNO match type to set for this APN
2034          */
2035         @NonNull
setMvnoType(@vnoType int mvnoType)2036         public Builder setMvnoType(@MvnoType int mvnoType) {
2037             this.mMvnoType = mvnoType;
2038             return this;
2039         }
2040 
2041         /**
2042          * Sets the carrier id for this APN.
2043          *
2044          * See {@link TelephonyManager#getSimCarrierId()} which provides more background for what a
2045          * carrier ID is.
2046          *
2047          * @param carrierId the carrier id to set for this APN
2048          */
2049         @NonNull
setCarrierId(int carrierId)2050         public Builder setCarrierId(int carrierId) {
2051             this.mCarrierId = carrierId;
2052             return this;
2053         }
2054 
2055         /**
2056          * Sets skip464xlat flag for this APN.
2057          *
2058          * @param skip464xlat skip464xlat for this APN.
2059          * @hide
2060          */
setSkip464Xlat(@kip464XlatStatus int skip464xlat)2061         public Builder setSkip464Xlat(@Skip464XlatStatus int skip464xlat) {
2062             this.mSkip464Xlat = skip464xlat;
2063             return this;
2064         }
2065 
2066         /**
2067          * Builds {@link ApnSetting} from this builder.
2068          *
2069          * @return {@code null} if {@link #setApnName(String)} or {@link #setEntryName(String)}
2070          * is empty, or {@link #setApnTypeBitmask(int)} doesn't contain a valid bit,
2071          * {@link ApnSetting} built from this builder otherwise.
2072          */
build()2073         public ApnSetting build() {
2074             if ((mApnTypeBitmask & (TYPE_DEFAULT | TYPE_MMS | TYPE_SUPL | TYPE_DUN | TYPE_HIPRI
2075                     | TYPE_FOTA | TYPE_IMS | TYPE_CBS | TYPE_IA | TYPE_EMERGENCY | TYPE_MCX
2076                     | TYPE_XCAP)) == 0
2077                 || TextUtils.isEmpty(mApnName) || TextUtils.isEmpty(mEntryName)) {
2078                 return null;
2079             }
2080             return new ApnSetting(this);
2081         }
2082 
2083         /**
2084          * Builds {@link ApnSetting} from this builder. This function doesn't check if
2085          * {@link #setApnName(String)} or {@link #setEntryName(String)}, or
2086          * {@link #setApnTypeBitmask(int)} is empty.
2087          * @hide
2088          */
buildWithoutCheck()2089         public ApnSetting buildWithoutCheck() {
2090             return new ApnSetting(this);
2091         }
2092     }
2093 }
2094