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