Merge "Addressing API council comments on NetworkStatsManager." into nyc-dev am: dc5f558640 am: 7598ac2aa7

am: 8b2dd44f2f

* commit '8b2dd44f2f30a9dd22799ee30dd4b9c5a093aae5':
  Addressing API council comments on NetworkStatsManager.

Change-Id: Ie6455799758f0bdb67440f72c266effaf412e22f
This commit is contained in:
Antonio Cansado
2016-05-10 21:14:09 +00:00
committed by android-build-merger
6 changed files with 167 additions and 223 deletions

View File

@@ -192,15 +192,10 @@ public final class NetworkStats implements AutoCloseable {
*/ */
public static final int ROAMING_YES = 0x2; public static final int ROAMING_YES = 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 * Special TAG value for total data across all tags
*/ */
public static final int TAG_ALL = android.net.NetworkStats.TAG_NONE; public static final int TAG_NONE = android.net.NetworkStats.TAG_NONE;
private int mUid; private int mUid;
private int mTag; private int mTag;
@@ -232,8 +227,7 @@ public final class NetworkStats implements AutoCloseable {
private static int convertTag(int tag) { private static int convertTag(int tag) {
switch (tag) { switch (tag) {
case android.net.NetworkStats.TAG_ALL: return TAG_ANY; case android.net.NetworkStats.TAG_NONE: return TAG_NONE;
case android.net.NetworkStats.TAG_NONE: return TAG_ALL;
} }
return tag; return tag;
} }
@@ -417,9 +411,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(boolean includeTags) throws RemoteException { void startSummaryEnumeration() throws RemoteException {
mSummary = mSession.getSummaryForAllUid(mTemplate, mStartTimeStamp, mEndTimeStamp, mSummary = mSession.getSummaryForAllUid(mTemplate, mStartTimeStamp, mEndTimeStamp,
includeTags); false /* includeTags */);
mEnumerationIndex = 0; mEnumerationIndex = 0;
} }

View File

@@ -106,7 +106,7 @@ public class NetworkStatsManager {
* device. Result is a single Bucket aggregated over time, state, uid, tag and roaming. This * 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 * 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 * 'endTime' parameters. State is going to be {@link NetworkStats.Bucket#STATE_ALL}, uid
* {@link NetworkStats.Bucket#UID_ALL}, tag {@link NetworkStats.Bucket#TAG_ALL} * {@link NetworkStats.Bucket#UID_ALL}, tag {@link NetworkStats.Bucket#TAG_NONE}
* and roaming {@link NetworkStats.Bucket#ROAMING_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.
@@ -122,8 +122,11 @@ public class NetworkStatsManager {
*/ */
public Bucket querySummaryForDevice(int networkType, String subscriberId, public Bucket querySummaryForDevice(int networkType, String subscriberId,
long startTime, long endTime) throws SecurityException, RemoteException { long startTime, long endTime) throws SecurityException, RemoteException {
NetworkTemplate template = createTemplate(networkType, subscriberId); NetworkTemplate template;
if (template == null) { try {
template = createTemplate(networkType, subscriberId);
} catch (IllegalArgumentException e) {
if (DBG) Log.e(TAG, "Cannot create template", e);
return null; return null;
} }
@@ -135,22 +138,11 @@ 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} and uid * and 'endTime' parameters, state is going to be {@link NetworkStats.Bucket#STATE_ALL} and uid
* {@link NetworkStats.Bucket#UID_ALL}. * {@link NetworkStats.Bucket#UID_ALL}.
* *
* @param networkType As defined in {@link ConnectivityManager}, e.g. * @param networkType As defined in {@link ConnectivityManager}, e.g.
@@ -161,42 +153,33 @@ 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, boolean includeTags) throws SecurityException, RemoteException { long endTime) throws SecurityException, RemoteException {
NetworkTemplate template = createTemplate(networkType, subscriberId); NetworkTemplate template;
if (template == null) { try {
template = createTemplate(networkType, subscriberId);
} catch (IllegalArgumentException e) {
if (DBG) Log.e(TAG, "Cannot create template", e);
return null; return null;
} }
NetworkStats stats; NetworkStats stats;
stats = new NetworkStats(mContext, template, startTime, endTime); stats = new NetworkStats(mContext, template, startTime, endTime);
stats.startSummaryEnumeration(includeTags); stats.startSummaryEnumeration();
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 or uid or tag. This means buckets' start and end * 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, * timestamps are going to be the same as the 'startTime' and 'endTime' parameters.
* uid and tag are going to vary. * State and uid are going to vary, and tag is going to be the same.
* *
* @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}
@@ -206,21 +189,22 @@ 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, boolean includeTags) throws SecurityException, RemoteException { long endTime) throws SecurityException, RemoteException {
NetworkTemplate template = createTemplate(networkType, subscriberId); NetworkTemplate template;
if (template == null) { try {
template = createTemplate(networkType, subscriberId);
} catch (IllegalArgumentException e) {
if (DBG) Log.e(TAG, "Cannot create template", e);
return null; return null;
} }
NetworkStats result; NetworkStats result;
result = new NetworkStats(mContext, template, startTime, endTime); result = new NetworkStats(mContext, template, startTime, endTime);
result.startSummaryEnumeration(includeTags); result.startSummaryEnumeration();
return result; return result;
} }
@@ -233,7 +217,7 @@ public class NetworkStatsManager {
public NetworkStats 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 {
return queryDetailsForUidTag(networkType, subscriberId, startTime, endTime, uid, return queryDetailsForUidTag(networkType, subscriberId, startTime, endTime, uid,
NetworkStats.Bucket.TAG_ALL); NetworkStats.Bucket.TAG_NONE);
} }
/** /**
@@ -255,22 +239,28 @@ 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 * @param tag TAG of interest. Use {@link NetworkStats.Bucket#TAG_NONE} for no tags.
* {@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 queryDetailsForUidTag(int networkType, String subscriberId, public NetworkStats queryDetailsForUidTag(int networkType, String subscriberId,
long startTime, long endTime, int uid, int tag) throws SecurityException, long startTime, long endTime, int uid, int tag) {
RemoteException { NetworkTemplate template;
NetworkTemplate template = createTemplate(networkType, subscriberId); try {
if (template == null) { template = createTemplate(networkType, subscriberId);
} catch (IllegalArgumentException e) {
if (DBG) Log.e(TAG, "Cannot create template", e);
return null; return null;
} }
NetworkStats result; NetworkStats result;
try {
result = new NetworkStats(mContext, template, startTime, endTime); result = new NetworkStats(mContext, template, startTime, endTime);
result.startHistoryEnumeration(uid, tag); result.startHistoryEnumeration(uid, tag);
} catch (RemoteException e) {
Log.e(TAG, "Error while querying stats for uid=" + uid + " tag=" + tag, e);
return null;
}
return result; return result;
} }
@@ -280,7 +270,7 @@ public class NetworkStatsManager {
* 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}, uid will vary, * 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 * tag {@link NetworkStats.Bucket#TAG_NONE} and roaming is going to be
* {@link NetworkStats.Bucket#ROAMING_ALL}. * {@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
@@ -299,44 +289,59 @@ public class NetworkStatsManager {
*/ */
public NetworkStats 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;
if (template == null) { try {
template = createTemplate(networkType, subscriberId);
} catch (IllegalArgumentException e) {
if (DBG) Log.e(TAG, "Cannot create template", e);
return null; return null;
} }
NetworkStats result; NetworkStats result;
result = new NetworkStats(mContext, template, startTime, endTime); result = new NetworkStats(mContext, template, startTime, endTime);
result.startUserUidEnumeration(); result.startUserUidEnumeration();
return result; return result;
} }
/** @removed */
public void registerDataUsageCallback(DataUsagePolicy policy, DataUsageCallback callback,
@Nullable Handler handler) {}
/** @removed */
public void registerDataUsageCallback(DataUsagePolicy policy, UsageCallback callback,
@Nullable Handler handler) {}
/** @removed */
public void unregisterDataUsageCallback(DataUsageCallback callback) {}
/** /**
* Registers to receive notifications about data usage on specified networks and uids. * Registers to receive notifications about data usage on specified networks.
* The callbacks will continue to be called as long as the process is live or
* {@link #unregisterDataUsageCallback} is called.
* *
* @param policy {@link DataUsagePolicy} describing this request. * #see registerUsageCallback(int, String[], long, UsageCallback, Handler)
* @param callback The {@link DataUsageCallback} that the system will call when data usage
* has exceeded the specified threshold.
*/ */
public void registerDataUsageCallback(DataUsagePolicy policy, DataUsageCallback callback) { public void registerUsageCallback(int networkType, String subscriberId, long thresholdBytes,
registerDataUsageCallback(policy, callback, null /* handler */); UsageCallback callback) {
registerUsageCallback(networkType, subscriberId, thresholdBytes, null /* handler */);
} }
/** /**
* Registers to receive notifications about data usage on specified networks and uids. * Registers to receive notifications about data usage on specified networks.
* The callbacks will continue to be called as long as the process is live or
* {@link #unregisterDataUsageCallback} is called.
* *
* @param policy {@link DataUsagePolicy} describing this request. * <p>The callbacks will continue to be called as long as the process is live or
* @param callback The {@link DataUsageCallback} that the system will call when data usage * {@link #unregisterUsageCallback} is called.
*
* @param networkType Type of network to monitor. Either
{@link ConnectivityManager#TYPE_MOBILE} or {@link ConnectivityManager#TYPE_WIFI}.
* @param subscriberId If applicable, the subscriber id of the network interface.
* @param thresholdBytes Threshold in bytes to be notified on.
* @param callback The {@link UsageCallback} that the system will call when data usage
* has exceeded the specified threshold. * has exceeded the specified threshold.
* @param handler to dispatch callback events through, otherwise if {@code null} it uses * @param handler to dispatch callback events through, otherwise if {@code null} it uses
* the calling thread. * the calling thread.
*/ */
public void registerDataUsageCallback(DataUsagePolicy policy, DataUsageCallback callback, public void registerUsageCallback(int networkType, String subscriberId, long thresholdBytes,
@Nullable Handler handler) { UsageCallback callback, @Nullable Handler handler) {
checkNotNull(policy, "DataUsagePolicy cannot be null"); checkNotNull(callback, "UsageCallback cannot be null");
checkNotNull(callback, "DataUsageCallback cannot be null");
final Looper looper; final Looper looper;
if (handler == null) { if (handler == null) {
@@ -345,62 +350,72 @@ public class NetworkStatsManager {
looper = handler.getLooper(); looper = handler.getLooper();
} }
if (DBG) Log.d(TAG, "registerDataUsageCallback called with " + policy); if (DBG) {
Log.d(TAG, "registerUsageCallback called with: {"
+ " networkType=" + networkType
+ " subscriberId=" + subscriberId
+ " thresholdBytes=" + thresholdBytes
+ " }");
}
NetworkTemplate[] templates; NetworkTemplate template = createTemplate(networkType, subscriberId);
if (policy.subscriberIds == null || policy.subscriberIds.length == 0) {
templates = new NetworkTemplate[1];
templates[0] = createTemplate(policy.networkType, null /* subscriberId */);
} else {
templates = new NetworkTemplate[policy.subscriberIds.length];
for (int i = 0; i < policy.subscriberIds.length; i++) {
templates[i] = createTemplate(policy.networkType, policy.subscriberIds[i]);
}
}
DataUsageRequest request = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, DataUsageRequest request = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET,
templates, policy.uids, policy.thresholdInBytes); template, thresholdBytes);
try { try {
CallbackHandler callbackHandler = new CallbackHandler(looper, callback); CallbackHandler callbackHandler = new CallbackHandler(looper, networkType,
callback.request = mService.registerDataUsageCallback( subscriberId, callback);
callback.request = mService.registerUsageCallback(
mContext.getOpPackageName(), request, new Messenger(callbackHandler), mContext.getOpPackageName(), request, new Messenger(callbackHandler),
new Binder()); new Binder());
if (DBG) Log.d(TAG, "registerDataUsageCallback returned " + callback.request); if (DBG) Log.d(TAG, "registerUsageCallback returned " + callback.request);
if (callback.request == null) { if (callback.request == null) {
Log.e(TAG, "Request from callback is null; should not happen"); Log.e(TAG, "Request from callback is null; should not happen");
} }
} catch (RemoteException e) { } catch (RemoteException e) {
if (DBG) Log.d(TAG, "Remote exception when registering callback"); if (DBG) Log.d(TAG, "Remote exception when registering callback");
throw e.rethrowFromSystemServer();
} }
} }
/** /**
* Unregisters callbacks on data usage. * Unregisters callbacks on data usage.
* *
* @param callback The {@link DataUsageCallback} used when registering. * @param callback The {@link UsageCallback} used when registering.
*/ */
public void unregisterDataUsageCallback(DataUsageCallback callback) { public void unregisterUsageCallback(UsageCallback callback) {
if (callback == null || callback.request == null if (callback == null || callback.request == null
|| callback.request.requestId == DataUsageRequest.REQUEST_ID_UNSET) { || callback.request.requestId == DataUsageRequest.REQUEST_ID_UNSET) {
throw new IllegalArgumentException("Invalid DataUsageCallback"); throw new IllegalArgumentException("Invalid UsageCallback");
} }
try { try {
mService.unregisterDataUsageRequest(callback.request); mService.unregisterUsageRequest(callback.request);
} catch (RemoteException e) { } catch (RemoteException e) {
if (DBG) Log.d(TAG, "Remote exception when unregistering callback"); if (DBG) Log.d(TAG, "Remote exception when unregistering callback");
throw e.rethrowFromSystemServer();
} }
} }
/** /** @removed */
* Base class for data usage callbacks. Should be extended by applications wanting public static abstract class DataUsageCallback {
* notifications. /** @removed */
*/ @Deprecated
public static class DataUsageCallback {
/**
* Called when data usage has reached the given policy threshold.
*/
public void onLimitReached() {} public void onLimitReached() {}
}
/**
* Base class for usage callbacks. Should be extended by applications wanting notifications.
*/
public static abstract class UsageCallback {
/**
* Called when data usage has reached the given threshold.
*/
public abstract void onThresholdReached(int networkType, String subscriberId);
/**
* @hide used for internal bookkeeping
*/
private DataUsageRequest request; private DataUsageRequest request;
} }
@@ -414,18 +429,24 @@ public class NetworkStatsManager {
template = NetworkTemplate.buildTemplateWifiWildcard(); template = NetworkTemplate.buildTemplateWifiWildcard();
} break; } break;
default: { default: {
Log.w(TAG, "Cannot create template for network type " + networkType throw new IllegalArgumentException("Cannot create template for network type "
+ ", subscriberId '" + NetworkIdentity.scrubSubscriberId(subscriberId) + + networkType + ", subscriberId '"
"'."); + NetworkIdentity.scrubSubscriberId(subscriberId) + "'.");
} }
} }
return template; return template;
} }
private static class CallbackHandler extends Handler { private static class CallbackHandler extends Handler {
private DataUsageCallback mCallback; private final int mNetworkType;
CallbackHandler(Looper looper, DataUsageCallback callback) { private final String mSubscriberId;
private UsageCallback mCallback;
CallbackHandler(Looper looper, int networkType, String subscriberId,
UsageCallback callback) {
super(looper); super(looper);
mNetworkType = networkType;
mSubscriberId = subscriberId;
mCallback = callback; mCallback = callback;
} }
@@ -437,7 +458,7 @@ public class NetworkStatsManager {
switch (message.what) { switch (message.what) {
case CALLBACK_LIMIT_REACHED: { case CALLBACK_LIMIT_REACHED: {
if (mCallback != null) { if (mCallback != null) {
mCallback.onLimitReached(); mCallback.onThresholdReached(mNetworkType, mSubscriberId);
} else { } else {
Log.e(TAG, "limit reached with released callback for " + request); Log.e(TAG, "limit reached with released callback for " + request);
} }

View File

@@ -20,7 +20,6 @@ import android.net.NetworkTemplate;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
/** /**
@@ -28,56 +27,33 @@ import java.util.Objects;
* {@link android.app.usage.NetworkStatsManager#registerDataUsageCallback}. * {@link android.app.usage.NetworkStatsManager#registerDataUsageCallback}.
* If no {@code uid}s are set, callbacks are restricted to device-owners, * If no {@code uid}s are set, callbacks are restricted to device-owners,
* carrier-privileged apps, or system apps. * carrier-privileged apps, or system apps.
*
* @hide
*/ */
public final class DataUsageRequest implements Parcelable { public final class DataUsageRequest implements Parcelable {
/**
* @hide
*/
public static final String PARCELABLE_KEY = "DataUsageRequest"; public static final String PARCELABLE_KEY = "DataUsageRequest";
/**
* @hide
*/
public static final int REQUEST_ID_UNSET = 0; public static final int REQUEST_ID_UNSET = 0;
/** /**
* Identifies the request. {@link DataUsageRequest}s should only be constructed by * Identifies the request. {@link DataUsageRequest}s should only be constructed by
* the Framework and it is used internally to identify the request. * the Framework and it is used internally to identify the request.
* @hide
*/ */
public final int requestId; public final int requestId;
/** /**
* Set of {@link NetworkTemplate}s describing the networks to monitor. * {@link NetworkTemplate} describing the network to monitor.
* @hide
*/ */
public final NetworkTemplate[] templates; public final NetworkTemplate template;
/**
* Set of UIDs of which to monitor data usage.
*
* <p>If not {@code null}, the caller will be notified when any of the uids exceed
* the given threshold. If {@code null} all uids for which the calling process has access
* to stats will be monitored.
* @hide
*/
public final int[] uids;
/** /**
* Threshold in bytes to be notified on. * Threshold in bytes to be notified on.
* @hide
*/ */
public final long thresholdInBytes; public final long thresholdInBytes;
/** public DataUsageRequest(int requestId, NetworkTemplate template, long thresholdInBytes) {
* @hide
*/
public DataUsageRequest(int requestId, NetworkTemplate[] templates, int[] uids,
long thresholdInBytes) {
this.requestId = requestId; this.requestId = requestId;
this.templates = templates; this.template = template;
this.uids = uids;
this.thresholdInBytes = thresholdInBytes; this.thresholdInBytes = thresholdInBytes;
} }
@@ -89,8 +65,7 @@ public final class DataUsageRequest implements Parcelable {
@Override @Override
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(requestId); dest.writeInt(requestId);
dest.writeTypedArray(templates, flags); dest.writeParcelable(template, flags);
dest.writeIntArray(uids);
dest.writeLong(thresholdInBytes); dest.writeLong(thresholdInBytes);
} }
@@ -99,11 +74,10 @@ public final class DataUsageRequest implements Parcelable {
@Override @Override
public DataUsageRequest createFromParcel(Parcel in) { public DataUsageRequest createFromParcel(Parcel in) {
int requestId = in.readInt(); int requestId = in.readInt();
NetworkTemplate[] templates = in.createTypedArray(NetworkTemplate.CREATOR); NetworkTemplate template = in.readParcelable(null);
int[] uids = in.createIntArray();
long thresholdInBytes = in.readLong(); long thresholdInBytes = in.readLong();
DataUsageRequest result = new DataUsageRequest(requestId, DataUsageRequest result = new DataUsageRequest(requestId, template,
templates, uids, thresholdInBytes); thresholdInBytes);
return result; return result;
} }
@@ -116,8 +90,7 @@ public final class DataUsageRequest implements Parcelable {
@Override @Override
public String toString() { public String toString() {
return "DataUsageRequest [ requestId=" + requestId return "DataUsageRequest [ requestId=" + requestId
+ ", networkTemplates=" + Arrays.toString(templates) + ", networkTemplate=" + template
+ ", uids=" + Arrays.toString(uids)
+ ", thresholdInBytes=" + thresholdInBytes + " ]"; + ", thresholdInBytes=" + thresholdInBytes + " ]";
} }
@@ -126,23 +99,13 @@ public final class DataUsageRequest implements Parcelable {
if (obj instanceof DataUsageRequest == false) return false; if (obj instanceof DataUsageRequest == false) return false;
DataUsageRequest that = (DataUsageRequest) obj; DataUsageRequest that = (DataUsageRequest) obj;
return that.requestId == this.requestId return that.requestId == this.requestId
&& Arrays.deepEquals(that.templates, this.templates) && Objects.equals(that.template, this.template)
&& Arrays.equals(that.uids, this.uids)
&& that.thresholdInBytes == this.thresholdInBytes; && that.thresholdInBytes == this.thresholdInBytes;
} }
@Override @Override
public int hashCode() { public int hashCode() {
// Start with a non-zero constant. return Objects.hash(requestId, template, thresholdInBytes);
int result = 17;
// Include a hash for each field.
result = 31 * result + requestId;
result = 31 * result + Arrays.deepHashCode(templates);
result = 31 * result + Arrays.hashCode(uids);
result = 31 * result + (int) (thresholdInBytes ^ (thresholdInBytes >>> 32));
return result;
} }
} }

View File

@@ -61,10 +61,10 @@ interface INetworkStatsService {
void advisePersistThreshold(long thresholdBytes); void advisePersistThreshold(long thresholdBytes);
/** Registers a callback on data usage. */ /** Registers a callback on data usage. */
DataUsageRequest registerDataUsageCallback(String callingPackage, DataUsageRequest registerUsageCallback(String callingPackage,
in DataUsageRequest request, in Messenger messenger, in IBinder binder); in DataUsageRequest request, in Messenger messenger, in IBinder binder);
/** Unregisters a callback on data usage. */ /** Unregisters a callback on data usage. */
void unregisterDataUsageRequest(in DataUsageRequest request); void unregisterUsageRequest(in DataUsageRequest request);
} }

View File

@@ -81,8 +81,6 @@ class NetworkStatsObservers {
*/ */
public DataUsageRequest register(DataUsageRequest inputRequest, Messenger messenger, public DataUsageRequest register(DataUsageRequest inputRequest, Messenger messenger,
IBinder binder, int callingUid, @NetworkStatsAccess.Level int accessLevel) { IBinder binder, int callingUid, @NetworkStatsAccess.Level int accessLevel) {
checkVisibilityUids(callingUid, accessLevel, inputRequest.uids);
DataUsageRequest request = buildRequest(inputRequest); DataUsageRequest request = buildRequest(inputRequest);
RequestInfo requestInfo = buildRequestInfo(request, messenger, binder, callingUid, RequestInfo requestInfo = buildRequestInfo(request, messenger, binder, callingUid,
accessLevel); accessLevel);
@@ -211,14 +209,13 @@ class NetworkStatsObservers {
+ ". Overriding to a safer default of " + thresholdInBytes + " bytes"); + ". Overriding to a safer default of " + thresholdInBytes + " bytes");
} }
return new DataUsageRequest(mNextDataUsageRequestId.incrementAndGet(), return new DataUsageRequest(mNextDataUsageRequestId.incrementAndGet(),
request.templates, request.uids, thresholdInBytes); request.template, thresholdInBytes);
} }
private RequestInfo buildRequestInfo(DataUsageRequest request, private RequestInfo buildRequestInfo(DataUsageRequest request,
Messenger messenger, IBinder binder, int callingUid, Messenger messenger, IBinder binder, int callingUid,
@NetworkStatsAccess.Level int accessLevel) { @NetworkStatsAccess.Level int accessLevel) {
if (accessLevel <= NetworkStatsAccess.Level.USER if (accessLevel <= NetworkStatsAccess.Level.USER) {
|| request.uids != null && request.uids.length > 0) {
return new UserUsageRequestInfo(this, request, messenger, binder, callingUid, return new UserUsageRequestInfo(this, request, messenger, binder, callingUid,
accessLevel); accessLevel);
} else { } else {
@@ -229,19 +226,6 @@ class NetworkStatsObservers {
} }
} }
private void checkVisibilityUids(int callingUid, @NetworkStatsAccess.Level int accessLevel,
int[] uids) {
if (uids == null) {
return;
}
for (int i = 0; i < uids.length; i++) {
if (!NetworkStatsAccess.isAccessibleToUser(uids[i], callingUid, accessLevel)) {
throw new SecurityException("Caller " + callingUid + " cannot monitor network stats"
+ " for uid " + uids[i] + " with accessLevel " + accessLevel);
}
}
}
/** /**
* Tracks information relevant to a data usage observer. * Tracks information relevant to a data usage observer.
* It will notice when the calling process dies so we can self-expire. * It will notice when the calling process dies so we can self-expire.
@@ -359,16 +343,14 @@ class NetworkStatsObservers {
@Override @Override
protected boolean checkStats() { protected boolean checkStats() {
for (int i = 0; i < mRequest.templates.length; i++) { long bytesSoFar = getTotalBytesForNetwork(mRequest.template);
long bytesSoFar = getTotalBytesForNetwork(mRequest.templates[i]);
if (LOGV) { if (LOGV) {
Slog.v(TAG, bytesSoFar + " bytes so far since notification for " Slog.v(TAG, bytesSoFar + " bytes so far since notification for "
+ mRequest.templates[i]); + mRequest.template);
} }
if (bytesSoFar > mRequest.thresholdInBytes) { if (bytesSoFar > mRequest.thresholdInBytes) {
return true; return true;
} }
}
return false; return false;
} }
@@ -405,22 +387,19 @@ class NetworkStatsObservers {
@Override @Override
protected boolean checkStats() { protected boolean checkStats() {
int[] uidsToMonitor = getUidsToMonitor(); int[] uidsToMonitor = mCollection.getRelevantUids(mAccessLevel, mCallingUid);
for (int i = 0; i < mRequest.templates.length; i++) { for (int i = 0; i < uidsToMonitor.length; i++) {
for (int j = 0; j < uidsToMonitor.length; j++) { long bytesSoFar = getTotalBytesForNetworkUid(mRequest.template, uidsToMonitor[i]);
long bytesSoFar = getTotalBytesForNetworkUid(mRequest.templates[i],
uidsToMonitor[j]);
if (LOGV) { if (LOGV) {
Slog.v(TAG, bytesSoFar + " bytes so far since notification for " Slog.v(TAG, bytesSoFar + " bytes so far since notification for "
+ mRequest.templates[i] + " for uid=" + uidsToMonitor[j]); + mRequest.template + " for uid=" + uidsToMonitor[i]);
} }
if (bytesSoFar > mRequest.thresholdInBytes) { if (bytesSoFar > mRequest.thresholdInBytes) {
return true; return true;
} }
} }
}
return false; return false;
} }
@@ -453,21 +432,6 @@ class NetworkStatsObservers {
return 0; return 0;
} }
} }
private int[] getUidsToMonitor() {
if (mRequest.uids == null || mRequest.uids.length == 0) {
return mCollection.getRelevantUids(mAccessLevel, mCallingUid);
}
// Pick only uids from the request that are currently accessible to the user
IntArray accessibleUids = new IntArray(mRequest.uids.length);
for (int i = 0; i < mRequest.uids.length; i++) {
int uid = mRequest.uids[i];
if (NetworkStatsAccess.isAccessibleToUser(uid, mCallingUid, mAccessLevel)) {
accessibleUids.add(uid);
}
}
return accessibleUids.toArray();
}
} }
private static class StatsContext { private static class StatsContext {

View File

@@ -578,9 +578,12 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
if (tag == TAG_NONE) { if (tag == TAG_NONE) {
return getUidComplete().getHistory(template, uid, set, tag, fields, start, end, return getUidComplete().getHistory(template, uid, set, tag, fields, start, end,
accessLevel); accessLevel);
} else { } else if (uid == Binder.getCallingUid()) {
return getUidTagComplete().getHistory(template, uid, set, tag, fields, return getUidTagComplete().getHistory(template, uid, set, tag, fields,
start, end, accessLevel); start, end, accessLevel);
} else {
throw new SecurityException("Calling package " + mCallingPackage
+ " cannot access tag information from a different uid");
} }
} }
@@ -761,12 +764,11 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
@Override @Override
public DataUsageRequest registerDataUsageCallback(String callingPackage, public DataUsageRequest registerUsageCallback(String callingPackage,
DataUsageRequest request, Messenger messenger, IBinder binder) { DataUsageRequest request, Messenger messenger, IBinder binder) {
checkNotNull(callingPackage, "calling package is null"); checkNotNull(callingPackage, "calling package is null");
checkNotNull(request, "DataUsageRequest is null"); checkNotNull(request, "DataUsageRequest is null");
checkNotNull(request.templates, "NetworkTemplate is null"); checkNotNull(request.template, "NetworkTemplate is null");
checkArgument(request.templates.length > 0);
checkNotNull(messenger, "messenger is null"); checkNotNull(messenger, "messenger is null");
checkNotNull(binder, "binder is null"); checkNotNull(binder, "binder is null");
@@ -788,7 +790,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
@Override @Override
public void unregisterDataUsageRequest(DataUsageRequest request) { public void unregisterUsageRequest(DataUsageRequest request) {
checkNotNull(request, "DataUsageRequest is null"); checkNotNull(request, "DataUsageRequest is null");
int callingUid = Binder.getCallingUid(); int callingUid = Binder.getCallingUid();