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