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.IntDef;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.annotation.SystemApi;
25 import android.os.Build;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 import android.telephony.TelephonyManager.NetworkTypeBitMask;
29 import android.telephony.data.ApnSetting.ApnType;
30 import android.telephony.data.ApnSetting.AuthType;
31 import android.text.TextUtils;
32 
33 import com.android.internal.telephony.RILConstants;
34 
35 import java.lang.annotation.Retention;
36 import java.lang.annotation.RetentionPolicy;
37 import java.util.Objects;
38 
39 /**
40  * Description of a mobile data profile used for establishing
41  * data connections.
42  *
43  * @hide
44  */
45 @SystemApi
46 public final class DataProfile implements Parcelable {
47     /** @hide */
48     @Retention(RetentionPolicy.SOURCE)
49     @IntDef(prefix = {"TYPE_"},
50             value = {
51                     TYPE_COMMON,
52                     TYPE_3GPP,
53                     TYPE_3GPP2})
54     public @interface Type {}
55 
56     /** Common data profile */
57     public static final int TYPE_COMMON = 0;
58 
59     /** 3GPP type data profile */
60     public static final int TYPE_3GPP = 1;
61 
62     /** 3GPP2 type data profile */
63     public static final int TYPE_3GPP2 = 2;
64 
65     private final int mProfileId;
66 
67     private final String mApn;
68 
69     @ProtocolType
70     private final int mProtocolType;
71 
72     @AuthType
73     private final int mAuthType;
74 
75     private final String mUserName;
76 
77     private final String mPassword;
78 
79     @Type
80     private final int mType;
81 
82     private final int mMaxConnectionsTime;
83 
84     private final int mMaxConnections;
85 
86     private final int mWaitTime;
87 
88     private final boolean mEnabled;
89 
90     @ApnType
91     private final int mSupportedApnTypesBitmask;
92 
93     @ProtocolType
94     private final int mRoamingProtocolType;
95 
96     @NetworkTypeBitMask
97     private final int mBearerBitmask;
98 
99     private final int mMtu;
100 
101     private final boolean mPersistent;
102 
103     private final boolean mPreferred;
104 
105     /** @hide */
DataProfile(int profileId, String apn, @ProtocolType int protocolType, int authType, String userName, String password, int type, int maxConnectionsTime, int maxConnections, int waitTime, boolean enabled, @ApnType int supportedApnTypesBitmask, @ProtocolType int roamingProtocolType, @NetworkTypeBitMask int bearerBitmask, int mtu, boolean persistent, boolean preferred)106     private DataProfile(int profileId, String apn, @ProtocolType int protocolType, int authType,
107                         String userName, String password, int type, int maxConnectionsTime,
108                         int maxConnections, int waitTime, boolean enabled,
109                         @ApnType int supportedApnTypesBitmask,
110                         @ProtocolType int roamingProtocolType,
111                         @NetworkTypeBitMask int bearerBitmask, int mtu, boolean persistent,
112                         boolean preferred) {
113         this.mProfileId = profileId;
114         this.mApn = apn;
115         this.mProtocolType = protocolType;
116         if (authType == -1) {
117             authType = TextUtils.isEmpty(userName) ? RILConstants.SETUP_DATA_AUTH_NONE
118                     : RILConstants.SETUP_DATA_AUTH_PAP_CHAP;
119         }
120         this.mAuthType = authType;
121         this.mUserName = userName;
122         this.mPassword = password;
123         this.mType = type;
124         this.mMaxConnectionsTime = maxConnectionsTime;
125         this.mMaxConnections = maxConnections;
126         this.mWaitTime = waitTime;
127         this.mEnabled = enabled;
128         this.mSupportedApnTypesBitmask = supportedApnTypesBitmask;
129         this.mRoamingProtocolType = roamingProtocolType;
130         this.mBearerBitmask = bearerBitmask;
131         this.mMtu = mtu;
132         this.mPersistent = persistent;
133         this.mPreferred = preferred;
134     }
135 
DataProfile(Parcel source)136     private DataProfile(Parcel source) {
137         mProfileId = source.readInt();
138         mApn = source.readString();
139         mProtocolType = source.readInt();
140         mAuthType = source.readInt();
141         mUserName = source.readString();
142         mPassword = source.readString();
143         mType = source.readInt();
144         mMaxConnectionsTime = source.readInt();
145         mMaxConnections = source.readInt();
146         mWaitTime = source.readInt();
147         mEnabled = source.readBoolean();
148         mSupportedApnTypesBitmask = source.readInt();
149         mRoamingProtocolType = source.readInt();
150         mBearerBitmask = source.readInt();
151         mMtu = source.readInt();
152         mPersistent = source.readBoolean();
153         mPreferred = source.readBoolean();
154     }
155 
156     /**
157      * @return Id of the data profile.
158      */
getProfileId()159     public int getProfileId() { return mProfileId; }
160 
161     /**
162      * @return The APN (Access Point Name) to establish data connection. This is a string
163      * specifically defined by the carrier.
164      */
165     @NonNull
getApn()166     public String getApn() { return mApn; }
167 
168     /**
169      * @return The connection protocol defined in 3GPP TS 27.007 section 10.1.1.
170      */
getProtocolType()171     public @ProtocolType int getProtocolType() { return mProtocolType; }
172 
173     /**
174      * @return The authentication protocol used for this PDP context.
175      */
getAuthType()176     public @AuthType int getAuthType() { return mAuthType; }
177 
178     /**
179      * @return The username for APN. Can be null.
180      */
181     @Nullable
getUserName()182     public String getUserName() { return mUserName; }
183 
184     /**
185      * @return The password for APN. Can be null.
186      */
187     @Nullable
getPassword()188     public String getPassword() { return mPassword; }
189 
190     /**
191      * @return The profile type.
192      */
getType()193     public @Type int getType() { return mType; }
194 
195     /**
196      * @return The period in seconds to limit the maximum connections.
197      *
198      * @hide
199      */
getMaxConnectionsTime()200     public int getMaxConnectionsTime() { return mMaxConnectionsTime; }
201 
202     /**
203      * @return The maximum connections allowed.
204      *
205      * @hide
206      */
getMaxConnections()207     public int getMaxConnections() { return mMaxConnections; }
208 
209     /**
210      * @return The required wait time in seconds after a successful UE initiated disconnect of a
211      * given PDN connection before the device can send a new PDN connection request for that given
212      * PDN.
213      *
214      * @hide
215      */
getWaitTime()216     public int getWaitTime() { return mWaitTime; }
217 
218     /**
219      * @return True if the profile is enabled.
220      */
isEnabled()221     public boolean isEnabled() { return mEnabled; }
222 
223     /**
224      * @return The supported APN types bitmask.
225      */
getSupportedApnTypesBitmask()226     public @ApnType int getSupportedApnTypesBitmask() { return mSupportedApnTypesBitmask; }
227 
228     /**
229      * @return The connection protocol on roaming network defined in 3GPP TS 27.007 section 10.1.1.
230      */
getRoamingProtocolType()231     public @ProtocolType int getRoamingProtocolType() { return mRoamingProtocolType; }
232 
233     /**
234      * @return The bearer bitmask indicating the applicable networks for this data profile.
235      */
getBearerBitmask()236     public @NetworkTypeBitMask int getBearerBitmask() { return mBearerBitmask; }
237 
238     /**
239      * @return The maximum transmission unit (MTU) size in bytes.
240      */
getMtu()241     public int getMtu() { return mMtu; }
242 
243     /**
244      * @return {@code true} if modem must persist this data profile.
245      */
isPersistent()246     public boolean isPersistent() { return mPersistent; }
247 
248     /**
249      * @return {@code true} if this data profile was used to bring up the last default
250      * (i.e internet) data connection successfully, or the one chosen by the user in Settings'
251      * APN editor. For one carrier there can be only one profiled preferred.
252      */
isPreferred()253     public boolean isPreferred() { return  mPreferred; }
254 
255     @Override
describeContents()256     public int describeContents() {
257         return 0;
258     }
259 
260     @Override
toString()261     public String toString() {
262         return "DataProfile=" + mProfileId + "/" + mProtocolType + "/" + mAuthType
263                 + "/" + (Build.IS_USER ? "***/***/***" :
264                          (mApn + "/" + mUserName + "/" + mPassword)) + "/" + mType + "/"
265                 + mMaxConnectionsTime + "/" + mMaxConnections + "/"
266                 + mWaitTime + "/" + mEnabled + "/" + mSupportedApnTypesBitmask + "/"
267                 + mRoamingProtocolType + "/" + mBearerBitmask + "/" + mMtu + "/" + mPersistent + "/"
268                 + mPreferred;
269     }
270 
271     @Override
writeToParcel(Parcel dest, int flags)272     public void writeToParcel(Parcel dest, int flags) {
273         dest.writeInt(mProfileId);
274         dest.writeString(mApn);
275         dest.writeInt(mProtocolType);
276         dest.writeInt(mAuthType);
277         dest.writeString(mUserName);
278         dest.writeString(mPassword);
279         dest.writeInt(mType);
280         dest.writeInt(mMaxConnectionsTime);
281         dest.writeInt(mMaxConnections);
282         dest.writeInt(mWaitTime);
283         dest.writeBoolean(mEnabled);
284         dest.writeInt(mSupportedApnTypesBitmask);
285         dest.writeInt(mRoamingProtocolType);
286         dest.writeInt(mBearerBitmask);
287         dest.writeInt(mMtu);
288         dest.writeBoolean(mPersistent);
289         dest.writeBoolean(mPreferred);
290     }
291 
292     public static final @android.annotation.NonNull Parcelable.Creator<DataProfile> CREATOR =
293             new Parcelable.Creator<DataProfile>() {
294         @Override
295         public DataProfile createFromParcel(Parcel source) {
296             return new DataProfile(source);
297         }
298 
299         @Override
300         public DataProfile[] newArray(int size) {
301             return new DataProfile[size];
302         }
303     };
304 
305     @Override
equals(Object o)306     public boolean equals(Object o) {
307         if (this == o) return true;
308         if (o == null || getClass() != o.getClass()) return false;
309         DataProfile that = (DataProfile) o;
310         return mProfileId == that.mProfileId
311                 && mProtocolType == that.mProtocolType
312                 && mAuthType == that.mAuthType
313                 && mType == that.mType
314                 && mMaxConnectionsTime == that.mMaxConnectionsTime
315                 && mMaxConnections == that.mMaxConnections
316                 && mWaitTime == that.mWaitTime
317                 && mEnabled == that.mEnabled
318                 && mSupportedApnTypesBitmask == that.mSupportedApnTypesBitmask
319                 && mRoamingProtocolType == that.mRoamingProtocolType
320                 && mBearerBitmask == that.mBearerBitmask
321                 && mMtu == that.mMtu
322                 && mPersistent == that.mPersistent
323                 && mPreferred == that.mPreferred
324                 && Objects.equals(mApn, that.mApn)
325                 && Objects.equals(mUserName, that.mUserName)
326                 && Objects.equals(mPassword, that.mPassword);
327     }
328 
329     @Override
hashCode()330     public int hashCode() {
331         return Objects.hash(mProfileId, mApn, mProtocolType, mAuthType, mUserName, mPassword, mType,
332                 mMaxConnectionsTime, mMaxConnections, mWaitTime, mEnabled,
333                 mSupportedApnTypesBitmask, mRoamingProtocolType, mBearerBitmask, mMtu, mPersistent,
334                 mPreferred);
335     }
336 
337     /**
338      * Provides a convenient way to set the fields of a {@link DataProfile} when creating a new
339      * instance.
340      *
341      * <p>The example below shows how you might create a new {@code DataProfile}:
342      *
343      * <pre><code>
344      *
345      * DataProfile dp = new DataProfile.Builder()
346      *     .setApn("apn.xyz.com")
347      *     .setProtocol(ApnSetting.PROTOCOL_IPV4V6)
348      *     .build();
349      * </code></pre>
350      */
351     public static final class Builder {
352         private int mProfileId;
353 
354         private String mApn;
355 
356         @ProtocolType
357         private int mProtocolType;
358 
359         @AuthType
360         private int mAuthType;
361 
362         private String mUserName;
363 
364         private String mPassword;
365 
366         @Type
367         private int mType;
368 
369         private int mMaxConnectionsTime;
370 
371         private int mMaxConnections;
372 
373         private int mWaitTime;
374 
375         private boolean mEnabled;
376 
377         @ApnType
378         private int mSupportedApnTypesBitmask;
379 
380         @ProtocolType
381         private int mRoamingProtocolType;
382 
383         @NetworkTypeBitMask
384         private int mBearerBitmask;
385 
386         private int mMtu;
387 
388         private boolean mPersistent;
389 
390         private boolean mPreferred;
391 
392         /**
393          * Default constructor for Builder.
394          */
Builder()395         public Builder() {
396         }
397 
398         /**
399          * Set profile id. Note that this is not a global unique id of the data profile. This id
400          * is only used by certain CDMA carriers to identify the type of data profile.
401          *
402          * @param profileId Network domain.
403          * @return The same instance of the builder.
404          */
setProfileId(int profileId)405         public @NonNull Builder setProfileId(int profileId) {
406             mProfileId = profileId;
407             return this;
408         }
409 
410         /**
411          * Set the APN (Access Point Name) to establish data connection. This is a string
412          * specifically defined by the carrier.
413          *
414          * @param apn Access point name
415          * @return The same instance of the builder.
416          */
setApn(@onNull String apn)417         public @NonNull Builder setApn(@NonNull String apn) {
418             mApn = apn;
419             return this;
420         }
421 
422         /**
423          * Set the connection protocol type.
424          *
425          * @param protocolType The connection protocol defined in 3GPP TS 27.007 section 10.1.1.
426          * @return The same instance of the builder.
427          */
setProtocolType(@rotocolType int protocolType)428         public @NonNull Builder setProtocolType(@ProtocolType int protocolType) {
429             mProtocolType = protocolType;
430             return this;
431         }
432 
433         /**
434          * Set the authentication type.
435          *
436          * @param authType The authentication type
437          * @return The same instance of the builder.
438          */
setAuthType(@uthType int authType)439         public @NonNull Builder setAuthType(@AuthType int authType) {
440             mAuthType = authType;
441             return this;
442         }
443 
444         /**
445          * Set the user name
446          *
447          * @param userName The user name
448          * @return The same instance of the builder.
449          */
setUserName(@onNull String userName)450         public @NonNull Builder setUserName(@NonNull String userName) {
451             mUserName = userName;
452             return this;
453         }
454 
455         /**
456          * Set the password
457          *
458          * @param password The password
459          * @return The same instance of the builder.
460          */
setPassword(@onNull String password)461         public @NonNull Builder setPassword(@NonNull String password) {
462             mPassword = password;
463             return this;
464         }
465 
466         /**
467          * Set the type
468          *
469          * @param type The profile type
470          * @return The same instance of the builder.
471          */
setType(@ype int type)472         public @NonNull Builder setType(@Type int type) {
473             mType = type;
474             return this;
475         }
476 
477         /**
478          * Set the period in seconds to limit the maximum connections.
479          *
480          * @param maxConnectionsTime The profile type
481          * @return The same instance of the builder.
482          *
483          * @hide
484          */
setMaxConnectionsTime(int maxConnectionsTime)485         public @NonNull Builder setMaxConnectionsTime(int maxConnectionsTime) {
486             mMaxConnectionsTime = maxConnectionsTime;
487             return this;
488         }
489 
490         /**
491          * Set the maximum connections allowed.
492          *
493          * @param maxConnections The maximum connections allowed.
494          * @return The same instance of the builder.
495          *
496          * @hide
497          */
setMaxConnections(int maxConnections)498         public @NonNull Builder setMaxConnections(int maxConnections) {
499             mMaxConnections = maxConnections;
500             return this;
501         }
502 
503         /**
504          * Set the period in seconds to limit the maximum connections.
505          *
506          * @param waitTime The required wait time in seconds after a successful UE initiated
507          * disconnect of a given PDN connection before the device can send a new PDN connection
508          * request for that given PDN.
509          *
510          * @return The same instance of the builder.
511          *
512          * @hide
513          */
setWaitTime(int waitTime)514         public @NonNull Builder setWaitTime(int waitTime) {
515             mWaitTime = waitTime;
516             return this;
517         }
518 
519         /**
520          * Enable the data profile
521          *
522          * @param isEnabled {@code true} to enable the data profile, otherwise disable.
523          * @return The same instance of the builder.
524          */
enable(boolean isEnabled)525         public @NonNull Builder enable(boolean isEnabled) {
526             mEnabled = isEnabled;
527             return this;
528         }
529 
530         /**
531          * Set the supported APN types bitmask.
532          *
533          * @param supportedApnTypesBitmask The supported APN types bitmask.
534          * @return The same instance of the builder.
535          */
setSupportedApnTypesBitmask(@pnType int supportedApnTypesBitmask)536         public @NonNull Builder setSupportedApnTypesBitmask(@ApnType int supportedApnTypesBitmask) {
537             mSupportedApnTypesBitmask = supportedApnTypesBitmask;
538             return this;
539         }
540 
541         /**
542          * Set the connection protocol type for roaming.
543          *
544          * @param protocolType The connection protocol defined in 3GPP TS 27.007 section 10.1.1.
545          * @return The same instance of the builder.
546          */
setRoamingProtocolType(@rotocolType int protocolType)547         public @NonNull Builder setRoamingProtocolType(@ProtocolType int protocolType) {
548             mRoamingProtocolType = protocolType;
549             return this;
550         }
551 
552         /**
553          * Set the bearer bitmask indicating the applicable networks for this data profile.
554          *
555          * @param bearerBitmask The bearer bitmask indicating the applicable networks for this data
556          * profile.
557          * @return The same instance of the builder.
558          */
setBearerBitmask(@etworkTypeBitMask int bearerBitmask)559         public @NonNull Builder setBearerBitmask(@NetworkTypeBitMask int bearerBitmask) {
560             mBearerBitmask = bearerBitmask;
561             return this;
562         }
563 
564         /**
565          * Set the maximum transmission unit (MTU) size in bytes.
566          *
567          * @param mtu The maximum transmission unit (MTU) size in bytes.
568          * @return The same instance of the builder.
569          */
setMtu(int mtu)570         public @NonNull Builder setMtu(int mtu) {
571             mMtu = mtu;
572             return this;
573         }
574 
575         /**
576          * Set data profile as preferred/non-preferred.
577          *
578          * @param isPreferred {@code true} if this data profile was used to bring up the last
579          * default (i.e internet) data connection successfully, or the one chosen by the user in
580          * Settings' APN editor. For one carrier there can be only one profiled preferred.
581          * @return The same instance of the builder.
582          */
setPreferred(boolean isPreferred)583         public @NonNull Builder setPreferred(boolean isPreferred) {
584             mPreferred = isPreferred;
585             return this;
586         }
587 
588         /**
589          * Set data profile as persistent/non-persistent
590          *
591          * @param isPersistent {@code true} if this data profile was used to bring up the last
592          * default (i.e internet) data connection successfully.
593          * @return The same instance of the builder.
594          */
setPersistent(boolean isPersistent)595         public @NonNull Builder setPersistent(boolean isPersistent) {
596             mPersistent = isPersistent;
597             return this;
598         }
599 
600         /**
601          * Build the DataProfile object
602          *
603          * @return The data profile object
604          */
build()605         public @NonNull DataProfile build() {
606             return new DataProfile(mProfileId, mApn, mProtocolType, mAuthType, mUserName, mPassword,
607                     mType, mMaxConnectionsTime, mMaxConnections, mWaitTime, mEnabled,
608                     mSupportedApnTypesBitmask, mRoamingProtocolType, mBearerBitmask, mMtu,
609                     mPersistent, mPreferred);
610         }
611     }
612 }
613