blob: 292cf50ac4ae0e42f8b4d45791309409a06c4a0f [file] [log] [blame]
Jeff Sharkeyd2a45872011-05-28 20:56:34 -07001/*
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
17package android.net;
18
Mathew Inwood53f089f2018-08-08 14:44:44 +010019import android.annotation.UnsupportedAppUsage;
Mathew Inwood8c854f82018-09-14 12:35:36 +010020import android.os.Build;
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070021import android.os.Parcel;
22import android.os.Parcelable;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070023import android.util.Slog;
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070024
25/**
26 * Snapshot of network state.
27 *
28 * @hide
29 */
30public class NetworkState implements Parcelable {
Jeff Sharkey2b3f6d52018-04-09 16:15:03 -060031 private static final boolean SANITY_CHECK_ROAMING = false;
32
Jeff Sharkeyf07c7b92016-04-22 09:50:16 -060033 public static final NetworkState EMPTY = new NetworkState(null, null, null, null, null, null);
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070034
35 public final NetworkInfo networkInfo;
36 public final LinkProperties linkProperties;
Robert Greenwaltf9cb86a2014-04-08 17:34:00 -070037 public final NetworkCapabilities networkCapabilities;
Mathew Inwood8c854f82018-09-14 12:35:36 +010038 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -080039 public final Network network;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070040 public final String subscriberId;
Jeff Sharkeye8914c32012-05-01 16:26:09 -070041 public final String networkId;
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070042
43 public NetworkState(NetworkInfo networkInfo, LinkProperties linkProperties,
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -080044 NetworkCapabilities networkCapabilities, Network network, String subscriberId,
45 String networkId) {
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070046 this.networkInfo = networkInfo;
47 this.linkProperties = linkProperties;
Robert Greenwaltf9cb86a2014-04-08 17:34:00 -070048 this.networkCapabilities = networkCapabilities;
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -080049 this.network = network;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070050 this.subscriberId = subscriberId;
Jeff Sharkeye8914c32012-05-01 16:26:09 -070051 this.networkId = networkId;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070052
53 // This object is an atomic view of a network, so the various components
54 // should always agree on roaming state.
Jeff Sharkey2b3f6d52018-04-09 16:15:03 -060055 if (SANITY_CHECK_ROAMING && networkInfo != null && networkCapabilities != null) {
Jeff Sharkey49bcd602017-11-09 13:11:50 -070056 if (networkInfo.isRoaming() == networkCapabilities
57 .hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING)) {
58 Slog.wtf("NetworkState", "Roaming state disagreement between " + networkInfo
59 + " and " + networkCapabilities);
60 }
61 }
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070062 }
63
Mathew Inwood53f089f2018-08-08 14:44:44 +010064 @UnsupportedAppUsage
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070065 public NetworkState(Parcel in) {
66 networkInfo = in.readParcelable(null);
67 linkProperties = in.readParcelable(null);
Robert Greenwaltf9cb86a2014-04-08 17:34:00 -070068 networkCapabilities = in.readParcelable(null);
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -080069 network = in.readParcelable(null);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070070 subscriberId = in.readString();
Jeff Sharkeye8914c32012-05-01 16:26:09 -070071 networkId = in.readString();
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070072 }
73
Jeff Sharkeybfdd6802012-04-09 10:49:19 -070074 @Override
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070075 public int describeContents() {
76 return 0;
77 }
78
Jeff Sharkeybfdd6802012-04-09 10:49:19 -070079 @Override
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070080 public void writeToParcel(Parcel out, int flags) {
81 out.writeParcelable(networkInfo, flags);
82 out.writeParcelable(linkProperties, flags);
Robert Greenwaltf9cb86a2014-04-08 17:34:00 -070083 out.writeParcelable(networkCapabilities, flags);
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -080084 out.writeParcelable(network, flags);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070085 out.writeString(subscriberId);
Jeff Sharkeye8914c32012-05-01 16:26:09 -070086 out.writeString(networkId);
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070087 }
88
Mathew Inwood53f089f2018-08-08 14:44:44 +010089 @UnsupportedAppUsage
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070090 public static final @android.annotation.NonNull Creator<NetworkState> CREATOR = new Creator<NetworkState>() {
Jeff Sharkeybfdd6802012-04-09 10:49:19 -070091 @Override
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070092 public NetworkState createFromParcel(Parcel in) {
93 return new NetworkState(in);
94 }
95
Jeff Sharkeybfdd6802012-04-09 10:49:19 -070096 @Override
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070097 public NetworkState[] newArray(int size) {
98 return new NetworkState[size];
99 }
100 };
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700101}