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