blob: e7436be273a33e97dd9dedcae2c22225b7d6ebba [file] [log] [blame]
Ken Shirrifff7d0b012009-12-07 15:56:05 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
Christopher Tatea2496de2014-08-06 14:19:56 -070019import android.annotation.SystemApi;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070020import android.app.DownloadManager;
21import android.app.backup.BackupManager;
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -070022import android.app.usage.NetworkStatsManager;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070023import android.content.Context;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070024import android.media.MediaPlayer;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070025import android.os.RemoteException;
26import android.os.ServiceManager;
27
Jesse Wilson8568db52011-06-28 19:06:31 -070028import com.android.server.NetworkManagementSocketTagger;
Ken Shirrifff7d0b012009-12-07 15:56:05 -080029
Jesse Wilson8568db52011-06-28 19:06:31 -070030import dalvik.system.SocketTagger;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070031
Jeff Sharkeyf0d76332015-12-04 15:21:52 -070032import java.net.DatagramSocket;
Jeff Sharkey43be1742011-04-21 18:45:43 -070033import java.net.Socket;
34import java.net.SocketException;
Ken Shirrifff7d0b012009-12-07 15:56:05 -080035
36/**
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -070037 * Class that provides network traffic statistics. These statistics include
Dan Egnor2b4abcd2010-04-07 17:30:50 -070038 * bytes transmitted and received and network packets transmitted and received,
39 * over all interfaces, over the mobile interface, and on a per-UID basis.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080040 * <p>
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -070041 * These statistics may not be available on all platforms. If the statistics are
42 * not supported by this device, {@link #UNSUPPORTED} will be returned.
43 * <p>
44 * Note that the statistics returned by this class reset and start from zero
45 * after every reboot. To access more robust historical network statistics data,
46 * use {@link NetworkStatsManager} instead.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080047 */
48public class TrafficStats {
49 /**
50 * The return value to indicate that the device does not support the statistic.
51 */
52 public final static int UNSUPPORTED = -1;
53
Jeff Sharkey241dde22012-02-03 14:50:07 -080054 /** @hide */
55 public static final long KB_IN_BYTES = 1024;
56 /** @hide */
57 public static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
58 /** @hide */
59 public static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
Jeff Sharkey48877892015-03-18 11:27:19 -070060 /** @hide */
61 public static final long TB_IN_BYTES = GB_IN_BYTES * 1024;
Jeff Sharkeyb521fea2015-06-15 21:09:10 -070062 /** @hide */
63 public static final long PB_IN_BYTES = TB_IN_BYTES * 1024;
Jeff Sharkey241dde22012-02-03 14:50:07 -080064
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070065 /**
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070066 * Special UID value used when collecting {@link NetworkStatsHistory} for
67 * removed applications.
68 *
69 * @hide
70 */
71 public static final int UID_REMOVED = -4;
72
73 /**
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070074 * Special UID value used when collecting {@link NetworkStatsHistory} for
75 * tethering traffic.
76 *
77 * @hide
78 */
79 public static final int UID_TETHERING = -5;
80
81 /**
Jeff Sharkey4414cea2011-06-24 17:05:24 -070082 * Default tag value for {@link DownloadManager} traffic.
83 *
84 * @hide
85 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070086 public static final int TAG_SYSTEM_DOWNLOAD = 0xFFFFFF01;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070087
88 /**
89 * Default tag value for {@link MediaPlayer} traffic.
90 *
91 * @hide
92 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070093 public static final int TAG_SYSTEM_MEDIA = 0xFFFFFF02;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070094
95 /**
Christopher Tate0a61b362015-11-10 10:49:20 -080096 * Default tag value for {@link BackupManager} backup traffic; that is,
97 * traffic from the device to the storage backend.
Jeff Sharkey4414cea2011-06-24 17:05:24 -070098 *
99 * @hide
100 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700101 public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03;
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700102
Christopher Tate0a61b362015-11-10 10:49:20 -0800103 /**
104 * Default tag value for {@link BackupManager} restore traffic; that is,
105 * app data retrieved from the storage backend at install time.
106 *
107 * @hide
108 */
109 public static final int TAG_SYSTEM_RESTORE = 0xFFFFFF04;
110
Jeff Sharkey763a1402016-11-21 12:14:50 -0700111 /** @hide */
112 public static final int TAG_SYSTEM_DHCP = 0xFFFFFF05;
113 /** @hide */
114 public static final int TAG_SYSTEM_NTP = 0xFFFFFF06;
115 /** @hide */
116 public static final int TAG_SYSTEM_PROBE = 0xFFFFFF07;
117 /** @hide */
118 public static final int TAG_SYSTEM_NEIGHBOR = 0xFFFFFF08;
119 /** @hide */
120 public static final int TAG_SYSTEM_GPS = 0xFFFFFF09;
121 /** @hide */
122 public static final int TAG_SYSTEM_PAC = 0xFFFFFF0A;
123
124 /**
125 * Sockets that are strictly local on device; never hits network.
126 *
127 * @hide
128 */
129 public static final int TAG_SYSTEM_LOCAL = 0xFFFFFFAA;
130
Jeff Sharkey234766a2012-04-10 19:48:07 -0700131 private static INetworkStatsService sStatsService;
132
133 private synchronized static INetworkStatsService getStatsService() {
134 if (sStatsService == null) {
135 sStatsService = INetworkStatsService.Stub.asInterface(
136 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
137 }
138 return sStatsService;
139 }
140
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700141 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700142 * Snapshot of {@link NetworkStats} when the currently active profiling
143 * session started, or {@code null} if no session active.
144 *
145 * @see #startDataProfiling(Context)
146 * @see #stopDataProfiling(Context)
147 */
148 private static NetworkStats sActiveProfilingStart;
149
150 private static Object sProfilingLock = new Object();
151
152 /**
Jeff Sharkey43be1742011-04-21 18:45:43 -0700153 * Set active tag to use when accounting {@link Socket} traffic originating
154 * from the current thread. Only one active tag per thread is supported.
155 * <p>
156 * Changes only take effect during subsequent calls to
157 * {@link #tagSocket(Socket)}.
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700158 * <p>
159 * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
160 * used internally by system services like {@link DownloadManager} when
161 * performing traffic on behalf of an application.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700162 *
163 * @see #clearThreadStatsTag()
Jeff Sharkey43be1742011-04-21 18:45:43 -0700164 */
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700165 public static void setThreadStatsTag(int tag) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700166 NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700167 }
168
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700169 /**
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600170 * Set active tag to use when accounting {@link Socket} traffic originating
171 * from the current thread. The tag used internally is well-defined to
172 * distinguish all backup-related traffic.
173 *
Christopher Tatea2496de2014-08-06 14:19:56 -0700174 * @hide
175 */
176 @SystemApi
177 public static void setThreadStatsTagBackup() {
178 setThreadStatsTag(TAG_SYSTEM_BACKUP);
179 }
180
181 /**
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600182 * Set active tag to use when accounting {@link Socket} traffic originating
183 * from the current thread. The tag used internally is well-defined to
184 * distinguish all restore-related traffic.
185 *
Christopher Tate0a61b362015-11-10 10:49:20 -0800186 * @hide
187 */
188 @SystemApi
189 public static void setThreadStatsTagRestore() {
190 setThreadStatsTag(TAG_SYSTEM_RESTORE);
191 }
192
193 /**
Alon Alberteaef3512011-07-19 11:16:09 +0300194 * Get the active tag used when accounting {@link Socket} traffic originating
195 * from the current thread. Only one active tag per thread is supported.
196 * {@link #tagSocket(Socket)}.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700197 *
198 * @see #setThreadStatsTag(int)
Alon Alberteaef3512011-07-19 11:16:09 +0300199 */
200 public static int getThreadStatsTag() {
201 return NetworkManagementSocketTagger.getThreadSocketStatsTag();
202 }
203
Jeff Sharkeydddace72013-03-26 13:46:05 -0700204 /**
205 * Clear any active tag set to account {@link Socket} traffic originating
206 * from the current thread.
207 *
208 * @see #setThreadStatsTag(int)
209 */
Jeff Sharkey43be1742011-04-21 18:45:43 -0700210 public static void clearThreadStatsTag() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700211 NetworkManagementSocketTagger.setThreadSocketStatsTag(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700212 }
213
214 /**
215 * Set specific UID to use when accounting {@link Socket} traffic
216 * originating from the current thread. Designed for use when performing an
217 * operation on behalf of another application.
218 * <p>
219 * Changes only take effect during subsequent calls to
220 * {@link #tagSocket(Socket)}.
221 * <p>
222 * To take effect, caller must hold
223 * {@link android.Manifest.permission#UPDATE_DEVICE_STATS} permission.
224 *
Jeff Sharkeydddace72013-03-26 13:46:05 -0700225 * @hide
Jeff Sharkey43be1742011-04-21 18:45:43 -0700226 */
Christopher Tatea2496de2014-08-06 14:19:56 -0700227 @SystemApi
Jeff Sharkey43be1742011-04-21 18:45:43 -0700228 public static void setThreadStatsUid(int uid) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700229 NetworkManagementSocketTagger.setThreadSocketStatsUid(uid);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700230 }
231
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600232 /**
233 * Clear any active UID set to account {@link Socket} traffic originating
234 * from the current thread.
235 *
236 * @see #setThreadStatsUid(int)
237 * @hide
238 */
Christopher Tatea2496de2014-08-06 14:19:56 -0700239 @SystemApi
Jeff Sharkey43be1742011-04-21 18:45:43 -0700240 public static void clearThreadStatsUid() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700241 NetworkManagementSocketTagger.setThreadSocketStatsUid(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700242 }
243
244 /**
245 * Tag the given {@link Socket} with any statistics parameters active for
246 * the current thread. Subsequent calls always replace any existing
247 * parameters. When finished, call {@link #untagSocket(Socket)} to remove
248 * statistics parameters.
249 *
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700250 * @see #setThreadStatsTag(int)
Jeff Sharkey43be1742011-04-21 18:45:43 -0700251 */
252 public static void tagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700253 SocketTagger.get().tag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700254 }
255
256 /**
257 * Remove any statistics parameters from the given {@link Socket}.
258 */
259 public static void untagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700260 SocketTagger.get().untag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700261 }
262
263 /**
Jeff Sharkeyf0d76332015-12-04 15:21:52 -0700264 * Tag the given {@link DatagramSocket} with any statistics parameters
265 * active for the current thread. Subsequent calls always replace any
266 * existing parameters. When finished, call
267 * {@link #untagDatagramSocket(DatagramSocket)} to remove statistics
268 * parameters.
269 *
270 * @see #setThreadStatsTag(int)
Jeff Sharkeyf0d76332015-12-04 15:21:52 -0700271 */
272 public static void tagDatagramSocket(DatagramSocket socket) throws SocketException {
273 SocketTagger.get().tag(socket);
274 }
275
276 /**
277 * Remove any statistics parameters from the given {@link DatagramSocket}.
278 */
279 public static void untagDatagramSocket(DatagramSocket socket) throws SocketException {
280 SocketTagger.get().untag(socket);
281 }
282
283 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700284 * Start profiling data usage for current UID. Only one profiling session
285 * can be active at a time.
286 *
287 * @hide
288 */
289 public static void startDataProfiling(Context context) {
290 synchronized (sProfilingLock) {
291 if (sActiveProfilingStart != null) {
292 throw new IllegalStateException("already profiling data");
293 }
294
295 // take snapshot in time; we calculate delta later
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700296 sActiveProfilingStart = getDataLayerSnapshotForUid(context);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700297 }
298 }
299
300 /**
301 * Stop profiling data usage for current UID.
302 *
303 * @return Detailed {@link NetworkStats} of data that occurred since last
304 * {@link #startDataProfiling(Context)} call.
305 * @hide
306 */
307 public static NetworkStats stopDataProfiling(Context context) {
308 synchronized (sProfilingLock) {
309 if (sActiveProfilingStart == null) {
310 throw new IllegalStateException("not profiling data");
311 }
312
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800313 // subtract starting values and return delta
314 final NetworkStats profilingStop = getDataLayerSnapshotForUid(context);
315 final NetworkStats profilingDelta = NetworkStats.subtract(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800316 profilingStop, sActiveProfilingStart, null, null);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800317 sActiveProfilingStart = null;
318 return profilingDelta;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700319 }
320 }
321
322 /**
Jeff Sharkey558a2322011-08-24 15:42:09 -0700323 * Increment count of network operations performed under the accounting tag
324 * currently active on the calling thread. This can be used to derive
325 * bytes-per-operation.
326 *
327 * @param operationCount Number of operations to increment count by.
328 */
329 public static void incrementOperationCount(int operationCount) {
330 final int tag = getThreadStatsTag();
331 incrementOperationCount(tag, operationCount);
332 }
333
334 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700335 * Increment count of network operations performed under the given
336 * accounting tag. This can be used to derive bytes-per-operation.
337 *
338 * @param tag Accounting tag used in {@link #setThreadStatsTag(int)}.
339 * @param operationCount Number of operations to increment count by.
340 */
341 public static void incrementOperationCount(int tag, int operationCount) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700342 final int uid = android.os.Process.myUid();
343 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700344 getStatsService().incrementOperationCount(uid, tag, operationCount);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700345 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700346 throw e.rethrowFromSystemServer();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700347 }
348 }
349
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700350 /** {@hide} */
351 public static void closeQuietly(INetworkStatsSession session) {
352 // TODO: move to NetworkStatsService once it exists
353 if (session != null) {
354 try {
355 session.close();
356 } catch (RuntimeException rethrown) {
357 throw rethrown;
358 } catch (Exception ignored) {
359 }
360 }
361 }
362
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700363 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700364 * Return number of packets transmitted across mobile networks since device
365 * boot. Counts packets across all mobile network interfaces, and always
366 * increases monotonically since device boot. Statistics are measured at the
367 * network layer, so they include both TCP and UDP usage.
368 * <p>
369 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
370 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800371 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700372 public static long getMobileTxPackets() {
373 long total = 0;
374 for (String iface : getMobileIfaces()) {
375 total += getTxPackets(iface);
376 }
377 return total;
378 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800379
380 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700381 * Return number of packets received across mobile networks since device
382 * boot. Counts packets across all mobile network interfaces, and always
383 * increases monotonically since device boot. Statistics are measured at the
384 * network layer, so they include both TCP and UDP usage.
385 * <p>
386 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
387 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800388 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700389 public static long getMobileRxPackets() {
390 long total = 0;
391 for (String iface : getMobileIfaces()) {
392 total += getRxPackets(iface);
393 }
394 return total;
395 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800396
397 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700398 * Return number of bytes transmitted across mobile networks since device
399 * boot. Counts packets across all mobile network interfaces, and always
400 * increases monotonically since device boot. Statistics are measured at the
401 * network layer, so they include both TCP and UDP usage.
402 * <p>
403 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
404 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800405 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700406 public static long getMobileTxBytes() {
407 long total = 0;
408 for (String iface : getMobileIfaces()) {
409 total += getTxBytes(iface);
410 }
411 return total;
412 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800413
414 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700415 * Return number of bytes received across mobile networks since device boot.
416 * Counts packets across all mobile network interfaces, and always increases
417 * monotonically since device boot. Statistics are measured at the network
418 * layer, so they include both TCP and UDP usage.
419 * <p>
420 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
421 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800422 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700423 public static long getMobileRxBytes() {
424 long total = 0;
425 for (String iface : getMobileIfaces()) {
426 total += getRxBytes(iface);
427 }
428 return total;
429 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800430
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800431 /** {@hide} */
432 public static long getMobileTcpRxPackets() {
433 long total = 0;
434 for (String iface : getMobileIfaces()) {
435 final long stat = nativeGetIfaceStat(iface, TYPE_TCP_RX_PACKETS);
436 if (stat != UNSUPPORTED) {
437 total += stat;
438 }
439 }
440 return total;
441 }
442
443 /** {@hide} */
444 public static long getMobileTcpTxPackets() {
445 long total = 0;
446 for (String iface : getMobileIfaces()) {
447 final long stat = nativeGetIfaceStat(iface, TYPE_TCP_TX_PACKETS);
448 if (stat != UNSUPPORTED) {
449 total += stat;
450 }
451 }
452 return total;
453 }
454
Jeff Sharkeydddace72013-03-26 13:46:05 -0700455 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700456 public static long getTxPackets(String iface) {
457 return nativeGetIfaceStat(iface, TYPE_TX_PACKETS);
458 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800459
Jeff Sharkeydddace72013-03-26 13:46:05 -0700460 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700461 public static long getRxPackets(String iface) {
462 return nativeGetIfaceStat(iface, TYPE_RX_PACKETS);
463 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800464
Jeff Sharkeydddace72013-03-26 13:46:05 -0700465 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700466 public static long getTxBytes(String iface) {
467 return nativeGetIfaceStat(iface, TYPE_TX_BYTES);
468 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800469
Jeff Sharkeydddace72013-03-26 13:46:05 -0700470 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700471 public static long getRxBytes(String iface) {
472 return nativeGetIfaceStat(iface, TYPE_RX_BYTES);
473 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800474
475 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700476 * Return number of packets transmitted since device boot. Counts packets
477 * across all network interfaces, and always increases monotonically since
478 * device boot. Statistics are measured at the network layer, so they
479 * include both TCP and UDP usage.
480 * <p>
481 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
482 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800483 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700484 public static long getTotalTxPackets() {
485 return nativeGetTotalStat(TYPE_TX_PACKETS);
486 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800487
488 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700489 * Return number of packets received since device boot. Counts packets
490 * across all network interfaces, and always increases monotonically since
491 * device boot. Statistics are measured at the network layer, so they
492 * include both TCP and UDP usage.
493 * <p>
494 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
495 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800496 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700497 public static long getTotalRxPackets() {
498 return nativeGetTotalStat(TYPE_RX_PACKETS);
499 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800500
501 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700502 * Return number of bytes transmitted since device boot. Counts packets
503 * across all network interfaces, and always increases monotonically since
504 * device boot. Statistics are measured at the network layer, so they
505 * include both TCP and UDP usage.
506 * <p>
507 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
508 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800509 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700510 public static long getTotalTxBytes() {
511 return nativeGetTotalStat(TYPE_TX_BYTES);
512 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800513
514 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700515 * Return number of bytes received since device boot. Counts packets across
516 * all network interfaces, and always increases monotonically since device
517 * boot. Statistics are measured at the network layer, so they include both
518 * TCP and UDP usage.
519 * <p>
520 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
521 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800522 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700523 public static long getTotalRxBytes() {
524 return nativeGetTotalStat(TYPE_RX_BYTES);
525 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800526
527 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800528 * Return number of bytes transmitted by the given UID since device boot.
529 * Counts packets across all network interfaces, and always increases
530 * monotonically since device boot. Statistics are measured at the network
531 * layer, so they include both TCP and UDP usage.
532 * <p>
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700533 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
534 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
535 * <p>
536 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
537 * report traffic statistics for the calling UID. It will return
538 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
539 * historical network statistics belonging to other UIDs, use
540 * {@link NetworkStatsManager}.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800541 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800542 * @see android.os.Process#myUid()
543 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800544 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800545 public static long getUidTxBytes(int uid) {
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700546 // This isn't actually enforcing any security; it just returns the
547 // unsupported value. The real filtering is done at the kernel level.
548 final int callingUid = android.os.Process.myUid();
549 if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) {
550 return nativeGetUidStat(uid, TYPE_TX_BYTES);
551 } else {
552 return UNSUPPORTED;
553 }
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800554 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800555
556 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800557 * Return number of bytes received by the given UID since device boot.
558 * Counts packets across all network interfaces, and always increases
559 * monotonically since device boot. Statistics are measured at the network
560 * layer, so they include both TCP and UDP usage.
561 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800562 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800563 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700564 * <p>
565 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
566 * report traffic statistics for the calling UID. It will return
567 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
568 * historical network statistics belonging to other UIDs, use
569 * {@link NetworkStatsManager}.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800570 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800571 * @see android.os.Process#myUid()
572 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800573 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800574 public static long getUidRxBytes(int uid) {
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700575 // This isn't actually enforcing any security; it just returns the
576 // unsupported value. The real filtering is done at the kernel level.
577 final int callingUid = android.os.Process.myUid();
578 if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) {
579 return nativeGetUidStat(uid, TYPE_RX_BYTES);
580 } else {
581 return UNSUPPORTED;
582 }
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800583 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800584
585 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800586 * Return number of packets transmitted by the given UID since device boot.
587 * Counts packets across all network interfaces, and always increases
588 * monotonically since device boot. Statistics are measured at the network
589 * layer, so they include both TCP and UDP usage.
590 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800591 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800592 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700593 * <p>
594 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
595 * report traffic statistics for the calling UID. It will return
596 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
597 * historical network statistics belonging to other UIDs, use
598 * {@link NetworkStatsManager}.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800599 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800600 * @see android.os.Process#myUid()
601 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800602 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800603 public static long getUidTxPackets(int uid) {
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700604 // This isn't actually enforcing any security; it just returns the
605 // unsupported value. The real filtering is done at the kernel level.
606 final int callingUid = android.os.Process.myUid();
607 if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) {
608 return nativeGetUidStat(uid, TYPE_TX_PACKETS);
609 } else {
610 return UNSUPPORTED;
611 }
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800612 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800613
614 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800615 * Return number of packets received by the given UID since device boot.
616 * Counts packets across all network interfaces, and always increases
617 * monotonically since device boot. Statistics are measured at the network
618 * layer, so they include both TCP and UDP usage.
619 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800620 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800621 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700622 * <p>
623 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
624 * report traffic statistics for the calling UID. It will return
625 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
626 * historical network statistics belonging to other UIDs, use
627 * {@link NetworkStatsManager}.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800628 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800629 * @see android.os.Process#myUid()
630 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800631 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800632 public static long getUidRxPackets(int uid) {
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700633 // This isn't actually enforcing any security; it just returns the
634 // unsupported value. The real filtering is done at the kernel level.
635 final int callingUid = android.os.Process.myUid();
636 if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) {
637 return nativeGetUidStat(uid, TYPE_RX_PACKETS);
638 } else {
639 return UNSUPPORTED;
640 }
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800641 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800642
643 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800644 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800645 * transport layer statistics are no longer available, and will
646 * always return {@link #UNSUPPORTED}.
647 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800648 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800649 @Deprecated
650 public static long getUidTcpTxBytes(int uid) {
651 return UNSUPPORTED;
652 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800653
654 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800655 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800656 * transport layer statistics are no longer available, and will
657 * always return {@link #UNSUPPORTED}.
658 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800659 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800660 @Deprecated
661 public static long getUidTcpRxBytes(int uid) {
662 return UNSUPPORTED;
663 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800664
665 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800666 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800667 * transport layer statistics are no longer available, and will
668 * always return {@link #UNSUPPORTED}.
669 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800670 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800671 @Deprecated
672 public static long getUidUdpTxBytes(int uid) {
673 return UNSUPPORTED;
674 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800675
676 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800677 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800678 * transport layer statistics are no longer available, and will
679 * always return {@link #UNSUPPORTED}.
680 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800681 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800682 @Deprecated
683 public static long getUidUdpRxBytes(int uid) {
684 return UNSUPPORTED;
685 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800686
687 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800688 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800689 * transport layer statistics are no longer available, and will
690 * always return {@link #UNSUPPORTED}.
691 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800692 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800693 @Deprecated
694 public static long getUidTcpTxSegments(int uid) {
695 return UNSUPPORTED;
696 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800697
698 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800699 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800700 * transport layer statistics are no longer available, and will
701 * always return {@link #UNSUPPORTED}.
702 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800703 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800704 @Deprecated
705 public static long getUidTcpRxSegments(int uid) {
706 return UNSUPPORTED;
707 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800708
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800709 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800710 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800711 * transport layer statistics are no longer available, and will
712 * always return {@link #UNSUPPORTED}.
713 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800714 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800715 @Deprecated
716 public static long getUidUdpTxPackets(int uid) {
717 return UNSUPPORTED;
718 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800719
720 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800721 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800722 * transport layer statistics are no longer available, and will
723 * always return {@link #UNSUPPORTED}.
724 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800725 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800726 @Deprecated
727 public static long getUidUdpRxPackets(int uid) {
728 return UNSUPPORTED;
729 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700730
731 /**
732 * Return detailed {@link NetworkStats} for the current UID. Requires no
733 * special permission.
734 */
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700735 private static NetworkStats getDataLayerSnapshotForUid(Context context) {
Jeff Sharkeydddace72013-03-26 13:46:05 -0700736 // TODO: take snapshot locally, since proc file is now visible
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700737 final int uid = android.os.Process.myUid();
738 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700739 return getStatsService().getDataLayerSnapshotForUid(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700740 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700741 throw e.rethrowFromSystemServer();
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700742 }
743 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700744
745 /**
746 * Return set of any ifaces associated with mobile networks since boot.
747 * Interfaces are never removed from this list, so counters should always be
748 * monotonic.
749 */
750 private static String[] getMobileIfaces() {
751 try {
752 return getStatsService().getMobileIfaces();
753 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700754 throw e.rethrowFromSystemServer();
Jeff Sharkey234766a2012-04-10 19:48:07 -0700755 }
756 }
757
758 // NOTE: keep these in sync with android_net_TrafficStats.cpp
759 private static final int TYPE_RX_BYTES = 0;
760 private static final int TYPE_RX_PACKETS = 1;
761 private static final int TYPE_TX_BYTES = 2;
762 private static final int TYPE_TX_PACKETS = 3;
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800763 private static final int TYPE_TCP_RX_PACKETS = 4;
764 private static final int TYPE_TCP_TX_PACKETS = 5;
Jeff Sharkey234766a2012-04-10 19:48:07 -0700765
766 private static native long nativeGetTotalStat(int type);
767 private static native long nativeGetIfaceStat(String iface, int type);
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800768 private static native long nativeGetUidStat(int uid, int type);
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800769}