Merge changes Id8776d14,Ifa291c62,I5ea9d200 am: c48f4cb7f1

am: b36b423230

Change-Id: I1ac9d349850d607c5fbf4a47760bc09de1c18043
This commit is contained in:
Lorenzo Colitti
2018-01-19 16:37:44 +00:00
committed by android-build-merger
16 changed files with 573 additions and 381 deletions

View File

@@ -18,6 +18,7 @@ package android.net;
import android.net.DataUsageRequest; import android.net.DataUsageRequest;
import android.net.INetworkStatsSession; import android.net.INetworkStatsSession;
import android.net.Network;
import android.net.NetworkStats; import android.net.NetworkStats;
import android.net.NetworkStatsHistory; import android.net.NetworkStatsHistory;
import android.net.NetworkTemplate; import android.net.NetworkTemplate;
@@ -53,7 +54,7 @@ interface INetworkStatsService {
void setUidForeground(int uid, boolean uidForeground); void setUidForeground(int uid, boolean uidForeground);
/** Force update of ifaces. */ /** Force update of ifaces. */
void forceUpdateIfaces(); void forceUpdateIfaces(in Network[] defaultNetworks);
/** Force update of statistics. */ /** Force update of statistics. */
void forceUpdate(); void forceUpdate();

View File

@@ -58,21 +58,24 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
final String mNetworkId; final String mNetworkId;
final boolean mRoaming; final boolean mRoaming;
final boolean mMetered; final boolean mMetered;
final boolean mDefaultNetwork;
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 metered, boolean defaultNetwork) {
mType = type; mType = type;
mSubType = COMBINE_SUBTYPE_ENABLED ? SUBTYPE_COMBINED : subType; mSubType = COMBINE_SUBTYPE_ENABLED ? SUBTYPE_COMBINED : subType;
mSubscriberId = subscriberId; mSubscriberId = subscriberId;
mNetworkId = networkId; mNetworkId = networkId;
mRoaming = roaming; mRoaming = roaming;
mMetered = metered; mMetered = metered;
mDefaultNetwork = defaultNetwork;
} }
@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);
} }
@Override @Override
@@ -82,7 +85,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
return mType == ident.mType && mSubType == ident.mSubType && mRoaming == ident.mRoaming return mType == ident.mType && mSubType == ident.mSubType && mRoaming == ident.mRoaming
&& 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;
} }
return false; return false;
} }
@@ -109,6 +113,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
builder.append(", ROAMING"); builder.append(", ROAMING");
} }
builder.append(", metered=").append(mMetered); builder.append(", metered=").append(mMetered);
builder.append(", defaultNetwork=").append(mDefaultNetwork);
return builder.append("}").toString(); return builder.append("}").toString();
} }
@@ -153,6 +158,10 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
return mMetered; return mMetered;
} }
public boolean getDefaultNetwork() {
return mDefaultNetwork;
}
/** /**
* Scrub given IMSI on production builds. * Scrub given IMSI on production builds.
*/ */
@@ -183,7 +192,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
* Build a {@link NetworkIdentity} from the given {@link NetworkState}, * Build a {@link NetworkIdentity} from the given {@link NetworkState},
* assuming that any mobile networks are using the current IMSI. * assuming that any mobile networks are using the current IMSI.
*/ */
public static NetworkIdentity buildNetworkIdentity(Context context, NetworkState state) { public static NetworkIdentity buildNetworkIdentity(Context context, NetworkState state,
boolean defaultNetwork) {
final int type = state.networkInfo.getType(); final int type = state.networkInfo.getType();
final int subType = state.networkInfo.getSubtype(); final int subType = state.networkInfo.getSubtype();
@@ -216,7 +226,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
} }
} }
return new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered); return new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered,
defaultNetwork);
} }
@Override @Override
@@ -237,6 +248,9 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
if (res == 0) { if (res == 0) {
res = Boolean.compare(mMetered, another.mMetered); res = Boolean.compare(mMetered, another.mMetered);
} }
if (res == 0) {
res = Boolean.compare(mDefaultNetwork, another.mDefaultNetwork);
}
return res; return res;
} }
} }

View File

@@ -82,6 +82,13 @@ public class NetworkStats implements Parcelable {
/** {@link #roaming} value where roaming data is accounted. */ /** {@link #roaming} value where roaming data is accounted. */
public static final int ROAMING_YES = 1; public static final int ROAMING_YES = 1;
/** {@link #onDefaultNetwork} value to account for all default network states. */
public static final int DEFAULT_NETWORK_ALL = -1;
/** {@link #onDefaultNetwork} value to account for usage while not the default network. */
public static final int DEFAULT_NETWORK_NO = 0;
/** {@link #onDefaultNetwork} value to account for usage while the default network. */
public static final int DEFAULT_NETWORK_YES = 1;
/** Denotes a request for stats at the interface level. */ /** Denotes a request for stats at the interface level. */
public static final int STATS_PER_IFACE = 0; public static final int STATS_PER_IFACE = 0;
/** Denotes a request for stats at the interface and UID level. */ /** Denotes a request for stats at the interface and UID level. */
@@ -102,6 +109,7 @@ public class NetworkStats implements Parcelable {
private int[] tag; private int[] tag;
private int[] metered; private int[] metered;
private int[] roaming; private int[] roaming;
private int[] defaultNetwork;
private long[] rxBytes; private long[] rxBytes;
private long[] rxPackets; private long[] rxPackets;
private long[] txBytes; private long[] txBytes;
@@ -125,6 +133,12 @@ public class NetworkStats implements Parcelable {
* getSummary(). * getSummary().
*/ */
public int roaming; public int roaming;
/**
* Note that this is only populated w/ the default value when read from /proc or written
* to disk. We merge in the correct value when reporting this value to clients of
* getSummary().
*/
public int defaultNetwork;
public long rxBytes; public long rxBytes;
public long rxPackets; public long rxPackets;
public long txBytes; public long txBytes;
@@ -142,18 +156,27 @@ public class NetworkStats implements Parcelable {
public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets, public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets,
long txBytes, long txPackets, long operations) { long txBytes, long txPackets, long operations) {
this(iface, uid, set, tag, METERED_NO, ROAMING_NO, rxBytes, rxPackets, txBytes, this(iface, uid, set, tag, METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO,
txPackets, operations); rxBytes, rxPackets, txBytes, txPackets, operations);
}
// TODO: fix the the telephony code to pass DEFAULT_NETWORK_YES and remove this constructor.
public Entry(String iface, int uid, int set, int tag, int metered, int roaming,
long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
this(iface, uid, set, tag, metered, roaming, DEFAULT_NETWORK_YES, rxBytes, rxPackets,
txBytes, txPackets, operations);
} }
public Entry(String iface, int uid, int set, int tag, int metered, int roaming, public Entry(String iface, int uid, int set, int tag, int metered, int roaming,
long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) { int defaultNetwork, long rxBytes, long rxPackets, long txBytes, long txPackets,
long operations) {
this.iface = iface; this.iface = iface;
this.uid = uid; this.uid = uid;
this.set = set; this.set = set;
this.tag = tag; this.tag = tag;
this.metered = metered; this.metered = metered;
this.roaming = roaming; this.roaming = roaming;
this.defaultNetwork = defaultNetwork;
this.rxBytes = rxBytes; this.rxBytes = rxBytes;
this.rxPackets = rxPackets; this.rxPackets = rxPackets;
this.txBytes = txBytes; this.txBytes = txBytes;
@@ -187,6 +210,7 @@ public class NetworkStats implements Parcelable {
builder.append(" tag=").append(tagToString(tag)); builder.append(" tag=").append(tagToString(tag));
builder.append(" metered=").append(meteredToString(metered)); builder.append(" metered=").append(meteredToString(metered));
builder.append(" roaming=").append(roamingToString(roaming)); builder.append(" roaming=").append(roamingToString(roaming));
builder.append(" defaultNetwork=").append(defaultNetworkToString(defaultNetwork));
builder.append(" rxBytes=").append(rxBytes); builder.append(" rxBytes=").append(rxBytes);
builder.append(" rxPackets=").append(rxPackets); builder.append(" rxPackets=").append(rxPackets);
builder.append(" txBytes=").append(txBytes); builder.append(" txBytes=").append(txBytes);
@@ -200,7 +224,8 @@ public class NetworkStats implements Parcelable {
if (o instanceof Entry) { if (o instanceof Entry) {
final Entry e = (Entry) o; final Entry e = (Entry) o;
return uid == e.uid && set == e.set && tag == e.tag && metered == e.metered return uid == e.uid && set == e.set && tag == e.tag && metered == e.metered
&& roaming == e.roaming && rxBytes == e.rxBytes && rxPackets == e.rxPackets && roaming == e.roaming && defaultNetwork == e.defaultNetwork
&& rxBytes == e.rxBytes && rxPackets == e.rxPackets
&& txBytes == e.txBytes && txPackets == e.txPackets && txBytes == e.txBytes && txPackets == e.txPackets
&& operations == e.operations && iface.equals(e.iface); && operations == e.operations && iface.equals(e.iface);
} }
@@ -209,7 +234,7 @@ public class NetworkStats implements Parcelable {
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(uid, set, tag, metered, roaming, iface); return Objects.hash(uid, set, tag, metered, roaming, defaultNetwork, iface);
} }
} }
@@ -224,6 +249,7 @@ public class NetworkStats implements Parcelable {
this.tag = new int[initialSize]; this.tag = new int[initialSize];
this.metered = new int[initialSize]; this.metered = new int[initialSize];
this.roaming = new int[initialSize]; this.roaming = new int[initialSize];
this.defaultNetwork = new int[initialSize];
this.rxBytes = new long[initialSize]; this.rxBytes = new long[initialSize];
this.rxPackets = new long[initialSize]; this.rxPackets = new long[initialSize];
this.txBytes = new long[initialSize]; this.txBytes = new long[initialSize];
@@ -238,6 +264,7 @@ public class NetworkStats implements Parcelable {
this.tag = EmptyArray.INT; this.tag = EmptyArray.INT;
this.metered = EmptyArray.INT; this.metered = EmptyArray.INT;
this.roaming = EmptyArray.INT; this.roaming = EmptyArray.INT;
this.defaultNetwork = EmptyArray.INT;
this.rxBytes = EmptyArray.LONG; this.rxBytes = EmptyArray.LONG;
this.rxPackets = EmptyArray.LONG; this.rxPackets = EmptyArray.LONG;
this.txBytes = EmptyArray.LONG; this.txBytes = EmptyArray.LONG;
@@ -256,6 +283,7 @@ public class NetworkStats implements Parcelable {
tag = parcel.createIntArray(); tag = parcel.createIntArray();
metered = parcel.createIntArray(); metered = parcel.createIntArray();
roaming = parcel.createIntArray(); roaming = parcel.createIntArray();
defaultNetwork = parcel.createIntArray();
rxBytes = parcel.createLongArray(); rxBytes = parcel.createLongArray();
rxPackets = parcel.createLongArray(); rxPackets = parcel.createLongArray();
txBytes = parcel.createLongArray(); txBytes = parcel.createLongArray();
@@ -274,6 +302,7 @@ public class NetworkStats implements Parcelable {
dest.writeIntArray(tag); dest.writeIntArray(tag);
dest.writeIntArray(metered); dest.writeIntArray(metered);
dest.writeIntArray(roaming); dest.writeIntArray(roaming);
dest.writeIntArray(defaultNetwork);
dest.writeLongArray(rxBytes); dest.writeLongArray(rxBytes);
dest.writeLongArray(rxPackets); dest.writeLongArray(rxPackets);
dest.writeLongArray(txBytes); dest.writeLongArray(txBytes);
@@ -308,10 +337,11 @@ public class NetworkStats implements Parcelable {
@VisibleForTesting @VisibleForTesting
public NetworkStats addValues(String iface, int uid, int set, int tag, int metered, int roaming, public NetworkStats addValues(String iface, int uid, int set, int tag, int metered, int roaming,
long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) { int defaultNetwork, long rxBytes, long rxPackets, long txBytes, long txPackets,
long operations) {
return addValues(new Entry( return addValues(new Entry(
iface, uid, set, tag, metered, roaming, rxBytes, rxPackets, txBytes, txPackets, iface, uid, set, tag, metered, roaming, defaultNetwork, rxBytes, rxPackets,
operations)); txBytes, txPackets, operations));
} }
/** /**
@@ -327,6 +357,7 @@ public class NetworkStats implements Parcelable {
tag = Arrays.copyOf(tag, newLength); tag = Arrays.copyOf(tag, newLength);
metered = Arrays.copyOf(metered, newLength); metered = Arrays.copyOf(metered, newLength);
roaming = Arrays.copyOf(roaming, newLength); roaming = Arrays.copyOf(roaming, newLength);
defaultNetwork = Arrays.copyOf(defaultNetwork, newLength);
rxBytes = Arrays.copyOf(rxBytes, newLength); rxBytes = Arrays.copyOf(rxBytes, newLength);
rxPackets = Arrays.copyOf(rxPackets, newLength); rxPackets = Arrays.copyOf(rxPackets, newLength);
txBytes = Arrays.copyOf(txBytes, newLength); txBytes = Arrays.copyOf(txBytes, newLength);
@@ -341,6 +372,7 @@ public class NetworkStats implements Parcelable {
tag[size] = entry.tag; tag[size] = entry.tag;
metered[size] = entry.metered; metered[size] = entry.metered;
roaming[size] = entry.roaming; roaming[size] = entry.roaming;
defaultNetwork[size] = entry.defaultNetwork;
rxBytes[size] = entry.rxBytes; rxBytes[size] = entry.rxBytes;
rxPackets[size] = entry.rxPackets; rxPackets[size] = entry.rxPackets;
txBytes[size] = entry.txBytes; txBytes[size] = entry.txBytes;
@@ -362,6 +394,7 @@ public class NetworkStats implements Parcelable {
entry.tag = tag[i]; entry.tag = tag[i];
entry.metered = metered[i]; entry.metered = metered[i];
entry.roaming = roaming[i]; entry.roaming = roaming[i];
entry.defaultNetwork = defaultNetwork[i];
entry.rxBytes = rxBytes[i]; entry.rxBytes = rxBytes[i];
entry.rxPackets = rxPackets[i]; entry.rxPackets = rxPackets[i];
entry.txBytes = txBytes[i]; entry.txBytes = txBytes[i];
@@ -416,7 +449,7 @@ public class NetworkStats implements Parcelable {
*/ */
public NetworkStats combineValues(Entry entry) { public NetworkStats combineValues(Entry entry) {
final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag, entry.metered, final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag, entry.metered,
entry.roaming); entry.roaming, entry.defaultNetwork);
if (i == -1) { if (i == -1) {
// only create new entry when positive contribution // only create new entry when positive contribution
addValues(entry); addValues(entry);
@@ -444,10 +477,12 @@ public class NetworkStats implements Parcelable {
/** /**
* Find first stats index that matches the requested parameters. * Find first stats index that matches the requested parameters.
*/ */
public int findIndex(String iface, int uid, int set, int tag, int metered, int roaming) { public int findIndex(String iface, int uid, int set, int tag, int metered, int roaming,
int defaultNetwork) {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i] if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
&& metered == this.metered[i] && roaming == this.roaming[i] && metered == this.metered[i] && roaming == this.roaming[i]
&& defaultNetwork == this.defaultNetwork[i]
&& Objects.equals(iface, this.iface[i])) { && Objects.equals(iface, this.iface[i])) {
return i; return i;
} }
@@ -461,7 +496,7 @@ public class NetworkStats implements Parcelable {
*/ */
@VisibleForTesting @VisibleForTesting
public int findIndexHinted(String iface, int uid, int set, int tag, int metered, int roaming, public int findIndexHinted(String iface, int uid, int set, int tag, int metered, int roaming,
int hintIndex) { int defaultNetwork, int hintIndex) {
for (int offset = 0; offset < size; offset++) { for (int offset = 0; offset < size; offset++) {
final int halfOffset = offset / 2; final int halfOffset = offset / 2;
@@ -475,6 +510,7 @@ public class NetworkStats implements Parcelable {
if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i] if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
&& metered == this.metered[i] && roaming == this.roaming[i] && metered == this.metered[i] && roaming == this.roaming[i]
&& defaultNetwork == this.defaultNetwork[i]
&& Objects.equals(iface, this.iface[i])) { && Objects.equals(iface, this.iface[i])) {
return i; return i;
} }
@@ -489,7 +525,8 @@ public class NetworkStats implements Parcelable {
*/ */
public void spliceOperationsFrom(NetworkStats stats) { public void spliceOperationsFrom(NetworkStats stats) {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
final int j = stats.findIndex(iface[i], uid[i], set[i], tag[i], metered[i], roaming[i]); final int j = stats.findIndex(iface[i], uid[i], set[i], tag[i], metered[i], roaming[i],
defaultNetwork[i]);
if (j == -1) { if (j == -1) {
operations[i] = 0; operations[i] = 0;
} else { } else {
@@ -581,6 +618,7 @@ public class NetworkStats implements Parcelable {
entry.tag = TAG_NONE; entry.tag = TAG_NONE;
entry.metered = METERED_ALL; entry.metered = METERED_ALL;
entry.roaming = ROAMING_ALL; entry.roaming = ROAMING_ALL;
entry.defaultNetwork = DEFAULT_NETWORK_ALL;
entry.rxBytes = 0; entry.rxBytes = 0;
entry.rxPackets = 0; entry.rxPackets = 0;
entry.txBytes = 0; entry.txBytes = 0;
@@ -677,6 +715,7 @@ public class NetworkStats implements Parcelable {
entry.tag = left.tag[i]; entry.tag = left.tag[i];
entry.metered = left.metered[i]; entry.metered = left.metered[i];
entry.roaming = left.roaming[i]; entry.roaming = left.roaming[i];
entry.defaultNetwork = left.defaultNetwork[i];
entry.rxBytes = left.rxBytes[i]; entry.rxBytes = left.rxBytes[i];
entry.rxPackets = left.rxPackets[i]; entry.rxPackets = left.rxPackets[i];
entry.txBytes = left.txBytes[i]; entry.txBytes = left.txBytes[i];
@@ -685,7 +724,7 @@ public class NetworkStats implements Parcelable {
// find remote row that matches, and subtract // find remote row that matches, and subtract
final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag, final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag,
entry.metered, entry.roaming, i); entry.metered, entry.roaming, entry.defaultNetwork, i);
if (j != -1) { if (j != -1) {
// Found matching row, subtract remote value. // Found matching row, subtract remote value.
entry.rxBytes -= right.rxBytes[j]; entry.rxBytes -= right.rxBytes[j];
@@ -725,6 +764,7 @@ public class NetworkStats implements Parcelable {
entry.tag = TAG_NONE; entry.tag = TAG_NONE;
entry.metered = METERED_ALL; entry.metered = METERED_ALL;
entry.roaming = ROAMING_ALL; entry.roaming = ROAMING_ALL;
entry.defaultNetwork = DEFAULT_NETWORK_ALL;
entry.operations = 0L; entry.operations = 0L;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
@@ -755,6 +795,7 @@ public class NetworkStats implements Parcelable {
entry.tag = TAG_NONE; entry.tag = TAG_NONE;
entry.metered = METERED_ALL; entry.metered = METERED_ALL;
entry.roaming = ROAMING_ALL; entry.roaming = ROAMING_ALL;
entry.defaultNetwork = DEFAULT_NETWORK_ALL;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
// skip specific tags, since already counted in TAG_NONE // skip specific tags, since already counted in TAG_NONE
@@ -802,6 +843,7 @@ public class NetworkStats implements Parcelable {
pw.print(" tag="); pw.print(tagToString(tag[i])); pw.print(" tag="); pw.print(tagToString(tag[i]));
pw.print(" metered="); pw.print(meteredToString(metered[i])); pw.print(" metered="); pw.print(meteredToString(metered[i]));
pw.print(" roaming="); pw.print(roamingToString(roaming[i])); pw.print(" roaming="); pw.print(roamingToString(roaming[i]));
pw.print(" defaultNetwork="); pw.print(defaultNetworkToString(defaultNetwork[i]));
pw.print(" rxBytes="); pw.print(rxBytes[i]); pw.print(" rxBytes="); pw.print(rxBytes[i]);
pw.print(" rxPackets="); pw.print(rxPackets[i]); pw.print(" rxPackets="); pw.print(rxPackets[i]);
pw.print(" txBytes="); pw.print(txBytes[i]); pw.print(" txBytes="); pw.print(txBytes[i]);
@@ -900,6 +942,22 @@ public class NetworkStats implements Parcelable {
} }
} }
/**
* Return text description of {@link #defaultNetwork} value.
*/
public static String defaultNetworkToString(int defaultNetwork) {
switch (defaultNetwork) {
case DEFAULT_NETWORK_ALL:
return "ALL";
case DEFAULT_NETWORK_NO:
return "NO";
case DEFAULT_NETWORK_YES:
return "YES";
default:
return "UNKNOWN";
}
}
@Override @Override
public String toString() { public String toString() {
final CharArrayWriter writer = new CharArrayWriter(); final CharArrayWriter writer = new CharArrayWriter();
@@ -1055,6 +1113,7 @@ public class NetworkStats implements Parcelable {
tmpEntry.set = set[i]; tmpEntry.set = set[i];
tmpEntry.metered = metered[i]; tmpEntry.metered = metered[i];
tmpEntry.roaming = roaming[i]; tmpEntry.roaming = roaming[i];
tmpEntry.defaultNetwork = defaultNetwork[i];
combineValues(tmpEntry); combineValues(tmpEntry);
if (tag[i] == TAG_NONE) { if (tag[i] == TAG_NONE) {
moved.add(tmpEntry); moved.add(tmpEntry);
@@ -1075,6 +1134,7 @@ public class NetworkStats implements Parcelable {
moved.iface = underlyingIface; moved.iface = underlyingIface;
moved.metered = METERED_ALL; moved.metered = METERED_ALL;
moved.roaming = ROAMING_ALL; moved.roaming = ROAMING_ALL;
moved.defaultNetwork = DEFAULT_NETWORK_ALL;
combineValues(moved); combineValues(moved);
// Caveat: if the vpn software uses tag, the total tagged traffic may be greater than // Caveat: if the vpn software uses tag, the total tagged traffic may be greater than
@@ -1085,13 +1145,13 @@ public class NetworkStats implements Parcelable {
// roaming data after applying these adjustments, by checking the NetworkIdentity of the // roaming data after applying these adjustments, by checking the NetworkIdentity of the
// underlying iface. // underlying iface.
int idxVpnBackground = findIndex(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, int idxVpnBackground = findIndex(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE,
METERED_NO, ROAMING_NO); METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO);
if (idxVpnBackground != -1) { if (idxVpnBackground != -1) {
tunSubtract(idxVpnBackground, this, moved); tunSubtract(idxVpnBackground, this, moved);
} }
int idxVpnForeground = findIndex(underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE, int idxVpnForeground = findIndex(underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE,
METERED_NO, ROAMING_NO); METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO);
if (idxVpnForeground != -1) { if (idxVpnForeground != -1) {
tunSubtract(idxVpnForeground, this, moved); tunSubtract(idxVpnForeground, this, moved);
} }

View File

@@ -217,7 +217,7 @@ public class NetworkStatsFactory {
} }
NetworkStats.Entry adjust = NetworkStats.Entry adjust =
new NetworkStats.Entry(baseIface, 0, 0, 0, 0L, 0L, 0L, 0L, 0L); new NetworkStats.Entry(baseIface, 0, 0, 0, 0, 0, 0, 0L, 0L, 0L, 0L, 0L);
// Subtract any 464lat traffic seen for the root UID on the current base interface. // Subtract any 464lat traffic seen for the root UID on the current base interface.
adjust.rxBytes -= (entry.rxBytes + entry.rxPackets * IPV4V6_HEADER_DELTA); adjust.rxBytes -= (entry.rxBytes + entry.rxPackets * IPV4V6_HEADER_DELTA);
adjust.txBytes -= (entry.txBytes + entry.txPackets * IPV4V6_HEADER_DELTA); adjust.txBytes -= (entry.txBytes + entry.txPackets * IPV4V6_HEADER_DELTA);

View File

@@ -45,6 +45,7 @@ static struct {
jfieldID tag; jfieldID tag;
jfieldID metered; jfieldID metered;
jfieldID roaming; jfieldID roaming;
jfieldID defaultNetwork;
jfieldID rxBytes; jfieldID rxBytes;
jfieldID rxPackets; jfieldID rxPackets;
jfieldID txBytes; jfieldID txBytes;
@@ -246,6 +247,9 @@ static int readNetworkStatsDetail(JNIEnv* env, jclass clazz, jobject stats,
ScopedIntArrayRW roaming(env, get_int_array(env, stats, ScopedIntArrayRW roaming(env, get_int_array(env, stats,
gNetworkStatsClassInfo.roaming, size, grow)); gNetworkStatsClassInfo.roaming, size, grow));
if (roaming.get() == NULL) return -1; if (roaming.get() == NULL) return -1;
ScopedIntArrayRW defaultNetwork(env, get_int_array(env, stats,
gNetworkStatsClassInfo.defaultNetwork, size, grow));
if (defaultNetwork.get() == NULL) return -1;
ScopedLongArrayRW rxBytes(env, get_long_array(env, stats, ScopedLongArrayRW rxBytes(env, get_long_array(env, stats,
gNetworkStatsClassInfo.rxBytes, size, grow)); gNetworkStatsClassInfo.rxBytes, size, grow));
if (rxBytes.get() == NULL) return -1; if (rxBytes.get() == NULL) return -1;
@@ -269,7 +273,7 @@ static int readNetworkStatsDetail(JNIEnv* env, jclass clazz, jobject stats,
uid[i] = lines[i].uid; uid[i] = lines[i].uid;
set[i] = lines[i].set; set[i] = lines[i].set;
tag[i] = lines[i].tag; tag[i] = lines[i].tag;
// Metered and Roaming are populated in Java-land by inspecting the iface properties. // Metered, roaming and defaultNetwork are populated in Java-land.
rxBytes[i] = lines[i].rxBytes; rxBytes[i] = lines[i].rxBytes;
rxPackets[i] = lines[i].rxPackets; rxPackets[i] = lines[i].rxPackets;
txBytes[i] = lines[i].txBytes; txBytes[i] = lines[i].txBytes;
@@ -285,6 +289,8 @@ static int readNetworkStatsDetail(JNIEnv* env, jclass clazz, jobject stats,
env->SetObjectField(stats, gNetworkStatsClassInfo.tag, tag.getJavaArray()); env->SetObjectField(stats, gNetworkStatsClassInfo.tag, tag.getJavaArray());
env->SetObjectField(stats, gNetworkStatsClassInfo.metered, metered.getJavaArray()); env->SetObjectField(stats, gNetworkStatsClassInfo.metered, metered.getJavaArray());
env->SetObjectField(stats, gNetworkStatsClassInfo.roaming, roaming.getJavaArray()); env->SetObjectField(stats, gNetworkStatsClassInfo.roaming, roaming.getJavaArray());
env->SetObjectField(stats, gNetworkStatsClassInfo.defaultNetwork,
defaultNetwork.getJavaArray());
env->SetObjectField(stats, gNetworkStatsClassInfo.rxBytes, rxBytes.getJavaArray()); env->SetObjectField(stats, gNetworkStatsClassInfo.rxBytes, rxBytes.getJavaArray());
env->SetObjectField(stats, gNetworkStatsClassInfo.rxPackets, rxPackets.getJavaArray()); env->SetObjectField(stats, gNetworkStatsClassInfo.rxPackets, rxPackets.getJavaArray());
env->SetObjectField(stats, gNetworkStatsClassInfo.txBytes, txBytes.getJavaArray()); env->SetObjectField(stats, gNetworkStatsClassInfo.txBytes, txBytes.getJavaArray());
@@ -318,6 +324,7 @@ int register_com_android_internal_net_NetworkStatsFactory(JNIEnv* env) {
gNetworkStatsClassInfo.tag = GetFieldIDOrDie(env, clazz, "tag", "[I"); gNetworkStatsClassInfo.tag = GetFieldIDOrDie(env, clazz, "tag", "[I");
gNetworkStatsClassInfo.metered = GetFieldIDOrDie(env, clazz, "metered", "[I"); gNetworkStatsClassInfo.metered = GetFieldIDOrDie(env, clazz, "metered", "[I");
gNetworkStatsClassInfo.roaming = GetFieldIDOrDie(env, clazz, "roaming", "[I"); gNetworkStatsClassInfo.roaming = GetFieldIDOrDie(env, clazz, "roaming", "[I");
gNetworkStatsClassInfo.defaultNetwork = GetFieldIDOrDie(env, clazz, "defaultNetwork", "[I");
gNetworkStatsClassInfo.rxBytes = GetFieldIDOrDie(env, clazz, "rxBytes", "[J"); gNetworkStatsClassInfo.rxBytes = GetFieldIDOrDie(env, clazz, "rxBytes", "[J");
gNetworkStatsClassInfo.rxPackets = GetFieldIDOrDie(env, clazz, "rxPackets", "[J"); gNetworkStatsClassInfo.rxPackets = GetFieldIDOrDie(env, clazz, "rxPackets", "[J");
gNetworkStatsClassInfo.txBytes = GetFieldIDOrDie(env, clazz, "txBytes", "[J"); gNetworkStatsClassInfo.txBytes = GetFieldIDOrDie(env, clazz, "txBytes", "[J");

View File

@@ -4957,10 +4957,12 @@ public class ConnectivityService extends IConnectivityManager.Stub
} catch (Exception e) { } catch (Exception e) {
loge("Exception setting default network :" + e); loge("Exception setting default network :" + e);
} }
notifyLockdownVpn(newNetwork); notifyLockdownVpn(newNetwork);
handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy()); handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy());
updateTcpBufferSizes(newNetwork); updateTcpBufferSizes(newNetwork);
mDnsManager.setDefaultDnsSystemProperties(newNetwork.linkProperties.getDnsServers()); mDnsManager.setDefaultDnsSystemProperties(newNetwork.linkProperties.getDnsServers());
notifyIfacesChangedForNetworkStats();
} }
private void processListenRequests(NetworkAgentInfo nai, boolean capabilitiesChanged) { private void processListenRequests(NetworkAgentInfo nai, boolean capabilitiesChanged) {
@@ -5533,13 +5535,31 @@ public class ConnectivityService extends IConnectivityManager.Stub
notifyNetworkCallbacks(networkAgent, notifyType, 0); notifyNetworkCallbacks(networkAgent, notifyType, 0);
} }
/**
* Returns the list of all interfaces that could be used by network traffic that does not
* explicitly specify a network. This includes the default network, but also all VPNs that are
* currently connected.
*
* Must be called on the handler thread.
*/
private Network[] getDefaultNetworks() {
ArrayList<Network> defaultNetworks = new ArrayList<>();
NetworkAgentInfo defaultNetwork = getDefaultNetwork();
for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) {
if (nai.everConnected && (nai == defaultNetwork || nai.isVPN())) {
defaultNetworks.add(nai.network);
}
}
return defaultNetworks.toArray(new Network[0]);
}
/** /**
* Notify NetworkStatsService that the set of active ifaces has changed, or that one of the * Notify NetworkStatsService that the set of active ifaces has changed, or that one of the
* properties tracked by NetworkStatsService on an active iface has changed. * properties tracked by NetworkStatsService on an active iface has changed.
*/ */
private void notifyIfacesChangedForNetworkStats() { private void notifyIfacesChangedForNetworkStats() {
try { try {
mStatsService.forceUpdateIfaces(); mStatsService.forceUpdateIfaces(getDefaultNetworks());
} catch (Exception ignored) { } catch (Exception ignored) {
} }
} }
@@ -5571,7 +5591,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
success = mVpns.get(user).setUnderlyingNetworks(networks); success = mVpns.get(user).setUnderlyingNetworks(networks);
} }
if (success) { if (success) {
notifyIfacesChangedForNetworkStats(); mHandler.post(() -> notifyIfacesChangedForNetworkStats());
} }
return success; return success;
} }

View File

@@ -39,6 +39,7 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
private static final int VERSION_ADD_ROAMING = 2; private static final int VERSION_ADD_ROAMING = 2;
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;
public NetworkIdentitySet() { public NetworkIdentitySet() {
} }
@@ -76,12 +77,20 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
metered = (type == TYPE_MOBILE); metered = (type == TYPE_MOBILE);
} }
add(new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered)); final boolean defaultNetwork;
if (version >= VERSION_ADD_DEFAULT_NETWORK) {
defaultNetwork = in.readBoolean();
} else {
defaultNetwork = true;
}
add(new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered,
defaultNetwork));
} }
} }
public void writeToStream(DataOutputStream out) throws IOException { public void writeToStream(DataOutputStream out) throws IOException {
out.writeInt(VERSION_ADD_METERED); out.writeInt(VERSION_ADD_DEFAULT_NETWORK);
out.writeInt(size()); out.writeInt(size());
for (NetworkIdentity ident : this) { for (NetworkIdentity ident : this) {
out.writeInt(ident.getType()); out.writeInt(ident.getType());
@@ -90,6 +99,7 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
writeOptionalString(out, ident.getNetworkId()); writeOptionalString(out, ident.getNetworkId());
out.writeBoolean(ident.getRoaming()); out.writeBoolean(ident.getRoaming());
out.writeBoolean(ident.getMetered()); out.writeBoolean(ident.getMetered());
out.writeBoolean(ident.getDefaultNetwork());
} }
} }
@@ -119,6 +129,20 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
return false; return false;
} }
/** @return whether any {@link NetworkIdentity} in this set is considered on the default
network. */
public boolean areAllMembersOnDefaultNetwork() {
if (isEmpty()) {
return true;
}
for (NetworkIdentity ident : this) {
if (!ident.getDefaultNetwork()) {
return false;
}
}
return true;
}
private static void writeOptionalString(DataOutputStream out, String value) throws IOException { private static void writeOptionalString(DataOutputStream out, String value) throws IOException {
if (value != null) { if (value != null) {
out.writeByte(1); out.writeByte(1);

View File

@@ -1103,7 +1103,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
for (int subId : subIds) { for (int subId : subIds) {
final String subscriberId = tele.getSubscriberId(subId); final String subscriberId = tele.getSubscriberId(subId);
final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE,
TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true); TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true,
true);
if (template.matches(probeIdent)) { if (template.matches(probeIdent)) {
return true; return true;
} }
@@ -1304,7 +1305,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
// find and update the mobile NetworkPolicy for this subscriber id // find and update the mobile NetworkPolicy for this subscriber id
final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE,
TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true); TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, true);
for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) {
final NetworkTemplate template = mNetworkPolicy.keyAt(i); final NetworkTemplate template = mNetworkPolicy.keyAt(i);
if (template.matches(probeIdent)) { if (template.matches(probeIdent)) {
@@ -1511,7 +1512,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
for (int subId : subIds) { for (int subId : subIds) {
final String subscriberId = tm.getSubscriberId(subId); final String subscriberId = tm.getSubscriberId(subId);
final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE,
TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true); TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true,
true);
// Template is matched when subscriber id matches. // Template is matched when subscriber id matches.
if (template.matches(probeIdent)) { if (template.matches(probeIdent)) {
tm.setPolicyDataEnabled(enabled, subId); tm.setPolicyDataEnabled(enabled, subId);
@@ -1557,7 +1559,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
final ArrayMap<NetworkState, NetworkIdentity> identified = new ArrayMap<>(); final ArrayMap<NetworkState, NetworkIdentity> identified = new ArrayMap<>();
for (NetworkState state : states) { for (NetworkState state : states) {
if (state.networkInfo != null && state.networkInfo.isConnected()) { if (state.networkInfo != null && state.networkInfo.isConnected()) {
final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state); final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state,
true);
identified.put(state, ident); identified.put(state, ident);
} }
} }
@@ -1692,7 +1695,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
private boolean ensureActiveMobilePolicyAL(int subId, String subscriberId) { private boolean ensureActiveMobilePolicyAL(int subId, String subscriberId) {
// Poke around to see if we already have a policy // Poke around to see if we already have a policy
final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE,
TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true); TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, true);
for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) {
final NetworkTemplate template = mNetworkPolicy.keyAt(i); final NetworkTemplate template = mNetworkPolicy.keyAt(i);
if (template.matches(probeIdent)) { if (template.matches(probeIdent)) {

View File

@@ -17,6 +17,8 @@
package com.android.server.net; package com.android.server.net;
import static android.net.NetworkStats.IFACE_ALL; import static android.net.NetworkStats.IFACE_ALL;
import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
import static android.net.NetworkStats.DEFAULT_NETWORK_YES;
import static android.net.NetworkStats.METERED_NO; import static android.net.NetworkStats.METERED_NO;
import static android.net.NetworkStats.METERED_YES; import static android.net.NetworkStats.METERED_YES;
import static android.net.NetworkStats.ROAMING_NO; import static android.net.NetworkStats.ROAMING_NO;
@@ -364,6 +366,8 @@ public class NetworkStatsCollection implements FileRotator.Reader {
entry.uid = key.uid; entry.uid = key.uid;
entry.set = key.set; entry.set = key.set;
entry.tag = key.tag; entry.tag = key.tag;
entry.defaultNetwork = key.ident.areAllMembersOnDefaultNetwork() ?
DEFAULT_NETWORK_YES : DEFAULT_NETWORK_NO;
entry.metered = key.ident.isAnyMemberMetered() ? METERED_YES : METERED_NO; entry.metered = key.ident.isAnyMemberMetered() ? METERED_YES : METERED_NO;
entry.roaming = key.ident.isAnyMemberRoaming() ? ROAMING_YES : ROAMING_NO; entry.roaming = key.ident.isAnyMemberRoaming() ? ROAMING_YES : ROAMING_NO;
entry.rxBytes = historyEntry.rxBytes; entry.rxBytes = historyEntry.rxBytes;

View File

@@ -25,6 +25,7 @@ import static android.content.Intent.ACTION_USER_REMOVED;
import static android.content.Intent.EXTRA_UID; import static android.content.Intent.EXTRA_UID;
import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED; import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED;
import static android.net.ConnectivityManager.isNetworkTypeMobile; import static android.net.ConnectivityManager.isNetworkTypeMobile;
import static android.net.NetworkStats.DEFAULT_NETWORK_ALL;
import static android.net.NetworkStats.IFACE_ALL; import static android.net.NetworkStats.IFACE_ALL;
import static android.net.NetworkStats.METERED_ALL; import static android.net.NetworkStats.METERED_ALL;
import static android.net.NetworkStats.ROAMING_ALL; import static android.net.NetworkStats.ROAMING_ALL;
@@ -83,6 +84,7 @@ import android.net.INetworkManagementEventObserver;
import android.net.INetworkStatsService; import android.net.INetworkStatsService;
import android.net.INetworkStatsSession; import android.net.INetworkStatsSession;
import android.net.LinkProperties; import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkIdentity; import android.net.NetworkIdentity;
import android.net.NetworkInfo; import android.net.NetworkInfo;
@@ -231,14 +233,24 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
private final Object mStatsLock = new Object(); private final Object mStatsLock = new Object();
/** Set of currently active ifaces. */ /** Set of currently active ifaces. */
@GuardedBy("mStatsLock")
private final ArrayMap<String, NetworkIdentitySet> mActiveIfaces = new ArrayMap<>(); private final ArrayMap<String, NetworkIdentitySet> mActiveIfaces = new ArrayMap<>();
/** Set of currently active ifaces for UID stats. */ /** Set of currently active ifaces for UID stats. */
@GuardedBy("mStatsLock")
private final ArrayMap<String, NetworkIdentitySet> mActiveUidIfaces = new ArrayMap<>(); private final ArrayMap<String, NetworkIdentitySet> mActiveUidIfaces = new ArrayMap<>();
/** Current default active iface. */ /** Current default active iface. */
private String mActiveIface; private String mActiveIface;
/** Set of any ifaces associated with mobile networks since boot. */ /** Set of any ifaces associated with mobile networks since boot. */
@GuardedBy("mStatsLock")
private String[] mMobileIfaces = new String[0]; private String[] mMobileIfaces = new String[0];
/** Set of all ifaces currently used by traffic that does not explicitly specify a Network. */
@GuardedBy("mStatsLock")
private Network[] mDefaultNetworks = new Network[0];
private final DropBoxNonMonotonicObserver mNonMonotonicObserver = private final DropBoxNonMonotonicObserver mNonMonotonicObserver =
new DropBoxNonMonotonicObserver(); new DropBoxNonMonotonicObserver();
@@ -666,9 +678,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
final NetworkStatsHistory.Entry entry = history.getValues(start, end, now, null); final NetworkStatsHistory.Entry entry = history.getValues(start, end, now, null);
final NetworkStats stats = new NetworkStats(end - start, 1); final NetworkStats stats = new NetworkStats(end - start, 1);
stats.addValues(new NetworkStats.Entry(IFACE_ALL, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, stats.addValues(new NetworkStats.Entry(IFACE_ALL, UID_ALL, SET_ALL, TAG_NONE,
ROAMING_ALL, entry.rxBytes, entry.rxPackets, entry.txBytes, entry.txPackets, METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, entry.rxBytes, entry.rxPackets,
entry.operations)); entry.txBytes, entry.txPackets, entry.operations));
return stats; return stats;
} }
@@ -779,13 +791,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
@Override @Override
public void forceUpdateIfaces() { public void forceUpdateIfaces(Network[] defaultNetworks) {
mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG); mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG);
assertBandwidthControlEnabled(); assertBandwidthControlEnabled();
final long token = Binder.clearCallingIdentity(); final long token = Binder.clearCallingIdentity();
try { try {
updateIfaces(); updateIfaces(defaultNetworks);
} finally { } finally {
Binder.restoreCallingIdentity(token); Binder.restoreCallingIdentity(token);
} }
@@ -996,11 +1008,11 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
}; };
private void updateIfaces() { private void updateIfaces(Network[] defaultNetworks) {
synchronized (mStatsLock) { synchronized (mStatsLock) {
mWakeLock.acquire(); mWakeLock.acquire();
try { try {
updateIfacesLocked(); updateIfacesLocked(defaultNetworks);
} finally { } finally {
mWakeLock.release(); mWakeLock.release();
} }
@@ -1013,7 +1025,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
* are active on a single {@code iface}, they are combined under a single * are active on a single {@code iface}, they are combined under a single
* {@link NetworkIdentitySet}. * {@link NetworkIdentitySet}.
*/ */
private void updateIfacesLocked() { private void updateIfacesLocked(Network[] defaultNetworks) {
if (!mSystemReady) return; if (!mSystemReady) return;
if (LOGV) Slog.v(TAG, "updateIfacesLocked()"); if (LOGV) Slog.v(TAG, "updateIfacesLocked()");
@@ -1040,12 +1052,18 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
// Rebuild active interfaces based on connected networks // Rebuild active interfaces based on connected networks
mActiveIfaces.clear(); mActiveIfaces.clear();
mActiveUidIfaces.clear(); mActiveUidIfaces.clear();
if (defaultNetworks != null) {
// Caller is ConnectivityService. Update the list of default networks.
mDefaultNetworks = defaultNetworks;
}
final ArraySet<String> mobileIfaces = new ArraySet<>(); final ArraySet<String> mobileIfaces = new ArraySet<>();
for (NetworkState state : states) { for (NetworkState state : states) {
if (state.networkInfo.isConnected()) { if (state.networkInfo.isConnected()) {
final boolean isMobile = isNetworkTypeMobile(state.networkInfo.getType()); final boolean isMobile = isNetworkTypeMobile(state.networkInfo.getType());
final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state); final boolean isDefault = ArrayUtils.contains(mDefaultNetworks, state.network);
final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state,
isDefault);
// Traffic occurring on the base interface is always counted for // Traffic occurring on the base interface is always counted for
// both total usage and UID details. // both total usage and UID details.
@@ -1065,7 +1083,8 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
// Copy the identify from IMS one but mark it as metered. // Copy the identify from IMS one but mark it as metered.
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); ident.getRoaming(), true /* metered */,
true /* onDefaultNetwork */);
findOrCreateNetworkIdentitySet(mActiveIfaces, VT_INTERFACE).add(vtIdent); findOrCreateNetworkIdentitySet(mActiveIfaces, VT_INTERFACE).add(vtIdent);
findOrCreateNetworkIdentitySet(mActiveUidIfaces, VT_INTERFACE).add(vtIdent); findOrCreateNetworkIdentitySet(mActiveUidIfaces, VT_INTERFACE).add(vtIdent);
} }
@@ -1511,7 +1530,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
return true; return true;
} }
case MSG_UPDATE_IFACES: { case MSG_UPDATE_IFACES: {
mService.updateIfaces(); mService.updateIfaces(null);
return true; return true;
} }
case MSG_REGISTER_GLOBAL_ALERT: { case MSG_REGISTER_GLOBAL_ALERT: {

View File

@@ -16,6 +16,9 @@
package android.net; package android.net;
import static android.net.NetworkStats.DEFAULT_NETWORK_ALL;
import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
import static android.net.NetworkStats.DEFAULT_NETWORK_YES;
import static android.net.NetworkStats.METERED_ALL; import static android.net.NetworkStats.METERED_ALL;
import static android.net.NetworkStats.METERED_NO; import static android.net.NetworkStats.METERED_NO;
import static android.net.NetworkStats.METERED_YES; import static android.net.NetworkStats.METERED_YES;
@@ -56,71 +59,75 @@ public class NetworkStatsTest {
@Test @Test
public void testFindIndex() throws Exception { public void testFindIndex() throws Exception {
final NetworkStats stats = new NetworkStats(TEST_START, 5) final NetworkStats stats = new NetworkStats(TEST_START, 5)
.addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1024L, .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
8L, 0L, 0L, 10) DEFAULT_NETWORK_YES, 1024L, 8L, 0L, 0L, 10)
.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 0L, 0L, .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
1024L, 8L, 11) DEFAULT_NETWORK_NO, 0L, 0L, 1024L, 8L, 11)
.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 0L, 0L, .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO,
1024L, 8L, 11) DEFAULT_NETWORK_YES, 0L, 0L, 1024L, 8L, 11)
.addValues(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1024L, .addValues(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
8L, 1024L, 8L, 12) DEFAULT_NETWORK_NO, 1024L, 8L, 1024L, 8L, 12)
.addValues(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_YES, 1024L, .addValues(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_YES,
8L, 1024L, 8L, 12); DEFAULT_NETWORK_YES, 1024L, 8L, 1024L, 8L, 12);
assertEquals(4, stats.findIndex(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_YES, assertEquals(4, stats.findIndex(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_YES,
ROAMING_YES)); ROAMING_YES, DEFAULT_NETWORK_YES));
assertEquals(3, stats.findIndex(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_NO, assertEquals(3, stats.findIndex(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO)); ROAMING_NO, DEFAULT_NETWORK_NO));
assertEquals(2, stats.findIndex(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, assertEquals(2, stats.findIndex(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES,
ROAMING_NO)); ROAMING_NO, DEFAULT_NETWORK_YES));
assertEquals(1, stats.findIndex(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, assertEquals(1, stats.findIndex(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO)); ROAMING_NO, DEFAULT_NETWORK_NO));
assertEquals(0, stats.findIndex(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, assertEquals(0, stats.findIndex(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO)); ROAMING_NO, DEFAULT_NETWORK_YES));
assertEquals(-1, stats.findIndex(TEST_IFACE, 6, SET_DEFAULT, TAG_NONE, METERED_NO, assertEquals(-1, stats.findIndex(TEST_IFACE, 6, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO)); ROAMING_NO, DEFAULT_NETWORK_NO));
assertEquals(-1, stats.findIndex(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO, DEFAULT_NETWORK_NO));
} }
@Test @Test
public void testFindIndexHinted() { public void testFindIndexHinted() {
final NetworkStats stats = new NetworkStats(TEST_START, 3) final NetworkStats stats = new NetworkStats(TEST_START, 3)
.addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1024L, .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
8L, 0L, 0L, 10) DEFAULT_NETWORK_YES, 1024L, 8L, 0L, 0L, 10)
.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 0L, 0L, .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
1024L, 8L, 11) DEFAULT_NETWORK_NO, 0L, 0L, 1024L, 8L, 11)
.addValues(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1024L, .addValues(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
8L, 1024L, 8L, 12) DEFAULT_NETWORK_YES, 1024L, 8L, 1024L, 8L, 12)
.addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
1024L, 8L, 0L, 0L, 10) DEFAULT_NETWORK_NO, 1024L, 8L, 0L, 0L, 10)
.addValues(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 0L, 0L, .addValues(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO,
1024L, 8L, 11) DEFAULT_NETWORK_YES, 0L, 0L, 1024L, 8L, 11)
.addValues(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D, METERED_YES, ROAMING_NO, 0L, 0L, .addValues(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D, METERED_YES, ROAMING_NO,
1024L, 8L, 11) DEFAULT_NETWORK_NO, 0L, 0L, 1024L, 8L, 11)
.addValues(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1024L, .addValues(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
8L, 1024L, 8L, 12) DEFAULT_NETWORK_YES, 1024L, 8L, 1024L, 8L, 12)
.addValues(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_YES, 1024L, .addValues(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_YES,
8L, 1024L, 8L, 12); DEFAULT_NETWORK_NO, 1024L, 8L, 1024L, 8L, 12);
// verify that we correctly find across regardless of hinting // verify that we correctly find across regardless of hinting
for (int hint = 0; hint < stats.size(); hint++) { for (int hint = 0; hint < stats.size(); hint++) {
assertEquals(0, stats.findIndexHinted(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, assertEquals(0, stats.findIndexHinted(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE,
METERED_NO, ROAMING_NO, hint)); METERED_NO, ROAMING_NO, DEFAULT_NETWORK_YES, hint));
assertEquals(1, stats.findIndexHinted(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, assertEquals(1, stats.findIndexHinted(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE,
METERED_NO, ROAMING_NO, hint)); METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, hint));
assertEquals(2, stats.findIndexHinted(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, assertEquals(2, stats.findIndexHinted(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE,
METERED_NO, ROAMING_NO, hint)); METERED_NO, ROAMING_NO, DEFAULT_NETWORK_YES, hint));
assertEquals(3, stats.findIndexHinted(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, assertEquals(3, stats.findIndexHinted(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE,
METERED_NO, ROAMING_NO, hint)); METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, hint));
assertEquals(4, stats.findIndexHinted(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D, assertEquals(4, stats.findIndexHinted(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D,
METERED_NO, ROAMING_NO, hint)); METERED_NO, ROAMING_NO, DEFAULT_NETWORK_YES, hint));
assertEquals(5, stats.findIndexHinted(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D, assertEquals(5, stats.findIndexHinted(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D,
METERED_YES, ROAMING_NO, hint)); METERED_YES, ROAMING_NO, DEFAULT_NETWORK_NO, hint));
assertEquals(6, stats.findIndexHinted(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE, assertEquals(6, stats.findIndexHinted(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE,
METERED_NO, ROAMING_NO, hint)); METERED_NO, ROAMING_NO, DEFAULT_NETWORK_YES, hint));
assertEquals(7, stats.findIndexHinted(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE, assertEquals(7, stats.findIndexHinted(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE,
METERED_YES, ROAMING_YES, hint)); METERED_YES, ROAMING_YES, DEFAULT_NETWORK_NO, hint));
assertEquals(-1, stats.findIndexHinted(TEST_IFACE, 6, SET_DEFAULT, TAG_NONE, assertEquals(-1, stats.findIndexHinted(TEST_IFACE, 6, SET_DEFAULT, TAG_NONE,
METERED_NO, ROAMING_NO, hint)); METERED_NO, ROAMING_NO, DEFAULT_NETWORK_YES, hint));
assertEquals(-1, stats.findIndexHinted(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE,
METERED_YES, ROAMING_YES, DEFAULT_NETWORK_YES, hint));
} }
} }
@@ -131,50 +138,50 @@ public class NetworkStatsTest {
assertEquals(0, stats.size()); assertEquals(0, stats.size());
assertEquals(4, stats.internalSize()); assertEquals(4, stats.internalSize());
stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1L, 1L, stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
2L, 2L, 3); DEFAULT_NETWORK_YES, 1L, 1L, 2L, 2L, 3);
stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 2L, 2L, stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
2L, 2L, 4); DEFAULT_NETWORK_NO, 2L, 2L, 2L, 2L, 4);
stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES, 3L, stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES,
3L, 2L, 2L, 5); DEFAULT_NETWORK_YES, 3L, 3L, 2L, 2L, 5);
stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_YES, 3L, stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_YES,
3L, 2L, 2L, 5); DEFAULT_NETWORK_NO, 3L, 3L, 2L, 2L, 5);
assertEquals(4, stats.size()); assertEquals(4, stats.size());
assertEquals(4, stats.internalSize()); assertEquals(4, stats.internalSize());
stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 4L, stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
40L, 4L, 40L, 7); DEFAULT_NETWORK_NO, 4L, 40L, 4L, 40L, 7);
stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 5L, stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
50L, 4L, 40L, 8); DEFAULT_NETWORK_YES, 5L, 50L, 4L, 40L, 8);
stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 6L, stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
60L, 5L, 50L, 10); DEFAULT_NETWORK_NO, 6L, 60L, 5L, 50L, 10);
stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES, 7L, stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES,
70L, 5L, 50L, 11); DEFAULT_NETWORK_YES, 7L, 70L, 5L, 50L, 11);
stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_YES, 7L, stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_YES,
70L, 5L, 50L, 11); DEFAULT_NETWORK_NO, 7L, 70L, 5L, 50L, 11);
assertEquals(9, stats.size()); assertEquals(9, stats.size());
assertTrue(stats.internalSize() >= 9); assertTrue(stats.internalSize() >= 9);
assertValues(stats, 0, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(stats, 0, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
1L, 1L, 2L, 2L, 3); DEFAULT_NETWORK_YES, 1L, 1L, 2L, 2L, 3);
assertValues(stats, 1, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(stats, 1, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
2L, 2L, 2L, 2L, 4); DEFAULT_NETWORK_NO, 2L, 2L, 2L, 2L, 4);
assertValues(stats, 2, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES, assertValues(stats, 2, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES,
3L, 3L, 2L, 2L, 5); DEFAULT_NETWORK_YES, 3L, 3L, 2L, 2L, 5);
assertValues(stats, 3, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_YES, assertValues(stats, 3, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_YES,
ROAMING_YES, 3L, 3L, 2L, 2L, 5); ROAMING_YES, DEFAULT_NETWORK_NO, 3L, 3L, 2L, 2L, 5);
assertValues(stats, 4, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(stats, 4, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
4L, 40L, 4L, 40L, 7); DEFAULT_NETWORK_NO, 4L, 40L, 4L, 40L, 7);
assertValues(stats, 5, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(stats, 5, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
5L, 50L, 4L, 40L, 8); DEFAULT_NETWORK_YES, 5L, 50L, 4L, 40L, 8);
assertValues(stats, 6, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(stats, 6, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
6L, 60L, 5L, 50L, 10); DEFAULT_NETWORK_NO, 6L, 60L, 5L, 50L, 10);
assertValues(stats, 7, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES, assertValues(stats, 7, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES,
7L, 70L, 5L, 50L, 11); DEFAULT_NETWORK_YES, 7L, 70L, 5L, 50L, 11);
assertValues(stats, 8, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_YES, assertValues(stats, 8, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_YES,
ROAMING_YES, 7L, 70L, 5L, 50L, 11); ROAMING_YES, DEFAULT_NETWORK_NO, 7L, 70L, 5L, 50L, 11);
} }
@Test @Test
@@ -187,17 +194,17 @@ public class NetworkStatsTest {
-128L, -1L, -1); -128L, -1L, -1);
assertValues(stats, 0, TEST_IFACE, 1001, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(stats, 0, TEST_IFACE, 1001, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
384L, 3L, 128L, 1L, 9); DEFAULT_NETWORK_NO, 384L, 3L, 128L, 1L, 9);
assertValues(stats, 1, TEST_IFACE, 1001, SET_DEFAULT, 0xff, METERED_NO, ROAMING_NO, 128L, assertValues(stats, 1, TEST_IFACE, 1001, SET_DEFAULT, 0xff, METERED_NO, ROAMING_NO,
1L, 128L, 1L, 2); DEFAULT_NETWORK_NO, 128L, 1L, 128L, 1L, 2);
// now try combining that should create row // now try combining that should create row
stats.combineValues(TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 3); stats.combineValues(TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 3);
assertValues(stats, 2, TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(stats, 2, TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
128L, 1L, 128L, 1L, 3); DEFAULT_NETWORK_NO, 128L, 1L, 128L, 1L, 3);
stats.combineValues(TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 3); stats.combineValues(TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 3);
assertValues(stats, 2, TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(stats, 2, TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
256L, 2L, 256L, 2L, 6); DEFAULT_NETWORK_NO, 256L, 2L, 256L, 2L, 6);
} }
@Test @Test
@@ -213,10 +220,10 @@ public class NetworkStatsTest {
final NetworkStats result = after.subtract(before); final NetworkStats result = after.subtract(before);
// identical data should result in zero delta // identical data should result in zero delta
assertValues(result, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 0L, assertValues(result, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L, 0); DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0);
assertValues(result, 1, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 0L, assertValues(result, 1, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L, 0); DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0);
} }
@Test @Test
@@ -232,10 +239,10 @@ public class NetworkStatsTest {
final NetworkStats result = after.subtract(before); final NetworkStats result = after.subtract(before);
// expect delta between measurements // expect delta between measurements
assertValues(result, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1L, assertValues(result, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
1L, 2L, 1L, 4); DEFAULT_NETWORK_NO, 1L, 1L, 2L, 1L, 4);
assertValues(result, 1, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 3L, assertValues(result, 1, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
1L, 4L, 1L, 8); DEFAULT_NETWORK_NO, 3L, 1L, 4L, 1L, 8);
} }
@Test @Test
@@ -252,12 +259,12 @@ public class NetworkStatsTest {
final NetworkStats result = after.subtract(before); final NetworkStats result = after.subtract(before);
// its okay to have new rows // its okay to have new rows
assertValues(result, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 0L, assertValues(result, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L, 0); DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0);
assertValues(result, 1, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 0L, assertValues(result, 1, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L, 0); DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0);
assertValues(result, 2, TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(result, 2, TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
1024L, 8L, 1024L, 8L, 20); DEFAULT_NETWORK_NO, 1024L, 8L, 1024L, 8L, 20);
} }
@Test @Test
@@ -274,7 +281,7 @@ public class NetworkStatsTest {
// should silently drop omitted rows // should silently drop omitted rows
assertEquals(1, result.size()); assertEquals(1, result.size());
assertValues(result, 0, TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, METERED_NO, assertValues(result, 0, TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO, 1L, 2L, 3L, 4L, 0); ROAMING_NO, DEFAULT_NETWORK_NO, 1L, 2L, 3L, 4L, 0);
assertEquals(4L, result.getTotalBytes()); assertEquals(4L, result.getTotalBytes());
} }
@@ -301,21 +308,21 @@ public class NetworkStatsTest {
assertEquals(64L, uidTag.getTotalBytes()); assertEquals(64L, uidTag.getTotalBytes());
final NetworkStats uidMetered = new NetworkStats(TEST_START, 3) final NetworkStats uidMetered = new NetworkStats(TEST_START, 3)
.addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 32L, 0L, .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L) DEFAULT_NETWORK_YES, 32L, 0L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 32L, 0L, .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO,
0L, 0L, 0L) DEFAULT_NETWORK_NO, 32L, 0L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 32L, 0L, .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO,
0L, 0L, 0L); DEFAULT_NETWORK_YES, 32L, 0L, 0L, 0L, 0L);
assertEquals(96L, uidMetered.getTotalBytes()); assertEquals(96L, uidMetered.getTotalBytes());
final NetworkStats uidRoaming = new NetworkStats(TEST_START, 3) final NetworkStats uidRoaming = new NetworkStats(TEST_START, 3)
.addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 32L, 0L, .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L) DEFAULT_NETWORK_YES, 32L, 0L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 32L, 0L, .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO,
0L, 0L, 0L) DEFAULT_NETWORK_NO, 32L, 0L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES, 32L, 0L, .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES,
0L, 0L, 0L); DEFAULT_NETWORK_YES, 32L, 0L, 0L, 0L, 0L);
assertEquals(96L, uidRoaming.getTotalBytes()); assertEquals(96L, uidRoaming.getTotalBytes());
} }
@@ -331,38 +338,38 @@ public class NetworkStatsTest {
@Test @Test
public void testGroupedByIfaceAll() throws Exception { public void testGroupedByIfaceAll() throws Exception {
final NetworkStats uidStats = new NetworkStats(TEST_START, 3) final NetworkStats uidStats = new NetworkStats(TEST_START, 3)
.addValues(IFACE_ALL, 100, SET_ALL, TAG_NONE, METERED_NO, ROAMING_NO, 128L, 8L, 0L, .addValues(IFACE_ALL, 100, SET_ALL, TAG_NONE, METERED_NO, ROAMING_NO,
2L, 20L) DEFAULT_NETWORK_YES, 128L, 8L, 0L, 2L, 20L)
.addValues(IFACE_ALL, 101, SET_FOREGROUND, TAG_NONE, METERED_YES, ROAMING_NO, 128L, .addValues(IFACE_ALL, 101, SET_FOREGROUND, TAG_NONE, METERED_YES, ROAMING_NO,
8L, 0L, 2L, 20L) DEFAULT_NETWORK_NO, 128L, 8L, 0L, 2L, 20L)
.addValues(IFACE_ALL, 101, SET_ALL, TAG_NONE, METERED_NO, ROAMING_YES, 128L, 8L, 0L, .addValues(IFACE_ALL, 101, SET_ALL, TAG_NONE, METERED_NO, ROAMING_YES,
2L, 20L); DEFAULT_NETWORK_YES, 128L, 8L, 0L, 2L, 20L);
final NetworkStats grouped = uidStats.groupedByIface(); final NetworkStats grouped = uidStats.groupedByIface();
assertEquals(3, uidStats.size()); assertEquals(3, uidStats.size());
assertEquals(1, grouped.size()); assertEquals(1, grouped.size());
assertValues(grouped, 0, IFACE_ALL, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL, assertValues(grouped, 0, IFACE_ALL, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL,
384L, 24L, 0L, 6L, 0L); DEFAULT_NETWORK_ALL, 384L, 24L, 0L, 6L, 0L);
} }
@Test @Test
public void testGroupedByIface() throws Exception { public void testGroupedByIface() throws Exception {
final NetworkStats uidStats = new NetworkStats(TEST_START, 7) final NetworkStats uidStats = new NetworkStats(TEST_START, 7)
.addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 128L, 8L, .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 2L, 20L) DEFAULT_NETWORK_YES, 128L, 8L, 0L, 2L, 20L)
.addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 512L, .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
32L, 0L, 0L, 0L) DEFAULT_NETWORK_NO, 512L, 32L, 0L, 0L, 0L)
.addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 64L, 4L, .addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO,
0L, 0L, 0L) DEFAULT_NETWORK_YES, 64L, 4L, 0L, 0L, 0L)
.addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 512L, .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
32L, 0L, 0L, 0L) DEFAULT_NETWORK_NO, 512L, 32L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 128L, 8L, .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L) DEFAULT_NETWORK_YES, 128L, 8L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, METERED_YES, ROAMING_NO, 128L, 8L, .addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, METERED_YES, ROAMING_NO,
0L, 0L, 0L) DEFAULT_NETWORK_NO, 128L, 8L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES, 128L, .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES,
8L, 0L, 0L, 0L); DEFAULT_NETWORK_YES, 128L, 8L, 0L, 0L, 0L);
final NetworkStats grouped = uidStats.groupedByIface(); final NetworkStats grouped = uidStats.groupedByIface();
@@ -370,59 +377,59 @@ public class NetworkStatsTest {
assertEquals(2, grouped.size()); assertEquals(2, grouped.size());
assertValues(grouped, 0, TEST_IFACE, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL, assertValues(grouped, 0, TEST_IFACE, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL,
384L, 24L, 0L, 2L, 0L); DEFAULT_NETWORK_ALL, 384L, 24L, 0L, 2L, 0L);
assertValues(grouped, 1, TEST_IFACE2, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL, assertValues(grouped, 1, TEST_IFACE2, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL,
1024L, 64L, 0L, 0L, 0L); DEFAULT_NETWORK_ALL, 1024L, 64L, 0L, 0L, 0L);
} }
@Test @Test
public void testAddAllValues() { public void testAddAllValues() {
final NetworkStats first = new NetworkStats(TEST_START, 5) final NetworkStats first = new NetworkStats(TEST_START, 5)
.addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 32L, 0L, .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO,
0L, 0L, 0L) DEFAULT_NETWORK_YES, 32L, 0L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 32L, .addValues(TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L, 0L) DEFAULT_NETWORK_NO, 32L, 0L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_YES, ROAMING_YES, 32L, .addValues(TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_YES, ROAMING_YES,
0L, 0L, 0L, 0L); DEFAULT_NETWORK_YES, 32L, 0L, 0L, 0L, 0L);
final NetworkStats second = new NetworkStats(TEST_START, 2) final NetworkStats second = new NetworkStats(TEST_START, 2)
.addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 32L, 0L, .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO,
0L, 0L, 0L) DEFAULT_NETWORK_YES, 32L, 0L, 0L, 0L, 0L)
.addValues(TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 32L, .addValues(TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L, 0L) DEFAULT_NETWORK_NO, 32L, 0L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_YES, ROAMING_YES, 32L, .addValues(TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_YES, ROAMING_YES,
0L, 0L, 0L, 0L); DEFAULT_NETWORK_YES, 32L, 0L, 0L, 0L, 0L);
first.combineAllValues(second); first.combineAllValues(second);
assertEquals(4, first.size()); assertEquals(4, first.size());
assertValues(first, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 64L, assertValues(first, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO,
0L, 0L, 0L, 0L); DEFAULT_NETWORK_YES, 64L, 0L, 0L, 0L, 0L);
assertValues(first, 1, TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(first, 1, TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
32L, 0L, 0L, 0L, 0L); DEFAULT_NETWORK_NO, 32L, 0L, 0L, 0L, 0L);
assertValues(first, 2, TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_YES, ROAMING_YES, assertValues(first, 2, TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_YES, ROAMING_YES,
64L, 0L, 0L, 0L, 0L); DEFAULT_NETWORK_YES, 64L, 0L, 0L, 0L, 0L);
assertValues(first, 3, TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(first, 3, TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
32L, 0L, 0L, 0L, 0L); DEFAULT_NETWORK_NO, 32L, 0L, 0L, 0L, 0L);
} }
@Test @Test
public void testGetTotal() { public void testGetTotal() {
final NetworkStats stats = new NetworkStats(TEST_START, 7) final NetworkStats stats = new NetworkStats(TEST_START, 7)
.addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 128L, 8L, .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 2L, 20L) DEFAULT_NETWORK_YES, 128L, 8L, 0L, 2L, 20L)
.addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 512L, .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
32L, 0L, 0L, 0L) DEFAULT_NETWORK_NO, 512L, 32L, 0L, 0L, 0L)
.addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 64L, 4L, .addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO,
0L, 0L, 0L) DEFAULT_NETWORK_YES, 64L, 4L, 0L, 0L, 0L)
.addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 512L, .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
32L, 0L, 0L, 0L) DEFAULT_NETWORK_NO, 512L,32L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 128L, .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO,
8L, 0L, 0L, 0L) DEFAULT_NETWORK_YES, 128L, 8L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 128L, 8L, .addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO,
0L, 0L, 0L) DEFAULT_NETWORK_NO, 128L, 8L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES, 128L, .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES,
8L, 0L, 0L, 0L); DEFAULT_NETWORK_NO, 128L, 8L, 0L, 0L, 0L);
assertValues(stats.getTotal(null), 1408L, 88L, 0L, 2L, 20L); assertValues(stats.getTotal(null), 1408L, 88L, 0L, 2L, 20L);
assertValues(stats.getTotal(null, 100), 1280L, 80L, 0L, 2L, 20L); assertValues(stats.getTotal(null, 100), 1280L, 80L, 0L, 2L, 20L);
@@ -449,9 +456,9 @@ public class NetworkStatsTest {
assertEquals(6, before.size()); assertEquals(6, before.size());
assertEquals(2, after.size()); assertEquals(2, after.size());
assertValues(after, 0, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(after, 0, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
128L, 8L, 0L, 0L, 0L); DEFAULT_NETWORK_NO, 128L, 8L, 0L, 0L, 0L);
assertValues(after, 1, TEST_IFACE, 101, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 128L, assertValues(after, 1, TEST_IFACE, 101, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO,
8L, 0L, 0L, 0L); DEFAULT_NETWORK_NO, 128L, 8L, 0L, 0L, 0L);
} }
@Test @Test
@@ -489,90 +496,90 @@ public class NetworkStatsTest {
final String underlyingIface = "wlan0"; final String underlyingIface = "wlan0";
final int testTag1 = 8888; final int testTag1 = 8888;
NetworkStats delta = new NetworkStats(TEST_START, 17) NetworkStats delta = new NetworkStats(TEST_START, 17)
.addValues(tunIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 39605L, 46L, .addValues(tunIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
12259L, 55L, 0L) DEFAULT_NETWORK_NO, 39605L, 46L, 12259L, 55L, 0L)
.addValues(tunIface, 10100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 0L, 0L, .addValues(tunIface, 10100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L) DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0L)
.addValues(tunIface, 10120, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 72667L, 197L, .addValues(tunIface, 10120, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
43909L, 241L, 0L) DEFAULT_NETWORK_NO, 72667L, 197L, 43909L, 241L, 0L)
.addValues(tunIface, 10120, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 9297L, .addValues(tunIface, 10120, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
17L, 4128L, 21L, 0L) DEFAULT_NETWORK_NO, 9297L, 17L, 4128L, 21L, 0L)
// VPN package also uses some traffic through unprotected network. // VPN package also uses some traffic through unprotected network.
.addValues(tunIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 4983L, 10L, .addValues(tunIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
1801L, 12L, 0L) DEFAULT_NETWORK_NO, 4983L, 10L, 1801L, 12L, 0L)
.addValues(tunIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 0L, 0L, .addValues(tunIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L) DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0L)
// Tag entries // Tag entries
.addValues(tunIface, 10120, SET_DEFAULT, testTag1, METERED_NO, ROAMING_NO, 21691L, 41L, .addValues(tunIface, 10120, SET_DEFAULT, testTag1, METERED_NO, ROAMING_NO,
13820L, 51L, 0L) DEFAULT_NETWORK_NO, 21691L, 41L, 13820L, 51L, 0L)
.addValues(tunIface, 10120, SET_FOREGROUND, testTag1, METERED_NO, ROAMING_NO, 1281L, 2L, .addValues(tunIface, 10120, SET_FOREGROUND, testTag1, METERED_NO, ROAMING_NO,
665L, 2L, 0L) DEFAULT_NETWORK_NO, 1281L, 2L, 665L, 2L, 0L)
// Irrelevant entries // Irrelevant entries
.addValues(TEST_IFACE, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1685L, 5L, .addValues(TEST_IFACE, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
2070L, 6L, 0L) DEFAULT_NETWORK_NO, 1685L, 5L, 2070L, 6L, 0L)
// Underlying Iface entries // Underlying Iface entries
.addValues(underlyingIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 5178L, .addValues(underlyingIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
8L, 2139L, 11L, 0L) DEFAULT_NETWORK_NO, 5178L, 8L, 2139L, 11L, 0L)
.addValues(underlyingIface, 10100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 0L, .addValues(underlyingIface, 10100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L, 0L) DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0L)
.addValues(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, .addValues(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
149873L, 287L, 59217L /* smaller than sum(tun0) */, DEFAULT_NETWORK_NO, 149873L, 287L, 59217L /* smaller than sum(tun0) */,
299L /* smaller than sum(tun0) */, 0L) 299L /* smaller than sum(tun0) */, 0L)
.addValues(underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, .addValues(underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L, 0L, 0L); DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0L);
assertTrue(delta.migrateTun(tunUid, tunIface, underlyingIface)); assertTrue(delta.toString(), delta.migrateTun(tunUid, tunIface, underlyingIface));
assertEquals(20, delta.size()); assertEquals(20, delta.size());
// tunIface and TEST_IFACE entries are not changed. // tunIface and TEST_IFACE entries are not changed.
assertValues(delta, 0, tunIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(delta, 0, tunIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
39605L, 46L, 12259L, 55L, 0L); DEFAULT_NETWORK_NO, 39605L, 46L, 12259L, 55L, 0L);
assertValues(delta, 1, tunIface, 10100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(delta, 1, tunIface, 10100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L, 0L, 0L); DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0L);
assertValues(delta, 2, tunIface, 10120, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(delta, 2, tunIface, 10120, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
72667L, 197L, 43909L, 241L, 0L); DEFAULT_NETWORK_NO, 72667L, 197L, 43909L, 241L, 0L);
assertValues(delta, 3, tunIface, 10120, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(delta, 3, tunIface, 10120, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
9297L, 17L, 4128L, 21L, 0L); DEFAULT_NETWORK_NO, 9297L, 17L, 4128L, 21L, 0L);
assertValues(delta, 4, tunIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(delta, 4, tunIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
4983L, 10L, 1801L, 12L, 0L); DEFAULT_NETWORK_NO, 4983L, 10L, 1801L, 12L, 0L);
assertValues(delta, 5, tunIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(delta, 5, tunIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
0L, 0L, 0L, 0L, 0L); DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0L);
assertValues(delta, 6, tunIface, 10120, SET_DEFAULT, testTag1, METERED_NO, ROAMING_NO, assertValues(delta, 6, tunIface, 10120, SET_DEFAULT, testTag1, METERED_NO, ROAMING_NO,
21691L, 41L, 13820L, 51L, 0L); DEFAULT_NETWORK_NO, 21691L, 41L, 13820L, 51L, 0L);
assertValues(delta, 7, tunIface, 10120, SET_FOREGROUND, testTag1, METERED_NO, ROAMING_NO, assertValues(delta, 7, tunIface, 10120, SET_FOREGROUND, testTag1, METERED_NO, ROAMING_NO,
1281L, 2L, 665L, 2L, 0L); DEFAULT_NETWORK_NO, 1281L, 2L, 665L, 2L, 0L);
assertValues(delta, 8, TEST_IFACE, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(delta, 8, TEST_IFACE, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
1685L, 5L, 2070L, 6L, 0L); DEFAULT_NETWORK_NO, 1685L, 5L, 2070L, 6L, 0L);
// Existing underlying Iface entries are updated // Existing underlying Iface entries are updated
assertValues(delta, 9, underlyingIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, assertValues(delta, 9, underlyingIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO, 44783L, 54L, 14178L, 62L, 0L); ROAMING_NO, DEFAULT_NETWORK_NO, 44783L, 54L, 14178L, 62L, 0L);
assertValues(delta, 10, underlyingIface, 10100, SET_FOREGROUND, TAG_NONE, METERED_NO, assertValues(delta, 10, underlyingIface, 10100, SET_FOREGROUND, TAG_NONE, METERED_NO,
ROAMING_NO, 0L, 0L, 0L, 0L, 0L); ROAMING_NO, DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0L);
// VPN underlying Iface entries are updated // VPN underlying Iface entries are updated
assertValues(delta, 11, underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, assertValues(delta, 11, underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO, 28304L, 27L, 1L, 2L, 0L); ROAMING_NO, DEFAULT_NETWORK_NO, 28304L, 27L, 1L, 2L, 0L);
assertValues(delta, 12, underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO, assertValues(delta, 12, underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO,
ROAMING_NO, 0L, 0L, 0L, 0L, 0L); ROAMING_NO, DEFAULT_NETWORK_NO, 0L, 0L, 0L, 0L, 0L);
// New entries are added for new application's underlying Iface traffic // New entries are added for new application's underlying Iface traffic
assertContains(delta, underlyingIface, 10120, SET_DEFAULT, TAG_NONE, METERED_NO, assertContains(delta, underlyingIface, 10120, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO, 72667L, 197L, 43123L, 227L, 0L); ROAMING_NO, DEFAULT_NETWORK_NO, 72667L, 197L, 43123L, 227L, 0L);
assertContains(delta, underlyingIface, 10120, SET_FOREGROUND, TAG_NONE, METERED_NO, assertContains(delta, underlyingIface, 10120, SET_FOREGROUND, TAG_NONE, METERED_NO,
ROAMING_NO, 9297L, 17L, 4054, 19L, 0L); ROAMING_NO, DEFAULT_NETWORK_NO, 9297L, 17L, 4054, 19L, 0L);
assertContains(delta, underlyingIface, 10120, SET_DEFAULT, testTag1, METERED_NO, assertContains(delta, underlyingIface, 10120, SET_DEFAULT, testTag1, METERED_NO,
ROAMING_NO, 21691L, 41L, 13572L, 48L, 0L); ROAMING_NO, DEFAULT_NETWORK_NO, 21691L, 41L, 13572L, 48L, 0L);
assertContains(delta, underlyingIface, 10120, SET_FOREGROUND, testTag1, METERED_NO, assertContains(delta, underlyingIface, 10120, SET_FOREGROUND, testTag1, METERED_NO,
ROAMING_NO, 1281L, 2L, 653L, 1L, 0L); ROAMING_NO, DEFAULT_NETWORK_NO, 1281L, 2L, 653L, 1L, 0L);
// New entries are added for debug purpose // New entries are added for debug purpose
assertContains(delta, underlyingIface, 10100, SET_DBG_VPN_IN, TAG_NONE, METERED_NO, assertContains(delta, underlyingIface, 10100, SET_DBG_VPN_IN, TAG_NONE, METERED_NO,
ROAMING_NO, 39605L, 46L, 12039, 51, 0); ROAMING_NO, DEFAULT_NETWORK_NO, 39605L, 46L, 12039, 51, 0);
assertContains(delta, underlyingIface, 10120, SET_DBG_VPN_IN, TAG_NONE, METERED_NO, assertContains(delta, underlyingIface, 10120, SET_DBG_VPN_IN, TAG_NONE, METERED_NO,
ROAMING_NO, 81964, 214, 47177, 246, 0); ROAMING_NO, DEFAULT_NETWORK_NO, 81964, 214, 47177, 246, 0);
assertContains(delta, underlyingIface, tunUid, SET_DBG_VPN_OUT, TAG_NONE, METERED_ALL, assertContains(delta, underlyingIface, tunUid, SET_DBG_VPN_OUT, TAG_NONE, METERED_ALL,
ROAMING_ALL, 121569, 260, 59216, 297, 0); ROAMING_ALL, DEFAULT_NETWORK_ALL, 121569, 260, 59216, 297, 0);
} }
@@ -587,79 +594,80 @@ public class NetworkStatsTest {
final String underlyingIface = "wlan0"; final String underlyingIface = "wlan0";
NetworkStats delta = new NetworkStats(TEST_START, 9) NetworkStats delta = new NetworkStats(TEST_START, 9)
// 2 different apps sent/receive data via tun0. // 2 different apps sent/receive data via tun0.
.addValues(tunIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 50000L, 25L, .addValues(tunIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
100000L, 50L, 0L) DEFAULT_NETWORK_NO, 50000L, 25L, 100000L, 50L, 0L)
.addValues(tunIface, 20100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 500L, 2L, .addValues(tunIface, 20100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
200L, 5L, 0L) DEFAULT_NETWORK_NO, 500L, 2L, 200L, 5L, 0L)
// VPN package resends data through the tunnel (with exaggerated overhead) // VPN package resends data through the tunnel (with exaggerated overhead)
.addValues(tunIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 240000, .addValues(tunIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
100L, 120000L, 60L, 0L) DEFAULT_NETWORK_NO, 240000, 100L, 120000L, 60L, 0L)
// 1 app already has some traffic on the underlying interface, the other doesn't yet // 1 app already has some traffic on the underlying interface, the other doesn't yet
.addValues(underlyingIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1000L, .addValues(underlyingIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
10L, 2000L, 20L, 0L) DEFAULT_NETWORK_NO, 1000L, 10L, 2000L, 20L, 0L)
// Traffic through the underlying interface via the vpn app. // Traffic through the underlying interface via the vpn app.
// This test should redistribute this data correctly. // This test should redistribute this data correctly.
.addValues(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, .addValues(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
75500L, 37L, 130000L, 70L, 0L); DEFAULT_NETWORK_NO, 75500L, 37L, 130000L, 70L, 0L);
assertTrue(delta.migrateTun(tunUid, tunIface, underlyingIface)); assertTrue(delta.migrateTun(tunUid, tunIface, underlyingIface));
assertEquals(9, delta.size()); assertEquals(9, delta.size());
// tunIface entries should not be changed. // tunIface entries should not be changed.
assertValues(delta, 0, tunIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(delta, 0, tunIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
50000L, 25L, 100000L, 50L, 0L); DEFAULT_NETWORK_NO, 50000L, 25L, 100000L, 50L, 0L);
assertValues(delta, 1, tunIface, 20100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(delta, 1, tunIface, 20100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
500L, 2L, 200L, 5L, 0L); DEFAULT_NETWORK_NO, 500L, 2L, 200L, 5L, 0L);
assertValues(delta, 2, tunIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(delta, 2, tunIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
240000L, 100L, 120000L, 60L, 0L); DEFAULT_NETWORK_NO, 240000L, 100L, 120000L, 60L, 0L);
// Existing underlying Iface entries are updated // Existing underlying Iface entries are updated
assertValues(delta, 3, underlyingIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, assertValues(delta, 3, underlyingIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO, 51000L, 35L, 102000L, 70L, 0L); ROAMING_NO, DEFAULT_NETWORK_NO, 51000L, 35L, 102000L, 70L, 0L);
// VPN underlying Iface entries are updated // VPN underlying Iface entries are updated
assertValues(delta, 4, underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, assertValues(delta, 4, underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO, 25000L, 10L, 29800L, 15L, 0L); ROAMING_NO, DEFAULT_NETWORK_NO, 25000L, 10L, 29800L, 15L, 0L);
// New entries are added for new application's underlying Iface traffic // New entries are added for new application's underlying Iface traffic
assertContains(delta, underlyingIface, 20100, SET_DEFAULT, TAG_NONE, METERED_NO, assertContains(delta, underlyingIface, 20100, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO, 500L, 2L, 200L, 5L, 0L); ROAMING_NO, DEFAULT_NETWORK_NO, 500L, 2L, 200L, 5L, 0L);
// New entries are added for debug purpose // New entries are added for debug purpose
assertContains(delta, underlyingIface, 10100, SET_DBG_VPN_IN, TAG_NONE, METERED_NO, assertContains(delta, underlyingIface, 10100, SET_DBG_VPN_IN, TAG_NONE, METERED_NO,
ROAMING_NO, 50000L, 25L, 100000L, 50L, 0L); ROAMING_NO, DEFAULT_NETWORK_NO, 50000L, 25L, 100000L, 50L, 0L);
assertContains(delta, underlyingIface, 20100, SET_DBG_VPN_IN, TAG_NONE, METERED_NO, assertContains(delta, underlyingIface, 20100, SET_DBG_VPN_IN, TAG_NONE, METERED_NO,
ROAMING_NO, 500, 2L, 200L, 5L, 0L); ROAMING_NO, DEFAULT_NETWORK_NO, 500, 2L, 200L, 5L, 0L);
assertContains(delta, underlyingIface, tunUid, SET_DBG_VPN_OUT, TAG_NONE, METERED_ALL, assertContains(delta, underlyingIface, tunUid, SET_DBG_VPN_OUT, TAG_NONE, METERED_ALL,
ROAMING_ALL, 50500L, 27L, 100200L, 55, 0); ROAMING_ALL, DEFAULT_NETWORK_ALL, 50500L, 27L, 100200L, 55, 0);
} }
private static void assertContains(NetworkStats stats, String iface, int uid, int set, private static void assertContains(NetworkStats stats, String iface, int uid, int set,
int tag, int metered, int roaming, long rxBytes, long rxPackets, long txBytes, int tag, int metered, int roaming, int defaultNetwork, long rxBytes, long rxPackets,
long txPackets, long operations) { long txBytes, long txPackets, long operations) {
int index = stats.findIndex(iface, uid, set, tag, metered, roaming); int index = stats.findIndex(iface, uid, set, tag, metered, roaming, defaultNetwork);
assertTrue(index != -1); assertTrue(index != -1);
assertValues(stats, index, iface, uid, set, tag, metered, roaming, assertValues(stats, index, iface, uid, set, tag, metered, roaming, defaultNetwork,
rxBytes, rxPackets, txBytes, txPackets, operations); rxBytes, rxPackets, txBytes, txPackets, operations);
} }
private static void assertValues(NetworkStats stats, int index, String iface, int uid, int set, private static void assertValues(NetworkStats stats, int index, String iface, int uid, int set,
int tag, int metered, int roaming, long rxBytes, long rxPackets, long txBytes, int tag, int metered, int roaming, int defaultNetwork, long rxBytes, long rxPackets,
long txPackets, long operations) { long txBytes, long txPackets, long operations) {
final NetworkStats.Entry entry = stats.getValues(index, null); final NetworkStats.Entry entry = stats.getValues(index, null);
assertValues(entry, iface, uid, set, tag, metered, roaming); assertValues(entry, iface, uid, set, tag, metered, roaming, defaultNetwork);
assertValues(entry, rxBytes, rxPackets, txBytes, txPackets, operations); assertValues(entry, rxBytes, rxPackets, txBytes, txPackets, operations);
} }
private static void assertValues( private static void assertValues(
NetworkStats.Entry entry, String iface, int uid, int set, int tag, int metered, NetworkStats.Entry entry, String iface, int uid, int set, int tag, int metered,
int roaming) { int roaming, int defaultNetwork) {
assertEquals(iface, entry.iface); assertEquals(iface, entry.iface);
assertEquals(uid, entry.uid); assertEquals(uid, entry.uid);
assertEquals(set, entry.set); assertEquals(set, entry.set);
assertEquals(tag, entry.tag); assertEquals(tag, entry.tag);
assertEquals(metered, entry.metered); assertEquals(metered, entry.metered);
assertEquals(roaming, entry.roaming); assertEquals(roaming, entry.roaming);
assertEquals(defaultNetwork, entry.defaultNetwork);
} }
private static void assertValues(NetworkStats.Entry entry, long rxBytes, long rxPackets, private static void assertValues(NetworkStats.Entry entry, long rxBytes, long rxPackets,

View File

@@ -16,6 +16,7 @@
package com.android.internal.net; package com.android.internal.net;
import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
import static android.net.NetworkStats.METERED_NO; import static android.net.NetworkStats.METERED_NO;
import static android.net.NetworkStats.ROAMING_NO; import static android.net.NetworkStats.ROAMING_NO;
import static android.net.NetworkStats.SET_ALL; import static android.net.NetworkStats.SET_ALL;
@@ -240,7 +241,8 @@ public class NetworkStatsFactoryTest {
private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set,
int tag, long rxBytes, long txBytes) { int tag, long rxBytes, long txBytes) {
final int i = stats.findIndex(iface, uid, set, tag, METERED_NO, ROAMING_NO); final int i = stats.findIndex(iface, uid, set, tag, METERED_NO, ROAMING_NO,
DEFAULT_NETWORK_NO);
if (i < 0) { if (i < 0) {
fail(String.format("no NetworkStats for (iface: %s, uid: %d, set: %d, tag: %d)", fail(String.format("no NetworkStats for (iface: %s, uid: %d, set: %d, tag: %d)",
iface, uid, set, tag)); iface, uid, set, tag));
@@ -252,7 +254,8 @@ public class NetworkStatsFactoryTest {
private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set,
int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) { int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) {
final int i = stats.findIndex(iface, uid, set, tag, METERED_NO, ROAMING_NO); final int i = stats.findIndex(iface, uid, set, tag, METERED_NO, ROAMING_NO,
DEFAULT_NETWORK_NO);
if (i < 0) { if (i < 0) {
fail(String.format("no NetworkStats for (iface: %s, uid: %d, set: %d, tag: %d)", fail(String.format("no NetworkStats for (iface: %s, uid: %d, set: %d, tag: %d)",
iface, uid, set, tag)); iface, uid, set, tag));

View File

@@ -3501,34 +3501,50 @@ public class ConnectivityServiceTest {
@Test @Test
public void testStatsIfacesChanged() throws Exception { public void testStatsIfacesChanged() throws Exception {
mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR); mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
Network[] onlyCell = new Network[]{mCellNetworkAgent.getNetwork()};
Network[] onlyWifi = new Network[]{mWiFiNetworkAgent.getNetwork()};
// Simple connection should have updated ifaces // Simple connection should have updated ifaces
mCellNetworkAgent.connect(false); mCellNetworkAgent.connect(false);
waitForIdle(); waitForIdle();
verify(mStatsService, atLeastOnce()).forceUpdateIfaces(); verify(mStatsService, atLeastOnce()).forceUpdateIfaces(onlyCell);
reset(mStatsService);
// Default network switch should update ifaces.
mWiFiNetworkAgent.connect(false);
waitForIdle();
verify(mStatsService, atLeastOnce()).forceUpdateIfaces(onlyWifi);
reset(mStatsService);
// Disconnect should update ifaces.
mWiFiNetworkAgent.disconnect();
waitForIdle();
verify(mStatsService, atLeastOnce()).forceUpdateIfaces(onlyCell);
reset(mStatsService); reset(mStatsService);
// Metered change should update ifaces // Metered change should update ifaces
mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
waitForIdle(); waitForIdle();
verify(mStatsService, atLeastOnce()).forceUpdateIfaces(); verify(mStatsService, atLeastOnce()).forceUpdateIfaces(onlyCell);
reset(mStatsService); reset(mStatsService);
mCellNetworkAgent.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); mCellNetworkAgent.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
waitForIdle(); waitForIdle();
verify(mStatsService, atLeastOnce()).forceUpdateIfaces(); verify(mStatsService, atLeastOnce()).forceUpdateIfaces(onlyCell);
reset(mStatsService); reset(mStatsService);
// Captive portal change shouldn't update ifaces // Captive portal change shouldn't update ifaces
mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL); mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL);
waitForIdle(); waitForIdle();
verify(mStatsService, never()).forceUpdateIfaces(); verify(mStatsService, never()).forceUpdateIfaces(onlyCell);
reset(mStatsService); reset(mStatsService);
// Roaming change should update ifaces // Roaming change should update ifaces
mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING); mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
waitForIdle(); waitForIdle();
verify(mStatsService, atLeastOnce()).forceUpdateIfaces(); verify(mStatsService, atLeastOnce()).forceUpdateIfaces(onlyCell);
reset(mStatsService); reset(mStatsService);
} }

View File

@@ -195,7 +195,7 @@ public class NetworkStatsCollectionTest extends AndroidTestCase {
final NetworkStats.Entry entry = new NetworkStats.Entry(); final NetworkStats.Entry entry = new NetworkStats.Entry();
final NetworkIdentitySet identSet = new NetworkIdentitySet(); final NetworkIdentitySet identSet = new NetworkIdentitySet();
identSet.add(new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, identSet.add(new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN,
TEST_IMSI, null, false, true)); TEST_IMSI, null, false, true, true));
int myUid = Process.myUid(); int myUid = Process.myUid();
int otherUidInSameUser = Process.myUid() + 1; int otherUidInSameUser = Process.myUid() + 1;
@@ -447,7 +447,7 @@ public class NetworkStatsCollectionTest extends AndroidTestCase {
final NetworkStatsCollection large = new NetworkStatsCollection(HOUR_IN_MILLIS); final NetworkStatsCollection large = new NetworkStatsCollection(HOUR_IN_MILLIS);
final NetworkIdentitySet ident = new NetworkIdentitySet(); final NetworkIdentitySet ident = new NetworkIdentitySet();
ident.add(new NetworkIdentity(ConnectivityManager.TYPE_MOBILE, -1, TEST_IMSI, null, ident.add(new NetworkIdentity(ConnectivityManager.TYPE_MOBILE, -1, TEST_IMSI, null,
false, true)); false, true, true));
large.recordData(ident, UID_ALL, SET_ALL, TAG_NONE, TIME_A, TIME_B, large.recordData(ident, UID_ALL, SET_ALL, TAG_NONE, TIME_A, TIME_B,
new NetworkStats.Entry(12_730_893_164L, 1, 0, 0, 0)); new NetworkStats.Entry(12_730_893_164L, 1, 0, 0, 0));

View File

@@ -18,6 +18,8 @@ package com.android.server.net;
import static android.net.ConnectivityManager.TYPE_MOBILE; import static android.net.ConnectivityManager.TYPE_MOBILE;
import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.ConnectivityManager.TYPE_WIFI;
import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
import static android.net.NetworkStats.DEFAULT_NETWORK_YES;
import static android.net.NetworkStats.METERED_NO; import static android.net.NetworkStats.METERED_NO;
import static android.net.NetworkStats.ROAMING_NO; import static android.net.NetworkStats.ROAMING_NO;
import static android.net.NetworkStats.SET_DEFAULT; import static android.net.NetworkStats.SET_DEFAULT;
@@ -224,6 +226,15 @@ public class NetworkStatsObserversTest {
Mockito.verifyZeroInteractions(mockBinder); Mockito.verifyZeroInteractions(mockBinder);
} }
private NetworkIdentitySet makeTestIdentSet() {
NetworkIdentitySet identSet = new NetworkIdentitySet();
identSet.add(new NetworkIdentity(
TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN,
IMSI_1, null /* networkId */, false /* roaming */, true /* metered */,
true /* defaultNetwork */));
return identSet;
}
@Test @Test
public void testUpdateStats_initialSample_doesNotNotify() throws Exception { public void testUpdateStats_initialSample_doesNotNotify() throws Exception {
DataUsageRequest inputRequest = new DataUsageRequest( DataUsageRequest inputRequest = new DataUsageRequest(
@@ -235,10 +246,7 @@ public class NetworkStatsObserversTest {
assertTrue(Objects.equals(sTemplateImsi1, request.template)); assertTrue(Objects.equals(sTemplateImsi1, request.template));
assertEquals(THRESHOLD_BYTES, request.thresholdInBytes); assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
NetworkIdentitySet identSet = new NetworkIdentitySet(); NetworkIdentitySet identSet = makeTestIdentSet();
identSet.add(new NetworkIdentity(
TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN,
IMSI_1, null /* networkId */, false /* roaming */, true /* metered */));
mActiveIfaces.put(TEST_IFACE, identSet); mActiveIfaces.put(TEST_IFACE, identSet);
// Baseline // Baseline
@@ -263,10 +271,7 @@ public class NetworkStatsObserversTest {
assertTrue(Objects.equals(sTemplateImsi1, request.template)); assertTrue(Objects.equals(sTemplateImsi1, request.template));
assertEquals(THRESHOLD_BYTES, request.thresholdInBytes); assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
NetworkIdentitySet identSet = new NetworkIdentitySet(); NetworkIdentitySet identSet = makeTestIdentSet();
identSet.add(new NetworkIdentity(
TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN,
IMSI_1, null /* networkId */, false /* roaming */, true /* metered */));
mActiveIfaces.put(TEST_IFACE, identSet); mActiveIfaces.put(TEST_IFACE, identSet);
// Baseline // Baseline
@@ -298,10 +303,7 @@ public class NetworkStatsObserversTest {
assertTrue(Objects.equals(sTemplateImsi1, request.template)); assertTrue(Objects.equals(sTemplateImsi1, request.template));
assertEquals(THRESHOLD_BYTES, request.thresholdInBytes); assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
NetworkIdentitySet identSet = new NetworkIdentitySet(); NetworkIdentitySet identSet = makeTestIdentSet();
identSet.add(new NetworkIdentity(
TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN,
IMSI_1, null /* networkId */, false /* roaming */, true /* metered */));
mActiveIfaces.put(TEST_IFACE, identSet); mActiveIfaces.put(TEST_IFACE, identSet);
// Baseline // Baseline
@@ -334,17 +336,14 @@ public class NetworkStatsObserversTest {
assertTrue(Objects.equals(sTemplateImsi1, request.template)); assertTrue(Objects.equals(sTemplateImsi1, request.template));
assertEquals(THRESHOLD_BYTES, request.thresholdInBytes); assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
NetworkIdentitySet identSet = new NetworkIdentitySet(); NetworkIdentitySet identSet = makeTestIdentSet();
identSet.add(new NetworkIdentity(
TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN,
IMSI_1, null /* networkId */, false /* roaming */, true /* metered */));
mActiveUidIfaces.put(TEST_IFACE, identSet); mActiveUidIfaces.put(TEST_IFACE, identSet);
// Baseline // Baseline
NetworkStats xtSnapshot = null; NetworkStats xtSnapshot = null;
NetworkStats uidSnapshot = new NetworkStats(TEST_START, 2 /* initialSize */) NetworkStats uidSnapshot = new NetworkStats(TEST_START, 2 /* initialSize */)
.addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
BASE_BYTES, 2L, BASE_BYTES, 2L, 0L); DEFAULT_NETWORK_YES, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L);
mStatsObservers.updateStats( mStatsObservers.updateStats(
xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
VPN_INFO, TEST_START); VPN_INFO, TEST_START);
@@ -352,7 +351,8 @@ public class NetworkStatsObserversTest {
// Delta // Delta
uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2 /* initialSize */) uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2 /* initialSize */)
.addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
BASE_BYTES + THRESHOLD_BYTES, 2L, BASE_BYTES + THRESHOLD_BYTES, 2L, 0L); DEFAULT_NETWORK_NO, BASE_BYTES + THRESHOLD_BYTES, 2L,
BASE_BYTES + THRESHOLD_BYTES, 2L, 0L);
mStatsObservers.updateStats( mStatsObservers.updateStats(
xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
VPN_INFO, TEST_START); VPN_INFO, TEST_START);
@@ -371,17 +371,14 @@ public class NetworkStatsObserversTest {
assertTrue(Objects.equals(sTemplateImsi1, request.template)); assertTrue(Objects.equals(sTemplateImsi1, request.template));
assertEquals(THRESHOLD_BYTES, request.thresholdInBytes); assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
NetworkIdentitySet identSet = new NetworkIdentitySet(); NetworkIdentitySet identSet = makeTestIdentSet();
identSet.add(new NetworkIdentity(
TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN,
IMSI_1, null /* networkId */, false /* roaming */, true /* metered */));
mActiveUidIfaces.put(TEST_IFACE, identSet); mActiveUidIfaces.put(TEST_IFACE, identSet);
// Baseline // Baseline
NetworkStats xtSnapshot = null; NetworkStats xtSnapshot = null;
NetworkStats uidSnapshot = new NetworkStats(TEST_START, 2 /* initialSize */) NetworkStats uidSnapshot = new NetworkStats(TEST_START, 2 /* initialSize */)
.addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
BASE_BYTES, 2L, BASE_BYTES, 2L, 0L); DEFAULT_NETWORK_NO, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L);
mStatsObservers.updateStats( mStatsObservers.updateStats(
xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
VPN_INFO, TEST_START); VPN_INFO, TEST_START);
@@ -389,7 +386,8 @@ public class NetworkStatsObserversTest {
// Delta // Delta
uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2 /* initialSize */) uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2 /* initialSize */)
.addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
BASE_BYTES + THRESHOLD_BYTES, 2L, BASE_BYTES + THRESHOLD_BYTES, 2L, 0L); DEFAULT_NETWORK_NO, BASE_BYTES + THRESHOLD_BYTES, 2L,
BASE_BYTES + THRESHOLD_BYTES, 2L, 0L);
mStatsObservers.updateStats( mStatsObservers.updateStats(
xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
VPN_INFO, TEST_START); VPN_INFO, TEST_START);
@@ -407,17 +405,14 @@ public class NetworkStatsObserversTest {
assertTrue(Objects.equals(sTemplateImsi1, request.template)); assertTrue(Objects.equals(sTemplateImsi1, request.template));
assertEquals(THRESHOLD_BYTES, request.thresholdInBytes); assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
NetworkIdentitySet identSet = new NetworkIdentitySet(); NetworkIdentitySet identSet = makeTestIdentSet();
identSet.add(new NetworkIdentity(
TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN,
IMSI_1, null /* networkId */, false /* roaming */, true /* metered */));
mActiveUidIfaces.put(TEST_IFACE, identSet); mActiveUidIfaces.put(TEST_IFACE, identSet);
// Baseline // Baseline
NetworkStats xtSnapshot = null; NetworkStats xtSnapshot = null;
NetworkStats uidSnapshot = new NetworkStats(TEST_START, 2 /* initialSize */) NetworkStats uidSnapshot = new NetworkStats(TEST_START, 2 /* initialSize */)
.addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
BASE_BYTES, 2L, BASE_BYTES, 2L, 0L); DEFAULT_NETWORK_YES, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L);
mStatsObservers.updateStats( mStatsObservers.updateStats(
xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
VPN_INFO, TEST_START); VPN_INFO, TEST_START);
@@ -425,7 +420,8 @@ public class NetworkStatsObserversTest {
// Delta // Delta
uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2 /* initialSize */) uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2 /* initialSize */)
.addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
BASE_BYTES + THRESHOLD_BYTES, 2L, BASE_BYTES + THRESHOLD_BYTES, 2L, 0L); DEFAULT_NETWORK_YES, BASE_BYTES + THRESHOLD_BYTES, 2L,
BASE_BYTES + THRESHOLD_BYTES, 2L, 0L);
mStatsObservers.updateStats( mStatsObservers.updateStats(
xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
VPN_INFO, TEST_START); VPN_INFO, TEST_START);
@@ -444,17 +440,14 @@ public class NetworkStatsObserversTest {
assertTrue(Objects.equals(sTemplateImsi1, request.template)); assertTrue(Objects.equals(sTemplateImsi1, request.template));
assertEquals(THRESHOLD_BYTES, request.thresholdInBytes); assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
NetworkIdentitySet identSet = new NetworkIdentitySet(); NetworkIdentitySet identSet = makeTestIdentSet();
identSet.add(new NetworkIdentity(
TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN,
IMSI_1, null /* networkId */, false /* roaming */, true /* metered */));
mActiveUidIfaces.put(TEST_IFACE, identSet); mActiveUidIfaces.put(TEST_IFACE, identSet);
// Baseline // Baseline
NetworkStats xtSnapshot = null; NetworkStats xtSnapshot = null;
NetworkStats uidSnapshot = new NetworkStats(TEST_START, 2 /* initialSize */) NetworkStats uidSnapshot = new NetworkStats(TEST_START, 2 /* initialSize */)
.addValues(TEST_IFACE, UID_ANOTHER_USER, SET_DEFAULT, TAG_NONE, METERED_NO, .addValues(TEST_IFACE, UID_ANOTHER_USER, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L); ROAMING_NO, DEFAULT_NETWORK_YES, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L);
mStatsObservers.updateStats( mStatsObservers.updateStats(
xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
VPN_INFO, TEST_START); VPN_INFO, TEST_START);
@@ -462,8 +455,8 @@ public class NetworkStatsObserversTest {
// Delta // Delta
uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2 /* initialSize */) uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2 /* initialSize */)
.addValues(TEST_IFACE, UID_ANOTHER_USER, SET_DEFAULT, TAG_NONE, METERED_NO, .addValues(TEST_IFACE, UID_ANOTHER_USER, SET_DEFAULT, TAG_NONE, METERED_NO,
ROAMING_NO, BASE_BYTES + THRESHOLD_BYTES, 2L, BASE_BYTES + THRESHOLD_BYTES, ROAMING_NO, DEFAULT_NETWORK_NO, BASE_BYTES + THRESHOLD_BYTES, 2L,
2L, 0L); BASE_BYTES + THRESHOLD_BYTES, 2L, 0L);
mStatsObservers.updateStats( mStatsObservers.updateStats(
xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
VPN_INFO, TEST_START); VPN_INFO, TEST_START);

View File

@@ -21,6 +21,9 @@ import static android.content.Intent.EXTRA_UID;
import static android.net.ConnectivityManager.TYPE_MOBILE; import static android.net.ConnectivityManager.TYPE_MOBILE;
import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.ConnectivityManager.TYPE_WIFI;
import static android.net.ConnectivityManager.TYPE_WIMAX; import static android.net.ConnectivityManager.TYPE_WIMAX;
import static android.net.NetworkStats.DEFAULT_NETWORK_ALL;
import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
import static android.net.NetworkStats.DEFAULT_NETWORK_YES;
import static android.net.NetworkStats.IFACE_ALL; import static android.net.NetworkStats.IFACE_ALL;
import static android.net.NetworkStats.METERED_ALL; import static android.net.NetworkStats.METERED_ALL;
import static android.net.NetworkStats.METERED_NO; import static android.net.NetworkStats.METERED_NO;
@@ -67,6 +70,7 @@ import android.net.IConnectivityManager;
import android.net.INetworkManagementEventObserver; import android.net.INetworkManagementEventObserver;
import android.net.INetworkStatsSession; import android.net.INetworkStatsSession;
import android.net.LinkProperties; import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState; import android.net.NetworkInfo.DetailedState;
@@ -136,6 +140,12 @@ public class NetworkStatsServiceTest {
private static final int UID_BLUE = 1002; private static final int UID_BLUE = 1002;
private static final int UID_GREEN = 1003; private static final int UID_GREEN = 1003;
private static final Network WIFI_NETWORK = new Network(100);
private static final Network MOBILE_NETWORK = new Network(101);
private static final Network[] NETWORKS_WIFI = new Network[]{ WIFI_NETWORK };
private static final Network[] NETWORKS_MOBILE = new Network[]{ MOBILE_NETWORK };
private static final long WAIT_TIMEOUT = 2 * 1000; // 2 secs private static final long WAIT_TIMEOUT = 2 * 1000; // 2 secs
private static final int INVALID_TYPE = -1; private static final int INVALID_TYPE = -1;
@@ -231,7 +241,7 @@ public class NetworkStatsServiceTest {
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_WIFI);
// verify service has empty history for wifi // verify service has empty history for wifi
assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
@@ -278,7 +288,7 @@ public class NetworkStatsServiceTest {
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_WIFI);
// verify service has empty history for wifi // verify service has empty history for wifi
assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
@@ -306,10 +316,10 @@ public class NetworkStatsServiceTest {
// verify service recorded history // verify service recorded history
assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0); assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10); assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, METERED_NO, ROAMING_NO, 512L, 4L, 256L, assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, METERED_NO, ROAMING_NO,
2L, 4); DEFAULT_NETWORK_YES, 512L, 4L, 256L, 2L, 4);
assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, METERED_NO, ROAMING_NO, 512L, 4L, assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, METERED_NO, ROAMING_NO,
256L, 2L, 6); DEFAULT_NETWORK_YES, 512L, 4L, 256L, 2L, 6);
assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0); assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
@@ -331,10 +341,10 @@ public class NetworkStatsServiceTest {
// after systemReady(), we should have historical stats loaded again // after systemReady(), we should have historical stats loaded again
assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0); assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10); assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, METERED_NO, ROAMING_NO, 512L, 4L, 256L, assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, METERED_NO, ROAMING_NO,
2L, 4); DEFAULT_NETWORK_YES, 512L, 4L, 256L, 2L, 4);
assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, METERED_NO, ROAMING_NO, 512L, 4L, assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, METERED_NO, ROAMING_NO,
256L, 2L, 6); DEFAULT_NETWORK_YES, 512L, 4L, 256L, 2L, 6);
assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0); assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
} }
@@ -356,7 +366,7 @@ public class NetworkStatsServiceTest {
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_WIFI);
// modify some number on wifi, and trigger poll event // modify some number on wifi, and trigger poll event
@@ -401,7 +411,7 @@ public class NetworkStatsServiceTest {
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_MOBILE);
// create some traffic on first network // create some traffic on first network
@@ -439,7 +449,7 @@ public class NetworkStatsServiceTest {
.addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L)); .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_MOBILE);
forcePollAndWaitForIdle(); forcePollAndWaitForIdle();
@@ -481,7 +491,7 @@ public class NetworkStatsServiceTest {
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_WIFI);
// create some traffic // create some traffic
@@ -543,7 +553,7 @@ public class NetworkStatsServiceTest {
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_MOBILE);
// create some traffic // create some traffic
@@ -573,7 +583,7 @@ public class NetworkStatsServiceTest {
.addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)); .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_MOBILE);
forcePollAndWaitForIdle(); forcePollAndWaitForIdle();
@@ -605,7 +615,7 @@ public class NetworkStatsServiceTest {
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_WIFI);
// create some traffic for two apps // create some traffic for two apps
@@ -641,12 +651,12 @@ public class NetworkStatsServiceTest {
NetworkStats stats = mSession.getSummaryForAllUid( NetworkStats stats = mSession.getSummaryForAllUid(
sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true); sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
assertEquals(3, stats.size()); assertEquals(3, stats.size());
assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 50L, assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
5L, 50L, 5L, 1); DEFAULT_NETWORK_YES, 50L, 5L, 50L, 5L, 1);
assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 10L, assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO,
1L, 10L, 1L, 1); DEFAULT_NETWORK_YES, 10L, 1L, 10L, 1L, 1);
assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
2048L, 16L, 1024L, 8L, 0); DEFAULT_NETWORK_YES, 2048L, 16L, 1024L, 8L, 0);
// now verify that recent history only contains one uid // now verify that recent history only contains one uid
final long currentTime = currentTimeMillis(); final long currentTime = currentTimeMillis();
@@ -654,7 +664,7 @@ public class NetworkStatsServiceTest {
sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true); sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true);
assertEquals(1, stats.size()); assertEquals(1, stats.size());
assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
1024L, 8L, 512L, 4L, 0); DEFAULT_NETWORK_YES, 1024L, 8L, 512L, 4L, 0);
} }
@Test @Test
@@ -667,7 +677,7 @@ public class NetworkStatsServiceTest {
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_WIFI);
// create some initial traffic // create some initial traffic
@@ -708,14 +718,14 @@ public class NetworkStatsServiceTest {
final NetworkStats stats = mSession.getSummaryForAllUid( final NetworkStats stats = mSession.getSummaryForAllUid(
sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true); sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
assertEquals(4, stats.size()); assertEquals(4, stats.size());
assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 128L, assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
2L, 128L, 2L, 1); DEFAULT_NETWORK_YES, 128L, 2L, 128L, 2L, 1);
assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 64L, assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO,
1L, 64L, 1L, 1); DEFAULT_NETWORK_YES, 64L, 1L, 64L, 1L, 1);
assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
32L, 2L, 32L, 2L, 1); DEFAULT_NETWORK_YES, 32L, 2L, 32L, 2L, 1);
assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, METERED_NO, ROAMING_NO, 1L, assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, METERED_NO, ROAMING_NO,
1L, 1L, 1L, 1); DEFAULT_NETWORK_YES, 1L, 1L, 1L, 1L, 1);
} }
@Test @Test
@@ -728,7 +738,7 @@ public class NetworkStatsServiceTest {
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_WIFI);
// create some initial traffic // create some initial traffic
@@ -736,14 +746,14 @@ public class NetworkStatsServiceTest {
expectCurrentTime(); expectCurrentTime();
expectDefaultSettings(); expectDefaultSettings();
expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsSummary(buildEmptyStats());
// Note that all traffic from NetworkManagementService is tagged as METERED_NO and // Note that all traffic from NetworkManagementService is tagged as METERED_NO, ROAMING_NO
// ROAMING_NO, because metered and roaming isn't tracked at that layer. We layer it // and DEFAULT_NETWORK_YES, because these three properties aren't tracked at that layer.
// on top by inspecting the iface properties. // We layer them on top by inspecting the iface properties.
expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1) expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
.addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 128L, .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
2L, 128L, 2L, 0L) DEFAULT_NETWORK_YES, 128L, 2L, 128L, 2L, 0L)
.addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 64L, .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO,
1L, 64L, 1L, 0L)); DEFAULT_NETWORK_YES, 64L, 1L, 64L, 1L, 0L));
mService.incrementOperationCount(UID_RED, 0xF00D, 1); mService.incrementOperationCount(UID_RED, 0xF00D, 1);
forcePollAndWaitForIdle(); forcePollAndWaitForIdle();
@@ -755,9 +765,9 @@ public class NetworkStatsServiceTest {
sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true); sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
assertEquals(2, stats.size()); assertEquals(2, stats.size());
assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO,
128L, 2L, 128L, 2L, 1); DEFAULT_NETWORK_YES, 128L, 2L, 128L, 2L, 1);
assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, METERED_YES, ROAMING_NO, 64L, assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, METERED_YES, ROAMING_NO,
1L, 64L, 1L, 1); DEFAULT_NETWORK_YES, 64L, 1L, 64L, 1L, 1);
} }
@Test @Test
@@ -770,7 +780,7 @@ public class NetworkStatsServiceTest {
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_MOBILE);
// Create some traffic // Create some traffic
@@ -783,9 +793,9 @@ public class NetworkStatsServiceTest {
// on top by inspecting the iface properties. // on top by inspecting the iface properties.
expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1) expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
.addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_ALL, ROAMING_NO, .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_ALL, ROAMING_NO,
128L, 2L, 128L, 2L, 0L) DEFAULT_NETWORK_YES, 128L, 2L, 128L, 2L, 0L)
.addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, METERED_ALL, ROAMING_NO, 64L, .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, METERED_ALL, ROAMING_NO,
1L, 64L, 1L, 0L)); DEFAULT_NETWORK_YES, 64L, 1L, 64L, 1L, 0L));
forcePollAndWaitForIdle(); forcePollAndWaitForIdle();
// verify service recorded history // verify service recorded history
@@ -796,9 +806,9 @@ public class NetworkStatsServiceTest {
sTemplateImsi1, Long.MIN_VALUE, Long.MAX_VALUE, true); sTemplateImsi1, Long.MIN_VALUE, Long.MAX_VALUE, true);
assertEquals(2, stats.size()); assertEquals(2, stats.size());
assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, METERED_ALL, ROAMING_YES, assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, METERED_ALL, ROAMING_YES,
128L, 2L, 128L, 2L, 0); DEFAULT_NETWORK_YES, 128L, 2L, 128L, 2L, 0);
assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, METERED_ALL, ROAMING_YES, 64L, assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, METERED_ALL, ROAMING_YES,
1L, 64L, 1L, 0); DEFAULT_NETWORK_YES, 64L, 1L, 64L, 1L, 0);
} }
@Test @Test
@@ -811,7 +821,7 @@ public class NetworkStatsServiceTest {
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_MOBILE);
// create some tethering traffic // create some tethering traffic
@@ -856,7 +866,7 @@ public class NetworkStatsServiceTest {
expectNetworkStatsUidDetail(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats());
expectBandwidthControlCheck(); expectBandwidthControlCheck();
mService.forceUpdateIfaces(); mService.forceUpdateIfaces(NETWORKS_WIFI);
// verify service has empty history for wifi // verify service has empty history for wifi
assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
@@ -977,18 +987,18 @@ public class NetworkStatsServiceTest {
// verify summary API // verify summary API
final NetworkStats stats = mSession.getSummaryForNetwork(template, start, end); final NetworkStats stats = mSession.getSummaryForNetwork(template, start, end);
assertValues(stats, IFACE_ALL, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL, assertValues(stats, IFACE_ALL, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL,
rxBytes, rxPackets, txBytes, txPackets, operations); DEFAULT_NETWORK_ALL, rxBytes, rxPackets, txBytes, txPackets, operations);
} }
private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long rxPackets, private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long rxPackets,
long txBytes, long txPackets, int operations) throws Exception { long txBytes, long txPackets, int operations) throws Exception {
assertUidTotal(template, uid, SET_ALL, METERED_ALL, ROAMING_ALL, rxBytes, rxPackets, assertUidTotal(template, uid, SET_ALL, METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL,
txBytes, txPackets, operations); rxBytes, rxPackets, txBytes, txPackets, operations);
} }
private void assertUidTotal(NetworkTemplate template, int uid, int set, int metered, private void assertUidTotal(NetworkTemplate template, int uid, int set, int metered,
int roaming, long rxBytes, long rxPackets, long txBytes, long txPackets, int operations) int roaming, int defaultNetwork, long rxBytes, long rxPackets, long txBytes,
throws Exception { long txPackets, int operations) throws Exception {
// verify history API // verify history API
final NetworkStatsHistory history = mSession.getHistoryForUid( final NetworkStatsHistory history = mSession.getHistoryForUid(
template, uid, set, TAG_NONE, FIELD_ALL); template, uid, set, TAG_NONE, FIELD_ALL);
@@ -998,8 +1008,8 @@ public class NetworkStatsServiceTest {
// verify summary API // verify summary API
final NetworkStats stats = mSession.getSummaryForAllUid( final NetworkStats stats = mSession.getSummaryForAllUid(
template, Long.MIN_VALUE, Long.MAX_VALUE, false); template, Long.MIN_VALUE, Long.MAX_VALUE, false);
assertValues(stats, IFACE_ALL, uid, set, TAG_NONE, metered, roaming, rxBytes, rxPackets, assertValues(stats, IFACE_ALL, uid, set, TAG_NONE, metered, roaming, defaultNetwork,
txBytes, txPackets, operations); rxBytes, rxPackets, txBytes, txPackets, operations);
} }
private void expectSystemReady() throws Exception { private void expectSystemReady() throws Exception {
@@ -1097,8 +1107,8 @@ public class NetworkStatsServiceTest {
} }
private static void assertValues(NetworkStats stats, String iface, int uid, int set, private static void assertValues(NetworkStats stats, String iface, int uid, int set,
int tag, int metered, int roaming, long rxBytes, long rxPackets, long txBytes, int tag, int metered, int roaming, int defaultNetwork, long rxBytes, long rxPackets,
long txPackets, int operations) { long txBytes, long txPackets, int operations) {
final NetworkStats.Entry entry = new NetworkStats.Entry(); final NetworkStats.Entry entry = new NetworkStats.Entry();
final int[] sets; final int[] sets;
if (set == SET_ALL) { if (set == SET_ALL) {
@@ -1121,12 +1131,22 @@ public class NetworkStatsServiceTest {
meterings = new int[] { metered }; meterings = new int[] { metered };
} }
final int[] defaultNetworks;
if (defaultNetwork == DEFAULT_NETWORK_ALL) {
defaultNetworks = new int[] { DEFAULT_NETWORK_ALL, DEFAULT_NETWORK_YES,
DEFAULT_NETWORK_NO };
} else {
defaultNetworks = new int[] { defaultNetwork };
}
for (int s : sets) { for (int s : sets) {
for (int r : roamings) { for (int r : roamings) {
for (int m : meterings) { for (int m : meterings) {
final int i = stats.findIndex(iface, uid, s, tag, m, r); for (int d : defaultNetworks) {
if (i != -1) { final int i = stats.findIndex(iface, uid, s, tag, m, r, d);
entry.add(stats.getValues(i, null)); if (i != -1) {
entry.add(stats.getValues(i, null));
}
} }
} }
} }
@@ -1161,7 +1181,7 @@ public class NetworkStatsServiceTest {
final NetworkCapabilities capabilities = new NetworkCapabilities(); final NetworkCapabilities capabilities = new NetworkCapabilities();
capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, !isMetered); capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, !isMetered);
capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, true); capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, true);
return new NetworkState(info, prop, capabilities, null, null, TEST_SSID); return new NetworkState(info, prop, capabilities, WIFI_NETWORK, null, TEST_SSID);
} }
private static NetworkState buildMobile3gState(String subscriberId) { private static NetworkState buildMobile3gState(String subscriberId) {
@@ -1178,7 +1198,7 @@ public class NetworkStatsServiceTest {
final NetworkCapabilities capabilities = new NetworkCapabilities(); final NetworkCapabilities capabilities = new NetworkCapabilities();
capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, false); capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, false);
capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, !isRoaming); capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, !isRoaming);
return new NetworkState(info, prop, capabilities, null, subscriberId, null); return new NetworkState(info, prop, capabilities, MOBILE_NETWORK, subscriberId, null);
} }
private static NetworkState buildMobile4gState(String iface) { private static NetworkState buildMobile4gState(String iface) {
@@ -1189,7 +1209,7 @@ public class NetworkStatsServiceTest {
final NetworkCapabilities capabilities = new NetworkCapabilities(); final NetworkCapabilities capabilities = new NetworkCapabilities();
capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, false); capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, false);
capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, true); capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, true);
return new NetworkState(info, prop, capabilities, null, null, null); return new NetworkState(info, prop, capabilities, MOBILE_NETWORK, null, null);
} }
private NetworkStats buildEmptyStats() { private NetworkStats buildEmptyStats() {