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