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.content.ContentValues;
21 import android.database.Cursor;
22 import android.hardware.radio.V1_0.ApnTypes;
23 import android.net.Uri;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 import android.provider.Telephony;
27 import android.telephony.Rlog;
28 import android.telephony.ServiceState;
29 import android.telephony.TelephonyManager;
30 import android.text.TextUtils;
31 import android.util.ArrayMap;
32 import android.util.Log;
33 import java.lang.annotation.Retention;
34 import java.lang.annotation.RetentionPolicy;
35 import java.net.InetAddress;
36 import java.net.UnknownHostException;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Objects;
41 
42 /**
43  * An Access Point Name (APN) configuration for a carrier data connection.
44  *
45  * <p>The APN provides configuration to connect a cellular network device to an IP data network. A
46  * carrier uses the name, type and other configuration in an {@code APNSetting} to decide which IP
47  * address to assign, any security methods to apply, and how the device might be connected to
48  * private networks.
49  *
50  * <p>Use {@link ApnSetting.Builder} to create new instances.
51  */
52 public class ApnSetting implements Parcelable {
53 
54     private static final String LOG_TAG = "ApnSetting";
55     private static final boolean VDBG = false;
56 
57     private static final Map<String, Integer> APN_TYPE_STRING_MAP;
58     private static final Map<Integer, String> APN_TYPE_INT_MAP;
59     private static final Map<String, Integer> PROTOCOL_STRING_MAP;
60     private static final Map<Integer, String> PROTOCOL_INT_MAP;
61     private static final Map<String, Integer> MVNO_TYPE_STRING_MAP;
62     private static final Map<Integer, String> MVNO_TYPE_INT_MAP;
63     private static final int NOT_IN_MAP_INT = -1;
64     private static final int NO_PORT_SPECIFIED = -1;
65 
66     /** All APN types except IA. */
67     private static final int TYPE_ALL_BUT_IA = ApnTypes.ALL & (~ApnTypes.IA);
68 
69     /** APN type for default data traffic and HiPri traffic. */
70     public static final int TYPE_DEFAULT = ApnTypes.DEFAULT | ApnTypes.HIPRI;
71     /** APN type for MMS traffic. */
72     public static final int TYPE_MMS = ApnTypes.MMS;
73     /** APN type for SUPL assisted GPS. */
74     public static final int TYPE_SUPL = ApnTypes.SUPL;
75     /** APN type for DUN traffic. */
76     public static final int TYPE_DUN = ApnTypes.DUN;
77     /** APN type for HiPri traffic. */
78     public static final int TYPE_HIPRI = ApnTypes.HIPRI;
79     /** APN type for accessing the carrier's FOTA portal, used for over the air updates. */
80     public static final int TYPE_FOTA = ApnTypes.FOTA;
81     /** APN type for IMS. */
82     public static final int TYPE_IMS = ApnTypes.IMS;
83     /** APN type for CBS. */
84     public static final int TYPE_CBS = ApnTypes.CBS;
85     /** APN type for IA Initial Attach APN. */
86     public static final int TYPE_IA = ApnTypes.IA;
87     /**
88      * APN type for Emergency PDN. This is not an IA apn, but is used
89      * for access to carrier services in an emergency call situation.
90      */
91     public static final int TYPE_EMERGENCY = ApnTypes.EMERGENCY;
92 
93     /** @hide */
94     @IntDef(flag = true, prefix = { "TYPE_" }, value = {
95         TYPE_DEFAULT,
96         TYPE_MMS,
97         TYPE_SUPL,
98         TYPE_DUN,
99         TYPE_HIPRI,
100         TYPE_FOTA,
101         TYPE_IMS,
102         TYPE_CBS,
103         TYPE_IA,
104         TYPE_EMERGENCY
105     })
106     @Retention(RetentionPolicy.SOURCE)
107     public @interface ApnType {}
108 
109     // Possible values for authentication types.
110     /** No authentication type. */
111     public static final int AUTH_TYPE_NONE = 0;
112     /** Authentication type for PAP. */
113     public static final int AUTH_TYPE_PAP = 1;
114     /** Authentication type for CHAP. */
115     public static final int AUTH_TYPE_CHAP = 2;
116     /** Authentication type for PAP or CHAP. */
117     public static final int AUTH_TYPE_PAP_OR_CHAP = 3;
118 
119     /** @hide */
120     @IntDef(prefix = { "AUTH_TYPE_" }, value = {
121         AUTH_TYPE_NONE,
122         AUTH_TYPE_PAP,
123         AUTH_TYPE_CHAP,
124         AUTH_TYPE_PAP_OR_CHAP,
125     })
126     @Retention(RetentionPolicy.SOURCE)
127     public @interface AuthType {}
128 
129     // Possible values for protocol.
130     /** Protocol type for IP. */
131     public static final int PROTOCOL_IP = 0;
132     /** Protocol type for IPV6. */
133     public static final int PROTOCOL_IPV6 = 1;
134     /** Protocol type for IPV4V6. */
135     public static final int PROTOCOL_IPV4V6 = 2;
136     /** Protocol type for PPP. */
137     public static final int PROTOCOL_PPP = 3;
138 
139     /** @hide */
140     @IntDef(prefix = { "PROTOCOL_" }, value = {
141         PROTOCOL_IP,
142         PROTOCOL_IPV6,
143         PROTOCOL_IPV4V6,
144         PROTOCOL_PPP,
145     })
146     @Retention(RetentionPolicy.SOURCE)
147     public @interface ProtocolType {}
148 
149     // Possible values for MVNO type.
150     /** MVNO type for service provider name. */
151     public static final int MVNO_TYPE_SPN = 0;
152     /** MVNO type for IMSI. */
153     public static final int MVNO_TYPE_IMSI = 1;
154     /** MVNO type for group identifier level 1. */
155     public static final int MVNO_TYPE_GID = 2;
156     /** MVNO type for ICCID. */
157     public static final int MVNO_TYPE_ICCID = 3;
158 
159     /** @hide */
160     @IntDef(prefix = { "MVNO_TYPE_" }, value = {
161         MVNO_TYPE_SPN,
162         MVNO_TYPE_IMSI,
163         MVNO_TYPE_GID,
164         MVNO_TYPE_ICCID,
165     })
166     @Retention(RetentionPolicy.SOURCE)
167     public @interface MvnoType {}
168 
169     static {
170         APN_TYPE_STRING_MAP = new ArrayMap<String, Integer>();
171         APN_TYPE_STRING_MAP.put("*", TYPE_ALL_BUT_IA);
172         APN_TYPE_STRING_MAP.put("default", TYPE_DEFAULT);
173         APN_TYPE_STRING_MAP.put("mms", TYPE_MMS);
174         APN_TYPE_STRING_MAP.put("supl", TYPE_SUPL);
175         APN_TYPE_STRING_MAP.put("dun", TYPE_DUN);
176         APN_TYPE_STRING_MAP.put("hipri", TYPE_HIPRI);
177         APN_TYPE_STRING_MAP.put("fota", TYPE_FOTA);
178         APN_TYPE_STRING_MAP.put("ims", TYPE_IMS);
179         APN_TYPE_STRING_MAP.put("cbs", TYPE_CBS);
180         APN_TYPE_STRING_MAP.put("ia", TYPE_IA);
181         APN_TYPE_STRING_MAP.put("emergency", TYPE_EMERGENCY);
182         APN_TYPE_INT_MAP = new ArrayMap<Integer, String>();
APN_TYPE_INT_MAP.put(TYPE_DEFAULT, "default")183         APN_TYPE_INT_MAP.put(TYPE_DEFAULT, "default");
APN_TYPE_INT_MAP.put(TYPE_MMS, "mms")184         APN_TYPE_INT_MAP.put(TYPE_MMS, "mms");
APN_TYPE_INT_MAP.put(TYPE_SUPL, "supl")185         APN_TYPE_INT_MAP.put(TYPE_SUPL, "supl");
APN_TYPE_INT_MAP.put(TYPE_DUN, "dun")186         APN_TYPE_INT_MAP.put(TYPE_DUN, "dun");
APN_TYPE_INT_MAP.put(TYPE_HIPRI, "hipri")187         APN_TYPE_INT_MAP.put(TYPE_HIPRI, "hipri");
APN_TYPE_INT_MAP.put(TYPE_FOTA, "fota")188         APN_TYPE_INT_MAP.put(TYPE_FOTA, "fota");
APN_TYPE_INT_MAP.put(TYPE_IMS, "ims")189         APN_TYPE_INT_MAP.put(TYPE_IMS, "ims");
APN_TYPE_INT_MAP.put(TYPE_CBS, "cbs")190         APN_TYPE_INT_MAP.put(TYPE_CBS, "cbs");
APN_TYPE_INT_MAP.put(TYPE_IA, "ia")191         APN_TYPE_INT_MAP.put(TYPE_IA, "ia");
APN_TYPE_INT_MAP.put(TYPE_EMERGENCY, "emergency")192         APN_TYPE_INT_MAP.put(TYPE_EMERGENCY, "emergency");
193 
194         PROTOCOL_STRING_MAP = new ArrayMap<String, Integer>();
195         PROTOCOL_STRING_MAP.put("IP", PROTOCOL_IP);
196         PROTOCOL_STRING_MAP.put("IPV6", PROTOCOL_IPV6);
197         PROTOCOL_STRING_MAP.put("IPV4V6", PROTOCOL_IPV4V6);
198         PROTOCOL_STRING_MAP.put("PPP", PROTOCOL_PPP);
199         PROTOCOL_INT_MAP = new ArrayMap<Integer, String>();
PROTOCOL_INT_MAP.put(PROTOCOL_IP, "IP")200         PROTOCOL_INT_MAP.put(PROTOCOL_IP, "IP");
PROTOCOL_INT_MAP.put(PROTOCOL_IPV6, "IPV6")201         PROTOCOL_INT_MAP.put(PROTOCOL_IPV6, "IPV6");
PROTOCOL_INT_MAP.put(PROTOCOL_IPV4V6, "IPV4V6")202         PROTOCOL_INT_MAP.put(PROTOCOL_IPV4V6, "IPV4V6");
PROTOCOL_INT_MAP.put(PROTOCOL_PPP, "PPP")203         PROTOCOL_INT_MAP.put(PROTOCOL_PPP, "PPP");
204 
205         MVNO_TYPE_STRING_MAP = new ArrayMap<String, Integer>();
206         MVNO_TYPE_STRING_MAP.put("spn", MVNO_TYPE_SPN);
207         MVNO_TYPE_STRING_MAP.put("imsi", MVNO_TYPE_IMSI);
208         MVNO_TYPE_STRING_MAP.put("gid", MVNO_TYPE_GID);
209         MVNO_TYPE_STRING_MAP.put("iccid", MVNO_TYPE_ICCID);
210         MVNO_TYPE_INT_MAP = new ArrayMap<Integer, String>();
MVNO_TYPE_INT_MAP.put(MVNO_TYPE_SPN, "spn")211         MVNO_TYPE_INT_MAP.put(MVNO_TYPE_SPN, "spn");
MVNO_TYPE_INT_MAP.put(MVNO_TYPE_IMSI, "imsi")212         MVNO_TYPE_INT_MAP.put(MVNO_TYPE_IMSI, "imsi");
MVNO_TYPE_INT_MAP.put(MVNO_TYPE_GID, "gid")213         MVNO_TYPE_INT_MAP.put(MVNO_TYPE_GID, "gid");
MVNO_TYPE_INT_MAP.put(MVNO_TYPE_ICCID, "iccid")214         MVNO_TYPE_INT_MAP.put(MVNO_TYPE_ICCID, "iccid");
215     }
216 
217     private final String mEntryName;
218     private final String mApnName;
219     private final InetAddress mProxyAddress;
220     private final int mProxyPort;
221     private final Uri mMmsc;
222     private final InetAddress mMmsProxyAddress;
223     private final int mMmsProxyPort;
224     private final String mUser;
225     private final String mPassword;
226     private final int mAuthType;
227     private final int mApnTypeBitmask;
228     private final int mId;
229     private final String mOperatorNumeric;
230     private final int mProtocol;
231     private final int mRoamingProtocol;
232     private final int mMtu;
233 
234     private final boolean mCarrierEnabled;
235 
236     private final int mNetworkTypeBitmask;
237 
238     private final int mProfileId;
239 
240     private final boolean mModemCognitive;
241     private final int mMaxConns;
242     private final int mWaitTime;
243     private final int mMaxConnsTime;
244 
245     private final int mMvnoType;
246     private final String mMvnoMatchData;
247 
248     private boolean mPermanentFailed = false;
249 
250     /**
251      * Returns the MTU size of the mobile interface to which the APN connected.
252      *
253      * @return the MTU size of the APN
254      * @hide
255      */
getMtu()256     public int getMtu() {
257         return mMtu;
258     }
259 
260     /**
261      * Returns the profile id to which the APN saved in modem.
262      *
263      * @return the profile id of the APN
264      * @hide
265      */
getProfileId()266     public int getProfileId() {
267         return mProfileId;
268     }
269 
270     /**
271      * Returns if the APN setting is to be set in modem.
272      *
273      * @return is the APN setting to be set in modem
274      * @hide
275      */
getModemCognitive()276     public boolean getModemCognitive() {
277         return mModemCognitive;
278     }
279 
280     /**
281      * Returns the max connections of this APN.
282      *
283      * @return the max connections of this APN
284      * @hide
285      */
getMaxConns()286     public int getMaxConns() {
287         return mMaxConns;
288     }
289 
290     /**
291      * Returns the wait time for retry of the APN.
292      *
293      * @return the wait time for retry of the APN
294      * @hide
295      */
getWaitTime()296     public int getWaitTime() {
297         return mWaitTime;
298     }
299 
300     /**
301      * Returns the time to limit max connection for the APN.
302      *
303      * @return the time to limit max connection for the APN
304      * @hide
305      */
getMaxConnsTime()306     public int getMaxConnsTime() {
307         return mMaxConnsTime;
308     }
309 
310     /**
311      * Returns the MVNO data. Examples:
312      *   "spn": A MOBILE, BEN NL
313      *   "imsi": 302720x94, 2060188
314      *   "gid": 4E, 33
315      *   "iccid": 898603 etc..
316      *
317      * @return the mvno match data
318      * @hide
319      */
getMvnoMatchData()320     public String getMvnoMatchData() {
321         return mMvnoMatchData;
322     }
323 
324     /**
325      * Indicates this APN setting is permanently failed and cannot be
326      * retried by the retry manager anymore.
327      *
328      * @return if this APN setting is permanently failed
329      * @hide
330      */
getPermanentFailed()331     public boolean getPermanentFailed() {
332         return mPermanentFailed;
333     }
334 
335     /**
336      * Sets if this APN setting is permanently failed.
337      *
338      * @param permanentFailed if this APN setting is permanently failed
339      * @hide
340      */
setPermanentFailed(boolean permanentFailed)341     public void setPermanentFailed(boolean permanentFailed) {
342         mPermanentFailed = permanentFailed;
343     }
344 
345     /**
346      * Gets the human-readable name that describes the APN.
347      *
348      * @return the entry name for the APN
349      */
getEntryName()350     public String getEntryName() {
351         return mEntryName;
352     }
353 
354     /**
355      * Returns the name of the APN.
356      *
357      * @return APN name
358      */
getApnName()359     public String getApnName() {
360         return mApnName;
361     }
362 
363     /**
364      * Gets the HTTP proxy address configured for the APN. The proxy address might be an IP address
365      * or hostname. This method returns {@code null} if system networking (typically DNS) isn’t
366      * available to resolve a hostname value—values set as IP addresses don’t have this restriction.
367      * This is a known problem and will be addressed in a future release.
368      *
369      * @return the HTTP proxy address or {@code null} if DNS isn’t available to resolve a hostname
370      */
getProxyAddress()371     public InetAddress getProxyAddress() {
372         return mProxyAddress;
373     }
374 
375     /**
376      * Returns the proxy address of the APN.
377      *
378      * @return proxy address.
379      */
getProxyPort()380     public int getProxyPort() {
381         return mProxyPort;
382     }
383     /**
384      * Returns the MMSC Uri of the APN.
385      *
386      * @return MMSC Uri.
387      */
getMmsc()388     public Uri getMmsc() {
389         return mMmsc;
390     }
391 
392     /**
393      * Gets the MMS proxy address configured for the APN. The MMS proxy address might be an IP
394      * address or hostname. This method returns {@code null} if system networking (typically DNS)
395      * isn’t available to resolve a hostname value—values set as IP addresses don’t have this
396      * restriction. This is a known problem and will be addressed in a future release.
397      *
398      * @return the MMS proxy address or {@code null} if DNS isn’t available to resolve a hostname
399      */
getMmsProxyAddress()400     public InetAddress getMmsProxyAddress() {
401         return mMmsProxyAddress;
402     }
403 
404     /**
405      * Returns the MMS proxy port of the APN.
406      *
407      * @return MMS proxy port
408      */
getMmsProxyPort()409     public int getMmsProxyPort() {
410         return mMmsProxyPort;
411     }
412 
413     /**
414      * Returns the APN username of the APN.
415      *
416      * @return APN username
417      */
getUser()418     public String getUser() {
419         return mUser;
420     }
421 
422     /**
423      * Returns the APN password of the APN.
424      *
425      * @return APN password
426      */
getPassword()427     public String getPassword() {
428         return mPassword;
429     }
430 
431     /**
432      * Returns the authentication type of the APN.
433      *
434      * @return authentication type
435      */
436     @AuthType
getAuthType()437     public int getAuthType() {
438         return mAuthType;
439     }
440 
441     /**
442      * Returns the bitmask of APN types.
443      *
444      * <p>Apn types are usage categories for an APN entry. One APN entry may support multiple
445      * APN types, eg, a single APN may service regular internet traffic ("default") as well as
446      * MMS-specific connections.
447      *
448      * <p>The bitmask of APN types is calculated from APN types defined in {@link ApnSetting}.
449      *
450      * @see Builder#setApnTypeBitmask(int)
451      * @return a bitmask describing the types of the APN
452      */
getApnTypeBitmask()453     public @ApnType int getApnTypeBitmask() {
454         return mApnTypeBitmask;
455     }
456 
457     /**
458      * Returns the unique database id for this entry.
459      *
460      * @return the unique database id
461      */
getId()462     public int getId() {
463         return mId;
464     }
465 
466     /**
467      * Returns the numeric operator ID for the APN. Numeric operator ID is defined as
468      * {@link android.provider.Telephony.Carriers#MCC} +
469      * {@link android.provider.Telephony.Carriers#MNC}.
470      *
471      * @return the numeric operator ID
472      */
getOperatorNumeric()473     public String getOperatorNumeric() {
474         return mOperatorNumeric;
475     }
476 
477     /**
478      * Returns the protocol to use to connect to this APN.
479      *
480      * <p>Protocol is one of the {@code PDP_type} values in TS 27.007 section 10.1.1.
481      *
482      * @see Builder#setProtocol(int)
483      * @return the protocol
484      */
485     @ProtocolType
getProtocol()486     public int getProtocol() {
487         return mProtocol;
488     }
489 
490     /**
491      * Returns the protocol to use to connect to this APN while the device is roaming.
492      *
493      * <p>Roaming protocol is one of the {@code PDP_type} values in TS 27.007 section 10.1.1.
494      *
495      * @see Builder#setRoamingProtocol(int)
496      * @return the roaming protocol
497      */
498     @ProtocolType
getRoamingProtocol()499     public int getRoamingProtocol() {
500         return mRoamingProtocol;
501     }
502 
503     /**
504      * Returns the current status of APN.
505      *
506      * {@code true} : enabled APN.
507      * {@code false} : disabled APN.
508      *
509      * @return the current status
510      */
isEnabled()511     public boolean isEnabled() {
512         return mCarrierEnabled;
513     }
514 
515     /**
516      * Returns a bitmask describing the Radio Technologies(Network Types) which this APN may use.
517      *
518      * NetworkType bitmask is calculated from NETWORK_TYPE defined in {@link TelephonyManager}.
519      *
520      * Examples of Network Types include {@link TelephonyManager#NETWORK_TYPE_UNKNOWN},
521      * {@link TelephonyManager#NETWORK_TYPE_GPRS}, {@link TelephonyManager#NETWORK_TYPE_EDGE}.
522      *
523      * @return a bitmask describing the Radio Technologies(Network Types)
524      */
getNetworkTypeBitmask()525     public int getNetworkTypeBitmask() {
526         return mNetworkTypeBitmask;
527     }
528 
529     /**
530      * Returns the MVNO match type for this APN.
531      *
532      * @see Builder#setMvnoType(int)
533      * @return the MVNO match type
534      */
535     @MvnoType
getMvnoType()536     public int getMvnoType() {
537         return mMvnoType;
538     }
539 
ApnSetting(Builder builder)540     private ApnSetting(Builder builder) {
541         this.mEntryName = builder.mEntryName;
542         this.mApnName = builder.mApnName;
543         this.mProxyAddress = builder.mProxyAddress;
544         this.mProxyPort = builder.mProxyPort;
545         this.mMmsc = builder.mMmsc;
546         this.mMmsProxyAddress = builder.mMmsProxyAddress;
547         this.mMmsProxyPort = builder.mMmsProxyPort;
548         this.mUser = builder.mUser;
549         this.mPassword = builder.mPassword;
550         this.mAuthType = builder.mAuthType;
551         this.mApnTypeBitmask = builder.mApnTypeBitmask;
552         this.mId = builder.mId;
553         this.mOperatorNumeric = builder.mOperatorNumeric;
554         this.mProtocol = builder.mProtocol;
555         this.mRoamingProtocol = builder.mRoamingProtocol;
556         this.mMtu = builder.mMtu;
557         this.mCarrierEnabled = builder.mCarrierEnabled;
558         this.mNetworkTypeBitmask = builder.mNetworkTypeBitmask;
559         this.mProfileId = builder.mProfileId;
560         this.mModemCognitive = builder.mModemCognitive;
561         this.mMaxConns = builder.mMaxConns;
562         this.mWaitTime = builder.mWaitTime;
563         this.mMaxConnsTime = builder.mMaxConnsTime;
564         this.mMvnoType = builder.mMvnoType;
565         this.mMvnoMatchData = builder.mMvnoMatchData;
566     }
567 
568     /** @hide */
makeApnSetting(int id, String operatorNumeric, String entryName, String apnName, InetAddress proxy, int port, Uri mmsc, InetAddress mmsProxy, int mmsPort, String user, String password, int authType, int mApnTypeBitmask, int protocol, int roamingProtocol, boolean carrierEnabled, int networkTypeBitmask, int profileId, boolean modemCognitive, int maxConns, int waitTime, int maxConnsTime, int mtu, int mvnoType, String mvnoMatchData)569     public static ApnSetting makeApnSetting(int id, String operatorNumeric, String entryName,
570             String apnName, InetAddress proxy, int port, Uri mmsc, InetAddress mmsProxy,
571             int mmsPort, String user, String password, int authType, int mApnTypeBitmask,
572             int protocol, int roamingProtocol, boolean carrierEnabled,
573             int networkTypeBitmask, int profileId, boolean modemCognitive, int maxConns,
574             int waitTime, int maxConnsTime, int mtu, int mvnoType, String mvnoMatchData) {
575         return new Builder()
576                 .setId(id)
577                 .setOperatorNumeric(operatorNumeric)
578                 .setEntryName(entryName)
579                 .setApnName(apnName)
580                 .setProxyAddress(proxy)
581                 .setProxyPort(port)
582                 .setMmsc(mmsc)
583                 .setMmsProxyAddress(mmsProxy)
584                 .setMmsProxyPort(mmsPort)
585                 .setUser(user)
586                 .setPassword(password)
587                 .setAuthType(authType)
588                 .setApnTypeBitmask(mApnTypeBitmask)
589                 .setProtocol(protocol)
590                 .setRoamingProtocol(roamingProtocol)
591                 .setCarrierEnabled(carrierEnabled)
592                 .setNetworkTypeBitmask(networkTypeBitmask)
593                 .setProfileId(profileId)
594                 .setModemCognitive(modemCognitive)
595                 .setMaxConns(maxConns)
596                 .setWaitTime(waitTime)
597                 .setMaxConnsTime(maxConnsTime)
598                 .setMtu(mtu)
599                 .setMvnoType(mvnoType)
600                 .setMvnoMatchData(mvnoMatchData)
601                 .build();
602     }
603 
604     /** @hide */
makeApnSetting(Cursor cursor)605     public static ApnSetting makeApnSetting(Cursor cursor) {
606         final int apnTypesBitmask = parseTypes(
607                 cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.TYPE)));
608         int networkTypeBitmask = cursor.getInt(
609                 cursor.getColumnIndexOrThrow(Telephony.Carriers.NETWORK_TYPE_BITMASK));
610         if (networkTypeBitmask == 0) {
611             final int bearerBitmask = cursor.getInt(cursor.getColumnIndexOrThrow(
612                     Telephony.Carriers.BEARER_BITMASK));
613             networkTypeBitmask =
614                     ServiceState.convertBearerBitmaskToNetworkTypeBitmask(bearerBitmask);
615         }
616 
617         return makeApnSetting(
618                 cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers._ID)),
619                 cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.NUMERIC)),
620                 cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.NAME)),
621                 cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.APN)),
622                 inetAddressFromString(cursor.getString(
623                         cursor.getColumnIndexOrThrow(Telephony.Carriers.PROXY))),
624                 portFromString(cursor.getString(
625                         cursor.getColumnIndexOrThrow(Telephony.Carriers.PORT))),
626                 UriFromString(cursor.getString(
627                         cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSC))),
628                 inetAddressFromString(cursor.getString(
629                         cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSPROXY))),
630                 portFromString(cursor.getString(
631                         cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSPORT))),
632                 cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.USER)),
633                 cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PASSWORD)),
634                 cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.AUTH_TYPE)),
635                 apnTypesBitmask,
636                 nullToNotInMapInt(PROTOCOL_STRING_MAP.get(
637                     cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PROTOCOL)))),
638                 nullToNotInMapInt(PROTOCOL_STRING_MAP.get(
639                     cursor.getString(cursor.getColumnIndexOrThrow(
640                         Telephony.Carriers.ROAMING_PROTOCOL)))),
641                 cursor.getInt(cursor.getColumnIndexOrThrow(
642                         Telephony.Carriers.CARRIER_ENABLED)) == 1,
643                 networkTypeBitmask,
644                 cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.PROFILE_ID)),
645                 cursor.getInt(cursor.getColumnIndexOrThrow(
646                         Telephony.Carriers.MODEM_COGNITIVE)) == 1,
647                 cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.MAX_CONNS)),
648                 cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.WAIT_TIME)),
649                 cursor.getInt(cursor.getColumnIndexOrThrow(
650                         Telephony.Carriers.MAX_CONNS_TIME)),
651                 cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.MTU)),
652                 nullToNotInMapInt(MVNO_TYPE_STRING_MAP.get(
653                     cursor.getString(cursor.getColumnIndexOrThrow(
654                         Telephony.Carriers.MVNO_TYPE)))),
655                 cursor.getString(cursor.getColumnIndexOrThrow(
656                         Telephony.Carriers.MVNO_MATCH_DATA)));
657     }
658 
659     /** @hide */
makeApnSetting(ApnSetting apn)660     public static ApnSetting makeApnSetting(ApnSetting apn) {
661         return makeApnSetting(apn.mId, apn.mOperatorNumeric, apn.mEntryName, apn.mApnName,
662                 apn.mProxyAddress, apn.mProxyPort, apn.mMmsc, apn.mMmsProxyAddress, apn.mMmsProxyPort, apn.mUser,
663                 apn.mPassword, apn.mAuthType, apn.mApnTypeBitmask, apn.mProtocol, apn.mRoamingProtocol,
664                 apn.mCarrierEnabled, apn.mNetworkTypeBitmask, apn.mProfileId,
665                 apn.mModemCognitive, apn.mMaxConns, apn.mWaitTime, apn.mMaxConnsTime, apn.mMtu,
666                 apn.mMvnoType, apn.mMvnoMatchData);
667     }
668 
669     /** @hide */
toString()670     public String toString() {
671         StringBuilder sb = new StringBuilder();
672         sb.append("[ApnSettingV4] ")
673                 .append(mEntryName)
674                 .append(", ").append(mId)
675                 .append(", ").append(mOperatorNumeric)
676                 .append(", ").append(mApnName)
677                 .append(", ").append(inetAddressToString(mProxyAddress))
678                 .append(", ").append(UriToString(mMmsc))
679                 .append(", ").append(inetAddressToString(mMmsProxyAddress))
680                 .append(", ").append(portToString(mMmsProxyPort))
681                 .append(", ").append(portToString(mProxyPort))
682                 .append(", ").append(mAuthType).append(", ");
683         final String[] types = deParseTypes(mApnTypeBitmask).split(",");
684         sb.append(TextUtils.join(" | ", types)).append(", ");
685         sb.append(", ").append(mProtocol);
686         sb.append(", ").append(mRoamingProtocol);
687         sb.append(", ").append(mCarrierEnabled);
688         sb.append(", ").append(mProfileId);
689         sb.append(", ").append(mModemCognitive);
690         sb.append(", ").append(mMaxConns);
691         sb.append(", ").append(mWaitTime);
692         sb.append(", ").append(mMaxConnsTime);
693         sb.append(", ").append(mMtu);
694         sb.append(", ").append(mMvnoType);
695         sb.append(", ").append(mMvnoMatchData);
696         sb.append(", ").append(mPermanentFailed);
697         sb.append(", ").append(mNetworkTypeBitmask);
698         return sb.toString();
699     }
700 
701     /**
702      * Returns true if there are MVNO params specified.
703      * @hide
704      */
hasMvnoParams()705     public boolean hasMvnoParams() {
706         return (mMvnoType != NOT_IN_MAP_INT) && !TextUtils.isEmpty(mMvnoMatchData);
707     }
708 
709     /** @hide */
canHandleType(@pnType int type)710     public boolean canHandleType(@ApnType int type) {
711         return mCarrierEnabled && ((mApnTypeBitmask & type) == type);
712     }
713 
714     // check whether the types of two APN same (even only one type of each APN is same)
typeSameAny(ApnSetting first, ApnSetting second)715     private boolean typeSameAny(ApnSetting first, ApnSetting second) {
716         if (VDBG) {
717             StringBuilder apnType1 = new StringBuilder(first.mApnName + ": ");
718             apnType1.append(deParseTypes(first.mApnTypeBitmask));
719 
720             StringBuilder apnType2 = new StringBuilder(second.mApnName + ": ");
721             apnType2.append(deParseTypes(second.mApnTypeBitmask));
722 
723             Rlog.d(LOG_TAG, "APN1: is " + apnType1);
724             Rlog.d(LOG_TAG, "APN2: is " + apnType2);
725         }
726 
727         if ((first.mApnTypeBitmask & second.mApnTypeBitmask) != 0) {
728             if (VDBG) {
729                 Rlog.d(LOG_TAG, "typeSameAny: return true");
730             }
731             return true;
732         }
733 
734         if (VDBG) {
735             Rlog.d(LOG_TAG, "typeSameAny: return false");
736         }
737         return false;
738     }
739 
740     // TODO - if we have this function we should also have hashCode.
741     // Also should handle changes in type order and perhaps case-insensitivity
742     /** @hide */
equals(Object o)743     public boolean equals(Object o) {
744         if (o instanceof ApnSetting == false) {
745             return false;
746         }
747 
748         ApnSetting other = (ApnSetting) o;
749 
750         return mEntryName.equals(other.mEntryName)
751                 && Objects.equals(mId, other.mId)
752                 && Objects.equals(mOperatorNumeric, other.mOperatorNumeric)
753                 && Objects.equals(mApnName, other.mApnName)
754                 && Objects.equals(mProxyAddress, other.mProxyAddress)
755                 && Objects.equals(mMmsc, other.mMmsc)
756                 && Objects.equals(mMmsProxyAddress, other.mMmsProxyAddress)
757                 && Objects.equals(mMmsProxyPort, other.mMmsProxyPort)
758                 && Objects.equals(mProxyPort,other.mProxyPort)
759                 && Objects.equals(mUser, other.mUser)
760                 && Objects.equals(mPassword, other.mPassword)
761                 && Objects.equals(mAuthType, other.mAuthType)
762                 && Objects.equals(mApnTypeBitmask, other.mApnTypeBitmask)
763                 && Objects.equals(mProtocol, other.mProtocol)
764                 && Objects.equals(mRoamingProtocol, other.mRoamingProtocol)
765                 && Objects.equals(mCarrierEnabled, other.mCarrierEnabled)
766                 && Objects.equals(mProfileId, other.mProfileId)
767                 && Objects.equals(mModemCognitive, other.mModemCognitive)
768                 && Objects.equals(mMaxConns, other.mMaxConns)
769                 && Objects.equals(mWaitTime, other.mWaitTime)
770                 && Objects.equals(mMaxConnsTime, other.mMaxConnsTime)
771                 && Objects.equals(mMtu, other.mMtu)
772                 && Objects.equals(mMvnoType, other.mMvnoType)
773                 && Objects.equals(mMvnoMatchData, other.mMvnoMatchData)
774                 && Objects.equals(mNetworkTypeBitmask, other.mNetworkTypeBitmask);
775     }
776 
777     /**
778      * Compare two APN settings
779      *
780      * Note: This method does not compare 'mId', 'mNetworkTypeBitmask'. We only use this for
781      * determining if tearing a data call is needed when conditions change. See
782      * cleanUpConnectionsOnUpdatedApns in DcTracker.
783      *
784      * @param o the other object to compare
785      * @param isDataRoaming True if the device is on data roaming
786      * @return True if the two APN settings are same
787      * @hide
788      */
equals(Object o, boolean isDataRoaming)789     public boolean equals(Object o, boolean isDataRoaming) {
790         if (!(o instanceof ApnSetting)) {
791             return false;
792         }
793 
794         ApnSetting other = (ApnSetting) o;
795 
796         return mEntryName.equals(other.mEntryName)
797                 && Objects.equals(mOperatorNumeric, other.mOperatorNumeric)
798                 && Objects.equals(mApnName, other.mApnName)
799                 && Objects.equals(mProxyAddress, other.mProxyAddress)
800                 && Objects.equals(mMmsc, other.mMmsc)
801                 && Objects.equals(mMmsProxyAddress, other.mMmsProxyAddress)
802                 && Objects.equals(mMmsProxyPort, other.mMmsProxyPort)
803                 && Objects.equals(mProxyPort, other.mProxyPort)
804                 && Objects.equals(mUser, other.mUser)
805                 && Objects.equals(mPassword, other.mPassword)
806                 && Objects.equals(mAuthType, other.mAuthType)
807                 && Objects.equals(mApnTypeBitmask, other.mApnTypeBitmask)
808                 && (isDataRoaming || Objects.equals(mProtocol,other.mProtocol))
809                 && (!isDataRoaming || Objects.equals(mRoamingProtocol, other.mRoamingProtocol))
810                 && Objects.equals(mCarrierEnabled, other.mCarrierEnabled)
811                 && Objects.equals(mProfileId, other.mProfileId)
812                 && Objects.equals(mModemCognitive, other.mModemCognitive)
813                 && Objects.equals(mMaxConns, other.mMaxConns)
814                 && Objects.equals(mWaitTime, other.mWaitTime)
815                 && Objects.equals(mMaxConnsTime, other.mMaxConnsTime)
816                 && Objects.equals(mMtu, other.mMtu)
817                 && Objects.equals(mMvnoType, other.mMvnoType)
818                 && Objects.equals(mMvnoMatchData, other.mMvnoMatchData);
819     }
820 
821     /**
822      * Check if neither mention DUN and are substantially similar
823      *
824      * @param other The other APN settings to compare
825      * @return True if two APN settings are similar
826      * @hide
827      */
similar(ApnSetting other)828     public boolean similar(ApnSetting other) {
829         return (!this.canHandleType(TYPE_DUN)
830                 && !other.canHandleType(TYPE_DUN)
831                 && Objects.equals(this.mApnName, other.mApnName)
832                 && !typeSameAny(this, other)
833                 && xorEquals(this.mProxyAddress, other.mProxyAddress)
834                 && xorEqualsPort(this.mProxyPort, other.mProxyPort)
835                 && xorEquals(this.mProtocol, other.mProtocol)
836                 && xorEquals(this.mRoamingProtocol, other.mRoamingProtocol)
837                 && Objects.equals(this.mCarrierEnabled, other.mCarrierEnabled)
838                 && Objects.equals(this.mProfileId, other.mProfileId)
839                 && Objects.equals(this.mMvnoType, other.mMvnoType)
840                 && Objects.equals(this.mMvnoMatchData, other.mMvnoMatchData)
841                 && xorEquals(this.mMmsc, other.mMmsc)
842                 && xorEquals(this.mMmsProxyAddress, other.mMmsProxyAddress)
843                 && xorEqualsPort(this.mMmsProxyPort, other.mMmsProxyPort))
844                 && Objects.equals(this.mNetworkTypeBitmask, other.mNetworkTypeBitmask);
845     }
846 
847     // Equal or one is not specified.
xorEquals(String first, String second)848     private boolean xorEquals(String first, String second) {
849         return (Objects.equals(first, second)
850                 || TextUtils.isEmpty(first)
851                 || TextUtils.isEmpty(second));
852     }
853 
854     // Equal or one is not null.
xorEquals(Object first, Object second)855     private boolean xorEquals(Object first, Object second) {
856         return first == null || second == null || first.equals(second);
857     }
858 
859     // Equal or one is not specified.
xorEqualsPort(int first, int second)860     private boolean xorEqualsPort(int first, int second) {
861         return first == NO_PORT_SPECIFIED || second == NO_PORT_SPECIFIED
862             || Objects.equals(first, second);
863     }
864 
deParseTypes(int apnTypeBitmask)865     private String deParseTypes(int apnTypeBitmask) {
866         List<String> types = new ArrayList<>();
867         for (Integer type : APN_TYPE_INT_MAP.keySet()) {
868             if ((apnTypeBitmask & type) == type) {
869                 types.add(APN_TYPE_INT_MAP.get(type));
870             }
871         }
872         return TextUtils.join(",", types);
873     }
874 
nullToEmpty(String stringValue)875     private String nullToEmpty(String stringValue) {
876         return stringValue == null ? "" : stringValue;
877     }
878 
879     /** @hide */
880     // Called by DPM.
toContentValues()881     public ContentValues toContentValues() {
882         ContentValues apnValue = new ContentValues();
883         apnValue.put(Telephony.Carriers.NUMERIC, nullToEmpty(mOperatorNumeric));
884         apnValue.put(Telephony.Carriers.NAME, nullToEmpty(mEntryName));
885         apnValue.put(Telephony.Carriers.APN, nullToEmpty(mApnName));
886         apnValue.put(Telephony.Carriers.PROXY, mProxyAddress == null ? ""
887             : inetAddressToString(mProxyAddress));
888         apnValue.put(Telephony.Carriers.PORT, portToString(mProxyPort));
889         apnValue.put(Telephony.Carriers.MMSC, mMmsc == null ? "" : UriToString(mMmsc));
890         apnValue.put(Telephony.Carriers.MMSPORT, portToString(mMmsProxyPort));
891         apnValue.put(Telephony.Carriers.MMSPROXY, mMmsProxyAddress == null
892                 ? "" : inetAddressToString(mMmsProxyAddress));
893         apnValue.put(Telephony.Carriers.USER, nullToEmpty(mUser));
894         apnValue.put(Telephony.Carriers.PASSWORD, nullToEmpty(mPassword));
895         apnValue.put(Telephony.Carriers.AUTH_TYPE, mAuthType);
896         String apnType = deParseTypes(mApnTypeBitmask);
897         apnValue.put(Telephony.Carriers.TYPE, nullToEmpty(apnType));
898         apnValue.put(Telephony.Carriers.PROTOCOL,
899             nullToEmpty(PROTOCOL_INT_MAP.get(mProtocol)));
900         apnValue.put(Telephony.Carriers.ROAMING_PROTOCOL,
901             nullToEmpty(PROTOCOL_INT_MAP.get(mRoamingProtocol)));
902         apnValue.put(Telephony.Carriers.CARRIER_ENABLED, mCarrierEnabled);
903         apnValue.put(Telephony.Carriers.MVNO_TYPE,
904             nullToEmpty(MVNO_TYPE_INT_MAP.get(mMvnoType)));
905         apnValue.put(Telephony.Carriers.NETWORK_TYPE_BITMASK, mNetworkTypeBitmask);
906 
907         return apnValue;
908     }
909 
910     /**
911      * @param types comma delimited list of APN types
912      * @return bitmask of APN types
913      * @hide
914      */
parseTypes(String types)915     public static int parseTypes(String types) {
916         // If unset, set to ALL.
917         if (TextUtils.isEmpty(types)) {
918             return TYPE_ALL_BUT_IA;
919         } else {
920             int result = 0;
921             for (String str : types.split(",")) {
922                 Integer type = APN_TYPE_STRING_MAP.get(str);
923                 if (type != null) {
924                     result |= type;
925                 }
926             }
927             return result;
928         }
929     }
930 
UriFromString(String uri)931     private static Uri UriFromString(String uri) {
932         return TextUtils.isEmpty(uri) ? null : Uri.parse(uri);
933     }
934 
UriToString(Uri uri)935     private static String UriToString(Uri uri) {
936         return uri == null ? "" : uri.toString();
937     }
938 
inetAddressFromString(String inetAddress)939     private static InetAddress inetAddressFromString(String inetAddress) {
940         if (TextUtils.isEmpty(inetAddress)) {
941             return null;
942         }
943         try {
944             return InetAddress.getByName(inetAddress);
945         } catch (UnknownHostException e) {
946             Log.e(LOG_TAG, "Can't parse InetAddress from string: unknown host.");
947             return null;
948         }
949     }
950 
inetAddressToString(InetAddress inetAddress)951     private static String inetAddressToString(InetAddress inetAddress) {
952         if (inetAddress == null) {
953             return null;
954         }
955         final String inetAddressString = inetAddress.toString();
956         if (TextUtils.isEmpty(inetAddressString)) {
957             return null;
958         }
959         final String hostName = inetAddressString.substring(0, inetAddressString.indexOf("/"));
960         final String address = inetAddressString.substring(inetAddressString.indexOf("/") + 1);
961         if (TextUtils.isEmpty(hostName) && TextUtils.isEmpty(address)) {
962             return null;
963         }
964         return TextUtils.isEmpty(hostName) ? address : hostName;
965     }
966 
portFromString(String strPort)967     private static int portFromString(String strPort) {
968         int port = NO_PORT_SPECIFIED;
969         if (!TextUtils.isEmpty(strPort)) {
970             try {
971                 port = Integer.parseInt(strPort);
972             } catch (NumberFormatException e) {
973                 Log.e(LOG_TAG, "Can't parse port from String");
974             }
975         }
976         return port;
977     }
978 
portToString(int port)979     private static String portToString(int port) {
980         return port == NO_PORT_SPECIFIED ? "" : Integer.toString(port);
981     }
982 
983     // Implement Parcelable.
984     @Override
985     /** @hide */
describeContents()986     public int describeContents() {
987         return 0;
988     }
989 
990     @Override
991     /** @hide */
writeToParcel(@onNull Parcel dest, int flags)992     public void writeToParcel(@NonNull Parcel dest, int flags) {
993         dest.writeInt(mId);
994         dest.writeString(mOperatorNumeric);
995         dest.writeString(mEntryName);
996         dest.writeString(mApnName);
997         dest.writeValue(mProxyAddress);
998         dest.writeInt(mProxyPort);
999         dest.writeValue(mMmsc);
1000         dest.writeValue(mMmsProxyAddress);
1001         dest.writeInt(mMmsProxyPort);
1002         dest.writeString(mUser);
1003         dest.writeString(mPassword);
1004         dest.writeInt(mAuthType);
1005         dest.writeInt(mApnTypeBitmask);
1006         dest.writeInt(mProtocol);
1007         dest.writeInt(mRoamingProtocol);
1008         dest.writeInt(mCarrierEnabled ? 1: 0);
1009         dest.writeInt(mMvnoType);
1010         dest.writeInt(mNetworkTypeBitmask);
1011     }
1012 
readFromParcel(Parcel in)1013     private static ApnSetting readFromParcel(Parcel in) {
1014         final int id = in.readInt();
1015         final String operatorNumeric = in.readString();
1016         final String entryName = in.readString();
1017         final String apnName = in.readString();
1018         final InetAddress proxy = (InetAddress)in.readValue(InetAddress.class.getClassLoader());
1019         final int port = in.readInt();
1020         final Uri mmsc = (Uri)in.readValue(Uri.class.getClassLoader());
1021         final InetAddress mmsProxy = (InetAddress)in.readValue(InetAddress.class.getClassLoader());
1022         final int mmsPort = in.readInt();
1023         final String user = in.readString();
1024         final String password = in.readString();
1025         final int authType = in.readInt();
1026         final int apnTypesBitmask = in.readInt();
1027         final int protocol = in.readInt();
1028         final int roamingProtocol = in.readInt();
1029         final boolean carrierEnabled = in.readInt() > 0;
1030         final int mvnoType = in.readInt();
1031         final int networkTypeBitmask = in.readInt();
1032 
1033         return makeApnSetting(id, operatorNumeric, entryName, apnName,
1034             proxy, port, mmsc, mmsProxy, mmsPort, user, password, authType, apnTypesBitmask,
1035             protocol, roamingProtocol, carrierEnabled, networkTypeBitmask, 0, false,
1036             0, 0, 0, 0, mvnoType, null);
1037     }
1038 
1039     public static final Parcelable.Creator<ApnSetting> CREATOR =
1040             new Parcelable.Creator<ApnSetting>() {
1041                 @Override
1042                 public ApnSetting createFromParcel(Parcel in) {
1043                     return readFromParcel(in);
1044                 }
1045 
1046                 @Override
1047                 public ApnSetting[] newArray(int size) {
1048                     return new ApnSetting[size];
1049                 }
1050             };
1051 
nullToNotInMapInt(Integer value)1052     private static int nullToNotInMapInt(Integer value) {
1053         return value == null ? NOT_IN_MAP_INT : value;
1054     }
1055 
1056     /**
1057      * Provides a convenient way to set the fields of a {@link ApnSetting} when creating a new
1058      * instance. The following settings are required to build an {@code ApnSetting}:
1059      *
1060      * <ul><li>apnTypeBitmask</li>
1061      * <li>apnName</li>
1062      * <li>entryName</li></ul>
1063      *
1064      * <p>The example below shows how you might create a new {@code ApnSetting}:
1065      *
1066      * <pre><code>
1067      * // Create an MMS proxy address with a hostname. A network might not be
1068      * // available, so supply a dummy (0.0.0.0) IPv4 address to avoid DNS lookup.
1069      * String host = "mms.example.com";
1070      * byte[] ipAddress = new byte[4];
1071      * InetAddress mmsProxy;
1072      * try {
1073      *   mmsProxy = InetAddress.getByAddress(host, ipAddress);
1074      * } catch (UnknownHostException e) {
1075      *   e.printStackTrace();
1076      *   return;
1077      * }
1078      *
1079      * ApnSetting apn = new ApnSetting.Builder()
1080      *     .setApnTypeBitmask(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS)
1081      *     .setApnName("apn.example.com")
1082      *     .setEntryName("Example Carrier APN")
1083      *     .setMmsc(Uri.parse("http://mms.example.com:8002"))
1084      *     .setMmsProxyAddress(mmsProxy)
1085      *     .setMmsProxyPort(8799)
1086      *     .build();
1087      * </code></pre>
1088      */
1089     public static class Builder{
1090         private String mEntryName;
1091         private String mApnName;
1092         private InetAddress mProxyAddress;
1093         private int mProxyPort = NO_PORT_SPECIFIED;
1094         private Uri mMmsc;
1095         private InetAddress mMmsProxyAddress;
1096         private int mMmsProxyPort = NO_PORT_SPECIFIED;
1097         private String mUser;
1098         private String mPassword;
1099         private int mAuthType;
1100         private int mApnTypeBitmask;
1101         private int mId;
1102         private String mOperatorNumeric;
1103         private int mProtocol = NOT_IN_MAP_INT;
1104         private int mRoamingProtocol = NOT_IN_MAP_INT;
1105         private int mMtu;
1106         private int mNetworkTypeBitmask;
1107         private boolean mCarrierEnabled;
1108         private int mProfileId;
1109         private boolean mModemCognitive;
1110         private int mMaxConns;
1111         private int mWaitTime;
1112         private int mMaxConnsTime;
1113         private int mMvnoType = NOT_IN_MAP_INT;
1114         private String mMvnoMatchData;
1115 
1116         /**
1117          * Default constructor for Builder.
1118          */
Builder()1119         public Builder() {}
1120 
1121         /**
1122          * Sets the unique database id for this entry.
1123          *
1124          * @param id the unique database id to set for this entry
1125          */
setId(int id)1126         private Builder setId(int id) {
1127             this.mId = id;
1128             return this;
1129         }
1130 
1131         /**
1132          * Set the MTU size of the mobile interface to which the APN connected.
1133          *
1134          * @param mtu the MTU size to set for the APN
1135          * @hide
1136          */
setMtu(int mtu)1137         public Builder setMtu(int mtu) {
1138             this.mMtu = mtu;
1139             return this;
1140         }
1141 
1142         /**
1143          * Sets the profile id to which the APN saved in modem.
1144          *
1145          * @param profileId the profile id to set for the APN
1146          * @hide
1147          */
setProfileId(int profileId)1148         public Builder setProfileId(int profileId) {
1149             this.mProfileId = profileId;
1150             return this;
1151         }
1152 
1153         /**
1154          * Sets if the APN setting is to be set in modem.
1155          *
1156          * @param modemCognitive if the APN setting is to be set in modem
1157          * @hide
1158          */
setModemCognitive(boolean modemCognitive)1159         public Builder setModemCognitive(boolean modemCognitive) {
1160             this.mModemCognitive = modemCognitive;
1161             return this;
1162         }
1163 
1164         /**
1165          * Sets the max connections of this APN.
1166          *
1167          * @param maxConns the max connections of this APN
1168          * @hide
1169          */
setMaxConns(int maxConns)1170         public Builder setMaxConns(int maxConns) {
1171             this.mMaxConns = maxConns;
1172             return this;
1173         }
1174 
1175         /**
1176          * Sets the wait time for retry of the APN.
1177          *
1178          * @param waitTime the wait time for retry of the APN
1179          * @hide
1180          */
setWaitTime(int waitTime)1181         public Builder setWaitTime(int waitTime) {
1182             this.mWaitTime = waitTime;
1183             return this;
1184         }
1185 
1186         /**
1187          * Sets the time to limit max connection for the APN.
1188          *
1189          * @param maxConnsTime the time to limit max connection for the APN
1190          * @hide
1191          */
setMaxConnsTime(int maxConnsTime)1192         public Builder setMaxConnsTime(int maxConnsTime) {
1193             this.mMaxConnsTime = maxConnsTime;
1194             return this;
1195         }
1196 
1197         /**
1198          * Sets the MVNO match data for the APN.
1199          *
1200          * @param mvnoMatchData the MVNO match data for the APN
1201          * @hide
1202          */
setMvnoMatchData(String mvnoMatchData)1203         public Builder setMvnoMatchData(String mvnoMatchData) {
1204             this.mMvnoMatchData = mvnoMatchData;
1205             return this;
1206         }
1207 
1208         /**
1209          * Sets a human-readable name that describes the APN.
1210          *
1211          * @param entryName the entry name to set for the APN
1212          */
setEntryName(String entryName)1213         public Builder setEntryName(String entryName) {
1214             this.mEntryName = entryName;
1215             return this;
1216         }
1217 
1218         /**
1219          * Sets the name of the APN.
1220          *
1221          * @param apnName the name to set for the APN
1222          */
setApnName(String apnName)1223         public Builder setApnName(String apnName) {
1224             this.mApnName = apnName;
1225             return this;
1226         }
1227 
1228         /**
1229          * Sets the address of an HTTP proxy for the APN. The proxy address can be an IP address or
1230          * hostname. If {@code proxy} contains both an IP address and hostname, this method ignores
1231          * the IP address.
1232          *
1233          * <p>The {@link java.net.InetAddress} methods
1234          * {@link java.net.InetAddress#getAllByName getAllByName()} require DNS for hostname
1235          * resolution. To avoid this requirement when setting a hostname, call
1236          * {@link java.net.InetAddress#getByAddress(java.lang.String, byte[])} with both the
1237          * hostname and a dummy IP address. See {@link ApnSetting.Builder above} for an example.
1238          *
1239          * @param proxy the proxy address to set for the APN
1240          */
setProxyAddress(InetAddress proxy)1241         public Builder setProxyAddress(InetAddress proxy) {
1242             this.mProxyAddress = proxy;
1243             return this;
1244         }
1245 
1246         /**
1247          * Sets the proxy port of the APN.
1248          *
1249          * @param port the proxy port to set for the APN
1250          */
setProxyPort(int port)1251         public Builder setProxyPort(int port) {
1252             this.mProxyPort = port;
1253             return this;
1254         }
1255 
1256         /**
1257          * Sets the MMSC Uri of the APN.
1258          *
1259          * @param mmsc the MMSC Uri to set for the APN
1260          */
setMmsc(Uri mmsc)1261         public Builder setMmsc(Uri mmsc) {
1262             this.mMmsc = mmsc;
1263             return this;
1264         }
1265 
1266         /**
1267          * Sets the address of an MMS proxy for the APN. The MMS proxy address can be an IP address
1268          * or hostname. If {@code mmsProxy} contains both an IP address and hostname, this method
1269          * ignores the IP address.
1270          *
1271          * <p>The {@link java.net.InetAddress} methods
1272          * {@link java.net.InetAddress#getByName getByName()} and
1273          * {@link java.net.InetAddress#getAllByName getAllByName()} require DNS for hostname
1274          * resolution. To avoid this requirement when setting a hostname, call
1275          * {@link java.net.InetAddress#getByAddress(java.lang.String, byte[])} with both the
1276          * hostname and a dummy IP address. See {@link ApnSetting.Builder above} for an example.
1277          *
1278          * @param mmsProxy the MMS proxy address to set for the APN
1279          */
setMmsProxyAddress(InetAddress mmsProxy)1280         public Builder setMmsProxyAddress(InetAddress mmsProxy) {
1281             this.mMmsProxyAddress = mmsProxy;
1282             return this;
1283         }
1284 
1285         /**
1286          * Sets the MMS proxy port of the APN.
1287          *
1288          * @param mmsPort the MMS proxy port to set for the APN
1289          */
setMmsProxyPort(int mmsPort)1290         public Builder setMmsProxyPort(int mmsPort) {
1291             this.mMmsProxyPort = mmsPort;
1292             return this;
1293         }
1294 
1295         /**
1296          * Sets the APN username of the APN.
1297          *
1298          * @param user the APN username to set for the APN
1299          */
setUser(String user)1300         public Builder setUser(String user) {
1301             this.mUser = user;
1302             return this;
1303         }
1304 
1305         /**
1306          * Sets the APN password of the APN.
1307          *
1308          * @see android.provider.Telephony.Carriers#PASSWORD
1309          * @param password the APN password to set for the APN
1310          */
setPassword(String password)1311         public Builder setPassword(String password) {
1312             this.mPassword = password;
1313             return this;
1314         }
1315 
1316         /**
1317          * Sets the authentication type of the APN.
1318          *
1319          * @param authType the authentication type to set for the APN
1320          */
setAuthType(@uthType int authType)1321         public Builder setAuthType(@AuthType int authType) {
1322             this.mAuthType = authType;
1323             return this;
1324         }
1325 
1326         /**
1327          * Sets the bitmask of APN types.
1328          *
1329          * <p>Apn types are usage categories for an APN entry. One APN entry may support multiple
1330          * APN types, eg, a single APN may service regular internet traffic ("default") as well as
1331          * MMS-specific connections.
1332          *
1333          * <p>The bitmask of APN types is calculated from APN types defined in {@link ApnSetting}.
1334          *
1335          * @param apnTypeBitmask a bitmask describing the types of the APN
1336          */
setApnTypeBitmask(@pnType int apnTypeBitmask)1337         public Builder setApnTypeBitmask(@ApnType int apnTypeBitmask) {
1338             this.mApnTypeBitmask = apnTypeBitmask;
1339             return this;
1340         }
1341 
1342         /**
1343          * Sets the numeric operator ID for the APN. Numeric operator ID is defined as
1344          * {@link android.provider.Telephony.Carriers#MCC} +
1345          * {@link android.provider.Telephony.Carriers#MNC}.
1346          *
1347          * @param operatorNumeric the numeric operator ID to set for this entry
1348          */
setOperatorNumeric(String operatorNumeric)1349         public Builder setOperatorNumeric(String operatorNumeric) {
1350             this.mOperatorNumeric = operatorNumeric;
1351             return this;
1352         }
1353 
1354         /**
1355          * Sets the protocol to use to connect to this APN.
1356          *
1357          * <p>Protocol is one of the {@code PDP_type} values in TS 27.007 section 10.1.1.
1358          *
1359          * @param protocol the protocol to set to use to connect to this APN
1360          */
setProtocol(@rotocolType int protocol)1361         public Builder setProtocol(@ProtocolType int protocol) {
1362             this.mProtocol = protocol;
1363             return this;
1364         }
1365 
1366         /**
1367          * Sets the protocol to use to connect to this APN when the device is roaming.
1368          *
1369          * <p>Roaming protocol is one of the {@code PDP_type} values in TS 27.007 section 10.1.1.
1370          *
1371          * @param roamingProtocol the protocol to set to use to connect to this APN when roaming
1372          */
setRoamingProtocol(@rotocolType int roamingProtocol)1373         public Builder setRoamingProtocol(@ProtocolType  int roamingProtocol) {
1374             this.mRoamingProtocol = roamingProtocol;
1375             return this;
1376         }
1377 
1378         /**
1379          * Sets the current status for this APN.
1380          *
1381          * @param carrierEnabled the current status to set for this APN
1382          */
setCarrierEnabled(boolean carrierEnabled)1383         public Builder setCarrierEnabled(boolean carrierEnabled) {
1384             this.mCarrierEnabled = carrierEnabled;
1385             return this;
1386         }
1387 
1388         /**
1389          * Sets Radio Technology (Network Type) info for this APN.
1390          *
1391          * @param networkTypeBitmask the Radio Technology (Network Type) info
1392          */
setNetworkTypeBitmask(int networkTypeBitmask)1393         public Builder setNetworkTypeBitmask(int networkTypeBitmask) {
1394             this.mNetworkTypeBitmask = networkTypeBitmask;
1395             return this;
1396         }
1397 
1398         /**
1399          * Sets the MVNO match type for this APN.
1400          *
1401          * @param mvnoType the MVNO match type to set for this APN
1402          */
setMvnoType(@vnoType int mvnoType)1403         public Builder setMvnoType(@MvnoType int mvnoType) {
1404             this.mMvnoType = mvnoType;
1405             return this;
1406         }
1407 
1408         /**
1409          * Builds {@link ApnSetting} from this builder.
1410          *
1411          * @return {@code null} if {@link #setApnName(String)} or {@link #setEntryName(String)}
1412          * is empty, or {@link #setApnTypeBitmask(int)} doesn't contain a valid bit,
1413          * {@link ApnSetting} built from this builder otherwise.
1414          */
build()1415         public ApnSetting build() {
1416             if ((mApnTypeBitmask & ApnTypes.ALL) == 0 || TextUtils.isEmpty(mApnName)
1417                 || TextUtils.isEmpty(mEntryName)) {
1418                 return null;
1419             }
1420             return new ApnSetting(this);
1421         }
1422     }
1423 }
1424