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