Map network identity using ConnectivityService.

Instead of deriving network identity based on raw subsystem broadcasts,
listen for updates from ConnectivityService.  Added atomic view of all
active NetworkState, and build map from "iface" to NetworkIdentity set
for stats tracking.

To avoid exposing internal complexity, INetworkStatsService calls use
general templates.  Added TelephonyManager mapping to classify network
types using broad labels like "3G" or "4G", used to drive templates.

Cleaned up Objects and Preconditions.

Change-Id: I1d4c1403f0503bc3635a59bb378841ba42239a91
This commit is contained in:
Jeff Sharkey
2011-05-28 20:56:34 -07:00
parent 9ab8c6a64d
commit 1a3eb889f0
5 changed files with 253 additions and 149 deletions

View File

@@ -16,12 +16,18 @@
package android.net;
import android.net.NetworkStats;
import android.net.NetworkStatsHistory;
/** {@hide} */
interface INetworkStatsService {
NetworkStatsHistory[] getNetworkStatsSummary(int networkType);
NetworkStatsHistory getNetworkStatsUid(int uid);
/** Return historical stats for traffic that matches template. */
NetworkStatsHistory getHistoryForNetwork(int networkTemplate);
/** Return historical stats for specific UID traffic that matches template. */
NetworkStatsHistory getHistoryForUid(int uid, int networkTemplate);
/** Return usage summary per UID for traffic that matches template. */
NetworkStats getSummaryPerUid(long start, long end, int networkTemplate);
}

View File

@@ -40,26 +40,17 @@ import java.util.Arrays;
public class NetworkStatsHistory implements Parcelable {
private static final int VERSION = 1;
/** {@link #uid} value when UID details unavailable. */
public static final int UID_ALL = -1;
// TODO: teach about zigzag encoding to use less disk space
// TODO: teach how to convert between bucket sizes
public final int networkType;
public final String identity;
public final int uid;
public final long bucketDuration;
int bucketCount;
long[] bucketStart;
long[] rx;
long[] tx;
public int bucketCount;
public long[] bucketStart;
public long[] rx;
public long[] tx;
public NetworkStatsHistory(int networkType, String identity, int uid, long bucketDuration) {
this.networkType = networkType;
this.identity = identity;
this.uid = uid;
public NetworkStatsHistory(long bucketDuration) {
this.bucketDuration = bucketDuration;
bucketStart = new long[0];
rx = new long[0];
@@ -68,9 +59,6 @@ public class NetworkStatsHistory implements Parcelable {
}
public NetworkStatsHistory(Parcel in) {
networkType = in.readInt();
identity = in.readString();
uid = in.readInt();
bucketDuration = in.readLong();
bucketStart = readLongArray(in);
rx = in.createLongArray();
@@ -80,9 +68,6 @@ public class NetworkStatsHistory implements Parcelable {
/** {@inheritDoc} */
public void writeToParcel(Parcel out, int flags) {
out.writeInt(networkType);
out.writeString(identity);
out.writeInt(uid);
out.writeLong(bucketDuration);
writeLongArray(out, bucketStart, bucketCount);
writeLongArray(out, rx, bucketCount);
@@ -91,9 +76,6 @@ public class NetworkStatsHistory implements Parcelable {
public NetworkStatsHistory(DataInputStream in) throws IOException {
final int version = in.readInt();
networkType = in.readInt();
identity = in.readUTF();
uid = in.readInt();
bucketDuration = in.readLong();
bucketStart = readLongArray(in);
rx = readLongArray(in);
@@ -103,9 +85,6 @@ public class NetworkStatsHistory implements Parcelable {
public void writeToStream(DataOutputStream out) throws IOException {
out.writeInt(VERSION);
out.writeInt(networkType);
out.writeUTF(identity);
out.writeInt(uid);
out.writeLong(bucketDuration);
writeLongArray(out, bucketStart, bucketCount);
writeLongArray(out, rx, bucketCount);
@@ -214,11 +193,8 @@ public class NetworkStatsHistory implements Parcelable {
}
public void dump(String prefix, PrintWriter pw) {
// TODO: consider stripping identity when dumping
pw.print(prefix);
pw.print("NetworkStatsHistory: networkType="); pw.print(networkType);
pw.print(" identity="); pw.print(identity);
pw.print(" uid="); pw.println(uid);
pw.println("NetworkStatsHistory:");
for (int i = 0; i < bucketCount; i++) {
pw.print(prefix);
pw.print(" timestamp="); pw.print(bucketStart[i]);

View File

@@ -41,6 +41,42 @@ public class TrafficStats {
*/
public final static int UNSUPPORTED = -1;
// TODO: find better home for these template constants
/**
* Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
* networks together. Only uses statistics for currently active IMSI.
*
* @hide
*/
public static final int TEMPLATE_MOBILE_ALL = 1;
/**
* Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
* networks together that roughly meet a "3G" definition, or lower. Only
* uses statistics for currently active IMSI.
*
* @hide
*/
public static final int TEMPLATE_MOBILE_3G_LOWER = 2;
/**
* Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
* networks together that meet a "4G" definition. Only uses statistics for
* currently active IMSI.
*
* @hide
*/
public static final int TEMPLATE_MOBILE_4G = 3;
/**
* Template to combine all {@link ConnectivityManager#TYPE_WIFI} style
* networks together.
*
* @hide
*/
public static final int TEMPLATE_WIFI = 4;
/**
* Snapshot of {@link NetworkStats} when the currently active profiling
* session started, or {@code null} if no session active.