1 /* 2 * Copyright (C) 2021 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 static android.annotation.SystemApi.Client.MODULE_LIBRARIES; 20 import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; 21 import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; 22 23 import android.annotation.NonNull; 24 import android.annotation.Nullable; 25 import android.annotation.SystemApi; 26 import android.os.Parcel; 27 import android.os.Parcelable; 28 29 import com.android.net.module.util.NetworkIdentityUtils; 30 31 import java.util.Objects; 32 33 /** 34 * Snapshot of network state. 35 * 36 * @hide 37 */ 38 @SystemApi(client = MODULE_LIBRARIES) 39 public final class NetworkStateSnapshot implements Parcelable { 40 /** The network associated with this snapshot. */ 41 @NonNull 42 private final Network mNetwork; 43 44 /** The {@link NetworkCapabilities} of the network associated with this snapshot. */ 45 @NonNull 46 private final NetworkCapabilities mNetworkCapabilities; 47 48 /** The {@link LinkProperties} of the network associated with this snapshot. */ 49 @NonNull 50 private final LinkProperties mLinkProperties; 51 52 /** 53 * The Subscriber Id of the network associated with this snapshot. See 54 * {@link android.telephony.TelephonyManager#getSubscriberId()}. 55 */ 56 @Nullable 57 private final String mSubscriberId; 58 59 /** 60 * The legacy type of the network associated with this snapshot. See 61 * {@code ConnectivityManager#TYPE_*}. 62 */ 63 private final int mLegacyType; 64 NetworkStateSnapshot(@onNull Network network, @NonNull NetworkCapabilities networkCapabilities, @NonNull LinkProperties linkProperties, @Nullable String subscriberId, int legacyType)65 public NetworkStateSnapshot(@NonNull Network network, 66 @NonNull NetworkCapabilities networkCapabilities, 67 @NonNull LinkProperties linkProperties, 68 @Nullable String subscriberId, int legacyType) { 69 mNetwork = Objects.requireNonNull(network); 70 mNetworkCapabilities = Objects.requireNonNull(networkCapabilities); 71 mLinkProperties = Objects.requireNonNull(linkProperties); 72 mSubscriberId = subscriberId; 73 mLegacyType = legacyType; 74 } 75 76 /** @hide */ NetworkStateSnapshot(@onNull Parcel in)77 public NetworkStateSnapshot(@NonNull Parcel in) { 78 mNetwork = in.readParcelable(null, android.net.Network.class); 79 mNetworkCapabilities = in.readParcelable(null, android.net.NetworkCapabilities.class); 80 mLinkProperties = in.readParcelable(null, android.net.LinkProperties.class); 81 mSubscriberId = in.readString(); 82 mLegacyType = in.readInt(); 83 } 84 85 /** Get the network associated with this snapshot */ 86 @NonNull getNetwork()87 public Network getNetwork() { 88 return mNetwork; 89 } 90 91 /** Get {@link NetworkCapabilities} of the network associated with this snapshot. */ 92 @NonNull getNetworkCapabilities()93 public NetworkCapabilities getNetworkCapabilities() { 94 return mNetworkCapabilities; 95 } 96 97 /** Get the {@link LinkProperties} of the network associated with this snapshot. */ 98 @NonNull getLinkProperties()99 public LinkProperties getLinkProperties() { 100 return mLinkProperties; 101 } 102 103 /** 104 * Get the Subscriber Id of the network associated with this snapshot. 105 * @deprecated Please use #getSubId, which doesn't return personally identifiable 106 * information. 107 */ 108 @Deprecated 109 @Nullable getSubscriberId()110 public String getSubscriberId() { 111 return mSubscriberId; 112 } 113 114 /** Get the subId of the network associated with this snapshot. */ getSubId()115 public int getSubId() { 116 if (mNetworkCapabilities.hasTransport(TRANSPORT_CELLULAR)) { 117 final NetworkSpecifier spec = mNetworkCapabilities.getNetworkSpecifier(); 118 if (spec instanceof TelephonyNetworkSpecifier) { 119 return ((TelephonyNetworkSpecifier) spec).getSubscriptionId(); 120 } 121 } 122 return INVALID_SUBSCRIPTION_ID; 123 } 124 125 126 /** 127 * Get the legacy type of the network associated with this snapshot. 128 * @return the legacy network type. See {@code ConnectivityManager#TYPE_*}. 129 */ getLegacyType()130 public int getLegacyType() { 131 return mLegacyType; 132 } 133 134 @Override describeContents()135 public int describeContents() { 136 return 0; 137 } 138 139 @Override writeToParcel(@onNull Parcel out, int flags)140 public void writeToParcel(@NonNull Parcel out, int flags) { 141 out.writeParcelable(mNetwork, flags); 142 out.writeParcelable(mNetworkCapabilities, flags); 143 out.writeParcelable(mLinkProperties, flags); 144 out.writeString(mSubscriberId); 145 out.writeInt(mLegacyType); 146 } 147 148 @NonNull 149 public static final Creator<NetworkStateSnapshot> CREATOR = 150 new Creator<NetworkStateSnapshot>() { 151 @NonNull 152 @Override 153 public NetworkStateSnapshot createFromParcel(@NonNull Parcel in) { 154 return new NetworkStateSnapshot(in); 155 } 156 157 @NonNull 158 @Override 159 public NetworkStateSnapshot[] newArray(int size) { 160 return new NetworkStateSnapshot[size]; 161 } 162 }; 163 164 @Override equals(Object o)165 public boolean equals(Object o) { 166 if (this == o) return true; 167 if (!(o instanceof NetworkStateSnapshot)) return false; 168 NetworkStateSnapshot that = (NetworkStateSnapshot) o; 169 return mLegacyType == that.mLegacyType 170 && Objects.equals(mNetwork, that.mNetwork) 171 && Objects.equals(mNetworkCapabilities, that.mNetworkCapabilities) 172 && Objects.equals(mLinkProperties, that.mLinkProperties) 173 && Objects.equals(mSubscriberId, that.mSubscriberId); 174 } 175 176 @Override hashCode()177 public int hashCode() { 178 return Objects.hash(mNetwork, 179 mNetworkCapabilities, mLinkProperties, mSubscriberId, mLegacyType); 180 } 181 182 @Override toString()183 public String toString() { 184 return "NetworkStateSnapshot{" 185 + "network=" + mNetwork 186 + ", networkCapabilities=" + mNetworkCapabilities 187 + ", linkProperties=" + mLinkProperties 188 + ", subscriberId='" + NetworkIdentityUtils.scrubSubscriberId(mSubscriberId) + '\'' 189 + ", legacyType=" + mLegacyType 190 + '}'; 191 } 192 } 193