TrafficStats.java revision c39c1d4dee917560d174f6ba5402e4c6644edd47
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 17package android.net; 18 19import android.util.Log; 20 21import java.io.File; 22import java.io.RandomAccessFile; 23import java.io.IOException; 24 25/** 26 * Class that provides network traffic statistics. These statistics include 27 * bytes transmitted and received and network packets transmitted and received, 28 * over all interfaces, over the mobile interface, and on a per-UID basis. 29 * <p> 30 * These statistics may not be available on all platforms. If the statistics 31 * are not supported by this device, {@link #UNSUPPORTED} will be returned. 32 */ 33public class TrafficStats { 34 /** 35 * The return value to indicate that the device does not support the statistic. 36 */ 37 public final static int UNSUPPORTED = -1; 38 39 /** 40 * Get the total number of packets transmitted through the mobile interface. 41 * 42 * @return number of packets. If the statistics are not supported by this device, 43 * {@link #UNSUPPORTED} will be returned. 44 */ 45 public static native long getMobileTxPackets(); 46 47 /** 48 * Get the total number of packets received through the mobile interface. 49 * 50 * @return number of packets. If the statistics are not supported by this device, 51 * {@link #UNSUPPORTED} will be returned. 52 */ 53 public static native long getMobileRxPackets(); 54 55 /** 56 * Get the total number of bytes transmitted through the mobile interface. 57 * 58 * @return number of bytes. If the statistics are not supported by this device, 59 * {@link #UNSUPPORTED} will be returned. 60 */ 61 public static native long getMobileTxBytes(); 62 63 /** 64 * Get the total number of bytes received through the mobile interface. 65 * 66 * @return number of bytes. If the statistics are not supported by this device, 67 * {@link #UNSUPPORTED} will be returned. 68 */ 69 public static native long getMobileRxBytes(); 70 71 /** 72 * Get the total number of packets sent through all network interfaces. 73 * 74 * @return the number of packets. If the statistics are not supported by this device, 75 * {@link #UNSUPPORTED} will be returned. 76 */ 77 public static native long getTotalTxPackets(); 78 79 /** 80 * Get the total number of packets received through all network interfaces. 81 * 82 * @return number of packets. If the statistics are not supported by this device, 83 * {@link #UNSUPPORTED} will be returned. 84 */ 85 public static native long getTotalRxPackets(); 86 87 /** 88 * Get the total number of bytes sent through all network interfaces. 89 * 90 * @return number of bytes. If the statistics are not supported by this device, 91 * {@link #UNSUPPORTED} will be returned. 92 */ 93 public static native long getTotalTxBytes(); 94 95 /** 96 * Get the total number of bytes received through all network interfaces. 97 * 98 * @return number of bytes. If the statistics are not supported by this device, 99 * {@link #UNSUPPORTED} will be returned. 100 */ 101 public static native long getTotalRxBytes(); 102 103 /** 104 * Get the number of bytes sent through the network for this UID. 105 * The statistics are across all interfaces. 106 * 107 * {@see android.os.Process#myUid()}. 108 * 109 * @param uid The UID of the process to examine. 110 * @return number of bytes. If the statistics are not supported by this device, 111 * {@link #UNSUPPORTED} will be returned. 112 */ 113 public static native long getUidTxBytes(int uid); 114 115 /** 116 * Get the number of bytes received through the network for this UID. 117 * The statistics are across all interfaces. 118 * 119 * {@see android.os.Process#myUid()}. 120 * 121 * @param uid The UID of the process to examine. 122 * @return number of bytes 123 */ 124 public static native long getUidRxBytes(int uid); 125 126 /** 127 * Get the number of packets (TCP segments + UDP) sent through 128 * the network for this UID. 129 * The statistics are across all interfaces. 130 * 131 * {@see android.os.Process#myUid()}. 132 * 133 * @param uid The UID of the process to examine. 134 * @return number of packets. 135 * If the statistics are not supported by this device, 136 * {@link #UNSUPPORTED} will be returned. 137 */ 138 public static native long getUidTxPackets(int uid); 139 140 /** 141 * Get the number of packets (TCP segments + UDP) received through 142 * the network for this UID. 143 * The statistics are across all interfaces. 144 * 145 * {@see android.os.Process#myUid()}. 146 * 147 * @param uid The UID of the process to examine. 148 * @return number of packets 149 */ 150 public static native long getUidRxPackets(int uid); 151 152 /** 153 * Get the number of TCP payload bytes sent for this UID. 154 * This total does not include protocol and control overheads at 155 * the transport and the lower layers of the networking stack. 156 * The statistics are across all interfaces. 157 * 158 * {@see android.os.Process#myUid()}. 159 * 160 * @param uid The UID of the process to examine. 161 * @return number of bytes. If the statistics are not supported by this device, 162 * {@link #UNSUPPORTED} will be returned. 163 */ 164 public static native long getUidTcpTxBytes(int uid); 165 166 /** 167 * Get the number of TCP payload bytes received for this UID. 168 * This total does not include protocol and control overheads at 169 * the transport and the lower layers of the networking stack. 170 * The statistics are across all interfaces. 171 * 172 * {@see android.os.Process#myUid()}. 173 * 174 * @param uid The UID of the process to examine. 175 * @return number of bytes. If the statistics are not supported by this device, 176 * {@link #UNSUPPORTED} will be returned. 177 */ 178 public static native long getUidTcpRxBytes(int uid); 179 180 /** 181 * Get the number of UDP payload bytes sent for this UID. 182 * This total does not include protocol and control overheads at 183 * the transport and the lower layers of the networking stack. 184 * The statistics are across all interfaces. 185 * 186 * {@see android.os.Process#myUid()}. 187 * 188 * @param uid The UID of the process to examine. 189 * @return number of bytes. If the statistics are not supported by this device, 190 * {@link #UNSUPPORTED} will be returned. 191 */ 192 public static native long getUidUdpTxBytes(int uid); 193 194 /** 195 * Get the number of UDP payload bytes received for this UID. 196 * This total does not include protocol and control overheads at 197 * the transport and the lower layers of the networking stack. 198 * The statistics are across all interfaces. 199 * 200 * {@see android.os.Process#myUid()}. 201 * 202 * @param uid The UID of the process to examine. 203 * @return number of bytes. If the statistics are not supported by this device, 204 * {@link #UNSUPPORTED} will be returned. 205 */ 206 public static native long getUidUdpRxBytes(int uid); 207 208 /** 209 * Get the number of TCP segments sent for this UID. 210 * Does not include TCP control packets (SYN/ACKs/FIN/..). 211 * The statistics are across all interfaces. 212 * 213 * {@see android.os.Process#myUid()}. 214 * 215 * @param uid The UID of the process to examine. 216 * @return number of TCP segments. If the statistics are not supported by this device, 217 * {@link #UNSUPPORTED} will be returned. 218 */ 219 public static native long getUidTcpTxSegments(int uid); 220 221 /** 222 * Get the number of TCP payload bytes received for this UID. 223 * Does not include TCP control packets (SYN/ACKs/FIN/..). 224 * The statistics are across all interfaces. 225 * 226 * {@see android.os.Process#myUid()}. 227 * 228 * @param uid The UID of the process to examine. 229 * @return number of TCP segments. If the statistics are not supported by this device, 230 * {@link #UNSUPPORTED} will be returned. 231 */ 232 public static native long getUidTcpRxSegments(int uid); 233 234 235 /** 236 * Get the number of UDP packets sent for this UID. 237 * Includes DNS requests. 238 * The statistics are across all interfaces. 239 * 240 * {@see android.os.Process#myUid()}. 241 * 242 * @param uid The UID of the process to examine. 243 * @return number of packets. If the statistics are not supported by this device, 244 * {@link #UNSUPPORTED} will be returned. 245 */ 246 public static native long getUidUdpTxPackets(int uid); 247 248 /** 249 * Get the number of UDP packets received for this UID. 250 * Includes DNS responses. 251 * The statistics are across all interfaces. 252 * 253 * {@see android.os.Process#myUid()}. 254 * 255 * @param uid The UID of the process to examine. 256 * @return number of packets. If the statistics are not supported by this device, 257 * {@link #UNSUPPORTED} will be returned. 258 */ 259 public static native long getUidUdpRxPackets(int uid); 260} 261