1 /*
2  * Copyright (C) 2011 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.Nullable;
21 import android.compat.annotation.UnsupportedAppUsage;
22 import android.os.Build;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.util.Log;
26 
27 /**
28  * Snapshot of network state.
29  *
30  * @hide
31  */
32 public class NetworkState implements Parcelable {
33     private static final boolean VALIDATE_ROAMING_STATE = false;
34 
35     // TODO: remove and make members @NonNull.
36     public static final NetworkState EMPTY = new NetworkState();
37 
38     public final NetworkInfo networkInfo;
39     public final LinkProperties linkProperties;
40     public final NetworkCapabilities networkCapabilities;
41     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
42     public final Network network;
43     public final String subscriberId;
44     public final int legacyNetworkType;
45 
46     private NetworkState() {
47         networkInfo = null;
48         linkProperties = null;
49         networkCapabilities = null;
50         network = null;
51         subscriberId = null;
52         legacyNetworkType = 0;
53     }
54 
55     public NetworkState(int legacyNetworkType, @NonNull LinkProperties linkProperties,
56             @NonNull NetworkCapabilities networkCapabilities, @NonNull Network network,
57             @Nullable String subscriberId) {
58         this(legacyNetworkType, new NetworkInfo(legacyNetworkType, 0, null, null), linkProperties,
59                 networkCapabilities, network, subscriberId);
60     }
61 
62     // Constructor that used internally in ConnectivityService mainline module.
63     public NetworkState(@NonNull NetworkInfo networkInfo, @NonNull LinkProperties linkProperties,
64             @NonNull NetworkCapabilities networkCapabilities, @NonNull Network network,
65             @Nullable String subscriberId) {
66         this(networkInfo.getType(), networkInfo, linkProperties,
67                 networkCapabilities, network, subscriberId);
68     }
69 
70     public NetworkState(int legacyNetworkType, @NonNull NetworkInfo networkInfo,
71             @NonNull LinkProperties linkProperties,
72             @NonNull NetworkCapabilities networkCapabilities, @NonNull Network network,
73             @Nullable String subscriberId) {
74         this.networkInfo = networkInfo;
75         this.linkProperties = linkProperties;
76         this.networkCapabilities = networkCapabilities;
77         this.network = network;
78         this.subscriberId = subscriberId;
79         this.legacyNetworkType = legacyNetworkType;
80 
81         // This object is an atomic view of a network, so the various components
82         // should always agree on roaming state.
83         if (VALIDATE_ROAMING_STATE && networkInfo != null && networkCapabilities != null) {
84             if (networkInfo.isRoaming() == networkCapabilities
85                     .hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING)) {
86                 Log.wtf("NetworkState", "Roaming state disagreement between " + networkInfo
87                         + " and " + networkCapabilities);
88             }
89         }
90     }
91 
92     @UnsupportedAppUsage
93     public NetworkState(Parcel in) {
94         networkInfo = in.readParcelable(null);
95         linkProperties = in.readParcelable(null);
96         networkCapabilities = in.readParcelable(null);
97         network = in.readParcelable(null);
98         subscriberId = in.readString();
99         legacyNetworkType = in.readInt();
100     }
101 
102     @Override
103     public int describeContents() {
104         return 0;
105     }
106 
107     @Override
108     public void writeToParcel(Parcel out, int flags) {
109         out.writeParcelable(networkInfo, flags);
110         out.writeParcelable(linkProperties, flags);
111         out.writeParcelable(networkCapabilities, flags);
112         out.writeParcelable(network, flags);
113         out.writeString(subscriberId);
114         out.writeInt(legacyNetworkType);
115     }
116 
117     @UnsupportedAppUsage
118     @NonNull
119     public static final Creator<NetworkState> CREATOR = new Creator<NetworkState>() {
120         @Override
121         public NetworkState createFromParcel(Parcel in) {
122             return new NetworkState(in);
123         }
124 
125         @Override
126         public NetworkState[] newArray(int size) {
127             return new NetworkState[size];
128         }
129     };
130 }
131