Merge "Add support for tracking PANS data usage"

This commit is contained in:
Chris Weir
2021-02-25 21:37:42 +00:00
committed by Gerrit Code Review
4 changed files with 107 additions and 13 deletions

View File

@@ -18,6 +18,7 @@ package android.net;
import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.ConnectivityManager.TYPE_WIFI;
import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.content.Context; import android.content.Context;
import android.net.wifi.WifiInfo; import android.net.wifi.WifiInfo;
@@ -41,6 +42,22 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
public static final int SUBTYPE_COMBINED = -1; public static final int SUBTYPE_COMBINED = -1;
/**
* Network has no {@code NetworkCapabilities#NET_CAPABILITY_OEM_*}.
* @hide
*/
public static final int OEM_NONE = 0x0;
/**
* Network has {@link NetworkCapabilities#NET_CAPABILITY_OEM_PAID}.
* @hide
*/
public static final int OEM_PAID = 0x1;
/**
* Network has {@link NetworkCapabilities#NET_CAPABILITY_OEM_PRIVATE}.
* @hide
*/
public static final int OEM_PRIVATE = 0x2;
final int mType; final int mType;
final int mSubType; final int mSubType;
final String mSubscriberId; final String mSubscriberId;
@@ -48,10 +65,11 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
final boolean mRoaming; final boolean mRoaming;
final boolean mMetered; final boolean mMetered;
final boolean mDefaultNetwork; final boolean mDefaultNetwork;
final int mOemManaged;
public NetworkIdentity( public NetworkIdentity(
int type, int subType, String subscriberId, String networkId, boolean roaming, int type, int subType, String subscriberId, String networkId, boolean roaming,
boolean metered, boolean defaultNetwork) { boolean metered, boolean defaultNetwork, int oemManaged) {
mType = type; mType = type;
mSubType = subType; mSubType = subType;
mSubscriberId = subscriberId; mSubscriberId = subscriberId;
@@ -59,12 +77,13 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
mRoaming = roaming; mRoaming = roaming;
mMetered = metered; mMetered = metered;
mDefaultNetwork = defaultNetwork; mDefaultNetwork = defaultNetwork;
mOemManaged = oemManaged;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(mType, mSubType, mSubscriberId, mNetworkId, mRoaming, mMetered, return Objects.hash(mType, mSubType, mSubscriberId, mNetworkId, mRoaming, mMetered,
mDefaultNetwork); mDefaultNetwork, mOemManaged);
} }
@Override @Override
@@ -75,7 +94,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
&& Objects.equals(mSubscriberId, ident.mSubscriberId) && Objects.equals(mSubscriberId, ident.mSubscriberId)
&& Objects.equals(mNetworkId, ident.mNetworkId) && Objects.equals(mNetworkId, ident.mNetworkId)
&& mMetered == ident.mMetered && mMetered == ident.mMetered
&& mDefaultNetwork == ident.mDefaultNetwork; && mDefaultNetwork == ident.mDefaultNetwork
&& mOemManaged == ident.mOemManaged;
} }
return false; return false;
} }
@@ -102,6 +122,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
} }
builder.append(", metered=").append(mMetered); builder.append(", metered=").append(mMetered);
builder.append(", defaultNetwork=").append(mDefaultNetwork); builder.append(", defaultNetwork=").append(mDefaultNetwork);
// TODO(180557699): Print a human readable string for OEM managed state.
builder.append(", oemManaged=").append(mOemManaged);
return builder.append("}").toString(); return builder.append("}").toString();
} }
@@ -120,6 +142,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
proto.write(NetworkIdentityProto.ROAMING, mRoaming); proto.write(NetworkIdentityProto.ROAMING, mRoaming);
proto.write(NetworkIdentityProto.METERED, mMetered); proto.write(NetworkIdentityProto.METERED, mMetered);
proto.write(NetworkIdentityProto.DEFAULT_NETWORK, mDefaultNetwork); proto.write(NetworkIdentityProto.DEFAULT_NETWORK, mDefaultNetwork);
proto.write(NetworkIdentityProto.OEM_MANAGED_NETWORK, mOemManaged);
proto.end(start); proto.end(start);
} }
@@ -152,6 +175,10 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
return mDefaultNetwork; return mDefaultNetwork;
} }
public int getOemManaged() {
return mOemManaged;
}
/** /**
* Build a {@link NetworkIdentity} from the given {@link NetworkState} and {@code subType}, * Build a {@link NetworkIdentity} from the given {@link NetworkState} and {@code subType},
* assuming that any mobile networks are using the current IMSI. The subType if applicable, * assuming that any mobile networks are using the current IMSI. The subType if applicable,
@@ -171,6 +198,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
subscriberId = state.subscriberId; subscriberId = state.subscriberId;
final int oemManaged = getOemBitfield(state.networkCapabilities);
if (legacyType == TYPE_WIFI) { if (legacyType == TYPE_WIFI) {
if (state.networkCapabilities.getSsid() != null) { if (state.networkCapabilities.getSsid() != null) {
networkId = state.networkCapabilities.getSsid(); networkId = state.networkCapabilities.getSsid();
@@ -185,7 +214,24 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
} }
return new NetworkIdentity(legacyType, subType, subscriberId, networkId, roaming, metered, return new NetworkIdentity(legacyType, subType, subscriberId, networkId, roaming, metered,
defaultNetwork); defaultNetwork, oemManaged);
}
/**
* Builds a bitfield of {@code NetworkIdentity.OEM_*} based on {@link NetworkCapabilities}.
* @hide
*/
public static int getOemBitfield(NetworkCapabilities nc) {
int oemManaged = OEM_NONE;
if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PAID)) {
oemManaged |= OEM_PAID;
}
if (nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE)) {
oemManaged |= OEM_PRIVATE;
}
return oemManaged;
} }
@Override @Override
@@ -209,6 +255,9 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
if (res == 0) { if (res == 0) {
res = Boolean.compare(mDefaultNetwork, another.mDefaultNetwork); res = Boolean.compare(mDefaultNetwork, another.mDefaultNetwork);
} }
if (res == 0) {
res = Integer.compare(mOemManaged, another.mOemManaged);
}
return res; return res;
} }
} }

View File

@@ -23,6 +23,7 @@ import static android.net.ConnectivityManager.TYPE_PROXY;
import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.ConnectivityManager.TYPE_WIFI;
import static android.net.ConnectivityManager.TYPE_WIFI_P2P; import static android.net.ConnectivityManager.TYPE_WIFI_P2P;
import static android.net.ConnectivityManager.TYPE_WIMAX; import static android.net.ConnectivityManager.TYPE_WIMAX;
import static android.net.NetworkIdentity.OEM_NONE;
import static android.net.NetworkStats.DEFAULT_NETWORK_ALL; import static android.net.NetworkStats.DEFAULT_NETWORK_ALL;
import static android.net.NetworkStats.DEFAULT_NETWORK_NO; import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
import static android.net.NetworkStats.DEFAULT_NETWORK_YES; import static android.net.NetworkStats.DEFAULT_NETWORK_YES;
@@ -99,6 +100,22 @@ public class NetworkTemplate implements Parcelable {
*/ */
public static final int NETWORK_TYPE_5G_NSA = -2; public static final int NETWORK_TYPE_5G_NSA = -2;
/**
* Value to match both OEM managed and unmanaged networks (all networks).
* @hide
*/
public static final int OEM_MANAGED_ALL = -1;
/**
* Value to match networks which are not OEM managed.
* @hide
*/
public static final int OEM_MANAGED_NO = OEM_NONE;
/**
* Value to match any OEM managed network.
* @hide
*/
public static final int OEM_MANAGED_YES = -2;
private static boolean isKnownMatchRule(final int rule) { private static boolean isKnownMatchRule(final int rule) {
switch (rule) { switch (rule) {
case MATCH_MOBILE: case MATCH_MOBILE:
@@ -151,10 +168,10 @@ public class NetworkTemplate implements Parcelable {
@NetworkType int ratType) { @NetworkType int ratType) {
if (TextUtils.isEmpty(subscriberId)) { if (TextUtils.isEmpty(subscriberId)) {
return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null, null, return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null, null,
METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType); METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType, OEM_MANAGED_ALL);
} }
return new NetworkTemplate(MATCH_MOBILE, subscriberId, new String[]{subscriberId}, null, return new NetworkTemplate(MATCH_MOBILE, subscriberId, new String[]{subscriberId}, null,
METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType); METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType, OEM_MANAGED_ALL);
} }
/** /**
@@ -235,6 +252,9 @@ public class NetworkTemplate implements Parcelable {
private final int mDefaultNetwork; private final int mDefaultNetwork;
private final int mSubType; private final int mSubType;
// Bitfield containing OEM network properties{@code NetworkIdentity#OEM_*}.
private final int mOemManaged;
@UnsupportedAppUsage @UnsupportedAppUsage
public NetworkTemplate(int matchRule, String subscriberId, String networkId) { public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
this(matchRule, subscriberId, new String[] { subscriberId }, networkId); this(matchRule, subscriberId, new String[] { subscriberId }, networkId);
@@ -243,11 +263,12 @@ public class NetworkTemplate implements Parcelable {
public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds, public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
String networkId) { String networkId) {
this(matchRule, subscriberId, matchSubscriberIds, networkId, METERED_ALL, ROAMING_ALL, this(matchRule, subscriberId, matchSubscriberIds, networkId, METERED_ALL, ROAMING_ALL,
DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL); DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL);
} }
public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds, public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
String networkId, int metered, int roaming, int defaultNetwork, int subType) { String networkId, int metered, int roaming, int defaultNetwork, int subType,
int oemManaged) {
mMatchRule = matchRule; mMatchRule = matchRule;
mSubscriberId = subscriberId; mSubscriberId = subscriberId;
mMatchSubscriberIds = matchSubscriberIds; mMatchSubscriberIds = matchSubscriberIds;
@@ -256,6 +277,7 @@ public class NetworkTemplate implements Parcelable {
mRoaming = roaming; mRoaming = roaming;
mDefaultNetwork = defaultNetwork; mDefaultNetwork = defaultNetwork;
mSubType = subType; mSubType = subType;
mOemManaged = oemManaged;
if (!isKnownMatchRule(matchRule)) { if (!isKnownMatchRule(matchRule)) {
Log.e(TAG, "Unknown network template rule " + matchRule Log.e(TAG, "Unknown network template rule " + matchRule
@@ -272,6 +294,7 @@ public class NetworkTemplate implements Parcelable {
mRoaming = in.readInt(); mRoaming = in.readInt();
mDefaultNetwork = in.readInt(); mDefaultNetwork = in.readInt();
mSubType = in.readInt(); mSubType = in.readInt();
mOemManaged = in.readInt();
} }
@Override @Override
@@ -284,6 +307,7 @@ public class NetworkTemplate implements Parcelable {
dest.writeInt(mRoaming); dest.writeInt(mRoaming);
dest.writeInt(mDefaultNetwork); dest.writeInt(mDefaultNetwork);
dest.writeInt(mSubType); dest.writeInt(mSubType);
dest.writeInt(mOemManaged);
} }
@Override @Override
@@ -319,13 +343,16 @@ public class NetworkTemplate implements Parcelable {
if (mSubType != NETWORK_TYPE_ALL) { if (mSubType != NETWORK_TYPE_ALL) {
builder.append(", subType=").append(mSubType); builder.append(", subType=").append(mSubType);
} }
if (mOemManaged != OEM_MANAGED_ALL) {
builder.append(", oemManaged=").append(mOemManaged);
}
return builder.toString(); return builder.toString();
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(mMatchRule, mSubscriberId, mNetworkId, mMetered, mRoaming, return Objects.hash(mMatchRule, mSubscriberId, mNetworkId, mMetered, mRoaming,
mDefaultNetwork, mSubType); mDefaultNetwork, mSubType, mOemManaged);
} }
@Override @Override
@@ -338,7 +365,8 @@ public class NetworkTemplate implements Parcelable {
&& mMetered == other.mMetered && mMetered == other.mMetered
&& mRoaming == other.mRoaming && mRoaming == other.mRoaming
&& mDefaultNetwork == other.mDefaultNetwork && mDefaultNetwork == other.mDefaultNetwork
&& mSubType == other.mSubType; && mSubType == other.mSubType
&& mOemManaged == other.mOemManaged;
} }
return false; return false;
} }
@@ -384,6 +412,7 @@ public class NetworkTemplate implements Parcelable {
if (!matchesMetered(ident)) return false; if (!matchesMetered(ident)) return false;
if (!matchesRoaming(ident)) return false; if (!matchesRoaming(ident)) return false;
if (!matchesDefaultNetwork(ident)) return false; if (!matchesDefaultNetwork(ident)) return false;
if (!matchesOemNetwork(ident)) return false;
switch (mMatchRule) { switch (mMatchRule) {
case MATCH_MOBILE: case MATCH_MOBILE:
@@ -425,6 +454,13 @@ public class NetworkTemplate implements Parcelable {
|| (mDefaultNetwork == DEFAULT_NETWORK_NO && !ident.mDefaultNetwork); || (mDefaultNetwork == DEFAULT_NETWORK_NO && !ident.mDefaultNetwork);
} }
private boolean matchesOemNetwork(NetworkIdentity ident) {
return (mOemManaged == OEM_MANAGED_ALL)
|| (mOemManaged == OEM_MANAGED_YES
&& ident.mOemManaged != OEM_NONE)
|| (mOemManaged == ident.mOemManaged);
}
private boolean matchesCollapsedRatType(NetworkIdentity ident) { private boolean matchesCollapsedRatType(NetworkIdentity ident) {
return mSubType == NETWORK_TYPE_ALL return mSubType == NETWORK_TYPE_ALL
|| getCollapsedRatType(mSubType) == getCollapsedRatType(ident.mSubType); || getCollapsedRatType(mSubType) == getCollapsedRatType(ident.mSubType);

View File

@@ -40,6 +40,7 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
private static final int VERSION_ADD_NETWORK_ID = 3; private static final int VERSION_ADD_NETWORK_ID = 3;
private static final int VERSION_ADD_METERED = 4; private static final int VERSION_ADD_METERED = 4;
private static final int VERSION_ADD_DEFAULT_NETWORK = 5; private static final int VERSION_ADD_DEFAULT_NETWORK = 5;
private static final int VERSION_ADD_OEM_MANAGED_NETWORK = 6;
public NetworkIdentitySet() { public NetworkIdentitySet() {
} }
@@ -84,13 +85,20 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
defaultNetwork = true; defaultNetwork = true;
} }
final int oemNetCapabilities;
if (version >= VERSION_ADD_OEM_MANAGED_NETWORK) {
oemNetCapabilities = in.readInt();
} else {
oemNetCapabilities = NetworkIdentity.OEM_NONE;
}
add(new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered, add(new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered,
defaultNetwork)); defaultNetwork, oemNetCapabilities));
} }
} }
public void writeToStream(DataOutput out) throws IOException { public void writeToStream(DataOutput out) throws IOException {
out.writeInt(VERSION_ADD_DEFAULT_NETWORK); out.writeInt(VERSION_ADD_OEM_MANAGED_NETWORK);
out.writeInt(size()); out.writeInt(size());
for (NetworkIdentity ident : this) { for (NetworkIdentity ident : this) {
out.writeInt(ident.getType()); out.writeInt(ident.getType());
@@ -100,6 +108,7 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
out.writeBoolean(ident.getRoaming()); out.writeBoolean(ident.getRoaming());
out.writeBoolean(ident.getMetered()); out.writeBoolean(ident.getMetered());
out.writeBoolean(ident.getDefaultNetwork()); out.writeBoolean(ident.getDefaultNetwork());
out.writeInt(ident.getOemManaged());
} }
} }

View File

@@ -1319,7 +1319,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
NetworkIdentity vtIdent = new NetworkIdentity(ident.getType(), NetworkIdentity vtIdent = new NetworkIdentity(ident.getType(),
ident.getSubType(), ident.getSubscriberId(), ident.getNetworkId(), ident.getSubType(), ident.getSubscriberId(), ident.getNetworkId(),
ident.getRoaming(), true /* metered */, ident.getRoaming(), true /* metered */,
true /* onDefaultNetwork */); true /* onDefaultNetwork */, ident.getOemManaged());
findOrCreateNetworkIdentitySet(mActiveIfaces, IFACE_VT).add(vtIdent); findOrCreateNetworkIdentitySet(mActiveIfaces, IFACE_VT).add(vtIdent);
findOrCreateNetworkIdentitySet(mActiveUidIfaces, IFACE_VT).add(vtIdent); findOrCreateNetworkIdentitySet(mActiveUidIfaces, IFACE_VT).add(vtIdent);
} }