blob: b682bdd7aee3388a833cde96d1bac3a2963b6f3a [file] [log] [blame]
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +02001/*
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
17package android.telephony;
18
chen xu81653862019-02-28 10:44:54 -080019import android.annotation.NonNull;
20import android.annotation.Nullable;
chen xu896e3d42018-12-11 18:09:41 -080021import android.annotation.SystemApi;
Nathan Harold4d340f12019-12-05 17:27:56 -080022import android.annotation.TestApi;
Shuo Qiand0c81812020-01-13 16:01:46 -080023import android.compat.Compatibility;
24import android.compat.annotation.ChangeId;
25import android.compat.annotation.EnabledAfter;
Artur Satayev74cb7192019-12-10 17:47:56 +000026import android.compat.annotation.UnsupportedAppUsage;
Jack Yuf05919a2019-01-02 16:51:08 -080027import android.net.LinkProperties;
Nathan Harold4d340f12019-12-05 17:27:56 -080028import android.os.Build;
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +020029import android.os.Parcel;
30import android.os.Parcelable;
Chen Xuc9d4ee12019-09-26 16:11:59 -070031import android.telephony.Annotation.ApnType;
32import android.telephony.Annotation.DataFailureCause;
33import android.telephony.Annotation.DataState;
34import android.telephony.Annotation.NetworkType;
chen xu896e3d42018-12-11 18:09:41 -080035import android.telephony.data.ApnSetting;
Jack Yuf05919a2019-01-02 16:51:08 -080036
37import java.util.Objects;
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +020038
chen xu81653862019-02-28 10:44:54 -080039
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +020040/**
41 * Contains precise data connection state.
42 *
43 * The following data connection information is included in returned PreciseDataConnectionState:
44 *
45 * <ul>
46 * <li>Data connection state.
47 * <li>Network type of the connection.
chen xu896e3d42018-12-11 18:09:41 -080048 * <li>APN types.
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +020049 * <li>APN.
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +020050 * <li>The properties of the network link.
51 * <li>Data connection fail cause.
52 * </ul>
53 *
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +020054 */
chen xu896e3d42018-12-11 18:09:41 -080055public final class PreciseDataConnectionState implements Parcelable {
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +020056
Chen Xuc9d4ee12019-09-26 16:11:59 -070057 private @DataState int mState = TelephonyManager.DATA_UNKNOWN;
58 private @NetworkType int mNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
59 private @DataFailureCause int mFailCause = DataFailCause.NONE;
Nathan Harold4d340f12019-12-05 17:27:56 -080060 private @ApnType int mApnTypes = ApnSetting.TYPE_NONE;
61 private String mApn = "";
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +020062 private LinkProperties mLinkProperties = null;
Nathan Harold4d340f12019-12-05 17:27:56 -080063 private ApnSetting mApnSetting = null;
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +020064
65 /**
66 * Constructor
67 *
Nathan Harold4d340f12019-12-05 17:27:56 -080068 * @deprecated this constructor has been superseded and should not be used.
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +020069 * @hide
70 */
Nathan Harold4d340f12019-12-05 17:27:56 -080071 @TestApi
72 @Deprecated
73 @UnsupportedAppUsage // (maxTargetSdk = Build.VERSION_CODES.Q)
74 // FIXME: figure out how to remove the UnsupportedAppUsage and delete this constructor
Chen Xuc9d4ee12019-09-26 16:11:59 -070075 public PreciseDataConnectionState(@DataState int state,
76 @NetworkType int networkType,
Nathan Harold4d340f12019-12-05 17:27:56 -080077 @ApnType int apnTypes, @NonNull String apn,
78 @Nullable LinkProperties linkProperties,
Chen Xuc9d4ee12019-09-26 16:11:59 -070079 @DataFailureCause int failCause) {
Nathan Harold4d340f12019-12-05 17:27:56 -080080 this(state, networkType, apnTypes, apn, linkProperties, failCause, null);
81 }
82
83
84 /**
Shuo Qianac4c8da2019-12-23 11:41:44 -080085 * Constructor of PreciseDataConnectionState
Nathan Harold4d340f12019-12-05 17:27:56 -080086 *
87 * @param state the state of the data connection
88 * @param networkType the access network that is/would carry this data connection
89 * @param apnTypes the APN types that this data connection carries
Shuo Qianac4c8da2019-12-23 11:41:44 -080090 * @param apn the APN of this data connection
Nathan Harold4d340f12019-12-05 17:27:56 -080091 * @param linkProperties if the data connection is connected, the properties of the connection
92 * @param failCause in case a procedure related to this data connection fails, a non-zero error
93 * code indicating the cause of the failure.
Shuo Qianac4c8da2019-12-23 11:41:44 -080094 * @param apnSetting if there is a valid APN for this Data Connection, then the APN Settings;
95 * if there is no valid APN setting for the specific type, then this will be null
Nathan Harold4d340f12019-12-05 17:27:56 -080096 * @hide
97 */
98 public PreciseDataConnectionState(@DataState int state,
99 @NetworkType int networkType,
100 @ApnType int apnTypes, @NonNull String apn,
101 @Nullable LinkProperties linkProperties,
102 @DataFailureCause int failCause,
103 @Nullable ApnSetting apnSetting) {
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200104 mState = state;
105 mNetworkType = networkType;
Nathan Harold4d340f12019-12-05 17:27:56 -0800106 mApnTypes = apnTypes;
107 mApn = apn;
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200108 mLinkProperties = linkProperties;
109 mFailCause = failCause;
Nathan Harold4d340f12019-12-05 17:27:56 -0800110 mApnSetting = apnSetting;
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200111 }
112
113 /**
114 * Empty Constructor
115 *
116 * @hide
117 */
118 public PreciseDataConnectionState() {
119 }
120
121 /**
122 * Construct a PreciseDataConnectionState object from the given parcel.
chen xu896e3d42018-12-11 18:09:41 -0800123 *
124 * @hide
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200125 */
126 private PreciseDataConnectionState(Parcel in) {
127 mState = in.readInt();
128 mNetworkType = in.readInt();
Nathan Harold4d340f12019-12-05 17:27:56 -0800129 mApnTypes = in.readInt();
130 mApn = in.readString();
131 mLinkProperties = (LinkProperties) in.readParcelable(null);
chen xu896e3d42018-12-11 18:09:41 -0800132 mFailCause = in.readInt();
Nathan Harold4d340f12019-12-05 17:27:56 -0800133 mApnSetting = (ApnSetting) in.readParcelable(null);
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200134 }
135
136 /**
Shuo Qianb32797b2020-01-29 10:31:35 -0800137 * Used for checking if the SDK version for
138 * {@code PreciseDataConnectionState#getDataConnectionState} is above Q.
Shuo Qiand0c81812020-01-13 16:01:46 -0800139 */
140 @ChangeId
Shuo Qiana78143c2020-01-14 19:16:31 -0800141 @EnabledAfter(targetSdkVersion = Build.VERSION_CODES.Q)
Shuo Qianb32797b2020-01-29 10:31:35 -0800142 private static final long GET_DATA_CONNECTION_STATE_R_VERSION = 148535736L;
Shuo Qiand0c81812020-01-13 16:01:46 -0800143
144 /**
chen xu896e3d42018-12-11 18:09:41 -0800145 * Returns the state of data connection that supported the apn types returned by
146 * {@link #getDataConnectionApnTypeBitMask()}
Nathan Harold4d340f12019-12-05 17:27:56 -0800147 *
148 * @deprecated use {@link #getState()}
149 * @hide
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200150 */
Nathan Harold4d340f12019-12-05 17:27:56 -0800151 @Deprecated
152 @SystemApi
Chen Xuc9d4ee12019-09-26 16:11:59 -0700153 public @DataState int getDataConnectionState() {
Nathan Harold4d340f12019-12-05 17:27:56 -0800154 if (mState == TelephonyManager.DATA_DISCONNECTING
Shuo Qianb32797b2020-01-29 10:31:35 -0800155 && !Compatibility.isChangeEnabled(GET_DATA_CONNECTION_STATE_R_VERSION)) {
Nathan Harold4d340f12019-12-05 17:27:56 -0800156 return TelephonyManager.DATA_CONNECTED;
157 }
158
159 return mState;
160 }
161
162 /**
163 * Returns the high-level state of this data connection.
164 */
165 public @DataState int getState() {
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200166 return mState;
167 }
168
169 /**
chen xu896e3d42018-12-11 18:09:41 -0800170 * Returns the network type associated with this data connection.
Nathan Harold4d340f12019-12-05 17:27:56 -0800171 *
172 * @deprecated use {@link getNetworkType()}
chen xu896e3d42018-12-11 18:09:41 -0800173 * @hide
Michael Wrighteec778c2020-03-30 21:25:02 +0100174 * @removed Removed from the R preview SDK but was never part of the stable API surface.
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200175 */
Nathan Harold4d340f12019-12-05 17:27:56 -0800176 @Deprecated
177 @SystemApi
Chen Xuc9d4ee12019-09-26 16:11:59 -0700178 public @NetworkType int getDataConnectionNetworkType() {
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200179 return mNetworkType;
180 }
181
182 /**
Nathan Harold4d340f12019-12-05 17:27:56 -0800183 * Returns the network type associated with this data connection.
184 *
185 * Return the current/latest (radio) bearer technology that carries this data connection.
186 * For a variety of reasons, the network type can change during the life of the data
187 * connection, and this information is not reliable unless the physical link is currently
188 * active; (there is currently no mechanism to know whether the physical link is active at
189 * any given moment). Thus, this value is generally correct but may not be relied-upon to
190 * represent the status of the radio bearer at any given moment.
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200191 */
Nathan Harold4d340f12019-12-05 17:27:56 -0800192 public @NetworkType int getNetworkType() {
193 return mNetworkType;
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200194 }
195
196 /**
Nathan Harold4d340f12019-12-05 17:27:56 -0800197 * Returns the APN types mapped to this data connection.
198 *
199 * @deprecated use {@link #getApnSetting()}
200 * @hide
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200201 */
Nathan Harold4d340f12019-12-05 17:27:56 -0800202 @Deprecated
203 @SystemApi
204 public @ApnType int getDataConnectionApnTypeBitMask() {
205 return mApnTypes;
206 }
207
208 /**
209 * Returns APN of this data connection.
210 *
211 * @deprecated use {@link #getApnSetting()}
212 * @hide
213 */
214 @NonNull
215 @SystemApi
216 @Deprecated
chen xu896e3d42018-12-11 18:09:41 -0800217 public String getDataConnectionApn() {
Nathan Harold4d340f12019-12-05 17:27:56 -0800218 return mApn;
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200219 }
220
221 /**
chen xu896e3d42018-12-11 18:09:41 -0800222 * Get the properties of the network link {@link LinkProperties}.
Nathan Harold4d340f12019-12-05 17:27:56 -0800223 *
224 * @deprecated use {@link #getLinkProperties()}
chen xu896e3d42018-12-11 18:09:41 -0800225 * @hide
Michael Wrighteec778c2020-03-30 21:25:02 +0100226 * @removed Removed from the R preview SDK but was never part of the stable API surface.
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200227 */
Nathan Harold4d340f12019-12-05 17:27:56 -0800228 @Deprecated
229 @SystemApi
230 @Nullable
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200231 public LinkProperties getDataConnectionLinkProperties() {
232 return mLinkProperties;
233 }
234
235 /**
Nathan Harold4d340f12019-12-05 17:27:56 -0800236 * Get the properties of the network link {@link LinkProperties}.
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200237 */
Nathan Harold4d340f12019-12-05 17:27:56 -0800238 @Nullable
239 public LinkProperties getLinkProperties() {
240 return mLinkProperties;
241 }
242
243 /**
244 * Returns the cause code generated by the most recent state change.
245 *
246 * @deprecated use {@link #getLastCauseCode()}
247 * @hide
248 */
249 @Deprecated
250 @SystemApi
251 public int getDataConnectionFailCause() {
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200252 return mFailCause;
253 }
254
Nathan Harold4d340f12019-12-05 17:27:56 -0800255 /**
256 * Returns the cause code generated by the most recent state change.
257 *
258 * Return the cause code for the most recent change in {@link #getState}. In the event of an
259 * error, this cause code will be non-zero.
260 */
Rambo Wang8a54f492020-02-12 23:49:41 -0800261 public @DataFailureCause int getLastCauseCode() {
Nathan Harold4d340f12019-12-05 17:27:56 -0800262 return mFailCause;
263 }
264
265 /**
266 * Return the APN Settings for this data connection.
267 *
Nathan Harolda61992b2020-01-29 16:17:22 -0800268 * @return the ApnSetting that was used to configure this data connection.
Nathan Harold4d340f12019-12-05 17:27:56 -0800269 */
Nathan Harolda61992b2020-01-29 16:17:22 -0800270 public @Nullable ApnSetting getApnSetting() {
Nathan Harold4d340f12019-12-05 17:27:56 -0800271 return mApnSetting;
272 }
273
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200274 @Override
275 public int describeContents() {
276 return 0;
277 }
278
279 @Override
Nathan Harold4d340f12019-12-05 17:27:56 -0800280 public void writeToParcel(@NonNull Parcel out, int flags) {
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200281 out.writeInt(mState);
282 out.writeInt(mNetworkType);
Nathan Harold4d340f12019-12-05 17:27:56 -0800283 out.writeInt(mApnTypes);
284 out.writeString(mApn);
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200285 out.writeParcelable(mLinkProperties, flags);
chen xu896e3d42018-12-11 18:09:41 -0800286 out.writeInt(mFailCause);
Nathan Harold4d340f12019-12-05 17:27:56 -0800287 out.writeParcelable(mApnSetting, flags);
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200288 }
289
Nathan Harold4d340f12019-12-05 17:27:56 -0800290 public static final @NonNull Parcelable.Creator<PreciseDataConnectionState> CREATOR
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200291 = new Parcelable.Creator<PreciseDataConnectionState>() {
292
293 public PreciseDataConnectionState createFromParcel(Parcel in) {
294 return new PreciseDataConnectionState(in);
295 }
296
297 public PreciseDataConnectionState[] newArray(int size) {
298 return new PreciseDataConnectionState[size];
299 }
300 };
301
302 @Override
303 public int hashCode() {
Nathan Harold4d340f12019-12-05 17:27:56 -0800304 return Objects.hash(mState, mNetworkType, mApnTypes, mApn, mLinkProperties,
305 mFailCause, mApnSetting);
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200306 }
307
308 @Override
Aurimas Liutikas4d1699d2019-08-28 13:01:05 -0700309 public boolean equals(@Nullable Object obj) {
chen xu896e3d42018-12-11 18:09:41 -0800310
311 if (!(obj instanceof PreciseDataConnectionState)) {
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200312 return false;
313 }
chen xu896e3d42018-12-11 18:09:41 -0800314
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200315 PreciseDataConnectionState other = (PreciseDataConnectionState) obj;
Nathan Harold4d340f12019-12-05 17:27:56 -0800316 return Objects.equals(mApn, other.mApn) && mApnTypes == other.mApnTypes
chen xu896e3d42018-12-11 18:09:41 -0800317 && mFailCause == other.mFailCause
318 && Objects.equals(mLinkProperties, other.mLinkProperties)
319 && mNetworkType == other.mNetworkType
Nathan Harold4d340f12019-12-05 17:27:56 -0800320 && mState == other.mState
321 && Objects.equals(mApnSetting, other.mApnSetting);
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200322 }
323
Aurimas Liutikas4d1699d2019-08-28 13:01:05 -0700324 @NonNull
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200325 @Override
326 public String toString() {
327 StringBuilder sb = new StringBuilder();
328
329 sb.append("Data Connection state: " + mState);
330 sb.append(", Network type: " + mNetworkType);
Nathan Harold4d340f12019-12-05 17:27:56 -0800331 sb.append(", APN types: " + ApnSetting.getApnTypesStringFromBitmask(mApnTypes));
332 sb.append(", APN: " + mApn);
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200333 sb.append(", Link properties: " + mLinkProperties);
chen xu896e3d42018-12-11 18:09:41 -0800334 sb.append(", Fail cause: " + DataFailCause.toString(mFailCause));
Nathan Harold4d340f12019-12-05 17:27:56 -0800335 sb.append(", Apn Setting: " + mApnSetting);
Antonio Marín Cerezuelac5ac15a2013-05-27 11:36:36 +0200336
337 return sb.toString();
338 }
339}