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 | |
Christopher Tate | a2496de | 2014-08-06 14:19:56 -0700 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 20 | import android.app.DownloadManager; |
| 21 | import android.app.backup.BackupManager; |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 22 | import android.app.usage.NetworkStatsManager; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 24 | import android.media.MediaPlayer; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 25 | import android.os.RemoteException; |
| 26 | import android.os.ServiceManager; |
| 27 | |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 28 | import com.android.server.NetworkManagementSocketTagger; |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 29 | |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 30 | import dalvik.system.SocketTagger; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 31 | |
Jeff Sharkey | f0d7633 | 2015-12-04 15:21:52 -0700 | [diff] [blame] | 32 | import java.net.DatagramSocket; |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 33 | import java.net.Socket; |
| 34 | import java.net.SocketException; |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 37 | * Class that provides network traffic statistics. These statistics include |
Dan Egnor | 2b4abcd | 2010-04-07 17:30:50 -0700 | [diff] [blame] | 38 | * bytes transmitted and received and network packets transmitted and received, |
| 39 | * 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] | 40 | * <p> |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 41 | * These statistics may not be available on all platforms. If the statistics are |
| 42 | * not supported by this device, {@link #UNSUPPORTED} will be returned. |
| 43 | * <p> |
| 44 | * Note that the statistics returned by this class reset and start from zero |
| 45 | * after every reboot. To access more robust historical network statistics data, |
| 46 | * use {@link NetworkStatsManager} instead. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 47 | */ |
| 48 | public class TrafficStats { |
| 49 | /** |
| 50 | * The return value to indicate that the device does not support the statistic. |
| 51 | */ |
| 52 | public final static int UNSUPPORTED = -1; |
| 53 | |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 54 | /** @hide */ |
| 55 | public static final long KB_IN_BYTES = 1024; |
| 56 | /** @hide */ |
| 57 | public static final long MB_IN_BYTES = KB_IN_BYTES * 1024; |
| 58 | /** @hide */ |
| 59 | public static final long GB_IN_BYTES = MB_IN_BYTES * 1024; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 60 | /** @hide */ |
| 61 | public static final long TB_IN_BYTES = GB_IN_BYTES * 1024; |
Jeff Sharkey | b521fea | 2015-06-15 21:09:10 -0700 | [diff] [blame] | 62 | /** @hide */ |
| 63 | public static final long PB_IN_BYTES = TB_IN_BYTES * 1024; |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 64 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 65 | /** |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 66 | * Special UID value used when collecting {@link NetworkStatsHistory} for |
| 67 | * removed applications. |
| 68 | * |
| 69 | * @hide |
| 70 | */ |
| 71 | public static final int UID_REMOVED = -4; |
| 72 | |
| 73 | /** |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 74 | * Special UID value used when collecting {@link NetworkStatsHistory} for |
| 75 | * tethering traffic. |
| 76 | * |
| 77 | * @hide |
| 78 | */ |
| 79 | public static final int UID_TETHERING = -5; |
| 80 | |
| 81 | /** |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 82 | * Default tag value for {@link DownloadManager} traffic. |
| 83 | * |
| 84 | * @hide |
| 85 | */ |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 86 | public static final int TAG_SYSTEM_DOWNLOAD = 0xFFFFFF01; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Default tag value for {@link MediaPlayer} traffic. |
| 90 | * |
| 91 | * @hide |
| 92 | */ |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 93 | public static final int TAG_SYSTEM_MEDIA = 0xFFFFFF02; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
Christopher Tate | 0a61b36 | 2015-11-10 10:49:20 -0800 | [diff] [blame] | 96 | * Default tag value for {@link BackupManager} backup traffic; that is, |
| 97 | * traffic from the device to the storage backend. |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 98 | * |
| 99 | * @hide |
| 100 | */ |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 101 | public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 102 | |
Christopher Tate | 0a61b36 | 2015-11-10 10:49:20 -0800 | [diff] [blame] | 103 | /** |
| 104 | * Default tag value for {@link BackupManager} restore traffic; that is, |
| 105 | * app data retrieved from the storage backend at install time. |
| 106 | * |
| 107 | * @hide |
| 108 | */ |
| 109 | public static final int TAG_SYSTEM_RESTORE = 0xFFFFFF04; |
| 110 | |
Jeff Sharkey | 763a140 | 2016-11-21 12:14:50 -0700 | [diff] [blame] | 111 | /** @hide */ |
| 112 | public static final int TAG_SYSTEM_DHCP = 0xFFFFFF05; |
| 113 | /** @hide */ |
| 114 | public static final int TAG_SYSTEM_NTP = 0xFFFFFF06; |
| 115 | /** @hide */ |
| 116 | public static final int TAG_SYSTEM_PROBE = 0xFFFFFF07; |
| 117 | /** @hide */ |
| 118 | public static final int TAG_SYSTEM_NEIGHBOR = 0xFFFFFF08; |
| 119 | /** @hide */ |
| 120 | public static final int TAG_SYSTEM_GPS = 0xFFFFFF09; |
| 121 | /** @hide */ |
| 122 | public static final int TAG_SYSTEM_PAC = 0xFFFFFF0A; |
| 123 | |
| 124 | /** |
| 125 | * Sockets that are strictly local on device; never hits network. |
| 126 | * |
| 127 | * @hide |
| 128 | */ |
| 129 | public static final int TAG_SYSTEM_LOCAL = 0xFFFFFFAA; |
| 130 | |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 131 | private static INetworkStatsService sStatsService; |
| 132 | |
| 133 | private synchronized static INetworkStatsService getStatsService() { |
| 134 | if (sStatsService == null) { |
| 135 | sStatsService = INetworkStatsService.Stub.asInterface( |
| 136 | ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); |
| 137 | } |
| 138 | return sStatsService; |
| 139 | } |
| 140 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 141 | /** |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 142 | * Snapshot of {@link NetworkStats} when the currently active profiling |
| 143 | * session started, or {@code null} if no session active. |
| 144 | * |
| 145 | * @see #startDataProfiling(Context) |
| 146 | * @see #stopDataProfiling(Context) |
| 147 | */ |
| 148 | private static NetworkStats sActiveProfilingStart; |
| 149 | |
| 150 | private static Object sProfilingLock = new Object(); |
| 151 | |
| 152 | /** |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 153 | * Set active tag to use when accounting {@link Socket} traffic originating |
| 154 | * from the current thread. Only one active tag per thread is supported. |
| 155 | * <p> |
| 156 | * Changes only take effect during subsequent calls to |
| 157 | * {@link #tagSocket(Socket)}. |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 158 | * <p> |
| 159 | * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and |
| 160 | * used internally by system services like {@link DownloadManager} when |
| 161 | * performing traffic on behalf of an application. |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 162 | * |
| 163 | * @see #clearThreadStatsTag() |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 164 | */ |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 165 | public static void setThreadStatsTag(int tag) { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 166 | NetworkManagementSocketTagger.setThreadSocketStatsTag(tag); |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 169 | /** |
Jeff Sharkey | 11cadda | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 170 | * Set active tag to use when accounting {@link Socket} traffic originating |
| 171 | * from the current thread. The tag used internally is well-defined to |
| 172 | * distinguish all backup-related traffic. |
| 173 | * |
Christopher Tate | a2496de | 2014-08-06 14:19:56 -0700 | [diff] [blame] | 174 | * @hide |
| 175 | */ |
| 176 | @SystemApi |
| 177 | public static void setThreadStatsTagBackup() { |
| 178 | setThreadStatsTag(TAG_SYSTEM_BACKUP); |
| 179 | } |
| 180 | |
| 181 | /** |
Jeff Sharkey | 11cadda | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 182 | * Set active tag to use when accounting {@link Socket} traffic originating |
| 183 | * from the current thread. The tag used internally is well-defined to |
| 184 | * distinguish all restore-related traffic. |
| 185 | * |
Christopher Tate | 0a61b36 | 2015-11-10 10:49:20 -0800 | [diff] [blame] | 186 | * @hide |
| 187 | */ |
| 188 | @SystemApi |
| 189 | public static void setThreadStatsTagRestore() { |
| 190 | setThreadStatsTag(TAG_SYSTEM_RESTORE); |
| 191 | } |
| 192 | |
| 193 | /** |
Alon Albert | eaef351 | 2011-07-19 11:16:09 +0300 | [diff] [blame] | 194 | * Get the active tag used when accounting {@link Socket} traffic originating |
| 195 | * from the current thread. Only one active tag per thread is supported. |
| 196 | * {@link #tagSocket(Socket)}. |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 197 | * |
| 198 | * @see #setThreadStatsTag(int) |
Alon Albert | eaef351 | 2011-07-19 11:16:09 +0300 | [diff] [blame] | 199 | */ |
| 200 | public static int getThreadStatsTag() { |
| 201 | return NetworkManagementSocketTagger.getThreadSocketStatsTag(); |
| 202 | } |
| 203 | |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Clear any active tag set to account {@link Socket} traffic originating |
| 206 | * from the current thread. |
| 207 | * |
| 208 | * @see #setThreadStatsTag(int) |
| 209 | */ |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 210 | public static void clearThreadStatsTag() { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 211 | NetworkManagementSocketTagger.setThreadSocketStatsTag(-1); |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Set specific UID to use when accounting {@link Socket} traffic |
| 216 | * originating from the current thread. Designed for use when performing an |
| 217 | * operation on behalf of another application. |
| 218 | * <p> |
| 219 | * Changes only take effect during subsequent calls to |
| 220 | * {@link #tagSocket(Socket)}. |
| 221 | * <p> |
| 222 | * To take effect, caller must hold |
| 223 | * {@link android.Manifest.permission#UPDATE_DEVICE_STATS} permission. |
| 224 | * |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 225 | * @hide |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 226 | */ |
Christopher Tate | a2496de | 2014-08-06 14:19:56 -0700 | [diff] [blame] | 227 | @SystemApi |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 228 | public static void setThreadStatsUid(int uid) { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 229 | NetworkManagementSocketTagger.setThreadSocketStatsUid(uid); |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Jeff Sharkey | 11cadda | 2016-03-22 10:20:12 -0600 | [diff] [blame] | 232 | /** |
| 233 | * Clear any active UID set to account {@link Socket} traffic originating |
| 234 | * from the current thread. |
| 235 | * |
| 236 | * @see #setThreadStatsUid(int) |
| 237 | * @hide |
| 238 | */ |
Christopher Tate | a2496de | 2014-08-06 14:19:56 -0700 | [diff] [blame] | 239 | @SystemApi |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 240 | public static void clearThreadStatsUid() { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 241 | NetworkManagementSocketTagger.setThreadSocketStatsUid(-1); |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Tag the given {@link Socket} with any statistics parameters active for |
| 246 | * the current thread. Subsequent calls always replace any existing |
| 247 | * parameters. When finished, call {@link #untagSocket(Socket)} to remove |
| 248 | * statistics parameters. |
| 249 | * |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 250 | * @see #setThreadStatsTag(int) |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 251 | */ |
| 252 | public static void tagSocket(Socket socket) throws SocketException { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 253 | SocketTagger.get().tag(socket); |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Remove any statistics parameters from the given {@link Socket}. |
| 258 | */ |
| 259 | public static void untagSocket(Socket socket) throws SocketException { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 260 | SocketTagger.get().untag(socket); |
Jeff Sharkey | 43be174 | 2011-04-21 18:45:43 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
Jeff Sharkey | f0d7633 | 2015-12-04 15:21:52 -0700 | [diff] [blame] | 264 | * Tag the given {@link DatagramSocket} with any statistics parameters |
| 265 | * active for the current thread. Subsequent calls always replace any |
| 266 | * existing parameters. When finished, call |
| 267 | * {@link #untagDatagramSocket(DatagramSocket)} to remove statistics |
| 268 | * parameters. |
| 269 | * |
| 270 | * @see #setThreadStatsTag(int) |
Jeff Sharkey | f0d7633 | 2015-12-04 15:21:52 -0700 | [diff] [blame] | 271 | */ |
| 272 | public static void tagDatagramSocket(DatagramSocket socket) throws SocketException { |
| 273 | SocketTagger.get().tag(socket); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Remove any statistics parameters from the given {@link DatagramSocket}. |
| 278 | */ |
| 279 | public static void untagDatagramSocket(DatagramSocket socket) throws SocketException { |
| 280 | SocketTagger.get().untag(socket); |
| 281 | } |
| 282 | |
| 283 | /** |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 284 | * Start profiling data usage for current UID. Only one profiling session |
| 285 | * can be active at a time. |
| 286 | * |
| 287 | * @hide |
| 288 | */ |
| 289 | public static void startDataProfiling(Context context) { |
| 290 | synchronized (sProfilingLock) { |
| 291 | if (sActiveProfilingStart != null) { |
| 292 | throw new IllegalStateException("already profiling data"); |
| 293 | } |
| 294 | |
| 295 | // take snapshot in time; we calculate delta later |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 296 | sActiveProfilingStart = getDataLayerSnapshotForUid(context); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Stop profiling data usage for current UID. |
| 302 | * |
| 303 | * @return Detailed {@link NetworkStats} of data that occurred since last |
| 304 | * {@link #startDataProfiling(Context)} call. |
| 305 | * @hide |
| 306 | */ |
| 307 | public static NetworkStats stopDataProfiling(Context context) { |
| 308 | synchronized (sProfilingLock) { |
| 309 | if (sActiveProfilingStart == null) { |
| 310 | throw new IllegalStateException("not profiling data"); |
| 311 | } |
| 312 | |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 313 | // subtract starting values and return delta |
| 314 | final NetworkStats profilingStop = getDataLayerSnapshotForUid(context); |
| 315 | final NetworkStats profilingDelta = NetworkStats.subtract( |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 316 | profilingStop, sActiveProfilingStart, null, null); |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 317 | sActiveProfilingStart = null; |
| 318 | return profilingDelta; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | /** |
Jeff Sharkey | 558a232 | 2011-08-24 15:42:09 -0700 | [diff] [blame] | 323 | * Increment count of network operations performed under the accounting tag |
| 324 | * currently active on the calling thread. This can be used to derive |
| 325 | * bytes-per-operation. |
| 326 | * |
| 327 | * @param operationCount Number of operations to increment count by. |
| 328 | */ |
| 329 | public static void incrementOperationCount(int operationCount) { |
| 330 | final int tag = getThreadStatsTag(); |
| 331 | incrementOperationCount(tag, operationCount); |
| 332 | } |
| 333 | |
| 334 | /** |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 335 | * Increment count of network operations performed under the given |
| 336 | * accounting tag. This can be used to derive bytes-per-operation. |
| 337 | * |
| 338 | * @param tag Accounting tag used in {@link #setThreadStatsTag(int)}. |
| 339 | * @param operationCount Number of operations to increment count by. |
| 340 | */ |
| 341 | public static void incrementOperationCount(int tag, int operationCount) { |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 342 | final int uid = android.os.Process.myUid(); |
| 343 | try { |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 344 | getStatsService().incrementOperationCount(uid, tag, operationCount); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 345 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 346 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 350 | /** {@hide} */ |
| 351 | public static void closeQuietly(INetworkStatsSession session) { |
| 352 | // TODO: move to NetworkStatsService once it exists |
| 353 | if (session != null) { |
| 354 | try { |
| 355 | session.close(); |
| 356 | } catch (RuntimeException rethrown) { |
| 357 | throw rethrown; |
| 358 | } catch (Exception ignored) { |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 363 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 364 | * Return number of packets transmitted across mobile networks since device |
| 365 | * boot. Counts packets across all mobile network interfaces, and always |
| 366 | * increases monotonically since device boot. Statistics are measured at the |
| 367 | * network layer, so they include both TCP and UDP usage. |
| 368 | * <p> |
| 369 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 370 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 371 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 372 | public static long getMobileTxPackets() { |
| 373 | long total = 0; |
| 374 | for (String iface : getMobileIfaces()) { |
| 375 | total += getTxPackets(iface); |
| 376 | } |
| 377 | return total; |
| 378 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 379 | |
| 380 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 381 | * Return number of packets received across mobile networks since device |
| 382 | * boot. Counts packets across all mobile network interfaces, and always |
| 383 | * increases monotonically since device boot. Statistics are measured at the |
| 384 | * network layer, so they include both TCP and UDP usage. |
| 385 | * <p> |
| 386 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 387 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 388 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 389 | public static long getMobileRxPackets() { |
| 390 | long total = 0; |
| 391 | for (String iface : getMobileIfaces()) { |
| 392 | total += getRxPackets(iface); |
| 393 | } |
| 394 | return total; |
| 395 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 396 | |
| 397 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 398 | * Return number of bytes transmitted across mobile networks since device |
| 399 | * boot. Counts packets across all mobile network interfaces, and always |
| 400 | * increases monotonically since device boot. Statistics are measured at the |
| 401 | * network layer, so they include both TCP and UDP usage. |
| 402 | * <p> |
| 403 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 404 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 405 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 406 | public static long getMobileTxBytes() { |
| 407 | long total = 0; |
| 408 | for (String iface : getMobileIfaces()) { |
| 409 | total += getTxBytes(iface); |
| 410 | } |
| 411 | return total; |
| 412 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 413 | |
| 414 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 415 | * Return number of bytes received across mobile networks since device boot. |
| 416 | * Counts packets across all mobile network interfaces, and always increases |
| 417 | * monotonically since device boot. Statistics are measured at the network |
| 418 | * layer, so they include both TCP and UDP usage. |
| 419 | * <p> |
| 420 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 421 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 422 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 423 | public static long getMobileRxBytes() { |
| 424 | long total = 0; |
| 425 | for (String iface : getMobileIfaces()) { |
| 426 | total += getRxBytes(iface); |
| 427 | } |
| 428 | return total; |
| 429 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 430 | |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 431 | /** {@hide} */ |
| 432 | public static long getMobileTcpRxPackets() { |
| 433 | long total = 0; |
| 434 | for (String iface : getMobileIfaces()) { |
| 435 | final long stat = nativeGetIfaceStat(iface, TYPE_TCP_RX_PACKETS); |
| 436 | if (stat != UNSUPPORTED) { |
| 437 | total += stat; |
| 438 | } |
| 439 | } |
| 440 | return total; |
| 441 | } |
| 442 | |
| 443 | /** {@hide} */ |
| 444 | public static long getMobileTcpTxPackets() { |
| 445 | long total = 0; |
| 446 | for (String iface : getMobileIfaces()) { |
| 447 | final long stat = nativeGetIfaceStat(iface, TYPE_TCP_TX_PACKETS); |
| 448 | if (stat != UNSUPPORTED) { |
| 449 | total += stat; |
| 450 | } |
| 451 | } |
| 452 | return total; |
| 453 | } |
| 454 | |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 455 | /** {@hide} */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 456 | public static long getTxPackets(String iface) { |
| 457 | return nativeGetIfaceStat(iface, TYPE_TX_PACKETS); |
| 458 | } |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 459 | |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 460 | /** {@hide} */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 461 | public static long getRxPackets(String iface) { |
| 462 | return nativeGetIfaceStat(iface, TYPE_RX_PACKETS); |
| 463 | } |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 464 | |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 465 | /** {@hide} */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 466 | public static long getTxBytes(String iface) { |
| 467 | return nativeGetIfaceStat(iface, TYPE_TX_BYTES); |
| 468 | } |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 469 | |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 470 | /** {@hide} */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 471 | public static long getRxBytes(String iface) { |
| 472 | return nativeGetIfaceStat(iface, TYPE_RX_BYTES); |
| 473 | } |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 474 | |
| 475 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 476 | * Return number of packets transmitted since device boot. Counts packets |
| 477 | * across all network interfaces, and always increases monotonically since |
| 478 | * device boot. Statistics are measured at the network layer, so they |
| 479 | * include both TCP and UDP usage. |
| 480 | * <p> |
| 481 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 482 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 483 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 484 | public static long getTotalTxPackets() { |
| 485 | return nativeGetTotalStat(TYPE_TX_PACKETS); |
| 486 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 487 | |
| 488 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 489 | * Return number of packets received since device boot. Counts packets |
| 490 | * across all network interfaces, and always increases monotonically since |
| 491 | * device boot. Statistics are measured at the network layer, so they |
| 492 | * include both TCP and UDP usage. |
| 493 | * <p> |
| 494 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 495 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 496 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 497 | public static long getTotalRxPackets() { |
| 498 | return nativeGetTotalStat(TYPE_RX_PACKETS); |
| 499 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 500 | |
| 501 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 502 | * Return number of bytes transmitted since device boot. Counts packets |
| 503 | * across all network interfaces, and always increases monotonically since |
| 504 | * device boot. Statistics are measured at the network layer, so they |
| 505 | * include both TCP and UDP usage. |
| 506 | * <p> |
| 507 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 508 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 509 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 510 | public static long getTotalTxBytes() { |
| 511 | return nativeGetTotalStat(TYPE_TX_BYTES); |
| 512 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 513 | |
| 514 | /** |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 515 | * Return number of bytes received since device boot. Counts packets across |
| 516 | * all network interfaces, and always increases monotonically since device |
| 517 | * boot. Statistics are measured at the network layer, so they include both |
| 518 | * TCP and UDP usage. |
| 519 | * <p> |
| 520 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 521 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 522 | */ |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 523 | public static long getTotalRxBytes() { |
| 524 | return nativeGetTotalStat(TYPE_RX_BYTES); |
| 525 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 526 | |
| 527 | /** |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 528 | * Return number of bytes transmitted by the given UID since device boot. |
| 529 | * Counts packets across all network interfaces, and always increases |
| 530 | * monotonically since device boot. Statistics are measured at the network |
| 531 | * layer, so they include both TCP and UDP usage. |
| 532 | * <p> |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 533 | * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may |
| 534 | * return {@link #UNSUPPORTED} on devices where statistics aren't available. |
| 535 | * <p> |
| 536 | * Starting in {@link android.os.Build.VERSION_CODES#N} this will only |
| 537 | * report traffic statistics for the calling UID. It will return |
| 538 | * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access |
| 539 | * historical network statistics belonging to other UIDs, use |
| 540 | * {@link NetworkStatsManager}. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 541 | * |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 542 | * @see android.os.Process#myUid() |
| 543 | * @see android.content.pm.ApplicationInfo#uid |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 544 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 545 | public static long getUidTxBytes(int uid) { |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 546 | // This isn't actually enforcing any security; it just returns the |
| 547 | // unsupported value. The real filtering is done at the kernel level. |
| 548 | final int callingUid = android.os.Process.myUid(); |
| 549 | if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) { |
| 550 | return nativeGetUidStat(uid, TYPE_TX_BYTES); |
| 551 | } else { |
| 552 | return UNSUPPORTED; |
| 553 | } |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 554 | } |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 555 | |
| 556 | /** |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 557 | * Return number of bytes received by the given UID since device boot. |
| 558 | * Counts packets across all network interfaces, and always increases |
| 559 | * monotonically since device boot. Statistics are measured at the network |
| 560 | * layer, so they include both TCP and UDP usage. |
| 561 | * <p> |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 562 | * 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] | 563 | * {@link #UNSUPPORTED} on devices where statistics aren't available. |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 564 | * <p> |
| 565 | * Starting in {@link android.os.Build.VERSION_CODES#N} this will only |
| 566 | * report traffic statistics for the calling UID. It will return |
| 567 | * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access |
| 568 | * historical network statistics belonging to other UIDs, use |
| 569 | * {@link NetworkStatsManager}. |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 570 | * |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 571 | * @see android.os.Process#myUid() |
| 572 | * @see android.content.pm.ApplicationInfo#uid |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 573 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 574 | public static long getUidRxBytes(int uid) { |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 575 | // This isn't actually enforcing any security; it just returns the |
| 576 | // unsupported value. The real filtering is done at the kernel level. |
| 577 | final int callingUid = android.os.Process.myUid(); |
| 578 | if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) { |
| 579 | return nativeGetUidStat(uid, TYPE_RX_BYTES); |
| 580 | } else { |
| 581 | return UNSUPPORTED; |
| 582 | } |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 583 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 584 | |
| 585 | /** |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 586 | * Return number of packets transmitted by the given UID since device boot. |
| 587 | * Counts packets across all network interfaces, and always increases |
| 588 | * monotonically since device boot. Statistics are measured at the network |
| 589 | * layer, so they include both TCP and UDP usage. |
| 590 | * <p> |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 591 | * 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] | 592 | * {@link #UNSUPPORTED} on devices where statistics aren't available. |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 593 | * <p> |
| 594 | * Starting in {@link android.os.Build.VERSION_CODES#N} this will only |
| 595 | * report traffic statistics for the calling UID. It will return |
| 596 | * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access |
| 597 | * historical network statistics belonging to other UIDs, use |
| 598 | * {@link NetworkStatsManager}. |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 599 | * |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 600 | * @see android.os.Process#myUid() |
| 601 | * @see android.content.pm.ApplicationInfo#uid |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 602 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 603 | public static long getUidTxPackets(int uid) { |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 604 | // This isn't actually enforcing any security; it just returns the |
| 605 | // unsupported value. The real filtering is done at the kernel level. |
| 606 | final int callingUid = android.os.Process.myUid(); |
| 607 | if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) { |
| 608 | return nativeGetUidStat(uid, TYPE_TX_PACKETS); |
| 609 | } else { |
| 610 | return UNSUPPORTED; |
| 611 | } |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 612 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 613 | |
| 614 | /** |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 615 | * Return number of packets received by the given UID since device boot. |
| 616 | * Counts packets across all network interfaces, and always increases |
| 617 | * monotonically since device boot. Statistics are measured at the network |
| 618 | * layer, so they include both TCP and UDP usage. |
| 619 | * <p> |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 620 | * 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] | 621 | * {@link #UNSUPPORTED} on devices where statistics aren't available. |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 622 | * <p> |
| 623 | * Starting in {@link android.os.Build.VERSION_CODES#N} this will only |
| 624 | * report traffic statistics for the calling UID. It will return |
| 625 | * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access |
| 626 | * historical network statistics belonging to other UIDs, use |
| 627 | * {@link NetworkStatsManager}. |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 628 | * |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 629 | * @see android.os.Process#myUid() |
| 630 | * @see android.content.pm.ApplicationInfo#uid |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 631 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 632 | public static long getUidRxPackets(int uid) { |
Jeff Sharkey | 0d69d3d | 2016-03-09 16:40:15 -0700 | [diff] [blame] | 633 | // This isn't actually enforcing any security; it just returns the |
| 634 | // unsupported value. The real filtering is done at the kernel level. |
| 635 | final int callingUid = android.os.Process.myUid(); |
| 636 | if (callingUid == android.os.Process.SYSTEM_UID || callingUid == uid) { |
| 637 | return nativeGetUidStat(uid, TYPE_RX_PACKETS); |
| 638 | } else { |
| 639 | return UNSUPPORTED; |
| 640 | } |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 641 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 642 | |
| 643 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 644 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 645 | * transport layer statistics are no longer available, and will |
| 646 | * always return {@link #UNSUPPORTED}. |
| 647 | * @see #getUidTxBytes(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 648 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 649 | @Deprecated |
| 650 | public static long getUidTcpTxBytes(int uid) { |
| 651 | return UNSUPPORTED; |
| 652 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 653 | |
| 654 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 655 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 656 | * transport layer statistics are no longer available, and will |
| 657 | * always return {@link #UNSUPPORTED}. |
| 658 | * @see #getUidRxBytes(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 659 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 660 | @Deprecated |
| 661 | public static long getUidTcpRxBytes(int uid) { |
| 662 | return UNSUPPORTED; |
| 663 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 664 | |
| 665 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 666 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 667 | * transport layer statistics are no longer available, and will |
| 668 | * always return {@link #UNSUPPORTED}. |
| 669 | * @see #getUidTxBytes(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 670 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 671 | @Deprecated |
| 672 | public static long getUidUdpTxBytes(int uid) { |
| 673 | return UNSUPPORTED; |
| 674 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 675 | |
| 676 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 677 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 678 | * transport layer statistics are no longer available, and will |
| 679 | * always return {@link #UNSUPPORTED}. |
| 680 | * @see #getUidRxBytes(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 681 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 682 | @Deprecated |
| 683 | public static long getUidUdpRxBytes(int uid) { |
| 684 | return UNSUPPORTED; |
| 685 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 686 | |
| 687 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 688 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 689 | * transport layer statistics are no longer available, and will |
| 690 | * always return {@link #UNSUPPORTED}. |
| 691 | * @see #getUidTxPackets(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 692 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 693 | @Deprecated |
| 694 | public static long getUidTcpTxSegments(int uid) { |
| 695 | return UNSUPPORTED; |
| 696 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 697 | |
| 698 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 699 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 700 | * transport layer statistics are no longer available, and will |
| 701 | * always return {@link #UNSUPPORTED}. |
| 702 | * @see #getUidRxPackets(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 703 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 704 | @Deprecated |
| 705 | public static long getUidTcpRxSegments(int uid) { |
| 706 | return UNSUPPORTED; |
| 707 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 708 | |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 709 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 710 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 711 | * transport layer statistics are no longer available, and will |
| 712 | * always return {@link #UNSUPPORTED}. |
| 713 | * @see #getUidTxPackets(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 714 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 715 | @Deprecated |
| 716 | public static long getUidUdpTxPackets(int uid) { |
| 717 | return UNSUPPORTED; |
| 718 | } |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 719 | |
| 720 | /** |
Dianne Hackborn | 45e9ede | 2013-02-25 15:55:37 -0800 | [diff] [blame] | 721 | * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 722 | * transport layer statistics are no longer available, and will |
| 723 | * always return {@link #UNSUPPORTED}. |
| 724 | * @see #getUidRxPackets(int) |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 725 | */ |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 726 | @Deprecated |
| 727 | public static long getUidUdpRxPackets(int uid) { |
| 728 | return UNSUPPORTED; |
| 729 | } |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 730 | |
| 731 | /** |
| 732 | * Return detailed {@link NetworkStats} for the current UID. Requires no |
| 733 | * special permission. |
| 734 | */ |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 735 | private static NetworkStats getDataLayerSnapshotForUid(Context context) { |
Jeff Sharkey | dddace7 | 2013-03-26 13:46:05 -0700 | [diff] [blame] | 736 | // TODO: take snapshot locally, since proc file is now visible |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 737 | final int uid = android.os.Process.myUid(); |
| 738 | try { |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 739 | return getStatsService().getDataLayerSnapshotForUid(uid); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 740 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 741 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 744 | |
| 745 | /** |
| 746 | * Return set of any ifaces associated with mobile networks since boot. |
| 747 | * Interfaces are never removed from this list, so counters should always be |
| 748 | * monotonic. |
| 749 | */ |
| 750 | private static String[] getMobileIfaces() { |
| 751 | try { |
| 752 | return getStatsService().getMobileIfaces(); |
| 753 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 754 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | // NOTE: keep these in sync with android_net_TrafficStats.cpp |
| 759 | private static final int TYPE_RX_BYTES = 0; |
| 760 | private static final int TYPE_RX_PACKETS = 1; |
| 761 | private static final int TYPE_TX_BYTES = 2; |
| 762 | private static final int TYPE_TX_PACKETS = 3; |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 763 | private static final int TYPE_TCP_RX_PACKETS = 4; |
| 764 | private static final int TYPE_TCP_TX_PACKETS = 5; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 765 | |
| 766 | private static native long nativeGetTotalStat(int type); |
| 767 | private static native long nativeGetIfaceStat(String iface, int type); |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 768 | private static native long nativeGetUidStat(int uid, int type); |
Ken Shirriff | f7d0b01 | 2009-12-07 15:56:05 -0800 | [diff] [blame] | 769 | } |