NetworkState.java revision 2b3f6d5c4c43d1a523ffdc73e0a435ea09140893
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 17package android.net; 18 19import android.os.Parcel; 20import android.os.Parcelable; 21import android.util.Slog; 22 23/** 24 * Snapshot of network state. 25 * 26 * @hide 27 */ 28public class NetworkState implements Parcelable { 29 private static final boolean SANITY_CHECK_ROAMING = false; 30 31 public static final NetworkState EMPTY = new NetworkState(null, null, null, null, null, null); 32 33 public final NetworkInfo networkInfo; 34 public final LinkProperties linkProperties; 35 public final NetworkCapabilities networkCapabilities; 36 public final Network network; 37 public final String subscriberId; 38 public final String networkId; 39 40 public NetworkState(NetworkInfo networkInfo, LinkProperties linkProperties, 41 NetworkCapabilities networkCapabilities, Network network, String subscriberId, 42 String networkId) { 43 this.networkInfo = networkInfo; 44 this.linkProperties = linkProperties; 45 this.networkCapabilities = networkCapabilities; 46 this.network = network; 47 this.subscriberId = subscriberId; 48 this.networkId = networkId; 49 50 // This object is an atomic view of a network, so the various components 51 // should always agree on roaming state. 52 if (SANITY_CHECK_ROAMING && networkInfo != null && networkCapabilities != null) { 53 if (networkInfo.isRoaming() == networkCapabilities 54 .hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING)) { 55 Slog.wtf("NetworkState", "Roaming state disagreement between " + networkInfo 56 + " and " + networkCapabilities); 57 } 58 } 59 } 60 61 public NetworkState(Parcel in) { 62 networkInfo = in.readParcelable(null); 63 linkProperties = in.readParcelable(null); 64 networkCapabilities = in.readParcelable(null); 65 network = in.readParcelable(null); 66 subscriberId = in.readString(); 67 networkId = in.readString(); 68 } 69 70 @Override 71 public int describeContents() { 72 return 0; 73 } 74 75 @Override 76 public void writeToParcel(Parcel out, int flags) { 77 out.writeParcelable(networkInfo, flags); 78 out.writeParcelable(linkProperties, flags); 79 out.writeParcelable(networkCapabilities, flags); 80 out.writeParcelable(network, flags); 81 out.writeString(subscriberId); 82 out.writeString(networkId); 83 } 84 85 public static final Creator<NetworkState> CREATOR = new Creator<NetworkState>() { 86 @Override 87 public NetworkState createFromParcel(Parcel in) { 88 return new NetworkState(in); 89 } 90 91 @Override 92 public NetworkState[] newArray(int size) { 93 return new NetworkState[size]; 94 } 95 }; 96} 97