blob: d8feb88f0fe46affb1a252cc446650b038c2222b [file] [log] [blame]
Ken Shirriffae521152009-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
Aaron Huangb0db4fd2019-09-27 22:31:22 +080019import android.annotation.NonNull;
Jeff Sharkeyf5c1f302017-11-29 11:18:23 -070020import android.annotation.SuppressLint;
Christopher Tate5a1878d2014-08-06 14:19:56 -070021import android.annotation.SystemApi;
Benedict Wong8231eaf2017-12-03 19:42:36 -080022import android.annotation.TestApi;
Jeff Sharkey92790062011-06-24 17:05:24 -070023import android.app.DownloadManager;
24import android.app.backup.BackupManager;
Jeff Sharkey27c8a072016-03-09 16:40:15 -070025import android.app.usage.NetworkStatsManager;
Artur Satayevaf1eb472019-12-10 17:47:52 +000026import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey33936ac2011-05-17 14:55:15 -070027import android.content.Context;
Jeff Sharkey92790062011-06-24 17:05:24 -070028import android.media.MediaPlayer;
Chalard Jean4722cf12019-04-09 15:46:21 +090029import android.os.Build;
Jeff Sharkey33936ac2011-05-17 14:55:15 -070030import android.os.RemoteException;
31import android.os.ServiceManager;
32
Jesse Wilsonb05f41c2011-06-28 19:06:31 -070033import com.android.server.NetworkManagementSocketTagger;
Ken Shirriffae521152009-12-07 15:56:05 -080034
Jesse Wilsonb05f41c2011-06-28 19:06:31 -070035import dalvik.system.SocketTagger;
Jeff Sharkey7407e092011-07-19 23:47:12 -070036
Jeff Sharkeyf5c1f302017-11-29 11:18:23 -070037import java.io.FileDescriptor;
38import java.io.IOException;
Jeff Sharkey7b124cb2015-12-04 15:21:52 -070039import java.net.DatagramSocket;
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -070040import java.net.Socket;
41import java.net.SocketException;
Ken Shirriffae521152009-12-07 15:56:05 -080042
43/**
Jeff Sharkey27c8a072016-03-09 16:40:15 -070044 * Class that provides network traffic statistics. These statistics include
Dan Egnoreaeb7022010-04-07 17:30:50 -070045 * bytes transmitted and received and network packets transmitted and received,
46 * over all interfaces, over the mobile interface, and on a per-UID basis.
Ken Shirriffae521152009-12-07 15:56:05 -080047 * <p>
Jeff Sharkey27c8a072016-03-09 16:40:15 -070048 * These statistics may not be available on all platforms. If the statistics are
49 * not supported by this device, {@link #UNSUPPORTED} will be returned.
50 * <p>
51 * Note that the statistics returned by this class reset and start from zero
52 * after every reboot. To access more robust historical network statistics data,
53 * use {@link NetworkStatsManager} instead.
Ken Shirriffae521152009-12-07 15:56:05 -080054 */
55public class TrafficStats {
56 /**
57 * The return value to indicate that the device does not support the statistic.
58 */
59 public final static int UNSUPPORTED = -1;
60
Junyu Lai989b2122021-12-27 13:56:59 +000061 /** @hide @deprecated use {@code DataUnit} instead to clarify SI-vs-IEC */
Jeff Sharkey69f6f062018-01-07 16:47:31 -070062 @Deprecated
Jeff Sharkey94a315c2012-02-03 14:50:07 -080063 public static final long KB_IN_BYTES = 1024;
Junyu Lai989b2122021-12-27 13:56:59 +000064 /** @hide @deprecated use {@code DataUnit} instead to clarify SI-vs-IEC */
Jeff Sharkey69f6f062018-01-07 16:47:31 -070065 @Deprecated
Jeff Sharkey94a315c2012-02-03 14:50:07 -080066 public static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
Junyu Lai989b2122021-12-27 13:56:59 +000067 /** @hide @deprecated use {@code DataUnit} instead to clarify SI-vs-IEC */
Jeff Sharkey69f6f062018-01-07 16:47:31 -070068 @Deprecated
Jeff Sharkey94a315c2012-02-03 14:50:07 -080069 public static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
Junyu Lai989b2122021-12-27 13:56:59 +000070 /** @hide @deprecated use {@code DataUnit} instead to clarify SI-vs-IEC */
Jeff Sharkey69f6f062018-01-07 16:47:31 -070071 @Deprecated
Jeff Sharkey6f2ba5e2015-03-18 11:27:19 -070072 public static final long TB_IN_BYTES = GB_IN_BYTES * 1024;
Junyu Lai989b2122021-12-27 13:56:59 +000073 /** @hide @deprecated use {@code DataUnit} instead to clarify SI-vs-IEC */
Jeff Sharkey69f6f062018-01-07 16:47:31 -070074 @Deprecated
Jeff Sharkey88069072015-06-15 21:09:10 -070075 public static final long PB_IN_BYTES = TB_IN_BYTES * 1024;
Jeff Sharkey94a315c2012-02-03 14:50:07 -080076
Jeff Sharkey1a3eb882011-05-28 20:56:34 -070077 /**
Jeff Sharkeyc04cda62011-06-19 01:08:12 -070078 * Special UID value used when collecting {@link NetworkStatsHistory} for
79 * removed applications.
80 *
81 * @hide
82 */
83 public static final int UID_REMOVED = -4;
84
85 /**
Jeff Sharkey987f5ff2011-09-16 01:52:49 -070086 * Special UID value used when collecting {@link NetworkStatsHistory} for
87 * tethering traffic.
88 *
89 * @hide
90 */
junyulai10a00a22019-11-15 17:15:01 +080091 public static final int UID_TETHERING = NetworkStats.UID_TETHERING;
Jeff Sharkey987f5ff2011-09-16 01:52:49 -070092
93 /**
Chalard Jean65c39682019-04-09 11:16:56 +090094 * Tag values in this range are reserved for the network stack. The network stack is
95 * running as UID {@link android.os.Process.NETWORK_STACK_UID} when in the mainline
96 * module separate process, and as the system UID otherwise.
97 */
98 /** @hide */
99 @SystemApi
100 public static final int TAG_NETWORK_STACK_RANGE_START = 0xFFFFFD00;
101 /** @hide */
102 @SystemApi
103 public static final int TAG_NETWORK_STACK_RANGE_END = 0xFFFFFEFF;
104
105 /**
106 * Tags between 0xFFFFFF00 and 0xFFFFFFFF are reserved and used internally by system services
107 * like DownloadManager when performing traffic on behalf of an application.
108 */
109 // Please note there is no enforcement of these constants, so do not rely on them to
110 // determine that the caller is a system caller.
111 /** @hide */
112 @SystemApi
113 public static final int TAG_SYSTEM_IMPERSONATION_RANGE_START = 0xFFFFFF00;
114 /** @hide */
115 @SystemApi
116 public static final int TAG_SYSTEM_IMPERSONATION_RANGE_END = 0xFFFFFF0F;
117
118 /**
119 * Tag values between these ranges are reserved for the network stack to do traffic
120 * on behalf of applications. It is a subrange of the range above.
121 */
122 /** @hide */
123 @SystemApi
124 public static final int TAG_NETWORK_STACK_IMPERSONATION_RANGE_START = 0xFFFFFF80;
125 /** @hide */
126 @SystemApi
127 public static final int TAG_NETWORK_STACK_IMPERSONATION_RANGE_END = 0xFFFFFF8F;
128
129 /**
Jeff Sharkey92790062011-06-24 17:05:24 -0700130 * Default tag value for {@link DownloadManager} traffic.
131 *
132 * @hide
133 */
Jeff Sharkey987f5ff2011-09-16 01:52:49 -0700134 public static final int TAG_SYSTEM_DOWNLOAD = 0xFFFFFF01;
Jeff Sharkey92790062011-06-24 17:05:24 -0700135
136 /**
137 * Default tag value for {@link MediaPlayer} traffic.
138 *
139 * @hide
140 */
Jeff Sharkey987f5ff2011-09-16 01:52:49 -0700141 public static final int TAG_SYSTEM_MEDIA = 0xFFFFFF02;
Jeff Sharkey92790062011-06-24 17:05:24 -0700142
143 /**
Christopher Tatec1c71fd2015-11-10 10:49:20 -0800144 * Default tag value for {@link BackupManager} backup traffic; that is,
145 * traffic from the device to the storage backend.
Jeff Sharkey92790062011-06-24 17:05:24 -0700146 *
147 * @hide
148 */
Jeff Sharkey987f5ff2011-09-16 01:52:49 -0700149 public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03;
Jeff Sharkey92790062011-06-24 17:05:24 -0700150
Christopher Tatec1c71fd2015-11-10 10:49:20 -0800151 /**
152 * Default tag value for {@link BackupManager} restore traffic; that is,
153 * app data retrieved from the storage backend at install time.
154 *
155 * @hide
156 */
157 public static final int TAG_SYSTEM_RESTORE = 0xFFFFFF04;
158
Jeff Sharkey66dca172016-11-21 12:14:50 -0700159 /**
Jeff Sharkey63ee5282017-08-11 15:04:12 -0600160 * Default tag value for code (typically APKs) downloaded by an app store on
161 * behalf of the app, such as updates.
Jeff Sharkey66dca172016-11-21 12:14:50 -0700162 *
163 * @hide
164 */
Jeff Sharkey63ee5282017-08-11 15:04:12 -0600165 public static final int TAG_SYSTEM_APP = 0xFFFFFF05;
Jeff Sharkey38fd65b2017-06-26 19:50:41 -0600166
Chalard Jean65c39682019-04-09 11:16:56 +0900167 // TODO : remove this constant when Wifi code is updated
Jeff Sharkey38fd65b2017-06-26 19:50:41 -0600168 /** @hide */
Jeff Sharkey38fd65b2017-06-26 19:50:41 -0600169 public static final int TAG_SYSTEM_PROBE = 0xFFFFFF42;
Jeff Sharkey66dca172016-11-21 12:14:50 -0700170
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700171 private static INetworkStatsService sStatsService;
172
Chalard Jean4722cf12019-04-09 15:46:21 +0900173 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700174 private synchronized static INetworkStatsService getStatsService() {
175 if (sStatsService == null) {
176 sStatsService = INetworkStatsService.Stub.asInterface(
177 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
178 }
179 return sStatsService;
180 }
181
Jeff Sharkey92790062011-06-24 17:05:24 -0700182 /**
Jeff Sharkey33936ac2011-05-17 14:55:15 -0700183 * Snapshot of {@link NetworkStats} when the currently active profiling
184 * session started, or {@code null} if no session active.
185 *
186 * @see #startDataProfiling(Context)
187 * @see #stopDataProfiling(Context)
188 */
189 private static NetworkStats sActiveProfilingStart;
190
191 private static Object sProfilingLock = new Object();
192
Benedict Wong8231eaf2017-12-03 19:42:36 -0800193 private static final String LOOPBACK_IFACE = "lo";
194
Jeff Sharkey33936ac2011-05-17 14:55:15 -0700195 /**
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700196 * Set active tag to use when accounting {@link Socket} traffic originating
197 * from the current thread. Only one active tag per thread is supported.
198 * <p>
199 * Changes only take effect during subsequent calls to
200 * {@link #tagSocket(Socket)}.
Jeff Sharkey987f5ff2011-09-16 01:52:49 -0700201 * <p>
202 * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
203 * used internally by system services like {@link DownloadManager} when
204 * performing traffic on behalf of an application.
Jeff Sharkey39fb9cd2013-03-26 13:46:05 -0700205 *
206 * @see #clearThreadStatsTag()
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700207 */
Jeff Sharkey92790062011-06-24 17:05:24 -0700208 public static void setThreadStatsTag(int tag) {
Jesse Wilsonb05f41c2011-06-28 19:06:31 -0700209 NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700210 }
211
Jeff Sharkey92790062011-06-24 17:05:24 -0700212 /**
Jeff Sharkeyf7591592016-03-22 10:20:12 -0600213 * Set active tag to use when accounting {@link Socket} traffic originating
Jeff Sharkey289eac12017-01-19 11:55:54 -0700214 * from the current thread. Only one active tag per thread is supported.
215 * <p>
216 * Changes only take effect during subsequent calls to
217 * {@link #tagSocket(Socket)}.
218 * <p>
219 * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
220 * used internally by system services like {@link DownloadManager} when
221 * performing traffic on behalf of an application.
222 *
223 * @return the current tag for the calling thread, which can be used to
224 * restore any existing values after a nested operation is finished
225 */
226 public static int getAndSetThreadStatsTag(int tag) {
227 return NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
228 }
229
230 /**
231 * Set active tag to use when accounting {@link Socket} traffic originating
Jeff Sharkeyf7591592016-03-22 10:20:12 -0600232 * from the current thread. The tag used internally is well-defined to
233 * distinguish all backup-related traffic.
234 *
Christopher Tate5a1878d2014-08-06 14:19:56 -0700235 * @hide
236 */
237 @SystemApi
238 public static void setThreadStatsTagBackup() {
239 setThreadStatsTag(TAG_SYSTEM_BACKUP);
240 }
241
242 /**
Jeff Sharkeyf7591592016-03-22 10:20:12 -0600243 * Set active tag to use when accounting {@link Socket} traffic originating
244 * from the current thread. The tag used internally is well-defined to
245 * distinguish all restore-related traffic.
246 *
Christopher Tatec1c71fd2015-11-10 10:49:20 -0800247 * @hide
248 */
249 @SystemApi
250 public static void setThreadStatsTagRestore() {
251 setThreadStatsTag(TAG_SYSTEM_RESTORE);
252 }
253
254 /**
Jeff Sharkey38fd65b2017-06-26 19:50:41 -0600255 * Set active tag to use when accounting {@link Socket} traffic originating
256 * from the current thread. The tag used internally is well-defined to
Jeff Sharkey63ee5282017-08-11 15:04:12 -0600257 * distinguish all code (typically APKs) downloaded by an app store on
258 * behalf of the app, such as updates.
Jeff Sharkey38fd65b2017-06-26 19:50:41 -0600259 *
260 * @hide
261 */
262 @SystemApi
Jeff Sharkey63ee5282017-08-11 15:04:12 -0600263 public static void setThreadStatsTagApp() {
264 setThreadStatsTag(TAG_SYSTEM_APP);
Jeff Sharkey38fd65b2017-06-26 19:50:41 -0600265 }
266
267 /**
Alon Albert2c295f02011-07-19 11:16:09 +0300268 * Get the active tag used when accounting {@link Socket} traffic originating
269 * from the current thread. Only one active tag per thread is supported.
270 * {@link #tagSocket(Socket)}.
Jeff Sharkey39fb9cd2013-03-26 13:46:05 -0700271 *
272 * @see #setThreadStatsTag(int)
Alon Albert2c295f02011-07-19 11:16:09 +0300273 */
274 public static int getThreadStatsTag() {
275 return NetworkManagementSocketTagger.getThreadSocketStatsTag();
276 }
277
Jeff Sharkey39fb9cd2013-03-26 13:46:05 -0700278 /**
279 * Clear any active tag set to account {@link Socket} traffic originating
280 * from the current thread.
281 *
282 * @see #setThreadStatsTag(int)
283 */
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700284 public static void clearThreadStatsTag() {
Jesse Wilsonb05f41c2011-06-28 19:06:31 -0700285 NetworkManagementSocketTagger.setThreadSocketStatsTag(-1);
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700286 }
287
288 /**
289 * Set specific UID to use when accounting {@link Socket} traffic
290 * originating from the current thread. Designed for use when performing an
Jeff Sharkey8d66e652018-03-26 13:11:33 -0600291 * operation on behalf of another application, or when another application
292 * is performing operations on your behalf.
293 * <p>
294 * Any app can <em>accept</em> blame for traffic performed on a socket
295 * originally created by another app by calling this method with the
296 * {@link android.system.Os#getuid()} value. However, only apps holding the
297 * {@code android.Manifest.permission#UPDATE_DEVICE_STATS} permission may
298 * <em>assign</em> blame to another UIDs.
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700299 * <p>
300 * Changes only take effect during subsequent calls to
301 * {@link #tagSocket(Socket)}.
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700302 */
Aurimas Liutikas0caeaa32020-11-12 18:29:11 -0800303 @SuppressLint("RequiresPermission")
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700304 public static void setThreadStatsUid(int uid) {
Jesse Wilsonb05f41c2011-06-28 19:06:31 -0700305 NetworkManagementSocketTagger.setThreadSocketStatsUid(uid);
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700306 }
307
Jeff Sharkeyf7591592016-03-22 10:20:12 -0600308 /**
Jeff Sharkey8d66e652018-03-26 13:11:33 -0600309 * Get the active UID used when accounting {@link Socket} traffic originating
310 * from the current thread. Only one active tag per thread is supported.
311 * {@link #tagSocket(Socket)}.
312 *
313 * @see #setThreadStatsUid(int)
314 */
315 public static int getThreadStatsUid() {
316 return NetworkManagementSocketTagger.getThreadSocketStatsUid();
317 }
318
319 /**
Jeff Sharkeyf5c1f302017-11-29 11:18:23 -0700320 * Set specific UID to use when accounting {@link Socket} traffic
321 * originating from the current thread as the calling UID. Designed for use
322 * when another application is performing operations on your behalf.
323 * <p>
324 * Changes only take effect during subsequent calls to
325 * {@link #tagSocket(Socket)}.
Jeff Sharkey8d66e652018-03-26 13:11:33 -0600326 *
327 * @removed
328 * @deprecated use {@link #setThreadStatsUid(int)} instead.
Jeff Sharkeyf5c1f302017-11-29 11:18:23 -0700329 */
Jeff Sharkey8d66e652018-03-26 13:11:33 -0600330 @Deprecated
Jeff Sharkeyf5c1f302017-11-29 11:18:23 -0700331 public static void setThreadStatsUidSelf() {
332 setThreadStatsUid(android.os.Process.myUid());
333 }
334
335 /**
Jeff Sharkeyf7591592016-03-22 10:20:12 -0600336 * Clear any active UID set to account {@link Socket} traffic originating
337 * from the current thread.
338 *
339 * @see #setThreadStatsUid(int)
Jeff Sharkeyf7591592016-03-22 10:20:12 -0600340 */
Aurimas Liutikas0caeaa32020-11-12 18:29:11 -0800341 @SuppressLint("RequiresPermission")
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700342 public static void clearThreadStatsUid() {
Jesse Wilsonb05f41c2011-06-28 19:06:31 -0700343 NetworkManagementSocketTagger.setThreadSocketStatsUid(-1);
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700344 }
345
346 /**
347 * Tag the given {@link Socket} with any statistics parameters active for
348 * the current thread. Subsequent calls always replace any existing
349 * parameters. When finished, call {@link #untagSocket(Socket)} to remove
350 * statistics parameters.
351 *
Jeff Sharkey92790062011-06-24 17:05:24 -0700352 * @see #setThreadStatsTag(int)
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700353 */
354 public static void tagSocket(Socket socket) throws SocketException {
Jesse Wilsonb05f41c2011-06-28 19:06:31 -0700355 SocketTagger.get().tag(socket);
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700356 }
357
358 /**
359 * Remove any statistics parameters from the given {@link Socket}.
koprivab02b6402018-07-23 18:19:39 -0700360 * <p>
361 * In Android 8.1 (API level 27) and lower, a socket is automatically
362 * untagged when it's sent to another process using binder IPC with a
363 * {@code ParcelFileDescriptor} container. In Android 9.0 (API level 28)
364 * and higher, the socket tag is kept when the socket is sent to another
365 * process using binder IPC. You can mimic the previous behavior by
366 * calling {@code untagSocket()} before sending the socket to another
367 * process.
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700368 */
369 public static void untagSocket(Socket socket) throws SocketException {
Jesse Wilsonb05f41c2011-06-28 19:06:31 -0700370 SocketTagger.get().untag(socket);
Jeff Sharkeyb82cd3d2011-04-21 18:45:43 -0700371 }
372
373 /**
Jeff Sharkey7b124cb2015-12-04 15:21:52 -0700374 * Tag the given {@link DatagramSocket} with any statistics parameters
375 * active for the current thread. Subsequent calls always replace any
376 * existing parameters. When finished, call
377 * {@link #untagDatagramSocket(DatagramSocket)} to remove statistics
378 * parameters.
379 *
380 * @see #setThreadStatsTag(int)
Jeff Sharkey7b124cb2015-12-04 15:21:52 -0700381 */
382 public static void tagDatagramSocket(DatagramSocket socket) throws SocketException {
383 SocketTagger.get().tag(socket);
384 }
385
386 /**
387 * Remove any statistics parameters from the given {@link DatagramSocket}.
388 */
389 public static void untagDatagramSocket(DatagramSocket socket) throws SocketException {
390 SocketTagger.get().untag(socket);
391 }
392
393 /**
Jeff Sharkeyf5c1f302017-11-29 11:18:23 -0700394 * Tag the given {@link FileDescriptor} socket with any statistics
395 * parameters active for the current thread. Subsequent calls always replace
396 * any existing parameters. When finished, call
397 * {@link #untagFileDescriptor(FileDescriptor)} to remove statistics
398 * parameters.
399 *
400 * @see #setThreadStatsTag(int)
401 */
402 public static void tagFileDescriptor(FileDescriptor fd) throws IOException {
403 SocketTagger.get().tag(fd);
404 }
405
406 /**
407 * Remove any statistics parameters from the given {@link FileDescriptor}
408 * socket.
409 */
410 public static void untagFileDescriptor(FileDescriptor fd) throws IOException {
411 SocketTagger.get().untag(fd);
412 }
413
414 /**
Jeff Sharkey33936ac2011-05-17 14:55:15 -0700415 * Start profiling data usage for current UID. Only one profiling session
416 * can be active at a time.
417 *
418 * @hide
419 */
420 public static void startDataProfiling(Context context) {
421 synchronized (sProfilingLock) {
422 if (sActiveProfilingStart != null) {
423 throw new IllegalStateException("already profiling data");
424 }
425
426 // take snapshot in time; we calculate delta later
Jeff Sharkey7407e092011-07-19 23:47:12 -0700427 sActiveProfilingStart = getDataLayerSnapshotForUid(context);
Jeff Sharkey33936ac2011-05-17 14:55:15 -0700428 }
429 }
430
431 /**
432 * Stop profiling data usage for current UID.
433 *
434 * @return Detailed {@link NetworkStats} of data that occurred since last
435 * {@link #startDataProfiling(Context)} call.
436 * @hide
437 */
438 public static NetworkStats stopDataProfiling(Context context) {
439 synchronized (sProfilingLock) {
440 if (sActiveProfilingStart == null) {
441 throw new IllegalStateException("not profiling data");
442 }
443
Jeff Sharkeyef7bded2012-01-10 17:24:44 -0800444 // subtract starting values and return delta
445 final NetworkStats profilingStop = getDataLayerSnapshotForUid(context);
446 final NetworkStats profilingDelta = NetworkStats.subtract(
Jeff Sharkeybfe82682012-01-11 18:38:16 -0800447 profilingStop, sActiveProfilingStart, null, null);
Jeff Sharkeyef7bded2012-01-10 17:24:44 -0800448 sActiveProfilingStart = null;
449 return profilingDelta;
Jeff Sharkey33936ac2011-05-17 14:55:15 -0700450 }
451 }
452
453 /**
Jeff Sharkey60ca0232011-08-24 15:42:09 -0700454 * Increment count of network operations performed under the accounting tag
455 * currently active on the calling thread. This can be used to derive
456 * bytes-per-operation.
457 *
458 * @param operationCount Number of operations to increment count by.
459 */
460 public static void incrementOperationCount(int operationCount) {
461 final int tag = getThreadStatsTag();
462 incrementOperationCount(tag, operationCount);
463 }
464
465 /**
Jeff Sharkey7407e092011-07-19 23:47:12 -0700466 * Increment count of network operations performed under the given
467 * accounting tag. This can be used to derive bytes-per-operation.
468 *
469 * @param tag Accounting tag used in {@link #setThreadStatsTag(int)}.
470 * @param operationCount Number of operations to increment count by.
471 */
472 public static void incrementOperationCount(int tag, int operationCount) {
Jeff Sharkey7407e092011-07-19 23:47:12 -0700473 final int uid = android.os.Process.myUid();
474 try {
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700475 getStatsService().incrementOperationCount(uid, tag, operationCount);
Jeff Sharkey7407e092011-07-19 23:47:12 -0700476 } catch (RemoteException e) {
Jeff Sharkeybc08d072016-02-26 13:03:01 -0700477 throw e.rethrowFromSystemServer();
Jeff Sharkey7407e092011-07-19 23:47:12 -0700478 }
479 }
480
Jeff Sharkey920d9042012-04-06 11:12:08 -0700481 /** {@hide} */
482 public static void closeQuietly(INetworkStatsSession session) {
483 // TODO: move to NetworkStatsService once it exists
484 if (session != null) {
485 try {
486 session.close();
487 } catch (RuntimeException rethrown) {
488 throw rethrown;
489 } catch (Exception ignored) {
490 }
491 }
492 }
493
Chenbo Feng7f880c92018-01-25 11:43:52 -0800494 private static long addIfSupported(long stat) {
495 return (stat == UNSUPPORTED) ? 0 : stat;
496 }
497
Jeff Sharkey7407e092011-07-19 23:47:12 -0700498 /**
Jeff Sharkey39fb9cd2013-03-26 13:46:05 -0700499 * Return number of packets transmitted across mobile networks since device
500 * boot. Counts packets across all mobile network interfaces, and always
501 * increases monotonically since device boot. Statistics are measured at the
502 * network layer, so they include both TCP and UDP usage.
503 * <p>
504 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
505 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirriffae521152009-12-07 15:56:05 -0800506 */
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700507 public static long getMobileTxPackets() {
508 long total = 0;
509 for (String iface : getMobileIfaces()) {
Chenbo Feng7f880c92018-01-25 11:43:52 -0800510 total += addIfSupported(getTxPackets(iface));
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700511 }
512 return total;
513 }
Ken Shirriffae521152009-12-07 15:56:05 -0800514
515 /**
Jeff Sharkey39fb9cd2013-03-26 13:46:05 -0700516 * Return number of packets received across mobile networks since device
517 * boot. Counts packets across all mobile network interfaces, and always
518 * increases monotonically since device boot. Statistics are measured at the
519 * network layer, so they include both TCP and UDP usage.
520 * <p>
521 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
522 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirriffae521152009-12-07 15:56:05 -0800523 */
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700524 public static long getMobileRxPackets() {
525 long total = 0;
526 for (String iface : getMobileIfaces()) {
Chenbo Feng7f880c92018-01-25 11:43:52 -0800527 total += addIfSupported(getRxPackets(iface));
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700528 }
529 return total;
530 }
Ken Shirriffae521152009-12-07 15:56:05 -0800531
532 /**
Jeff Sharkey39fb9cd2013-03-26 13:46:05 -0700533 * Return number of bytes transmitted across mobile networks since device
534 * boot. Counts packets across all mobile network interfaces, and always
535 * increases monotonically since device boot. Statistics are measured at the
536 * network layer, so they include both TCP and UDP usage.
537 * <p>
538 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
539 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirriffae521152009-12-07 15:56:05 -0800540 */
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700541 public static long getMobileTxBytes() {
542 long total = 0;
543 for (String iface : getMobileIfaces()) {
Chenbo Feng7f880c92018-01-25 11:43:52 -0800544 total += addIfSupported(getTxBytes(iface));
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700545 }
546 return total;
547 }
Ken Shirriffae521152009-12-07 15:56:05 -0800548
549 /**
Jeff Sharkey39fb9cd2013-03-26 13:46:05 -0700550 * Return number of bytes received across mobile networks since device boot.
551 * Counts packets across all mobile network interfaces, and always increases
552 * monotonically since device boot. Statistics are measured at the network
553 * layer, so they include both TCP and UDP usage.
554 * <p>
555 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
556 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirriffae521152009-12-07 15:56:05 -0800557 */
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700558 public static long getMobileRxBytes() {
559 long total = 0;
560 for (String iface : getMobileIfaces()) {
Chenbo Feng7f880c92018-01-25 11:43:52 -0800561 total += addIfSupported(getRxBytes(iface));
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700562 }
563 return total;
564 }
Ken Shirriffae521152009-12-07 15:56:05 -0800565
Jeff Sharkey9e766ac2013-02-05 21:32:33 -0800566 /** {@hide} */
Mathew Inwood5c74a0f2020-11-04 09:29:36 +0000567 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Jeff Sharkey9e766ac2013-02-05 21:32:33 -0800568 public static long getMobileTcpRxPackets() {
569 long total = 0;
570 for (String iface : getMobileIfaces()) {
Chenbo Feng65b99192017-11-14 17:54:17 -0800571 long stat = UNSUPPORTED;
572 try {
573 stat = getStatsService().getIfaceStats(iface, TYPE_TCP_RX_PACKETS);
574 } catch (RemoteException e) {
575 throw e.rethrowFromSystemServer();
576 }
Chenbo Feng7f880c92018-01-25 11:43:52 -0800577 total += addIfSupported(stat);
Jeff Sharkey9e766ac2013-02-05 21:32:33 -0800578 }
579 return total;
580 }
581
582 /** {@hide} */
Mathew Inwood5c74a0f2020-11-04 09:29:36 +0000583 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Jeff Sharkey9e766ac2013-02-05 21:32:33 -0800584 public static long getMobileTcpTxPackets() {
585 long total = 0;
586 for (String iface : getMobileIfaces()) {
Chenbo Feng65b99192017-11-14 17:54:17 -0800587 long stat = UNSUPPORTED;
588 try {
589 stat = getStatsService().getIfaceStats(iface, TYPE_TCP_TX_PACKETS);
590 } catch (RemoteException e) {
591 throw e.rethrowFromSystemServer();
592 }
Chenbo Feng7f880c92018-01-25 11:43:52 -0800593 total += addIfSupported(stat);
Jeff Sharkey9e766ac2013-02-05 21:32:33 -0800594 }
595 return total;
596 }
597
Aaron Huangb0db4fd2019-09-27 22:31:22 +0800598 /**
junyulai01f60b22020-09-29 14:19:24 +0800599 * Return the number of packets transmitted on the specified interface since the interface
600 * was created. Statistics are measured at the network layer, so both TCP and
Aaron Huangb0db4fd2019-09-27 22:31:22 +0800601 * UDP usage are included.
602 *
junyulai01f60b22020-09-29 14:19:24 +0800603 * Note that the returned values are partial statistics that do not count data from several
604 * sources and do not apply several adjustments that are necessary for correctness, such
605 * as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to
606 * determine whether traffic is being transferred on the specific interface but are not a
607 * substitute for the more accurate statistics provided by the {@link NetworkStatsManager}
608 * APIs.
609 *
Aaron Huangb0db4fd2019-09-27 22:31:22 +0800610 * @param iface The name of the interface.
611 * @return The number of transmitted packets.
612 */
613 public static long getTxPackets(@NonNull String iface) {
Chenbo Feng65b99192017-11-14 17:54:17 -0800614 try {
615 return getStatsService().getIfaceStats(iface, TYPE_TX_PACKETS);
616 } catch (RemoteException e) {
617 throw e.rethrowFromSystemServer();
618 }
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700619 }
Irfan Sheriff042f4e12011-02-15 19:30:27 -0800620
Aaron Huangb0db4fd2019-09-27 22:31:22 +0800621 /**
junyulai01f60b22020-09-29 14:19:24 +0800622 * Return the number of packets received on the specified interface since the interface was
623 * created. Statistics are measured at the network layer, so both TCP
Aaron Huangb0db4fd2019-09-27 22:31:22 +0800624 * and UDP usage are included.
625 *
junyulai01f60b22020-09-29 14:19:24 +0800626 * Note that the returned values are partial statistics that do not count data from several
627 * sources and do not apply several adjustments that are necessary for correctness, such
628 * as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to
629 * determine whether traffic is being transferred on the specific interface but are not a
630 * substitute for the more accurate statistics provided by the {@link NetworkStatsManager}
631 * APIs.
632 *
Aaron Huangb0db4fd2019-09-27 22:31:22 +0800633 * @param iface The name of the interface.
634 * @return The number of received packets.
635 */
636 public static long getRxPackets(@NonNull String iface) {
Chenbo Feng65b99192017-11-14 17:54:17 -0800637 try {
638 return getStatsService().getIfaceStats(iface, TYPE_RX_PACKETS);
639 } catch (RemoteException e) {
640 throw e.rethrowFromSystemServer();
641 }
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700642 }
Irfan Sheriff042f4e12011-02-15 19:30:27 -0800643
junyulai01f60b22020-09-29 14:19:24 +0800644 /**
645 * Return the number of bytes transmitted on the specified interface since the interface
646 * was created. Statistics are measured at the network layer, so both TCP and
647 * UDP usage are included.
648 *
649 * Note that the returned values are partial statistics that do not count data from several
650 * sources and do not apply several adjustments that are necessary for correctness, such
651 * as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to
652 * determine whether traffic is being transferred on the specific interface but are not a
653 * substitute for the more accurate statistics provided by the {@link NetworkStatsManager}
654 * APIs.
655 *
656 * @param iface The name of the interface.
657 * @return The number of transmitted bytes.
658 */
659 public static long getTxBytes(@NonNull String iface) {
Chenbo Feng65b99192017-11-14 17:54:17 -0800660 try {
661 return getStatsService().getIfaceStats(iface, TYPE_TX_BYTES);
662 } catch (RemoteException e) {
663 throw e.rethrowFromSystemServer();
664 }
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700665 }
Irfan Sheriff042f4e12011-02-15 19:30:27 -0800666
junyulai01f60b22020-09-29 14:19:24 +0800667 /**
668 * Return the number of bytes received on the specified interface since the interface
669 * was created. Statistics are measured at the network layer, so both TCP
670 * and UDP usage are included.
671 *
672 * Note that the returned values are partial statistics that do not count data from several
673 * sources and do not apply several adjustments that are necessary for correctness, such
674 * as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to
675 * determine whether traffic is being transferred on the specific interface but are not a
676 * substitute for the more accurate statistics provided by the {@link NetworkStatsManager}
677 * APIs.
678 *
679 * @param iface The name of the interface.
680 * @return The number of received bytes.
681 */
682 public static long getRxBytes(@NonNull String iface) {
Chenbo Feng65b99192017-11-14 17:54:17 -0800683 try {
684 return getStatsService().getIfaceStats(iface, TYPE_RX_BYTES);
685 } catch (RemoteException e) {
686 throw e.rethrowFromSystemServer();
687 }
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700688 }
Irfan Sheriff042f4e12011-02-15 19:30:27 -0800689
Benedict Wong8231eaf2017-12-03 19:42:36 -0800690 /** {@hide} */
691 @TestApi
692 public static long getLoopbackTxPackets() {
Chenbo Feng65b99192017-11-14 17:54:17 -0800693 try {
694 return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_TX_PACKETS);
695 } catch (RemoteException e) {
696 throw e.rethrowFromSystemServer();
697 }
Benedict Wong8231eaf2017-12-03 19:42:36 -0800698 }
699
700 /** {@hide} */
701 @TestApi
702 public static long getLoopbackRxPackets() {
Chenbo Feng65b99192017-11-14 17:54:17 -0800703 try {
704 return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_RX_PACKETS);
705 } catch (RemoteException e) {
706 throw e.rethrowFromSystemServer();
707 }
Benedict Wong8231eaf2017-12-03 19:42:36 -0800708 }
709
710 /** {@hide} */
711 @TestApi
712 public static long getLoopbackTxBytes() {
Chenbo Feng65b99192017-11-14 17:54:17 -0800713 try {
714 return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_TX_BYTES);
715 } catch (RemoteException e) {
716 throw e.rethrowFromSystemServer();
717 }
Benedict Wong8231eaf2017-12-03 19:42:36 -0800718 }
719
720 /** {@hide} */
721 @TestApi
722 public static long getLoopbackRxBytes() {
Chenbo Feng65b99192017-11-14 17:54:17 -0800723 try {
724 return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_RX_BYTES);
725 } catch (RemoteException e) {
726 throw e.rethrowFromSystemServer();
727 }
Benedict Wong8231eaf2017-12-03 19:42:36 -0800728 }
729
Irfan Sheriff042f4e12011-02-15 19:30:27 -0800730 /**
Jeff Sharkey39fb9cd2013-03-26 13:46:05 -0700731 * Return number of packets transmitted since device boot. Counts packets
732 * across all network interfaces, and always increases monotonically since
733 * device boot. Statistics are measured at the network layer, so they
734 * include both TCP and UDP usage.
735 * <p>
736 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
737 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirriffae521152009-12-07 15:56:05 -0800738 */
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700739 public static long getTotalTxPackets() {
Chenbo Feng65b99192017-11-14 17:54:17 -0800740 try {
741 return getStatsService().getTotalStats(TYPE_TX_PACKETS);
742 } catch (RemoteException e) {
743 throw e.rethrowFromSystemServer();
744 }
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700745 }
Ken Shirriffae521152009-12-07 15:56:05 -0800746
747 /**
Jeff Sharkey39fb9cd2013-03-26 13:46:05 -0700748 * Return number of packets received since device boot. Counts packets
749 * across all network interfaces, and always increases monotonically since
750 * device boot. Statistics are measured at the network layer, so they
751 * include both TCP and UDP usage.
752 * <p>
753 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
754 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirriffae521152009-12-07 15:56:05 -0800755 */
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700756 public static long getTotalRxPackets() {
Chenbo Feng65b99192017-11-14 17:54:17 -0800757 try {
758 return getStatsService().getTotalStats(TYPE_RX_PACKETS);
759 } catch (RemoteException e) {
760 throw e.rethrowFromSystemServer();
761 }
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700762 }
Ken Shirriffae521152009-12-07 15:56:05 -0800763
764 /**
Jeff Sharkey39fb9cd2013-03-26 13:46:05 -0700765 * Return number of bytes transmitted since device boot. Counts packets
766 * across all network interfaces, and always increases monotonically since
767 * device boot. Statistics are measured at the network layer, so they
768 * include both TCP and UDP usage.
769 * <p>
770 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
771 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirriffae521152009-12-07 15:56:05 -0800772 */
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700773 public static long getTotalTxBytes() {
Chenbo Feng65b99192017-11-14 17:54:17 -0800774 try {
775 return getStatsService().getTotalStats(TYPE_TX_BYTES);
776 } catch (RemoteException e) {
777 throw e.rethrowFromSystemServer();
778 }
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700779 }
Ken Shirriffae521152009-12-07 15:56:05 -0800780
781 /**
Jeff Sharkey39fb9cd2013-03-26 13:46:05 -0700782 * Return number of bytes received since device boot. Counts packets across
783 * all network interfaces, and always increases monotonically since device
784 * boot. Statistics are measured at the network layer, so they include both
785 * TCP and UDP usage.
786 * <p>
787 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
788 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirriffae521152009-12-07 15:56:05 -0800789 */
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700790 public static long getTotalRxBytes() {
Chenbo Feng65b99192017-11-14 17:54:17 -0800791 try {
792 return getStatsService().getTotalStats(TYPE_RX_BYTES);
793 } catch (RemoteException e) {
794 throw e.rethrowFromSystemServer();
795 }
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700796 }
Ken Shirriffae521152009-12-07 15:56:05 -0800797
798 /**
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800799 * Return number of bytes transmitted by the given UID since device boot.
800 * Counts packets across all network interfaces, and always increases
801 * monotonically since device boot. Statistics are measured at the network
802 * layer, so they include both TCP and UDP usage.
803 * <p>
Jeff Sharkey27c8a072016-03-09 16:40:15 -0700804 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
805 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
806 * <p>
807 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
808 * report traffic statistics for the calling UID. It will return
809 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
810 * historical network statistics belonging to other UIDs, use
811 * {@link NetworkStatsManager}.
Ken Shirriffae521152009-12-07 15:56:05 -0800812 *
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800813 * @see android.os.Process#myUid()
814 * @see android.content.pm.ApplicationInfo#uid
Ken Shirriffae521152009-12-07 15:56:05 -0800815 */
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800816 public static long getUidTxBytes(int uid) {
Chenbo Fenge3cff5b2019-06-17 16:22:28 -0700817 try {
818 return getStatsService().getUidStats(uid, TYPE_TX_BYTES);
819 } catch (RemoteException e) {
820 throw e.rethrowFromSystemServer();
Jeff Sharkey27c8a072016-03-09 16:40:15 -0700821 }
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800822 }
Ken Shirriffae521152009-12-07 15:56:05 -0800823
824 /**
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800825 * Return number of bytes received by the given UID since device boot.
826 * Counts packets across all network interfaces, and always increases
827 * monotonically since device boot. Statistics are measured at the network
828 * layer, so they include both TCP and UDP usage.
829 * <p>
Dianne Hackborne9d72072013-02-25 15:55:37 -0800830 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800831 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Jeff Sharkey27c8a072016-03-09 16:40:15 -0700832 * <p>
833 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
834 * report traffic statistics for the calling UID. It will return
835 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
836 * historical network statistics belonging to other UIDs, use
837 * {@link NetworkStatsManager}.
Ken Shirriffae521152009-12-07 15:56:05 -0800838 *
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800839 * @see android.os.Process#myUid()
840 * @see android.content.pm.ApplicationInfo#uid
Ken Shirriffae521152009-12-07 15:56:05 -0800841 */
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800842 public static long getUidRxBytes(int uid) {
Chenbo Fenge3cff5b2019-06-17 16:22:28 -0700843 try {
844 return getStatsService().getUidStats(uid, TYPE_RX_BYTES);
845 } catch (RemoteException e) {
846 throw e.rethrowFromSystemServer();
Jeff Sharkey27c8a072016-03-09 16:40:15 -0700847 }
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800848 }
Ashish Sharmadf852d82011-01-27 15:52:38 -0800849
850 /**
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800851 * Return number of packets transmitted by the given UID since device boot.
852 * Counts packets across all network interfaces, and always increases
853 * monotonically since device boot. Statistics are measured at the network
854 * layer, so they include both TCP and UDP usage.
855 * <p>
Dianne Hackborne9d72072013-02-25 15:55:37 -0800856 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800857 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Jeff Sharkey27c8a072016-03-09 16:40:15 -0700858 * <p>
859 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
860 * report traffic statistics for the calling UID. It will return
861 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
862 * historical network statistics belonging to other UIDs, use
863 * {@link NetworkStatsManager}.
Ashish Sharmadf852d82011-01-27 15:52:38 -0800864 *
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800865 * @see android.os.Process#myUid()
866 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmadf852d82011-01-27 15:52:38 -0800867 */
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800868 public static long getUidTxPackets(int uid) {
Chenbo Fenge3cff5b2019-06-17 16:22:28 -0700869 try {
870 return getStatsService().getUidStats(uid, TYPE_TX_PACKETS);
871 } catch (RemoteException e) {
872 throw e.rethrowFromSystemServer();
Jeff Sharkey27c8a072016-03-09 16:40:15 -0700873 }
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800874 }
Ashish Sharmadf852d82011-01-27 15:52:38 -0800875
876 /**
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800877 * Return number of packets received by the given UID since device boot.
878 * Counts packets across all network interfaces, and always increases
879 * monotonically since device boot. Statistics are measured at the network
880 * layer, so they include both TCP and UDP usage.
881 * <p>
Dianne Hackborne9d72072013-02-25 15:55:37 -0800882 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800883 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Jeff Sharkey27c8a072016-03-09 16:40:15 -0700884 * <p>
885 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
886 * report traffic statistics for the calling UID. It will return
887 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
888 * historical network statistics belonging to other UIDs, use
889 * {@link NetworkStatsManager}.
Ashish Sharmadf852d82011-01-27 15:52:38 -0800890 *
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800891 * @see android.os.Process#myUid()
892 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmadf852d82011-01-27 15:52:38 -0800893 */
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800894 public static long getUidRxPackets(int uid) {
Chenbo Fenge3cff5b2019-06-17 16:22:28 -0700895 try {
896 return getStatsService().getUidStats(uid, TYPE_RX_PACKETS);
897 } catch (RemoteException e) {
898 throw e.rethrowFromSystemServer();
Jeff Sharkey27c8a072016-03-09 16:40:15 -0700899 }
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800900 }
Ashish Sharmadf852d82011-01-27 15:52:38 -0800901
902 /**
Dianne Hackborne9d72072013-02-25 15:55:37 -0800903 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800904 * transport layer statistics are no longer available, and will
905 * always return {@link #UNSUPPORTED}.
906 * @see #getUidTxBytes(int)
Ashish Sharmadf852d82011-01-27 15:52:38 -0800907 */
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800908 @Deprecated
909 public static long getUidTcpTxBytes(int uid) {
910 return UNSUPPORTED;
911 }
Ashish Sharmadf852d82011-01-27 15:52:38 -0800912
913 /**
Dianne Hackborne9d72072013-02-25 15:55:37 -0800914 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800915 * transport layer statistics are no longer available, and will
916 * always return {@link #UNSUPPORTED}.
917 * @see #getUidRxBytes(int)
Ashish Sharmadf852d82011-01-27 15:52:38 -0800918 */
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800919 @Deprecated
920 public static long getUidTcpRxBytes(int uid) {
921 return UNSUPPORTED;
922 }
Ashish Sharmadf852d82011-01-27 15:52:38 -0800923
924 /**
Dianne Hackborne9d72072013-02-25 15:55:37 -0800925 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800926 * transport layer statistics are no longer available, and will
927 * always return {@link #UNSUPPORTED}.
928 * @see #getUidTxBytes(int)
Ashish Sharmadf852d82011-01-27 15:52:38 -0800929 */
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800930 @Deprecated
931 public static long getUidUdpTxBytes(int uid) {
932 return UNSUPPORTED;
933 }
Ashish Sharmadf852d82011-01-27 15:52:38 -0800934
935 /**
Dianne Hackborne9d72072013-02-25 15:55:37 -0800936 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800937 * transport layer statistics are no longer available, and will
938 * always return {@link #UNSUPPORTED}.
939 * @see #getUidRxBytes(int)
Ashish Sharmadf852d82011-01-27 15:52:38 -0800940 */
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800941 @Deprecated
942 public static long getUidUdpRxBytes(int uid) {
943 return UNSUPPORTED;
944 }
Ashish Sharmadf852d82011-01-27 15:52:38 -0800945
946 /**
Dianne Hackborne9d72072013-02-25 15:55:37 -0800947 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800948 * transport layer statistics are no longer available, and will
949 * always return {@link #UNSUPPORTED}.
950 * @see #getUidTxPackets(int)
Ashish Sharmadf852d82011-01-27 15:52:38 -0800951 */
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800952 @Deprecated
953 public static long getUidTcpTxSegments(int uid) {
954 return UNSUPPORTED;
955 }
Ashish Sharmadf852d82011-01-27 15:52:38 -0800956
957 /**
Dianne Hackborne9d72072013-02-25 15:55:37 -0800958 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800959 * transport layer statistics are no longer available, and will
960 * always return {@link #UNSUPPORTED}.
961 * @see #getUidRxPackets(int)
Ashish Sharmadf852d82011-01-27 15:52:38 -0800962 */
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800963 @Deprecated
964 public static long getUidTcpRxSegments(int uid) {
965 return UNSUPPORTED;
966 }
Ashish Sharmadf852d82011-01-27 15:52:38 -0800967
Ashish Sharmadf852d82011-01-27 15:52:38 -0800968 /**
Dianne Hackborne9d72072013-02-25 15:55:37 -0800969 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800970 * transport layer statistics are no longer available, and will
971 * always return {@link #UNSUPPORTED}.
972 * @see #getUidTxPackets(int)
Ashish Sharmadf852d82011-01-27 15:52:38 -0800973 */
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800974 @Deprecated
975 public static long getUidUdpTxPackets(int uid) {
976 return UNSUPPORTED;
977 }
Ashish Sharmadf852d82011-01-27 15:52:38 -0800978
979 /**
Dianne Hackborne9d72072013-02-25 15:55:37 -0800980 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800981 * transport layer statistics are no longer available, and will
982 * always return {@link #UNSUPPORTED}.
983 * @see #getUidRxPackets(int)
Ashish Sharmadf852d82011-01-27 15:52:38 -0800984 */
Jeff Sharkey5fee2c92013-01-15 17:25:09 -0800985 @Deprecated
986 public static long getUidUdpRxPackets(int uid) {
987 return UNSUPPORTED;
988 }
Jeff Sharkey33936ac2011-05-17 14:55:15 -0700989
990 /**
991 * Return detailed {@link NetworkStats} for the current UID. Requires no
992 * special permission.
993 */
Jeff Sharkey7407e092011-07-19 23:47:12 -0700994 private static NetworkStats getDataLayerSnapshotForUid(Context context) {
Jeff Sharkey39fb9cd2013-03-26 13:46:05 -0700995 // TODO: take snapshot locally, since proc file is now visible
Jeff Sharkey33936ac2011-05-17 14:55:15 -0700996 final int uid = android.os.Process.myUid();
997 try {
Jeff Sharkey8308b9b2012-04-10 19:48:07 -0700998 return getStatsService().getDataLayerSnapshotForUid(uid);
Jeff Sharkey33936ac2011-05-17 14:55:15 -0700999 } catch (RemoteException e) {
Jeff Sharkeybc08d072016-02-26 13:03:01 -07001000 throw e.rethrowFromSystemServer();
Jeff Sharkey33936ac2011-05-17 14:55:15 -07001001 }
1002 }
Jeff Sharkey8308b9b2012-04-10 19:48:07 -07001003
1004 /**
1005 * Return set of any ifaces associated with mobile networks since boot.
1006 * Interfaces are never removed from this list, so counters should always be
1007 * monotonic.
1008 */
Chalard Jean4722cf12019-04-09 15:46:21 +09001009 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Jeff Sharkey8308b9b2012-04-10 19:48:07 -07001010 private static String[] getMobileIfaces() {
1011 try {
1012 return getStatsService().getMobileIfaces();
1013 } catch (RemoteException e) {
Jeff Sharkeybc08d072016-02-26 13:03:01 -07001014 throw e.rethrowFromSystemServer();
Jeff Sharkey8308b9b2012-04-10 19:48:07 -07001015 }
1016 }
1017
junyulai3841fbe2020-10-06 11:59:56 +08001018 // NOTE: keep these in sync with {@code com_android_server_net_NetworkStatsService.cpp}.
1019 /** {@hide} */
1020 public static final int TYPE_RX_BYTES = 0;
1021 /** {@hide} */
1022 public static final int TYPE_RX_PACKETS = 1;
1023 /** {@hide} */
1024 public static final int TYPE_TX_BYTES = 2;
1025 /** {@hide} */
1026 public static final int TYPE_TX_PACKETS = 3;
1027 /** {@hide} */
1028 public static final int TYPE_TCP_RX_PACKETS = 4;
1029 /** {@hide} */
1030 public static final int TYPE_TCP_TX_PACKETS = 5;
Ken Shirriffae521152009-12-07 15:56:05 -08001031}