Track and persist in stats whether traffic is on the default network.
This change adds the defaultNetwork to the ident based on current system networking state, and persists that value to flash. Bug: 35142602 Test: runtest frameworks-net Test: downloaded files using default/non-default networks while watching dumpsys netstats Change-Id: Id8776d149b2977548a9eb455ad4926af55e25aba
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
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.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;
|
||||||
@@ -365,7 +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 = DEFAULT_NETWORK_YES;
|
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;
|
||||||
|
|||||||
@@ -1061,7 +1061,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
|||||||
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.
|
||||||
@@ -1081,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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user