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:
@@ -48,7 +48,6 @@ public final class NetworkStats implements AutoCloseable {
|
|||||||
*/
|
*/
|
||||||
private final long mEndTimeStamp;
|
private final long mEndTimeStamp;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Non-null array indicates the query enumerates over uids.
|
* 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;
|
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 mUid;
|
||||||
|
private int mTag;
|
||||||
private int mState;
|
private int mState;
|
||||||
private int mRoaming;
|
private int mRoaming;
|
||||||
private long mBeginTimeStamp;
|
private long mBeginTimeStamp;
|
||||||
@@ -192,6 +202,14 @@ public final class NetworkStats implements AutoCloseable {
|
|||||||
return uid;
|
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) {
|
private static int convertRoaming(int roaming) {
|
||||||
switch (roaming) {
|
switch (roaming) {
|
||||||
case android.net.NetworkStats.ROAMING_ALL : return ROAMING_ALL;
|
case android.net.NetworkStats.ROAMING_ALL : return ROAMING_ALL;
|
||||||
@@ -217,6 +235,14 @@ public final class NetworkStats implements AutoCloseable {
|
|||||||
return mUid;
|
return mUid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tag of the bucket.<p />
|
||||||
|
* @return Bucket tag.
|
||||||
|
*/
|
||||||
|
public int getTag() {
|
||||||
|
return mTag;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Usage state. One of the following values:<p/>
|
* Usage state. One of the following values:<p/>
|
||||||
* <ul>
|
* <ul>
|
||||||
@@ -363,9 +389,9 @@ public final class NetworkStats implements AutoCloseable {
|
|||||||
* Collects summary results and sets summary enumeration mode.
|
* Collects summary results and sets summary enumeration mode.
|
||||||
* @throws RemoteException
|
* @throws RemoteException
|
||||||
*/
|
*/
|
||||||
void startSummaryEnumeration() throws RemoteException {
|
void startSummaryEnumeration(boolean includeTags) throws RemoteException {
|
||||||
mSummary = mSession.getSummaryForAllUid(mTemplate, mStartTimeStamp, mEndTimeStamp, false);
|
mSummary = mSession.getSummaryForAllUid(mTemplate, mStartTimeStamp, mEndTimeStamp,
|
||||||
|
includeTags);
|
||||||
mEnumerationIndex = 0;
|
mEnumerationIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -373,10 +399,17 @@ public final class NetworkStats implements AutoCloseable {
|
|||||||
* Collects history results for uid and resets history enumeration index.
|
* Collects history results for uid and resets history enumeration index.
|
||||||
*/
|
*/
|
||||||
void startHistoryEnumeration(int uid) {
|
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;
|
mHistory = null;
|
||||||
try {
|
try {
|
||||||
mHistory = mSession.getHistoryIntervalForUid(mTemplate, uid,
|
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);
|
NetworkStatsHistory.FIELD_ALL, mStartTimeStamp, mEndTimeStamp);
|
||||||
setSingleUid(uid);
|
setSingleUid(uid);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
@@ -434,6 +467,7 @@ public final class NetworkStats implements AutoCloseable {
|
|||||||
|
|
||||||
private void fillBucketFromSummaryEntry(Bucket bucketOut) {
|
private void fillBucketFromSummaryEntry(Bucket bucketOut) {
|
||||||
bucketOut.mUid = Bucket.convertUid(mRecycledSummaryEntry.uid);
|
bucketOut.mUid = Bucket.convertUid(mRecycledSummaryEntry.uid);
|
||||||
|
bucketOut.mTag = Bucket.convertTag(mRecycledSummaryEntry.tag);
|
||||||
bucketOut.mState = Bucket.convertState(mRecycledSummaryEntry.set);
|
bucketOut.mState = Bucket.convertState(mRecycledSummaryEntry.set);
|
||||||
bucketOut.mRoaming = Bucket.convertRoaming(mRecycledSummaryEntry.roaming);
|
bucketOut.mRoaming = Bucket.convertRoaming(mRecycledSummaryEntry.roaming);
|
||||||
bucketOut.mBeginTimeStamp = mStartTimeStamp;
|
bucketOut.mBeginTimeStamp = mStartTimeStamp;
|
||||||
|
|||||||
@@ -82,10 +82,11 @@ public class NetworkStatsManager {
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Query network usage statistics summaries. Result is summarised data usage for the whole
|
* 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
|
* device. Result is a single Bucket aggregated over time, state, uid, tag and roaming. This
|
||||||
* bucket's start and end timestamp are going to be the same as the 'startTime' and 'endTime'
|
* means the bucket's start and end timestamp are going to be the same as the 'startTime' and
|
||||||
* parameters, state is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid
|
* '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}.
|
* {@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.
|
* @param networkType As defined in {@link ConnectivityManager}, e.g.
|
||||||
* {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
|
* {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
|
||||||
@@ -113,12 +114,23 @@ public class NetworkStatsManager {
|
|||||||
return bucket;
|
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
|
* 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.
|
* 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'
|
* 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
|
* and 'endTime' parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL} and uid
|
||||||
* {@link NetworkStats.Bucket#UID_ALL}, and roaming {@link NetworkStats.Bucket#ROAMING_ALL}.
|
* {@link NetworkStats.Bucket#UID_ALL}.
|
||||||
*
|
*
|
||||||
* @param networkType As defined in {@link ConnectivityManager}, e.g.
|
* @param networkType As defined in {@link ConnectivityManager}, e.g.
|
||||||
* {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
|
* {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
|
||||||
@@ -128,11 +140,13 @@ public class NetworkStatsManager {
|
|||||||
* {@link java.lang.System#currentTimeMillis}.
|
* {@link java.lang.System#currentTimeMillis}.
|
||||||
* @param endTime End of period. Defined in terms of "Unix time", see
|
* @param endTime End of period. Defined in terms of "Unix time", see
|
||||||
* {@link java.lang.System#currentTimeMillis}.
|
* {@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
|
* @return Bucket object or null if permissions are insufficient or error happened during
|
||||||
* statistics collection.
|
* statistics collection.
|
||||||
*/
|
*/
|
||||||
public Bucket querySummaryForUser(int networkType, String subscriberId, long startTime,
|
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);
|
NetworkTemplate template = createTemplate(networkType, subscriberId);
|
||||||
if (template == null) {
|
if (template == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -140,18 +154,28 @@ public class NetworkStatsManager {
|
|||||||
|
|
||||||
NetworkStats stats;
|
NetworkStats stats;
|
||||||
stats = new NetworkStats(mContext, template, startTime, endTime);
|
stats = new NetworkStats(mContext, template, startTime, endTime);
|
||||||
stats.startSummaryEnumeration();
|
stats.startSummaryEnumeration(includeTags);
|
||||||
|
|
||||||
stats.close();
|
stats.close();
|
||||||
return stats.getSummaryAggregate();
|
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
|
* 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
|
* 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
|
* 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 and
|
* timestamps are going to be the same as the 'startTime' and 'endTime' parameters. State,
|
||||||
* uid are going to vary.
|
* uid and tag are going to vary.
|
||||||
*
|
*
|
||||||
* @param networkType As defined in {@link ConnectivityManager}, e.g.
|
* @param networkType As defined in {@link ConnectivityManager}, e.g.
|
||||||
* {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
|
* {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
|
||||||
@@ -161,11 +185,13 @@ public class NetworkStatsManager {
|
|||||||
* {@link java.lang.System#currentTimeMillis}.
|
* {@link java.lang.System#currentTimeMillis}.
|
||||||
* @param endTime End of period. Defined in terms of "Unix time", see
|
* @param endTime End of period. Defined in terms of "Unix time", see
|
||||||
* {@link java.lang.System#currentTimeMillis}.
|
* {@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
|
* @return Statistics object or null if permissions are insufficient or error happened during
|
||||||
* statistics collection.
|
* statistics collection.
|
||||||
*/
|
*/
|
||||||
public NetworkStats querySummary(int networkType, String subscriberId, long startTime,
|
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);
|
NetworkTemplate template = createTemplate(networkType, subscriberId);
|
||||||
if (template == null) {
|
if (template == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -173,17 +199,28 @@ public class NetworkStatsManager {
|
|||||||
|
|
||||||
NetworkStats result;
|
NetworkStats result;
|
||||||
result = new NetworkStats(mContext, template, startTime, endTime);
|
result = new NetworkStats(mContext, template, startTime, endTime);
|
||||||
result.startSummaryEnumeration();
|
result.startSummaryEnumeration(includeTags);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Query network usage statistics details. Only usable for uids belonging to calling user.
|
* Query network usage statistics details for a given uid.
|
||||||
* 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
|
* #see queryDetailsForUidTag(int, String, long, long, int, int)
|
||||||
* 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}.
|
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
|
* <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
|
* 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.
|
* 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
|
* @param endTime End of period. Defined in terms of "Unix time", see
|
||||||
* {@link java.lang.System#currentTimeMillis}.
|
* {@link java.lang.System#currentTimeMillis}.
|
||||||
* @param uid UID of app
|
* @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
|
* @return Statistics object or null if permissions are insufficient or error happened during
|
||||||
* statistics collection.
|
* statistics collection.
|
||||||
*/
|
*/
|
||||||
public NetworkStats queryDetailsForUid(int networkType, String subscriberId,
|
public NetworkStats queryDetailsForUidTag(int networkType, String subscriberId,
|
||||||
long startTime, long endTime, int uid) throws SecurityException, RemoteException {
|
long startTime, long endTime, int uid, int tag) throws SecurityException,
|
||||||
|
RemoteException {
|
||||||
NetworkTemplate template = createTemplate(networkType, subscriberId);
|
NetworkTemplate template = createTemplate(networkType, subscriberId);
|
||||||
if (template == null) {
|
if (template == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -209,7 +249,7 @@ public class NetworkStatsManager {
|
|||||||
|
|
||||||
NetworkStats result;
|
NetworkStats result;
|
||||||
result = new NetworkStats(mContext, template, startTime, endTime);
|
result = new NetworkStats(mContext, template, startTime, endTime);
|
||||||
result.startHistoryEnumeration(uid);
|
result.startHistoryEnumeration(uid, tag);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -218,8 +258,9 @@ public class NetworkStatsManager {
|
|||||||
* Query network usage statistics details. Result filtered to include only uids belonging to
|
* 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
|
* 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'
|
* 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.
|
* parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid will vary,
|
||||||
* roaming is going to be {@link NetworkStats.Bucket#ROAMING_ALL}.
|
* 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
|
* <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
|
* 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.
|
* method cannot be used to measure data usage on a fine grained time scale.
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ public class NetworkStats implements Parcelable {
|
|||||||
/** {@link #uid} value when UID details unavailable. */
|
/** {@link #uid} value when UID details unavailable. */
|
||||||
public static final int UID_ALL = -1;
|
public static final int UID_ALL = -1;
|
||||||
/** {@link #tag} value matching any tag. */
|
/** {@link #tag} value matching any tag. */
|
||||||
|
// TODO: Rename TAG_ALL to TAG_ANY.
|
||||||
public static final int TAG_ALL = -1;
|
public static final int TAG_ALL = -1;
|
||||||
/** {@link #set} value for all sets combined, not including debug sets. */
|
/** {@link #set} value for all sets combined, not including debug sets. */
|
||||||
public static final int SET_ALL = -1;
|
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;
|
public static final int SET_DBG_VPN_OUT = 1002;
|
||||||
|
|
||||||
/** {@link #tag} value for total data across all tags. */
|
/** {@link #tag} value for total data across all tags. */
|
||||||
|
// TODO: Rename TAG_NONE to TAG_ALL.
|
||||||
public static final int TAG_NONE = 0;
|
public static final int TAG_NONE = 0;
|
||||||
|
|
||||||
/** {@link #set} value for all roaming values. */
|
/** {@link #set} value for all roaming values. */
|
||||||
|
|||||||
Reference in New Issue
Block a user