blob: 49c6f74b1a34e22cd4c2116cf024860367d0874b [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 Sharkeya4239cf2017-11-29 11:18:23 -070019import android.annotation.SuppressLint;
Christopher Tatea2496de2014-08-06 14:19:56 -070020import android.annotation.SystemApi;
Benedict Wongbabe5d72017-12-03 19:42:36 -080021import android.annotation.TestApi;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010022import android.annotation.UnsupportedAppUsage;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070023import android.app.DownloadManager;
24import android.app.backup.BackupManager;
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -070025import android.app.usage.NetworkStatsManager;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070026import android.content.Context;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070027import android.media.MediaPlayer;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070028import android.os.RemoteException;
29import android.os.ServiceManager;
Jeff Sharkey9f2dc052018-01-07 16:47:31 -070030import android.util.DataUnit;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070031
Jesse Wilson8568db52011-06-28 19:06:31 -070032import com.android.server.NetworkManagementSocketTagger;
Ken Shirrifff7d0b012009-12-07 15:56:05 -080033
Jesse Wilson8568db52011-06-28 19:06:31 -070034import dalvik.system.SocketTagger;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070035
Jeff Sharkeya4239cf2017-11-29 11:18:23 -070036import java.io.FileDescriptor;
37import java.io.IOException;
Jeff Sharkeyf0d76332015-12-04 15:21:52 -070038import java.net.DatagramSocket;
Jeff Sharkey43be1742011-04-21 18:45:43 -070039import java.net.Socket;
40import java.net.SocketException;
Ken Shirrifff7d0b012009-12-07 15:56:05 -080041
42/**
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -070043 * Class that provides network traffic statistics. These statistics include
Dan Egnor2b4abcd2010-04-07 17:30:50 -070044 * bytes transmitted and received and network packets transmitted and received,
45 * over all interfaces, over the mobile interface, and on a per-UID basis.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080046 * <p>
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -070047 * These statistics may not be available on all platforms. If the statistics are
48 * not supported by this device, {@link #UNSUPPORTED} will be returned.
49 * <p>
50 * Note that the statistics returned by this class reset and start from zero
51 * after every reboot. To access more robust historical network statistics data,
52 * use {@link NetworkStatsManager} instead.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080053 */
54public class TrafficStats {
55 /**
56 * The return value to indicate that the device does not support the statistic.
57 */
58 public final static int UNSUPPORTED = -1;
59
Jeff Sharkey9f2dc052018-01-07 16:47:31 -070060 /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
61 @Deprecated
Jeff Sharkey241dde22012-02-03 14:50:07 -080062 public static final long KB_IN_BYTES = 1024;
Jeff Sharkey9f2dc052018-01-07 16:47:31 -070063 /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
64 @Deprecated
Jeff Sharkey241dde22012-02-03 14:50:07 -080065 public static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
Jeff Sharkey9f2dc052018-01-07 16:47:31 -070066 /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
67 @Deprecated
Jeff Sharkey241dde22012-02-03 14:50:07 -080068 public static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
Jeff Sharkey9f2dc052018-01-07 16:47:31 -070069 /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
70 @Deprecated
Jeff Sharkey48877892015-03-18 11:27:19 -070071 public static final long TB_IN_BYTES = GB_IN_BYTES * 1024;
Jeff Sharkey9f2dc052018-01-07 16:47:31 -070072 /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
73 @Deprecated
Jeff Sharkeyb521fea2015-06-15 21:09:10 -070074 public static final long PB_IN_BYTES = TB_IN_BYTES * 1024;
Jeff Sharkey241dde22012-02-03 14:50:07 -080075
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070076 /**
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070077 * Special UID value used when collecting {@link NetworkStatsHistory} for
78 * removed applications.
79 *
80 * @hide
81 */
82 public static final int UID_REMOVED = -4;
83
84 /**
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070085 * Special UID value used when collecting {@link NetworkStatsHistory} for
86 * tethering traffic.
87 *
88 * @hide
89 */
90 public static final int UID_TETHERING = -5;
91
92 /**
Jeff Sharkey4414cea2011-06-24 17:05:24 -070093 * Default tag value for {@link DownloadManager} traffic.
94 *
95 * @hide
96 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070097 public static final int TAG_SYSTEM_DOWNLOAD = 0xFFFFFF01;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070098
99 /**
100 * Default tag value for {@link MediaPlayer} traffic.
101 *
102 * @hide
103 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700104 public static final int TAG_SYSTEM_MEDIA = 0xFFFFFF02;
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700105
106 /**
Christopher Tate0a61b362015-11-10 10:49:20 -0800107 * Default tag value for {@link BackupManager} backup traffic; that is,
108 * traffic from the device to the storage backend.
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700109 *
110 * @hide
111 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700112 public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03;
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700113
Christopher Tate0a61b362015-11-10 10:49:20 -0800114 /**
115 * Default tag value for {@link BackupManager} restore traffic; that is,
116 * app data retrieved from the storage backend at install time.
117 *
118 * @hide
119 */
120 public static final int TAG_SYSTEM_RESTORE = 0xFFFFFF04;
121
Jeff Sharkey763a1402016-11-21 12:14:50 -0700122 /**
Jeff Sharkeyd62e3cb2017-08-11 15:04:12 -0600123 * Default tag value for code (typically APKs) downloaded by an app store on
124 * behalf of the app, such as updates.
Jeff Sharkey763a1402016-11-21 12:14:50 -0700125 *
126 * @hide
127 */
Jeff Sharkeyd62e3cb2017-08-11 15:04:12 -0600128 public static final int TAG_SYSTEM_APP = 0xFFFFFF05;
Jeff Sharkeyf23a5e12017-06-26 19:50:41 -0600129
130 /** @hide */
Remi NGUYEN VAN53b03142019-01-23 23:11:12 +0900131 @SystemApi
132 @TestApi
Jeff Sharkeyf23a5e12017-06-26 19:50:41 -0600133 public static final int TAG_SYSTEM_DHCP = 0xFFFFFF40;
134 /** @hide */
135 public static final int TAG_SYSTEM_NTP = 0xFFFFFF41;
136 /** @hide */
Remi NGUYEN VAN53b03142019-01-23 23:11:12 +0900137 @SystemApi
138 @TestApi
Jeff Sharkeyf23a5e12017-06-26 19:50:41 -0600139 public static final int TAG_SYSTEM_PROBE = 0xFFFFFF42;
140 /** @hide */
141 public static final int TAG_SYSTEM_NEIGHBOR = 0xFFFFFF43;
142 /** @hide */
143 public static final int TAG_SYSTEM_GPS = 0xFFFFFF44;
144 /** @hide */
145 public static final int TAG_SYSTEM_PAC = 0xFFFFFF45;
Remi NGUYEN VANa13007a2018-08-13 15:54:27 +0900146 /** @hide */
Remi NGUYEN VAN53b03142019-01-23 23:11:12 +0900147 @SystemApi
148 @TestApi
Remi NGUYEN VANa13007a2018-08-13 15:54:27 +0900149 public static final int TAG_SYSTEM_DHCP_SERVER = 0xFFFFFF46;
Jeff Sharkey763a1402016-11-21 12:14:50 -0700150
Jeff Sharkey234766a2012-04-10 19:48:07 -0700151 private static INetworkStatsService sStatsService;
152
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100153 @UnsupportedAppUsage
Jeff Sharkey234766a2012-04-10 19:48:07 -0700154 private synchronized static INetworkStatsService getStatsService() {
155 if (sStatsService == null) {
156 sStatsService = INetworkStatsService.Stub.asInterface(
157 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
158 }
159 return sStatsService;
160 }
161
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700162 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700163 * Snapshot of {@link NetworkStats} when the currently active profiling
164 * session started, or {@code null} if no session active.
165 *
166 * @see #startDataProfiling(Context)
167 * @see #stopDataProfiling(Context)
168 */
169 private static NetworkStats sActiveProfilingStart;
170
171 private static Object sProfilingLock = new Object();
172
Benedict Wongbabe5d72017-12-03 19:42:36 -0800173 private static final String LOOPBACK_IFACE = "lo";
174
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700175 /**
Jeff Sharkey43be1742011-04-21 18:45:43 -0700176 * Set active tag to use when accounting {@link Socket} traffic originating
177 * from the current thread. Only one active tag per thread is supported.
178 * <p>
179 * Changes only take effect during subsequent calls to
180 * {@link #tagSocket(Socket)}.
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700181 * <p>
182 * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
183 * used internally by system services like {@link DownloadManager} when
184 * performing traffic on behalf of an application.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700185 *
186 * @see #clearThreadStatsTag()
Jeff Sharkey43be1742011-04-21 18:45:43 -0700187 */
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700188 public static void setThreadStatsTag(int tag) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700189 NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700190 }
191
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700192 /**
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600193 * Set active tag to use when accounting {@link Socket} traffic originating
Jeff Sharkey619a5112017-01-19 11:55:54 -0700194 * from the current thread. Only one active tag per thread is supported.
195 * <p>
196 * Changes only take effect during subsequent calls to
197 * {@link #tagSocket(Socket)}.
198 * <p>
199 * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
200 * used internally by system services like {@link DownloadManager} when
201 * performing traffic on behalf of an application.
202 *
203 * @return the current tag for the calling thread, which can be used to
204 * restore any existing values after a nested operation is finished
205 */
206 public static int getAndSetThreadStatsTag(int tag) {
207 return NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
208 }
209
210 /**
211 * Set active tag to use when accounting {@link Socket} traffic originating
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600212 * from the current thread. The tag used internally is well-defined to
213 * distinguish all backup-related traffic.
214 *
Christopher Tatea2496de2014-08-06 14:19:56 -0700215 * @hide
216 */
217 @SystemApi
218 public static void setThreadStatsTagBackup() {
219 setThreadStatsTag(TAG_SYSTEM_BACKUP);
220 }
221
222 /**
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600223 * Set active tag to use when accounting {@link Socket} traffic originating
224 * from the current thread. The tag used internally is well-defined to
225 * distinguish all restore-related traffic.
226 *
Christopher Tate0a61b362015-11-10 10:49:20 -0800227 * @hide
228 */
229 @SystemApi
230 public static void setThreadStatsTagRestore() {
231 setThreadStatsTag(TAG_SYSTEM_RESTORE);
232 }
233
234 /**
Jeff Sharkeyf23a5e12017-06-26 19:50:41 -0600235 * Set active tag to use when accounting {@link Socket} traffic originating
236 * from the current thread. The tag used internally is well-defined to
Jeff Sharkeyd62e3cb2017-08-11 15:04:12 -0600237 * distinguish all code (typically APKs) downloaded by an app store on
238 * behalf of the app, such as updates.
Jeff Sharkeyf23a5e12017-06-26 19:50:41 -0600239 *
240 * @hide
241 */
242 @SystemApi
Jeff Sharkeyd62e3cb2017-08-11 15:04:12 -0600243 public static void setThreadStatsTagApp() {
244 setThreadStatsTag(TAG_SYSTEM_APP);
Jeff Sharkeyf23a5e12017-06-26 19:50:41 -0600245 }
246
247 /**
Alon Alberteaef3512011-07-19 11:16:09 +0300248 * Get the active tag used when accounting {@link Socket} traffic originating
249 * from the current thread. Only one active tag per thread is supported.
250 * {@link #tagSocket(Socket)}.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700251 *
252 * @see #setThreadStatsTag(int)
Alon Alberteaef3512011-07-19 11:16:09 +0300253 */
254 public static int getThreadStatsTag() {
255 return NetworkManagementSocketTagger.getThreadSocketStatsTag();
256 }
257
Jeff Sharkeydddace72013-03-26 13:46:05 -0700258 /**
259 * Clear any active tag set to account {@link Socket} traffic originating
260 * from the current thread.
261 *
262 * @see #setThreadStatsTag(int)
263 */
Jeff Sharkey43be1742011-04-21 18:45:43 -0700264 public static void clearThreadStatsTag() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700265 NetworkManagementSocketTagger.setThreadSocketStatsTag(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700266 }
267
268 /**
269 * Set specific UID to use when accounting {@link Socket} traffic
270 * originating from the current thread. Designed for use when performing an
Jeff Sharkey121d5652018-03-26 13:11:33 -0600271 * operation on behalf of another application, or when another application
272 * is performing operations on your behalf.
273 * <p>
274 * Any app can <em>accept</em> blame for traffic performed on a socket
275 * originally created by another app by calling this method with the
276 * {@link android.system.Os#getuid()} value. However, only apps holding the
277 * {@code android.Manifest.permission#UPDATE_DEVICE_STATS} permission may
278 * <em>assign</em> blame to another UIDs.
Jeff Sharkey43be1742011-04-21 18:45:43 -0700279 * <p>
280 * Changes only take effect during subsequent calls to
281 * {@link #tagSocket(Socket)}.
Jeff Sharkey43be1742011-04-21 18:45:43 -0700282 */
Jeff Sharkey121d5652018-03-26 13:11:33 -0600283 @SuppressLint("Doclava125")
Jeff Sharkey43be1742011-04-21 18:45:43 -0700284 public static void setThreadStatsUid(int uid) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700285 NetworkManagementSocketTagger.setThreadSocketStatsUid(uid);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700286 }
287
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600288 /**
Jeff Sharkey121d5652018-03-26 13:11:33 -0600289 * Get the active UID used when accounting {@link Socket} traffic originating
290 * from the current thread. Only one active tag per thread is supported.
291 * {@link #tagSocket(Socket)}.
292 *
293 * @see #setThreadStatsUid(int)
294 */
295 public static int getThreadStatsUid() {
296 return NetworkManagementSocketTagger.getThreadSocketStatsUid();
297 }
298
299 /**
Jeff Sharkeya4239cf2017-11-29 11:18:23 -0700300 * Set specific UID to use when accounting {@link Socket} traffic
301 * originating from the current thread as the calling UID. Designed for use
302 * when another application is performing operations on your behalf.
303 * <p>
304 * Changes only take effect during subsequent calls to
305 * {@link #tagSocket(Socket)}.
Jeff Sharkey121d5652018-03-26 13:11:33 -0600306 *
307 * @removed
308 * @deprecated use {@link #setThreadStatsUid(int)} instead.
Jeff Sharkeya4239cf2017-11-29 11:18:23 -0700309 */
Jeff Sharkey121d5652018-03-26 13:11:33 -0600310 @Deprecated
Jeff Sharkeya4239cf2017-11-29 11:18:23 -0700311 public static void setThreadStatsUidSelf() {
312 setThreadStatsUid(android.os.Process.myUid());
313 }
314
315 /**
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600316 * Clear any active UID set to account {@link Socket} traffic originating
317 * from the current thread.
318 *
319 * @see #setThreadStatsUid(int)
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600320 */
Jeff Sharkeya4239cf2017-11-29 11:18:23 -0700321 @SuppressLint("Doclava125")
Jeff Sharkey43be1742011-04-21 18:45:43 -0700322 public static void clearThreadStatsUid() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700323 NetworkManagementSocketTagger.setThreadSocketStatsUid(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700324 }
325
326 /**
327 * Tag the given {@link Socket} with any statistics parameters active for
328 * the current thread. Subsequent calls always replace any existing
329 * parameters. When finished, call {@link #untagSocket(Socket)} to remove
330 * statistics parameters.
331 *
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700332 * @see #setThreadStatsTag(int)
Jeff Sharkey43be1742011-04-21 18:45:43 -0700333 */
334 public static void tagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700335 SocketTagger.get().tag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700336 }
337
338 /**
339 * Remove any statistics parameters from the given {@link Socket}.
kopriva5ad001e2018-07-23 18:19:39 -0700340 * <p>
341 * In Android 8.1 (API level 27) and lower, a socket is automatically
342 * untagged when it's sent to another process using binder IPC with a
343 * {@code ParcelFileDescriptor} container. In Android 9.0 (API level 28)
344 * and higher, the socket tag is kept when the socket is sent to another
345 * process using binder IPC. You can mimic the previous behavior by
346 * calling {@code untagSocket()} before sending the socket to another
347 * process.
Jeff Sharkey43be1742011-04-21 18:45:43 -0700348 */
349 public static void untagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700350 SocketTagger.get().untag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700351 }
352
353 /**
Jeff Sharkeyf0d76332015-12-04 15:21:52 -0700354 * Tag the given {@link DatagramSocket} with any statistics parameters
355 * active for the current thread. Subsequent calls always replace any
356 * existing parameters. When finished, call
357 * {@link #untagDatagramSocket(DatagramSocket)} to remove statistics
358 * parameters.
359 *
360 * @see #setThreadStatsTag(int)
Jeff Sharkeyf0d76332015-12-04 15:21:52 -0700361 */
362 public static void tagDatagramSocket(DatagramSocket socket) throws SocketException {
363 SocketTagger.get().tag(socket);
364 }
365
366 /**
367 * Remove any statistics parameters from the given {@link DatagramSocket}.
368 */
369 public static void untagDatagramSocket(DatagramSocket socket) throws SocketException {
370 SocketTagger.get().untag(socket);
371 }
372
373 /**
Jeff Sharkeya4239cf2017-11-29 11:18:23 -0700374 * Tag the given {@link FileDescriptor} socket with any statistics
375 * parameters active for the current thread. Subsequent calls always replace
376 * any existing parameters. When finished, call
377 * {@link #untagFileDescriptor(FileDescriptor)} to remove statistics
378 * parameters.
379 *
380 * @see #setThreadStatsTag(int)
381 */
382 public static void tagFileDescriptor(FileDescriptor fd) throws IOException {
383 SocketTagger.get().tag(fd);
384 }
385
386 /**
387 * Remove any statistics parameters from the given {@link FileDescriptor}
388 * socket.
389 */
390 public static void untagFileDescriptor(FileDescriptor fd) throws IOException {
391 SocketTagger.get().untag(fd);
392 }
393
394 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700395 * Start profiling data usage for current UID. Only one profiling session
396 * can be active at a time.
397 *
398 * @hide
399 */
400 public static void startDataProfiling(Context context) {
401 synchronized (sProfilingLock) {
402 if (sActiveProfilingStart != null) {
403 throw new IllegalStateException("already profiling data");
404 }
405
406 // take snapshot in time; we calculate delta later
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700407 sActiveProfilingStart = getDataLayerSnapshotForUid(context);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700408 }
409 }
410
411 /**
412 * Stop profiling data usage for current UID.
413 *
414 * @return Detailed {@link NetworkStats} of data that occurred since last
415 * {@link #startDataProfiling(Context)} call.
416 * @hide
417 */
418 public static NetworkStats stopDataProfiling(Context context) {
419 synchronized (sProfilingLock) {
420 if (sActiveProfilingStart == null) {
421 throw new IllegalStateException("not profiling data");
422 }
423
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800424 // subtract starting values and return delta
425 final NetworkStats profilingStop = getDataLayerSnapshotForUid(context);
426 final NetworkStats profilingDelta = NetworkStats.subtract(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800427 profilingStop, sActiveProfilingStart, null, null);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800428 sActiveProfilingStart = null;
429 return profilingDelta;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700430 }
431 }
432
433 /**
Jeff Sharkey558a2322011-08-24 15:42:09 -0700434 * Increment count of network operations performed under the accounting tag
435 * currently active on the calling thread. This can be used to derive
436 * bytes-per-operation.
437 *
438 * @param operationCount Number of operations to increment count by.
439 */
440 public static void incrementOperationCount(int operationCount) {
441 final int tag = getThreadStatsTag();
442 incrementOperationCount(tag, operationCount);
443 }
444
445 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700446 * Increment count of network operations performed under the given
447 * accounting tag. This can be used to derive bytes-per-operation.
448 *
449 * @param tag Accounting tag used in {@link #setThreadStatsTag(int)}.
450 * @param operationCount Number of operations to increment count by.
451 */
452 public static void incrementOperationCount(int tag, int operationCount) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700453 final int uid = android.os.Process.myUid();
454 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700455 getStatsService().incrementOperationCount(uid, tag, operationCount);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700456 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700457 throw e.rethrowFromSystemServer();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700458 }
459 }
460
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700461 /** {@hide} */
462 public static void closeQuietly(INetworkStatsSession session) {
463 // TODO: move to NetworkStatsService once it exists
464 if (session != null) {
465 try {
466 session.close();
467 } catch (RuntimeException rethrown) {
468 throw rethrown;
469 } catch (Exception ignored) {
470 }
471 }
472 }
473
Chenbo Feng20328e82018-01-25 11:43:52 -0800474 private static long addIfSupported(long stat) {
475 return (stat == UNSUPPORTED) ? 0 : stat;
476 }
477
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700478 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700479 * Return number of packets transmitted across mobile networks since device
480 * boot. Counts packets across all mobile network interfaces, and always
481 * increases monotonically since device boot. Statistics are measured at the
482 * network layer, so they include both TCP and UDP usage.
483 * <p>
484 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
485 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800486 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700487 public static long getMobileTxPackets() {
488 long total = 0;
489 for (String iface : getMobileIfaces()) {
Chenbo Feng20328e82018-01-25 11:43:52 -0800490 total += addIfSupported(getTxPackets(iface));
Jeff Sharkey234766a2012-04-10 19:48:07 -0700491 }
492 return total;
493 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800494
495 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700496 * Return number of packets received across mobile networks since device
497 * boot. Counts packets across all mobile network interfaces, and always
498 * increases monotonically since device boot. Statistics are measured at the
499 * network layer, so they include both TCP and UDP usage.
500 * <p>
501 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
502 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800503 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700504 public static long getMobileRxPackets() {
505 long total = 0;
506 for (String iface : getMobileIfaces()) {
Chenbo Feng20328e82018-01-25 11:43:52 -0800507 total += addIfSupported(getRxPackets(iface));
Jeff Sharkey234766a2012-04-10 19:48:07 -0700508 }
509 return total;
510 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800511
512 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700513 * Return number of bytes transmitted across mobile networks since device
514 * boot. Counts packets across all mobile network interfaces, and always
515 * increases monotonically since device boot. Statistics are measured at the
516 * network layer, so they include both TCP and UDP usage.
517 * <p>
518 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
519 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800520 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700521 public static long getMobileTxBytes() {
522 long total = 0;
523 for (String iface : getMobileIfaces()) {
Chenbo Feng20328e82018-01-25 11:43:52 -0800524 total += addIfSupported(getTxBytes(iface));
Jeff Sharkey234766a2012-04-10 19:48:07 -0700525 }
526 return total;
527 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800528
529 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700530 * Return number of bytes received across mobile networks since device boot.
531 * Counts packets across all mobile network interfaces, and always increases
532 * monotonically since device boot. Statistics are measured at the network
533 * layer, so they include both TCP and UDP usage.
534 * <p>
535 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
536 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800537 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700538 public static long getMobileRxBytes() {
539 long total = 0;
540 for (String iface : getMobileIfaces()) {
Chenbo Feng20328e82018-01-25 11:43:52 -0800541 total += addIfSupported(getRxBytes(iface));
Jeff Sharkey234766a2012-04-10 19:48:07 -0700542 }
543 return total;
544 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800545
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800546 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100547 @UnsupportedAppUsage
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800548 public static long getMobileTcpRxPackets() {
549 long total = 0;
550 for (String iface : getMobileIfaces()) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800551 long stat = UNSUPPORTED;
552 try {
553 stat = getStatsService().getIfaceStats(iface, TYPE_TCP_RX_PACKETS);
554 } catch (RemoteException e) {
555 throw e.rethrowFromSystemServer();
556 }
Chenbo Feng20328e82018-01-25 11:43:52 -0800557 total += addIfSupported(stat);
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800558 }
559 return total;
560 }
561
562 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100563 @UnsupportedAppUsage
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800564 public static long getMobileTcpTxPackets() {
565 long total = 0;
566 for (String iface : getMobileIfaces()) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800567 long stat = UNSUPPORTED;
568 try {
569 stat = getStatsService().getIfaceStats(iface, TYPE_TCP_TX_PACKETS);
570 } catch (RemoteException e) {
571 throw e.rethrowFromSystemServer();
572 }
Chenbo Feng20328e82018-01-25 11:43:52 -0800573 total += addIfSupported(stat);
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800574 }
575 return total;
576 }
577
Jeff Sharkeydddace72013-03-26 13:46:05 -0700578 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700579 public static long getTxPackets(String iface) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800580 try {
581 return getStatsService().getIfaceStats(iface, TYPE_TX_PACKETS);
582 } catch (RemoteException e) {
583 throw e.rethrowFromSystemServer();
584 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700585 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800586
Jeff Sharkeydddace72013-03-26 13:46:05 -0700587 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700588 public static long getRxPackets(String iface) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800589 try {
590 return getStatsService().getIfaceStats(iface, TYPE_RX_PACKETS);
591 } catch (RemoteException e) {
592 throw e.rethrowFromSystemServer();
593 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700594 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800595
Jeff Sharkeydddace72013-03-26 13:46:05 -0700596 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100597 @UnsupportedAppUsage
Jeff Sharkey234766a2012-04-10 19:48:07 -0700598 public static long getTxBytes(String iface) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800599 try {
600 return getStatsService().getIfaceStats(iface, TYPE_TX_BYTES);
601 } catch (RemoteException e) {
602 throw e.rethrowFromSystemServer();
603 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700604 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800605
Jeff Sharkeydddace72013-03-26 13:46:05 -0700606 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100607 @UnsupportedAppUsage
Jeff Sharkey234766a2012-04-10 19:48:07 -0700608 public static long getRxBytes(String iface) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800609 try {
610 return getStatsService().getIfaceStats(iface, TYPE_RX_BYTES);
611 } catch (RemoteException e) {
612 throw e.rethrowFromSystemServer();
613 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700614 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800615
Benedict Wongbabe5d72017-12-03 19:42:36 -0800616 /** {@hide} */
617 @TestApi
618 public static long getLoopbackTxPackets() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800619 try {
620 return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_TX_PACKETS);
621 } catch (RemoteException e) {
622 throw e.rethrowFromSystemServer();
623 }
Benedict Wongbabe5d72017-12-03 19:42:36 -0800624 }
625
626 /** {@hide} */
627 @TestApi
628 public static long getLoopbackRxPackets() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800629 try {
630 return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_RX_PACKETS);
631 } catch (RemoteException e) {
632 throw e.rethrowFromSystemServer();
633 }
Benedict Wongbabe5d72017-12-03 19:42:36 -0800634 }
635
636 /** {@hide} */
637 @TestApi
638 public static long getLoopbackTxBytes() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800639 try {
640 return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_TX_BYTES);
641 } catch (RemoteException e) {
642 throw e.rethrowFromSystemServer();
643 }
Benedict Wongbabe5d72017-12-03 19:42:36 -0800644 }
645
646 /** {@hide} */
647 @TestApi
648 public static long getLoopbackRxBytes() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800649 try {
650 return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_RX_BYTES);
651 } catch (RemoteException e) {
652 throw e.rethrowFromSystemServer();
653 }
Benedict Wongbabe5d72017-12-03 19:42:36 -0800654 }
655
Irfan Sheriff227bec42011-02-15 19:30:27 -0800656 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700657 * Return number of packets transmitted since device boot. Counts packets
658 * across all network interfaces, and always increases monotonically since
659 * device boot. Statistics are measured at the network layer, so they
660 * include both TCP and UDP usage.
661 * <p>
662 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
663 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800664 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700665 public static long getTotalTxPackets() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800666 try {
667 return getStatsService().getTotalStats(TYPE_TX_PACKETS);
668 } catch (RemoteException e) {
669 throw e.rethrowFromSystemServer();
670 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700671 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800672
673 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700674 * Return number of packets received since device boot. Counts packets
675 * across all network interfaces, and always increases monotonically since
676 * device boot. Statistics are measured at the network layer, so they
677 * include both TCP and UDP usage.
678 * <p>
679 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
680 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800681 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700682 public static long getTotalRxPackets() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800683 try {
684 return getStatsService().getTotalStats(TYPE_RX_PACKETS);
685 } catch (RemoteException e) {
686 throw e.rethrowFromSystemServer();
687 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700688 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800689
690 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700691 * Return number of bytes transmitted since device boot. Counts packets
692 * across all network interfaces, and always increases monotonically since
693 * device boot. Statistics are measured at the network layer, so they
694 * include both TCP and UDP usage.
695 * <p>
696 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
697 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800698 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700699 public static long getTotalTxBytes() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800700 try {
701 return getStatsService().getTotalStats(TYPE_TX_BYTES);
702 } catch (RemoteException e) {
703 throw e.rethrowFromSystemServer();
704 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700705 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800706
707 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700708 * Return number of bytes received since device boot. Counts packets across
709 * all network interfaces, and always increases monotonically since device
710 * boot. Statistics are measured at the network layer, so they include both
711 * TCP and UDP usage.
712 * <p>
713 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
714 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800715 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700716 public static long getTotalRxBytes() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800717 try {
718 return getStatsService().getTotalStats(TYPE_RX_BYTES);
719 } catch (RemoteException e) {
720 throw e.rethrowFromSystemServer();
721 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700722 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800723
724 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800725 * Return number of bytes transmitted by the given UID since device boot.
726 * Counts packets across all network interfaces, and always increases
727 * monotonically since device boot. Statistics are measured at the network
728 * layer, so they include both TCP and UDP usage.
729 * <p>
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700730 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
731 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
732 * <p>
733 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
734 * report traffic statistics for the calling UID. It will return
735 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
736 * historical network statistics belonging to other UIDs, use
737 * {@link NetworkStatsManager}.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800738 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800739 * @see android.os.Process#myUid()
740 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800741 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800742 public static long getUidTxBytes(int uid) {
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700743 // This isn't actually enforcing any security; it just returns the
744 // unsupported value. The real filtering is done at the kernel level.
745 final int callingUid = android.os.Process.myUid();
746 if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800747 try {
748 return getStatsService().getUidStats(uid, TYPE_TX_BYTES);
749 } catch (RemoteException e) {
750 throw e.rethrowFromSystemServer();
751 }
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700752 } else {
753 return UNSUPPORTED;
754 }
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800755 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800756
757 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800758 * Return number of bytes received by the given UID since device boot.
759 * Counts packets across all network interfaces, and always increases
760 * monotonically since device boot. Statistics are measured at the network
761 * layer, so they include both TCP and UDP usage.
762 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800763 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800764 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700765 * <p>
766 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
767 * report traffic statistics for the calling UID. It will return
768 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
769 * historical network statistics belonging to other UIDs, use
770 * {@link NetworkStatsManager}.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800771 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800772 * @see android.os.Process#myUid()
773 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800774 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800775 public static long getUidRxBytes(int uid) {
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700776 // This isn't actually enforcing any security; it just returns the
777 // unsupported value. The real filtering is done at the kernel level.
778 final int callingUid = android.os.Process.myUid();
779 if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800780 try {
781 return getStatsService().getUidStats(uid, TYPE_RX_BYTES);
782 } catch (RemoteException e) {
783 throw e.rethrowFromSystemServer();
784 }
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700785 } else {
786 return UNSUPPORTED;
787 }
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800788 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800789
790 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800791 * Return number of packets transmitted by the given UID since device boot.
792 * Counts packets across all network interfaces, and always increases
793 * monotonically since device boot. Statistics are measured at the network
794 * layer, so they include both TCP and UDP usage.
795 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800796 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800797 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700798 * <p>
799 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
800 * report traffic statistics for the calling UID. It will return
801 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
802 * historical network statistics belonging to other UIDs, use
803 * {@link NetworkStatsManager}.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800804 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800805 * @see android.os.Process#myUid()
806 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800807 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800808 public static long getUidTxPackets(int uid) {
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700809 // This isn't actually enforcing any security; it just returns the
810 // unsupported value. The real filtering is done at the kernel level.
811 final int callingUid = android.os.Process.myUid();
812 if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800813 try {
814 return getStatsService().getUidStats(uid, TYPE_TX_PACKETS);
815 } catch (RemoteException e) {
816 throw e.rethrowFromSystemServer();
817 }
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700818 } else {
819 return UNSUPPORTED;
820 }
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800821 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800822
823 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800824 * Return number of packets received by the given UID since device boot.
825 * Counts packets across all network interfaces, and always increases
826 * monotonically since device boot. Statistics are measured at the network
827 * layer, so they include both TCP and UDP usage.
828 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800829 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800830 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700831 * <p>
832 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
833 * report traffic statistics for the calling UID. It will return
834 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
835 * historical network statistics belonging to other UIDs, use
836 * {@link NetworkStatsManager}.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800837 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800838 * @see android.os.Process#myUid()
839 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800840 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800841 public static long getUidRxPackets(int uid) {
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700842 // This isn't actually enforcing any security; it just returns the
843 // unsupported value. The real filtering is done at the kernel level.
844 final int callingUid = android.os.Process.myUid();
845 if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800846 try {
847 return getStatsService().getUidStats(uid, TYPE_RX_PACKETS);
848 } catch (RemoteException e) {
849 throw e.rethrowFromSystemServer();
850 }
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700851 } else {
852 return UNSUPPORTED;
853 }
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800854 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800855
856 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800857 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800858 * transport layer statistics are no longer available, and will
859 * always return {@link #UNSUPPORTED}.
860 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800861 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800862 @Deprecated
863 public static long getUidTcpTxBytes(int uid) {
864 return UNSUPPORTED;
865 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800866
867 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800868 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800869 * transport layer statistics are no longer available, and will
870 * always return {@link #UNSUPPORTED}.
871 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800872 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800873 @Deprecated
874 public static long getUidTcpRxBytes(int uid) {
875 return UNSUPPORTED;
876 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800877
878 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800879 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800880 * transport layer statistics are no longer available, and will
881 * always return {@link #UNSUPPORTED}.
882 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800883 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800884 @Deprecated
885 public static long getUidUdpTxBytes(int uid) {
886 return UNSUPPORTED;
887 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800888
889 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800890 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800891 * transport layer statistics are no longer available, and will
892 * always return {@link #UNSUPPORTED}.
893 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800894 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800895 @Deprecated
896 public static long getUidUdpRxBytes(int uid) {
897 return UNSUPPORTED;
898 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800899
900 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800901 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800902 * transport layer statistics are no longer available, and will
903 * always return {@link #UNSUPPORTED}.
904 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800905 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800906 @Deprecated
907 public static long getUidTcpTxSegments(int uid) {
908 return UNSUPPORTED;
909 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800910
911 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800912 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800913 * transport layer statistics are no longer available, and will
914 * always return {@link #UNSUPPORTED}.
915 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800916 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800917 @Deprecated
918 public static long getUidTcpRxSegments(int uid) {
919 return UNSUPPORTED;
920 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800921
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800922 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800923 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800924 * transport layer statistics are no longer available, and will
925 * always return {@link #UNSUPPORTED}.
926 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800927 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800928 @Deprecated
929 public static long getUidUdpTxPackets(int uid) {
930 return UNSUPPORTED;
931 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800932
933 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800934 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800935 * transport layer statistics are no longer available, and will
936 * always return {@link #UNSUPPORTED}.
937 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800938 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800939 @Deprecated
940 public static long getUidUdpRxPackets(int uid) {
941 return UNSUPPORTED;
942 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700943
944 /**
945 * Return detailed {@link NetworkStats} for the current UID. Requires no
946 * special permission.
947 */
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700948 private static NetworkStats getDataLayerSnapshotForUid(Context context) {
Jeff Sharkeydddace72013-03-26 13:46:05 -0700949 // TODO: take snapshot locally, since proc file is now visible
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700950 final int uid = android.os.Process.myUid();
951 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700952 return getStatsService().getDataLayerSnapshotForUid(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700953 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700954 throw e.rethrowFromSystemServer();
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700955 }
956 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700957
958 /**
959 * Return set of any ifaces associated with mobile networks since boot.
960 * Interfaces are never removed from this list, so counters should always be
961 * monotonic.
962 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100963 @UnsupportedAppUsage
Jeff Sharkey234766a2012-04-10 19:48:07 -0700964 private static String[] getMobileIfaces() {
965 try {
966 return getStatsService().getMobileIfaces();
967 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700968 throw e.rethrowFromSystemServer();
Jeff Sharkey234766a2012-04-10 19:48:07 -0700969 }
970 }
971
972 // NOTE: keep these in sync with android_net_TrafficStats.cpp
973 private static final int TYPE_RX_BYTES = 0;
974 private static final int TYPE_RX_PACKETS = 1;
975 private static final int TYPE_TX_BYTES = 2;
976 private static final int TYPE_TX_PACKETS = 3;
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800977 private static final int TYPE_TCP_RX_PACKETS = 4;
978 private static final int TYPE_TCP_TX_PACKETS = 5;
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800979}