Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net; |
| 18 | |
Junyu Lai | 8e58c82 | 2022-01-17 17:05:30 +0000 | [diff] [blame] | 19 | import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; |
| 20 | |
Aaron Huang | 443651d | 2019-09-27 22:31:22 +0800 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Jeff Sharkey | 1fb7431 | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 22 | import android.annotation.SuppressLint; |
Christopher Tate | 5a1878d | 2014-08-06 14:19:56 -0700 | [diff] [blame] | 23 | import android.annotation.SystemApi; |
Benedict Wong | 083faee | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 24 | import android.annotation.TestApi; |
Jeff Sharkey | 9279006 | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 25 | import android.app.DownloadManager; |
| 26 | import android.app.backup.BackupManager; |
Jeff Sharkey | 27c8a07 | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 27 | import android.app.usage.NetworkStatsManager; |
Artur Satayev | 164c756 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 28 | import android.compat.annotation.UnsupportedAppUsage; |
Jeff Sharkey | 33936ac | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 29 | import android.content.Context; |
Jeff Sharkey | 9279006 | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 30 | import android.media.MediaPlayer; |
Junyu Lai | 92d5b4c | 2022-01-19 08:33:21 +0000 | [diff] [blame] | 31 | import android.os.Binder; |
Chalard Jean | 3cfb499 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 32 | import android.os.Build; |
Jeff Sharkey | 33936ac | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 33 | import android.os.RemoteException; |
Lorenzo Colitti | d2ae739 | 2022-01-29 21:07:33 +0900 | [diff] [blame] | 34 | import android.os.StrictMode; |
Junyu Lai | 3355c40 | 2022-01-25 07:40:06 +0000 | [diff] [blame] | 35 | import android.util.Log; |
Jeff Sharkey | 33936ac | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 36 | |
Jeff Sharkey | 1fb7431 | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 37 | import java.io.FileDescriptor; |
| 38 | import java.io.IOException; |
Jeff Sharkey | 7b124cb | 2015-12-04 15:21:52 -0700 | [diff] [blame] | 39 | import java.net.DatagramSocket; |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 40 | import java.net.Socket; |
| 41 | import java.net.SocketException; |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
Jeff Sharkey | 27c8a07 | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 44 | * Class that provides network traffic statistics. These statistics include |
Dan Egnor | eaeb702 | 2010-04-07 17:30:50 -0700 | [diff] [blame] | 45 | * 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 Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 47 | * <p> |
Jeff Sharkey | 27c8a07 | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 48 | * 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 Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 54 | */ |
| 55 | public class TrafficStats { |
Lorenzo Colitti | d2ae739 | 2022-01-29 21:07:33 +0900 | [diff] [blame] | 56 | static { |
| 57 | System.loadLibrary("framework-connectivity-tiramisu-jni"); |
| 58 | } |
| 59 | |
Junyu Lai | 3f7bb33 | 2021-12-28 16:18:43 +0000 | [diff] [blame] | 60 | private static final String TAG = TrafficStats.class.getSimpleName(); |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 61 | /** |
| 62 | * The return value to indicate that the device does not support the statistic. |
| 63 | */ |
| 64 | public final static int UNSUPPORTED = -1; |
| 65 | |
Junyu Lai | 1d3ce85 | 2021-12-27 13:56:59 +0000 | [diff] [blame] | 66 | /** @hide @deprecated use {@code DataUnit} instead to clarify SI-vs-IEC */ |
Jeff Sharkey | a6886af | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 67 | @Deprecated |
Jeff Sharkey | 94a315c | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 68 | public static final long KB_IN_BYTES = 1024; |
Junyu Lai | 1d3ce85 | 2021-12-27 13:56:59 +0000 | [diff] [blame] | 69 | /** @hide @deprecated use {@code DataUnit} instead to clarify SI-vs-IEC */ |
Jeff Sharkey | a6886af | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 70 | @Deprecated |
Jeff Sharkey | 94a315c | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 71 | public static final long MB_IN_BYTES = KB_IN_BYTES * 1024; |
Junyu Lai | 1d3ce85 | 2021-12-27 13:56:59 +0000 | [diff] [blame] | 72 | /** @hide @deprecated use {@code DataUnit} instead to clarify SI-vs-IEC */ |
Jeff Sharkey | a6886af | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 73 | @Deprecated |
Jeff Sharkey | 94a315c | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 74 | public static final long GB_IN_BYTES = MB_IN_BYTES * 1024; |
Junyu Lai | 1d3ce85 | 2021-12-27 13:56:59 +0000 | [diff] [blame] | 75 | /** @hide @deprecated use {@code DataUnit} instead to clarify SI-vs-IEC */ |
Jeff Sharkey | a6886af | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 76 | @Deprecated |
Jeff Sharkey | 6f2ba5e | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 77 | public static final long TB_IN_BYTES = GB_IN_BYTES * 1024; |
Junyu Lai | 1d3ce85 | 2021-12-27 13:56:59 +0000 | [diff] [blame] | 78 | /** @hide @deprecated use {@code DataUnit} instead to clarify SI-vs-IEC */ |
Jeff Sharkey | a6886af | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 79 | @Deprecated |
Jeff Sharkey | 8806907 | 2015-06-15 21:09:10 -0700 | [diff] [blame] | 80 | public static final long PB_IN_BYTES = TB_IN_BYTES * 1024; |
Jeff Sharkey | 94a315c | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 81 | |
Jeff Sharkey | 1a3eb88 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 82 | /** |
Jeff Sharkey | c04cda6 | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 83 | * Special UID value used when collecting {@link NetworkStatsHistory} for |
| 84 | * removed applications. |
| 85 | * |
| 86 | * @hide |
| 87 | */ |
| 88 | public static final int UID_REMOVED = -4; |
| 89 | |
| 90 | /** |
Jeff Sharkey | 987f5ff | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 91 | * Special UID value used when collecting {@link NetworkStatsHistory} for |
| 92 | * tethering traffic. |
| 93 | * |
| 94 | * @hide |
| 95 | */ |
junyulai | 55041a4 | 2019-11-15 17:15:01 +0800 | [diff] [blame] | 96 | public static final int UID_TETHERING = NetworkStats.UID_TETHERING; |
Jeff Sharkey | 987f5ff | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
Chalard Jean | 8a93ab8 | 2019-04-09 11:16:56 +0900 | [diff] [blame] | 99 | * Tag values in this range are reserved for the network stack. The network stack is |
| 100 | * running as UID {@link android.os.Process.NETWORK_STACK_UID} when in the mainline |
| 101 | * module separate process, and as the system UID otherwise. |
| 102 | */ |
| 103 | /** @hide */ |
| 104 | @SystemApi |
| 105 | public static final int TAG_NETWORK_STACK_RANGE_START = 0xFFFFFD00; |
| 106 | /** @hide */ |
| 107 | @SystemApi |
| 108 | public static final int TAG_NETWORK_STACK_RANGE_END = 0xFFFFFEFF; |
| 109 | |
| 110 | /** |
| 111 | * Tags between 0xFFFFFF00 and 0xFFFFFFFF are reserved and used internally by system services |
| 112 | * like DownloadManager when performing traffic on behalf of an application. |
| 113 | */ |
| 114 | // Please note there is no enforcement of these constants, so do not rely on them to |
| 115 | // determine that the caller is a system caller. |
| 116 | /** @hide */ |
| 117 | @SystemApi |
| 118 | public static final int TAG_SYSTEM_IMPERSONATION_RANGE_START = 0xFFFFFF00; |
| 119 | /** @hide */ |
| 120 | @SystemApi |
| 121 | public static final int TAG_SYSTEM_IMPERSONATION_RANGE_END = 0xFFFFFF0F; |
| 122 | |
| 123 | /** |
| 124 | * Tag values between these ranges are reserved for the network stack to do traffic |
| 125 | * on behalf of applications. It is a subrange of the range above. |
| 126 | */ |
| 127 | /** @hide */ |
| 128 | @SystemApi |
| 129 | public static final int TAG_NETWORK_STACK_IMPERSONATION_RANGE_START = 0xFFFFFF80; |
| 130 | /** @hide */ |
| 131 | @SystemApi |
| 132 | public static final int TAG_NETWORK_STACK_IMPERSONATION_RANGE_END = 0xFFFFFF8F; |
| 133 | |
| 134 | /** |
Jeff Sharkey | 9279006 | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 135 | * Default tag value for {@link DownloadManager} traffic. |
| 136 | * |
| 137 | * @hide |
| 138 | */ |
Jeff Sharkey | 987f5ff | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 139 | public static final int TAG_SYSTEM_DOWNLOAD = 0xFFFFFF01; |
Jeff Sharkey | 9279006 | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * Default tag value for {@link MediaPlayer} traffic. |
| 143 | * |
| 144 | * @hide |
| 145 | */ |
Jeff Sharkey | 987f5ff | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 146 | public static final int TAG_SYSTEM_MEDIA = 0xFFFFFF02; |
Jeff Sharkey | 9279006 | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 147 | |
| 148 | /** |
Christopher Tate | c1c71fd | 2015-11-10 10:49:20 -0800 | [diff] [blame] | 149 | * Default tag value for {@link BackupManager} backup traffic; that is, |
| 150 | * traffic from the device to the storage backend. |
Jeff Sharkey | 9279006 | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 151 | * |
| 152 | * @hide |
| 153 | */ |
Jeff Sharkey | 987f5ff | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 154 | public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03; |
Jeff Sharkey | 9279006 | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 155 | |
Christopher Tate | c1c71fd | 2015-11-10 10:49:20 -0800 | [diff] [blame] | 156 | /** |
| 157 | * Default tag value for {@link BackupManager} restore traffic; that is, |
| 158 | * app data retrieved from the storage backend at install time. |
| 159 | * |
| 160 | * @hide |
| 161 | */ |
| 162 | public static final int TAG_SYSTEM_RESTORE = 0xFFFFFF04; |
| 163 | |
Jeff Sharkey | 66dca17 | 2016-11-21 12:14:50 -0700 | [diff] [blame] | 164 | /** |
Jeff Sharkey | b1f97ac | 2017-08-11 15:04:12 -0600 | [diff] [blame] | 165 | * Default tag value for code (typically APKs) downloaded by an app store on |
| 166 | * behalf of the app, such as updates. |
Jeff Sharkey | 66dca17 | 2016-11-21 12:14:50 -0700 | [diff] [blame] | 167 | * |
| 168 | * @hide |
| 169 | */ |
Jeff Sharkey | b1f97ac | 2017-08-11 15:04:12 -0600 | [diff] [blame] | 170 | public static final int TAG_SYSTEM_APP = 0xFFFFFF05; |
Jeff Sharkey | 8af5ad1 | 2017-06-26 19:50:41 -0600 | [diff] [blame] | 171 | |
Chalard Jean | 8a93ab8 | 2019-04-09 11:16:56 +0900 | [diff] [blame] | 172 | // TODO : remove this constant when Wifi code is updated |
Jeff Sharkey | 8af5ad1 | 2017-06-26 19:50:41 -0600 | [diff] [blame] | 173 | /** @hide */ |
Jeff Sharkey | 8af5ad1 | 2017-06-26 19:50:41 -0600 | [diff] [blame] | 174 | public static final int TAG_SYSTEM_PROBE = 0xFFFFFF42; |
Jeff Sharkey | 66dca17 | 2016-11-21 12:14:50 -0700 | [diff] [blame] | 175 | |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 176 | private static INetworkStatsService sStatsService; |
| 177 | |
Chalard Jean | 3cfb499 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 178 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 179 | private synchronized static INetworkStatsService getStatsService() { |
| 180 | if (sStatsService == null) { |
Junyu Lai | 92d5b4c | 2022-01-19 08:33:21 +0000 | [diff] [blame] | 181 | throw new IllegalStateException("TrafficStats not initialized, uid=" |
| 182 | + Binder.getCallingUid()); |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 183 | } |
| 184 | return sStatsService; |
| 185 | } |
| 186 | |
Jeff Sharkey | 9279006 | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 187 | /** |
Jeff Sharkey | 33936ac | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 188 | * Snapshot of {@link NetworkStats} when the currently active profiling |
| 189 | * session started, or {@code null} if no session active. |
| 190 | * |
| 191 | * @see #startDataProfiling(Context) |
| 192 | * @see #stopDataProfiling(Context) |
| 193 | */ |
| 194 | private static NetworkStats sActiveProfilingStart; |
| 195 | |
| 196 | private static Object sProfilingLock = new Object(); |
| 197 | |
Benedict Wong | 083faee | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 198 | private static final String LOOPBACK_IFACE = "lo"; |
| 199 | |
Jeff Sharkey | 33936ac | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 200 | /** |
Junyu Lai | 92d5b4c | 2022-01-19 08:33:21 +0000 | [diff] [blame] | 201 | * Initialization {@link TrafficStats} with the context, to |
| 202 | * allow {@link TrafficStats} to fetch the needed binder. |
| 203 | * |
| 204 | * @param context a long-lived context, such as the application context or system |
| 205 | * server context. |
| 206 | * @hide |
| 207 | */ |
junyulai | 44e61e2 | 2022-03-01 16:53:28 +0800 | [diff] [blame] | 208 | @SystemApi(client = MODULE_LIBRARIES) |
Junyu Lai | 92d5b4c | 2022-01-19 08:33:21 +0000 | [diff] [blame] | 209 | @SuppressLint("VisiblySynchronized") |
| 210 | public static synchronized void init(@NonNull final Context context) { |
| 211 | if (sStatsService != null) { |
| 212 | throw new IllegalStateException("TrafficStats is already initialized, uid=" |
| 213 | + Binder.getCallingUid()); |
| 214 | } |
| 215 | final NetworkStatsManager statsManager = |
| 216 | context.getSystemService(NetworkStatsManager.class); |
Junyu Lai | 3355c40 | 2022-01-25 07:40:06 +0000 | [diff] [blame] | 217 | if (statsManager == null) { |
| 218 | // TODO: Currently Process.isSupplemental is not working yet, because it depends on |
| 219 | // process to run in a certain UID range, which is not true for now. Change this |
| 220 | // to Log.wtf once Process.isSupplemental is ready. |
| 221 | Log.e(TAG, "TrafficStats not initialized, uid=" + Binder.getCallingUid()); |
| 222 | return; |
| 223 | } |
Junyu Lai | 92d5b4c | 2022-01-19 08:33:21 +0000 | [diff] [blame] | 224 | sStatsService = statsManager.getBinder(); |
| 225 | } |
| 226 | |
| 227 | /** |
Junyu Lai | 8e58c82 | 2022-01-17 17:05:30 +0000 | [diff] [blame] | 228 | * Attach the socket tagger implementation to the current process, to |
| 229 | * get notified when a socket's {@link FileDescriptor} is assigned to |
| 230 | * a thread. See {@link SocketTagger#set(SocketTagger)}. |
| 231 | * |
| 232 | * @hide |
| 233 | */ |
| 234 | @SystemApi(client = MODULE_LIBRARIES) |
| 235 | public static void attachSocketTagger() { |
Lorenzo Colitti | d2ae739 | 2022-01-29 21:07:33 +0900 | [diff] [blame] | 236 | dalvik.system.SocketTagger.set(new SocketTagger()); |
Junyu Lai | 8e58c82 | 2022-01-17 17:05:30 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Lorenzo Colitti | d2ae739 | 2022-01-29 21:07:33 +0900 | [diff] [blame] | 239 | private static class SocketTagger extends dalvik.system.SocketTagger { |
| 240 | |
| 241 | // TODO: set to false |
| 242 | private static final boolean LOGD = true; |
| 243 | |
| 244 | SocketTagger() { |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void tag(FileDescriptor fd) throws SocketException { |
| 249 | final UidTag tagInfo = sThreadUidTag.get(); |
| 250 | if (LOGD) { |
| 251 | Log.d(TAG, "tagSocket(" + fd.getInt$() + ") with statsTag=0x" |
| 252 | + Integer.toHexString(tagInfo.tag) + ", statsUid=" + tagInfo.uid); |
| 253 | } |
| 254 | if (tagInfo.tag == -1) { |
| 255 | StrictMode.noteUntaggedSocket(); |
| 256 | } |
| 257 | |
| 258 | if (tagInfo.tag == -1 && tagInfo.uid == -1) return; |
| 259 | final int errno = native_tagSocketFd(fd, tagInfo.tag, tagInfo.uid); |
| 260 | if (errno < 0) { |
| 261 | Log.i(TAG, "tagSocketFd(" + fd.getInt$() + ", " |
| 262 | + tagInfo.tag + ", " |
| 263 | + tagInfo.uid + ") failed with errno" + errno); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public void untag(FileDescriptor fd) throws SocketException { |
| 269 | if (LOGD) { |
| 270 | Log.i(TAG, "untagSocket(" + fd.getInt$() + ")"); |
| 271 | } |
| 272 | |
| 273 | final UidTag tagInfo = sThreadUidTag.get(); |
| 274 | if (tagInfo.tag == -1 && tagInfo.uid == -1) return; |
| 275 | |
| 276 | final int errno = native_untagSocketFd(fd); |
| 277 | if (errno < 0) { |
| 278 | Log.w(TAG, "untagSocket(" + fd.getInt$() + ") failed with errno " + errno); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | private static native int native_tagSocketFd(FileDescriptor fd, int tag, int uid); |
| 284 | private static native int native_untagSocketFd(FileDescriptor fd); |
| 285 | |
| 286 | private static class UidTag { |
| 287 | public int tag = -1; |
| 288 | public int uid = -1; |
| 289 | } |
| 290 | |
| 291 | private static ThreadLocal<UidTag> sThreadUidTag = new ThreadLocal<UidTag>() { |
| 292 | @Override |
| 293 | protected UidTag initialValue() { |
| 294 | return new UidTag(); |
| 295 | } |
| 296 | }; |
| 297 | |
Junyu Lai | 8e58c82 | 2022-01-17 17:05:30 +0000 | [diff] [blame] | 298 | /** |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 299 | * Set active tag to use when accounting {@link Socket} traffic originating |
| 300 | * from the current thread. Only one active tag per thread is supported. |
| 301 | * <p> |
| 302 | * Changes only take effect during subsequent calls to |
| 303 | * {@link #tagSocket(Socket)}. |
Jeff Sharkey | 987f5ff | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 304 | * <p> |
| 305 | * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and |
| 306 | * used internally by system services like {@link DownloadManager} when |
| 307 | * performing traffic on behalf of an application. |
Jeff Sharkey | 39fb9cd | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 308 | * |
| 309 | * @see #clearThreadStatsTag() |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 310 | */ |
Jeff Sharkey | 9279006 | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 311 | public static void setThreadStatsTag(int tag) { |
Lorenzo Colitti | d2ae739 | 2022-01-29 21:07:33 +0900 | [diff] [blame] | 312 | getAndSetThreadStatsTag(tag); |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Jeff Sharkey | 9279006 | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 315 | /** |
Jeff Sharkey | f759159 | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 316 | * Set active tag to use when accounting {@link Socket} traffic originating |
Jeff Sharkey | 289eac1 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 317 | * from the current thread. Only one active tag per thread is supported. |
| 318 | * <p> |
| 319 | * Changes only take effect during subsequent calls to |
| 320 | * {@link #tagSocket(Socket)}. |
| 321 | * <p> |
| 322 | * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and |
| 323 | * used internally by system services like {@link DownloadManager} when |
| 324 | * performing traffic on behalf of an application. |
| 325 | * |
| 326 | * @return the current tag for the calling thread, which can be used to |
| 327 | * restore any existing values after a nested operation is finished |
| 328 | */ |
| 329 | public static int getAndSetThreadStatsTag(int tag) { |
Lorenzo Colitti | d2ae739 | 2022-01-29 21:07:33 +0900 | [diff] [blame] | 330 | final int old = sThreadUidTag.get().tag; |
| 331 | sThreadUidTag.get().tag = tag; |
| 332 | return old; |
Jeff Sharkey | 289eac1 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Set active tag to use when accounting {@link Socket} traffic originating |
Jeff Sharkey | f759159 | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 337 | * from the current thread. The tag used internally is well-defined to |
| 338 | * distinguish all backup-related traffic. |
| 339 | * |
Christopher Tate | 5a1878d | 2014-08-06 14:19:56 -0700 | [diff] [blame] | 340 | * @hide |
| 341 | */ |
| 342 | @SystemApi |
| 343 | public static void setThreadStatsTagBackup() { |
| 344 | setThreadStatsTag(TAG_SYSTEM_BACKUP); |
| 345 | } |
| 346 | |
| 347 | /** |
Jeff Sharkey | f759159 | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 348 | * Set active tag to use when accounting {@link Socket} traffic originating |
| 349 | * from the current thread. The tag used internally is well-defined to |
| 350 | * distinguish all restore-related traffic. |
| 351 | * |
Christopher Tate | c1c71fd | 2015-11-10 10:49:20 -0800 | [diff] [blame] | 352 | * @hide |
| 353 | */ |
| 354 | @SystemApi |
| 355 | public static void setThreadStatsTagRestore() { |
| 356 | setThreadStatsTag(TAG_SYSTEM_RESTORE); |
| 357 | } |
| 358 | |
| 359 | /** |
Jeff Sharkey | 8af5ad1 | 2017-06-26 19:50:41 -0600 | [diff] [blame] | 360 | * Set active tag to use when accounting {@link Socket} traffic originating |
| 361 | * from the current thread. The tag used internally is well-defined to |
Jeff Sharkey | b1f97ac | 2017-08-11 15:04:12 -0600 | [diff] [blame] | 362 | * distinguish all code (typically APKs) downloaded by an app store on |
| 363 | * behalf of the app, such as updates. |
Jeff Sharkey | 8af5ad1 | 2017-06-26 19:50:41 -0600 | [diff] [blame] | 364 | * |
| 365 | * @hide |
| 366 | */ |
| 367 | @SystemApi |
Jeff Sharkey | b1f97ac | 2017-08-11 15:04:12 -0600 | [diff] [blame] | 368 | public static void setThreadStatsTagApp() { |
| 369 | setThreadStatsTag(TAG_SYSTEM_APP); |
Jeff Sharkey | 8af5ad1 | 2017-06-26 19:50:41 -0600 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /** |
Junyu Lai | ab2c35e | 2022-01-18 02:46:23 +0000 | [diff] [blame] | 373 | * Set active tag to use when accounting {@link Socket} traffic originating |
| 374 | * from the current thread. The tag used internally is well-defined to |
| 375 | * distinguish all download provider traffic. |
| 376 | * |
| 377 | * @hide |
| 378 | */ |
junyulai | 44e61e2 | 2022-03-01 16:53:28 +0800 | [diff] [blame] | 379 | @SystemApi(client = MODULE_LIBRARIES) |
Junyu Lai | ab2c35e | 2022-01-18 02:46:23 +0000 | [diff] [blame] | 380 | public static void setThreadStatsTagDownload() { |
| 381 | setThreadStatsTag(TAG_SYSTEM_DOWNLOAD); |
| 382 | } |
| 383 | |
| 384 | /** |
Alon Albert | 2c295f0 | 2011-07-19 11:16:09 +0300 | [diff] [blame] | 385 | * Get the active tag used when accounting {@link Socket} traffic originating |
| 386 | * from the current thread. Only one active tag per thread is supported. |
| 387 | * {@link #tagSocket(Socket)}. |
Jeff Sharkey | 39fb9cd | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 388 | * |
| 389 | * @see #setThreadStatsTag(int) |
Alon Albert | 2c295f0 | 2011-07-19 11:16:09 +0300 | [diff] [blame] | 390 | */ |
| 391 | public static int getThreadStatsTag() { |
Lorenzo Colitti | d2ae739 | 2022-01-29 21:07:33 +0900 | [diff] [blame] | 392 | return sThreadUidTag.get().tag; |
Alon Albert | 2c295f0 | 2011-07-19 11:16:09 +0300 | [diff] [blame] | 393 | } |
| 394 | |
Jeff Sharkey | 39fb9cd | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 395 | /** |
| 396 | * Clear any active tag set to account {@link Socket} traffic originating |
| 397 | * from the current thread. |
| 398 | * |
| 399 | * @see #setThreadStatsTag(int) |
| 400 | */ |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 401 | public static void clearThreadStatsTag() { |
Lorenzo Colitti | d2ae739 | 2022-01-29 21:07:33 +0900 | [diff] [blame] | 402 | sThreadUidTag.get().tag = -1; |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Set specific UID to use when accounting {@link Socket} traffic |
| 407 | * originating from the current thread. Designed for use when performing an |
Jeff Sharkey | 17a3875 | 2018-03-26 13:11:33 -0600 | [diff] [blame] | 408 | * operation on behalf of another application, or when another application |
| 409 | * is performing operations on your behalf. |
| 410 | * <p> |
| 411 | * Any app can <em>accept</em> blame for traffic performed on a socket |
| 412 | * originally created by another app by calling this method with the |
| 413 | * {@link android.system.Os#getuid()} value. However, only apps holding the |
| 414 | * {@code android.Manifest.permission#UPDATE_DEVICE_STATS} permission may |
| 415 | * <em>assign</em> blame to another UIDs. |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 416 | * <p> |
| 417 | * Changes only take effect during subsequent calls to |
| 418 | * {@link #tagSocket(Socket)}. |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 419 | */ |
Aurimas Liutikas | 9de1e0e | 2020-11-12 18:29:11 -0800 | [diff] [blame] | 420 | @SuppressLint("RequiresPermission") |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 421 | public static void setThreadStatsUid(int uid) { |
Lorenzo Colitti | d2ae739 | 2022-01-29 21:07:33 +0900 | [diff] [blame] | 422 | sThreadUidTag.get().uid = uid; |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Jeff Sharkey | f759159 | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 425 | /** |
Jeff Sharkey | 17a3875 | 2018-03-26 13:11:33 -0600 | [diff] [blame] | 426 | * Get the active UID used when accounting {@link Socket} traffic originating |
| 427 | * from the current thread. Only one active tag per thread is supported. |
| 428 | * {@link #tagSocket(Socket)}. |
| 429 | * |
| 430 | * @see #setThreadStatsUid(int) |
| 431 | */ |
| 432 | public static int getThreadStatsUid() { |
Lorenzo Colitti | d2ae739 | 2022-01-29 21:07:33 +0900 | [diff] [blame] | 433 | return sThreadUidTag.get().uid; |
Jeff Sharkey | 17a3875 | 2018-03-26 13:11:33 -0600 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /** |
Jeff Sharkey | 1fb7431 | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 437 | * Set specific UID to use when accounting {@link Socket} traffic |
| 438 | * originating from the current thread as the calling UID. Designed for use |
| 439 | * when another application is performing operations on your behalf. |
| 440 | * <p> |
| 441 | * Changes only take effect during subsequent calls to |
| 442 | * {@link #tagSocket(Socket)}. |
Jeff Sharkey | 17a3875 | 2018-03-26 13:11:33 -0600 | [diff] [blame] | 443 | * |
| 444 | * @removed |
| 445 | * @deprecated use {@link #setThreadStatsUid(int)} instead. |
Jeff Sharkey | 1fb7431 | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 446 | */ |
Jeff Sharkey | 17a3875 | 2018-03-26 13:11:33 -0600 | [diff] [blame] | 447 | @Deprecated |
Jeff Sharkey | 1fb7431 | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 448 | public static void setThreadStatsUidSelf() { |
| 449 | setThreadStatsUid(android.os.Process.myUid()); |
| 450 | } |
| 451 | |
| 452 | /** |
Jeff Sharkey | f759159 | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 453 | * Clear any active UID set to account {@link Socket} traffic originating |
| 454 | * from the current thread. |
| 455 | * |
| 456 | * @see #setThreadStatsUid(int) |
Jeff Sharkey | f759159 | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 457 | */ |
Aurimas Liutikas | 9de1e0e | 2020-11-12 18:29:11 -0800 | [diff] [blame] | 458 | @SuppressLint("RequiresPermission") |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 459 | public static void clearThreadStatsUid() { |
Lorenzo Colitti | d2ae739 | 2022-01-29 21:07:33 +0900 | [diff] [blame] | 460 | setThreadStatsUid(-1); |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Tag the given {@link Socket} with any statistics parameters active for |
| 465 | * the current thread. Subsequent calls always replace any existing |
| 466 | * parameters. When finished, call {@link #untagSocket(Socket)} to remove |
| 467 | * statistics parameters. |
| 468 | * |
Jeff Sharkey | 9279006 | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 469 | * @see #setThreadStatsTag(int) |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 470 | */ |
junyulai | 44e61e2 | 2022-03-01 16:53:28 +0800 | [diff] [blame] | 471 | public static void tagSocket(@NonNull Socket socket) throws SocketException { |
Jesse Wilson | b05f41c | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 472 | SocketTagger.get().tag(socket); |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Remove any statistics parameters from the given {@link Socket}. |
kopriva | c134e24 | 2018-07-23 18:19:39 -0700 | [diff] [blame] | 477 | * <p> |
| 478 | * In Android 8.1 (API level 27) and lower, a socket is automatically |
| 479 | * untagged when it's sent to another process using binder IPC with a |
| 480 | * {@code ParcelFileDescriptor} container. In Android 9.0 (API level 28) |
| 481 | * and higher, the socket tag is kept when the socket is sent to another |
| 482 | * process using binder IPC. You can mimic the previous behavior by |
| 483 | * calling {@code untagSocket()} before sending the socket to another |
| 484 | * process. |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 485 | */ |
junyulai | 44e61e2 | 2022-03-01 16:53:28 +0800 | [diff] [blame] | 486 | public static void untagSocket(@NonNull Socket socket) throws SocketException { |
Jesse Wilson | b05f41c | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 487 | SocketTagger.get().untag(socket); |
Jeff Sharkey | b82cd3d | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /** |
Jeff Sharkey | 7b124cb | 2015-12-04 15:21:52 -0700 | [diff] [blame] | 491 | * Tag the given {@link DatagramSocket} with any statistics parameters |
| 492 | * active for the current thread. Subsequent calls always replace any |
| 493 | * existing parameters. When finished, call |
| 494 | * {@link #untagDatagramSocket(DatagramSocket)} to remove statistics |
| 495 | * parameters. |
| 496 | * |
| 497 | * @see #setThreadStatsTag(int) |
Jeff Sharkey | 7b124cb | 2015-12-04 15:21:52 -0700 | [diff] [blame] | 498 | */ |
junyulai | 44e61e2 | 2022-03-01 16:53:28 +0800 | [diff] [blame] | 499 | public static void tagDatagramSocket(@NonNull DatagramSocket socket) throws SocketException { |
Jeff Sharkey | 7b124cb | 2015-12-04 15:21:52 -0700 | [diff] [blame] | 500 | SocketTagger.get().tag(socket); |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Remove any statistics parameters from the given {@link DatagramSocket}. |
| 505 | */ |
junyulai | 44e61e2 | 2022-03-01 16:53:28 +0800 | [diff] [blame] | 506 | public static void untagDatagramSocket(@NonNull DatagramSocket socket) throws SocketException { |
Jeff Sharkey | 7b124cb | 2015-12-04 15:21:52 -0700 | [diff] [blame] | 507 | SocketTagger.get().untag(socket); |
| 508 | } |
| 509 | |
| 510 | /** |
Jeff Sharkey | 1fb7431 | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 511 | * Tag the given {@link FileDescriptor} socket with any statistics |
| 512 | * parameters active for the current thread. Subsequent calls always replace |
| 513 | * any existing parameters. When finished, call |
| 514 | * {@link #untagFileDescriptor(FileDescriptor)} to remove statistics |
| 515 | * parameters. |
| 516 | * |
| 517 | * @see #setThreadStatsTag(int) |
| 518 | */ |
junyulai | 44e61e2 | 2022-03-01 16:53:28 +0800 | [diff] [blame] | 519 | public static void tagFileDescriptor(@NonNull FileDescriptor fd) throws IOException { |
Jeff Sharkey | 1fb7431 | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 520 | SocketTagger.get().tag(fd); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Remove any statistics parameters from the given {@link FileDescriptor} |
| 525 | * socket. |
| 526 | */ |
junyulai | 44e61e2 | 2022-03-01 16:53:28 +0800 | [diff] [blame] | 527 | public static void untagFileDescriptor(@NonNull FileDescriptor fd) throws IOException { |
Jeff Sharkey | 1fb7431 | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 528 | SocketTagger.get().untag(fd); |
| 529 | } |
| 530 | |
| 531 | /** |
Jeff Sharkey | 33936ac | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 532 | * Start profiling data usage for current UID. Only one profiling session |
| 533 | * can be active at a time. |
| 534 | * |
| 535 | * @hide |
| 536 | */ |
| 537 | public static void startDataProfiling(Context context) { |
| 538 | synchronized (sProfilingLock) { |
| 539 | if (sActiveProfilingStart != null) { |
| 540 | throw new IllegalStateException("already profiling data"); |
| 541 | } |
| 542 | |
| 543 | // take snapshot in time; we calculate delta later |
Jeff Sharkey | 7407e09 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 544 | sActiveProfilingStart = getDataLayerSnapshotForUid(context); |
Jeff Sharkey | 33936ac | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * Stop profiling data usage for current UID. |
| 550 | * |
| 551 | * @return Detailed {@link NetworkStats} of data that occurred since last |
| 552 | * {@link #startDataProfiling(Context)} call. |
| 553 | * @hide |
| 554 | */ |
| 555 | public static NetworkStats stopDataProfiling(Context context) { |
| 556 | synchronized (sProfilingLock) { |
| 557 | if (sActiveProfilingStart == null) { |
| 558 | throw new IllegalStateException("not profiling data"); |
| 559 | } |
| 560 | |
Jeff Sharkey | ef7bded | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 561 | // subtract starting values and return delta |
| 562 | final NetworkStats profilingStop = getDataLayerSnapshotForUid(context); |
| 563 | final NetworkStats profilingDelta = NetworkStats.subtract( |
Jeff Sharkey | bfe8268 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 564 | profilingStop, sActiveProfilingStart, null, null); |
Jeff Sharkey | ef7bded | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 565 | sActiveProfilingStart = null; |
| 566 | return profilingDelta; |
Jeff Sharkey | 33936ac | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
| 570 | /** |
Jeff Sharkey | 60ca023 | 2011-08-24 15:42:09 -0700 | [diff] [blame] | 571 | * Increment count of network operations performed under the accounting tag |
| 572 | * currently active on the calling thread. This can be used to derive |
| 573 | * bytes-per-operation. |
| 574 | * |
| 575 | * @param operationCount Number of operations to increment count by. |
| 576 | */ |
| 577 | public static void incrementOperationCount(int operationCount) { |
| 578 | final int tag = getThreadStatsTag(); |
| 579 | incrementOperationCount(tag, operationCount); |
| 580 | } |
| 581 | |
| 582 | /** |
Jeff Sharkey | 7407e09 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 583 | * Increment count of network operations performed under the given |
| 584 | * accounting tag. This can be used to derive bytes-per-operation. |
| 585 | * |
| 586 | * @param tag Accounting tag used in {@link #setThreadStatsTag(int)}. |
| 587 | * @param operationCount Number of operations to increment count by. |
| 588 | */ |
| 589 | public static void incrementOperationCount(int tag, int operationCount) { |
Jeff Sharkey | 7407e09 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 590 | final int uid = android.os.Process.myUid(); |
| 591 | try { |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 592 | getStatsService().incrementOperationCount(uid, tag, operationCount); |
Jeff Sharkey | 7407e09 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 593 | } catch (RemoteException e) { |
Jeff Sharkey | bc08d07 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 594 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 7407e09 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
Jeff Sharkey | 920d904 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 598 | /** {@hide} */ |
| 599 | public static void closeQuietly(INetworkStatsSession session) { |
| 600 | // TODO: move to NetworkStatsService once it exists |
| 601 | if (session != null) { |
| 602 | try { |
| 603 | session.close(); |
| 604 | } catch (RuntimeException rethrown) { |
| 605 | throw rethrown; |
| 606 | } catch (Exception ignored) { |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
Chenbo Feng | 905f034 | 2018-01-25 11:43:52 -0800 | [diff] [blame] | 611 | private static long addIfSupported(long stat) { |
| 612 | return (stat == UNSUPPORTED) ? 0 : stat; |
| 613 | } |
| 614 | |
Jeff Sharkey | 7407e09 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 615 | /** |
Jeff Sharkey | 39fb9cd | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 616 | * Return number of packets transmitted across mobile networks since device |
| 617 | * boot. Counts packets across all mobile network interfaces, and always |
| 618 | * increases monotonically since device boot. Statistics are measured at the |
| 619 | * network layer, so they include both TCP and UDP usage. |
| 620 | * <p> |
| 621 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 622 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 623 | */ |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 624 | public static long getMobileTxPackets() { |
| 625 | long total = 0; |
| 626 | for (String iface : getMobileIfaces()) { |
Chenbo Feng | 905f034 | 2018-01-25 11:43:52 -0800 | [diff] [blame] | 627 | total += addIfSupported(getTxPackets(iface)); |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 628 | } |
| 629 | return total; |
| 630 | } |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 631 | |
| 632 | /** |
Jeff Sharkey | 39fb9cd | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 633 | * Return number of packets received across mobile networks since device |
| 634 | * boot. Counts packets across all mobile network interfaces, and always |
| 635 | * increases monotonically since device boot. Statistics are measured at the |
| 636 | * network layer, so they include both TCP and UDP usage. |
| 637 | * <p> |
| 638 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 639 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 640 | */ |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 641 | public static long getMobileRxPackets() { |
| 642 | long total = 0; |
| 643 | for (String iface : getMobileIfaces()) { |
Chenbo Feng | 905f034 | 2018-01-25 11:43:52 -0800 | [diff] [blame] | 644 | total += addIfSupported(getRxPackets(iface)); |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 645 | } |
| 646 | return total; |
| 647 | } |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 648 | |
| 649 | /** |
Jeff Sharkey | 39fb9cd | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 650 | * Return number of bytes transmitted across mobile networks since device |
| 651 | * boot. Counts packets across all mobile network interfaces, and always |
| 652 | * increases monotonically since device boot. Statistics are measured at the |
| 653 | * network layer, so they include both TCP and UDP usage. |
| 654 | * <p> |
| 655 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 656 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 657 | */ |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 658 | public static long getMobileTxBytes() { |
| 659 | long total = 0; |
| 660 | for (String iface : getMobileIfaces()) { |
Chenbo Feng | 905f034 | 2018-01-25 11:43:52 -0800 | [diff] [blame] | 661 | total += addIfSupported(getTxBytes(iface)); |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 662 | } |
| 663 | return total; |
| 664 | } |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 665 | |
| 666 | /** |
Jeff Sharkey | 39fb9cd | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 667 | * Return number of bytes received across mobile networks since device boot. |
| 668 | * Counts packets across all mobile network interfaces, and always increases |
| 669 | * monotonically since device boot. Statistics are measured at the network |
| 670 | * layer, so they include both TCP and UDP usage. |
| 671 | * <p> |
| 672 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 673 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 674 | */ |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 675 | public static long getMobileRxBytes() { |
| 676 | long total = 0; |
| 677 | for (String iface : getMobileIfaces()) { |
Chenbo Feng | 905f034 | 2018-01-25 11:43:52 -0800 | [diff] [blame] | 678 | total += addIfSupported(getRxBytes(iface)); |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 679 | } |
| 680 | return total; |
| 681 | } |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 682 | |
Jeff Sharkey | 9e766ac | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 683 | /** {@hide} */ |
Mathew Inwood | fe2fed7 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 684 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Jeff Sharkey | 9e766ac | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 685 | public static long getMobileTcpRxPackets() { |
Maciej Żenczykowski | 66645a8 | 2023-08-12 08:26:44 +0000 | [diff] [blame] | 686 | return UNSUPPORTED; |
Jeff Sharkey | 9e766ac | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /** {@hide} */ |
Mathew Inwood | fe2fed7 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 690 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Jeff Sharkey | 9e766ac | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 691 | public static long getMobileTcpTxPackets() { |
Maciej Żenczykowski | 66645a8 | 2023-08-12 08:26:44 +0000 | [diff] [blame] | 692 | return UNSUPPORTED; |
Jeff Sharkey | 9e766ac | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 693 | } |
| 694 | |
Aaron Huang | 443651d | 2019-09-27 22:31:22 +0800 | [diff] [blame] | 695 | /** |
junyulai | 3154d51 | 2020-09-29 14:19:24 +0800 | [diff] [blame] | 696 | * Return the number of packets transmitted on the specified interface since the interface |
| 697 | * was created. Statistics are measured at the network layer, so both TCP and |
Aaron Huang | 443651d | 2019-09-27 22:31:22 +0800 | [diff] [blame] | 698 | * UDP usage are included. |
| 699 | * |
junyulai | 3154d51 | 2020-09-29 14:19:24 +0800 | [diff] [blame] | 700 | * Note that the returned values are partial statistics that do not count data from several |
| 701 | * sources and do not apply several adjustments that are necessary for correctness, such |
| 702 | * as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to |
| 703 | * determine whether traffic is being transferred on the specific interface but are not a |
| 704 | * substitute for the more accurate statistics provided by the {@link NetworkStatsManager} |
| 705 | * APIs. |
| 706 | * |
Aaron Huang | 443651d | 2019-09-27 22:31:22 +0800 | [diff] [blame] | 707 | * @param iface The name of the interface. |
| 708 | * @return The number of transmitted packets. |
| 709 | */ |
| 710 | public static long getTxPackets(@NonNull String iface) { |
Chenbo Feng | aa7ba31 | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 711 | try { |
| 712 | return getStatsService().getIfaceStats(iface, TYPE_TX_PACKETS); |
| 713 | } catch (RemoteException e) { |
| 714 | throw e.rethrowFromSystemServer(); |
| 715 | } |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 716 | } |
Irfan Sheriff | 042f4e1 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 717 | |
Aaron Huang | 443651d | 2019-09-27 22:31:22 +0800 | [diff] [blame] | 718 | /** |
junyulai | 3154d51 | 2020-09-29 14:19:24 +0800 | [diff] [blame] | 719 | * Return the number of packets received on the specified interface since the interface was |
| 720 | * created. Statistics are measured at the network layer, so both TCP |
Aaron Huang | 443651d | 2019-09-27 22:31:22 +0800 | [diff] [blame] | 721 | * and UDP usage are included. |
| 722 | * |
junyulai | 3154d51 | 2020-09-29 14:19:24 +0800 | [diff] [blame] | 723 | * Note that the returned values are partial statistics that do not count data from several |
| 724 | * sources and do not apply several adjustments that are necessary for correctness, such |
| 725 | * as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to |
| 726 | * determine whether traffic is being transferred on the specific interface but are not a |
| 727 | * substitute for the more accurate statistics provided by the {@link NetworkStatsManager} |
| 728 | * APIs. |
| 729 | * |
Aaron Huang | 443651d | 2019-09-27 22:31:22 +0800 | [diff] [blame] | 730 | * @param iface The name of the interface. |
| 731 | * @return The number of received packets. |
| 732 | */ |
| 733 | public static long getRxPackets(@NonNull String iface) { |
Chenbo Feng | aa7ba31 | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 734 | try { |
| 735 | return getStatsService().getIfaceStats(iface, TYPE_RX_PACKETS); |
| 736 | } catch (RemoteException e) { |
| 737 | throw e.rethrowFromSystemServer(); |
| 738 | } |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 739 | } |
Irfan Sheriff | 042f4e1 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 740 | |
junyulai | 3154d51 | 2020-09-29 14:19:24 +0800 | [diff] [blame] | 741 | /** |
| 742 | * Return the number of bytes transmitted on the specified interface since the interface |
| 743 | * was created. Statistics are measured at the network layer, so both TCP and |
| 744 | * UDP usage are included. |
| 745 | * |
| 746 | * Note that the returned values are partial statistics that do not count data from several |
| 747 | * sources and do not apply several adjustments that are necessary for correctness, such |
| 748 | * as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to |
| 749 | * determine whether traffic is being transferred on the specific interface but are not a |
| 750 | * substitute for the more accurate statistics provided by the {@link NetworkStatsManager} |
| 751 | * APIs. |
| 752 | * |
| 753 | * @param iface The name of the interface. |
| 754 | * @return The number of transmitted bytes. |
| 755 | */ |
| 756 | public static long getTxBytes(@NonNull String iface) { |
Chenbo Feng | aa7ba31 | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 757 | try { |
| 758 | return getStatsService().getIfaceStats(iface, TYPE_TX_BYTES); |
| 759 | } catch (RemoteException e) { |
| 760 | throw e.rethrowFromSystemServer(); |
| 761 | } |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 762 | } |
Irfan Sheriff | 042f4e1 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 763 | |
junyulai | 3154d51 | 2020-09-29 14:19:24 +0800 | [diff] [blame] | 764 | /** |
| 765 | * Return the number of bytes received on the specified interface since the interface |
| 766 | * was created. Statistics are measured at the network layer, so both TCP |
| 767 | * and UDP usage are included. |
| 768 | * |
| 769 | * Note that the returned values are partial statistics that do not count data from several |
| 770 | * sources and do not apply several adjustments that are necessary for correctness, such |
| 771 | * as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to |
| 772 | * determine whether traffic is being transferred on the specific interface but are not a |
| 773 | * substitute for the more accurate statistics provided by the {@link NetworkStatsManager} |
| 774 | * APIs. |
| 775 | * |
| 776 | * @param iface The name of the interface. |
| 777 | * @return The number of received bytes. |
| 778 | */ |
| 779 | public static long getRxBytes(@NonNull String iface) { |
Chenbo Feng | aa7ba31 | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 780 | try { |
| 781 | return getStatsService().getIfaceStats(iface, TYPE_RX_BYTES); |
| 782 | } catch (RemoteException e) { |
| 783 | throw e.rethrowFromSystemServer(); |
| 784 | } |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 785 | } |
Irfan Sheriff | 042f4e1 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 786 | |
Benedict Wong | 083faee | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 787 | /** {@hide} */ |
| 788 | @TestApi |
| 789 | public static long getLoopbackTxPackets() { |
Chenbo Feng | aa7ba31 | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 790 | try { |
| 791 | return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_TX_PACKETS); |
| 792 | } catch (RemoteException e) { |
| 793 | throw e.rethrowFromSystemServer(); |
| 794 | } |
Benedict Wong | 083faee | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /** {@hide} */ |
| 798 | @TestApi |
| 799 | public static long getLoopbackRxPackets() { |
Chenbo Feng | aa7ba31 | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 800 | try { |
| 801 | return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_RX_PACKETS); |
| 802 | } catch (RemoteException e) { |
| 803 | throw e.rethrowFromSystemServer(); |
| 804 | } |
Benedict Wong | 083faee | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /** {@hide} */ |
| 808 | @TestApi |
| 809 | public static long getLoopbackTxBytes() { |
Chenbo Feng | aa7ba31 | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 810 | try { |
| 811 | return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_TX_BYTES); |
| 812 | } catch (RemoteException e) { |
| 813 | throw e.rethrowFromSystemServer(); |
| 814 | } |
Benedict Wong | 083faee | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /** {@hide} */ |
| 818 | @TestApi |
| 819 | public static long getLoopbackRxBytes() { |
Chenbo Feng | aa7ba31 | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 820 | try { |
| 821 | return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_RX_BYTES); |
| 822 | } catch (RemoteException e) { |
| 823 | throw e.rethrowFromSystemServer(); |
| 824 | } |
Benedict Wong | 083faee | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 825 | } |
| 826 | |
Irfan Sheriff | 042f4e1 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 827 | /** |
Jeff Sharkey | 39fb9cd | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 828 | * Return number of packets transmitted since device boot. Counts packets |
| 829 | * across all network interfaces, and always increases monotonically since |
| 830 | * device boot. Statistics are measured at the network layer, so they |
| 831 | * include both TCP and UDP usage. |
| 832 | * <p> |
| 833 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 834 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 835 | */ |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 836 | public static long getTotalTxPackets() { |
Chenbo Feng | aa7ba31 | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 837 | try { |
| 838 | return getStatsService().getTotalStats(TYPE_TX_PACKETS); |
| 839 | } catch (RemoteException e) { |
| 840 | throw e.rethrowFromSystemServer(); |
| 841 | } |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 842 | } |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 843 | |
| 844 | /** |
Jeff Sharkey | 39fb9cd | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 845 | * Return number of packets received since device boot. Counts packets |
| 846 | * across all network interfaces, and always increases monotonically since |
| 847 | * device boot. Statistics are measured at the network layer, so they |
| 848 | * include both TCP and UDP usage. |
| 849 | * <p> |
| 850 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 851 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 852 | */ |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 853 | public static long getTotalRxPackets() { |
Chenbo Feng | aa7ba31 | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 854 | try { |
| 855 | return getStatsService().getTotalStats(TYPE_RX_PACKETS); |
| 856 | } catch (RemoteException e) { |
| 857 | throw e.rethrowFromSystemServer(); |
| 858 | } |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 859 | } |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 860 | |
| 861 | /** |
Jeff Sharkey | 39fb9cd | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 862 | * Return number of bytes transmitted since device boot. Counts packets |
| 863 | * across all network interfaces, and always increases monotonically since |
| 864 | * device boot. Statistics are measured at the network layer, so they |
| 865 | * include both TCP and UDP usage. |
| 866 | * <p> |
| 867 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 868 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 869 | */ |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 870 | public static long getTotalTxBytes() { |
Chenbo Feng | aa7ba31 | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 871 | try { |
| 872 | return getStatsService().getTotalStats(TYPE_TX_BYTES); |
| 873 | } catch (RemoteException e) { |
| 874 | throw e.rethrowFromSystemServer(); |
| 875 | } |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 876 | } |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 877 | |
| 878 | /** |
Jeff Sharkey | 39fb9cd | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 879 | * Return number of bytes received since device boot. Counts packets across |
| 880 | * all network interfaces, and always increases monotonically since device |
| 881 | * boot. Statistics are measured at the network layer, so they include both |
| 882 | * TCP and UDP usage. |
| 883 | * <p> |
| 884 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 885 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 886 | */ |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 887 | public static long getTotalRxBytes() { |
Chenbo Feng | aa7ba31 | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 888 | try { |
| 889 | return getStatsService().getTotalStats(TYPE_RX_BYTES); |
| 890 | } catch (RemoteException e) { |
| 891 | throw e.rethrowFromSystemServer(); |
| 892 | } |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 893 | } |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 894 | |
| 895 | /** |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 896 | * Return number of bytes transmitted by the given UID since device boot. |
| 897 | * Counts packets across all network interfaces, and always increases |
| 898 | * monotonically since device boot. Statistics are measured at the network |
| 899 | * layer, so they include both TCP and UDP usage. |
| 900 | * <p> |
Jeff Sharkey | 27c8a07 | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 901 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 902 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
| 903 | * <p> |
| 904 | * Starting in {@link android.os.Build.VERSION_CODES#N} this will only |
| 905 | * report traffic statistics for the calling UID. It will return |
| 906 | * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access |
| 907 | * historical network statistics belonging to other UIDs, use |
| 908 | * {@link NetworkStatsManager}. |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 909 | * |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 910 | * @see android.os.Process#myUid() |
| 911 | * @see android.content.pm.ApplicationInfo#uid |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 912 | */ |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 913 | public static long getUidTxBytes(int uid) { |
Chenbo Feng | d80a6ed | 2019-06-17 16:22:28 -0700 | [diff] [blame] | 914 | try { |
| 915 | return getStatsService().getUidStats(uid, TYPE_TX_BYTES); |
| 916 | } catch (RemoteException e) { |
| 917 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 27c8a07 | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 918 | } |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 919 | } |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 920 | |
| 921 | /** |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 922 | * Return number of bytes received by the given UID since device boot. |
| 923 | * Counts packets across all network interfaces, and always increases |
| 924 | * monotonically since device boot. Statistics are measured at the network |
| 925 | * layer, so they include both TCP and UDP usage. |
| 926 | * <p> |
Dianne Hackborn | e9d7207 | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 927 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 928 | * {@link #UNSUPPORTED} on devices where statistics aren't available. |
Jeff Sharkey | 27c8a07 | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 929 | * <p> |
| 930 | * Starting in {@link android.os.Build.VERSION_CODES#N} this will only |
| 931 | * report traffic statistics for the calling UID. It will return |
| 932 | * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access |
| 933 | * historical network statistics belonging to other UIDs, use |
| 934 | * {@link NetworkStatsManager}. |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 935 | * |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 936 | * @see android.os.Process#myUid() |
| 937 | * @see android.content.pm.ApplicationInfo#uid |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 938 | */ |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 939 | public static long getUidRxBytes(int uid) { |
Chenbo Feng | d80a6ed | 2019-06-17 16:22:28 -0700 | [diff] [blame] | 940 | try { |
| 941 | return getStatsService().getUidStats(uid, TYPE_RX_BYTES); |
| 942 | } catch (RemoteException e) { |
| 943 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 27c8a07 | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 944 | } |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 945 | } |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 946 | |
| 947 | /** |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 948 | * Return number of packets transmitted by the given UID since device boot. |
| 949 | * Counts packets across all network interfaces, and always increases |
| 950 | * monotonically since device boot. Statistics are measured at the network |
| 951 | * layer, so they include both TCP and UDP usage. |
| 952 | * <p> |
Dianne Hackborn | e9d7207 | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 953 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 954 | * {@link #UNSUPPORTED} on devices where statistics aren't available. |
Jeff Sharkey | 27c8a07 | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 955 | * <p> |
| 956 | * Starting in {@link android.os.Build.VERSION_CODES#N} this will only |
| 957 | * report traffic statistics for the calling UID. It will return |
| 958 | * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access |
| 959 | * historical network statistics belonging to other UIDs, use |
| 960 | * {@link NetworkStatsManager}. |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 961 | * |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 962 | * @see android.os.Process#myUid() |
| 963 | * @see android.content.pm.ApplicationInfo#uid |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 964 | */ |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 965 | public static long getUidTxPackets(int uid) { |
Chenbo Feng | d80a6ed | 2019-06-17 16:22:28 -0700 | [diff] [blame] | 966 | try { |
| 967 | return getStatsService().getUidStats(uid, TYPE_TX_PACKETS); |
| 968 | } catch (RemoteException e) { |
| 969 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 27c8a07 | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 970 | } |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 971 | } |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 972 | |
| 973 | /** |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 974 | * Return number of packets received by the given UID since device boot. |
| 975 | * Counts packets across all network interfaces, and always increases |
| 976 | * monotonically since device boot. Statistics are measured at the network |
| 977 | * layer, so they include both TCP and UDP usage. |
| 978 | * <p> |
Dianne Hackborn | e9d7207 | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 979 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 980 | * {@link #UNSUPPORTED} on devices where statistics aren't available. |
Jeff Sharkey | 27c8a07 | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 981 | * <p> |
| 982 | * Starting in {@link android.os.Build.VERSION_CODES#N} this will only |
| 983 | * report traffic statistics for the calling UID. It will return |
| 984 | * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access |
| 985 | * historical network statistics belonging to other UIDs, use |
| 986 | * {@link NetworkStatsManager}. |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 987 | * |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 988 | * @see android.os.Process#myUid() |
| 989 | * @see android.content.pm.ApplicationInfo#uid |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 990 | */ |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 991 | public static long getUidRxPackets(int uid) { |
Chenbo Feng | d80a6ed | 2019-06-17 16:22:28 -0700 | [diff] [blame] | 992 | try { |
| 993 | return getStatsService().getUidStats(uid, TYPE_RX_PACKETS); |
| 994 | } catch (RemoteException e) { |
| 995 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 27c8a07 | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 996 | } |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 997 | } |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 998 | |
| 999 | /** |
Dianne Hackborn | e9d7207 | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 1000 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1001 | * transport layer statistics are no longer available, and will |
| 1002 | * always return {@link #UNSUPPORTED}. |
| 1003 | * @see #getUidTxBytes(int) |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1004 | */ |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1005 | @Deprecated |
| 1006 | public static long getUidTcpTxBytes(int uid) { |
| 1007 | return UNSUPPORTED; |
| 1008 | } |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1009 | |
| 1010 | /** |
Dianne Hackborn | e9d7207 | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 1011 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1012 | * transport layer statistics are no longer available, and will |
| 1013 | * always return {@link #UNSUPPORTED}. |
| 1014 | * @see #getUidRxBytes(int) |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1015 | */ |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1016 | @Deprecated |
| 1017 | public static long getUidTcpRxBytes(int uid) { |
| 1018 | return UNSUPPORTED; |
| 1019 | } |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1020 | |
| 1021 | /** |
Dianne Hackborn | e9d7207 | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 1022 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1023 | * transport layer statistics are no longer available, and will |
| 1024 | * always return {@link #UNSUPPORTED}. |
| 1025 | * @see #getUidTxBytes(int) |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1026 | */ |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1027 | @Deprecated |
| 1028 | public static long getUidUdpTxBytes(int uid) { |
| 1029 | return UNSUPPORTED; |
| 1030 | } |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1031 | |
| 1032 | /** |
Dianne Hackborn | e9d7207 | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 1033 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1034 | * transport layer statistics are no longer available, and will |
| 1035 | * always return {@link #UNSUPPORTED}. |
| 1036 | * @see #getUidRxBytes(int) |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1037 | */ |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1038 | @Deprecated |
| 1039 | public static long getUidUdpRxBytes(int uid) { |
| 1040 | return UNSUPPORTED; |
| 1041 | } |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1042 | |
| 1043 | /** |
Dianne Hackborn | e9d7207 | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 1044 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1045 | * transport layer statistics are no longer available, and will |
| 1046 | * always return {@link #UNSUPPORTED}. |
| 1047 | * @see #getUidTxPackets(int) |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1048 | */ |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1049 | @Deprecated |
| 1050 | public static long getUidTcpTxSegments(int uid) { |
| 1051 | return UNSUPPORTED; |
| 1052 | } |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1053 | |
| 1054 | /** |
Dianne Hackborn | e9d7207 | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 1055 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1056 | * transport layer statistics are no longer available, and will |
| 1057 | * always return {@link #UNSUPPORTED}. |
| 1058 | * @see #getUidRxPackets(int) |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1059 | */ |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1060 | @Deprecated |
| 1061 | public static long getUidTcpRxSegments(int uid) { |
| 1062 | return UNSUPPORTED; |
| 1063 | } |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1064 | |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1065 | /** |
Dianne Hackborn | e9d7207 | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 1066 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1067 | * transport layer statistics are no longer available, and will |
| 1068 | * always return {@link #UNSUPPORTED}. |
| 1069 | * @see #getUidTxPackets(int) |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1070 | */ |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1071 | @Deprecated |
| 1072 | public static long getUidUdpTxPackets(int uid) { |
| 1073 | return UNSUPPORTED; |
| 1074 | } |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1075 | |
| 1076 | /** |
Dianne Hackborn | e9d7207 | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 1077 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1078 | * transport layer statistics are no longer available, and will |
| 1079 | * always return {@link #UNSUPPORTED}. |
| 1080 | * @see #getUidRxPackets(int) |
Ashish Sharma | df852d8 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 1081 | */ |
Jeff Sharkey | 5fee2c9 | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 1082 | @Deprecated |
| 1083 | public static long getUidUdpRxPackets(int uid) { |
| 1084 | return UNSUPPORTED; |
| 1085 | } |
Jeff Sharkey | 33936ac | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 1086 | |
| 1087 | /** |
| 1088 | * Return detailed {@link NetworkStats} for the current UID. Requires no |
| 1089 | * special permission. |
| 1090 | */ |
Jeff Sharkey | 7407e09 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 1091 | private static NetworkStats getDataLayerSnapshotForUid(Context context) { |
Jeff Sharkey | 39fb9cd | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 1092 | // TODO: take snapshot locally, since proc file is now visible |
Jeff Sharkey | 33936ac | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 1093 | final int uid = android.os.Process.myUid(); |
| 1094 | try { |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 1095 | return getStatsService().getDataLayerSnapshotForUid(uid); |
Jeff Sharkey | 33936ac | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 1096 | } catch (RemoteException e) { |
Jeff Sharkey | bc08d07 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1097 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 33936ac | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 1098 | } |
| 1099 | } |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 1100 | |
| 1101 | /** |
| 1102 | * Return set of any ifaces associated with mobile networks since boot. |
| 1103 | * Interfaces are never removed from this list, so counters should always be |
| 1104 | * monotonic. |
| 1105 | */ |
Chalard Jean | 3cfb499 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 1106 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 1107 | private static String[] getMobileIfaces() { |
| 1108 | try { |
| 1109 | return getStatsService().getMobileIfaces(); |
| 1110 | } catch (RemoteException e) { |
Jeff Sharkey | bc08d07 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1111 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 8308b9b | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
junyulai | ea23632 | 2020-10-06 11:59:56 +0800 | [diff] [blame] | 1115 | // NOTE: keep these in sync with {@code com_android_server_net_NetworkStatsService.cpp}. |
| 1116 | /** {@hide} */ |
| 1117 | public static final int TYPE_RX_BYTES = 0; |
| 1118 | /** {@hide} */ |
| 1119 | public static final int TYPE_RX_PACKETS = 1; |
| 1120 | /** {@hide} */ |
| 1121 | public static final int TYPE_TX_BYTES = 2; |
| 1122 | /** {@hide} */ |
| 1123 | public static final int TYPE_TX_PACKETS = 3; |
Ken Shirriff | ae52115 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 1124 | } |