• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 
17 package android.telephony.data;
18 
19 import static android.telephony.data.ApnSetting.ProtocolType;
20 
21 import android.annotation.ElapsedRealtimeLong;
22 import android.annotation.IntDef;
23 import android.annotation.NonNull;
24 import android.annotation.Nullable;
25 import android.annotation.SystemApi;
26 import android.net.NetworkCapabilities;
27 import android.os.Parcel;
28 import android.os.Parcelable;
29 import android.telephony.Annotation.NetCapability;
30 import android.telephony.TelephonyManager;
31 import android.telephony.TelephonyManager.NetworkTypeBitMask;
32 import android.telephony.data.ApnSetting.ApnType;
33 import android.telephony.data.ApnSetting.AuthType;
34 import android.text.TextUtils;
35 
36 import java.lang.annotation.Retention;
37 import java.lang.annotation.RetentionPolicy;
38 import java.util.Objects;
39 
40 /**
41  * Description of a mobile data profile used for establishing data networks. The data profile
42  * consist an {@link ApnSetting} which is needed for 2G/3G/4G networks bring up, and a
43  * {@link TrafficDescriptor} contains additional information that can be used for 5G standalone
44  * network bring up.
45  *
46  * @hide
47  */
48 @SystemApi
49 public final class DataProfile implements Parcelable {
50     /** @hide */
51     @Retention(RetentionPolicy.SOURCE)
52     @IntDef(prefix = {"TYPE_"},
53             value = {
54                     TYPE_COMMON,
55                     TYPE_3GPP,
56                     TYPE_3GPP2})
57     public @interface Type {}
58 
59     /** Common data profile */
60     public static final int TYPE_COMMON = 0;
61 
62     /** 3GPP type data profile */
63     public static final int TYPE_3GPP = 1;
64 
65     /** 3GPP2 type data profile */
66     public static final int TYPE_3GPP2 = 2;
67 
68     private final @Type int mType;
69 
70     private final @Nullable ApnSetting mApnSetting;
71 
72     private final @Nullable TrafficDescriptor mTrafficDescriptor;
73 
74     private boolean mPreferred;
75 
76     /**
77      * The last timestamp of this data profile being used for data network setup. Never add this
78      * to {@link #equals(Object)} and {@link #hashCode()}.
79      */
80     private @ElapsedRealtimeLong long mSetupTimestamp;
81 
DataProfile(@onNull Builder builder)82     private DataProfile(@NonNull Builder builder) {
83         mApnSetting = builder.mApnSetting;
84         mTrafficDescriptor = builder.mTrafficDescriptor;
85         mPreferred = builder.mPreferred;
86 
87         if (builder.mType != -1) {
88             mType = builder.mType;
89         } else if (mApnSetting != null) {
90             int networkTypes = mApnSetting.getNetworkTypeBitmask();
91 
92             if (networkTypes == 0) {
93                 mType = DataProfile.TYPE_COMMON;
94             } else if ((networkTypes & TelephonyManager.NETWORK_STANDARDS_FAMILY_BITMASK_3GPP2)
95                     == networkTypes) {
96                 mType = DataProfile.TYPE_3GPP2;
97             } else if ((networkTypes & TelephonyManager.NETWORK_STANDARDS_FAMILY_BITMASK_3GPP)
98                     == networkTypes) {
99                 mType = DataProfile.TYPE_3GPP;
100             } else {
101                 mType = DataProfile.TYPE_COMMON;
102             }
103         } else {
104             mType = DataProfile.TYPE_COMMON;
105         }
106     }
107 
DataProfile(Parcel source)108     private DataProfile(Parcel source) {
109         mType = source.readInt();
110         mApnSetting = source.readParcelable(ApnSetting.class.getClassLoader(), android.telephony.data.ApnSetting.class);
111         mTrafficDescriptor = source.readParcelable(TrafficDescriptor.class.getClassLoader(), android.telephony.data.TrafficDescriptor.class);
112         mPreferred = source.readBoolean();
113         mSetupTimestamp = source.readLong();
114     }
115 
116     /**
117      * @return Id of the data profile.
118      * @deprecated Use {@link #getApnSetting()} and {@link ApnSetting#getProfileId()} instead.
119      */
120     @Deprecated
getProfileId()121     public int getProfileId() {
122         if (mApnSetting != null) {
123             return mApnSetting.getProfileId();
124         }
125         return 0;
126     }
127 
128     /**
129      * @return The APN (Access Point Name) to establish data connection. This is a string
130      * specifically defined by the carrier.
131      * @deprecated Use {@link #getApnSetting()} and {@link ApnSetting#getApnName()} instead.
132      */
133     @Deprecated
getApn()134     public @NonNull String getApn() {
135         if (mApnSetting != null) {
136             return TextUtils.emptyIfNull(mApnSetting.getApnName());
137         }
138         return "";
139     }
140 
141     /**
142      * @return The connection protocol defined in 3GPP TS 27.007 section 10.1.1.
143      * @deprecated Use {@link #getApnSetting()} and {@link ApnSetting#getProtocol()} instead.
144      */
145     @Deprecated
getProtocolType()146     public @ProtocolType int getProtocolType() {
147         if (mApnSetting != null) {
148             return mApnSetting.getProtocol();
149         }
150         return ApnSetting.PROTOCOL_IP;
151     }
152 
153     /**
154      * @return The authentication protocol used for this PDP context.
155      * @deprecated Use {@link #getApnSetting()} and {@link ApnSetting#getAuthType()} instead.
156      */
157     @Deprecated
getAuthType()158     public @AuthType int getAuthType() {
159         if (mApnSetting != null) {
160             return mApnSetting.getAuthType();
161         }
162         return ApnSetting.AUTH_TYPE_NONE;
163     }
164 
165     /**
166      * @return The username for APN.
167      * @deprecated Use {@link #getApnSetting()} and {@link ApnSetting#getUser()} instead.
168      */
169     @Deprecated
getUserName()170     public @Nullable String getUserName() {
171         if (mApnSetting != null) {
172             return mApnSetting.getUser();
173         }
174         return null;
175     }
176 
177     /**
178      * @return The password for APN.
179      * @deprecated Use {@link #getApnSetting()} and {@link ApnSetting#getPassword()} instead.
180      */
181     @Deprecated
getPassword()182     public @Nullable String getPassword() {
183         if (mApnSetting != null) {
184             return mApnSetting.getPassword();
185         }
186         return null;
187     }
188 
189     /**
190      * @return The profile type.
191      */
getType()192     public @Type int getType() {
193         return mType;
194     }
195 
196     /**
197      * @return The period in seconds to limit the maximum connections.
198      *
199      * @hide
200      */
getMaxConnectionsTime()201     public int getMaxConnectionsTime() {
202         if (mApnSetting != null) {
203             return mApnSetting.getMaxConnsTime();
204         }
205         return 0;
206     }
207 
208     /**
209      * @return The maximum connections allowed.
210      *
211      * @hide
212      */
getMaxConnections()213     public int getMaxConnections() {
214         if (mApnSetting != null) {
215             return mApnSetting.getMaxConns();
216         }
217         return 0;
218     }
219 
220     /**
221      * @return The required wait time in seconds after a successful UE initiated disconnect of a
222      * given PDN connection before the device can send a new PDN connection request for that given
223      * PDN.
224      *
225      * @hide
226      */
getWaitTime()227     public int getWaitTime() {
228         if (mApnSetting != null) {
229             return mApnSetting.getWaitTime();
230         }
231         return 0;
232     }
233 
234     /**
235      * @return {@code true} if the profile is enabled. If the profile only has a
236      * {@link TrafficDescriptor}, but no {@link ApnSetting}, then this profile is always enabled.
237      */
isEnabled()238     public boolean isEnabled() {
239         if (mApnSetting != null) {
240             return mApnSetting.isEnabled();
241         }
242         return true;
243     }
244 
245     /**
246      * @return The supported APN types bitmask.
247      * @deprecated Use {@link #getApnSetting()} and {@link ApnSetting#getApnTypeBitmask()} instead.
248      */
getSupportedApnTypesBitmask()249     @Deprecated public @ApnType int getSupportedApnTypesBitmask() {
250         if (mApnSetting != null) {
251             return mApnSetting.getApnTypeBitmask();
252         }
253         return ApnSetting.TYPE_NONE;
254     }
255 
256     /**
257      * @return The connection protocol on roaming network defined in 3GPP TS 27.007 section 10.1.1.
258      * @deprecated Use {@link #getApnSetting()} and {@link ApnSetting#getRoamingProtocol()} instead.
259      */
260     @Deprecated
getRoamingProtocolType()261     public @ProtocolType int getRoamingProtocolType() {
262         if (mApnSetting != null) {
263             return mApnSetting.getRoamingProtocol();
264         }
265         return ApnSetting.PROTOCOL_IP;
266     }
267 
268     /**
269      * @return The bearer bitmask indicating the applicable networks for this data profile.
270      * @deprecated use {@link #getApnSetting()} and {@link ApnSetting#getNetworkTypeBitmask()}
271      * instead.
272      */
273     @Deprecated
getBearerBitmask()274     public @NetworkTypeBitMask int getBearerBitmask() {
275         if (mApnSetting != null) {
276             return mApnSetting.getNetworkTypeBitmask();
277         }
278         return (int) TelephonyManager.NETWORK_TYPE_BITMASK_UNKNOWN;
279     }
280 
281     /**
282      * @return The maximum transmission unit (MTU) size in bytes.
283      * @deprecated use {@link #getApnSetting()} and {@link ApnSetting#getMtuV4()}/
284      * {@link ApnSetting#getMtuV6()} instead.
285      */
286     @Deprecated
getMtu()287     public int getMtu() {
288         return getMtuV4();
289     }
290 
291     /**
292      * This replaces the deprecated method getMtu.
293      * @return The maximum transmission unit (MTU) size in bytes, for IPv4.
294      * @deprecated use {@link #getApnSetting()} and {@link ApnSetting#getMtuV4()} instead.
295      */
296     @Deprecated
getMtuV4()297     public int getMtuV4() {
298         if (mApnSetting != null) {
299             return mApnSetting.getMtuV4();
300         }
301         return 0;
302     }
303 
304     /**
305      * @return The maximum transmission unit (MTU) size in bytes, for IPv6.
306      * @deprecated use {@link #getApnSetting()} and {@link ApnSetting#getMtuV6()} instead.
307      */
308     @Deprecated
getMtuV6()309     public int getMtuV6() {
310         if (mApnSetting != null) {
311             return mApnSetting.getMtuV6();
312         }
313         return 0;
314     }
315 
316     /**
317      * @return {@code true} if modem must persist this data profile.
318      * @deprecated Use {@link #getApnSetting()} and {@link ApnSetting#isPersistent()} instead.
319      */
320     @Deprecated
isPersistent()321     public boolean isPersistent() {
322         if (mApnSetting != null) {
323             return mApnSetting.isPersistent();
324         }
325         return false;
326     }
327 
328     /**
329      * Set the preferred flag for the data profile.
330      *
331      * @param preferred {@code true} if this data profile is preferred for internet.
332      * @hide
333      */
setPreferred(boolean preferred)334     public void setPreferred(boolean preferred) {
335         mPreferred = preferred;
336     }
337 
338     /**
339      * @return {@code true} if this data profile was used to bring up the last default
340      * (i.e internet) data connection successfully, or the one chosen by the user in Settings'
341      * APN editor. For one carrier there can be only one profiled preferred.
342      */
isPreferred()343     public boolean isPreferred() {
344         return mPreferred;
345     }
346 
347     /**
348      * @return The APN setting {@link ApnSetting}, which is used to establish data network on
349      * 2G/3G/4G.
350      */
getApnSetting()351     public @Nullable ApnSetting getApnSetting() {
352         return mApnSetting;
353     }
354 
355     /**
356      * @return The traffic descriptor {@link TrafficDescriptor}, which can be used to establish
357      * data network on 5G.
358      */
getTrafficDescriptor()359     public @Nullable TrafficDescriptor getTrafficDescriptor() {
360         return mTrafficDescriptor;
361     }
362 
363     /**
364      * Check if this data profile can satisfy certain network capabilities
365      *
366      * @param networkCapabilities The network capabilities. Note that the non-APN-type capabilities
367      * will be ignored.
368      *
369      * @return {@code true} if this data profile can satisfy the given network capabilities.
370      * @hide
371      */
canSatisfy(@onNull @etCapability int[] networkCapabilities)372     public boolean canSatisfy(@NonNull @NetCapability int[] networkCapabilities) {
373         if (mApnSetting != null) {
374             for (int netCap : networkCapabilities) {
375                 if (!canSatisfy(netCap)) {
376                     return false;
377                 }
378             }
379             return true;
380         }
381         return false;
382     }
383 
384     /**
385      * Check if this data profile can satisfy a certain network capability.
386      *
387      * @param networkCapability The network capability. Note that the non-APN-type capability
388      * will always be satisfied.
389      * @return {@code true} if this data profile can satisfy the given network capability.
390      * @hide
391      */
canSatisfy(@etCapability int networkCapability)392     public boolean canSatisfy(@NetCapability int networkCapability) {
393         return mApnSetting != null && mApnSetting.canHandleType(
394                 networkCapabilityToApnType(networkCapability));
395     }
396 
397     /**
398      * Convert network capability into APN type.
399      *
400      * @param networkCapability Network capability.
401      * @return APN type.
402      * @hide
403      */
networkCapabilityToApnType(@etCapability int networkCapability)404     private static @ApnType int networkCapabilityToApnType(@NetCapability int networkCapability) {
405         switch (networkCapability) {
406             case NetworkCapabilities.NET_CAPABILITY_MMS:
407                 return ApnSetting.TYPE_MMS;
408             case NetworkCapabilities.NET_CAPABILITY_SUPL:
409                 return ApnSetting.TYPE_SUPL;
410             case NetworkCapabilities.NET_CAPABILITY_DUN:
411                 return ApnSetting.TYPE_DUN;
412             case NetworkCapabilities.NET_CAPABILITY_FOTA:
413                 return ApnSetting.TYPE_FOTA;
414             case NetworkCapabilities.NET_CAPABILITY_IMS:
415                 return ApnSetting.TYPE_IMS;
416             case NetworkCapabilities.NET_CAPABILITY_CBS:
417                 return ApnSetting.TYPE_CBS;
418             case NetworkCapabilities.NET_CAPABILITY_XCAP:
419                 return ApnSetting.TYPE_XCAP;
420             case NetworkCapabilities.NET_CAPABILITY_EIMS:
421                 return ApnSetting.TYPE_EMERGENCY;
422             case NetworkCapabilities.NET_CAPABILITY_INTERNET:
423                 return ApnSetting.TYPE_DEFAULT;
424             case NetworkCapabilities.NET_CAPABILITY_MCX:
425                 return ApnSetting.TYPE_MCX;
426             case NetworkCapabilities.NET_CAPABILITY_IA:
427                 return ApnSetting.TYPE_IA;
428             case NetworkCapabilities.NET_CAPABILITY_BIP:
429                 return ApnSetting.TYPE_BIP;
430             case NetworkCapabilities.NET_CAPABILITY_VSIM:
431                 return ApnSetting.TYPE_VSIM;
432             case NetworkCapabilities.NET_CAPABILITY_ENTERPRISE:
433                 return ApnSetting.TYPE_ENTERPRISE;
434             default:
435                 return ApnSetting.TYPE_NONE;
436         }
437     }
438 
439     /**
440      * Set the timestamp of this data profile being used for data network setup.
441      *
442      * @hide
443      */
setLastSetupTimestamp(@lapsedRealtimeLong long timestamp)444     public void setLastSetupTimestamp(@ElapsedRealtimeLong long timestamp) {
445         mSetupTimestamp = timestamp;
446     }
447 
448     /**
449      * @return the timestamp of this data profile being used for data network setup.
450      *
451      * @hide
452      */
getLastSetupTimestamp()453     public @ElapsedRealtimeLong long getLastSetupTimestamp() {
454         return mSetupTimestamp;
455     }
456 
457     @Override
describeContents()458     public int describeContents() {
459         return 0;
460     }
461 
462     @NonNull
463     @Override
toString()464     public String toString() {
465         return "[DataProfile=" + mApnSetting + ", " + mTrafficDescriptor + ", preferred="
466                 + mPreferred + "]";
467     }
468 
469     @Override
writeToParcel(Parcel dest, int flags)470     public void writeToParcel(Parcel dest, int flags) {
471         dest.writeInt(mType);
472         dest.writeParcelable(mApnSetting, flags);
473         dest.writeParcelable(mTrafficDescriptor, flags);
474         dest.writeBoolean(mPreferred);
475         dest.writeLong(mSetupTimestamp);
476     }
477 
478     public static final @android.annotation.NonNull Parcelable.Creator<DataProfile> CREATOR =
479             new Parcelable.Creator<DataProfile>() {
480         @Override
481         public DataProfile createFromParcel(Parcel source) {
482             return new DataProfile(source);
483         }
484 
485         @Override
486         public DataProfile[] newArray(int size) {
487             return new DataProfile[size];
488         }
489     };
490 
491     @Override
equals(Object o)492     public boolean equals(Object o) {
493         if (this == o) return true;
494         if (o == null || getClass() != o.getClass()) return false;
495         DataProfile that = (DataProfile) o;
496         return mType == that.mType
497                 && Objects.equals(mApnSetting, that.mApnSetting)
498                 && Objects.equals(mTrafficDescriptor, that.mTrafficDescriptor);
499     }
500 
501     @Override
hashCode()502     public int hashCode() {
503         return Objects.hash(mType, mApnSetting, mTrafficDescriptor);
504     }
505 
506     /**
507      * Provides a convenient way to set the fields of a {@link DataProfile} when creating a new
508      * instance.
509      *
510      * <p>The example below shows how you might create a new {@code DataProfile}:
511      *
512      * <pre><code>
513      *
514      * DataProfile dp = new DataProfile.Builder()
515      *     .setApn("apn.xyz.com")
516      *     .setProtocol(ApnSetting.PROTOCOL_IPV4V6)
517      *     .build();
518      * </code></pre>
519      */
520     public static final class Builder {
521         private int mProfileId;
522 
523         private String mApn;
524 
525         @ProtocolType
526         private int mProtocolType;
527 
528         @AuthType
529         private int mAuthType;
530 
531         private String mUserName;
532 
533         private String mPassword;
534 
535         @Type
536         private int mType = -1;
537 
538         private boolean mEnabled = true;
539 
540         @ApnType
541         private int mSupportedApnTypesBitmask;
542 
543         @ProtocolType
544         private int mRoamingProtocolType;
545 
546         @NetworkTypeBitMask
547         private int mBearerBitmask;
548 
549         private int mMtuV4;
550 
551         private int mMtuV6;
552 
553         private boolean mPersistent;
554 
555         private boolean mPreferred;
556 
557         private ApnSetting mApnSetting;
558 
559         private TrafficDescriptor mTrafficDescriptor;
560 
561         /**
562          * Default constructor for Builder.
563          */
Builder()564         public Builder() {
565         }
566 
567         /**
568          * Set profile id. Note that this is not a global unique id of the data profile. This id
569          * is only used by certain CDMA carriers to identify the type of data profile.
570          *
571          * @param profileId Network domain.
572          * @return The same instance of the builder.
573          * @deprecated use {@link #setApnSetting(ApnSetting)} and
574          * {@link ApnSetting.Builder#setProfileId(int)} instead.
575          */
576         @Deprecated
setProfileId(int profileId)577         public @NonNull Builder setProfileId(int profileId) {
578             mProfileId = profileId;
579             return this;
580         }
581 
582         /**
583          * Set the APN (Access Point Name) to establish data connection. This is a string
584          * specifically defined by the carrier.
585          *
586          * @param apn Access point name
587          * @return The same instance of the builder.
588          * @deprecated use {@link #setApnSetting(ApnSetting)} and
589          * {@link ApnSetting.Builder#setApnName(String)} instead.
590          */
591         @Deprecated
setApn(@onNull String apn)592         public @NonNull Builder setApn(@NonNull String apn) {
593             mApn = apn;
594             return this;
595         }
596 
597         /**
598          * Set the connection protocol type.
599          *
600          * @param protocolType The connection protocol defined in 3GPP TS 27.007 section 10.1.1.
601          * @return The same instance of the builder.
602          * @deprecated use {@link #setApnSetting(ApnSetting)} and
603          * {@link ApnSetting.Builder#setProtocol(int)} instead.
604          */
605         @Deprecated
setProtocolType(@rotocolType int protocolType)606         public @NonNull Builder setProtocolType(@ProtocolType int protocolType) {
607             mProtocolType = protocolType;
608             return this;
609         }
610 
611         /**
612          * Set the authentication type.
613          *
614          * @param authType The authentication type
615          * @return The same instance of the builder.
616          * @deprecated use {@link #setApnSetting(ApnSetting)} and
617          * {@link ApnSetting.Builder#setAuthType(int)} instead.
618          */
619         @Deprecated
setAuthType(@uthType int authType)620         public @NonNull Builder setAuthType(@AuthType int authType) {
621             mAuthType = authType;
622             return this;
623         }
624 
625         /**
626          * Set the user name
627          *
628          * @param userName The user name
629          * @return The same instance of the builder.
630          * @deprecated use {@link #setApnSetting(ApnSetting)} and
631          * {@link ApnSetting.Builder#setUser(String)} instead.
632          */
633         @Deprecated
setUserName(@onNull String userName)634         public @NonNull Builder setUserName(@NonNull String userName) {
635             mUserName = userName;
636             return this;
637         }
638 
639         /**
640          * Set the password
641          *
642          * @param password The password
643          * @return The same instance of the builder.
644          * @deprecated use {@link #setApnSetting(ApnSetting)} and
645          * {@link ApnSetting.Builder#setPassword(String)} (int)} instead.
646          */
647         @Deprecated
setPassword(@onNull String password)648         public @NonNull Builder setPassword(@NonNull String password) {
649             mPassword = password;
650             return this;
651         }
652 
653         /**
654          * Set the type
655          *
656          * @param type The profile type
657          * @return The same instance of the builder.
658          */
setType(@ype int type)659         public @NonNull Builder setType(@Type int type) {
660             mType = type;
661             return this;
662         }
663 
664         /**
665          * Enable the data profile
666          *
667          * @param isEnabled {@code true} to enable the data profile, otherwise disable.
668          * @return The same instance of the builder.
669          */
enable(boolean isEnabled)670         public @NonNull Builder enable(boolean isEnabled) {
671             mEnabled = isEnabled;
672             return this;
673         }
674 
675         /**
676          * Set the supported APN types bitmask.
677          *
678          * @param supportedApnTypesBitmask The supported APN types bitmask.
679          * @return The same instance of the builder.
680          * @deprecated use {@link #setApnSetting(ApnSetting)} and
681          * {@link ApnSetting.Builder#setApnTypeBitmask(int)} instead.
682          */
683         @Deprecated
setSupportedApnTypesBitmask(@pnType int supportedApnTypesBitmask)684         public @NonNull Builder setSupportedApnTypesBitmask(@ApnType int supportedApnTypesBitmask) {
685             mSupportedApnTypesBitmask = supportedApnTypesBitmask;
686             return this;
687         }
688 
689         /**
690          * Set the connection protocol type for roaming.
691          *
692          * @param protocolType The connection protocol defined in 3GPP TS 27.007 section 10.1.1.
693          * @return The same instance of the builder.
694          * @deprecated use {@link #setApnSetting(ApnSetting)} and
695          * {@link ApnSetting.Builder#setRoamingProtocol(int)} instead.
696          */
697         @Deprecated
setRoamingProtocolType(@rotocolType int protocolType)698         public @NonNull Builder setRoamingProtocolType(@ProtocolType int protocolType) {
699             mRoamingProtocolType = protocolType;
700             return this;
701         }
702 
703         /**
704          * Set the bearer bitmask indicating the applicable networks for this data profile.
705          *
706          * @param bearerBitmask The bearer bitmask indicating the applicable networks for this data
707          * profile.
708          * @return The same instance of the builder.
709          * @deprecated use {@link #setApnSetting(ApnSetting)} and
710          * {@link ApnSetting.Builder#setNetworkTypeBitmask(int)} instead.
711          */
712         @Deprecated
setBearerBitmask(@etworkTypeBitMask int bearerBitmask)713         public @NonNull Builder setBearerBitmask(@NetworkTypeBitMask int bearerBitmask) {
714             mBearerBitmask = bearerBitmask;
715             return this;
716         }
717 
718         /**
719          * Set the maximum transmission unit (MTU) size in bytes.
720          *
721          * @param mtu The maximum transmission unit (MTU) size in bytes.
722          * @return The same instance of the builder.
723          * @deprecated use {@link #setApnSetting(ApnSetting)} and
724          * {@link ApnSetting.Builder#setMtuV4(int)}/{@link ApnSetting.Builder#setMtuV6(int)}
725          * instead.
726          */
727         @Deprecated
setMtu(int mtu)728         public @NonNull Builder setMtu(int mtu) {
729             mMtuV4 = mMtuV6 = mtu;
730             return this;
731         }
732 
733         /**
734          * Set the maximum transmission unit (MTU) size in bytes, for IPv4.
735          *
736          * @param mtu The maximum transmission unit (MTU) size in bytes.
737          * @return The same instance of the builder.
738          * @deprecated Use {{@link #setApnSetting(ApnSetting)}} and
739          * {@link ApnSetting.Builder#setMtuV4(int)} instead.
740          */
741         @Deprecated
setMtuV4(int mtu)742         public @NonNull Builder setMtuV4(int mtu) {
743             mMtuV4 = mtu;
744             return this;
745         }
746 
747         /**
748          * Set the maximum transmission unit (MTU) size in bytes, for IPv6.
749          *
750          * @param mtu The maximum transmission unit (MTU) size in bytes.
751          * @return The same instance of the builder.
752          * @deprecated Use {{@link #setApnSetting(ApnSetting)}} and
753          * {@link ApnSetting.Builder#setMtuV6(int)} instead.
754          */
755         @Deprecated
setMtuV6(int mtu)756         public @NonNull Builder setMtuV6(int mtu) {
757             mMtuV6 = mtu;
758             return this;
759         }
760 
761         /**
762          * Set data profile as preferred/non-preferred.
763          *
764          * @param isPreferred {@code true} if this data profile was used to bring up the last
765          * default (i.e internet) data connection successfully, or the one chosen by the user in
766          * Settings' APN editor. For one carrier there can be only one profiled preferred.
767          * @return The same instance of the builder.
768          */
setPreferred(boolean isPreferred)769         public @NonNull Builder setPreferred(boolean isPreferred) {
770             mPreferred = isPreferred;
771             return this;
772         }
773 
774         /**
775          * Set data profile as persistent/non-persistent.
776          *
777          * @param isPersistent {@code true} if this data profile was used to bring up the last
778          * default (i.e internet) data connection successfully.
779          * @return The same instance of the builder.
780          * @deprecated Use {{@link #setApnSetting(ApnSetting)}} and
781          * {@link ApnSetting.Builder#setPersistent(boolean)} instead.
782          */
783         @Deprecated
setPersistent(boolean isPersistent)784         public @NonNull Builder setPersistent(boolean isPersistent) {
785             mPersistent = isPersistent;
786             return this;
787         }
788 
789         /**
790          * Set the APN setting. Note that if an APN setting is not set here, then either
791          * {@link #setApn(String)} or {@link #setTrafficDescriptor(TrafficDescriptor)} must be
792          * called. Otherwise {@link IllegalArgumentException} will be thrown when {@link #build()}
793          * the data profile.
794          *
795          * @param apnSetting The APN setting.
796          * @return The same instance of the builder.
797          */
setApnSetting(@onNull ApnSetting apnSetting)798         public @NonNull Builder setApnSetting(@NonNull ApnSetting apnSetting) {
799             mApnSetting = apnSetting;
800             return this;
801         }
802 
803         /**
804          * Set the traffic descriptor. Note that if a traffic descriptor is not set here, then
805          * either {@link #setApnSetting(ApnSetting)} or {@link #setApn(String)} must be called.
806          * Otherwise {@link IllegalArgumentException} will be thrown when {@link #build()} the data
807          * profile.
808          *
809          * @param trafficDescriptor The traffic descriptor.
810          * @return The same instance of the builder.
811          */
setTrafficDescriptor(@onNull TrafficDescriptor trafficDescriptor)812         public @NonNull Builder setTrafficDescriptor(@NonNull TrafficDescriptor trafficDescriptor) {
813             mTrafficDescriptor = trafficDescriptor;
814             return this;
815         }
816 
817         /**
818          * Build the DataProfile object.
819          *
820          * @return The data profile object.
821          */
build()822         public @NonNull DataProfile build() {
823             if (mApnSetting == null && mApn != null) {
824                 // This is for backwards compatibility.
825                 mApnSetting = new ApnSetting.Builder()
826                         .setEntryName(mApn)
827                         .setApnName(mApn)
828                         .setApnTypeBitmask(mSupportedApnTypesBitmask)
829                         .setAuthType(mAuthType)
830                         .setCarrierEnabled(mEnabled)
831                         .setModemCognitive(mPersistent)
832                         .setMtuV4(mMtuV4)
833                         .setMtuV6(mMtuV6)
834                         .setNetworkTypeBitmask(mBearerBitmask)
835                         .setProfileId(mProfileId)
836                         .setPassword(mPassword)
837                         .setProtocol(mProtocolType)
838                         .setRoamingProtocol(mRoamingProtocolType)
839                         .setUser(mUserName)
840                         .build();
841             }
842 
843             if (mApnSetting == null && mTrafficDescriptor == null) {
844                 throw new IllegalArgumentException("APN setting and traffic descriptor can't be "
845                         + "both null.");
846             }
847 
848             return new DataProfile(this);
849         }
850     }
851 }
852