TrafficStats.java revision 227bec49157bc496f7c9e8e8f63c12728a448922
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 transmitted through the specified interface.
73     *
74     * @return number of packets.  If the statistics are not supported by this interface,
75     * {@link #UNSUPPORTED} will be returned.
76     * @hide
77     */
78    public static native long getTxPackets(String iface);
79
80    /**
81     * Get the total number of packets received through the specified interface.
82     *
83     * @return number of packets.  If the statistics are not supported by this interface,
84     * {@link #UNSUPPORTED} will be returned.
85     * @hide
86     */
87    public static native long getRxPackets(String iface);
88
89    /**
90     * Get the total number of bytes transmitted through the specified interface.
91     *
92     * @return number of bytes.  If the statistics are not supported by this interface,
93     * {@link #UNSUPPORTED} will be returned.
94     * @hide
95     */
96    public static native long getTxBytes(String iface);
97
98    /**
99     * Get the total number of bytes received through the specified interface.
100     *
101     * @return number of bytes.  If the statistics are not supported by this interface,
102     * {@link #UNSUPPORTED} will be returned.
103     * @hide
104     */
105    public static native long getRxBytes(String iface);
106
107
108    /**
109     * Get the total number of packets sent through all network interfaces.
110     *
111     * @return the number of packets.  If the statistics are not supported by this device,
112     * {@link #UNSUPPORTED} will be returned.
113     */
114    public static native long getTotalTxPackets();
115
116    /**
117     * Get the total number of packets received through all network interfaces.
118     *
119     * @return number of packets.  If the statistics are not supported by this device,
120     * {@link #UNSUPPORTED} will be returned.
121     */
122    public static native long getTotalRxPackets();
123
124    /**
125     * Get the total number of bytes sent through all network interfaces.
126     *
127     * @return number of bytes.  If the statistics are not supported by this device,
128     * {@link #UNSUPPORTED} will be returned.
129     */
130    public static native long getTotalTxBytes();
131
132    /**
133     * Get the total number of bytes received through all network interfaces.
134     *
135     * @return number of bytes.  If the statistics are not supported by this device,
136     * {@link #UNSUPPORTED} will be returned.
137     */
138    public static native long getTotalRxBytes();
139
140    /**
141     * Get the number of bytes sent through the network for this UID.
142     * The statistics are across all interfaces.
143     *
144     * {@see android.os.Process#myUid()}.
145     *
146     * @param uid The UID of the process to examine.
147     * @return number of bytes.  If the statistics are not supported by this device,
148     * {@link #UNSUPPORTED} will be returned.
149     */
150    public static native long getUidTxBytes(int uid);
151
152    /**
153     * Get the number of bytes received through the network for this UID.
154     * The statistics are across all interfaces.
155     *
156     * {@see android.os.Process#myUid()}.
157     *
158     * @param uid The UID of the process to examine.
159     * @return number of bytes
160     */
161    public static native long getUidRxBytes(int uid);
162
163    /**
164     * Get the number of packets (TCP segments + UDP) sent through
165     * the network for this UID.
166     * The statistics are across all interfaces.
167     *
168     * {@see android.os.Process#myUid()}.
169     *
170     * @param uid The UID of the process to examine.
171     * @return number of packets.
172     * If the statistics are not supported by this device,
173     * {@link #UNSUPPORTED} will be returned.
174     */
175    public static native long getUidTxPackets(int uid);
176
177    /**
178     * Get the number of packets (TCP segments + UDP) received through
179     * the network for this UID.
180     * The statistics are across all interfaces.
181     *
182     * {@see android.os.Process#myUid()}.
183     *
184     * @param uid The UID of the process to examine.
185     * @return number of packets
186     */
187    public static native long getUidRxPackets(int uid);
188
189    /**
190     * Get the number of TCP payload bytes sent for this UID.
191     * This total does not include protocol and control overheads at
192     * the transport and the lower layers of the networking stack.
193     * The statistics are across all interfaces.
194     *
195     * {@see android.os.Process#myUid()}.
196     *
197     * @param uid The UID of the process to examine.
198     * @return number of bytes.  If the statistics are not supported by this device,
199     * {@link #UNSUPPORTED} will be returned.
200     */
201    public static native long getUidTcpTxBytes(int uid);
202
203    /**
204     * Get the number of TCP payload bytes received for this UID.
205     * This total does not include protocol and control overheads at
206     * the transport and the lower layers of the networking stack.
207     * The statistics are across all interfaces.
208     *
209     * {@see android.os.Process#myUid()}.
210     *
211     * @param uid The UID of the process to examine.
212     * @return number of bytes.  If the statistics are not supported by this device,
213     * {@link #UNSUPPORTED} will be returned.
214     */
215    public static native long getUidTcpRxBytes(int uid);
216
217    /**
218     * Get the number of UDP payload bytes sent for this UID.
219     * This total does not include protocol and control overheads at
220     * the transport and the lower layers of the networking stack.
221     * The statistics are across all interfaces.
222     *
223     * {@see android.os.Process#myUid()}.
224     *
225     * @param uid The UID of the process to examine.
226     * @return number of bytes.  If the statistics are not supported by this device,
227     * {@link #UNSUPPORTED} will be returned.
228     */
229    public static native long getUidUdpTxBytes(int uid);
230
231    /**
232     * Get the number of UDP payload bytes received for this UID.
233     * This total does not include protocol and control overheads at
234     * the transport and the lower layers of the networking stack.
235     * The statistics are across all interfaces.
236     *
237     * {@see android.os.Process#myUid()}.
238     *
239     * @param uid The UID of the process to examine.
240     * @return number of bytes.  If the statistics are not supported by this device,
241     * {@link #UNSUPPORTED} will be returned.
242     */
243    public static native long getUidUdpRxBytes(int uid);
244
245    /**
246     * Get the number of TCP segments sent for this UID.
247     * Does not include TCP control packets (SYN/ACKs/FIN/..).
248     * The statistics are across all interfaces.
249     *
250     * {@see android.os.Process#myUid()}.
251     *
252     * @param uid The UID of the process to examine.
253     * @return number of TCP segments.  If the statistics are not supported by this device,
254     * {@link #UNSUPPORTED} will be returned.
255     */
256    public static native long getUidTcpTxSegments(int uid);
257
258    /**
259     * Get the number of TCP segments received for this UID.
260     * Does not include TCP control packets (SYN/ACKs/FIN/..).
261     * The statistics are across all interfaces.
262     *
263     * {@see android.os.Process#myUid()}.
264     *
265     * @param uid The UID of the process to examine.
266     * @return number of TCP segments.  If the statistics are not supported by this device,
267     * {@link #UNSUPPORTED} will be returned.
268     */
269    public static native long getUidTcpRxSegments(int uid);
270
271
272    /**
273     * Get the number of UDP packets sent for this UID.
274     * Includes DNS requests.
275     * The statistics are across all interfaces.
276     *
277     * {@see android.os.Process#myUid()}.
278     *
279     * @param uid The UID of the process to examine.
280     * @return number of packets.  If the statistics are not supported by this device,
281     * {@link #UNSUPPORTED} will be returned.
282     */
283    public static native long getUidUdpTxPackets(int uid);
284
285    /**
286     * Get the number of UDP packets received for this UID.
287     * Includes DNS responses.
288     * The statistics are across all interfaces.
289     *
290     * {@see android.os.Process#myUid()}.
291     *
292     * @param uid The UID of the process to examine.
293     * @return number of packets.  If the statistics are not supported by this device,
294     * {@link #UNSUPPORTED} will be returned.
295     */
296    public static native long getUidUdpRxPackets(int uid);
297}
298