Allow null subscriberId in NetworkStatsManager.
Use a MATCH_MOBILE_WILDCARD template to avoid filtering by
subscriberId when querying statistics from NetworkStatsService.
Bug: 74038898
Change-Id: I8296220472a9ba37044dd1a5ede9bdb45d3ed339
Fixes: 74038898
Test: runtest frameworks-net, CTS tests pass
Merged-In: I1e4e283c6eaecf33d12488e41e0c524f6ff83954
Merged-In: Ia84d2c7cc63bf8b8bf30f133e0382fd7103bf490
(cherry-picked from aosp I4b39e7031416cb33b23d89aa36ff0f774eaa942f)
diff --git a/core/java/android/app/usage/NetworkStatsManager.java b/core/java/android/app/usage/NetworkStatsManager.java
index 5576e86..2357637 100644
--- a/core/java/android/app/usage/NetworkStatsManager.java
+++ b/core/java/android/app/usage/NetworkStatsManager.java
@@ -37,6 +37,8 @@
import android.os.ServiceManager.ServiceNotFoundException;
import android.util.Log;
+import com.android.internal.annotations.VisibleForTesting;
+
/**
* Provides access to network usage history and statistics. Usage data is collected in
* discrete bins of time called 'Buckets'. See {@link NetworkStats.Bucket} for details.
@@ -107,9 +109,15 @@
* {@hide}
*/
public NetworkStatsManager(Context context) throws ServiceNotFoundException {
+ this(context, INetworkStatsService.Stub.asInterface(
+ ServiceManager.getServiceOrThrow(Context.NETWORK_STATS_SERVICE)));
+ }
+
+ /** @hide */
+ @VisibleForTesting
+ public NetworkStatsManager(Context context, INetworkStatsService service) {
mContext = context;
- mService = INetworkStatsService.Stub.asInterface(
- ServiceManager.getServiceOrThrow(Context.NETWORK_STATS_SERVICE));
+ mService = service;
setPollOnOpen(true);
}
@@ -135,7 +143,8 @@
public Bucket querySummaryForDevice(NetworkTemplate template,
long startTime, long endTime) throws SecurityException, RemoteException {
Bucket bucket = null;
- NetworkStats stats = new NetworkStats(mContext, template, mFlags, startTime, endTime);
+ NetworkStats stats = new NetworkStats(mContext, template, mFlags, startTime, endTime,
+ mService);
bucket = stats.getDeviceSummaryForNetwork();
stats.close();
@@ -208,7 +217,7 @@
}
NetworkStats stats;
- stats = new NetworkStats(mContext, template, mFlags, startTime, endTime);
+ stats = new NetworkStats(mContext, template, mFlags, startTime, endTime, mService);
stats.startSummaryEnumeration();
stats.close();
@@ -245,7 +254,7 @@
}
NetworkStats result;
- result = new NetworkStats(mContext, template, mFlags, startTime, endTime);
+ result = new NetworkStats(mContext, template, mFlags, startTime, endTime, mService);
result.startSummaryEnumeration();
return result;
@@ -295,7 +304,7 @@
NetworkStats result;
try {
- result = new NetworkStats(mContext, template, mFlags, startTime, endTime);
+ result = new NetworkStats(mContext, template, mFlags, startTime, endTime, mService);
result.startHistoryEnumeration(uid, tag);
} catch (RemoteException e) {
Log.e(TAG, "Error while querying stats for uid=" + uid + " tag=" + tag, e);
@@ -341,7 +350,7 @@
}
NetworkStats result;
- result = new NetworkStats(mContext, template, mFlags, startTime, endTime);
+ result = new NetworkStats(mContext, template, mFlags, startTime, endTime, mService);
result.startUserUidEnumeration();
return result;
}
@@ -451,19 +460,20 @@
}
private static NetworkTemplate createTemplate(int networkType, String subscriberId) {
- NetworkTemplate template = null;
+ final NetworkTemplate template;
switch (networkType) {
- case ConnectivityManager.TYPE_MOBILE: {
- template = NetworkTemplate.buildTemplateMobileAll(subscriberId);
- } break;
- case ConnectivityManager.TYPE_WIFI: {
+ case ConnectivityManager.TYPE_MOBILE:
+ template = subscriberId == null
+ ? NetworkTemplate.buildTemplateMobileWildcard()
+ : NetworkTemplate.buildTemplateMobileAll(subscriberId);
+ break;
+ case ConnectivityManager.TYPE_WIFI:
template = NetworkTemplate.buildTemplateWifiWildcard();
- } break;
- default: {
+ break;
+ default:
throw new IllegalArgumentException("Cannot create template for network type "
+ networkType + ", subscriberId '"
+ NetworkIdentity.scrubSubscriberId(subscriberId) + "'.");
- }
}
return template;
}