Retuning tag information through NetworkStatsManager.

Network tags could be set since ICS but was not exposed
through the SDK. This CL extends existing functionality
of NetworkStatsManager to return network tags.

Bug: 25813338
Change-Id: I414b98193249ba88a3f2d64cb2e0d2633f64fa3f
This commit is contained in:
Antonio Cansado
2015-12-10 15:57:56 -08:00
parent 3bbf8f320d
commit 2bb66f5d48
3 changed files with 105 additions and 28 deletions

View File

@@ -48,7 +48,6 @@ public final class NetworkStats implements AutoCloseable {
*/
private final long mEndTimeStamp;
/**
* Non-null array indicates the query enumerates over uids.
*/
@@ -165,7 +164,18 @@ public final class NetworkStats implements AutoCloseable {
*/
public static final int ROAMING_ROAMING = 0x2;
/**
* Special TAG value matching any tag.
*/
public static final int TAG_ANY = android.net.NetworkStats.TAG_ALL;
/**
* Special TAG value for total data across all tags
*/
public static final int TAG_ALL = android.net.NetworkStats.TAG_NONE;
private int mUid;
private int mTag;
private int mState;
private int mRoaming;
private long mBeginTimeStamp;
@@ -192,6 +202,14 @@ public final class NetworkStats implements AutoCloseable {
return uid;
}
private static int convertTag(int tag) {
switch (tag) {
case android.net.NetworkStats.TAG_ALL: return TAG_ANY;
case android.net.NetworkStats.TAG_NONE: return TAG_ALL;
}
return tag;
}
private static int convertRoaming(int roaming) {
switch (roaming) {
case android.net.NetworkStats.ROAMING_ALL : return ROAMING_ALL;
@@ -217,6 +235,14 @@ public final class NetworkStats implements AutoCloseable {
return mUid;
}
/**
* Tag of the bucket.<p />
* @return Bucket tag.
*/
public int getTag() {
return mTag;
}
/**
* Usage state. One of the following values:<p/>
* <ul>
@@ -363,9 +389,9 @@ public final class NetworkStats implements AutoCloseable {
* Collects summary results and sets summary enumeration mode.
* @throws RemoteException
*/
void startSummaryEnumeration() throws RemoteException {
mSummary = mSession.getSummaryForAllUid(mTemplate, mStartTimeStamp, mEndTimeStamp, false);
void startSummaryEnumeration(boolean includeTags) throws RemoteException {
mSummary = mSession.getSummaryForAllUid(mTemplate, mStartTimeStamp, mEndTimeStamp,
includeTags);
mEnumerationIndex = 0;
}
@@ -373,10 +399,17 @@ public final class NetworkStats implements AutoCloseable {
* Collects history results for uid and resets history enumeration index.
*/
void startHistoryEnumeration(int uid) {
startHistoryEnumeration(uid, android.net.NetworkStats.TAG_NONE);
}
/**
* Collects history results for uid and resets history enumeration index.
*/
void startHistoryEnumeration(int uid, int tag) {
mHistory = null;
try {
mHistory = mSession.getHistoryIntervalForUid(mTemplate, uid,
android.net.NetworkStats.SET_ALL, android.net.NetworkStats.TAG_NONE,
android.net.NetworkStats.SET_ALL, tag,
NetworkStatsHistory.FIELD_ALL, mStartTimeStamp, mEndTimeStamp);
setSingleUid(uid);
} catch (RemoteException e) {
@@ -434,6 +467,7 @@ public final class NetworkStats implements AutoCloseable {
private void fillBucketFromSummaryEntry(Bucket bucketOut) {
bucketOut.mUid = Bucket.convertUid(mRecycledSummaryEntry.uid);
bucketOut.mTag = Bucket.convertTag(mRecycledSummaryEntry.tag);
bucketOut.mState = Bucket.convertState(mRecycledSummaryEntry.set);
bucketOut.mRoaming = Bucket.convertRoaming(mRecycledSummaryEntry.roaming);
bucketOut.mBeginTimeStamp = mStartTimeStamp;

View File

@@ -82,10 +82,11 @@ public class NetworkStatsManager {
}
/**
* Query network usage statistics summaries. Result is summarised data usage for the whole
* device. Result is a single Bucket aggregated over time, state and uid. This means the
* bucket's start and end timestamp are going to be the same as the 'startTime' and 'endTime'
* parameters, state is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid
* {@link NetworkStats.Bucket#UID_ALL}, and roaming {@link NetworkStats.Bucket#ROAMING_ALL}.
* device. Result is a single Bucket aggregated over time, state, uid, tag and roaming. This
* means the bucket's start and end timestamp are going to be the same as the 'startTime' and
* 'endTime' parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid
* {@link NetworkStats.Bucket#UID_ALL}, tag {@link NetworkStats.Bucket#TAG_ALL}
* and roaming {@link NetworkStats.Bucket#ROAMING_ALL}.
*
* @param networkType As defined in {@link ConnectivityManager}, e.g.
* {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
@@ -113,12 +114,23 @@ public class NetworkStatsManager {
return bucket;
}
/**
* Query network usage statistics summaries aggregated across tags.
*
* #see querySummaryForUser(int, String, long, long, boolean)
*/
public Bucket querySummaryForUser(int networkType, String subscriberId, long startTime,
long endTime) throws SecurityException, RemoteException {
return querySummaryForUser(networkType, subscriberId, startTime, endTime,
false /* includeTags */);
}
/**
* Query network usage statistics summaries. Result is summarised data usage for all uids
* belonging to calling user. Result is a single Bucket aggregated over time, state and uid.
* This means the bucket's start and end timestamp are going to be the same as the 'startTime'
* and 'endTime' parameters, state is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid
* {@link NetworkStats.Bucket#UID_ALL}, and roaming {@link NetworkStats.Bucket#ROAMING_ALL}.
* and 'endTime' parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL} and uid
* {@link NetworkStats.Bucket#UID_ALL}.
*
* @param networkType As defined in {@link ConnectivityManager}, e.g.
* {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
@@ -128,11 +140,13 @@ public class NetworkStatsManager {
* {@link java.lang.System#currentTimeMillis}.
* @param endTime End of period. Defined in terms of "Unix time", see
* {@link java.lang.System#currentTimeMillis}.
* @param includeTags whether to include network tags. If {@code true}, tags will be returned
* and history retention may be shorter.
* @return Bucket object or null if permissions are insufficient or error happened during
* statistics collection.
*/
public Bucket querySummaryForUser(int networkType, String subscriberId, long startTime,
long endTime) throws SecurityException, RemoteException {
long endTime, boolean includeTags) throws SecurityException, RemoteException {
NetworkTemplate template = createTemplate(networkType, subscriberId);
if (template == null) {
return null;
@@ -140,18 +154,28 @@ public class NetworkStatsManager {
NetworkStats stats;
stats = new NetworkStats(mContext, template, startTime, endTime);
stats.startSummaryEnumeration();
stats.startSummaryEnumeration(includeTags);
stats.close();
return stats.getSummaryAggregate();
}
/**
* Query network usage statistics summaries aggregated across tags.
*
* #see querySummary(int, String, long, long, boolean)
*/
public NetworkStats querySummary(int networkType, String subscriberId, long startTime,
long endTime) throws SecurityException, RemoteException {
return querySummary(networkType, subscriberId, startTime, endTime, false /* includeTags */);
}
/**
* Query network usage statistics summaries. Result filtered to include only uids belonging to
* calling user. Result is aggregated over time, hence all buckets will have the same start and
* end timestamps. Not aggregated over state, uid, or roaming. This means buckets' start and end
* timestamps are going to be the same as the 'startTime' and 'endTime' parameters, state and
* uid are going to vary.
* end timestamps. Not aggregated over state or uid or tag. This means buckets' start and end
* timestamps are going to be the same as the 'startTime' and 'endTime' parameters. State,
* uid and tag are going to vary.
*
* @param networkType As defined in {@link ConnectivityManager}, e.g.
* {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
@@ -161,11 +185,13 @@ public class NetworkStatsManager {
* {@link java.lang.System#currentTimeMillis}.
* @param endTime End of period. Defined in terms of "Unix time", see
* {@link java.lang.System#currentTimeMillis}.
* @param includeTags whether to include network tags. If {@code true}, tags will be returned
* and history retention may be shorter.
* @return Statistics object or null if permissions are insufficient or error happened during
* statistics collection.
*/
public NetworkStats querySummary(int networkType, String subscriberId, long startTime,
long endTime) throws SecurityException, RemoteException {
long endTime, boolean includeTags) throws SecurityException, RemoteException {
NetworkTemplate template = createTemplate(networkType, subscriberId);
if (template == null) {
return null;
@@ -173,17 +199,28 @@ public class NetworkStatsManager {
NetworkStats result;
result = new NetworkStats(mContext, template, startTime, endTime);
result.startSummaryEnumeration();
result.startSummaryEnumeration(includeTags);
return result;
}
/**
* Query network usage statistics details. Only usable for uids belonging to calling user.
* Result is aggregated over state but not aggregated over time. This means buckets' start and
* end timestamps are going to be between 'startTime' and 'endTime' parameters, state is going
* to be {@link NetworkStats.Bucket#STATE_ALL} and uid the same as the 'uid' parameter. roaming
* is going to be {@link NetworkStats.Bucket#ROAMING_ALL}.
* Query network usage statistics details for a given uid.
*
* #see queryDetailsForUidTag(int, String, long, long, int, int)
*/
public NetworkStats queryDetailsForUid(int networkType, String subscriberId,
long startTime, long endTime, int uid) throws SecurityException, RemoteException {
return queryDetailsForUidTag(networkType, subscriberId, startTime, endTime, uid,
NetworkStats.Bucket.TAG_ALL);
}
/**
* Query network usage statistics details for a given uid and tag. Only usable for uids
* belonging to calling user. Result is aggregated over state but not aggregated over time.
* This means buckets' start and end timestamps are going to be between 'startTime' and
* 'endTime' parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid the
* same as the 'uid' parameter and tag the same as 'tag' parameter.
* <p>Only includes buckets that atomically occur in the inclusive time range. Doesn't
* interpolate across partial buckets. Since bucket length is in the order of hours, this
* method cannot be used to measure data usage on a fine grained time scale.
@@ -197,11 +234,14 @@ public class NetworkStatsManager {
* @param endTime End of period. Defined in terms of "Unix time", see
* {@link java.lang.System#currentTimeMillis}.
* @param uid UID of app
* @param tag TAG of interest. Use {@link NetworkStats.Bucket#TAG_ANY} for any tags, use
* {@link NetworkStats.Bucket#TAG_ALL} to aggregate over tags.
* @return Statistics object or null if permissions are insufficient or error happened during
* statistics collection.
*/
public NetworkStats queryDetailsForUid(int networkType, String subscriberId,
long startTime, long endTime, int uid) throws SecurityException, RemoteException {
public NetworkStats queryDetailsForUidTag(int networkType, String subscriberId,
long startTime, long endTime, int uid, int tag) throws SecurityException,
RemoteException {
NetworkTemplate template = createTemplate(networkType, subscriberId);
if (template == null) {
return null;
@@ -209,7 +249,7 @@ public class NetworkStatsManager {
NetworkStats result;
result = new NetworkStats(mContext, template, startTime, endTime);
result.startHistoryEnumeration(uid);
result.startHistoryEnumeration(uid, tag);
return result;
}
@@ -218,8 +258,9 @@ public class NetworkStatsManager {
* Query network usage statistics details. Result filtered to include only uids belonging to
* calling user. Result is aggregated over state but not aggregated over time or uid. This means
* buckets' start and end timestamps are going to be between 'startTime' and 'endTime'
* parameters, state is going to be {@link NetworkStats.Bucket#STATE_ALL} and uid will vary.
* roaming is going to be {@link NetworkStats.Bucket#ROAMING_ALL}.
* parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid will vary,
* tag {@link NetworkStats.Bucket#TAG_ALL} and roaming is going to be
* {@link NetworkStats.Bucket#ROAMING_ALL}.
* <p>Only includes buckets that atomically occur in the inclusive time range. Doesn't
* interpolate across partial buckets. Since bucket length is in the order of hours, this
* method cannot be used to measure data usage on a fine grained time scale.

View File

@@ -49,6 +49,7 @@ public class NetworkStats implements Parcelable {
/** {@link #uid} value when UID details unavailable. */
public static final int UID_ALL = -1;
/** {@link #tag} value matching any tag. */
// TODO: Rename TAG_ALL to TAG_ANY.
public static final int TAG_ALL = -1;
/** {@link #set} value for all sets combined, not including debug sets. */
public static final int SET_ALL = -1;
@@ -64,6 +65,7 @@ public class NetworkStats implements Parcelable {
public static final int SET_DBG_VPN_OUT = 1002;
/** {@link #tag} value for total data across all tags. */
// TODO: Rename TAG_NONE to TAG_ALL.
public static final int TAG_NONE = 0;
/** {@link #set} value for all roaming values. */