blob: ba8bd34bfc6d6f9435b9923e6b36f199a062e7c8 [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 Sharkeyeedcb952011-05-17 14:55:15 -070022import android.content.Context;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070023import android.media.MediaPlayer;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070024import android.os.RemoteException;
25import android.os.ServiceManager;
26
Jesse Wilson8568db52011-06-28 19:06:31 -070027import com.android.server.NetworkManagementSocketTagger;
Ken Shirrifff7d0b012009-12-07 15:56:05 -080028
Jesse Wilson8568db52011-06-28 19:06:31 -070029import dalvik.system.SocketTagger;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070030
Jeff Sharkeyf0d76332015-12-04 15:21:52 -070031import java.net.DatagramSocket;
Jeff Sharkey43be1742011-04-21 18:45:43 -070032import java.net.Socket;
33import java.net.SocketException;
Ken Shirrifff7d0b012009-12-07 15:56:05 -080034
35/**
Dan Egnor2b4abcd2010-04-07 17:30:50 -070036 * Class that provides network traffic statistics. These statistics include
37 * bytes transmitted and received and network packets transmitted and received,
38 * over all interfaces, over the mobile interface, and on a per-UID basis.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080039 * <p>
Dan Egnor2b4abcd2010-04-07 17:30:50 -070040 * These statistics may not be available on all platforms. If the statistics
41 * are not supported by this device, {@link #UNSUPPORTED} will be returned.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080042 */
43public class TrafficStats {
44 /**
45 * The return value to indicate that the device does not support the statistic.
46 */
47 public final static int UNSUPPORTED = -1;
48
Jeff Sharkey241dde22012-02-03 14:50:07 -080049 /** @hide */
50 public static final long KB_IN_BYTES = 1024;
51 /** @hide */
52 public static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
53 /** @hide */
54 public static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
Jeff Sharkey48877892015-03-18 11:27:19 -070055 /** @hide */
56 public static final long TB_IN_BYTES = GB_IN_BYTES * 1024;
Jeff Sharkeyb521fea2015-06-15 21:09:10 -070057 /** @hide */
58 public static final long PB_IN_BYTES = TB_IN_BYTES * 1024;
Jeff Sharkey241dde22012-02-03 14:50:07 -080059
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070060 /**
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070061 * Special UID value used when collecting {@link NetworkStatsHistory} for
62 * removed applications.
63 *
64 * @hide
65 */
66 public static final int UID_REMOVED = -4;
67
68 /**
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070069 * Special UID value used when collecting {@link NetworkStatsHistory} for
70 * tethering traffic.
71 *
72 * @hide
73 */
74 public static final int UID_TETHERING = -5;
75
76 /**
Jeff Sharkey4414cea2011-06-24 17:05:24 -070077 * Default tag value for {@link DownloadManager} traffic.
78 *
79 * @hide
80 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070081 public static final int TAG_SYSTEM_DOWNLOAD = 0xFFFFFF01;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070082
83 /**
84 * Default tag value for {@link MediaPlayer} traffic.
85 *
86 * @hide
87 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070088 public static final int TAG_SYSTEM_MEDIA = 0xFFFFFF02;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070089
90 /**
Christopher Tate0a61b362015-11-10 10:49:20 -080091 * Default tag value for {@link BackupManager} backup traffic; that is,
92 * traffic from the device to the storage backend.
Jeff Sharkey4414cea2011-06-24 17:05:24 -070093 *
94 * @hide
95 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070096 public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070097
Christopher Tate0a61b362015-11-10 10:49:20 -080098 /**
99 * Default tag value for {@link BackupManager} restore traffic; that is,
100 * app data retrieved from the storage backend at install time.
101 *
102 * @hide
103 */
104 public static final int TAG_SYSTEM_RESTORE = 0xFFFFFF04;
105
Jeff Sharkey234766a2012-04-10 19:48:07 -0700106 private static INetworkStatsService sStatsService;
107
108 private synchronized static INetworkStatsService getStatsService() {
109 if (sStatsService == null) {
110 sStatsService = INetworkStatsService.Stub.asInterface(
111 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
112 }
113 return sStatsService;
114 }
115
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700116 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700117 * Snapshot of {@link NetworkStats} when the currently active profiling
118 * session started, or {@code null} if no session active.
119 *
120 * @see #startDataProfiling(Context)
121 * @see #stopDataProfiling(Context)
122 */
123 private static NetworkStats sActiveProfilingStart;
124
125 private static Object sProfilingLock = new Object();
126
127 /**
Jeff Sharkey43be1742011-04-21 18:45:43 -0700128 * Set active tag to use when accounting {@link Socket} traffic originating
129 * from the current thread. Only one active tag per thread is supported.
130 * <p>
131 * Changes only take effect during subsequent calls to
132 * {@link #tagSocket(Socket)}.
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700133 * <p>
134 * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
135 * used internally by system services like {@link DownloadManager} when
136 * performing traffic on behalf of an application.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700137 *
138 * @see #clearThreadStatsTag()
Jeff Sharkey43be1742011-04-21 18:45:43 -0700139 */
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700140 public static void setThreadStatsTag(int tag) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700141 NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700142 }
143
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700144 /**
Christopher Tatea2496de2014-08-06 14:19:56 -0700145 * System API for backup-related support components to tag network traffic
146 * appropriately.
147 * @hide
148 */
149 @SystemApi
150 public static void setThreadStatsTagBackup() {
151 setThreadStatsTag(TAG_SYSTEM_BACKUP);
152 }
153
154 /**
Christopher Tate0a61b362015-11-10 10:49:20 -0800155 * System API for restore-related support components to tag network traffic
156 * appropriately.
157 * @hide
158 */
159 @SystemApi
160 public static void setThreadStatsTagRestore() {
161 setThreadStatsTag(TAG_SYSTEM_RESTORE);
162 }
163
164 /**
Alon Alberteaef3512011-07-19 11:16:09 +0300165 * Get the active tag used when accounting {@link Socket} traffic originating
166 * from the current thread. Only one active tag per thread is supported.
167 * {@link #tagSocket(Socket)}.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700168 *
169 * @see #setThreadStatsTag(int)
Alon Alberteaef3512011-07-19 11:16:09 +0300170 */
171 public static int getThreadStatsTag() {
172 return NetworkManagementSocketTagger.getThreadSocketStatsTag();
173 }
174
Jeff Sharkeydddace72013-03-26 13:46:05 -0700175 /**
176 * Clear any active tag set to account {@link Socket} traffic originating
177 * from the current thread.
178 *
179 * @see #setThreadStatsTag(int)
180 */
Jeff Sharkey43be1742011-04-21 18:45:43 -0700181 public static void clearThreadStatsTag() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700182 NetworkManagementSocketTagger.setThreadSocketStatsTag(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700183 }
184
185 /**
186 * Set specific UID to use when accounting {@link Socket} traffic
187 * originating from the current thread. Designed for use when performing an
188 * operation on behalf of another application.
189 * <p>
190 * Changes only take effect during subsequent calls to
191 * {@link #tagSocket(Socket)}.
192 * <p>
193 * To take effect, caller must hold
194 * {@link android.Manifest.permission#UPDATE_DEVICE_STATS} permission.
195 *
Jeff Sharkeydddace72013-03-26 13:46:05 -0700196 * @hide
Jeff Sharkey43be1742011-04-21 18:45:43 -0700197 */
Christopher Tatea2496de2014-08-06 14:19:56 -0700198 @SystemApi
Jeff Sharkey43be1742011-04-21 18:45:43 -0700199 public static void setThreadStatsUid(int uid) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700200 NetworkManagementSocketTagger.setThreadSocketStatsUid(uid);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700201 }
202
203 /** {@hide} */
Christopher Tatea2496de2014-08-06 14:19:56 -0700204 @SystemApi
Jeff Sharkey43be1742011-04-21 18:45:43 -0700205 public static void clearThreadStatsUid() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700206 NetworkManagementSocketTagger.setThreadSocketStatsUid(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700207 }
208
209 /**
210 * Tag the given {@link Socket} with any statistics parameters active for
211 * the current thread. Subsequent calls always replace any existing
212 * parameters. When finished, call {@link #untagSocket(Socket)} to remove
213 * statistics parameters.
214 *
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700215 * @see #setThreadStatsTag(int)
Jeff Sharkey43be1742011-04-21 18:45:43 -0700216 * @see #setThreadStatsUid(int)
217 */
218 public static void tagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700219 SocketTagger.get().tag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700220 }
221
222 /**
223 * Remove any statistics parameters from the given {@link Socket}.
224 */
225 public static void untagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700226 SocketTagger.get().untag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700227 }
228
229 /**
Jeff Sharkeyf0d76332015-12-04 15:21:52 -0700230 * Tag the given {@link DatagramSocket} with any statistics parameters
231 * active for the current thread. Subsequent calls always replace any
232 * existing parameters. When finished, call
233 * {@link #untagDatagramSocket(DatagramSocket)} to remove statistics
234 * parameters.
235 *
236 * @see #setThreadStatsTag(int)
237 * @see #setThreadStatsUid(int)
238 */
239 public static void tagDatagramSocket(DatagramSocket socket) throws SocketException {
240 SocketTagger.get().tag(socket);
241 }
242
243 /**
244 * Remove any statistics parameters from the given {@link DatagramSocket}.
245 */
246 public static void untagDatagramSocket(DatagramSocket socket) throws SocketException {
247 SocketTagger.get().untag(socket);
248 }
249
250 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700251 * Start profiling data usage for current UID. Only one profiling session
252 * can be active at a time.
253 *
254 * @hide
255 */
256 public static void startDataProfiling(Context context) {
257 synchronized (sProfilingLock) {
258 if (sActiveProfilingStart != null) {
259 throw new IllegalStateException("already profiling data");
260 }
261
262 // take snapshot in time; we calculate delta later
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700263 sActiveProfilingStart = getDataLayerSnapshotForUid(context);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700264 }
265 }
266
267 /**
268 * Stop profiling data usage for current UID.
269 *
270 * @return Detailed {@link NetworkStats} of data that occurred since last
271 * {@link #startDataProfiling(Context)} call.
272 * @hide
273 */
274 public static NetworkStats stopDataProfiling(Context context) {
275 synchronized (sProfilingLock) {
276 if (sActiveProfilingStart == null) {
277 throw new IllegalStateException("not profiling data");
278 }
279
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800280 // subtract starting values and return delta
281 final NetworkStats profilingStop = getDataLayerSnapshotForUid(context);
282 final NetworkStats profilingDelta = NetworkStats.subtract(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800283 profilingStop, sActiveProfilingStart, null, null);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800284 sActiveProfilingStart = null;
285 return profilingDelta;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700286 }
287 }
288
289 /**
Jeff Sharkey558a2322011-08-24 15:42:09 -0700290 * Increment count of network operations performed under the accounting tag
291 * currently active on the calling thread. This can be used to derive
292 * bytes-per-operation.
293 *
294 * @param operationCount Number of operations to increment count by.
295 */
296 public static void incrementOperationCount(int operationCount) {
297 final int tag = getThreadStatsTag();
298 incrementOperationCount(tag, operationCount);
299 }
300
301 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700302 * Increment count of network operations performed under the given
303 * accounting tag. This can be used to derive bytes-per-operation.
304 *
305 * @param tag Accounting tag used in {@link #setThreadStatsTag(int)}.
306 * @param operationCount Number of operations to increment count by.
307 */
308 public static void incrementOperationCount(int tag, int operationCount) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700309 final int uid = android.os.Process.myUid();
310 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700311 getStatsService().incrementOperationCount(uid, tag, operationCount);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700312 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700313 throw e.rethrowFromSystemServer();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700314 }
315 }
316
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700317 /** {@hide} */
318 public static void closeQuietly(INetworkStatsSession session) {
319 // TODO: move to NetworkStatsService once it exists
320 if (session != null) {
321 try {
322 session.close();
323 } catch (RuntimeException rethrown) {
324 throw rethrown;
325 } catch (Exception ignored) {
326 }
327 }
328 }
329
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700330 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700331 * Return number of packets transmitted across mobile networks since device
332 * boot. Counts packets across all mobile network interfaces, and always
333 * increases monotonically since device boot. Statistics are measured at the
334 * network layer, so they include both TCP and UDP usage.
335 * <p>
336 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
337 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800338 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700339 public static long getMobileTxPackets() {
340 long total = 0;
341 for (String iface : getMobileIfaces()) {
342 total += getTxPackets(iface);
343 }
344 return total;
345 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800346
347 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700348 * Return number of packets received across mobile networks since device
349 * boot. Counts packets across all mobile network interfaces, and always
350 * increases monotonically since device boot. Statistics are measured at the
351 * network layer, so they include both TCP and UDP usage.
352 * <p>
353 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
354 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800355 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700356 public static long getMobileRxPackets() {
357 long total = 0;
358 for (String iface : getMobileIfaces()) {
359 total += getRxPackets(iface);
360 }
361 return total;
362 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800363
364 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700365 * Return number of bytes transmitted across mobile networks since device
366 * boot. Counts packets across all mobile network interfaces, and always
367 * increases monotonically since device boot. Statistics are measured at the
368 * network layer, so they include both TCP and UDP usage.
369 * <p>
370 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
371 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800372 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700373 public static long getMobileTxBytes() {
374 long total = 0;
375 for (String iface : getMobileIfaces()) {
376 total += getTxBytes(iface);
377 }
378 return total;
379 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800380
381 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700382 * Return number of bytes received across mobile networks since device boot.
383 * Counts packets across all mobile network interfaces, and always increases
384 * monotonically since device boot. Statistics are measured at the network
385 * layer, so they include both TCP and UDP usage.
386 * <p>
387 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
388 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800389 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700390 public static long getMobileRxBytes() {
391 long total = 0;
392 for (String iface : getMobileIfaces()) {
393 total += getRxBytes(iface);
394 }
395 return total;
396 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800397
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800398 /** {@hide} */
399 public static long getMobileTcpRxPackets() {
400 long total = 0;
401 for (String iface : getMobileIfaces()) {
402 final long stat = nativeGetIfaceStat(iface, TYPE_TCP_RX_PACKETS);
403 if (stat != UNSUPPORTED) {
404 total += stat;
405 }
406 }
407 return total;
408 }
409
410 /** {@hide} */
411 public static long getMobileTcpTxPackets() {
412 long total = 0;
413 for (String iface : getMobileIfaces()) {
414 final long stat = nativeGetIfaceStat(iface, TYPE_TCP_TX_PACKETS);
415 if (stat != UNSUPPORTED) {
416 total += stat;
417 }
418 }
419 return total;
420 }
421
Jeff Sharkeydddace72013-03-26 13:46:05 -0700422 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700423 public static long getTxPackets(String iface) {
424 return nativeGetIfaceStat(iface, TYPE_TX_PACKETS);
425 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800426
Jeff Sharkeydddace72013-03-26 13:46:05 -0700427 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700428 public static long getRxPackets(String iface) {
429 return nativeGetIfaceStat(iface, TYPE_RX_PACKETS);
430 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800431
Jeff Sharkeydddace72013-03-26 13:46:05 -0700432 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700433 public static long getTxBytes(String iface) {
434 return nativeGetIfaceStat(iface, TYPE_TX_BYTES);
435 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800436
Jeff Sharkeydddace72013-03-26 13:46:05 -0700437 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700438 public static long getRxBytes(String iface) {
439 return nativeGetIfaceStat(iface, TYPE_RX_BYTES);
440 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800441
442 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700443 * Return number of packets transmitted since device boot. Counts packets
444 * across all network interfaces, and always increases monotonically since
445 * device boot. Statistics are measured at the network layer, so they
446 * include both TCP and UDP usage.
447 * <p>
448 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
449 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800450 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700451 public static long getTotalTxPackets() {
452 return nativeGetTotalStat(TYPE_TX_PACKETS);
453 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800454
455 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700456 * Return number of packets received since device boot. Counts packets
457 * across all network interfaces, and always increases monotonically since
458 * device boot. Statistics are measured at the network layer, so they
459 * include both TCP and UDP usage.
460 * <p>
461 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
462 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800463 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700464 public static long getTotalRxPackets() {
465 return nativeGetTotalStat(TYPE_RX_PACKETS);
466 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800467
468 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700469 * Return number of bytes transmitted since device boot. Counts packets
470 * across all network interfaces, and always increases monotonically since
471 * device boot. Statistics are measured at the network layer, so they
472 * include both TCP and UDP usage.
473 * <p>
474 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
475 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800476 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700477 public static long getTotalTxBytes() {
478 return nativeGetTotalStat(TYPE_TX_BYTES);
479 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800480
481 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700482 * Return number of bytes received since device boot. Counts packets across
483 * all network interfaces, and always increases monotonically since device
484 * boot. Statistics are measured at the network layer, so they include both
485 * TCP and UDP usage.
486 * <p>
487 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
488 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800489 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700490 public static long getTotalRxBytes() {
491 return nativeGetTotalStat(TYPE_RX_BYTES);
492 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800493
494 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800495 * Return number of bytes transmitted by the given UID since device boot.
496 * Counts packets across all network interfaces, and always increases
497 * monotonically since device boot. Statistics are measured at the network
498 * layer, so they include both TCP and UDP usage.
499 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800500 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800501 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800502 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800503 * @see android.os.Process#myUid()
504 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800505 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800506 public static long getUidTxBytes(int uid) {
507 return nativeGetUidStat(uid, TYPE_TX_BYTES);
508 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800509
510 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800511 * Return number of bytes received by the given UID since device boot.
512 * Counts packets across all network interfaces, and always increases
513 * monotonically since device boot. Statistics are measured at the network
514 * layer, so they include both TCP and UDP usage.
515 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800516 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800517 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800518 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800519 * @see android.os.Process#myUid()
520 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800521 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800522 public static long getUidRxBytes(int uid) {
523 return nativeGetUidStat(uid, TYPE_RX_BYTES);
524 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800525
526 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800527 * Return number of packets transmitted by the given UID since device boot.
528 * Counts packets across all network interfaces, and always increases
529 * monotonically since device boot. Statistics are measured at the network
530 * layer, so they include both TCP and UDP usage.
531 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800532 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800533 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800534 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800535 * @see android.os.Process#myUid()
536 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800537 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800538 public static long getUidTxPackets(int uid) {
539 return nativeGetUidStat(uid, TYPE_TX_PACKETS);
540 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800541
542 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800543 * Return number of packets received by the given UID since device boot.
544 * Counts packets across all network interfaces, and always increases
545 * monotonically since device boot. Statistics are measured at the network
546 * layer, so they include both TCP and UDP usage.
547 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800548 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800549 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800550 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800551 * @see android.os.Process#myUid()
552 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800553 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800554 public static long getUidRxPackets(int uid) {
555 return nativeGetUidStat(uid, TYPE_RX_PACKETS);
556 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800557
558 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800559 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800560 * transport layer statistics are no longer available, and will
561 * always return {@link #UNSUPPORTED}.
562 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800563 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800564 @Deprecated
565 public static long getUidTcpTxBytes(int uid) {
566 return UNSUPPORTED;
567 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800568
569 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800570 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800571 * transport layer statistics are no longer available, and will
572 * always return {@link #UNSUPPORTED}.
573 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800574 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800575 @Deprecated
576 public static long getUidTcpRxBytes(int uid) {
577 return UNSUPPORTED;
578 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800579
580 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800581 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800582 * transport layer statistics are no longer available, and will
583 * always return {@link #UNSUPPORTED}.
584 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800585 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800586 @Deprecated
587 public static long getUidUdpTxBytes(int uid) {
588 return UNSUPPORTED;
589 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800590
591 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800592 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800593 * transport layer statistics are no longer available, and will
594 * always return {@link #UNSUPPORTED}.
595 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800596 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800597 @Deprecated
598 public static long getUidUdpRxBytes(int uid) {
599 return UNSUPPORTED;
600 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800601
602 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800603 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800604 * transport layer statistics are no longer available, and will
605 * always return {@link #UNSUPPORTED}.
606 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800607 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800608 @Deprecated
609 public static long getUidTcpTxSegments(int uid) {
610 return UNSUPPORTED;
611 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800612
613 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800614 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800615 * transport layer statistics are no longer available, and will
616 * always return {@link #UNSUPPORTED}.
617 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800618 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800619 @Deprecated
620 public static long getUidTcpRxSegments(int uid) {
621 return UNSUPPORTED;
622 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800623
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800624 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800625 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800626 * transport layer statistics are no longer available, and will
627 * always return {@link #UNSUPPORTED}.
628 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800629 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800630 @Deprecated
631 public static long getUidUdpTxPackets(int uid) {
632 return UNSUPPORTED;
633 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800634
635 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800636 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800637 * transport layer statistics are no longer available, and will
638 * always return {@link #UNSUPPORTED}.
639 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800640 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800641 @Deprecated
642 public static long getUidUdpRxPackets(int uid) {
643 return UNSUPPORTED;
644 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700645
646 /**
647 * Return detailed {@link NetworkStats} for the current UID. Requires no
648 * special permission.
649 */
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700650 private static NetworkStats getDataLayerSnapshotForUid(Context context) {
Jeff Sharkeydddace72013-03-26 13:46:05 -0700651 // TODO: take snapshot locally, since proc file is now visible
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700652 final int uid = android.os.Process.myUid();
653 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700654 return getStatsService().getDataLayerSnapshotForUid(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700655 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700656 throw e.rethrowFromSystemServer();
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700657 }
658 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700659
660 /**
661 * Return set of any ifaces associated with mobile networks since boot.
662 * Interfaces are never removed from this list, so counters should always be
663 * monotonic.
664 */
665 private static String[] getMobileIfaces() {
666 try {
667 return getStatsService().getMobileIfaces();
668 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700669 throw e.rethrowFromSystemServer();
Jeff Sharkey234766a2012-04-10 19:48:07 -0700670 }
671 }
672
673 // NOTE: keep these in sync with android_net_TrafficStats.cpp
674 private static final int TYPE_RX_BYTES = 0;
675 private static final int TYPE_RX_PACKETS = 1;
676 private static final int TYPE_TX_BYTES = 2;
677 private static final int TYPE_TX_PACKETS = 3;
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800678 private static final int TYPE_TCP_RX_PACKETS = 4;
679 private static final int TYPE_TCP_TX_PACKETS = 5;
Jeff Sharkey234766a2012-04-10 19:48:07 -0700680
681 private static native long nativeGetTotalStat(int type);
682 private static native long nativeGetIfaceStat(String iface, int type);
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800683 private static native long nativeGetUidStat(int uid, int type);
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800684}