blob: 3f1851901fad5f6e3509ae2e9c392606f16c1709 [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 Sharkey43be1742011-04-21 18:45:43 -070031import java.net.Socket;
32import java.net.SocketException;
Ken Shirrifff7d0b012009-12-07 15:56:05 -080033
34/**
Dan Egnor2b4abcd2010-04-07 17:30:50 -070035 * Class that provides network traffic statistics. These statistics include
36 * bytes transmitted and received and network packets transmitted and received,
37 * over all interfaces, over the mobile interface, and on a per-UID basis.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080038 * <p>
Dan Egnor2b4abcd2010-04-07 17:30:50 -070039 * These statistics may not be available on all platforms. If the statistics
40 * are not supported by this device, {@link #UNSUPPORTED} will be returned.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080041 */
42public class TrafficStats {
43 /**
44 * The return value to indicate that the device does not support the statistic.
45 */
46 public final static int UNSUPPORTED = -1;
47
Jeff Sharkey241dde22012-02-03 14:50:07 -080048 /** @hide */
49 public static final long KB_IN_BYTES = 1024;
50 /** @hide */
51 public static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
52 /** @hide */
53 public static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
54
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070055 /**
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070056 * Special UID value used when collecting {@link NetworkStatsHistory} for
57 * removed applications.
58 *
59 * @hide
60 */
61 public static final int UID_REMOVED = -4;
62
63 /**
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070064 * Special UID value used when collecting {@link NetworkStatsHistory} for
65 * tethering traffic.
66 *
67 * @hide
68 */
69 public static final int UID_TETHERING = -5;
70
71 /**
Jeff Sharkey4414cea2011-06-24 17:05:24 -070072 * Default tag value for {@link DownloadManager} traffic.
73 *
74 * @hide
75 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070076 public static final int TAG_SYSTEM_DOWNLOAD = 0xFFFFFF01;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070077
78 /**
79 * Default tag value for {@link MediaPlayer} traffic.
80 *
81 * @hide
82 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070083 public static final int TAG_SYSTEM_MEDIA = 0xFFFFFF02;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070084
85 /**
86 * Default tag value for {@link BackupManager} traffic.
87 *
88 * @hide
89 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070090 public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070091
Jeff Sharkey234766a2012-04-10 19:48:07 -070092 private static INetworkStatsService sStatsService;
93
94 private synchronized static INetworkStatsService getStatsService() {
95 if (sStatsService == null) {
96 sStatsService = INetworkStatsService.Stub.asInterface(
97 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
98 }
99 return sStatsService;
100 }
101
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700102 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700103 * Snapshot of {@link NetworkStats} when the currently active profiling
104 * session started, or {@code null} if no session active.
105 *
106 * @see #startDataProfiling(Context)
107 * @see #stopDataProfiling(Context)
108 */
109 private static NetworkStats sActiveProfilingStart;
110
111 private static Object sProfilingLock = new Object();
112
113 /**
Jeff Sharkey43be1742011-04-21 18:45:43 -0700114 * Set active tag to use when accounting {@link Socket} traffic originating
115 * from the current thread. Only one active tag per thread is supported.
116 * <p>
117 * Changes only take effect during subsequent calls to
118 * {@link #tagSocket(Socket)}.
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700119 * <p>
120 * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
121 * used internally by system services like {@link DownloadManager} when
122 * performing traffic on behalf of an application.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700123 *
124 * @see #clearThreadStatsTag()
Jeff Sharkey43be1742011-04-21 18:45:43 -0700125 */
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700126 public static void setThreadStatsTag(int tag) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700127 NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700128 }
129
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700130 /**
Christopher Tatea2496de2014-08-06 14:19:56 -0700131 * System API for backup-related support components to tag network traffic
132 * appropriately.
133 * @hide
134 */
135 @SystemApi
136 public static void setThreadStatsTagBackup() {
137 setThreadStatsTag(TAG_SYSTEM_BACKUP);
138 }
139
140 /**
Alon Alberteaef3512011-07-19 11:16:09 +0300141 * Get the active tag used when accounting {@link Socket} traffic originating
142 * from the current thread. Only one active tag per thread is supported.
143 * {@link #tagSocket(Socket)}.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700144 *
145 * @see #setThreadStatsTag(int)
Alon Alberteaef3512011-07-19 11:16:09 +0300146 */
147 public static int getThreadStatsTag() {
148 return NetworkManagementSocketTagger.getThreadSocketStatsTag();
149 }
150
Jeff Sharkeydddace72013-03-26 13:46:05 -0700151 /**
152 * Clear any active tag set to account {@link Socket} traffic originating
153 * from the current thread.
154 *
155 * @see #setThreadStatsTag(int)
156 */
Jeff Sharkey43be1742011-04-21 18:45:43 -0700157 public static void clearThreadStatsTag() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700158 NetworkManagementSocketTagger.setThreadSocketStatsTag(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700159 }
160
161 /**
162 * Set specific UID to use when accounting {@link Socket} traffic
163 * originating from the current thread. Designed for use when performing an
164 * operation on behalf of another application.
165 * <p>
166 * Changes only take effect during subsequent calls to
167 * {@link #tagSocket(Socket)}.
168 * <p>
169 * To take effect, caller must hold
170 * {@link android.Manifest.permission#UPDATE_DEVICE_STATS} permission.
171 *
Jeff Sharkeydddace72013-03-26 13:46:05 -0700172 * @hide
Jeff Sharkey43be1742011-04-21 18:45:43 -0700173 */
Christopher Tatea2496de2014-08-06 14:19:56 -0700174 @SystemApi
Jeff Sharkey43be1742011-04-21 18:45:43 -0700175 public static void setThreadStatsUid(int uid) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700176 NetworkManagementSocketTagger.setThreadSocketStatsUid(uid);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700177 }
178
179 /** {@hide} */
Christopher Tatea2496de2014-08-06 14:19:56 -0700180 @SystemApi
Jeff Sharkey43be1742011-04-21 18:45:43 -0700181 public static void clearThreadStatsUid() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700182 NetworkManagementSocketTagger.setThreadSocketStatsUid(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700183 }
184
185 /**
186 * Tag the given {@link Socket} with any statistics parameters active for
187 * the current thread. Subsequent calls always replace any existing
188 * parameters. When finished, call {@link #untagSocket(Socket)} to remove
189 * statistics parameters.
190 *
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700191 * @see #setThreadStatsTag(int)
Jeff Sharkey43be1742011-04-21 18:45:43 -0700192 * @see #setThreadStatsUid(int)
193 */
194 public static void tagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700195 SocketTagger.get().tag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700196 }
197
198 /**
199 * Remove any statistics parameters from the given {@link Socket}.
200 */
201 public static void untagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700202 SocketTagger.get().untag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700203 }
204
205 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700206 * Start profiling data usage for current UID. Only one profiling session
207 * can be active at a time.
208 *
209 * @hide
210 */
211 public static void startDataProfiling(Context context) {
212 synchronized (sProfilingLock) {
213 if (sActiveProfilingStart != null) {
214 throw new IllegalStateException("already profiling data");
215 }
216
217 // take snapshot in time; we calculate delta later
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700218 sActiveProfilingStart = getDataLayerSnapshotForUid(context);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700219 }
220 }
221
222 /**
223 * Stop profiling data usage for current UID.
224 *
225 * @return Detailed {@link NetworkStats} of data that occurred since last
226 * {@link #startDataProfiling(Context)} call.
227 * @hide
228 */
229 public static NetworkStats stopDataProfiling(Context context) {
230 synchronized (sProfilingLock) {
231 if (sActiveProfilingStart == null) {
232 throw new IllegalStateException("not profiling data");
233 }
234
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800235 // subtract starting values and return delta
236 final NetworkStats profilingStop = getDataLayerSnapshotForUid(context);
237 final NetworkStats profilingDelta = NetworkStats.subtract(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800238 profilingStop, sActiveProfilingStart, null, null);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800239 sActiveProfilingStart = null;
240 return profilingDelta;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700241 }
242 }
243
244 /**
Jeff Sharkey558a2322011-08-24 15:42:09 -0700245 * Increment count of network operations performed under the accounting tag
246 * currently active on the calling thread. This can be used to derive
247 * bytes-per-operation.
248 *
249 * @param operationCount Number of operations to increment count by.
250 */
251 public static void incrementOperationCount(int operationCount) {
252 final int tag = getThreadStatsTag();
253 incrementOperationCount(tag, operationCount);
254 }
255
256 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700257 * Increment count of network operations performed under the given
258 * accounting tag. This can be used to derive bytes-per-operation.
259 *
260 * @param tag Accounting tag used in {@link #setThreadStatsTag(int)}.
261 * @param operationCount Number of operations to increment count by.
262 */
263 public static void incrementOperationCount(int tag, int operationCount) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700264 final int uid = android.os.Process.myUid();
265 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700266 getStatsService().incrementOperationCount(uid, tag, operationCount);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700267 } catch (RemoteException e) {
268 throw new RuntimeException(e);
269 }
270 }
271
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700272 /** {@hide} */
273 public static void closeQuietly(INetworkStatsSession session) {
274 // TODO: move to NetworkStatsService once it exists
275 if (session != null) {
276 try {
277 session.close();
278 } catch (RuntimeException rethrown) {
279 throw rethrown;
280 } catch (Exception ignored) {
281 }
282 }
283 }
284
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700285 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700286 * Return number of packets transmitted across mobile networks since device
287 * boot. Counts packets across all mobile network interfaces, and always
288 * increases monotonically since device boot. Statistics are measured at the
289 * network layer, so they include both TCP and UDP usage.
290 * <p>
291 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
292 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800293 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700294 public static long getMobileTxPackets() {
295 long total = 0;
296 for (String iface : getMobileIfaces()) {
297 total += getTxPackets(iface);
298 }
299 return total;
300 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800301
302 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700303 * Return number of packets received across mobile networks since device
304 * boot. Counts packets across all mobile network interfaces, and always
305 * increases monotonically since device boot. Statistics are measured at the
306 * network layer, so they include both TCP and UDP usage.
307 * <p>
308 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
309 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800310 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700311 public static long getMobileRxPackets() {
312 long total = 0;
313 for (String iface : getMobileIfaces()) {
314 total += getRxPackets(iface);
315 }
316 return total;
317 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800318
319 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700320 * Return number of bytes transmitted across mobile networks since device
321 * boot. Counts packets across all mobile network interfaces, and always
322 * increases monotonically since device boot. Statistics are measured at the
323 * network layer, so they include both TCP and UDP usage.
324 * <p>
325 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
326 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800327 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700328 public static long getMobileTxBytes() {
329 long total = 0;
330 for (String iface : getMobileIfaces()) {
331 total += getTxBytes(iface);
332 }
333 return total;
334 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800335
336 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700337 * Return number of bytes received across mobile networks since device boot.
338 * Counts packets across all mobile network interfaces, and always increases
339 * monotonically since device boot. Statistics are measured at the network
340 * layer, so they include both TCP and UDP usage.
341 * <p>
342 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
343 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800344 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700345 public static long getMobileRxBytes() {
346 long total = 0;
347 for (String iface : getMobileIfaces()) {
348 total += getRxBytes(iface);
349 }
350 return total;
351 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800352
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800353 /** {@hide} */
354 public static long getMobileTcpRxPackets() {
355 long total = 0;
356 for (String iface : getMobileIfaces()) {
357 final long stat = nativeGetIfaceStat(iface, TYPE_TCP_RX_PACKETS);
358 if (stat != UNSUPPORTED) {
359 total += stat;
360 }
361 }
362 return total;
363 }
364
365 /** {@hide} */
366 public static long getMobileTcpTxPackets() {
367 long total = 0;
368 for (String iface : getMobileIfaces()) {
369 final long stat = nativeGetIfaceStat(iface, TYPE_TCP_TX_PACKETS);
370 if (stat != UNSUPPORTED) {
371 total += stat;
372 }
373 }
374 return total;
375 }
376
Jeff Sharkeydddace72013-03-26 13:46:05 -0700377 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700378 public static long getTxPackets(String iface) {
379 return nativeGetIfaceStat(iface, TYPE_TX_PACKETS);
380 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800381
Jeff Sharkeydddace72013-03-26 13:46:05 -0700382 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700383 public static long getRxPackets(String iface) {
384 return nativeGetIfaceStat(iface, TYPE_RX_PACKETS);
385 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800386
Jeff Sharkeydddace72013-03-26 13:46:05 -0700387 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700388 public static long getTxBytes(String iface) {
389 return nativeGetIfaceStat(iface, TYPE_TX_BYTES);
390 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800391
Jeff Sharkeydddace72013-03-26 13:46:05 -0700392 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700393 public static long getRxBytes(String iface) {
394 return nativeGetIfaceStat(iface, TYPE_RX_BYTES);
395 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800396
397 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700398 * Return number of packets transmitted since device boot. Counts packets
399 * across all network interfaces, and always increases monotonically since
400 * device boot. Statistics are measured at the network layer, so they
401 * 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 getTotalTxPackets() {
407 return nativeGetTotalStat(TYPE_TX_PACKETS);
408 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800409
410 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700411 * Return number of packets received since device boot. Counts packets
412 * across all network interfaces, and always increases monotonically since
413 * device boot. Statistics are measured at the network layer, so they
414 * include both TCP and UDP usage.
415 * <p>
416 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
417 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800418 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700419 public static long getTotalRxPackets() {
420 return nativeGetTotalStat(TYPE_RX_PACKETS);
421 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800422
423 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700424 * Return number of bytes transmitted since device boot. Counts packets
425 * across all network interfaces, and always increases monotonically since
426 * device boot. Statistics are measured at the network layer, so they
427 * include both TCP and UDP usage.
428 * <p>
429 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
430 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800431 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700432 public static long getTotalTxBytes() {
433 return nativeGetTotalStat(TYPE_TX_BYTES);
434 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800435
436 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700437 * Return number of bytes received since device boot. Counts packets across
438 * all network interfaces, and always increases monotonically since device
439 * boot. Statistics are measured at the network layer, so they include both
440 * TCP and UDP usage.
441 * <p>
442 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
443 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800444 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700445 public static long getTotalRxBytes() {
446 return nativeGetTotalStat(TYPE_RX_BYTES);
447 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800448
449 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800450 * Return number of bytes transmitted by the given UID since device boot.
451 * Counts packets across all network interfaces, and always increases
452 * monotonically since device boot. Statistics are measured at the network
453 * layer, so they include both TCP and UDP usage.
454 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800455 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800456 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800457 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800458 * @see android.os.Process#myUid()
459 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800460 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800461 public static long getUidTxBytes(int uid) {
462 return nativeGetUidStat(uid, TYPE_TX_BYTES);
463 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800464
465 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800466 * Return number of bytes received by the given UID since device boot.
467 * Counts packets across all network interfaces, and always increases
468 * monotonically since device boot. Statistics are measured at the network
469 * layer, so they include both TCP and UDP usage.
470 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800471 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800472 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800473 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800474 * @see android.os.Process#myUid()
475 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800476 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800477 public static long getUidRxBytes(int uid) {
478 return nativeGetUidStat(uid, TYPE_RX_BYTES);
479 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800480
481 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800482 * Return number of packets transmitted by the given UID since device boot.
483 * Counts packets across all network interfaces, and always increases
484 * monotonically since device boot. Statistics are measured at the network
485 * layer, so they include both TCP and UDP usage.
486 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800487 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800488 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800489 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800490 * @see android.os.Process#myUid()
491 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800492 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800493 public static long getUidTxPackets(int uid) {
494 return nativeGetUidStat(uid, TYPE_TX_PACKETS);
495 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800496
497 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800498 * Return number of packets received by the given UID since device boot.
499 * Counts packets across all network interfaces, and always increases
500 * monotonically since device boot. Statistics are measured at the network
501 * layer, so they include both TCP and UDP usage.
502 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800503 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800504 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800505 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800506 * @see android.os.Process#myUid()
507 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800508 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800509 public static long getUidRxPackets(int uid) {
510 return nativeGetUidStat(uid, TYPE_RX_PACKETS);
511 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800512
513 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800514 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800515 * transport layer statistics are no longer available, and will
516 * always return {@link #UNSUPPORTED}.
517 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800518 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800519 @Deprecated
520 public static long getUidTcpTxBytes(int uid) {
521 return UNSUPPORTED;
522 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800523
524 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800525 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800526 * transport layer statistics are no longer available, and will
527 * always return {@link #UNSUPPORTED}.
528 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800529 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800530 @Deprecated
531 public static long getUidTcpRxBytes(int uid) {
532 return UNSUPPORTED;
533 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800534
535 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800536 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800537 * transport layer statistics are no longer available, and will
538 * always return {@link #UNSUPPORTED}.
539 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800540 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800541 @Deprecated
542 public static long getUidUdpTxBytes(int uid) {
543 return UNSUPPORTED;
544 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800545
546 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800547 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800548 * transport layer statistics are no longer available, and will
549 * always return {@link #UNSUPPORTED}.
550 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800551 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800552 @Deprecated
553 public static long getUidUdpRxBytes(int uid) {
554 return UNSUPPORTED;
555 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800556
557 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800558 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800559 * transport layer statistics are no longer available, and will
560 * always return {@link #UNSUPPORTED}.
561 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800562 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800563 @Deprecated
564 public static long getUidTcpTxSegments(int uid) {
565 return UNSUPPORTED;
566 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800567
568 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800569 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800570 * transport layer statistics are no longer available, and will
571 * always return {@link #UNSUPPORTED}.
572 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800573 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800574 @Deprecated
575 public static long getUidTcpRxSegments(int uid) {
576 return UNSUPPORTED;
577 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800578
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800579 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800580 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800581 * transport layer statistics are no longer available, and will
582 * always return {@link #UNSUPPORTED}.
583 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800584 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800585 @Deprecated
586 public static long getUidUdpTxPackets(int uid) {
587 return UNSUPPORTED;
588 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800589
590 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800591 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800592 * transport layer statistics are no longer available, and will
593 * always return {@link #UNSUPPORTED}.
594 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800595 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800596 @Deprecated
597 public static long getUidUdpRxPackets(int uid) {
598 return UNSUPPORTED;
599 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700600
601 /**
602 * Return detailed {@link NetworkStats} for the current UID. Requires no
603 * special permission.
604 */
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700605 private static NetworkStats getDataLayerSnapshotForUid(Context context) {
Jeff Sharkeydddace72013-03-26 13:46:05 -0700606 // TODO: take snapshot locally, since proc file is now visible
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700607 final int uid = android.os.Process.myUid();
608 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700609 return getStatsService().getDataLayerSnapshotForUid(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700610 } catch (RemoteException e) {
611 throw new RuntimeException(e);
612 }
613 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700614
615 /**
616 * Return set of any ifaces associated with mobile networks since boot.
617 * Interfaces are never removed from this list, so counters should always be
618 * monotonic.
619 */
620 private static String[] getMobileIfaces() {
621 try {
622 return getStatsService().getMobileIfaces();
623 } catch (RemoteException e) {
624 throw new RuntimeException(e);
625 }
626 }
627
628 // NOTE: keep these in sync with android_net_TrafficStats.cpp
629 private static final int TYPE_RX_BYTES = 0;
630 private static final int TYPE_RX_PACKETS = 1;
631 private static final int TYPE_TX_BYTES = 2;
632 private static final int TYPE_TX_PACKETS = 3;
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800633 private static final int TYPE_TCP_RX_PACKETS = 4;
634 private static final int TYPE_TCP_TX_PACKETS = 5;
Jeff Sharkey234766a2012-04-10 19:48:07 -0700635
636 private static native long nativeGetTotalStat(int type);
637 private static native long nativeGetIfaceStat(String iface, int type);
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800638 private static native long nativeGetUidStat(int uid, int type);
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800639}