blob: 8108cf08d5c366de5067bda79780c67a48e7a395 [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
Aaron Huang29667d22019-09-27 22:31:22 +080019import android.annotation.NonNull;
Jeff Sharkeya4239cf2017-11-29 11:18:23 -070020import android.annotation.SuppressLint;
Christopher Tatea2496de2014-08-06 14:19:56 -070021import android.annotation.SystemApi;
Benedict Wongbabe5d72017-12-03 19:42:36 -080022import android.annotation.TestApi;
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;
Artur Satayev33f92172019-12-10 17:47:52 +000026import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070027import android.content.Context;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070028import android.media.MediaPlayer;
Chalard Jean0bb53dbb2019-04-09 15:46:21 +090029import android.os.Build;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070030import android.os.RemoteException;
31import android.os.ServiceManager;
Jeff Sharkey9f2dc052018-01-07 16:47:31 -070032import android.util.DataUnit;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070033
Jesse Wilson8568db52011-06-28 19:06:31 -070034import com.android.server.NetworkManagementSocketTagger;
Ken Shirrifff7d0b012009-12-07 15:56:05 -080035
Jesse Wilson8568db52011-06-28 19:06:31 -070036import dalvik.system.SocketTagger;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070037
Jeff Sharkeya4239cf2017-11-29 11:18:23 -070038import java.io.FileDescriptor;
39import java.io.IOException;
Jeff Sharkeyf0d76332015-12-04 15:21:52 -070040import java.net.DatagramSocket;
Jeff Sharkey43be1742011-04-21 18:45:43 -070041import java.net.Socket;
42import java.net.SocketException;
Ken Shirrifff7d0b012009-12-07 15:56:05 -080043
44/**
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -070045 * Class that provides network traffic statistics. These statistics include
Dan Egnor2b4abcd2010-04-07 17:30:50 -070046 * bytes transmitted and received and network packets transmitted and received,
47 * over all interfaces, over the mobile interface, and on a per-UID basis.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080048 * <p>
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -070049 * These statistics may not be available on all platforms. If the statistics are
50 * not supported by this device, {@link #UNSUPPORTED} will be returned.
51 * <p>
52 * Note that the statistics returned by this class reset and start from zero
53 * after every reboot. To access more robust historical network statistics data,
54 * use {@link NetworkStatsManager} instead.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080055 */
56public class TrafficStats {
57 /**
58 * The return value to indicate that the device does not support the statistic.
59 */
60 public final static int UNSUPPORTED = -1;
61
Jeff Sharkey9f2dc052018-01-07 16:47:31 -070062 /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
63 @Deprecated
Jeff Sharkey241dde22012-02-03 14:50:07 -080064 public static final long KB_IN_BYTES = 1024;
Jeff Sharkey9f2dc052018-01-07 16:47:31 -070065 /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
66 @Deprecated
Jeff Sharkey241dde22012-02-03 14:50:07 -080067 public static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
Jeff Sharkey9f2dc052018-01-07 16:47:31 -070068 /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
69 @Deprecated
Jeff Sharkey241dde22012-02-03 14:50:07 -080070 public static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
Jeff Sharkey9f2dc052018-01-07 16:47:31 -070071 /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
72 @Deprecated
Jeff Sharkey48877892015-03-18 11:27:19 -070073 public static final long TB_IN_BYTES = GB_IN_BYTES * 1024;
Jeff Sharkey9f2dc052018-01-07 16:47:31 -070074 /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
75 @Deprecated
Jeff Sharkeyb521fea2015-06-15 21:09:10 -070076 public static final long PB_IN_BYTES = TB_IN_BYTES * 1024;
Jeff Sharkey241dde22012-02-03 14:50:07 -080077
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070078 /**
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070079 * Special UID value used when collecting {@link NetworkStatsHistory} for
80 * removed applications.
81 *
82 * @hide
83 */
84 public static final int UID_REMOVED = -4;
85
86 /**
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070087 * Special UID value used when collecting {@link NetworkStatsHistory} for
88 * tethering traffic.
89 *
90 * @hide
91 */
junyulaid27a1722019-11-15 17:15:01 +080092 public static final int UID_TETHERING = NetworkStats.UID_TETHERING;
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070093
94 /**
Chalard Jean896245a2019-04-09 11:16:56 +090095 * Tag values in this range are reserved for the network stack. The network stack is
96 * running as UID {@link android.os.Process.NETWORK_STACK_UID} when in the mainline
97 * module separate process, and as the system UID otherwise.
98 */
99 /** @hide */
100 @SystemApi
101 public static final int TAG_NETWORK_STACK_RANGE_START = 0xFFFFFD00;
102 /** @hide */
103 @SystemApi
104 public static final int TAG_NETWORK_STACK_RANGE_END = 0xFFFFFEFF;
105
106 /**
107 * Tags between 0xFFFFFF00 and 0xFFFFFFFF are reserved and used internally by system services
108 * like DownloadManager when performing traffic on behalf of an application.
109 */
110 // Please note there is no enforcement of these constants, so do not rely on them to
111 // determine that the caller is a system caller.
112 /** @hide */
113 @SystemApi
114 public static final int TAG_SYSTEM_IMPERSONATION_RANGE_START = 0xFFFFFF00;
115 /** @hide */
116 @SystemApi
117 public static final int TAG_SYSTEM_IMPERSONATION_RANGE_END = 0xFFFFFF0F;
118
119 /**
120 * Tag values between these ranges are reserved for the network stack to do traffic
121 * on behalf of applications. It is a subrange of the range above.
122 */
123 /** @hide */
124 @SystemApi
125 public static final int TAG_NETWORK_STACK_IMPERSONATION_RANGE_START = 0xFFFFFF80;
126 /** @hide */
127 @SystemApi
128 public static final int TAG_NETWORK_STACK_IMPERSONATION_RANGE_END = 0xFFFFFF8F;
129
130 /**
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700131 * Default tag value for {@link DownloadManager} traffic.
132 *
133 * @hide
134 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700135 public static final int TAG_SYSTEM_DOWNLOAD = 0xFFFFFF01;
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700136
137 /**
138 * Default tag value for {@link MediaPlayer} traffic.
139 *
140 * @hide
141 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700142 public static final int TAG_SYSTEM_MEDIA = 0xFFFFFF02;
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700143
144 /**
Christopher Tate0a61b362015-11-10 10:49:20 -0800145 * Default tag value for {@link BackupManager} backup traffic; that is,
146 * traffic from the device to the storage backend.
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700147 *
148 * @hide
149 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700150 public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03;
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700151
Christopher Tate0a61b362015-11-10 10:49:20 -0800152 /**
153 * Default tag value for {@link BackupManager} restore traffic; that is,
154 * app data retrieved from the storage backend at install time.
155 *
156 * @hide
157 */
158 public static final int TAG_SYSTEM_RESTORE = 0xFFFFFF04;
159
Jeff Sharkey763a1402016-11-21 12:14:50 -0700160 /**
Jeff Sharkeyd62e3cb2017-08-11 15:04:12 -0600161 * Default tag value for code (typically APKs) downloaded by an app store on
162 * behalf of the app, such as updates.
Jeff Sharkey763a1402016-11-21 12:14:50 -0700163 *
164 * @hide
165 */
Jeff Sharkeyd62e3cb2017-08-11 15:04:12 -0600166 public static final int TAG_SYSTEM_APP = 0xFFFFFF05;
Jeff Sharkeyf23a5e12017-06-26 19:50:41 -0600167
Chalard Jean896245a2019-04-09 11:16:56 +0900168 // TODO : remove this constant when Wifi code is updated
Jeff Sharkeyf23a5e12017-06-26 19:50:41 -0600169 /** @hide */
Jeff Sharkeyf23a5e12017-06-26 19:50:41 -0600170 public static final int TAG_SYSTEM_PROBE = 0xFFFFFF42;
Jeff Sharkey763a1402016-11-21 12:14:50 -0700171
Jeff Sharkey234766a2012-04-10 19:48:07 -0700172 private static INetworkStatsService sStatsService;
173
Chalard Jean0bb53dbb2019-04-09 15:46:21 +0900174 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Jeff Sharkey234766a2012-04-10 19:48:07 -0700175 private synchronized static INetworkStatsService getStatsService() {
176 if (sStatsService == null) {
177 sStatsService = INetworkStatsService.Stub.asInterface(
178 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
179 }
180 return sStatsService;
181 }
182
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700183 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700184 * Snapshot of {@link NetworkStats} when the currently active profiling
185 * session started, or {@code null} if no session active.
186 *
187 * @see #startDataProfiling(Context)
188 * @see #stopDataProfiling(Context)
189 */
190 private static NetworkStats sActiveProfilingStart;
191
192 private static Object sProfilingLock = new Object();
193
Benedict Wongbabe5d72017-12-03 19:42:36 -0800194 private static final String LOOPBACK_IFACE = "lo";
195
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700196 /**
Jeff Sharkey43be1742011-04-21 18:45:43 -0700197 * Set active tag to use when accounting {@link Socket} traffic originating
198 * from the current thread. Only one active tag per thread is supported.
199 * <p>
200 * Changes only take effect during subsequent calls to
201 * {@link #tagSocket(Socket)}.
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700202 * <p>
203 * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
204 * used internally by system services like {@link DownloadManager} when
205 * performing traffic on behalf of an application.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700206 *
207 * @see #clearThreadStatsTag()
Jeff Sharkey43be1742011-04-21 18:45:43 -0700208 */
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700209 public static void setThreadStatsTag(int tag) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700210 NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700211 }
212
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700213 /**
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600214 * Set active tag to use when accounting {@link Socket} traffic originating
Jeff Sharkey619a5112017-01-19 11:55:54 -0700215 * from the current thread. Only one active tag per thread is supported.
216 * <p>
217 * Changes only take effect during subsequent calls to
218 * {@link #tagSocket(Socket)}.
219 * <p>
220 * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
221 * used internally by system services like {@link DownloadManager} when
222 * performing traffic on behalf of an application.
223 *
224 * @return the current tag for the calling thread, which can be used to
225 * restore any existing values after a nested operation is finished
226 */
227 public static int getAndSetThreadStatsTag(int tag) {
228 return NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
229 }
230
231 /**
232 * Set active tag to use when accounting {@link Socket} traffic originating
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600233 * from the current thread. The tag used internally is well-defined to
234 * distinguish all backup-related traffic.
235 *
Christopher Tatea2496de2014-08-06 14:19:56 -0700236 * @hide
237 */
238 @SystemApi
239 public static void setThreadStatsTagBackup() {
240 setThreadStatsTag(TAG_SYSTEM_BACKUP);
241 }
242
243 /**
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600244 * Set active tag to use when accounting {@link Socket} traffic originating
245 * from the current thread. The tag used internally is well-defined to
246 * distinguish all restore-related traffic.
247 *
Christopher Tate0a61b362015-11-10 10:49:20 -0800248 * @hide
249 */
250 @SystemApi
251 public static void setThreadStatsTagRestore() {
252 setThreadStatsTag(TAG_SYSTEM_RESTORE);
253 }
254
255 /**
Jeff Sharkeyf23a5e12017-06-26 19:50:41 -0600256 * Set active tag to use when accounting {@link Socket} traffic originating
257 * from the current thread. The tag used internally is well-defined to
Jeff Sharkeyd62e3cb2017-08-11 15:04:12 -0600258 * distinguish all code (typically APKs) downloaded by an app store on
259 * behalf of the app, such as updates.
Jeff Sharkeyf23a5e12017-06-26 19:50:41 -0600260 *
261 * @hide
262 */
263 @SystemApi
Jeff Sharkeyd62e3cb2017-08-11 15:04:12 -0600264 public static void setThreadStatsTagApp() {
265 setThreadStatsTag(TAG_SYSTEM_APP);
Jeff Sharkeyf23a5e12017-06-26 19:50:41 -0600266 }
267
268 /**
Alon Alberteaef3512011-07-19 11:16:09 +0300269 * Get the active tag used when accounting {@link Socket} traffic originating
270 * from the current thread. Only one active tag per thread is supported.
271 * {@link #tagSocket(Socket)}.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700272 *
273 * @see #setThreadStatsTag(int)
Alon Alberteaef3512011-07-19 11:16:09 +0300274 */
275 public static int getThreadStatsTag() {
276 return NetworkManagementSocketTagger.getThreadSocketStatsTag();
277 }
278
Jeff Sharkeydddace72013-03-26 13:46:05 -0700279 /**
280 * Clear any active tag set to account {@link Socket} traffic originating
281 * from the current thread.
282 *
283 * @see #setThreadStatsTag(int)
284 */
Jeff Sharkey43be1742011-04-21 18:45:43 -0700285 public static void clearThreadStatsTag() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700286 NetworkManagementSocketTagger.setThreadSocketStatsTag(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700287 }
288
289 /**
290 * Set specific UID to use when accounting {@link Socket} traffic
291 * originating from the current thread. Designed for use when performing an
Jeff Sharkey121d5652018-03-26 13:11:33 -0600292 * operation on behalf of another application, or when another application
293 * is performing operations on your behalf.
294 * <p>
295 * Any app can <em>accept</em> blame for traffic performed on a socket
296 * originally created by another app by calling this method with the
297 * {@link android.system.Os#getuid()} value. However, only apps holding the
298 * {@code android.Manifest.permission#UPDATE_DEVICE_STATS} permission may
299 * <em>assign</em> blame to another UIDs.
Jeff Sharkey43be1742011-04-21 18:45:43 -0700300 * <p>
301 * Changes only take effect during subsequent calls to
302 * {@link #tagSocket(Socket)}.
Jeff Sharkey43be1742011-04-21 18:45:43 -0700303 */
Jeff Sharkey121d5652018-03-26 13:11:33 -0600304 @SuppressLint("Doclava125")
Jeff Sharkey43be1742011-04-21 18:45:43 -0700305 public static void setThreadStatsUid(int uid) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700306 NetworkManagementSocketTagger.setThreadSocketStatsUid(uid);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700307 }
308
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600309 /**
Jeff Sharkey121d5652018-03-26 13:11:33 -0600310 * Get the active UID used when accounting {@link Socket} traffic originating
311 * from the current thread. Only one active tag per thread is supported.
312 * {@link #tagSocket(Socket)}.
313 *
314 * @see #setThreadStatsUid(int)
315 */
316 public static int getThreadStatsUid() {
317 return NetworkManagementSocketTagger.getThreadSocketStatsUid();
318 }
319
320 /**
Jeff Sharkeya4239cf2017-11-29 11:18:23 -0700321 * Set specific UID to use when accounting {@link Socket} traffic
322 * originating from the current thread as the calling UID. Designed for use
323 * when another application is performing operations on your behalf.
324 * <p>
325 * Changes only take effect during subsequent calls to
326 * {@link #tagSocket(Socket)}.
Jeff Sharkey121d5652018-03-26 13:11:33 -0600327 *
328 * @removed
329 * @deprecated use {@link #setThreadStatsUid(int)} instead.
Jeff Sharkeya4239cf2017-11-29 11:18:23 -0700330 */
Jeff Sharkey121d5652018-03-26 13:11:33 -0600331 @Deprecated
Jeff Sharkeya4239cf2017-11-29 11:18:23 -0700332 public static void setThreadStatsUidSelf() {
333 setThreadStatsUid(android.os.Process.myUid());
334 }
335
336 /**
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600337 * Clear any active UID set to account {@link Socket} traffic originating
338 * from the current thread.
339 *
340 * @see #setThreadStatsUid(int)
Jeff Sharkey11cadda2016-03-22 10:20:12 -0600341 */
Jeff Sharkeya4239cf2017-11-29 11:18:23 -0700342 @SuppressLint("Doclava125")
Jeff Sharkey43be1742011-04-21 18:45:43 -0700343 public static void clearThreadStatsUid() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700344 NetworkManagementSocketTagger.setThreadSocketStatsUid(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700345 }
346
347 /**
348 * Tag the given {@link Socket} with any statistics parameters active for
349 * the current thread. Subsequent calls always replace any existing
350 * parameters. When finished, call {@link #untagSocket(Socket)} to remove
351 * statistics parameters.
352 *
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700353 * @see #setThreadStatsTag(int)
Jeff Sharkey43be1742011-04-21 18:45:43 -0700354 */
355 public static void tagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700356 SocketTagger.get().tag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700357 }
358
359 /**
360 * Remove any statistics parameters from the given {@link Socket}.
kopriva5ad001e2018-07-23 18:19:39 -0700361 * <p>
362 * In Android 8.1 (API level 27) and lower, a socket is automatically
363 * untagged when it's sent to another process using binder IPC with a
364 * {@code ParcelFileDescriptor} container. In Android 9.0 (API level 28)
365 * and higher, the socket tag is kept when the socket is sent to another
366 * process using binder IPC. You can mimic the previous behavior by
367 * calling {@code untagSocket()} before sending the socket to another
368 * process.
Jeff Sharkey43be1742011-04-21 18:45:43 -0700369 */
370 public static void untagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700371 SocketTagger.get().untag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700372 }
373
374 /**
Jeff Sharkeyf0d76332015-12-04 15:21:52 -0700375 * Tag the given {@link DatagramSocket} with any statistics parameters
376 * active for the current thread. Subsequent calls always replace any
377 * existing parameters. When finished, call
378 * {@link #untagDatagramSocket(DatagramSocket)} to remove statistics
379 * parameters.
380 *
381 * @see #setThreadStatsTag(int)
Jeff Sharkeyf0d76332015-12-04 15:21:52 -0700382 */
383 public static void tagDatagramSocket(DatagramSocket socket) throws SocketException {
384 SocketTagger.get().tag(socket);
385 }
386
387 /**
388 * Remove any statistics parameters from the given {@link DatagramSocket}.
389 */
390 public static void untagDatagramSocket(DatagramSocket socket) throws SocketException {
391 SocketTagger.get().untag(socket);
392 }
393
394 /**
Jeff Sharkeya4239cf2017-11-29 11:18:23 -0700395 * Tag the given {@link FileDescriptor} socket with any statistics
396 * parameters active for the current thread. Subsequent calls always replace
397 * any existing parameters. When finished, call
398 * {@link #untagFileDescriptor(FileDescriptor)} to remove statistics
399 * parameters.
400 *
401 * @see #setThreadStatsTag(int)
402 */
403 public static void tagFileDescriptor(FileDescriptor fd) throws IOException {
404 SocketTagger.get().tag(fd);
405 }
406
407 /**
408 * Remove any statistics parameters from the given {@link FileDescriptor}
409 * socket.
410 */
411 public static void untagFileDescriptor(FileDescriptor fd) throws IOException {
412 SocketTagger.get().untag(fd);
413 }
414
415 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700416 * Start profiling data usage for current UID. Only one profiling session
417 * can be active at a time.
418 *
419 * @hide
420 */
421 public static void startDataProfiling(Context context) {
422 synchronized (sProfilingLock) {
423 if (sActiveProfilingStart != null) {
424 throw new IllegalStateException("already profiling data");
425 }
426
427 // take snapshot in time; we calculate delta later
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700428 sActiveProfilingStart = getDataLayerSnapshotForUid(context);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700429 }
430 }
431
432 /**
433 * Stop profiling data usage for current UID.
434 *
435 * @return Detailed {@link NetworkStats} of data that occurred since last
436 * {@link #startDataProfiling(Context)} call.
437 * @hide
438 */
439 public static NetworkStats stopDataProfiling(Context context) {
440 synchronized (sProfilingLock) {
441 if (sActiveProfilingStart == null) {
442 throw new IllegalStateException("not profiling data");
443 }
444
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800445 // subtract starting values and return delta
446 final NetworkStats profilingStop = getDataLayerSnapshotForUid(context);
447 final NetworkStats profilingDelta = NetworkStats.subtract(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800448 profilingStop, sActiveProfilingStart, null, null);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800449 sActiveProfilingStart = null;
450 return profilingDelta;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700451 }
452 }
453
454 /**
Jeff Sharkey558a2322011-08-24 15:42:09 -0700455 * Increment count of network operations performed under the accounting tag
456 * currently active on the calling thread. This can be used to derive
457 * bytes-per-operation.
458 *
459 * @param operationCount Number of operations to increment count by.
460 */
461 public static void incrementOperationCount(int operationCount) {
462 final int tag = getThreadStatsTag();
463 incrementOperationCount(tag, operationCount);
464 }
465
466 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700467 * Increment count of network operations performed under the given
468 * accounting tag. This can be used to derive bytes-per-operation.
469 *
470 * @param tag Accounting tag used in {@link #setThreadStatsTag(int)}.
471 * @param operationCount Number of operations to increment count by.
472 */
473 public static void incrementOperationCount(int tag, int operationCount) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700474 final int uid = android.os.Process.myUid();
475 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700476 getStatsService().incrementOperationCount(uid, tag, operationCount);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700477 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700478 throw e.rethrowFromSystemServer();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700479 }
480 }
481
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700482 /** {@hide} */
483 public static void closeQuietly(INetworkStatsSession session) {
484 // TODO: move to NetworkStatsService once it exists
485 if (session != null) {
486 try {
487 session.close();
488 } catch (RuntimeException rethrown) {
489 throw rethrown;
490 } catch (Exception ignored) {
491 }
492 }
493 }
494
Chenbo Feng20328e82018-01-25 11:43:52 -0800495 private static long addIfSupported(long stat) {
496 return (stat == UNSUPPORTED) ? 0 : stat;
497 }
498
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700499 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700500 * Return number of packets transmitted across mobile networks since device
501 * boot. Counts packets across all mobile network interfaces, and always
502 * increases monotonically since device boot. Statistics are measured at the
503 * network layer, so they include both TCP and UDP usage.
504 * <p>
505 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
506 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800507 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700508 public static long getMobileTxPackets() {
509 long total = 0;
510 for (String iface : getMobileIfaces()) {
Chenbo Feng20328e82018-01-25 11:43:52 -0800511 total += addIfSupported(getTxPackets(iface));
Jeff Sharkey234766a2012-04-10 19:48:07 -0700512 }
513 return total;
514 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800515
516 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700517 * Return number of packets received across mobile networks since device
518 * boot. Counts packets across all mobile network interfaces, and always
519 * increases monotonically since device boot. Statistics are measured at the
520 * network layer, so they include both TCP and UDP usage.
521 * <p>
522 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
523 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800524 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700525 public static long getMobileRxPackets() {
526 long total = 0;
527 for (String iface : getMobileIfaces()) {
Chenbo Feng20328e82018-01-25 11:43:52 -0800528 total += addIfSupported(getRxPackets(iface));
Jeff Sharkey234766a2012-04-10 19:48:07 -0700529 }
530 return total;
531 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800532
533 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700534 * Return number of bytes transmitted across mobile networks since device
535 * boot. Counts packets across all mobile network interfaces, and always
536 * increases monotonically since device boot. Statistics are measured at the
537 * network layer, so they include both TCP and UDP usage.
538 * <p>
539 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
540 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800541 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700542 public static long getMobileTxBytes() {
543 long total = 0;
544 for (String iface : getMobileIfaces()) {
Chenbo Feng20328e82018-01-25 11:43:52 -0800545 total += addIfSupported(getTxBytes(iface));
Jeff Sharkey234766a2012-04-10 19:48:07 -0700546 }
547 return total;
548 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800549
550 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700551 * Return number of bytes received across mobile networks since device boot.
552 * Counts packets across all mobile network interfaces, and always increases
553 * monotonically since device boot. Statistics are measured at the network
554 * layer, so they include both TCP and UDP usage.
555 * <p>
556 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
557 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800558 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700559 public static long getMobileRxBytes() {
560 long total = 0;
561 for (String iface : getMobileIfaces()) {
Chenbo Feng20328e82018-01-25 11:43:52 -0800562 total += addIfSupported(getRxBytes(iface));
Jeff Sharkey234766a2012-04-10 19:48:07 -0700563 }
564 return total;
565 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800566
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800567 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100568 @UnsupportedAppUsage
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800569 public static long getMobileTcpRxPackets() {
570 long total = 0;
571 for (String iface : getMobileIfaces()) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800572 long stat = UNSUPPORTED;
573 try {
574 stat = getStatsService().getIfaceStats(iface, TYPE_TCP_RX_PACKETS);
575 } catch (RemoteException e) {
576 throw e.rethrowFromSystemServer();
577 }
Chenbo Feng20328e82018-01-25 11:43:52 -0800578 total += addIfSupported(stat);
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800579 }
580 return total;
581 }
582
583 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100584 @UnsupportedAppUsage
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800585 public static long getMobileTcpTxPackets() {
586 long total = 0;
587 for (String iface : getMobileIfaces()) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800588 long stat = UNSUPPORTED;
589 try {
590 stat = getStatsService().getIfaceStats(iface, TYPE_TCP_TX_PACKETS);
591 } catch (RemoteException e) {
592 throw e.rethrowFromSystemServer();
593 }
Chenbo Feng20328e82018-01-25 11:43:52 -0800594 total += addIfSupported(stat);
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800595 }
596 return total;
597 }
598
Aaron Huang29667d22019-09-27 22:31:22 +0800599 /**
600 * Return the number of packets transmitted on the specified interface since
601 * device boot. Statistics are measured at the network layer, so both TCP and
602 * UDP usage are included.
603 *
604 * @param iface The name of the interface.
605 * @return The number of transmitted packets.
606 */
607 public static long getTxPackets(@NonNull String iface) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800608 try {
609 return getStatsService().getIfaceStats(iface, TYPE_TX_PACKETS);
610 } catch (RemoteException e) {
611 throw e.rethrowFromSystemServer();
612 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700613 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800614
Aaron Huang29667d22019-09-27 22:31:22 +0800615 /**
616 * Return the number of packets received on the specified interface since
617 * device boot. Statistics are measured at the network layer, so both TCP
618 * and UDP usage are included.
619 *
620 * @param iface The name of the interface.
621 * @return The number of received packets.
622 */
623 public static long getRxPackets(@NonNull String iface) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800624 try {
625 return getStatsService().getIfaceStats(iface, TYPE_RX_PACKETS);
626 } catch (RemoteException e) {
627 throw e.rethrowFromSystemServer();
628 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700629 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800630
Jeff Sharkeydddace72013-03-26 13:46:05 -0700631 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100632 @UnsupportedAppUsage
Jeff Sharkey234766a2012-04-10 19:48:07 -0700633 public static long getTxBytes(String iface) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800634 try {
635 return getStatsService().getIfaceStats(iface, TYPE_TX_BYTES);
636 } catch (RemoteException e) {
637 throw e.rethrowFromSystemServer();
638 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700639 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800640
Jeff Sharkeydddace72013-03-26 13:46:05 -0700641 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100642 @UnsupportedAppUsage
Jeff Sharkey234766a2012-04-10 19:48:07 -0700643 public static long getRxBytes(String iface) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800644 try {
645 return getStatsService().getIfaceStats(iface, TYPE_RX_BYTES);
646 } catch (RemoteException e) {
647 throw e.rethrowFromSystemServer();
648 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700649 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800650
Benedict Wongbabe5d72017-12-03 19:42:36 -0800651 /** {@hide} */
652 @TestApi
653 public static long getLoopbackTxPackets() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800654 try {
655 return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_TX_PACKETS);
656 } catch (RemoteException e) {
657 throw e.rethrowFromSystemServer();
658 }
Benedict Wongbabe5d72017-12-03 19:42:36 -0800659 }
660
661 /** {@hide} */
662 @TestApi
663 public static long getLoopbackRxPackets() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800664 try {
665 return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_RX_PACKETS);
666 } catch (RemoteException e) {
667 throw e.rethrowFromSystemServer();
668 }
Benedict Wongbabe5d72017-12-03 19:42:36 -0800669 }
670
671 /** {@hide} */
672 @TestApi
673 public static long getLoopbackTxBytes() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800674 try {
675 return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_TX_BYTES);
676 } catch (RemoteException e) {
677 throw e.rethrowFromSystemServer();
678 }
Benedict Wongbabe5d72017-12-03 19:42:36 -0800679 }
680
681 /** {@hide} */
682 @TestApi
683 public static long getLoopbackRxBytes() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800684 try {
685 return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_RX_BYTES);
686 } catch (RemoteException e) {
687 throw e.rethrowFromSystemServer();
688 }
Benedict Wongbabe5d72017-12-03 19:42:36 -0800689 }
690
Irfan Sheriff227bec42011-02-15 19:30:27 -0800691 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700692 * Return number of packets transmitted since device boot. Counts packets
693 * across all network interfaces, and always increases monotonically since
694 * device boot. Statistics are measured at the network layer, so they
695 * include both TCP and UDP usage.
696 * <p>
697 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
698 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800699 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700700 public static long getTotalTxPackets() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800701 try {
702 return getStatsService().getTotalStats(TYPE_TX_PACKETS);
703 } catch (RemoteException e) {
704 throw e.rethrowFromSystemServer();
705 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700706 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800707
708 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700709 * Return number of packets received since device boot. Counts packets
710 * across all network interfaces, and always increases monotonically since
711 * device boot. Statistics are measured at the network layer, so they
712 * include both TCP and UDP usage.
713 * <p>
714 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
715 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800716 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700717 public static long getTotalRxPackets() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800718 try {
719 return getStatsService().getTotalStats(TYPE_RX_PACKETS);
720 } catch (RemoteException e) {
721 throw e.rethrowFromSystemServer();
722 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700723 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800724
725 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700726 * Return number of bytes transmitted since device boot. Counts packets
727 * across all network interfaces, and always increases monotonically since
728 * device boot. Statistics are measured at the network layer, so they
729 * include both TCP and UDP usage.
730 * <p>
731 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
732 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800733 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700734 public static long getTotalTxBytes() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800735 try {
736 return getStatsService().getTotalStats(TYPE_TX_BYTES);
737 } catch (RemoteException e) {
738 throw e.rethrowFromSystemServer();
739 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700740 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800741
742 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700743 * Return number of bytes received since device boot. Counts packets across
744 * all network interfaces, and always increases monotonically since device
745 * boot. Statistics are measured at the network layer, so they include both
746 * TCP and UDP usage.
747 * <p>
748 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
749 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800750 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700751 public static long getTotalRxBytes() {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800752 try {
753 return getStatsService().getTotalStats(TYPE_RX_BYTES);
754 } catch (RemoteException e) {
755 throw e.rethrowFromSystemServer();
756 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700757 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800758
759 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800760 * Return number of bytes transmitted by the given UID since device boot.
761 * Counts packets across all network interfaces, and always increases
762 * monotonically since device boot. Statistics are measured at the network
763 * layer, so they include both TCP and UDP usage.
764 * <p>
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700765 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
766 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
767 * <p>
768 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
769 * report traffic statistics for the calling UID. It will return
770 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
771 * historical network statistics belonging to other UIDs, use
772 * {@link NetworkStatsManager}.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800773 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800774 * @see android.os.Process#myUid()
775 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800776 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800777 public static long getUidTxBytes(int uid) {
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700778 // This isn't actually enforcing any security; it just returns the
779 // unsupported value. The real filtering is done at the kernel level.
780 final int callingUid = android.os.Process.myUid();
781 if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800782 try {
783 return getStatsService().getUidStats(uid, TYPE_TX_BYTES);
784 } catch (RemoteException e) {
785 throw e.rethrowFromSystemServer();
786 }
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700787 } else {
788 return UNSUPPORTED;
789 }
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800790 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800791
792 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800793 * Return number of bytes received by the given UID since device boot.
794 * Counts packets across all network interfaces, and always increases
795 * monotonically since device boot. Statistics are measured at the network
796 * layer, so they include both TCP and UDP usage.
797 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800798 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800799 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700800 * <p>
801 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
802 * report traffic statistics for the calling UID. It will return
803 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
804 * historical network statistics belonging to other UIDs, use
805 * {@link NetworkStatsManager}.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800806 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800807 * @see android.os.Process#myUid()
808 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800809 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800810 public static long getUidRxBytes(int uid) {
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700811 // This isn't actually enforcing any security; it just returns the
812 // unsupported value. The real filtering is done at the kernel level.
813 final int callingUid = android.os.Process.myUid();
814 if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800815 try {
816 return getStatsService().getUidStats(uid, TYPE_RX_BYTES);
817 } catch (RemoteException e) {
818 throw e.rethrowFromSystemServer();
819 }
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700820 } else {
821 return UNSUPPORTED;
822 }
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800823 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800824
825 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800826 * Return number of packets transmitted by the given UID since device boot.
827 * Counts packets across all network interfaces, and always increases
828 * monotonically since device boot. Statistics are measured at the network
829 * layer, so they include both TCP and UDP usage.
830 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800831 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800832 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700833 * <p>
834 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
835 * report traffic statistics for the calling UID. It will return
836 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
837 * historical network statistics belonging to other UIDs, use
838 * {@link NetworkStatsManager}.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800839 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800840 * @see android.os.Process#myUid()
841 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800842 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800843 public static long getUidTxPackets(int uid) {
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700844 // This isn't actually enforcing any security; it just returns the
845 // unsupported value. The real filtering is done at the kernel level.
846 final int callingUid = android.os.Process.myUid();
847 if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800848 try {
849 return getStatsService().getUidStats(uid, TYPE_TX_PACKETS);
850 } catch (RemoteException e) {
851 throw e.rethrowFromSystemServer();
852 }
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700853 } else {
854 return UNSUPPORTED;
855 }
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800856 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800857
858 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800859 * Return number of packets received by the given UID since device boot.
860 * Counts packets across all network interfaces, and always increases
861 * monotonically since device boot. Statistics are measured at the network
862 * layer, so they include both TCP and UDP usage.
863 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800864 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800865 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700866 * <p>
867 * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
868 * report traffic statistics for the calling UID. It will return
869 * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
870 * historical network statistics belonging to other UIDs, use
871 * {@link NetworkStatsManager}.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800872 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800873 * @see android.os.Process#myUid()
874 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800875 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800876 public static long getUidRxPackets(int uid) {
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700877 // This isn't actually enforcing any security; it just returns the
878 // unsupported value. The real filtering is done at the kernel level.
879 final int callingUid = android.os.Process.myUid();
880 if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) {
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800881 try {
882 return getStatsService().getUidStats(uid, TYPE_RX_PACKETS);
883 } catch (RemoteException e) {
884 throw e.rethrowFromSystemServer();
885 }
Jeff Sharkey0d69d3d2016-03-09 16:40:15 -0700886 } else {
887 return UNSUPPORTED;
888 }
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800889 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800890
891 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800892 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800893 * transport layer statistics are no longer available, and will
894 * always return {@link #UNSUPPORTED}.
895 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800896 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800897 @Deprecated
898 public static long getUidTcpTxBytes(int uid) {
899 return UNSUPPORTED;
900 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800901
902 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800903 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800904 * transport layer statistics are no longer available, and will
905 * always return {@link #UNSUPPORTED}.
906 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800907 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800908 @Deprecated
909 public static long getUidTcpRxBytes(int uid) {
910 return UNSUPPORTED;
911 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800912
913 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800914 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800915 * transport layer statistics are no longer available, and will
916 * always return {@link #UNSUPPORTED}.
917 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800918 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800919 @Deprecated
920 public static long getUidUdpTxBytes(int uid) {
921 return UNSUPPORTED;
922 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800923
924 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800925 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800926 * transport layer statistics are no longer available, and will
927 * always return {@link #UNSUPPORTED}.
928 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800929 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800930 @Deprecated
931 public static long getUidUdpRxBytes(int uid) {
932 return UNSUPPORTED;
933 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800934
935 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800936 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800937 * transport layer statistics are no longer available, and will
938 * always return {@link #UNSUPPORTED}.
939 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800940 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800941 @Deprecated
942 public static long getUidTcpTxSegments(int uid) {
943 return UNSUPPORTED;
944 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800945
946 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800947 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800948 * transport layer statistics are no longer available, and will
949 * always return {@link #UNSUPPORTED}.
950 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800951 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800952 @Deprecated
953 public static long getUidTcpRxSegments(int uid) {
954 return UNSUPPORTED;
955 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800956
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800957 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800958 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800959 * transport layer statistics are no longer available, and will
960 * always return {@link #UNSUPPORTED}.
961 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800962 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800963 @Deprecated
964 public static long getUidUdpTxPackets(int uid) {
965 return UNSUPPORTED;
966 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800967
968 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800969 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800970 * transport layer statistics are no longer available, and will
971 * always return {@link #UNSUPPORTED}.
972 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800973 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800974 @Deprecated
975 public static long getUidUdpRxPackets(int uid) {
976 return UNSUPPORTED;
977 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700978
979 /**
980 * Return detailed {@link NetworkStats} for the current UID. Requires no
981 * special permission.
982 */
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700983 private static NetworkStats getDataLayerSnapshotForUid(Context context) {
Jeff Sharkeydddace72013-03-26 13:46:05 -0700984 // TODO: take snapshot locally, since proc file is now visible
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700985 final int uid = android.os.Process.myUid();
986 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700987 return getStatsService().getDataLayerSnapshotForUid(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700988 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700989 throw e.rethrowFromSystemServer();
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700990 }
991 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700992
993 /**
994 * Return set of any ifaces associated with mobile networks since boot.
995 * Interfaces are never removed from this list, so counters should always be
996 * monotonic.
997 */
Chalard Jean0bb53dbb2019-04-09 15:46:21 +0900998 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
Jeff Sharkey234766a2012-04-10 19:48:07 -0700999 private static String[] getMobileIfaces() {
1000 try {
1001 return getStatsService().getMobileIfaces();
1002 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001003 throw e.rethrowFromSystemServer();
Jeff Sharkey234766a2012-04-10 19:48:07 -07001004 }
1005 }
1006
1007 // NOTE: keep these in sync with android_net_TrafficStats.cpp
1008 private static final int TYPE_RX_BYTES = 0;
1009 private static final int TYPE_RX_PACKETS = 1;
1010 private static final int TYPE_TX_BYTES = 2;
1011 private static final int TYPE_TX_PACKETS = 3;
Jeff Sharkey4b17a132013-02-05 21:32:33 -08001012 private static final int TYPE_TCP_RX_PACKETS = 4;
1013 private static final int TYPE_TCP_TX_PACKETS = 5;
Ken Shirrifff7d0b012009-12-07 15:56:05 -08001014}