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