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