Rename NetworkUsageStats -> NetworkStats

Also making other changes to Data Usage API as requested by API review.

Bug: 20823478
Change-Id: Id766c5a725c856da9d8883d73ae788fc1472440a
This commit is contained in:
Zoltan Szatmary-Ban
2015-05-13 17:53:17 +01:00
parent 24d204021b
commit 43a69f7a67
2 changed files with 56 additions and 35 deletions

View File

@@ -19,7 +19,6 @@ package android.app.usage;
import android.content.Context; import android.content.Context;
import android.net.INetworkStatsService; import android.net.INetworkStatsService;
import android.net.INetworkStatsSession; import android.net.INetworkStatsSession;
import android.net.NetworkStats;
import android.net.NetworkStatsHistory; import android.net.NetworkStatsHistory;
import android.net.NetworkTemplate; import android.net.NetworkTemplate;
import android.net.TrafficStats; import android.net.TrafficStats;
@@ -33,7 +32,7 @@ import dalvik.system.CloseGuard;
* Class providing enumeration over buckets of network usage statistics. NetworkUsageStats objects * Class providing enumeration over buckets of network usage statistics. NetworkUsageStats objects
* are returned as results to various queries in {@link NetworkStatsManager}. * are returned as results to various queries in {@link NetworkStatsManager}.
*/ */
public final class NetworkUsageStats implements AutoCloseable { public final class NetworkStats implements AutoCloseable {
private final static String TAG = "NetworkUsageStats"; private final static String TAG = "NetworkUsageStats";
private final CloseGuard mCloseGuard = CloseGuard.get(); private final CloseGuard mCloseGuard = CloseGuard.get();
@@ -70,7 +69,7 @@ public final class NetworkUsageStats implements AutoCloseable {
/** /**
* Results of a summary query. * Results of a summary query.
*/ */
private NetworkStats mSummary = null; private android.net.NetworkStats mSummary = null;
/** /**
* Results of detail queries. * Results of detail queries.
@@ -85,11 +84,11 @@ public final class NetworkUsageStats implements AutoCloseable {
/** /**
* Recycling entry objects to prevent heap fragmentation. * Recycling entry objects to prevent heap fragmentation.
*/ */
private NetworkStats.Entry mRecycledSummaryEntry = null; private android.net.NetworkStats.Entry mRecycledSummaryEntry = null;
private NetworkStatsHistory.Entry mRecycledHistoryEntry = null; private NetworkStatsHistory.Entry mRecycledHistoryEntry = null;
/** @hide */ /** @hide */
NetworkUsageStats(Context context, NetworkTemplate template, long startTimestamp, NetworkStats(Context context, NetworkTemplate template, long startTimestamp,
long endTimestamp) throws RemoteException, SecurityException { long endTimestamp) throws RemoteException, SecurityException {
final INetworkStatsService statsService = INetworkStatsService.Stub.asInterface( final INetworkStatsService statsService = INetworkStatsService.Stub.asInterface(
ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
@@ -135,15 +134,20 @@ public final class NetworkUsageStats implements AutoCloseable {
*/ */
public static final int STATE_FOREGROUND = 0x2; public static final int STATE_FOREGROUND = 0x2;
/**
* Special UID value for aggregate/unspecified.
*/
public static final int UID_ALL = android.net.NetworkStats.UID_ALL;
/** /**
* Special UID value for removed apps. * Special UID value for removed apps.
*/ */
public static final int UID_REMOVED = -4; public static final int UID_REMOVED = TrafficStats.UID_REMOVED;
/** /**
* Special UID value for data usage by tethering. * Special UID value for data usage by tethering.
*/ */
public static final int UID_TETHERING = -5; public static final int UID_TETHERING = TrafficStats.UID_TETHERING;
private int mUid; private int mUid;
private int mState; private int mState;
@@ -156,9 +160,9 @@ public final class NetworkUsageStats implements AutoCloseable {
private static int convertState(int networkStatsSet) { private static int convertState(int networkStatsSet) {
switch (networkStatsSet) { switch (networkStatsSet) {
case NetworkStats.SET_ALL : return STATE_ALL; case android.net.NetworkStats.SET_ALL : return STATE_ALL;
case NetworkStats.SET_DEFAULT : return STATE_DEFAULT; case android.net.NetworkStats.SET_DEFAULT : return STATE_DEFAULT;
case NetworkStats.SET_FOREGROUND : return STATE_FOREGROUND; case android.net.NetworkStats.SET_FOREGROUND : return STATE_FOREGROUND;
} }
return 0; return 0;
} }
@@ -337,8 +341,8 @@ public final class NetworkUsageStats implements AutoCloseable {
void startHistoryEnumeration(int uid) { void startHistoryEnumeration(int uid) {
mHistory = null; mHistory = null;
try { try {
mHistory = mSession.getHistoryForUid(mTemplate, uid, NetworkStats.SET_ALL, mHistory = mSession.getHistoryForUid(mTemplate, uid, android.net.NetworkStats.SET_ALL,
NetworkStats.TAG_NONE, NetworkStatsHistory.FIELD_ALL); android.net.NetworkStats.TAG_NONE, NetworkStatsHistory.FIELD_ALL);
setSingleUid(uid); setSingleUid(uid);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.w(TAG, e); Log.w(TAG, e);
@@ -364,8 +368,9 @@ public final class NetworkUsageStats implements AutoCloseable {
stepUid(); stepUid();
mHistory = null; mHistory = null;
try { try {
mHistory = mSession.getHistoryForUid(mTemplate, getUid(), NetworkStats.SET_ALL, mHistory = mSession.getHistoryForUid(mTemplate, getUid(),
NetworkStats.TAG_NONE, NetworkStatsHistory.FIELD_ALL); android.net.NetworkStats.SET_ALL, android.net.NetworkStats.TAG_NONE,
NetworkStatsHistory.FIELD_ALL);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.w(TAG, e); Log.w(TAG, e);
// Leaving mHistory null // Leaving mHistory null
@@ -405,7 +410,7 @@ public final class NetworkUsageStats implements AutoCloseable {
} }
Bucket bucket = new Bucket(); Bucket bucket = new Bucket();
if (mRecycledSummaryEntry == null) { if (mRecycledSummaryEntry == null) {
mRecycledSummaryEntry = new NetworkStats.Entry(); mRecycledSummaryEntry = new android.net.NetworkStats.Entry();
} }
mSummary.getTotal(mRecycledSummaryEntry); mSummary.getTotal(mRecycledSummaryEntry);
fillBucketFromSummaryEntry(bucket); fillBucketFromSummaryEntry(bucket);

View File

@@ -16,18 +16,17 @@
package android.app.usage; package android.app.usage;
import android.app.usage.NetworkUsageStats.Bucket; import android.app.usage.NetworkStats.Bucket;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkIdentity; import android.net.NetworkIdentity;
import android.net.NetworkTemplate; import android.net.NetworkTemplate;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.UserHandle;
import android.util.Log; import android.util.Log;
/** /**
* Provides access to network usage history and statistics. Usage data is collected in * Provides access to network usage history and statistics. Usage data is collected in
* discrete bins of time called 'Buckets'. See {@link NetworkUsageStats.Bucket} for details. * discrete bins of time called 'Buckets'. See {@link NetworkStats.Bucket} for details.
* <p /> * <p />
* Queries can define a time interval in the form of start and end timestamps (Long.MIN_VALUE and * Queries can define a time interval in the form of start and end timestamps (Long.MIN_VALUE and
* Long.MAX_VALUE can be used to simulate open ended intervals). All queries (except * Long.MAX_VALUE can be used to simulate open ended intervals). All queries (except
@@ -37,15 +36,20 @@ import android.util.Log;
* <h3> * <h3>
* Summary queries * Summary queries
* </h3> * </h3>
* {@link #querySummaryForDevice} <p />
* {@link #querySummaryForUser} <p />
* {@link #querySummary} <p />
* These queries aggregate network usage across the whole interval. Therefore there will be only one * These queries aggregate network usage across the whole interval. Therefore there will be only one
* bucket for a particular key and state combination. In case of the user-wide and device-wide * bucket for a particular key and state combination. In case of the user-wide and device-wide
* summaries a single bucket containing the totalised network usage is returned. * summaries a single bucket containing the totalised network usage is returned.
* <h3> * <h3>
* History queries * History queries
* </h3> * </h3>
* {@link #queryDetailsForUid} <p />
* {@link #queryDetails} <p />
* These queries do not aggregate over time but do aggregate over state. Therefore there can be * These queries do not aggregate over time but do aggregate over state. Therefore there can be
* multiple buckets for a particular key but all Bucket's state is going to be * multiple buckets for a particular key but all Bucket's state is going to be
* {@link NetworkUsageStats.Bucket#STATE_ALL}. * {@link NetworkStats.Bucket#STATE_ALL}.
* <p /> * <p />
* <b>NOTE:</b> This API requires the permission * <b>NOTE:</b> This API requires the permission
* {@link android.Manifest.permission#PACKAGE_USAGE_STATS}, which is a system-level permission and * {@link android.Manifest.permission#PACKAGE_USAGE_STATS}, which is a system-level permission and
@@ -68,7 +72,10 @@ 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. * 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} and uid
* {@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}
@@ -89,7 +96,7 @@ public class NetworkStatsManager {
} }
Bucket bucket = null; Bucket bucket = null;
NetworkUsageStats stats = new NetworkUsageStats(mContext, template, startTime, endTime); NetworkStats stats = new NetworkStats(mContext, template, startTime, endTime);
bucket = stats.getDeviceSummaryForNetwork(startTime, endTime); bucket = stats.getDeviceSummaryForNetwork(startTime, endTime);
stats.close(); stats.close();
@@ -99,6 +106,9 @@ public class NetworkStatsManager {
/** /**
* 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'
* 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. * @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}
@@ -118,8 +128,8 @@ public class NetworkStatsManager {
return null; return null;
} }
NetworkUsageStats stats; NetworkStats stats;
stats = new NetworkUsageStats(mContext, template, startTime, endTime); stats = new NetworkStats(mContext, template, startTime, endTime);
stats.startSummaryEnumeration(startTime, endTime); stats.startSummaryEnumeration(startTime, endTime);
stats.close(); stats.close();
@@ -129,7 +139,9 @@ public class NetworkStatsManager {
/** /**
* 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 or uid. * end timestamps. Not aggregated over state or uid. 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.
* *
* @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}
@@ -142,15 +154,15 @@ public class NetworkStatsManager {
* @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 NetworkUsageStats querySummary(int networkType, String subscriberId, long startTime, public NetworkStats querySummary(int networkType, String subscriberId, long startTime,
long endTime) throws SecurityException, RemoteException { long endTime) throws SecurityException, RemoteException {
NetworkTemplate template = createTemplate(networkType, subscriberId); NetworkTemplate template = createTemplate(networkType, subscriberId);
if (template == null) { if (template == null) {
return null; return null;
} }
NetworkUsageStats result; NetworkStats result;
result = new NetworkUsageStats(mContext, template, startTime, endTime); result = new NetworkStats(mContext, template, startTime, endTime);
result.startSummaryEnumeration(startTime, endTime); result.startSummaryEnumeration(startTime, endTime);
return result; return result;
@@ -158,7 +170,9 @@ public class NetworkStatsManager {
/** /**
* Query network usage statistics details. Only usable for uids belonging to calling user. * Query network usage statistics details. Only usable for uids belonging to calling user.
* Result is aggregated over state but not aggregated over time. * 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.
* *
* @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}
@@ -172,15 +186,15 @@ public class NetworkStatsManager {
* @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 NetworkUsageStats queryDetailsForUid(int networkType, String subscriberId, public NetworkStats queryDetailsForUid(int networkType, String subscriberId,
long startTime, long endTime, int uid) throws SecurityException, RemoteException { long startTime, long endTime, int uid) throws SecurityException, RemoteException {
NetworkTemplate template = createTemplate(networkType, subscriberId); NetworkTemplate template = createTemplate(networkType, subscriberId);
if (template == null) { if (template == null) {
return null; return null;
} }
NetworkUsageStats result; NetworkStats result;
result = new NetworkUsageStats(mContext, template, startTime, endTime); result = new NetworkStats(mContext, template, startTime, endTime);
result.startHistoryEnumeration(uid); result.startHistoryEnumeration(uid);
return result; return result;
@@ -188,7 +202,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. * 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.
* *
* @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}
@@ -201,14 +217,14 @@ public class NetworkStatsManager {
* @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 NetworkUsageStats queryDetails(int networkType, String subscriberId, long startTime, public NetworkStats queryDetails(int networkType, String subscriberId, long startTime,
long endTime) throws SecurityException, RemoteException { long endTime) throws SecurityException, RemoteException {
NetworkTemplate template = createTemplate(networkType, subscriberId); NetworkTemplate template = createTemplate(networkType, subscriberId);
if (template == null) { if (template == null) {
return null; return null;
} }
NetworkUsageStats result; NetworkStats result;
result = new NetworkUsageStats(mContext, template, startTime, endTime); result = new NetworkStats(mContext, template, startTime, endTime);
result.startUserUidEnumeration(); result.startUserUidEnumeration();
return result; return result;
} }