Ken Shirriff | f7d0b01 | 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 | 29667d2 | 2019-09-27 22:31:22 +0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Jeff Sharkey | a4239cf | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 20 | import android.annotation.SuppressLint; |
Christopher Tate | a2496de | 2014-08-06 14:19:56 -0700 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 22 | import android.annotation.TestApi; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 23 | import android.app.DownloadManager; |
| 24 | import android.app.backup.BackupManager; |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 25 | import android.app.usage.NetworkStatsManager; |
Artur Satayev | 33f9217 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 26 | import android.compat.annotation.UnsupportedAppUsage; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 27 | import android.content.Context; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 28 | import android.media.MediaPlayer; |
Chalard Jean | 0bb53dbb | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 29 | import android.os.Build; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 30 | import android.os.RemoteException; |
| 31 | import android.os.ServiceManager; |
Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 32 | import android.util.DataUnit; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 33 | |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 34 | import com.android.server.NetworkManagementSocketTagger; |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 35 | |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 36 | import dalvik.system.SocketTagger; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 37 | |
Jeff Sharkey | a4239cf | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 38 | import java.io.FileDescriptor; |
| 39 | import java.io.IOException; |
Jeff Sharkey | f0d7633 | 2015-12-04 15:21:52 -0700 | [diff] [blame] | 40 | import java.net.DatagramSocket; |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 41 | import java.net.Socket; |
| 42 | import java.net.SocketException; |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 45 | * Class that provides network traffic statistics. These statistics include |
Dan Egnor | 2b4abcd | 2010-04-07 17:30:50 -0700 | [diff] [blame] | 46 | * bytes transmitted and received and network packets transmitted and received, |
| 47 | * over all interfaces, over the mobile interface, and on a per-UID basis. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 48 | * <p> |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 49 | * These statistics may not be available on all platforms. If the statistics are |
| 50 | * not supported by this device, {@link #UNSUPPORTED} will be returned. |
| 51 | * <p> |
| 52 | * Note that the statistics returned by this class reset and start from zero |
| 53 | * after every reboot. To access more robust historical network statistics data, |
| 54 | * use {@link NetworkStatsManager} instead. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 55 | */ |
| 56 | public class TrafficStats { |
| 57 | /** |
| 58 | * The return value to indicate that the device does not support the statistic. |
| 59 | */ |
| 60 | public final static int UNSUPPORTED = -1; |
| 61 | |
Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 62 | /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */ |
| 63 | @Deprecated |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 64 | public static final long KB_IN_BYTES = 1024; |
Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 65 | /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */ |
| 66 | @Deprecated |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 67 | public static final long MB_IN_BYTES = KB_IN_BYTES * 1024; |
Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 68 | /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */ |
| 69 | @Deprecated |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 70 | public static final long GB_IN_BYTES = MB_IN_BYTES * 1024; |
Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 71 | /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */ |
| 72 | @Deprecated |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 73 | public static final long TB_IN_BYTES = GB_IN_BYTES * 1024; |
Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 74 | /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */ |
| 75 | @Deprecated |
Jeff Sharkey | b521fea | 2015-06-15 21:09:10 -0700 | [diff] [blame] | 76 | public static final long PB_IN_BYTES = TB_IN_BYTES * 1024; |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 77 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 78 | /** |
Jeff Sharkey | b09540f | 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 | cdd02c5d | 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 | d27a172 | 2019-11-15 17:15:01 +0800 | [diff] [blame] | 92 | public static final int UID_TETHERING = NetworkStats.UID_TETHERING; |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
Chalard Jean | 896245a | 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 | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 131 | * Default tag value for {@link DownloadManager} traffic. |
| 132 | * |
| 133 | * @hide |
| 134 | */ |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 135 | public static final int TAG_SYSTEM_DOWNLOAD = 0xFFFFFF01; |
Jeff Sharkey | 4414cea | 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 | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 142 | public static final int TAG_SYSTEM_MEDIA = 0xFFFFFF02; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
Christopher Tate | 0a61b36 | 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 | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 147 | * |
| 148 | * @hide |
| 149 | */ |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 150 | public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 151 | |
Christopher Tate | 0a61b36 | 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 | 763a140 | 2016-11-21 12:14:50 -0700 | [diff] [blame] | 160 | /** |
Jeff Sharkey | d62e3cb | 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 | 763a140 | 2016-11-21 12:14:50 -0700 | [diff] [blame] | 163 | * |
| 164 | * @hide |
| 165 | */ |
Jeff Sharkey | d62e3cb | 2017-08-11 15:04:12 -0600 | [diff] [blame] | 166 | public static final int TAG_SYSTEM_APP = 0xFFFFFF05; |
Jeff Sharkey | f23a5e1 | 2017-06-26 19:50:41 -0600 | [diff] [blame] | 167 | |
Chalard Jean | 896245a | 2019-04-09 11:16:56 +0900 | [diff] [blame] | 168 | // TODO : remove this constant when Wifi code is updated |
Jeff Sharkey | f23a5e1 | 2017-06-26 19:50:41 -0600 | [diff] [blame] | 169 | /** @hide */ |
Jeff Sharkey | f23a5e1 | 2017-06-26 19:50:41 -0600 | [diff] [blame] | 170 | public static final int TAG_SYSTEM_PROBE = 0xFFFFFF42; |
Jeff Sharkey | 763a140 | 2016-11-21 12:14:50 -0700 | [diff] [blame] | 171 | |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 172 | private static INetworkStatsService sStatsService; |
| 173 | |
Chalard Jean | 0bb53dbb | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 174 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 175 | private synchronized static INetworkStatsService getStatsService() { |
| 176 | if (sStatsService == null) { |
| 177 | sStatsService = INetworkStatsService.Stub.asInterface( |
| 178 | ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); |
| 179 | } |
| 180 | return sStatsService; |
| 181 | } |
| 182 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 183 | /** |
Jeff Sharkey | eedcb95 | 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 | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 194 | private static final String LOOPBACK_IFACE = "lo"; |
| 195 | |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 196 | /** |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 197 | * Set active tag to use when accounting {@link Socket} traffic originating |
| 198 | * from the current thread. Only one active tag per thread is supported. |
| 199 | * <p> |
| 200 | * Changes only take effect during subsequent calls to |
| 201 | * {@link #tagSocket(Socket)}. |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 202 | * <p> |
| 203 | * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and |
| 204 | * used internally by system services like {@link DownloadManager} when |
| 205 | * performing traffic on behalf of an application. |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 206 | * |
| 207 | * @see #clearThreadStatsTag() |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 208 | */ |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 209 | public static void setThreadStatsTag(int tag) { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 210 | NetworkManagementSocketTagger.setThreadSocketStatsTag(tag); |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 213 | /** |
Jeff Sharkey | 11cadda | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 214 | * Set active tag to use when accounting {@link Socket} traffic originating |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 215 | * from the current thread. Only one active tag per thread is supported. |
| 216 | * <p> |
| 217 | * Changes only take effect during subsequent calls to |
| 218 | * {@link #tagSocket(Socket)}. |
| 219 | * <p> |
| 220 | * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and |
| 221 | * used internally by system services like {@link DownloadManager} when |
| 222 | * performing traffic on behalf of an application. |
| 223 | * |
| 224 | * @return the current tag for the calling thread, which can be used to |
| 225 | * restore any existing values after a nested operation is finished |
| 226 | */ |
| 227 | public static int getAndSetThreadStatsTag(int tag) { |
| 228 | return NetworkManagementSocketTagger.setThreadSocketStatsTag(tag); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Set active tag to use when accounting {@link Socket} traffic originating |
Jeff Sharkey | 11cadda | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 233 | * from the current thread. The tag used internally is well-defined to |
| 234 | * distinguish all backup-related traffic. |
| 235 | * |
Christopher Tate | a2496de | 2014-08-06 14:19:56 -0700 | [diff] [blame] | 236 | * @hide |
| 237 | */ |
| 238 | @SystemApi |
| 239 | public static void setThreadStatsTagBackup() { |
| 240 | setThreadStatsTag(TAG_SYSTEM_BACKUP); |
| 241 | } |
| 242 | |
| 243 | /** |
Jeff Sharkey | 11cadda | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 244 | * Set active tag to use when accounting {@link Socket} traffic originating |
| 245 | * from the current thread. The tag used internally is well-defined to |
| 246 | * distinguish all restore-related traffic. |
| 247 | * |
Christopher Tate | 0a61b36 | 2015-11-10 10:49:20 -0800 | [diff] [blame] | 248 | * @hide |
| 249 | */ |
| 250 | @SystemApi |
| 251 | public static void setThreadStatsTagRestore() { |
| 252 | setThreadStatsTag(TAG_SYSTEM_RESTORE); |
| 253 | } |
| 254 | |
| 255 | /** |
Jeff Sharkey | f23a5e1 | 2017-06-26 19:50:41 -0600 | [diff] [blame] | 256 | * Set active tag to use when accounting {@link Socket} traffic originating |
| 257 | * from the current thread. The tag used internally is well-defined to |
Jeff Sharkey | d62e3cb | 2017-08-11 15:04:12 -0600 | [diff] [blame] | 258 | * distinguish all code (typically APKs) downloaded by an app store on |
| 259 | * behalf of the app, such as updates. |
Jeff Sharkey | f23a5e1 | 2017-06-26 19:50:41 -0600 | [diff] [blame] | 260 | * |
| 261 | * @hide |
| 262 | */ |
| 263 | @SystemApi |
Jeff Sharkey | d62e3cb | 2017-08-11 15:04:12 -0600 | [diff] [blame] | 264 | public static void setThreadStatsTagApp() { |
| 265 | setThreadStatsTag(TAG_SYSTEM_APP); |
Jeff Sharkey | f23a5e1 | 2017-06-26 19:50:41 -0600 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
Alon Albert | eaef351 | 2011-07-19 11:16:09 +0300 | [diff] [blame] | 269 | * Get the active tag used when accounting {@link Socket} traffic originating |
| 270 | * from the current thread. Only one active tag per thread is supported. |
| 271 | * {@link #tagSocket(Socket)}. |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 272 | * |
| 273 | * @see #setThreadStatsTag(int) |
Alon Albert | eaef351 | 2011-07-19 11:16:09 +0300 | [diff] [blame] | 274 | */ |
| 275 | public static int getThreadStatsTag() { |
| 276 | return NetworkManagementSocketTagger.getThreadSocketStatsTag(); |
| 277 | } |
| 278 | |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 279 | /** |
| 280 | * Clear any active tag set to account {@link Socket} traffic originating |
| 281 | * from the current thread. |
| 282 | * |
| 283 | * @see #setThreadStatsTag(int) |
| 284 | */ |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 285 | public static void clearThreadStatsTag() { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 286 | NetworkManagementSocketTagger.setThreadSocketStatsTag(-1); |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Set specific UID to use when accounting {@link Socket} traffic |
| 291 | * originating from the current thread. Designed for use when performing an |
Jeff Sharkey | 121d565 | 2018-03-26 13:11:33 -0600 | [diff] [blame] | 292 | * operation on behalf of another application, or when another application |
| 293 | * is performing operations on your behalf. |
| 294 | * <p> |
| 295 | * Any app can <em>accept</em> blame for traffic performed on a socket |
| 296 | * originally created by another app by calling this method with the |
| 297 | * {@link android.system.Os#getuid()} value. However, only apps holding the |
| 298 | * {@code android.Manifest.permission#UPDATE_DEVICE_STATS} permission may |
| 299 | * <em>assign</em> blame to another UIDs. |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 300 | * <p> |
| 301 | * Changes only take effect during subsequent calls to |
| 302 | * {@link #tagSocket(Socket)}. |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 303 | */ |
Jeff Sharkey | 121d565 | 2018-03-26 13:11:33 -0600 | [diff] [blame] | 304 | @SuppressLint("Doclava125") |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 305 | public static void setThreadStatsUid(int uid) { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 306 | NetworkManagementSocketTagger.setThreadSocketStatsUid(uid); |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Jeff Sharkey | 11cadda | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 309 | /** |
Jeff Sharkey | 121d565 | 2018-03-26 13:11:33 -0600 | [diff] [blame] | 310 | * Get the active UID used when accounting {@link Socket} traffic originating |
| 311 | * from the current thread. Only one active tag per thread is supported. |
| 312 | * {@link #tagSocket(Socket)}. |
| 313 | * |
| 314 | * @see #setThreadStatsUid(int) |
| 315 | */ |
| 316 | public static int getThreadStatsUid() { |
| 317 | return NetworkManagementSocketTagger.getThreadSocketStatsUid(); |
| 318 | } |
| 319 | |
| 320 | /** |
Jeff Sharkey | a4239cf | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 321 | * Set specific UID to use when accounting {@link Socket} traffic |
| 322 | * originating from the current thread as the calling UID. Designed for use |
| 323 | * when another application is performing operations on your behalf. |
| 324 | * <p> |
| 325 | * Changes only take effect during subsequent calls to |
| 326 | * {@link #tagSocket(Socket)}. |
Jeff Sharkey | 121d565 | 2018-03-26 13:11:33 -0600 | [diff] [blame] | 327 | * |
| 328 | * @removed |
| 329 | * @deprecated use {@link #setThreadStatsUid(int)} instead. |
Jeff Sharkey | a4239cf | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 330 | */ |
Jeff Sharkey | 121d565 | 2018-03-26 13:11:33 -0600 | [diff] [blame] | 331 | @Deprecated |
Jeff Sharkey | a4239cf | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 332 | public static void setThreadStatsUidSelf() { |
| 333 | setThreadStatsUid(android.os.Process.myUid()); |
| 334 | } |
| 335 | |
| 336 | /** |
Jeff Sharkey | 11cadda | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 337 | * Clear any active UID set to account {@link Socket} traffic originating |
| 338 | * from the current thread. |
| 339 | * |
| 340 | * @see #setThreadStatsUid(int) |
Jeff Sharkey | 11cadda | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 341 | */ |
Jeff Sharkey | a4239cf | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 342 | @SuppressLint("Doclava125") |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 343 | public static void clearThreadStatsUid() { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 344 | NetworkManagementSocketTagger.setThreadSocketStatsUid(-1); |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Tag the given {@link Socket} with any statistics parameters active for |
| 349 | * the current thread. Subsequent calls always replace any existing |
| 350 | * parameters. When finished, call {@link #untagSocket(Socket)} to remove |
| 351 | * statistics parameters. |
| 352 | * |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 353 | * @see #setThreadStatsTag(int) |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 354 | */ |
| 355 | public static void tagSocket(Socket socket) throws SocketException { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 356 | SocketTagger.get().tag(socket); |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Remove any statistics parameters from the given {@link Socket}. |
kopriva | 5ad001e | 2018-07-23 18:19:39 -0700 | [diff] [blame] | 361 | * <p> |
| 362 | * In Android 8.1 (API level 27) and lower, a socket is automatically |
| 363 | * untagged when it's sent to another process using binder IPC with a |
| 364 | * {@code ParcelFileDescriptor} container. In Android 9.0 (API level 28) |
| 365 | * and higher, the socket tag is kept when the socket is sent to another |
| 366 | * process using binder IPC. You can mimic the previous behavior by |
| 367 | * calling {@code untagSocket()} before sending the socket to another |
| 368 | * process. |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 369 | */ |
| 370 | public static void untagSocket(Socket socket) throws SocketException { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 371 | SocketTagger.get().untag(socket); |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
Jeff Sharkey | f0d7633 | 2015-12-04 15:21:52 -0700 | [diff] [blame] | 375 | * Tag the given {@link DatagramSocket} with any statistics parameters |
| 376 | * active for the current thread. Subsequent calls always replace any |
| 377 | * existing parameters. When finished, call |
| 378 | * {@link #untagDatagramSocket(DatagramSocket)} to remove statistics |
| 379 | * parameters. |
| 380 | * |
| 381 | * @see #setThreadStatsTag(int) |
Jeff Sharkey | f0d7633 | 2015-12-04 15:21:52 -0700 | [diff] [blame] | 382 | */ |
| 383 | public static void tagDatagramSocket(DatagramSocket socket) throws SocketException { |
| 384 | SocketTagger.get().tag(socket); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Remove any statistics parameters from the given {@link DatagramSocket}. |
| 389 | */ |
| 390 | public static void untagDatagramSocket(DatagramSocket socket) throws SocketException { |
| 391 | SocketTagger.get().untag(socket); |
| 392 | } |
| 393 | |
| 394 | /** |
Jeff Sharkey | a4239cf | 2017-11-29 11:18:23 -0700 | [diff] [blame] | 395 | * Tag the given {@link FileDescriptor} socket with any statistics |
| 396 | * parameters active for the current thread. Subsequent calls always replace |
| 397 | * any existing parameters. When finished, call |
| 398 | * {@link #untagFileDescriptor(FileDescriptor)} to remove statistics |
| 399 | * parameters. |
| 400 | * |
| 401 | * @see #setThreadStatsTag(int) |
| 402 | */ |
| 403 | public static void tagFileDescriptor(FileDescriptor fd) throws IOException { |
| 404 | SocketTagger.get().tag(fd); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Remove any statistics parameters from the given {@link FileDescriptor} |
| 409 | * socket. |
| 410 | */ |
| 411 | public static void untagFileDescriptor(FileDescriptor fd) throws IOException { |
| 412 | SocketTagger.get().untag(fd); |
| 413 | } |
| 414 | |
| 415 | /** |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 416 | * Start profiling data usage for current UID. Only one profiling session |
| 417 | * can be active at a time. |
| 418 | * |
| 419 | * @hide |
| 420 | */ |
| 421 | public static void startDataProfiling(Context context) { |
| 422 | synchronized (sProfilingLock) { |
| 423 | if (sActiveProfilingStart != null) { |
| 424 | throw new IllegalStateException("already profiling data"); |
| 425 | } |
| 426 | |
| 427 | // take snapshot in time; we calculate delta later |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 428 | sActiveProfilingStart = getDataLayerSnapshotForUid(context); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Stop profiling data usage for current UID. |
| 434 | * |
| 435 | * @return Detailed {@link NetworkStats} of data that occurred since last |
| 436 | * {@link #startDataProfiling(Context)} call. |
| 437 | * @hide |
| 438 | */ |
| 439 | public static NetworkStats stopDataProfiling(Context context) { |
| 440 | synchronized (sProfilingLock) { |
| 441 | if (sActiveProfilingStart == null) { |
| 442 | throw new IllegalStateException("not profiling data"); |
| 443 | } |
| 444 | |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 445 | // subtract starting values and return delta |
| 446 | final NetworkStats profilingStop = getDataLayerSnapshotForUid(context); |
| 447 | final NetworkStats profilingDelta = NetworkStats.subtract( |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 448 | profilingStop, sActiveProfilingStart, null, null); |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 449 | sActiveProfilingStart = null; |
| 450 | return profilingDelta; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | /** |
Jeff Sharkey | 558a232 | 2011-08-24 15:42:09 -0700 | [diff] [blame] | 455 | * Increment count of network operations performed under the accounting tag |
| 456 | * currently active on the calling thread. This can be used to derive |
| 457 | * bytes-per-operation. |
| 458 | * |
| 459 | * @param operationCount Number of operations to increment count by. |
| 460 | */ |
| 461 | public static void incrementOperationCount(int operationCount) { |
| 462 | final int tag = getThreadStatsTag(); |
| 463 | incrementOperationCount(tag, operationCount); |
| 464 | } |
| 465 | |
| 466 | /** |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 467 | * Increment count of network operations performed under the given |
| 468 | * accounting tag. This can be used to derive bytes-per-operation. |
| 469 | * |
| 470 | * @param tag Accounting tag used in {@link #setThreadStatsTag(int)}. |
| 471 | * @param operationCount Number of operations to increment count by. |
| 472 | */ |
| 473 | public static void incrementOperationCount(int tag, int operationCount) { |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 474 | final int uid = android.os.Process.myUid(); |
| 475 | try { |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 476 | getStatsService().incrementOperationCount(uid, tag, operationCount); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 477 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 478 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 482 | /** {@hide} */ |
| 483 | public static void closeQuietly(INetworkStatsSession session) { |
| 484 | // TODO: move to NetworkStatsService once it exists |
| 485 | if (session != null) { |
| 486 | try { |
| 487 | session.close(); |
| 488 | } catch (RuntimeException rethrown) { |
| 489 | throw rethrown; |
| 490 | } catch (Exception ignored) { |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
Chenbo Feng | 20328e8 | 2018-01-25 11:43:52 -0800 | [diff] [blame] | 495 | private static long addIfSupported(long stat) { |
| 496 | return (stat == UNSUPPORTED) ? 0 : stat; |
| 497 | } |
| 498 | |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 499 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 500 | * Return number of packets transmitted across mobile networks since device |
| 501 | * boot. Counts packets across all mobile network interfaces, and always |
| 502 | * increases monotonically since device boot. Statistics are measured at the |
| 503 | * network layer, so they include both TCP and UDP usage. |
| 504 | * <p> |
| 505 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 506 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 507 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 508 | public static long getMobileTxPackets() { |
| 509 | long total = 0; |
| 510 | for (String iface : getMobileIfaces()) { |
Chenbo Feng | 20328e8 | 2018-01-25 11:43:52 -0800 | [diff] [blame] | 511 | total += addIfSupported(getTxPackets(iface)); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 512 | } |
| 513 | return total; |
| 514 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 515 | |
| 516 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 517 | * Return number of packets received across mobile networks since device |
| 518 | * boot. Counts packets across all mobile network interfaces, and always |
| 519 | * increases monotonically since device boot. Statistics are measured at the |
| 520 | * network layer, so they include both TCP and UDP usage. |
| 521 | * <p> |
| 522 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 523 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 524 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 525 | public static long getMobileRxPackets() { |
| 526 | long total = 0; |
| 527 | for (String iface : getMobileIfaces()) { |
Chenbo Feng | 20328e8 | 2018-01-25 11:43:52 -0800 | [diff] [blame] | 528 | total += addIfSupported(getRxPackets(iface)); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 529 | } |
| 530 | return total; |
| 531 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 532 | |
| 533 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 534 | * Return number of bytes transmitted across mobile networks since device |
| 535 | * boot. Counts packets across all mobile network interfaces, and always |
| 536 | * increases monotonically since device boot. Statistics are measured at the |
| 537 | * network layer, so they include both TCP and UDP usage. |
| 538 | * <p> |
| 539 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 540 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 541 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 542 | public static long getMobileTxBytes() { |
| 543 | long total = 0; |
| 544 | for (String iface : getMobileIfaces()) { |
Chenbo Feng | 20328e8 | 2018-01-25 11:43:52 -0800 | [diff] [blame] | 545 | total += addIfSupported(getTxBytes(iface)); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 546 | } |
| 547 | return total; |
| 548 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 549 | |
| 550 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 551 | * Return number of bytes received across mobile networks since device boot. |
| 552 | * Counts packets across all mobile network interfaces, and always increases |
| 553 | * monotonically since device boot. Statistics are measured at the network |
| 554 | * layer, so they include both TCP and UDP usage. |
| 555 | * <p> |
| 556 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 557 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 558 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 559 | public static long getMobileRxBytes() { |
| 560 | long total = 0; |
| 561 | for (String iface : getMobileIfaces()) { |
Chenbo Feng | 20328e8 | 2018-01-25 11:43:52 -0800 | [diff] [blame] | 562 | total += addIfSupported(getRxBytes(iface)); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 563 | } |
| 564 | return total; |
| 565 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 566 | |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 567 | /** {@hide} */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 568 | @UnsupportedAppUsage |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 569 | public static long getMobileTcpRxPackets() { |
| 570 | long total = 0; |
| 571 | for (String iface : getMobileIfaces()) { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 572 | long stat = UNSUPPORTED; |
| 573 | try { |
| 574 | stat = getStatsService().getIfaceStats(iface, TYPE_TCP_RX_PACKETS); |
| 575 | } catch (RemoteException e) { |
| 576 | throw e.rethrowFromSystemServer(); |
| 577 | } |
Chenbo Feng | 20328e8 | 2018-01-25 11:43:52 -0800 | [diff] [blame] | 578 | total += addIfSupported(stat); |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 579 | } |
| 580 | return total; |
| 581 | } |
| 582 | |
| 583 | /** {@hide} */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 584 | @UnsupportedAppUsage |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 585 | public static long getMobileTcpTxPackets() { |
| 586 | long total = 0; |
| 587 | for (String iface : getMobileIfaces()) { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 588 | long stat = UNSUPPORTED; |
| 589 | try { |
| 590 | stat = getStatsService().getIfaceStats(iface, TYPE_TCP_TX_PACKETS); |
| 591 | } catch (RemoteException e) { |
| 592 | throw e.rethrowFromSystemServer(); |
| 593 | } |
Chenbo Feng | 20328e8 | 2018-01-25 11:43:52 -0800 | [diff] [blame] | 594 | total += addIfSupported(stat); |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 595 | } |
| 596 | return total; |
| 597 | } |
| 598 | |
Aaron Huang | 29667d2 | 2019-09-27 22:31:22 +0800 | [diff] [blame] | 599 | /** |
| 600 | * Return the number of packets transmitted on the specified interface since |
| 601 | * device boot. Statistics are measured at the network layer, so both TCP and |
| 602 | * UDP usage are included. |
| 603 | * |
| 604 | * @param iface The name of the interface. |
| 605 | * @return The number of transmitted packets. |
| 606 | */ |
| 607 | public static long getTxPackets(@NonNull String iface) { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 608 | try { |
| 609 | return getStatsService().getIfaceStats(iface, TYPE_TX_PACKETS); |
| 610 | } catch (RemoteException e) { |
| 611 | throw e.rethrowFromSystemServer(); |
| 612 | } |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 613 | } |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 614 | |
Aaron Huang | 29667d2 | 2019-09-27 22:31:22 +0800 | [diff] [blame] | 615 | /** |
| 616 | * Return the number of packets received on the specified interface since |
| 617 | * device boot. Statistics are measured at the network layer, so both TCP |
| 618 | * and UDP usage are included. |
| 619 | * |
| 620 | * @param iface The name of the interface. |
| 621 | * @return The number of received packets. |
| 622 | */ |
| 623 | public static long getRxPackets(@NonNull String iface) { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 624 | try { |
| 625 | return getStatsService().getIfaceStats(iface, TYPE_RX_PACKETS); |
| 626 | } catch (RemoteException e) { |
| 627 | throw e.rethrowFromSystemServer(); |
| 628 | } |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 629 | } |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 630 | |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 631 | /** {@hide} */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 632 | @UnsupportedAppUsage |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 633 | public static long getTxBytes(String iface) { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 634 | try { |
| 635 | return getStatsService().getIfaceStats(iface, TYPE_TX_BYTES); |
| 636 | } catch (RemoteException e) { |
| 637 | throw e.rethrowFromSystemServer(); |
| 638 | } |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 639 | } |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 640 | |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 641 | /** {@hide} */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 642 | @UnsupportedAppUsage |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 643 | public static long getRxBytes(String iface) { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 644 | try { |
| 645 | return getStatsService().getIfaceStats(iface, TYPE_RX_BYTES); |
| 646 | } catch (RemoteException e) { |
| 647 | throw e.rethrowFromSystemServer(); |
| 648 | } |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 649 | } |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 650 | |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 651 | /** {@hide} */ |
| 652 | @TestApi |
| 653 | public static long getLoopbackTxPackets() { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 654 | try { |
| 655 | return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_TX_PACKETS); |
| 656 | } catch (RemoteException e) { |
| 657 | throw e.rethrowFromSystemServer(); |
| 658 | } |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /** {@hide} */ |
| 662 | @TestApi |
| 663 | public static long getLoopbackRxPackets() { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 664 | try { |
| 665 | return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_RX_PACKETS); |
| 666 | } catch (RemoteException e) { |
| 667 | throw e.rethrowFromSystemServer(); |
| 668 | } |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /** {@hide} */ |
| 672 | @TestApi |
| 673 | public static long getLoopbackTxBytes() { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 674 | try { |
| 675 | return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_TX_BYTES); |
| 676 | } catch (RemoteException e) { |
| 677 | throw e.rethrowFromSystemServer(); |
| 678 | } |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /** {@hide} */ |
| 682 | @TestApi |
| 683 | public static long getLoopbackRxBytes() { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 684 | try { |
| 685 | return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_RX_BYTES); |
| 686 | } catch (RemoteException e) { |
| 687 | throw e.rethrowFromSystemServer(); |
| 688 | } |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 691 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 692 | * Return number of packets transmitted since device boot. Counts packets |
| 693 | * across all network interfaces, and always increases monotonically since |
| 694 | * device boot. Statistics are measured at the network layer, so they |
| 695 | * include both TCP and UDP usage. |
| 696 | * <p> |
| 697 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 698 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 699 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 700 | public static long getTotalTxPackets() { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 701 | try { |
| 702 | return getStatsService().getTotalStats(TYPE_TX_PACKETS); |
| 703 | } catch (RemoteException e) { |
| 704 | throw e.rethrowFromSystemServer(); |
| 705 | } |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 706 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 707 | |
| 708 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 709 | * Return number of packets received since device boot. Counts packets |
| 710 | * across all network interfaces, and always increases monotonically since |
| 711 | * device boot. Statistics are measured at the network layer, so they |
| 712 | * include both TCP and UDP usage. |
| 713 | * <p> |
| 714 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 715 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 716 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 717 | public static long getTotalRxPackets() { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 718 | try { |
| 719 | return getStatsService().getTotalStats(TYPE_RX_PACKETS); |
| 720 | } catch (RemoteException e) { |
| 721 | throw e.rethrowFromSystemServer(); |
| 722 | } |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 723 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 724 | |
| 725 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 726 | * Return number of bytes transmitted since device boot. Counts packets |
| 727 | * across all network interfaces, and always increases monotonically since |
| 728 | * device boot. Statistics are measured at the network layer, so they |
| 729 | * include both TCP and UDP usage. |
| 730 | * <p> |
| 731 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 732 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 733 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 734 | public static long getTotalTxBytes() { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 735 | try { |
| 736 | return getStatsService().getTotalStats(TYPE_TX_BYTES); |
| 737 | } catch (RemoteException e) { |
| 738 | throw e.rethrowFromSystemServer(); |
| 739 | } |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 740 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 741 | |
| 742 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 743 | * Return number of bytes received since device boot. Counts packets across |
| 744 | * all network interfaces, and always increases monotonically since device |
| 745 | * boot. Statistics are measured at the network layer, so they include both |
| 746 | * TCP and UDP usage. |
| 747 | * <p> |
| 748 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 749 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 750 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 751 | public static long getTotalRxBytes() { |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 752 | try { |
| 753 | return getStatsService().getTotalStats(TYPE_RX_BYTES); |
| 754 | } catch (RemoteException e) { |
| 755 | throw e.rethrowFromSystemServer(); |
| 756 | } |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 757 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 758 | |
| 759 | /** |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 760 | * Return number of bytes transmitted by the given UID since device boot. |
| 761 | * Counts packets across all network interfaces, and always increases |
| 762 | * monotonically since device boot. Statistics are measured at the network |
| 763 | * layer, so they include both TCP and UDP usage. |
| 764 | * <p> |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 765 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 766 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
| 767 | * <p> |
| 768 | * Starting in {@link android.os.Build.VERSION_CODES#N} this will only |
| 769 | * report traffic statistics for the calling UID. It will return |
| 770 | * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access |
| 771 | * historical network statistics belonging to other UIDs, use |
| 772 | * {@link NetworkStatsManager}. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 773 | * |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 774 | * @see android.os.Process#myUid() |
| 775 | * @see android.content.pm.ApplicationInfo#uid |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 776 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 777 | public static long getUidTxBytes(int uid) { |
Chenbo Feng | 78cd384 | 2019-06-17 16:22:28 -0700 | [diff] [blame] | 778 | try { |
| 779 | return getStatsService().getUidStats(uid, TYPE_TX_BYTES); |
| 780 | } catch (RemoteException e) { |
| 781 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 782 | } |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 783 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 784 | |
| 785 | /** |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 786 | * Return number of bytes received by the given UID since device boot. |
| 787 | * Counts packets across all network interfaces, and always increases |
| 788 | * monotonically since device boot. Statistics are measured at the network |
| 789 | * layer, so they include both TCP and UDP usage. |
| 790 | * <p> |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 791 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 792 | * {@link #UNSUPPORTED} on devices where statistics aren't available. |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 793 | * <p> |
| 794 | * Starting in {@link android.os.Build.VERSION_CODES#N} this will only |
| 795 | * report traffic statistics for the calling UID. It will return |
| 796 | * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access |
| 797 | * historical network statistics belonging to other UIDs, use |
| 798 | * {@link NetworkStatsManager}. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 799 | * |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 800 | * @see android.os.Process#myUid() |
| 801 | * @see android.content.pm.ApplicationInfo#uid |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 802 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 803 | public static long getUidRxBytes(int uid) { |
Chenbo Feng | 78cd384 | 2019-06-17 16:22:28 -0700 | [diff] [blame] | 804 | try { |
| 805 | return getStatsService().getUidStats(uid, TYPE_RX_BYTES); |
| 806 | } catch (RemoteException e) { |
| 807 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 808 | } |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 809 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 810 | |
| 811 | /** |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 812 | * Return number of packets transmitted by the given UID since device boot. |
| 813 | * Counts packets across all network interfaces, and always increases |
| 814 | * monotonically since device boot. Statistics are measured at the network |
| 815 | * layer, so they include both TCP and UDP usage. |
| 816 | * <p> |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 817 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 818 | * {@link #UNSUPPORTED} on devices where statistics aren't available. |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 819 | * <p> |
| 820 | * Starting in {@link android.os.Build.VERSION_CODES#N} this will only |
| 821 | * report traffic statistics for the calling UID. It will return |
| 822 | * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access |
| 823 | * historical network statistics belonging to other UIDs, use |
| 824 | * {@link NetworkStatsManager}. |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 825 | * |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 826 | * @see android.os.Process#myUid() |
| 827 | * @see android.content.pm.ApplicationInfo#uid |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 828 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 829 | public static long getUidTxPackets(int uid) { |
Chenbo Feng | 78cd384 | 2019-06-17 16:22:28 -0700 | [diff] [blame] | 830 | try { |
| 831 | return getStatsService().getUidStats(uid, TYPE_TX_PACKETS); |
| 832 | } catch (RemoteException e) { |
| 833 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 834 | } |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 835 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 836 | |
| 837 | /** |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 838 | * Return number of packets received by the given UID since device boot. |
| 839 | * Counts packets across all network interfaces, and always increases |
| 840 | * monotonically since device boot. Statistics are measured at the network |
| 841 | * layer, so they include both TCP and UDP usage. |
| 842 | * <p> |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 843 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 844 | * {@link #UNSUPPORTED} on devices where statistics aren't available. |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 845 | * <p> |
| 846 | * Starting in {@link android.os.Build.VERSION_CODES#N} this will only |
| 847 | * report traffic statistics for the calling UID. It will return |
| 848 | * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access |
| 849 | * historical network statistics belonging to other UIDs, use |
| 850 | * {@link NetworkStatsManager}. |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 851 | * |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 852 | * @see android.os.Process#myUid() |
| 853 | * @see android.content.pm.ApplicationInfo#uid |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 854 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 855 | public static long getUidRxPackets(int uid) { |
Chenbo Feng | 78cd384 | 2019-06-17 16:22:28 -0700 | [diff] [blame] | 856 | try { |
| 857 | return getStatsService().getUidStats(uid, TYPE_RX_PACKETS); |
| 858 | } catch (RemoteException e) { |
| 859 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 860 | } |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 861 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 862 | |
| 863 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 864 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 865 | * transport layer statistics are no longer available, and will |
| 866 | * always return {@link #UNSUPPORTED}. |
| 867 | * @see #getUidTxBytes(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 868 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 869 | @Deprecated |
| 870 | public static long getUidTcpTxBytes(int uid) { |
| 871 | return UNSUPPORTED; |
| 872 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 873 | |
| 874 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 875 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 876 | * transport layer statistics are no longer available, and will |
| 877 | * always return {@link #UNSUPPORTED}. |
| 878 | * @see #getUidRxBytes(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 879 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 880 | @Deprecated |
| 881 | public static long getUidTcpRxBytes(int uid) { |
| 882 | return UNSUPPORTED; |
| 883 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 884 | |
| 885 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 886 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 887 | * transport layer statistics are no longer available, and will |
| 888 | * always return {@link #UNSUPPORTED}. |
| 889 | * @see #getUidTxBytes(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 890 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 891 | @Deprecated |
| 892 | public static long getUidUdpTxBytes(int uid) { |
| 893 | return UNSUPPORTED; |
| 894 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 895 | |
| 896 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 897 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 898 | * transport layer statistics are no longer available, and will |
| 899 | * always return {@link #UNSUPPORTED}. |
| 900 | * @see #getUidRxBytes(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 901 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 902 | @Deprecated |
| 903 | public static long getUidUdpRxBytes(int uid) { |
| 904 | return UNSUPPORTED; |
| 905 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 906 | |
| 907 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 908 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 909 | * transport layer statistics are no longer available, and will |
| 910 | * always return {@link #UNSUPPORTED}. |
| 911 | * @see #getUidTxPackets(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 912 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 913 | @Deprecated |
| 914 | public static long getUidTcpTxSegments(int uid) { |
| 915 | return UNSUPPORTED; |
| 916 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 917 | |
| 918 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 919 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 920 | * transport layer statistics are no longer available, and will |
| 921 | * always return {@link #UNSUPPORTED}. |
| 922 | * @see #getUidRxPackets(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 923 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 924 | @Deprecated |
| 925 | public static long getUidTcpRxSegments(int uid) { |
| 926 | return UNSUPPORTED; |
| 927 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 928 | |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 929 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 930 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 931 | * transport layer statistics are no longer available, and will |
| 932 | * always return {@link #UNSUPPORTED}. |
| 933 | * @see #getUidTxPackets(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 934 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 935 | @Deprecated |
| 936 | public static long getUidUdpTxPackets(int uid) { |
| 937 | return UNSUPPORTED; |
| 938 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 939 | |
| 940 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 941 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 942 | * transport layer statistics are no longer available, and will |
| 943 | * always return {@link #UNSUPPORTED}. |
| 944 | * @see #getUidRxPackets(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 945 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 946 | @Deprecated |
| 947 | public static long getUidUdpRxPackets(int uid) { |
| 948 | return UNSUPPORTED; |
| 949 | } |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 950 | |
| 951 | /** |
| 952 | * Return detailed {@link NetworkStats} for the current UID. Requires no |
| 953 | * special permission. |
| 954 | */ |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 955 | private static NetworkStats getDataLayerSnapshotForUid(Context context) { |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 956 | // TODO: take snapshot locally, since proc file is now visible |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 957 | final int uid = android.os.Process.myUid(); |
| 958 | try { |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 959 | return getStatsService().getDataLayerSnapshotForUid(uid); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 960 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 961 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 962 | } |
| 963 | } |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 964 | |
| 965 | /** |
| 966 | * Return set of any ifaces associated with mobile networks since boot. |
| 967 | * Interfaces are never removed from this list, so counters should always be |
| 968 | * monotonic. |
| 969 | */ |
Chalard Jean | 0bb53dbb | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 970 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 971 | private static String[] getMobileIfaces() { |
| 972 | try { |
| 973 | return getStatsService().getMobileIfaces(); |
| 974 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 975 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
| 979 | // NOTE: keep these in sync with android_net_TrafficStats.cpp |
| 980 | private static final int TYPE_RX_BYTES = 0; |
| 981 | private static final int TYPE_RX_PACKETS = 1; |
| 982 | private static final int TYPE_TX_BYTES = 2; |
| 983 | private static final int TYPE_TX_PACKETS = 3; |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 984 | private static final int TYPE_TCP_RX_PACKETS = 4; |
| 985 | private static final int TYPE_TCP_TX_PACKETS = 5; |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 986 | } |