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