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