summaryrefslogtreecommitdiffstats
path: root/core/java/android/net
diff options
context:
space:
mode:
Diffstat (limited to 'core/java/android/net')
-rw-r--r--core/java/android/net/INetworkStatsService.aidl3
-rw-r--r--core/java/android/net/NetworkTemplate.java46
-rw-r--r--core/java/android/net/TrafficStats.java106
3 files changed, 127 insertions, 28 deletions
diff --git a/core/java/android/net/INetworkStatsService.aidl b/core/java/android/net/INetworkStatsService.aidl
index b4f6367..08d4c6c 100644
--- a/core/java/android/net/INetworkStatsService.aidl
+++ b/core/java/android/net/INetworkStatsService.aidl
@@ -32,6 +32,9 @@ interface INetworkStatsService {
/** Return data layer snapshot of UID network usage. */
NetworkStats getDataLayerSnapshotForUid(int uid);
+ /** Return set of any ifaces associated with mobile networks since boot. */
+ String[] getMobileIfaces();
+
/** Increment data layer count of operations performed for UID and tag. */
void incrementOperationCount(int uid, int tag, int operationCount);
diff --git a/core/java/android/net/NetworkTemplate.java b/core/java/android/net/NetworkTemplate.java
index 50432a1..39a4d7b 100644
--- a/core/java/android/net/NetworkTemplate.java
+++ b/core/java/android/net/NetworkTemplate.java
@@ -48,6 +48,8 @@ public class NetworkTemplate implements Parcelable {
public static final int MATCH_MOBILE_4G = 3;
public static final int MATCH_WIFI = 4;
public static final int MATCH_ETHERNET = 5;
+ public static final int MATCH_MOBILE_WILDCARD = 6;
+ public static final int MATCH_WIFI_WILDCARD = 7;
/**
* Set of {@link NetworkInfo#getType()} that reflect data usage.
@@ -86,11 +88,19 @@ public class NetworkTemplate implements Parcelable {
}
/**
+ * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks,
+ * regardless of IMSI.
+ */
+ public static NetworkTemplate buildTemplateMobileWildcard() {
+ return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
+ }
+
+ /**
* Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
* regardless of SSID.
*/
public static NetworkTemplate buildTemplateWifiWildcard() {
- return new NetworkTemplate(MATCH_WIFI, null, null);
+ return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
}
@Deprecated
@@ -198,6 +208,10 @@ public class NetworkTemplate implements Parcelable {
return matchesWifi(ident);
case MATCH_ETHERNET:
return matchesEthernet(ident);
+ case MATCH_MOBILE_WILDCARD:
+ return matchesMobileWildcard(ident);
+ case MATCH_WIFI_WILDCARD:
+ return matchesWifiWildcard(ident);
default:
throw new IllegalArgumentException("unknown network template");
}
@@ -257,13 +271,7 @@ public class NetworkTemplate implements Parcelable {
private boolean matchesWifi(NetworkIdentity ident) {
switch (ident.mType) {
case TYPE_WIFI:
- if (mNetworkId == null) {
- return true;
- } else {
- return Objects.equal(mNetworkId, ident.mNetworkId);
- }
- case TYPE_WIFI_P2P:
- return mNetworkId == null;
+ return Objects.equal(mNetworkId, ident.mNetworkId);
default:
return false;
}
@@ -279,6 +287,24 @@ public class NetworkTemplate implements Parcelable {
return false;
}
+ private boolean matchesMobileWildcard(NetworkIdentity ident) {
+ if (ident.mType == TYPE_WIMAX) {
+ return true;
+ } else {
+ return contains(DATA_USAGE_NETWORK_TYPES, ident.mType);
+ }
+ }
+
+ private boolean matchesWifiWildcard(NetworkIdentity ident) {
+ switch (ident.mType) {
+ case TYPE_WIFI:
+ case TYPE_WIFI_P2P:
+ return true;
+ default:
+ return false;
+ }
+ }
+
private static String getMatchRuleName(int matchRule) {
switch (matchRule) {
case MATCH_MOBILE_3G_LOWER:
@@ -291,6 +317,10 @@ public class NetworkTemplate implements Parcelable {
return "WIFI";
case MATCH_ETHERNET:
return "ETHERNET";
+ case MATCH_MOBILE_WILDCARD:
+ return "MOBILE_WILDCARD";
+ case MATCH_WIFI_WILDCARD:
+ return "WIFI_WILDCARD";
default:
return "UNKNOWN";
}
diff --git a/core/java/android/net/TrafficStats.java b/core/java/android/net/TrafficStats.java
index ee3e165..e437d2e 100644
--- a/core/java/android/net/TrafficStats.java
+++ b/core/java/android/net/TrafficStats.java
@@ -88,6 +88,16 @@ public class TrafficStats {
*/
public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03;
+ private static INetworkStatsService sStatsService;
+
+ private synchronized static INetworkStatsService getStatsService() {
+ if (sStatsService == null) {
+ sStatsService = INetworkStatsService.Stub.asInterface(
+ ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
+ }
+ return sStatsService;
+ }
+
/**
* Snapshot of {@link NetworkStats} when the currently active profiling
* session started, or {@code null} if no session active.
@@ -228,11 +238,9 @@ public class TrafficStats {
* @param operationCount Number of operations to increment count by.
*/
public static void incrementOperationCount(int tag, int operationCount) {
- final INetworkStatsService statsService = INetworkStatsService.Stub.asInterface(
- ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
final int uid = android.os.Process.myUid();
try {
- statsService.incrementOperationCount(uid, tag, operationCount);
+ getStatsService().incrementOperationCount(uid, tag, operationCount);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
@@ -257,7 +265,13 @@ public class TrafficStats {
* @return number of packets. If the statistics are not supported by this device,
* {@link #UNSUPPORTED} will be returned.
*/
- public static native long getMobileTxPackets();
+ public static long getMobileTxPackets() {
+ long total = 0;
+ for (String iface : getMobileIfaces()) {
+ total += getTxPackets(iface);
+ }
+ return total;
+ }
/**
* Get the total number of packets received through the mobile interface.
@@ -265,7 +279,13 @@ public class TrafficStats {
* @return number of packets. If the statistics are not supported by this device,
* {@link #UNSUPPORTED} will be returned.
*/
- public static native long getMobileRxPackets();
+ public static long getMobileRxPackets() {
+ long total = 0;
+ for (String iface : getMobileIfaces()) {
+ total += getRxPackets(iface);
+ }
+ return total;
+ }
/**
* Get the total number of bytes transmitted through the mobile interface.
@@ -273,7 +293,13 @@ public class TrafficStats {
* @return number of bytes. If the statistics are not supported by this device,
* {@link #UNSUPPORTED} will be returned.
*/
- public static native long getMobileTxBytes();
+ public static long getMobileTxBytes() {
+ long total = 0;
+ for (String iface : getMobileIfaces()) {
+ total += getTxBytes(iface);
+ }
+ return total;
+ }
/**
* Get the total number of bytes received through the mobile interface.
@@ -281,7 +307,13 @@ public class TrafficStats {
* @return number of bytes. If the statistics are not supported by this device,
* {@link #UNSUPPORTED} will be returned.
*/
- public static native long getMobileRxBytes();
+ public static long getMobileRxBytes() {
+ long total = 0;
+ for (String iface : getMobileIfaces()) {
+ total += getRxBytes(iface);
+ }
+ return total;
+ }
/**
* Get the total number of packets transmitted through the specified interface.
@@ -290,7 +322,9 @@ public class TrafficStats {
* {@link #UNSUPPORTED} will be returned.
* @hide
*/
- public static native long getTxPackets(String iface);
+ public static long getTxPackets(String iface) {
+ return nativeGetIfaceStat(iface, TYPE_TX_PACKETS);
+ }
/**
* Get the total number of packets received through the specified interface.
@@ -299,7 +333,9 @@ public class TrafficStats {
* {@link #UNSUPPORTED} will be returned.
* @hide
*/
- public static native long getRxPackets(String iface);
+ public static long getRxPackets(String iface) {
+ return nativeGetIfaceStat(iface, TYPE_RX_PACKETS);
+ }
/**
* Get the total number of bytes transmitted through the specified interface.
@@ -308,7 +344,9 @@ public class TrafficStats {
* {@link #UNSUPPORTED} will be returned.
* @hide
*/
- public static native long getTxBytes(String iface);
+ public static long getTxBytes(String iface) {
+ return nativeGetIfaceStat(iface, TYPE_TX_BYTES);
+ }
/**
* Get the total number of bytes received through the specified interface.
@@ -317,8 +355,9 @@ public class TrafficStats {
* {@link #UNSUPPORTED} will be returned.
* @hide
*/
- public static native long getRxBytes(String iface);
-
+ public static long getRxBytes(String iface) {
+ return nativeGetIfaceStat(iface, TYPE_RX_BYTES);
+ }
/**
* Get the total number of packets sent through all network interfaces.
@@ -326,7 +365,9 @@ public class TrafficStats {
* @return the number of packets. If the statistics are not supported by this device,
* {@link #UNSUPPORTED} will be returned.
*/
- public static native long getTotalTxPackets();
+ public static long getTotalTxPackets() {
+ return nativeGetTotalStat(TYPE_TX_PACKETS);
+ }
/**
* Get the total number of packets received through all network interfaces.
@@ -334,7 +375,9 @@ public class TrafficStats {
* @return number of packets. If the statistics are not supported by this device,
* {@link #UNSUPPORTED} will be returned.
*/
- public static native long getTotalRxPackets();
+ public static long getTotalRxPackets() {
+ return nativeGetTotalStat(TYPE_RX_PACKETS);
+ }
/**
* Get the total number of bytes sent through all network interfaces.
@@ -342,7 +385,9 @@ public class TrafficStats {
* @return number of bytes. If the statistics are not supported by this device,
* {@link #UNSUPPORTED} will be returned.
*/
- public static native long getTotalTxBytes();
+ public static long getTotalTxBytes() {
+ return nativeGetTotalStat(TYPE_TX_BYTES);
+ }
/**
* Get the total number of bytes received through all network interfaces.
@@ -350,7 +395,9 @@ public class TrafficStats {
* @return number of bytes. If the statistics are not supported by this device,
* {@link #UNSUPPORTED} will be returned.
*/
- public static native long getTotalRxBytes();
+ public static long getTotalRxBytes() {
+ return nativeGetTotalStat(TYPE_RX_BYTES);
+ }
/**
* Get the number of bytes sent through the network for this UID.
@@ -483,7 +530,6 @@ public class TrafficStats {
*/
public static native long getUidTcpRxSegments(int uid);
-
/**
* Get the number of UDP packets sent for this UID.
* Includes DNS requests.
@@ -515,13 +561,33 @@ public class TrafficStats {
* special permission.
*/
private static NetworkStats getDataLayerSnapshotForUid(Context context) {
- final INetworkStatsService statsService = INetworkStatsService.Stub.asInterface(
- ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
final int uid = android.os.Process.myUid();
try {
- return statsService.getDataLayerSnapshotForUid(uid);
+ return getStatsService().getDataLayerSnapshotForUid(uid);
+ } catch (RemoteException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Return set of any ifaces associated with mobile networks since boot.
+ * Interfaces are never removed from this list, so counters should always be
+ * monotonic.
+ */
+ private static String[] getMobileIfaces() {
+ try {
+ return getStatsService().getMobileIfaces();
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
+
+ // NOTE: keep these in sync with android_net_TrafficStats.cpp
+ private static final int TYPE_RX_BYTES = 0;
+ private static final int TYPE_RX_PACKETS = 1;
+ private static final int TYPE_TX_BYTES = 2;
+ private static final int TYPE_TX_PACKETS = 3;
+
+ private static native long nativeGetTotalStat(int type);
+ private static native long nativeGetIfaceStat(String iface, int type);
}