1 /* 2 * Copyright (C) 2014 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; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.SystemApi; 23 import android.annotation.TestApi; 24 import android.compat.Compatibility; 25 import android.compat.annotation.ChangeId; 26 import android.compat.annotation.EnabledAfter; 27 import android.compat.annotation.UnsupportedAppUsage; 28 import android.net.LinkProperties; 29 import android.os.Build; 30 import android.os.Parcel; 31 import android.os.Parcelable; 32 import android.telephony.AccessNetworkConstants.TransportType; 33 import android.telephony.Annotation.ApnType; 34 import android.telephony.Annotation.DataFailureCause; 35 import android.telephony.Annotation.DataState; 36 import android.telephony.Annotation.NetworkType; 37 import android.telephony.data.ApnSetting; 38 import android.telephony.data.DataCallResponse; 39 import android.telephony.data.Qos; 40 41 import com.android.internal.telephony.util.TelephonyUtils; 42 43 import java.lang.annotation.Retention; 44 import java.lang.annotation.RetentionPolicy; 45 import java.util.Objects; 46 47 48 /** 49 * Contains precise data connection state. 50 * 51 * The following data connection information is included in returned PreciseDataConnectionState: 52 * 53 * <ul> 54 * <li>Data connection state. 55 * <li>Network type of the connection. 56 * <li>APN types. 57 * <li>APN. 58 * <li>The properties of the network link. 59 * <li>Data connection fail cause. 60 * </ul> 61 * 62 */ 63 public final class PreciseDataConnectionState implements Parcelable { 64 private final @TransportType int mTransportType; 65 private final int mId; 66 private final int mNetId; 67 private final @DataState int mState; 68 private final @NetworkType int mNetworkType; 69 private final @DataFailureCause int mFailCause; 70 private final LinkProperties mLinkProperties; 71 private final ApnSetting mApnSetting; 72 private final Qos mDefaultQos; 73 private final @NetworkValidationStatus int mNetworkValidationStatus; 74 75 /** @hide */ 76 @IntDef(prefix = "NETWORK_VALIDATION_", value = { 77 NETWORK_VALIDATION_UNSUPPORTED, 78 NETWORK_VALIDATION_NOT_REQUESTED, 79 NETWORK_VALIDATION_IN_PROGRESS, 80 NETWORK_VALIDATION_SUCCESS, 81 NETWORK_VALIDATION_FAILURE, 82 }) 83 @Retention(RetentionPolicy.SOURCE) 84 public @interface NetworkValidationStatus {} 85 86 /** 87 * Unsupported. The unsupported state is used when the data network cannot support the network 88 * validation function for the current data connection state. 89 */ 90 public static final int NETWORK_VALIDATION_UNSUPPORTED = 0; 91 92 /** 93 * Not Requested. The not requested status is used when the data network supports the network 94 * validation function, but no network validation is being performed yet. 95 */ 96 public static final int NETWORK_VALIDATION_NOT_REQUESTED = 1; 97 98 /** 99 * In progress. The in progress state is used when the network validation process for the data 100 * network is in progress. This state is followed by either success or failure. 101 */ 102 public static final int NETWORK_VALIDATION_IN_PROGRESS = 2; 103 104 /** 105 * Success. The Success status is used when network validation has been completed for the data 106 * network and the result is successful. 107 */ 108 public static final int NETWORK_VALIDATION_SUCCESS = 3; 109 110 /** 111 * Failure. The Failure status is used when network validation has been completed for the data 112 * network and the result is failure. 113 */ 114 public static final int NETWORK_VALIDATION_FAILURE = 4; 115 116 /** 117 * Constructor 118 * 119 * @deprecated this constructor has been superseded and should not be used. 120 * @hide 121 */ 122 @TestApi 123 @Deprecated 124 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) // (maxTargetSdk = Build.VERSION_CODES.Q) 125 // FIXME: figure out how to remove the UnsupportedAppUsage and delete this constructor PreciseDataConnectionState(@ataState int state, @NetworkType int networkType, @ApnType int apnTypes, @NonNull String apn, @Nullable LinkProperties linkProperties, @DataFailureCause int failCause)126 public PreciseDataConnectionState(@DataState int state, 127 @NetworkType int networkType, 128 @ApnType int apnTypes, @NonNull String apn, 129 @Nullable LinkProperties linkProperties, 130 @DataFailureCause int failCause) { 131 this(AccessNetworkConstants.TRANSPORT_TYPE_INVALID, -1, -1, state, networkType, 132 linkProperties, failCause, new ApnSetting.Builder() 133 .setApnTypeBitmask(apnTypes) 134 .setApnName(apn) 135 .setEntryName(apn) 136 .build(), null, NETWORK_VALIDATION_UNSUPPORTED); 137 } 138 139 140 /** 141 * Constructor of PreciseDataConnectionState 142 * 143 * @param transportType The transport of the data connection 144 * @param id The id of the data connection 145 * @param state The state of the data connection 146 * @param networkType The access network that is/would carry this data connection 147 * @param linkProperties If the data connection is connected, the properties of the connection 148 * @param failCause In case a procedure related to this data connection fails, a non-zero error 149 * code indicating the cause of the failure. 150 * @param apnSetting If there is a valid APN for this Data Connection, then the APN Settings; 151 * if there is no valid APN setting for the specific type, then this will be null 152 * @param defaultQos If there is a valid QoS for the default bearer supporting this data call, 153 * (supported for LTE and NR), then this is specified. Otherwise it should be null. 154 */ PreciseDataConnectionState(@ransportType int transportType, int id, int netId, @DataState int state, @NetworkType int networkType, @Nullable LinkProperties linkProperties, @DataFailureCause int failCause, @Nullable ApnSetting apnSetting, @Nullable Qos defaultQos, @NetworkValidationStatus int networkValidationStatus)155 private PreciseDataConnectionState(@TransportType int transportType, int id, int netId, 156 @DataState int state, @NetworkType int networkType, 157 @Nullable LinkProperties linkProperties, @DataFailureCause int failCause, 158 @Nullable ApnSetting apnSetting, @Nullable Qos defaultQos, 159 @NetworkValidationStatus int networkValidationStatus) { 160 mTransportType = transportType; 161 mId = id; 162 mNetId = netId; 163 mState = state; 164 mNetworkType = networkType; 165 mLinkProperties = linkProperties; 166 mFailCause = failCause; 167 mApnSetting = apnSetting; 168 mDefaultQos = defaultQos; 169 mNetworkValidationStatus = networkValidationStatus; 170 } 171 172 /** 173 * Construct a PreciseDataConnectionState object from the given parcel. 174 * 175 * @hide 176 */ PreciseDataConnectionState(Parcel in)177 private PreciseDataConnectionState(Parcel in) { 178 mTransportType = in.readInt(); 179 mId = in.readInt(); 180 mNetId = in.readInt(); 181 mState = in.readInt(); 182 mNetworkType = in.readInt(); 183 mLinkProperties = in.readParcelable( 184 LinkProperties.class.getClassLoader(), 185 android.net.LinkProperties.class); 186 mFailCause = in.readInt(); 187 mApnSetting = in.readParcelable( 188 ApnSetting.class.getClassLoader(), 189 android.telephony.data.ApnSetting.class); 190 mDefaultQos = in.readParcelable( 191 Qos.class.getClassLoader(), 192 android.telephony.data.Qos.class); 193 mNetworkValidationStatus = in.readInt(); 194 } 195 196 /** 197 * Used for checking if the SDK version for 198 * {@code PreciseDataConnectionState#getDataConnectionState} is above Q. 199 */ 200 @ChangeId 201 @EnabledAfter(targetSdkVersion = Build.VERSION_CODES.Q) 202 private static final long GET_DATA_CONNECTION_STATE_R_VERSION = 148535736L; 203 204 /** 205 * Returns the state of data connection that supported the apn types returned by 206 * {@link #getDataConnectionApnTypeBitMask()} 207 * 208 * @deprecated use {@link #getState()} 209 * @hide 210 */ 211 @Deprecated 212 @SystemApi getDataConnectionState()213 public @DataState int getDataConnectionState() { 214 if (mState == TelephonyManager.DATA_DISCONNECTING 215 && !Compatibility.isChangeEnabled(GET_DATA_CONNECTION_STATE_R_VERSION)) { 216 return TelephonyManager.DATA_CONNECTED; 217 } 218 219 return mState; 220 } 221 222 /** 223 * @return The transport type of this data connection. 224 */ getTransportType()225 public @TransportType int getTransportType() { 226 return mTransportType; 227 } 228 229 /** 230 * @return The unique id of the data connection 231 * 232 * Note this is the id assigned by the data service. 233 * The id remains the same for data connection handover between 234 * {@link AccessNetworkConstants#TRANSPORT_TYPE_WLAN} and 235 * {@link AccessNetworkConstants#TRANSPORT_TYPE_WWAN} 236 * 237 */ getId()238 public int getId() { 239 return mId; 240 } 241 242 /** 243 * @return the current TelephonyNetworkAgent ID. {@code -1} if no network agent. 244 * @hide 245 */ getNetId()246 public int getNetId() { 247 return mNetId; 248 } 249 250 /** 251 * @return The high-level state of this data connection. 252 */ getState()253 public @DataState int getState() { 254 return mState; 255 } 256 257 /** 258 * Get the network type associated with this data connection. 259 * 260 * @return The current/latest (radio) bearer technology that carries this data connection. 261 * For a variety of reasons, the network type can change during the life of the data 262 * connection, and this information is not reliable unless the physical link is currently 263 * active; (there is currently no mechanism to know whether the physical link is active at 264 * any given moment). Thus, this value is generally correct but may not be relied-upon to 265 * represent the status of the radio bearer at any given moment. 266 */ getNetworkType()267 public @NetworkType int getNetworkType() { 268 return mNetworkType; 269 } 270 271 /** 272 * Returns the APN types mapped to this data connection. 273 * 274 * @deprecated use {@link #getApnSetting()} 275 * @hide 276 */ 277 @Deprecated 278 @SystemApi getDataConnectionApnTypeBitMask()279 public @ApnType int getDataConnectionApnTypeBitMask() { 280 return (mApnSetting != null) ? mApnSetting.getApnTypeBitmask() : ApnSetting.TYPE_NONE; 281 } 282 283 /** 284 * Returns APN of this data connection. 285 * 286 * @deprecated use {@link #getApnSetting()} 287 * @hide 288 */ 289 @NonNull 290 @SystemApi 291 @Deprecated getDataConnectionApn()292 public String getDataConnectionApn() { 293 return (mApnSetting != null) ? mApnSetting.getApnName() : ""; 294 } 295 296 /** 297 * Get the properties of the network link {@link LinkProperties}. 298 */ 299 @Nullable getLinkProperties()300 public LinkProperties getLinkProperties() { 301 return mLinkProperties; 302 } 303 304 /** 305 * Returns the cause code generated by the most recent state change. 306 * 307 * @deprecated use {@link #getLastCauseCode()} 308 * @hide 309 */ 310 @Deprecated 311 @SystemApi getDataConnectionFailCause()312 public int getDataConnectionFailCause() { 313 return mFailCause; 314 } 315 316 /** 317 * Returns the cause code generated by the most recent state change. 318 * 319 * Return the cause code for the most recent change in {@link #getState}. In the event of an 320 * error, this cause code will be non-zero. 321 */ getLastCauseCode()322 public @DataFailureCause int getLastCauseCode() { 323 return mFailCause; 324 } 325 326 /** 327 * Return the APN Settings for this data connection. 328 * 329 * @return the ApnSetting that was used to configure this data connection. Note that a data 330 * connection cannot be established without a valid {@link ApnSetting}. The return value would 331 * never be {@code null} even though it has {@link Nullable} annotation. 332 */ getApnSetting()333 public @Nullable ApnSetting getApnSetting() { 334 return mApnSetting; 335 } 336 337 /** 338 * Return the QoS for the default bearer of this data connection. 339 * 340 * @return the default QoS if known or {@code null} if it is unknown. If the value is reported 341 * for LTE, then it will be an {@link android.telephony.data.EpsQos EpsQos}. If the value is 342 * reported for 5G, then it will be an {@link android.telehpony.data.NrQos NrQos}. Otherwise it 343 * shall always be {@code null}. 344 * 345 * @hide 346 */ getDefaultQos()347 public @Nullable Qos getDefaultQos() { 348 return mDefaultQos; 349 } 350 351 /** 352 * Returns the network validation state. 353 * 354 * @return the network validation status of the data call 355 */ getNetworkValidationStatus()356 public @NetworkValidationStatus int getNetworkValidationStatus() { 357 return mNetworkValidationStatus; 358 } 359 360 @Override describeContents()361 public int describeContents() { 362 return 0; 363 } 364 365 @Override writeToParcel(@onNull Parcel out, int flags)366 public void writeToParcel(@NonNull Parcel out, int flags) { 367 out.writeInt(mTransportType); 368 out.writeInt(mId); 369 out.writeInt(mNetId); 370 out.writeInt(mState); 371 out.writeInt(mNetworkType); 372 out.writeParcelable(mLinkProperties, flags); 373 out.writeInt(mFailCause); 374 out.writeParcelable(mApnSetting, flags); 375 out.writeParcelable(mDefaultQos, flags); 376 out.writeInt(mNetworkValidationStatus); 377 } 378 379 public static final @NonNull Parcelable.Creator<PreciseDataConnectionState> CREATOR 380 = new Parcelable.Creator<PreciseDataConnectionState>() { 381 382 public PreciseDataConnectionState createFromParcel(Parcel in) { 383 return new PreciseDataConnectionState(in); 384 } 385 386 public PreciseDataConnectionState[] newArray(int size) { 387 return new PreciseDataConnectionState[size]; 388 } 389 }; 390 391 @Override hashCode()392 public int hashCode() { 393 return Objects.hash(mTransportType, mId, mNetId, mState, mNetworkType, mFailCause, 394 mLinkProperties, mApnSetting, mDefaultQos, mNetworkValidationStatus); 395 } 396 397 398 @Override equals(Object o)399 public boolean equals(Object o) { 400 if (this == o) return true; 401 if (o == null || getClass() != o.getClass()) return false; 402 PreciseDataConnectionState that = (PreciseDataConnectionState) o; 403 return mTransportType == that.mTransportType 404 && mId == that.mId 405 && mNetId == that.mNetId 406 && mState == that.mState 407 && mNetworkType == that.mNetworkType 408 && mFailCause == that.mFailCause 409 && Objects.equals(mLinkProperties, that.mLinkProperties) 410 && Objects.equals(mApnSetting, that.mApnSetting) 411 && Objects.equals(mDefaultQos, that.mDefaultQos) 412 && mNetworkValidationStatus == that.mNetworkValidationStatus; 413 } 414 415 @NonNull 416 @Override toString()417 public String toString() { 418 StringBuilder sb = new StringBuilder(); 419 420 sb.append(" state: ").append(TelephonyUtils.dataStateToString(mState)); 421 sb.append(", transport: ").append( 422 AccessNetworkConstants.transportTypeToString(mTransportType)); 423 sb.append(", id: ").append(mId); 424 sb.append(", netId: ").append(mNetId); 425 sb.append(", network type: ").append(TelephonyManager.getNetworkTypeName(mNetworkType)); 426 sb.append(", APN Setting: ").append(mApnSetting); 427 sb.append(", link properties: ").append(mLinkProperties); 428 sb.append(", default QoS: ").append(mDefaultQos); 429 sb.append(", fail cause: ").append(DataFailCause.toString(mFailCause)); 430 sb.append(", network validation status: ").append( 431 networkValidationStatusToString(mNetworkValidationStatus)); 432 433 return sb.toString(); 434 } 435 436 /** 437 * Convert a network validation status to string. 438 * 439 * @param networkValidationStatus network validation status. 440 * @return string of validation status. 441 * 442 * @hide 443 */ 444 @NonNull networkValidationStatusToString( @etworkValidationStatus int networkValidationStatus)445 public static String networkValidationStatusToString( 446 @NetworkValidationStatus int networkValidationStatus) { 447 switch (networkValidationStatus) { 448 case NETWORK_VALIDATION_UNSUPPORTED: return "unsupported"; 449 case NETWORK_VALIDATION_NOT_REQUESTED: return "not requested"; 450 case NETWORK_VALIDATION_IN_PROGRESS: return "in progress"; 451 case NETWORK_VALIDATION_SUCCESS: return "success"; 452 case NETWORK_VALIDATION_FAILURE: return "failure"; 453 default: return Integer.toString(networkValidationStatus); 454 } 455 } 456 457 /** 458 * {@link PreciseDataConnectionState} builder 459 * 460 * @hide 461 */ 462 public static final class Builder { 463 /** The transport type of the data connection */ 464 private @TransportType int mTransportType = AccessNetworkConstants.TRANSPORT_TYPE_INVALID; 465 466 /** 467 * The unique ID of the data connection. This is the id assigned in 468 * {@link DataCallResponse)}. 469 */ 470 private int mId = -1; 471 472 /** 473 * The current TelephonyNetworkAgent ID. {@code -1} if no network agent. 474 */ 475 private int mNetworkAgentId = -1; 476 477 /** The state of the data connection */ 478 private @DataState int mState = TelephonyManager.DATA_UNKNOWN; 479 480 /** The network type associated with this data connection */ 481 private @NetworkType int mNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN; 482 483 /** If the data connection is connected, the properties of the connection */ 484 private @Nullable LinkProperties mLinkProperties; 485 486 /** 487 * In case a procedure related to this data connection fails, a non-zero error code 488 * indicating the cause of the failure. 489 */ 490 private @DataFailureCause int mFailCause = DataFailCause.NONE; 491 492 /** The APN Setting for this data connection */ 493 private @Nullable ApnSetting mApnSetting; 494 495 /** The Default QoS for this EPS/5GS bearer or null otherwise */ 496 private @Nullable Qos mDefaultQos; 497 498 /** The network validation status for the data connection. */ 499 private @NetworkValidationStatus int mNetworkValidationStatus = 500 NETWORK_VALIDATION_UNSUPPORTED; 501 502 /** 503 * Set the transport type of the data connection. 504 * 505 * @param transportType The transport type of the data connection 506 * @return The builder 507 */ setTransportType(@ransportType int transportType)508 public @NonNull Builder setTransportType(@TransportType int transportType) { 509 mTransportType = transportType; 510 return this; 511 } 512 513 /** 514 * Set the id of the data connection. 515 * 516 * @param id The id of the data connection 517 * @return The builder 518 */ setId(int id)519 public @NonNull Builder setId(int id) { 520 mId = id; 521 return this; 522 } 523 524 /** 525 * Set the id of the data connection. 526 * 527 * @param agentId The id of the data connection 528 * @return The builder 529 */ setNetworkAgentId(int agentId)530 public @NonNull Builder setNetworkAgentId(int agentId) { 531 mNetworkAgentId = agentId; 532 return this; 533 } 534 535 /** 536 * Set the state of the data connection. 537 * 538 * @param state The state of the data connection 539 * @return The builder 540 */ setState(@ataState int state)541 public @NonNull Builder setState(@DataState int state) { 542 mState = state; 543 return this; 544 } 545 546 /** 547 * Set the network type associated with this data connection. 548 * 549 * @param networkType The network type 550 * @return The builder 551 */ setNetworkType(@etworkType int networkType)552 public @NonNull Builder setNetworkType(@NetworkType int networkType) { 553 mNetworkType = networkType; 554 return this; 555 } 556 557 /** 558 * Set the link properties of the connection. 559 * 560 * @param linkProperties Link properties 561 * @return The builder 562 */ setLinkProperties(LinkProperties linkProperties)563 public @NonNull Builder setLinkProperties(LinkProperties linkProperties) { 564 mLinkProperties = linkProperties; 565 return this; 566 } 567 568 /** 569 * Set the fail cause of the data connection. 570 * 571 * @param failCause In case a procedure related to this data connection fails, a non-zero 572 * error code indicating the cause of the failure. 573 * @return The builder 574 */ setFailCause(@ataFailureCause int failCause)575 public @NonNull Builder setFailCause(@DataFailureCause int failCause) { 576 mFailCause = failCause; 577 return this; 578 } 579 580 /** 581 * Set the APN Setting for this data connection. 582 * 583 * @param apnSetting APN setting 584 * @return This builder 585 */ setApnSetting(@onNull ApnSetting apnSetting)586 public @NonNull Builder setApnSetting(@NonNull ApnSetting apnSetting) { 587 mApnSetting = apnSetting; 588 return this; 589 } 590 591 /** 592 * Set the default QoS for this data connection. 593 * 594 * @param qos The qos information, if any, associated with the default bearer of the 595 * data connection. 596 * @return The builder 597 * @hide 598 */ setDefaultQos(@ullable Qos qos)599 public @NonNull Builder setDefaultQos(@Nullable Qos qos) { 600 mDefaultQos = qos; 601 return this; 602 } 603 604 /** 605 * Set the network validation state for the data connection. 606 * 607 * @param networkValidationStatus the network validation status of the data call 608 * @return The builder 609 */ setNetworkValidationStatus( @etworkValidationStatus int networkValidationStatus)610 public @NonNull Builder setNetworkValidationStatus( 611 @NetworkValidationStatus int networkValidationStatus) { 612 mNetworkValidationStatus = networkValidationStatus; 613 return this; 614 } 615 616 /** 617 * Build the {@link PreciseDataConnectionState} instance. 618 * 619 * @return The {@link PreciseDataConnectionState} instance 620 */ build()621 public PreciseDataConnectionState build() { 622 return new PreciseDataConnectionState(mTransportType, mId, mNetworkAgentId, mState, 623 mNetworkType, mLinkProperties, mFailCause, mApnSetting, mDefaultQos, 624 mNetworkValidationStatus); 625 } 626 } 627 } 628