blob: 786439e5d866cefeb093b07cff5801e7b1c2e4c6 [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
Jeff Sharkey4414cea2011-06-24 17:05:24 -070019import android.app.DownloadManager;
20import android.app.backup.BackupManager;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070021import android.content.Context;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070022import android.media.MediaPlayer;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070023import android.os.RemoteException;
24import android.os.ServiceManager;
25
Jesse Wilson8568db52011-06-28 19:06:31 -070026import com.android.server.NetworkManagementSocketTagger;
Ken Shirrifff7d0b012009-12-07 15:56:05 -080027
Jesse Wilson8568db52011-06-28 19:06:31 -070028import dalvik.system.SocketTagger;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070029
Jeff Sharkey43be1742011-04-21 18:45:43 -070030import java.net.Socket;
31import java.net.SocketException;
Ken Shirrifff7d0b012009-12-07 15:56:05 -080032
33/**
Dan Egnor2b4abcd2010-04-07 17:30:50 -070034 * Class that provides network traffic statistics. These statistics include
35 * bytes transmitted and received and network packets transmitted and received,
36 * over all interfaces, over the mobile interface, and on a per-UID basis.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080037 * <p>
Dan Egnor2b4abcd2010-04-07 17:30:50 -070038 * These statistics may not be available on all platforms. If the statistics
39 * are not supported by this device, {@link #UNSUPPORTED} will be returned.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080040 */
41public class TrafficStats {
42 /**
43 * The return value to indicate that the device does not support the statistic.
44 */
45 public final static int UNSUPPORTED = -1;
46
Jeff Sharkey241dde22012-02-03 14:50:07 -080047 /** @hide */
48 public static final long KB_IN_BYTES = 1024;
49 /** @hide */
50 public static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
51 /** @hide */
52 public static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
53
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070054 /**
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070055 * Special UID value used when collecting {@link NetworkStatsHistory} for
56 * removed applications.
57 *
58 * @hide
59 */
60 public static final int UID_REMOVED = -4;
61
62 /**
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070063 * Special UID value used when collecting {@link NetworkStatsHistory} for
64 * tethering traffic.
65 *
66 * @hide
67 */
68 public static final int UID_TETHERING = -5;
69
70 /**
Jeff Sharkey4414cea2011-06-24 17:05:24 -070071 * Default tag value for {@link DownloadManager} traffic.
72 *
73 * @hide
74 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070075 public static final int TAG_SYSTEM_DOWNLOAD = 0xFFFFFF01;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070076
77 /**
78 * Default tag value for {@link MediaPlayer} traffic.
79 *
80 * @hide
81 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070082 public static final int TAG_SYSTEM_MEDIA = 0xFFFFFF02;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070083
84 /**
85 * Default tag value for {@link BackupManager} traffic.
86 *
87 * @hide
88 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070089 public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070090
Jeff Sharkey234766a2012-04-10 19:48:07 -070091 private static INetworkStatsService sStatsService;
92
93 private synchronized static INetworkStatsService getStatsService() {
94 if (sStatsService == null) {
95 sStatsService = INetworkStatsService.Stub.asInterface(
96 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
97 }
98 return sStatsService;
99 }
100
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700101 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700102 * Snapshot of {@link NetworkStats} when the currently active profiling
103 * session started, or {@code null} if no session active.
104 *
105 * @see #startDataProfiling(Context)
106 * @see #stopDataProfiling(Context)
107 */
108 private static NetworkStats sActiveProfilingStart;
109
110 private static Object sProfilingLock = new Object();
111
112 /**
Jeff Sharkey43be1742011-04-21 18:45:43 -0700113 * Set active tag to use when accounting {@link Socket} traffic originating
114 * from the current thread. Only one active tag per thread is supported.
115 * <p>
116 * Changes only take effect during subsequent calls to
117 * {@link #tagSocket(Socket)}.
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700118 * <p>
119 * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
120 * used internally by system services like {@link DownloadManager} when
121 * performing traffic on behalf of an application.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700122 *
123 * @see #clearThreadStatsTag()
Jeff Sharkey43be1742011-04-21 18:45:43 -0700124 */
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700125 public static void setThreadStatsTag(int tag) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700126 NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700127 }
128
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700129 /**
Alon Alberteaef3512011-07-19 11:16:09 +0300130 * Get the active tag used when accounting {@link Socket} traffic originating
131 * from the current thread. Only one active tag per thread is supported.
132 * {@link #tagSocket(Socket)}.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700133 *
134 * @see #setThreadStatsTag(int)
Alon Alberteaef3512011-07-19 11:16:09 +0300135 */
136 public static int getThreadStatsTag() {
137 return NetworkManagementSocketTagger.getThreadSocketStatsTag();
138 }
139
Jeff Sharkeydddace72013-03-26 13:46:05 -0700140 /**
141 * Clear any active tag set to account {@link Socket} traffic originating
142 * from the current thread.
143 *
144 * @see #setThreadStatsTag(int)
145 */
Jeff Sharkey43be1742011-04-21 18:45:43 -0700146 public static void clearThreadStatsTag() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700147 NetworkManagementSocketTagger.setThreadSocketStatsTag(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700148 }
149
150 /**
151 * Set specific UID to use when accounting {@link Socket} traffic
152 * originating from the current thread. Designed for use when performing an
153 * operation on behalf of another application.
154 * <p>
155 * Changes only take effect during subsequent calls to
156 * {@link #tagSocket(Socket)}.
157 * <p>
158 * To take effect, caller must hold
159 * {@link android.Manifest.permission#UPDATE_DEVICE_STATS} permission.
160 *
Jeff Sharkeydddace72013-03-26 13:46:05 -0700161 * @hide
Jeff Sharkey43be1742011-04-21 18:45:43 -0700162 */
163 public static void setThreadStatsUid(int uid) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700164 NetworkManagementSocketTagger.setThreadSocketStatsUid(uid);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700165 }
166
167 /** {@hide} */
168 public static void clearThreadStatsUid() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700169 NetworkManagementSocketTagger.setThreadSocketStatsUid(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700170 }
171
172 /**
173 * Tag the given {@link Socket} with any statistics parameters active for
174 * the current thread. Subsequent calls always replace any existing
175 * parameters. When finished, call {@link #untagSocket(Socket)} to remove
176 * statistics parameters.
177 *
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700178 * @see #setThreadStatsTag(int)
Jeff Sharkey43be1742011-04-21 18:45:43 -0700179 * @see #setThreadStatsUid(int)
180 */
181 public static void tagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700182 SocketTagger.get().tag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700183 }
184
185 /**
186 * Remove any statistics parameters from the given {@link Socket}.
187 */
188 public static void untagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700189 SocketTagger.get().untag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700190 }
191
192 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700193 * Start profiling data usage for current UID. Only one profiling session
194 * can be active at a time.
195 *
196 * @hide
197 */
198 public static void startDataProfiling(Context context) {
199 synchronized (sProfilingLock) {
200 if (sActiveProfilingStart != null) {
201 throw new IllegalStateException("already profiling data");
202 }
203
204 // take snapshot in time; we calculate delta later
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700205 sActiveProfilingStart = getDataLayerSnapshotForUid(context);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700206 }
207 }
208
209 /**
210 * Stop profiling data usage for current UID.
211 *
212 * @return Detailed {@link NetworkStats} of data that occurred since last
213 * {@link #startDataProfiling(Context)} call.
214 * @hide
215 */
216 public static NetworkStats stopDataProfiling(Context context) {
217 synchronized (sProfilingLock) {
218 if (sActiveProfilingStart == null) {
219 throw new IllegalStateException("not profiling data");
220 }
221
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800222 // subtract starting values and return delta
223 final NetworkStats profilingStop = getDataLayerSnapshotForUid(context);
224 final NetworkStats profilingDelta = NetworkStats.subtract(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800225 profilingStop, sActiveProfilingStart, null, null);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800226 sActiveProfilingStart = null;
227 return profilingDelta;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700228 }
229 }
230
231 /**
Jeff Sharkey558a2322011-08-24 15:42:09 -0700232 * Increment count of network operations performed under the accounting tag
233 * currently active on the calling thread. This can be used to derive
234 * bytes-per-operation.
235 *
236 * @param operationCount Number of operations to increment count by.
237 */
238 public static void incrementOperationCount(int operationCount) {
239 final int tag = getThreadStatsTag();
240 incrementOperationCount(tag, operationCount);
241 }
242
243 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700244 * Increment count of network operations performed under the given
245 * accounting tag. This can be used to derive bytes-per-operation.
246 *
247 * @param tag Accounting tag used in {@link #setThreadStatsTag(int)}.
248 * @param operationCount Number of operations to increment count by.
249 */
250 public static void incrementOperationCount(int tag, int operationCount) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700251 final int uid = android.os.Process.myUid();
252 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700253 getStatsService().incrementOperationCount(uid, tag, operationCount);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700254 } catch (RemoteException e) {
255 throw new RuntimeException(e);
256 }
257 }
258
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700259 /** {@hide} */
260 public static void closeQuietly(INetworkStatsSession session) {
261 // TODO: move to NetworkStatsService once it exists
262 if (session != null) {
263 try {
264 session.close();
265 } catch (RuntimeException rethrown) {
266 throw rethrown;
267 } catch (Exception ignored) {
268 }
269 }
270 }
271
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700272 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700273 * Return number of packets transmitted across mobile networks since device
274 * boot. Counts packets across all mobile network interfaces, and always
275 * increases monotonically since device boot. Statistics are measured at the
276 * network layer, so they include both TCP and UDP usage.
277 * <p>
278 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
279 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800280 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700281 public static long getMobileTxPackets() {
282 long total = 0;
283 for (String iface : getMobileIfaces()) {
284 total += getTxPackets(iface);
285 }
286 return total;
287 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800288
289 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700290 * Return number of packets received across mobile networks since device
291 * boot. Counts packets across all mobile network interfaces, and always
292 * increases monotonically since device boot. Statistics are measured at the
293 * network layer, so they include both TCP and UDP usage.
294 * <p>
295 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
296 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800297 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700298 public static long getMobileRxPackets() {
299 long total = 0;
300 for (String iface : getMobileIfaces()) {
301 total += getRxPackets(iface);
302 }
303 return total;
304 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800305
306 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700307 * Return number of bytes transmitted across mobile networks since device
308 * boot. Counts packets across all mobile network interfaces, and always
309 * increases monotonically since device boot. Statistics are measured at the
310 * network layer, so they include both TCP and UDP usage.
311 * <p>
312 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
313 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800314 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700315 public static long getMobileTxBytes() {
316 long total = 0;
317 for (String iface : getMobileIfaces()) {
318 total += getTxBytes(iface);
319 }
320 return total;
321 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800322
323 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700324 * Return number of bytes received across mobile networks since device boot.
325 * Counts packets across all mobile network interfaces, and always increases
326 * monotonically since device boot. Statistics are measured at the network
327 * layer, so they include both TCP and UDP usage.
328 * <p>
329 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
330 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800331 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700332 public static long getMobileRxBytes() {
333 long total = 0;
334 for (String iface : getMobileIfaces()) {
335 total += getRxBytes(iface);
336 }
337 return total;
338 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800339
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800340 /** {@hide} */
341 public static long getMobileTcpRxPackets() {
342 long total = 0;
343 for (String iface : getMobileIfaces()) {
344 final long stat = nativeGetIfaceStat(iface, TYPE_TCP_RX_PACKETS);
345 if (stat != UNSUPPORTED) {
346 total += stat;
347 }
348 }
349 return total;
350 }
351
352 /** {@hide} */
353 public static long getMobileTcpTxPackets() {
354 long total = 0;
355 for (String iface : getMobileIfaces()) {
356 final long stat = nativeGetIfaceStat(iface, TYPE_TCP_TX_PACKETS);
357 if (stat != UNSUPPORTED) {
358 total += stat;
359 }
360 }
361 return total;
362 }
363
Jeff Sharkeydddace72013-03-26 13:46:05 -0700364 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700365 public static long getTxPackets(String iface) {
366 return nativeGetIfaceStat(iface, TYPE_TX_PACKETS);
367 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800368
Jeff Sharkeydddace72013-03-26 13:46:05 -0700369 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700370 public static long getRxPackets(String iface) {
371 return nativeGetIfaceStat(iface, TYPE_RX_PACKETS);
372 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800373
Jeff Sharkeydddace72013-03-26 13:46:05 -0700374 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700375 public static long getTxBytes(String iface) {
376 return nativeGetIfaceStat(iface, TYPE_TX_BYTES);
377 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800378
Jeff Sharkeydddace72013-03-26 13:46:05 -0700379 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700380 public static long getRxBytes(String iface) {
381 return nativeGetIfaceStat(iface, TYPE_RX_BYTES);
382 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800383
384 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700385 * Return number of packets transmitted since device boot. Counts packets
386 * across all network interfaces, and always increases monotonically since
387 * device boot. Statistics are measured at the network layer, so they
388 * include both TCP and UDP usage.
389 * <p>
390 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
391 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800392 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700393 public static long getTotalTxPackets() {
394 return nativeGetTotalStat(TYPE_TX_PACKETS);
395 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800396
397 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700398 * Return number of packets received 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 getTotalRxPackets() {
407 return nativeGetTotalStat(TYPE_RX_PACKETS);
408 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800409
410 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700411 * Return number of bytes transmitted 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 getTotalTxBytes() {
420 return nativeGetTotalStat(TYPE_TX_BYTES);
421 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800422
423 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700424 * Return number of bytes received since device boot. Counts packets across
425 * all network interfaces, and always increases monotonically since device
426 * boot. Statistics are measured at the network layer, so they include both
427 * 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 getTotalRxBytes() {
433 return nativeGetTotalStat(TYPE_RX_BYTES);
434 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800435
436 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800437 * Return number of bytes transmitted by the given UID since device boot.
438 * Counts packets across all network interfaces, and always increases
439 * monotonically since device boot. Statistics are measured at the network
440 * layer, so they include both TCP and UDP usage.
441 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800442 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800443 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800444 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800445 * @see android.os.Process#myUid()
446 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800447 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800448 public static long getUidTxBytes(int uid) {
449 return nativeGetUidStat(uid, TYPE_TX_BYTES);
450 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800451
452 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800453 * Return number of bytes received by the given UID since device boot.
454 * Counts packets across all network interfaces, and always increases
455 * monotonically since device boot. Statistics are measured at the network
456 * layer, so they include both TCP and UDP usage.
457 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800458 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800459 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800460 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800461 * @see android.os.Process#myUid()
462 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800463 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800464 public static long getUidRxBytes(int uid) {
465 return nativeGetUidStat(uid, TYPE_RX_BYTES);
466 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800467
468 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800469 * Return number of packets transmitted by the given UID since device boot.
470 * Counts packets across all network interfaces, and always increases
471 * monotonically since device boot. Statistics are measured at the network
472 * layer, so they include both TCP and UDP usage.
473 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800474 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800475 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800476 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800477 * @see android.os.Process#myUid()
478 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800479 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800480 public static long getUidTxPackets(int uid) {
481 return nativeGetUidStat(uid, TYPE_TX_PACKETS);
482 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800483
484 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800485 * Return number of packets received by the given UID since device boot.
486 * Counts packets across all network interfaces, and always increases
487 * monotonically since device boot. Statistics are measured at the network
488 * layer, so they include both TCP and UDP usage.
489 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800490 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800491 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800492 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800493 * @see android.os.Process#myUid()
494 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800495 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800496 public static long getUidRxPackets(int uid) {
497 return nativeGetUidStat(uid, TYPE_RX_PACKETS);
498 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800499
500 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800501 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800502 * transport layer statistics are no longer available, and will
503 * always return {@link #UNSUPPORTED}.
504 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800505 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800506 @Deprecated
507 public static long getUidTcpTxBytes(int uid) {
508 return UNSUPPORTED;
509 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800510
511 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800512 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800513 * transport layer statistics are no longer available, and will
514 * always return {@link #UNSUPPORTED}.
515 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800516 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800517 @Deprecated
518 public static long getUidTcpRxBytes(int uid) {
519 return UNSUPPORTED;
520 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800521
522 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800523 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800524 * transport layer statistics are no longer available, and will
525 * always return {@link #UNSUPPORTED}.
526 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800527 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800528 @Deprecated
529 public static long getUidUdpTxBytes(int uid) {
530 return UNSUPPORTED;
531 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800532
533 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800534 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800535 * transport layer statistics are no longer available, and will
536 * always return {@link #UNSUPPORTED}.
537 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800538 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800539 @Deprecated
540 public static long getUidUdpRxBytes(int uid) {
541 return UNSUPPORTED;
542 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800543
544 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800545 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800546 * transport layer statistics are no longer available, and will
547 * always return {@link #UNSUPPORTED}.
548 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800549 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800550 @Deprecated
551 public static long getUidTcpTxSegments(int uid) {
552 return UNSUPPORTED;
553 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800554
555 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800556 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800557 * transport layer statistics are no longer available, and will
558 * always return {@link #UNSUPPORTED}.
559 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800560 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800561 @Deprecated
562 public static long getUidTcpRxSegments(int uid) {
563 return UNSUPPORTED;
564 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800565
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800566 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800567 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800568 * transport layer statistics are no longer available, and will
569 * always return {@link #UNSUPPORTED}.
570 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800571 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800572 @Deprecated
573 public static long getUidUdpTxPackets(int uid) {
574 return UNSUPPORTED;
575 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800576
577 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800578 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800579 * transport layer statistics are no longer available, and will
580 * always return {@link #UNSUPPORTED}.
581 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800582 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800583 @Deprecated
584 public static long getUidUdpRxPackets(int uid) {
585 return UNSUPPORTED;
586 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700587
588 /**
589 * Return detailed {@link NetworkStats} for the current UID. Requires no
590 * special permission.
591 */
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700592 private static NetworkStats getDataLayerSnapshotForUid(Context context) {
Jeff Sharkeydddace72013-03-26 13:46:05 -0700593 // TODO: take snapshot locally, since proc file is now visible
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700594 final int uid = android.os.Process.myUid();
595 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700596 return getStatsService().getDataLayerSnapshotForUid(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700597 } catch (RemoteException e) {
598 throw new RuntimeException(e);
599 }
600 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700601
602 /**
603 * Return set of any ifaces associated with mobile networks since boot.
604 * Interfaces are never removed from this list, so counters should always be
605 * monotonic.
606 */
607 private static String[] getMobileIfaces() {
608 try {
609 return getStatsService().getMobileIfaces();
610 } catch (RemoteException e) {
611 throw new RuntimeException(e);
612 }
613 }
614
615 // NOTE: keep these in sync with android_net_TrafficStats.cpp
616 private static final int TYPE_RX_BYTES = 0;
617 private static final int TYPE_RX_PACKETS = 1;
618 private static final int TYPE_TX_BYTES = 2;
619 private static final int TYPE_TX_PACKETS = 3;
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800620 private static final int TYPE_TCP_RX_PACKETS = 4;
621 private static final int TYPE_TCP_TX_PACKETS = 5;
Jeff Sharkey234766a2012-04-10 19:48:07 -0700622
623 private static native long nativeGetTotalStat(int type);
624 private static native long nativeGetIfaceStat(String iface, int type);
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800625 private static native long nativeGetUidStat(int uid, int type);
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800626}