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