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