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