diff --git a/core/java/android/net/NetworkIdentity.java b/core/java/android/net/NetworkIdentity.java index 1eef7d9a53..3bde6fa691 100644 --- a/core/java/android/net/NetworkIdentity.java +++ b/core/java/android/net/NetworkIdentity.java @@ -186,19 +186,19 @@ public class NetworkIdentity implements Comparable { */ public static NetworkIdentity buildNetworkIdentity(Context context, NetworkStateSnapshot snapshot, boolean defaultNetwork, @NetworkType int subType) { - final int legacyType = snapshot.legacyType; + final int legacyType = snapshot.getLegacyType(); - final String subscriberId = snapshot.subscriberId; + final String subscriberId = snapshot.getSubscriberId(); String networkId = null; - boolean roaming = !snapshot.networkCapabilities.hasCapability( + boolean roaming = !snapshot.getNetworkCapabilities().hasCapability( NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING); - boolean metered = !snapshot.networkCapabilities.hasCapability( + boolean metered = !snapshot.getNetworkCapabilities().hasCapability( NetworkCapabilities.NET_CAPABILITY_NOT_METERED); - final int oemManaged = getOemBitfield(snapshot.networkCapabilities); + final int oemManaged = getOemBitfield(snapshot.getNetworkCapabilities()); if (legacyType == TYPE_WIFI) { - networkId = snapshot.networkCapabilities.getSsid(); + networkId = snapshot.getNetworkCapabilities().getSsid(); if (networkId == null) { final WifiManager wifi = context.getSystemService(WifiManager.class); final WifiInfo info = wifi.getConnectionInfo(); diff --git a/core/java/android/net/NetworkStateSnapshot.java b/core/java/android/net/NetworkStateSnapshot.java index 0d26c2de86..9df861a6ff 100644 --- a/core/java/android/net/NetworkStateSnapshot.java +++ b/core/java/android/net/NetworkStateSnapshot.java @@ -37,47 +37,76 @@ import java.util.Objects; public final class NetworkStateSnapshot implements Parcelable { /** The network associated with this snapshot. */ @NonNull - public final Network network; + private final Network mNetwork; /** The {@link NetworkCapabilities} of the network associated with this snapshot. */ @NonNull - public final NetworkCapabilities networkCapabilities; + private final NetworkCapabilities mNetworkCapabilities; /** The {@link LinkProperties} of the network associated with this snapshot. */ @NonNull - public final LinkProperties linkProperties; + private final LinkProperties mLinkProperties; /** * The Subscriber Id of the network associated with this snapshot. See * {@link android.telephony.TelephonyManager#getSubscriberId()}. */ @Nullable - public final String subscriberId; + private final String mSubscriberId; /** * The legacy type of the network associated with this snapshot. See * {@code ConnectivityManager#TYPE_*}. */ - public final int legacyType; + private final int mLegacyType; public NetworkStateSnapshot(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities, @NonNull LinkProperties linkProperties, @Nullable String subscriberId, int legacyType) { - this.network = Objects.requireNonNull(network); - this.networkCapabilities = Objects.requireNonNull(networkCapabilities); - this.linkProperties = Objects.requireNonNull(linkProperties); - this.subscriberId = subscriberId; - this.legacyType = legacyType; + mNetwork = Objects.requireNonNull(network); + mNetworkCapabilities = Objects.requireNonNull(networkCapabilities); + mLinkProperties = Objects.requireNonNull(linkProperties); + mSubscriberId = subscriberId; + mLegacyType = legacyType; } /** @hide */ public NetworkStateSnapshot(@NonNull Parcel in) { - network = in.readParcelable(null); - networkCapabilities = in.readParcelable(null); - linkProperties = in.readParcelable(null); - subscriberId = in.readString(); - legacyType = in.readInt(); + mNetwork = in.readParcelable(null); + mNetworkCapabilities = in.readParcelable(null); + mLinkProperties = in.readParcelable(null); + mSubscriberId = in.readString(); + mLegacyType = in.readInt(); + } + + /** Get the network associated with this snapshot */ + @NonNull + public Network getNetwork() { + return mNetwork; + } + + /** Get {@link NetworkCapabilities} of the network associated with this snapshot. */ + @NonNull + public NetworkCapabilities getNetworkCapabilities() { + return mNetworkCapabilities; + } + + /** Get the {@link LinkProperties} of the network associated with this snapshot. */ + @NonNull + public LinkProperties getLinkProperties() { + return mLinkProperties; + } + + /** Get the Subscriber Id of the network associated with this snapshot. */ + @Nullable + public String getSubscriberId() { + return mSubscriberId; + } + + /** Get the legacy type of the network associated with this snapshot. */ + public int getLegacyType() { + return mLegacyType; } @Override @@ -87,11 +116,11 @@ public final class NetworkStateSnapshot implements Parcelable { @Override public void writeToParcel(@NonNull Parcel out, int flags) { - out.writeParcelable(network, flags); - out.writeParcelable(networkCapabilities, flags); - out.writeParcelable(linkProperties, flags); - out.writeString(subscriberId); - out.writeInt(legacyType); + out.writeParcelable(mNetwork, flags); + out.writeParcelable(mNetworkCapabilities, flags); + out.writeParcelable(mLinkProperties, flags); + out.writeString(mSubscriberId); + out.writeInt(mLegacyType); } @NonNull @@ -115,26 +144,27 @@ public final class NetworkStateSnapshot implements Parcelable { if (this == o) return true; if (!(o instanceof NetworkStateSnapshot)) return false; NetworkStateSnapshot that = (NetworkStateSnapshot) o; - return legacyType == that.legacyType - && Objects.equals(network, that.network) - && Objects.equals(networkCapabilities, that.networkCapabilities) - && Objects.equals(linkProperties, that.linkProperties) - && Objects.equals(subscriberId, that.subscriberId); + return mLegacyType == that.mLegacyType + && Objects.equals(mNetwork, that.mNetwork) + && Objects.equals(mNetworkCapabilities, that.mNetworkCapabilities) + && Objects.equals(mLinkProperties, that.mLinkProperties) + && Objects.equals(mSubscriberId, that.mSubscriberId); } @Override public int hashCode() { - return Objects.hash(network, networkCapabilities, linkProperties, subscriberId, legacyType); + return Objects.hash(mNetwork, + mNetworkCapabilities, mLinkProperties, mSubscriberId, mLegacyType); } @Override public String toString() { return "NetworkStateSnapshot{" - + "network=" + network - + ", networkCapabilities=" + networkCapabilities - + ", linkProperties=" + linkProperties - + ", subscriberId='" + NetworkIdentityUtils.scrubSubscriberId(subscriberId) + '\'' - + ", legacyType=" + legacyType + + "network=" + mNetwork + + ", networkCapabilities=" + mNetworkCapabilities + + ", linkProperties=" + mLinkProperties + + ", subscriberId='" + NetworkIdentityUtils.scrubSubscriberId(mSubscriberId) + '\'' + + ", legacyType=" + mLegacyType + '}'; } } diff --git a/services/core/java/com/android/server/net/NetworkStatsService.java b/services/core/java/com/android/server/net/NetworkStatsService.java index 19f5e3cd5d..3c14440c64 100644 --- a/services/core/java/com/android/server/net/NetworkStatsService.java +++ b/services/core/java/com/android/server/net/NetworkStatsService.java @@ -24,7 +24,6 @@ import static android.content.Intent.ACTION_UID_REMOVED; import static android.content.Intent.ACTION_USER_REMOVED; import static android.content.Intent.EXTRA_UID; import static android.content.pm.PackageManager.PERMISSION_GRANTED; -import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; import static android.net.NetworkIdentity.SUBTYPE_COMBINED; import static android.net.NetworkStack.checkNetworkStackPermission; import static android.net.NetworkStats.DEFAULT_NETWORK_ALL; @@ -97,12 +96,12 @@ import android.net.INetworkStatsSession; import android.net.Network; import android.net.NetworkCapabilities; import android.net.NetworkIdentity; +import android.net.NetworkSpecifier; import android.net.NetworkStack; import android.net.NetworkStateSnapshot; import android.net.NetworkStats; import android.net.NetworkStats.NonMonotonicObserver; import android.net.NetworkStatsHistory; -import android.net.NetworkSpecifier; import android.net.NetworkTemplate; import android.net.TelephonyNetworkSpecifier; import android.net.TrafficStats; @@ -1296,9 +1295,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub { final ArraySet mobileIfaces = new ArraySet<>(); for (NetworkStateSnapshot snapshot : snapshots) { final int displayTransport = - getDisplayTransport(snapshot.networkCapabilities.getTransportTypes()); + getDisplayTransport(snapshot.getNetworkCapabilities().getTransportTypes()); final boolean isMobile = (NetworkCapabilities.TRANSPORT_CELLULAR == displayTransport); - final boolean isDefault = ArrayUtils.contains(mDefaultNetworks, snapshot.network); + final boolean isDefault = ArrayUtils.contains(mDefaultNetworks, snapshot.getNetwork()); final int subType = combineSubtypeEnabled ? SUBTYPE_COMBINED : getSubTypeForStateSnapshot(snapshot); final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, snapshot, @@ -1306,7 +1305,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { // Traffic occurring on the base interface is always counted for // both total usage and UID details. - final String baseIface = snapshot.linkProperties.getInterfaceName(); + final String baseIface = snapshot.getLinkProperties().getInterfaceName(); if (baseIface != null) { findOrCreateNetworkIdentitySet(mActiveIfaces, baseIface).add(ident); findOrCreateNetworkIdentitySet(mActiveUidIfaces, baseIface).add(ident); @@ -1316,7 +1315,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { // If IMS is metered, then the IMS network usage has already included VT usage. // VT is considered always metered in framework's layer. If VT is not metered // per carrier's policy, modem will report 0 usage for VT calls. - if (snapshot.networkCapabilities.hasCapability( + if (snapshot.getNetworkCapabilities().hasCapability( NetworkCapabilities.NET_CAPABILITY_IMS) && !ident.getMetered()) { // Copy the identify from IMS one but mark it as metered. @@ -1364,7 +1363,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { // accounting is explicitly bypassed for traffic from the clat uid. // // TODO: This code might be combined to above code. - for (String iface : snapshot.linkProperties.getAllInterfaceNames()) { + for (String iface : snapshot.getLinkProperties().getAllInterfaceNames()) { // baseIface has been handled, so ignore it. if (TextUtils.equals(baseIface, iface)) continue; if (iface != null) { @@ -1383,11 +1382,11 @@ public class NetworkStatsService extends INetworkStatsService.Stub { } private static int getSubIdForMobile(@NonNull NetworkStateSnapshot state) { - if (!state.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { + if (!state.getNetworkCapabilities().hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { throw new IllegalArgumentException("Mobile state need capability TRANSPORT_CELLULAR"); } - final NetworkSpecifier spec = state.networkCapabilities.getNetworkSpecifier(); + final NetworkSpecifier spec = state.getNetworkCapabilities().getNetworkSpecifier(); if (spec instanceof TelephonyNetworkSpecifier) { return ((TelephonyNetworkSpecifier) spec).getSubscriptionId(); } else { @@ -1402,11 +1401,11 @@ public class NetworkStatsService extends INetworkStatsService.Stub { * transport types do not actually fill this value. */ private int getSubTypeForStateSnapshot(@NonNull NetworkStateSnapshot state) { - if (!state.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { + if (!state.getNetworkCapabilities().hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { return 0; } - return mNetworkStatsSubscriptionsMonitor.getRatTypeForSubscriberId(state.subscriberId); + return mNetworkStatsSubscriptionsMonitor.getRatTypeForSubscriberId(state.getSubscriberId()); } private static NetworkIdentitySet findOrCreateNetworkIdentitySet(