1 /*
2  * Copyright (C) 2008 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.net;
18 
19 import android.annotation.NonNull;
20 import android.annotation.UnsupportedAppUsage;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import com.android.internal.annotations.VisibleForTesting;
25 
26 import java.util.EnumMap;
27 
28 /**
29  * Describes the status of a network interface.
30  * <p>Use {@link ConnectivityManager#getActiveNetworkInfo()} to get an instance that represents
31  * the current network connection.
32  *
33  * @deprecated Callers should instead use the {@link ConnectivityManager.NetworkCallback} API to
34  *             learn about connectivity changes, or switch to use
35  *             {@link ConnectivityManager#getNetworkCapabilities} or
36  *             {@link ConnectivityManager#getLinkProperties} to get information synchronously. Keep
37  *             in mind that while callbacks are guaranteed to be called for every event in order,
38  *             synchronous calls have no such constraints, and as such it is unadvisable to use the
39  *             synchronous methods inside the callbacks as they will often not offer a view of
40  *             networking that is consistent (that is: they may return a past or a future state with
41  *             respect to the event being processed by the callback). Instead, callers are advised
42  *             to only use the arguments of the callbacks, possibly memorizing the specific bits of
43  *             information they need to keep from one callback to another.
44  */
45 @Deprecated
46 public class NetworkInfo implements Parcelable {
47 
48     /**
49      * Coarse-grained network state. This is probably what most applications should
50      * use, rather than {@link android.net.NetworkInfo.DetailedState DetailedState}.
51      * The mapping between the two is as follows:
52      * <br/><br/>
53      * <table>
54      * <tr><td><b>Detailed state</b></td><td><b>Coarse-grained state</b></td></tr>
55      * <tr><td><code>IDLE</code></td><td><code>DISCONNECTED</code></td></tr>
56      * <tr><td><code>SCANNING</code></td><td><code>DISCONNECTED</code></td></tr>
57      * <tr><td><code>CONNECTING</code></td><td><code>CONNECTING</code></td></tr>
58      * <tr><td><code>AUTHENTICATING</code></td><td><code>CONNECTING</code></td></tr>
59      * <tr><td><code>OBTAINING_IPADDR</code></td><td><code>CONNECTING</code></td></tr>
60      * <tr><td><code>VERIFYING_POOR_LINK</code></td><td><code>CONNECTING</code></td></tr>
61      * <tr><td><code>CAPTIVE_PORTAL_CHECK</code></td><td><code>CONNECTING</code></td></tr>
62      * <tr><td><code>CONNECTED</code></td><td><code>CONNECTED</code></td></tr>
63      * <tr><td><code>SUSPENDED</code></td><td><code>SUSPENDED</code></td></tr>
64      * <tr><td><code>DISCONNECTING</code></td><td><code>DISCONNECTING</code></td></tr>
65      * <tr><td><code>DISCONNECTED</code></td><td><code>DISCONNECTED</code></td></tr>
66      * <tr><td><code>FAILED</code></td><td><code>DISCONNECTED</code></td></tr>
67      * <tr><td><code>BLOCKED</code></td><td><code>DISCONNECTED</code></td></tr>
68      * </table>
69      *
70      * @deprecated See {@link NetworkInfo}.
71      */
72     @Deprecated
73     public enum State {
74         CONNECTING, CONNECTED, SUSPENDED, DISCONNECTING, DISCONNECTED, UNKNOWN
75     }
76 
77     /**
78      * The fine-grained state of a network connection. This level of detail
79      * is probably of interest to few applications. Most should use
80      * {@link android.net.NetworkInfo.State State} instead.
81      *
82      * @deprecated See {@link NetworkInfo}.
83      */
84     @Deprecated
85     public enum DetailedState {
86         /** Ready to start data connection setup. */
87         IDLE,
88         /** Searching for an available access point. */
89         SCANNING,
90         /** Currently setting up data connection. */
91         CONNECTING,
92         /** Network link established, performing authentication. */
93         AUTHENTICATING,
94         /** Awaiting response from DHCP server in order to assign IP address information. */
95         OBTAINING_IPADDR,
96         /** IP traffic should be available. */
97         CONNECTED,
98         /** IP traffic is suspended */
99         SUSPENDED,
100         /** Currently tearing down data connection. */
101         DISCONNECTING,
102         /** IP traffic not available. */
103         DISCONNECTED,
104         /** Attempt to connect failed. */
105         FAILED,
106         /** Access to this network is blocked. */
107         BLOCKED,
108         /** Link has poor connectivity. */
109         VERIFYING_POOR_LINK,
110         /** Checking if network is a captive portal */
111         CAPTIVE_PORTAL_CHECK
112     }
113 
114     /**
115      * This is the map described in the Javadoc comment above. The positions
116      * of the elements of the array must correspond to the ordinal values
117      * of <code>DetailedState</code>.
118      */
119     private static final EnumMap<DetailedState, State> stateMap =
120         new EnumMap<DetailedState, State>(DetailedState.class);
121 
122     static {
stateMap.put(DetailedState.IDLE, State.DISCONNECTED)123         stateMap.put(DetailedState.IDLE, State.DISCONNECTED);
stateMap.put(DetailedState.SCANNING, State.DISCONNECTED)124         stateMap.put(DetailedState.SCANNING, State.DISCONNECTED);
stateMap.put(DetailedState.CONNECTING, State.CONNECTING)125         stateMap.put(DetailedState.CONNECTING, State.CONNECTING);
stateMap.put(DetailedState.AUTHENTICATING, State.CONNECTING)126         stateMap.put(DetailedState.AUTHENTICATING, State.CONNECTING);
stateMap.put(DetailedState.OBTAINING_IPADDR, State.CONNECTING)127         stateMap.put(DetailedState.OBTAINING_IPADDR, State.CONNECTING);
stateMap.put(DetailedState.VERIFYING_POOR_LINK, State.CONNECTING)128         stateMap.put(DetailedState.VERIFYING_POOR_LINK, State.CONNECTING);
stateMap.put(DetailedState.CAPTIVE_PORTAL_CHECK, State.CONNECTING)129         stateMap.put(DetailedState.CAPTIVE_PORTAL_CHECK, State.CONNECTING);
stateMap.put(DetailedState.CONNECTED, State.CONNECTED)130         stateMap.put(DetailedState.CONNECTED, State.CONNECTED);
stateMap.put(DetailedState.SUSPENDED, State.SUSPENDED)131         stateMap.put(DetailedState.SUSPENDED, State.SUSPENDED);
stateMap.put(DetailedState.DISCONNECTING, State.DISCONNECTING)132         stateMap.put(DetailedState.DISCONNECTING, State.DISCONNECTING);
stateMap.put(DetailedState.DISCONNECTED, State.DISCONNECTED)133         stateMap.put(DetailedState.DISCONNECTED, State.DISCONNECTED);
stateMap.put(DetailedState.FAILED, State.DISCONNECTED)134         stateMap.put(DetailedState.FAILED, State.DISCONNECTED);
stateMap.put(DetailedState.BLOCKED, State.DISCONNECTED)135         stateMap.put(DetailedState.BLOCKED, State.DISCONNECTED);
136     }
137 
138     private int mNetworkType;
139     private int mSubtype;
140     private String mTypeName;
141     private String mSubtypeName;
142     @NonNull
143     private State mState;
144     @NonNull
145     private DetailedState mDetailedState;
146     private String mReason;
147     private String mExtraInfo;
148     private boolean mIsFailover;
149     private boolean mIsAvailable;
150     private boolean mIsRoaming;
151 
152     /**
153      * @hide
154      */
155     @UnsupportedAppUsage
NetworkInfo(int type, int subtype, String typeName, String subtypeName)156     public NetworkInfo(int type, int subtype, String typeName, String subtypeName) {
157         if (!ConnectivityManager.isNetworkTypeValid(type)
158                 && type != ConnectivityManager.TYPE_NONE) {
159             throw new IllegalArgumentException("Invalid network type: " + type);
160         }
161         mNetworkType = type;
162         mSubtype = subtype;
163         mTypeName = typeName;
164         mSubtypeName = subtypeName;
165         setDetailedState(DetailedState.IDLE, null, null);
166         mState = State.UNKNOWN;
167     }
168 
169     /** {@hide} */
170     @UnsupportedAppUsage
NetworkInfo(NetworkInfo source)171     public NetworkInfo(NetworkInfo source) {
172         if (source != null) {
173             synchronized (source) {
174                 mNetworkType = source.mNetworkType;
175                 mSubtype = source.mSubtype;
176                 mTypeName = source.mTypeName;
177                 mSubtypeName = source.mSubtypeName;
178                 mState = source.mState;
179                 mDetailedState = source.mDetailedState;
180                 mReason = source.mReason;
181                 mExtraInfo = source.mExtraInfo;
182                 mIsFailover = source.mIsFailover;
183                 mIsAvailable = source.mIsAvailable;
184                 mIsRoaming = source.mIsRoaming;
185             }
186         }
187     }
188 
189     /**
190      * Reports the type of network to which the
191      * info in this {@code NetworkInfo} pertains.
192      * @return one of {@link ConnectivityManager#TYPE_MOBILE}, {@link
193      * ConnectivityManager#TYPE_WIFI}, {@link ConnectivityManager#TYPE_WIMAX}, {@link
194      * ConnectivityManager#TYPE_ETHERNET},  {@link ConnectivityManager#TYPE_BLUETOOTH}, or other
195      * types defined by {@link ConnectivityManager}.
196      * @deprecated Callers should switch to checking {@link NetworkCapabilities#hasTransport}
197      *             instead with one of the NetworkCapabilities#TRANSPORT_* constants :
198      *             {@link #getType} and {@link #getTypeName} cannot account for networks using
199      *             multiple transports. Note that generally apps should not care about transport;
200      *             {@link NetworkCapabilities#NET_CAPABILITY_NOT_METERED} and
201      *             {@link NetworkCapabilities#getLinkDownstreamBandwidthKbps} are calls that
202      *             apps concerned with meteredness or bandwidth should be looking at, as they
203      *             offer this information with much better accuracy.
204      */
205     @Deprecated
getType()206     public int getType() {
207         synchronized (this) {
208             return mNetworkType;
209         }
210     }
211 
212     /**
213      * @deprecated Use {@link NetworkCapabilities} instead
214      * @hide
215      */
216     @Deprecated
setType(int type)217     public void setType(int type) {
218         synchronized (this) {
219             mNetworkType = type;
220         }
221     }
222 
223     /**
224      * Return a network-type-specific integer describing the subtype
225      * of the network.
226      * @return the network subtype
227      * @deprecated Use {@link android.telephony.TelephonyManager#getDataNetworkType} instead.
228      */
229     @Deprecated
getSubtype()230     public int getSubtype() {
231         synchronized (this) {
232             return mSubtype;
233         }
234     }
235 
236     /**
237      * @hide
238      */
239     @UnsupportedAppUsage
setSubtype(int subtype, String subtypeName)240     public void setSubtype(int subtype, String subtypeName) {
241         synchronized (this) {
242             mSubtype = subtype;
243             mSubtypeName = subtypeName;
244         }
245     }
246 
247     /**
248      * Return a human-readable name describe the type of the network,
249      * for example "WIFI" or "MOBILE".
250      * @return the name of the network type
251      * @deprecated Callers should switch to checking {@link NetworkCapabilities#hasTransport}
252      *             instead with one of the NetworkCapabilities#TRANSPORT_* constants :
253      *             {@link #getType} and {@link #getTypeName} cannot account for networks using
254      *             multiple transports. Note that generally apps should not care about transport;
255      *             {@link NetworkCapabilities#NET_CAPABILITY_NOT_METERED} and
256      *             {@link NetworkCapabilities#getLinkDownstreamBandwidthKbps} are calls that
257      *             apps concerned with meteredness or bandwidth should be looking at, as they
258      *             offer this information with much better accuracy.
259      */
260     @Deprecated
getTypeName()261     public String getTypeName() {
262         synchronized (this) {
263             return mTypeName;
264         }
265     }
266 
267     /**
268      * Return a human-readable name describing the subtype of the network.
269      * @return the name of the network subtype
270      * @deprecated Use {@link android.telephony.TelephonyManager#getDataNetworkType} instead.
271      */
272     @Deprecated
getSubtypeName()273     public String getSubtypeName() {
274         synchronized (this) {
275             return mSubtypeName;
276         }
277     }
278 
279     /**
280      * Indicates whether network connectivity exists or is in the process
281      * of being established. This is good for applications that need to
282      * do anything related to the network other than read or write data.
283      * For the latter, call {@link #isConnected()} instead, which guarantees
284      * that the network is fully usable.
285      * @return {@code true} if network connectivity exists or is in the process
286      * of being established, {@code false} otherwise.
287      * @deprecated Apps should instead use the
288      *             {@link android.net.ConnectivityManager.NetworkCallback} API to
289      *             learn about connectivity changes.
290      *             {@link ConnectivityManager#registerDefaultNetworkCallback} and
291      *             {@link ConnectivityManager#registerNetworkCallback}. These will
292      *             give a more accurate picture of the connectivity state of
293      *             the device and let apps react more easily and quickly to changes.
294      */
295     @Deprecated
isConnectedOrConnecting()296     public boolean isConnectedOrConnecting() {
297         synchronized (this) {
298             return mState == State.CONNECTED || mState == State.CONNECTING;
299         }
300     }
301 
302     /**
303      * Indicates whether network connectivity exists and it is possible to establish
304      * connections and pass data.
305      * <p>Always call this before attempting to perform data transactions.
306      * @return {@code true} if network connectivity exists, {@code false} otherwise.
307      * @deprecated Apps should instead use the
308      *             {@link android.net.ConnectivityManager.NetworkCallback} API to
309      *             learn about connectivity changes. See
310      *             {@link ConnectivityManager#registerDefaultNetworkCallback} and
311      *             {@link ConnectivityManager#registerNetworkCallback}. These will
312      *             give a more accurate picture of the connectivity state of
313      *             the device and let apps react more easily and quickly to changes.
314      */
315     @Deprecated
isConnected()316     public boolean isConnected() {
317         synchronized (this) {
318             return mState == State.CONNECTED;
319         }
320     }
321 
322     /**
323      * Indicates whether network connectivity is possible. A network is unavailable
324      * when a persistent or semi-persistent condition prevents the possibility
325      * of connecting to that network. Examples include
326      * <ul>
327      * <li>The device is out of the coverage area for any network of this type.</li>
328      * <li>The device is on a network other than the home network (i.e., roaming), and
329      * data roaming has been disabled.</li>
330      * <li>The device's radio is turned off, e.g., because airplane mode is enabled.</li>
331      * </ul>
332      * Since Android L, this always returns {@code true}, because the system only
333      * returns info for available networks.
334      * @return {@code true} if the network is available, {@code false} otherwise
335      * @deprecated Apps should instead use the
336      *             {@link android.net.ConnectivityManager.NetworkCallback} API to
337      *             learn about connectivity changes.
338      *             {@link ConnectivityManager#registerDefaultNetworkCallback} and
339      *             {@link ConnectivityManager#registerNetworkCallback}. These will
340      *             give a more accurate picture of the connectivity state of
341      *             the device and let apps react more easily and quickly to changes.
342      */
343     @Deprecated
isAvailable()344     public boolean isAvailable() {
345         synchronized (this) {
346             return mIsAvailable;
347         }
348     }
349 
350     /**
351      * Sets if the network is available, ie, if the connectivity is possible.
352      * @param isAvailable the new availability value.
353      * @deprecated Use {@link NetworkCapabilities} instead
354      *
355      * @hide
356      */
357     @Deprecated
358     @UnsupportedAppUsage
setIsAvailable(boolean isAvailable)359     public void setIsAvailable(boolean isAvailable) {
360         synchronized (this) {
361             mIsAvailable = isAvailable;
362         }
363     }
364 
365     /**
366      * Indicates whether the current attempt to connect to the network
367      * resulted from the ConnectivityManager trying to fail over to this
368      * network following a disconnect from another network.
369      * @return {@code true} if this is a failover attempt, {@code false}
370      * otherwise.
371      * @deprecated This field is not populated in recent Android releases,
372      *             and does not make a lot of sense in a multi-network world.
373      */
374     @Deprecated
isFailover()375     public boolean isFailover() {
376         synchronized (this) {
377             return mIsFailover;
378         }
379     }
380 
381     /**
382      * Set the failover boolean.
383      * @param isFailover {@code true} to mark the current connection attempt
384      * as a failover.
385      * @deprecated This hasn't been set in any recent Android release.
386      * @hide
387      */
388     @Deprecated
389     @UnsupportedAppUsage
setFailover(boolean isFailover)390     public void setFailover(boolean isFailover) {
391         synchronized (this) {
392             mIsFailover = isFailover;
393         }
394     }
395 
396     /**
397      * Indicates whether the device is currently roaming on this network. When
398      * {@code true}, it suggests that use of data on this network may incur
399      * extra costs.
400      *
401      * @return {@code true} if roaming is in effect, {@code false} otherwise.
402      * @deprecated Callers should switch to checking
403      *             {@link NetworkCapabilities#NET_CAPABILITY_NOT_ROAMING}
404      *             instead, since that handles more complex situations, such as
405      *             VPNs.
406      */
407     @Deprecated
isRoaming()408     public boolean isRoaming() {
409         synchronized (this) {
410             return mIsRoaming;
411         }
412     }
413 
414     /**
415      * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_NOT_ROAMING} instead.
416      * {@hide}
417      */
418     @VisibleForTesting
419     @Deprecated
420     @UnsupportedAppUsage
setRoaming(boolean isRoaming)421     public void setRoaming(boolean isRoaming) {
422         synchronized (this) {
423             mIsRoaming = isRoaming;
424         }
425     }
426 
427     /**
428      * Reports the current coarse-grained state of the network.
429      * @return the coarse-grained state
430      * @deprecated Apps should instead use the
431      *             {@link android.net.ConnectivityManager.NetworkCallback} API to
432      *             learn about connectivity changes.
433      *             {@link ConnectivityManager#registerDefaultNetworkCallback} and
434      *             {@link ConnectivityManager#registerNetworkCallback}. These will
435      *             give a more accurate picture of the connectivity state of
436      *             the device and let apps react more easily and quickly to changes.
437      */
438     @Deprecated
getState()439     public State getState() {
440         synchronized (this) {
441             return mState;
442         }
443     }
444 
445     /**
446      * Reports the current fine-grained state of the network.
447      * @return the fine-grained state
448      * @deprecated Apps should instead use the
449      *             {@link android.net.ConnectivityManager.NetworkCallback} API to
450      *             learn about connectivity changes. See
451      *             {@link ConnectivityManager#registerDefaultNetworkCallback} and
452      *             {@link ConnectivityManager#registerNetworkCallback}. These will
453      *             give a more accurate picture of the connectivity state of
454      *             the device and let apps react more easily and quickly to changes.
455      */
456     @Deprecated
getDetailedState()457     public @NonNull DetailedState getDetailedState() {
458         synchronized (this) {
459             return mDetailedState;
460         }
461     }
462 
463     /**
464      * Sets the fine-grained state of the network.
465      * @param detailedState the {@link DetailedState}.
466      * @param reason a {@code String} indicating the reason for the state change,
467      * if one was supplied. May be {@code null}.
468      * @param extraInfo an optional {@code String} providing addditional network state
469      * information passed up from the lower networking layers.
470      * @deprecated Use {@link NetworkCapabilities} instead.
471      * @hide
472      */
473     @Deprecated
474     @UnsupportedAppUsage
setDetailedState(DetailedState detailedState, String reason, String extraInfo)475     public void setDetailedState(DetailedState detailedState, String reason, String extraInfo) {
476         synchronized (this) {
477             this.mDetailedState = detailedState;
478             this.mState = stateMap.get(detailedState);
479             this.mReason = reason;
480             this.mExtraInfo = extraInfo;
481         }
482     }
483 
484     /**
485      * Set the extraInfo field.
486      * @param extraInfo an optional {@code String} providing addditional network state
487      * information passed up from the lower networking layers.
488      * @deprecated See {@link NetworkInfo#getExtraInfo}.
489      * @hide
490      */
491     @Deprecated
setExtraInfo(String extraInfo)492     public void setExtraInfo(String extraInfo) {
493         synchronized (this) {
494             this.mExtraInfo = extraInfo;
495         }
496     }
497 
498     /**
499      * Report the reason an attempt to establish connectivity failed,
500      * if one is available.
501      * @return the reason for failure, or null if not available
502      * @deprecated This method does not have a consistent contract that could make it useful
503      *             to callers.
504      */
getReason()505     public String getReason() {
506         synchronized (this) {
507             return mReason;
508         }
509     }
510 
511     /**
512      * Report the extra information about the network state, if any was
513      * provided by the lower networking layers.
514      * @return the extra information, or null if not available
515      * @deprecated Use other services e.g. WifiManager to get additional information passed up from
516      *             the lower networking layers.
517      */
518     @Deprecated
getExtraInfo()519     public String getExtraInfo() {
520         synchronized (this) {
521             return mExtraInfo;
522         }
523     }
524 
525     @Override
toString()526     public String toString() {
527         synchronized (this) {
528             StringBuilder builder = new StringBuilder("[");
529             builder.append("type: ").append(getTypeName()).append("[").append(getSubtypeName()).
530             append("], state: ").append(mState).append("/").append(mDetailedState).
531             append(", reason: ").append(mReason == null ? "(unspecified)" : mReason).
532             append(", extra: ").append(mExtraInfo == null ? "(none)" : mExtraInfo).
533             append(", failover: ").append(mIsFailover).
534             append(", available: ").append(mIsAvailable).
535             append(", roaming: ").append(mIsRoaming).
536             append("]");
537             return builder.toString();
538         }
539     }
540 
541     @Override
describeContents()542     public int describeContents() {
543         return 0;
544     }
545 
546     @Override
writeToParcel(Parcel dest, int flags)547     public void writeToParcel(Parcel dest, int flags) {
548         synchronized (this) {
549             dest.writeInt(mNetworkType);
550             dest.writeInt(mSubtype);
551             dest.writeString(mTypeName);
552             dest.writeString(mSubtypeName);
553             dest.writeString(mState.name());
554             dest.writeString(mDetailedState.name());
555             dest.writeInt(mIsFailover ? 1 : 0);
556             dest.writeInt(mIsAvailable ? 1 : 0);
557             dest.writeInt(mIsRoaming ? 1 : 0);
558             dest.writeString(mReason);
559             dest.writeString(mExtraInfo);
560         }
561     }
562 
563     public static final @android.annotation.NonNull Creator<NetworkInfo> CREATOR = new Creator<NetworkInfo>() {
564         @Override
565         public NetworkInfo createFromParcel(Parcel in) {
566             int netType = in.readInt();
567             int subtype = in.readInt();
568             String typeName = in.readString();
569             String subtypeName = in.readString();
570             NetworkInfo netInfo = new NetworkInfo(netType, subtype, typeName, subtypeName);
571             netInfo.mState = State.valueOf(in.readString());
572             netInfo.mDetailedState = DetailedState.valueOf(in.readString());
573             netInfo.mIsFailover = in.readInt() != 0;
574             netInfo.mIsAvailable = in.readInt() != 0;
575             netInfo.mIsRoaming = in.readInt() != 0;
576             netInfo.mReason = in.readString();
577             netInfo.mExtraInfo = in.readString();
578             return netInfo;
579         }
580 
581         @Override
582         public NetworkInfo[] newArray(int size) {
583             return new NetworkInfo[size];
584         }
585     };
586 }
587