• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.annotation.TestApi;
23 import android.compat.Compatibility;
24 import android.compat.annotation.ChangeId;
25 import android.compat.annotation.EnabledAfter;
26 import android.compat.annotation.UnsupportedAppUsage;
27 import android.net.LinkProperties;
28 import android.os.Build;
29 import android.os.Parcel;
30 import android.os.Parcelable;
31 import android.telephony.AccessNetworkConstants.TransportType;
32 import android.telephony.Annotation.ApnType;
33 import android.telephony.Annotation.DataFailureCause;
34 import android.telephony.Annotation.DataState;
35 import android.telephony.Annotation.NetworkType;
36 import android.telephony.data.ApnSetting;
37 import android.telephony.data.DataCallResponse;
38 
39 import com.android.internal.telephony.util.TelephonyUtils;
40 
41 import java.util.Objects;
42 
43 
44 /**
45  * Contains precise data connection state.
46  *
47  * The following data connection information is included in returned PreciseDataConnectionState:
48  *
49  * <ul>
50  *   <li>Data connection state.
51  *   <li>Network type of the connection.
52  *   <li>APN types.
53  *   <li>APN.
54  *   <li>The properties of the network link.
55  *   <li>Data connection fail cause.
56  * </ul>
57  *
58  */
59 public final class PreciseDataConnectionState implements Parcelable {
60     private final @TransportType int mTransportType;
61     private final int mId;
62     private final @DataState int mState;
63     private final @NetworkType int mNetworkType;
64     private final @DataFailureCause int mFailCause;
65     private final LinkProperties mLinkProperties;
66     private final ApnSetting mApnSetting;
67 
68     /**
69      * Constructor
70      *
71      * @deprecated this constructor has been superseded and should not be used.
72      * @hide
73      */
74     @TestApi
75     @Deprecated
76     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) // (maxTargetSdk = Build.VERSION_CODES.Q)
77     // 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)78     public PreciseDataConnectionState(@DataState int state,
79                                       @NetworkType int networkType,
80                                       @ApnType int apnTypes, @NonNull String apn,
81                                       @Nullable LinkProperties linkProperties,
82                                       @DataFailureCause int failCause) {
83         this(AccessNetworkConstants.TRANSPORT_TYPE_INVALID, -1, state, networkType,
84                 linkProperties, failCause, new ApnSetting.Builder()
85                         .setApnTypeBitmask(apnTypes)
86                         .setApnName(apn)
87                         .setEntryName(apn)
88                         .build());
89     }
90 
91 
92     /**
93      * Constructor of PreciseDataConnectionState
94      *
95      * @param transportType The transport of the data connection
96      * @param id The id of the data connection
97      * @param state The state of the data connection
98      * @param networkType The access network that is/would carry this data connection
99      * @param linkProperties If the data connection is connected, the properties of the connection
100      * @param failCause In case a procedure related to this data connection fails, a non-zero error
101      *        code indicating the cause of the failure.
102      * @param apnSetting If there is a valid APN for this Data Connection, then the APN Settings;
103      *        if there is no valid APN setting for the specific type, then this will be null
104      */
PreciseDataConnectionState(@ransportType int transportType, int id, @DataState int state, @NetworkType int networkType, @Nullable LinkProperties linkProperties, @DataFailureCause int failCause, @Nullable ApnSetting apnSetting)105     private PreciseDataConnectionState(@TransportType int transportType, int id,
106             @DataState int state, @NetworkType int networkType,
107             @Nullable LinkProperties linkProperties, @DataFailureCause int failCause,
108             @Nullable ApnSetting apnSetting) {
109         mTransportType = transportType;
110         mId = id;
111         mState = state;
112         mNetworkType = networkType;
113         mLinkProperties = linkProperties;
114         mFailCause = failCause;
115         mApnSetting = apnSetting;
116     }
117 
118     /**
119      * Construct a PreciseDataConnectionState object from the given parcel.
120      *
121      * @hide
122      */
PreciseDataConnectionState(Parcel in)123     private PreciseDataConnectionState(Parcel in) {
124         mTransportType = in.readInt();
125         mId = in.readInt();
126         mState = in.readInt();
127         mNetworkType = in.readInt();
128         mLinkProperties = in.readParcelable(LinkProperties.class.getClassLoader());
129         mFailCause = in.readInt();
130         mApnSetting = in.readParcelable(ApnSetting.class.getClassLoader());
131     }
132 
133     /**
134      * Used for checking if the SDK version for
135      * {@code PreciseDataConnectionState#getDataConnectionState} is above Q.
136      */
137     @ChangeId
138     @EnabledAfter(targetSdkVersion = Build.VERSION_CODES.Q)
139     private static final long GET_DATA_CONNECTION_STATE_R_VERSION = 148535736L;
140 
141     /**
142      * Returns the state of data connection that supported the apn types returned by
143      * {@link #getDataConnectionApnTypeBitMask()}
144      *
145      * @deprecated use {@link #getState()}
146      * @hide
147      */
148     @Deprecated
149     @SystemApi
getDataConnectionState()150     public @DataState int getDataConnectionState() {
151         if (mState == TelephonyManager.DATA_DISCONNECTING
152                 && !Compatibility.isChangeEnabled(GET_DATA_CONNECTION_STATE_R_VERSION)) {
153             return TelephonyManager.DATA_CONNECTED;
154         }
155 
156         return mState;
157     }
158 
159     /**
160      * @return The transport type of this data connection.
161      */
getTransportType()162     public @TransportType int getTransportType() {
163         return mTransportType;
164     }
165 
166     /**
167      * @return The unique id of the data connection
168      *
169      * Note this is the id assigned by the data service.
170      * The id remains the same for data connection handover between
171      * {@link AccessNetworkConstants#TRANSPORT_TYPE_WLAN} and
172      * {@link AccessNetworkConstants#TRANSPORT_TYPE_WWAN}
173      *
174      */
getId()175     public int getId() {
176         return mId;
177     }
178 
179     /**
180      * @return The high-level state of this data connection.
181      */
getState()182     public @DataState int getState() {
183         return mState;
184     }
185 
186     /**
187      * Get the network type associated with this data connection.
188      *
189      * @return The current/latest (radio) bearer technology that carries this data connection.
190      * For a variety of reasons, the network type can change during the life of the data
191      * connection, and this information is not reliable unless the physical link is currently
192      * active; (there is currently no mechanism to know whether the physical link is active at
193      * any given moment). Thus, this value is generally correct but may not be relied-upon to
194      * represent the status of the radio bearer at any given moment.
195      */
getNetworkType()196     public @NetworkType int getNetworkType() {
197         return mNetworkType;
198     }
199 
200     /**
201      * Returns the APN types mapped to this data connection.
202      *
203      * @deprecated use {@link #getApnSetting()}
204      * @hide
205      */
206     @Deprecated
207     @SystemApi
getDataConnectionApnTypeBitMask()208     public @ApnType int getDataConnectionApnTypeBitMask() {
209         return (mApnSetting != null) ? mApnSetting.getApnTypeBitmask() : ApnSetting.TYPE_NONE;
210     }
211 
212     /**
213      * Returns APN of this data connection.
214      *
215      * @deprecated use {@link #getApnSetting()}
216      * @hide
217      */
218     @NonNull
219     @SystemApi
220     @Deprecated
getDataConnectionApn()221     public String getDataConnectionApn() {
222         return (mApnSetting != null) ? mApnSetting.getApnName() : "";
223     }
224 
225     /**
226      * Get the properties of the network link {@link LinkProperties}.
227      */
228     @Nullable
getLinkProperties()229     public LinkProperties getLinkProperties() {
230         return mLinkProperties;
231     }
232 
233     /**
234      * Returns the cause code generated by the most recent state change.
235      *
236      * @deprecated use {@link #getLastCauseCode()}
237      * @hide
238      */
239     @Deprecated
240     @SystemApi
getDataConnectionFailCause()241     public int getDataConnectionFailCause() {
242         return mFailCause;
243     }
244 
245     /**
246      * Returns the cause code generated by the most recent state change.
247      *
248      * Return the cause code for the most recent change in {@link #getState}. In the event of an
249      * error, this cause code will be non-zero.
250      */
getLastCauseCode()251     public @DataFailureCause int getLastCauseCode() {
252         return mFailCause;
253     }
254 
255     /**
256      * Return the APN Settings for this data connection.
257      *
258      * @return the ApnSetting that was used to configure this data connection. Note that a data
259      * connection cannot be established without a valid {@link ApnSetting}. The return value would
260      * never be {@code null} even though it has {@link Nullable} annotation.
261      */
getApnSetting()262     public @Nullable ApnSetting getApnSetting() {
263         return mApnSetting;
264     }
265 
266     @Override
describeContents()267     public int describeContents() {
268         return 0;
269     }
270 
271     @Override
writeToParcel(@onNull Parcel out, int flags)272     public void writeToParcel(@NonNull Parcel out, int flags) {
273         out.writeInt(mTransportType);
274         out.writeInt(mId);
275         out.writeInt(mState);
276         out.writeInt(mNetworkType);
277         out.writeParcelable(mLinkProperties, flags);
278         out.writeInt(mFailCause);
279         out.writeParcelable(mApnSetting, flags);
280     }
281 
282     public static final @NonNull Parcelable.Creator<PreciseDataConnectionState> CREATOR
283             = new Parcelable.Creator<PreciseDataConnectionState>() {
284 
285         public PreciseDataConnectionState createFromParcel(Parcel in) {
286             return new PreciseDataConnectionState(in);
287         }
288 
289         public PreciseDataConnectionState[] newArray(int size) {
290             return new PreciseDataConnectionState[size];
291         }
292     };
293 
294     @Override
hashCode()295     public int hashCode() {
296         return Objects.hash(mTransportType, mId, mState, mNetworkType, mFailCause,
297                 mLinkProperties, mApnSetting);
298     }
299 
300 
301     @Override
equals(Object o)302     public boolean equals(Object o) {
303         if (this == o) return true;
304         if (o == null || getClass() != o.getClass()) return false;
305         PreciseDataConnectionState that = (PreciseDataConnectionState) o;
306         return mTransportType == that.mTransportType
307                 && mId == that.mId
308                 && mState == that.mState
309                 && mNetworkType == that.mNetworkType
310                 && mFailCause == that.mFailCause
311                 && Objects.equals(mLinkProperties, that.mLinkProperties)
312                 && Objects.equals(mApnSetting, that.mApnSetting);
313     }
314 
315     @NonNull
316     @Override
toString()317     public String toString() {
318         StringBuilder sb = new StringBuilder();
319 
320         sb.append(" state: " + TelephonyUtils.dataStateToString(mState));
321         sb.append(", transport: "
322                 + AccessNetworkConstants.transportTypeToString(mTransportType));
323         sb.append(", id: " + mId);
324         sb.append(", network type: " + TelephonyManager.getNetworkTypeName(mNetworkType));
325         sb.append(", APN Setting: " + mApnSetting);
326         sb.append(", link properties: " + mLinkProperties);
327         sb.append(", fail cause: " + DataFailCause.toString(mFailCause));
328 
329         return sb.toString();
330     }
331 
332     /**
333      * {@link PreciseDataConnectionState} builder
334      *
335      * @hide
336      */
337     public static final class Builder {
338         /** The transport type of the data connection */
339         private @TransportType int mTransportType = AccessNetworkConstants.TRANSPORT_TYPE_INVALID;
340 
341         /**
342          * The unique ID of the data connection. This is the id assigned in
343          * {@link DataCallResponse)}.
344          */
345         private int mId = -1;
346 
347         /** The state of the data connection */
348         private @DataState int mState = TelephonyManager.DATA_UNKNOWN;
349 
350         /** The network type associated with this data connection */
351         private @NetworkType int mNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
352 
353         /** If the data connection is connected, the properties of the connection */
354         private @Nullable LinkProperties mLinkProperties = null;
355 
356         /**
357          * In case a procedure related to this data connection fails, a non-zero error code
358          * indicating the cause of the failure.
359          */
360         private @DataFailureCause int mFailCause = DataFailCause.NONE;
361 
362         /** The APN Setting for this data connection */
363         private @Nullable ApnSetting mApnSetting = null;
364 
365         /**
366          * Set the transport type of the data connection.
367          *
368          * @param transportType The transport type of the data connection
369          * @return The builder
370          */
setTransportType(@ransportType int transportType)371         public @NonNull Builder setTransportType(@TransportType int transportType) {
372             mTransportType = transportType;
373             return this;
374         }
375 
376         /**
377          * Set the id of the data connection.
378          *
379          * @param id The id of the data connection
380          * @return The builder
381          */
setId(int id)382         public @NonNull Builder setId(int id) {
383             mId = id;
384             return this;
385         }
386 
387         /**
388          * Set the state of the data connection.
389          *
390          * @param state The state of the data connection
391          * @return The builder
392          */
setState(@ataState int state)393         public @NonNull Builder setState(@DataState int state) {
394             mState = state;
395             return this;
396         }
397 
398         /**
399          * Set the network type associated with this data connection.
400          *
401          * @param networkType The network type
402          * @return The builder
403          */
setNetworkType(@etworkType int networkType)404         public @NonNull Builder setNetworkType(@NetworkType int networkType) {
405             mNetworkType = networkType;
406             return this;
407         }
408 
409         /**
410          * Set the link properties of the connection.
411          *
412          * @param linkProperties Link properties
413          * @return The builder
414          */
setLinkProperties(LinkProperties linkProperties)415         public @NonNull Builder setLinkProperties(LinkProperties linkProperties) {
416             mLinkProperties = linkProperties;
417             return this;
418         }
419 
420         /**
421          * Set the fail cause of the data connection.
422          *
423          * @param failCause In case a procedure related to this data connection fails, a non-zero
424          * error code indicating the cause of the failure.
425          * @return The builder
426          */
setFailCause(@ataFailureCause int failCause)427         public @NonNull Builder setFailCause(@DataFailureCause int failCause) {
428             mFailCause = failCause;
429             return this;
430         }
431 
432         /**
433          * Set the APN Setting for this data connection.
434          *
435          * @param apnSetting APN setting
436          * @return This builder
437          */
setApnSetting(@onNull ApnSetting apnSetting)438         public @NonNull Builder setApnSetting(@NonNull ApnSetting apnSetting) {
439             mApnSetting = apnSetting;
440             return this;
441         }
442 
443         /**
444          * Build the {@link PreciseDataConnectionState} instance.
445          *
446          * @return The {@link PreciseDataConnectionState} instance
447          */
build()448         public PreciseDataConnectionState build() {
449             return new PreciseDataConnectionState(mTransportType, mId, mState, mNetworkType,
450                     mLinkProperties, mFailCause, mApnSetting);
451         }
452     }
453 }
454