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.NonNull;
20 import android.annotation.SuppressLint;
21 import android.annotation.SystemApi;
22 import android.annotation.TestApi;
23 import android.app.DownloadManager;
24 import android.app.backup.BackupManager;
25 import android.app.usage.NetworkStatsManager;
26 import android.compat.annotation.UnsupportedAppUsage;
27 import android.content.Context;
28 import android.media.MediaPlayer;
29 import android.os.Build;
30 import android.os.RemoteException;
31 import android.os.ServiceManager;
32 import android.util.DataUnit;
33 
34 import com.android.server.NetworkManagementSocketTagger;
35 
36 import dalvik.system.SocketTagger;
37 
38 import java.io.FileDescriptor;
39 import java.io.IOException;
40 import java.net.DatagramSocket;
41 import java.net.Socket;
42 import java.net.SocketException;
43 
44 /**
45  * Class that provides network traffic statistics. These statistics include
46  * bytes transmitted and received and network packets transmitted and received,
47  * over all interfaces, over the mobile interface, and on a per-UID basis.
48  * <p>
49  * These statistics may not be available on all platforms. If the statistics are
50  * not supported by this device, {@link #UNSUPPORTED} will be returned.
51  * <p>
52  * Note that the statistics returned by this class reset and start from zero
53  * after every reboot. To access more robust historical network statistics data,
54  * use {@link NetworkStatsManager} instead.
55  */
56 public class TrafficStats {
57     /**
58      * The return value to indicate that the device does not support the statistic.
59      */
60     public final static int UNSUPPORTED = -1;
61 
62     /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
63     @Deprecated
64     public static final long KB_IN_BYTES = 1024;
65     /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
66     @Deprecated
67     public static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
68     /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
69     @Deprecated
70     public static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
71     /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
72     @Deprecated
73     public static final long TB_IN_BYTES = GB_IN_BYTES * 1024;
74     /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
75     @Deprecated
76     public static final long PB_IN_BYTES = TB_IN_BYTES * 1024;
77 
78     /**
79      * Special UID value used when collecting {@link NetworkStatsHistory} for
80      * removed applications.
81      *
82      * @hide
83      */
84     public static final int UID_REMOVED = -4;
85 
86     /**
87      * Special UID value used when collecting {@link NetworkStatsHistory} for
88      * tethering traffic.
89      *
90      * @hide
91      */
92     public static final int UID_TETHERING = NetworkStats.UID_TETHERING;
93 
94     /**
95      * Tag values in this range are reserved for the network stack. The network stack is
96      * running as UID {@link android.os.Process.NETWORK_STACK_UID} when in the mainline
97      * module separate process, and as the system UID otherwise.
98      */
99     /** @hide */
100     @SystemApi
101     public static final int TAG_NETWORK_STACK_RANGE_START = 0xFFFFFD00;
102     /** @hide */
103     @SystemApi
104     public static final int TAG_NETWORK_STACK_RANGE_END = 0xFFFFFEFF;
105 
106     /**
107      * Tags between 0xFFFFFF00 and 0xFFFFFFFF are reserved and used internally by system services
108      * like DownloadManager when performing traffic on behalf of an application.
109      */
110     // Please note there is no enforcement of these constants, so do not rely on them to
111     // determine that the caller is a system caller.
112     /** @hide */
113     @SystemApi
114     public static final int TAG_SYSTEM_IMPERSONATION_RANGE_START = 0xFFFFFF00;
115     /** @hide */
116     @SystemApi
117     public static final int TAG_SYSTEM_IMPERSONATION_RANGE_END = 0xFFFFFF0F;
118 
119     /**
120      * Tag values between these ranges are reserved for the network stack to do traffic
121      * on behalf of applications. It is a subrange of the range above.
122      */
123     /** @hide */
124     @SystemApi
125     public static final int TAG_NETWORK_STACK_IMPERSONATION_RANGE_START = 0xFFFFFF80;
126     /** @hide */
127     @SystemApi
128     public static final int TAG_NETWORK_STACK_IMPERSONATION_RANGE_END = 0xFFFFFF8F;
129 
130     /**
131      * Default tag value for {@link DownloadManager} traffic.
132      *
133      * @hide
134      */
135     public static final int TAG_SYSTEM_DOWNLOAD = 0xFFFFFF01;
136 
137     /**
138      * Default tag value for {@link MediaPlayer} traffic.
139      *
140      * @hide
141      */
142     public static final int TAG_SYSTEM_MEDIA = 0xFFFFFF02;
143 
144     /**
145      * Default tag value for {@link BackupManager} backup traffic; that is,
146      * traffic from the device to the storage backend.
147      *
148      * @hide
149      */
150     public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03;
151 
152     /**
153      * Default tag value for {@link BackupManager} restore traffic; that is,
154      * app data retrieved from the storage backend at install time.
155      *
156      * @hide
157      */
158     public static final int TAG_SYSTEM_RESTORE = 0xFFFFFF04;
159 
160     /**
161      * Default tag value for code (typically APKs) downloaded by an app store on
162      * behalf of the app, such as updates.
163      *
164      * @hide
165      */
166     public static final int TAG_SYSTEM_APP = 0xFFFFFF05;
167 
168     // TODO : remove this constant when Wifi code is updated
169     /** @hide */
170     public static final int TAG_SYSTEM_PROBE = 0xFFFFFF42;
171 
172     private static INetworkStatsService sStatsService;
173 
174     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
getStatsService()175     private synchronized static INetworkStatsService getStatsService() {
176         if (sStatsService == null) {
177             sStatsService = INetworkStatsService.Stub.asInterface(
178                     ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
179         }
180         return sStatsService;
181     }
182 
183     /**
184      * Snapshot of {@link NetworkStats} when the currently active profiling
185      * session started, or {@code null} if no session active.
186      *
187      * @see #startDataProfiling(Context)
188      * @see #stopDataProfiling(Context)
189      */
190     private static NetworkStats sActiveProfilingStart;
191 
192     private static Object sProfilingLock = new Object();
193 
194     private static final String LOOPBACK_IFACE = "lo";
195 
196     /**
197      * Set active tag to use when accounting {@link Socket} traffic originating
198      * from the current thread. Only one active tag per thread is supported.
199      * <p>
200      * Changes only take effect during subsequent calls to
201      * {@link #tagSocket(Socket)}.
202      * <p>
203      * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
204      * used internally by system services like {@link DownloadManager} when
205      * performing traffic on behalf of an application.
206      *
207      * @see #clearThreadStatsTag()
208      */
setThreadStatsTag(int tag)209     public static void setThreadStatsTag(int tag) {
210         NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
211     }
212 
213     /**
214      * Set active tag to use when accounting {@link Socket} traffic originating
215      * from the current thread. Only one active tag per thread is supported.
216      * <p>
217      * Changes only take effect during subsequent calls to
218      * {@link #tagSocket(Socket)}.
219      * <p>
220      * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
221      * used internally by system services like {@link DownloadManager} when
222      * performing traffic on behalf of an application.
223      *
224      * @return the current tag for the calling thread, which can be used to
225      *         restore any existing values after a nested operation is finished
226      */
getAndSetThreadStatsTag(int tag)227     public static int getAndSetThreadStatsTag(int tag) {
228         return NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
229     }
230 
231     /**
232      * Set active tag to use when accounting {@link Socket} traffic originating
233      * from the current thread. The tag used internally is well-defined to
234      * distinguish all backup-related traffic.
235      *
236      * @hide
237      */
238     @SystemApi
setThreadStatsTagBackup()239     public static void setThreadStatsTagBackup() {
240         setThreadStatsTag(TAG_SYSTEM_BACKUP);
241     }
242 
243     /**
244      * Set active tag to use when accounting {@link Socket} traffic originating
245      * from the current thread. The tag used internally is well-defined to
246      * distinguish all restore-related traffic.
247      *
248      * @hide
249      */
250     @SystemApi
setThreadStatsTagRestore()251     public static void setThreadStatsTagRestore() {
252         setThreadStatsTag(TAG_SYSTEM_RESTORE);
253     }
254 
255     /**
256      * Set active tag to use when accounting {@link Socket} traffic originating
257      * from the current thread. The tag used internally is well-defined to
258      * distinguish all code (typically APKs) downloaded by an app store on
259      * behalf of the app, such as updates.
260      *
261      * @hide
262      */
263     @SystemApi
setThreadStatsTagApp()264     public static void setThreadStatsTagApp() {
265         setThreadStatsTag(TAG_SYSTEM_APP);
266     }
267 
268     /**
269      * Get the active tag used when accounting {@link Socket} traffic originating
270      * from the current thread. Only one active tag per thread is supported.
271      * {@link #tagSocket(Socket)}.
272      *
273      * @see #setThreadStatsTag(int)
274      */
getThreadStatsTag()275     public static int getThreadStatsTag() {
276         return NetworkManagementSocketTagger.getThreadSocketStatsTag();
277     }
278 
279     /**
280      * Clear any active tag set to account {@link Socket} traffic originating
281      * from the current thread.
282      *
283      * @see #setThreadStatsTag(int)
284      */
clearThreadStatsTag()285     public static void clearThreadStatsTag() {
286         NetworkManagementSocketTagger.setThreadSocketStatsTag(-1);
287     }
288 
289     /**
290      * Set specific UID to use when accounting {@link Socket} traffic
291      * originating from the current thread. Designed for use when performing an
292      * operation on behalf of another application, or when another application
293      * is performing operations on your behalf.
294      * <p>
295      * Any app can <em>accept</em> blame for traffic performed on a socket
296      * originally created by another app by calling this method with the
297      * {@link android.system.Os#getuid()} value. However, only apps holding the
298      * {@code android.Manifest.permission#UPDATE_DEVICE_STATS} permission may
299      * <em>assign</em> blame to another UIDs.
300      * <p>
301      * Changes only take effect during subsequent calls to
302      * {@link #tagSocket(Socket)}.
303      */
304     @SuppressLint("Doclava125")
setThreadStatsUid(int uid)305     public static void setThreadStatsUid(int uid) {
306         NetworkManagementSocketTagger.setThreadSocketStatsUid(uid);
307     }
308 
309     /**
310      * Get the active UID used when accounting {@link Socket} traffic originating
311      * from the current thread. Only one active tag per thread is supported.
312      * {@link #tagSocket(Socket)}.
313      *
314      * @see #setThreadStatsUid(int)
315      */
getThreadStatsUid()316     public static int getThreadStatsUid() {
317         return NetworkManagementSocketTagger.getThreadSocketStatsUid();
318     }
319 
320     /**
321      * Set specific UID to use when accounting {@link Socket} traffic
322      * originating from the current thread as the calling UID. Designed for use
323      * when another application is performing operations on your behalf.
324      * <p>
325      * Changes only take effect during subsequent calls to
326      * {@link #tagSocket(Socket)}.
327      *
328      * @removed
329      * @deprecated use {@link #setThreadStatsUid(int)} instead.
330      */
331     @Deprecated
setThreadStatsUidSelf()332     public static void setThreadStatsUidSelf() {
333         setThreadStatsUid(android.os.Process.myUid());
334     }
335 
336     /**
337      * Clear any active UID set to account {@link Socket} traffic originating
338      * from the current thread.
339      *
340      * @see #setThreadStatsUid(int)
341      */
342     @SuppressLint("Doclava125")
clearThreadStatsUid()343     public static void clearThreadStatsUid() {
344         NetworkManagementSocketTagger.setThreadSocketStatsUid(-1);
345     }
346 
347     /**
348      * Tag the given {@link Socket} with any statistics parameters active for
349      * the current thread. Subsequent calls always replace any existing
350      * parameters. When finished, call {@link #untagSocket(Socket)} to remove
351      * statistics parameters.
352      *
353      * @see #setThreadStatsTag(int)
354      */
tagSocket(Socket socket)355     public static void tagSocket(Socket socket) throws SocketException {
356         SocketTagger.get().tag(socket);
357     }
358 
359     /**
360      * Remove any statistics parameters from the given {@link Socket}.
361      * <p>
362      * In Android 8.1 (API level 27) and lower, a socket is automatically
363      * untagged when it's sent to another process using binder IPC with a
364      * {@code ParcelFileDescriptor} container. In Android 9.0 (API level 28)
365      * and higher, the socket tag is kept when the socket is sent to another
366      * process using binder IPC. You can mimic the previous behavior by
367      * calling {@code untagSocket()} before sending the socket to another
368      * process.
369      */
untagSocket(Socket socket)370     public static void untagSocket(Socket socket) throws SocketException {
371         SocketTagger.get().untag(socket);
372     }
373 
374     /**
375      * Tag the given {@link DatagramSocket} with any statistics parameters
376      * active for the current thread. Subsequent calls always replace any
377      * existing parameters. When finished, call
378      * {@link #untagDatagramSocket(DatagramSocket)} to remove statistics
379      * parameters.
380      *
381      * @see #setThreadStatsTag(int)
382      */
tagDatagramSocket(DatagramSocket socket)383     public static void tagDatagramSocket(DatagramSocket socket) throws SocketException {
384         SocketTagger.get().tag(socket);
385     }
386 
387     /**
388      * Remove any statistics parameters from the given {@link DatagramSocket}.
389      */
untagDatagramSocket(DatagramSocket socket)390     public static void untagDatagramSocket(DatagramSocket socket) throws SocketException {
391         SocketTagger.get().untag(socket);
392     }
393 
394     /**
395      * Tag the given {@link FileDescriptor} socket with any statistics
396      * parameters active for the current thread. Subsequent calls always replace
397      * any existing parameters. When finished, call
398      * {@link #untagFileDescriptor(FileDescriptor)} to remove statistics
399      * parameters.
400      *
401      * @see #setThreadStatsTag(int)
402      */
tagFileDescriptor(FileDescriptor fd)403     public static void tagFileDescriptor(FileDescriptor fd) throws IOException {
404         SocketTagger.get().tag(fd);
405     }
406 
407     /**
408      * Remove any statistics parameters from the given {@link FileDescriptor}
409      * socket.
410      */
untagFileDescriptor(FileDescriptor fd)411     public static void untagFileDescriptor(FileDescriptor fd) throws IOException {
412         SocketTagger.get().untag(fd);
413     }
414 
415     /**
416      * Start profiling data usage for current UID. Only one profiling session
417      * can be active at a time.
418      *
419      * @hide
420      */
startDataProfiling(Context context)421     public static void startDataProfiling(Context context) {
422         synchronized (sProfilingLock) {
423             if (sActiveProfilingStart != null) {
424                 throw new IllegalStateException("already profiling data");
425             }
426 
427             // take snapshot in time; we calculate delta later
428             sActiveProfilingStart = getDataLayerSnapshotForUid(context);
429         }
430     }
431 
432     /**
433      * Stop profiling data usage for current UID.
434      *
435      * @return Detailed {@link NetworkStats} of data that occurred since last
436      *         {@link #startDataProfiling(Context)} call.
437      * @hide
438      */
stopDataProfiling(Context context)439     public static NetworkStats stopDataProfiling(Context context) {
440         synchronized (sProfilingLock) {
441             if (sActiveProfilingStart == null) {
442                 throw new IllegalStateException("not profiling data");
443             }
444 
445             // subtract starting values and return delta
446             final NetworkStats profilingStop = getDataLayerSnapshotForUid(context);
447             final NetworkStats profilingDelta = NetworkStats.subtract(
448                     profilingStop, sActiveProfilingStart, null, null);
449             sActiveProfilingStart = null;
450             return profilingDelta;
451         }
452     }
453 
454     /**
455      * Increment count of network operations performed under the accounting tag
456      * currently active on the calling thread. This can be used to derive
457      * bytes-per-operation.
458      *
459      * @param operationCount Number of operations to increment count by.
460      */
incrementOperationCount(int operationCount)461     public static void incrementOperationCount(int operationCount) {
462         final int tag = getThreadStatsTag();
463         incrementOperationCount(tag, operationCount);
464     }
465 
466     /**
467      * Increment count of network operations performed under the given
468      * accounting tag. This can be used to derive bytes-per-operation.
469      *
470      * @param tag Accounting tag used in {@link #setThreadStatsTag(int)}.
471      * @param operationCount Number of operations to increment count by.
472      */
incrementOperationCount(int tag, int operationCount)473     public static void incrementOperationCount(int tag, int operationCount) {
474         final int uid = android.os.Process.myUid();
475         try {
476             getStatsService().incrementOperationCount(uid, tag, operationCount);
477         } catch (RemoteException e) {
478             throw e.rethrowFromSystemServer();
479         }
480     }
481 
482     /** {@hide} */
closeQuietly(INetworkStatsSession session)483     public static void closeQuietly(INetworkStatsSession session) {
484         // TODO: move to NetworkStatsService once it exists
485         if (session != null) {
486             try {
487                 session.close();
488             } catch (RuntimeException rethrown) {
489                 throw rethrown;
490             } catch (Exception ignored) {
491             }
492         }
493     }
494 
addIfSupported(long stat)495     private static long addIfSupported(long stat) {
496         return (stat == UNSUPPORTED) ? 0 : stat;
497     }
498 
499     /**
500      * Return number of packets transmitted across mobile networks since device
501      * boot. Counts packets across all mobile network interfaces, and always
502      * increases monotonically since device boot. Statistics are measured at the
503      * network layer, so they include both TCP and UDP usage.
504      * <p>
505      * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
506      * return {@link #UNSUPPORTED} on devices where statistics aren't available.
507      */
getMobileTxPackets()508     public static long getMobileTxPackets() {
509         long total = 0;
510         for (String iface : getMobileIfaces()) {
511             total += addIfSupported(getTxPackets(iface));
512         }
513         return total;
514     }
515 
516     /**
517      * Return number of packets received across mobile networks since device
518      * boot. Counts packets across all mobile network interfaces, and always
519      * increases monotonically since device boot. Statistics are measured at the
520      * network layer, so they include both TCP and UDP usage.
521      * <p>
522      * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
523      * return {@link #UNSUPPORTED} on devices where statistics aren't available.
524      */
getMobileRxPackets()525     public static long getMobileRxPackets() {
526         long total = 0;
527         for (String iface : getMobileIfaces()) {
528             total += addIfSupported(getRxPackets(iface));
529         }
530         return total;
531     }
532 
533     /**
534      * Return number of bytes transmitted across mobile networks since device
535      * boot. Counts packets across all mobile network interfaces, and always
536      * increases monotonically since device boot. Statistics are measured at the
537      * network layer, so they include both TCP and UDP usage.
538      * <p>
539      * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
540      * return {@link #UNSUPPORTED} on devices where statistics aren't available.
541      */
getMobileTxBytes()542     public static long getMobileTxBytes() {
543         long total = 0;
544         for (String iface : getMobileIfaces()) {
545             total += addIfSupported(getTxBytes(iface));
546         }
547         return total;
548     }
549 
550     /**
551      * Return number of bytes received across mobile networks since device boot.
552      * Counts packets across all mobile network interfaces, and always increases
553      * monotonically since device boot. Statistics are measured at the network
554      * layer, so they include both TCP and UDP usage.
555      * <p>
556      * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
557      * return {@link #UNSUPPORTED} on devices where statistics aren't available.
558      */
getMobileRxBytes()559     public static long getMobileRxBytes() {
560         long total = 0;
561         for (String iface : getMobileIfaces()) {
562             total += addIfSupported(getRxBytes(iface));
563         }
564         return total;
565     }
566 
567     /** {@hide} */
568     @UnsupportedAppUsage
getMobileTcpRxPackets()569     public static long getMobileTcpRxPackets() {
570         long total = 0;
571         for (String iface : getMobileIfaces()) {
572             long stat = UNSUPPORTED;
573             try {
574                 stat = getStatsService().getIfaceStats(iface, TYPE_TCP_RX_PACKETS);
575             } catch (RemoteException e) {
576                 throw e.rethrowFromSystemServer();
577             }
578             total += addIfSupported(stat);
579         }
580         return total;
581     }
582 
583     /** {@hide} */
584     @UnsupportedAppUsage
getMobileTcpTxPackets()585     public static long getMobileTcpTxPackets() {
586         long total = 0;
587         for (String iface : getMobileIfaces()) {
588             long stat = UNSUPPORTED;
589             try {
590                 stat = getStatsService().getIfaceStats(iface, TYPE_TCP_TX_PACKETS);
591             } catch (RemoteException e) {
592                 throw e.rethrowFromSystemServer();
593             }
594             total += addIfSupported(stat);
595         }
596         return total;
597     }
598 
599     /**
600      * Return the number of packets transmitted on the specified interface since
601      * device boot. Statistics are measured at the network layer, so both TCP and
602      * UDP usage are included.
603      *
604      * @param iface The name of the interface.
605      * @return The number of transmitted packets.
606      */
getTxPackets(@onNull String iface)607     public static long getTxPackets(@NonNull String iface) {
608         try {
609             return getStatsService().getIfaceStats(iface, TYPE_TX_PACKETS);
610         } catch (RemoteException e) {
611             throw e.rethrowFromSystemServer();
612         }
613     }
614 
615     /**
616      * Return the number of packets received on the specified interface since
617      * device boot. Statistics are measured at the network layer, so both TCP
618      * and UDP usage are included.
619      *
620      * @param iface The name of the interface.
621      * @return The number of received packets.
622      */
getRxPackets(@onNull String iface)623     public static long getRxPackets(@NonNull String iface) {
624         try {
625             return getStatsService().getIfaceStats(iface, TYPE_RX_PACKETS);
626         } catch (RemoteException e) {
627             throw e.rethrowFromSystemServer();
628         }
629     }
630 
631     /** {@hide} */
632     @UnsupportedAppUsage
getTxBytes(String iface)633     public static long getTxBytes(String iface) {
634         try {
635             return getStatsService().getIfaceStats(iface, TYPE_TX_BYTES);
636         } catch (RemoteException e) {
637             throw e.rethrowFromSystemServer();
638         }
639     }
640 
641     /** {@hide} */
642     @UnsupportedAppUsage
getRxBytes(String iface)643     public static long getRxBytes(String iface) {
644         try {
645             return getStatsService().getIfaceStats(iface, TYPE_RX_BYTES);
646         } catch (RemoteException e) {
647             throw e.rethrowFromSystemServer();
648         }
649     }
650 
651     /** {@hide} */
652     @TestApi
getLoopbackTxPackets()653     public static long getLoopbackTxPackets() {
654         try {
655             return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_TX_PACKETS);
656         } catch (RemoteException e) {
657             throw e.rethrowFromSystemServer();
658         }
659     }
660 
661     /** {@hide} */
662     @TestApi
getLoopbackRxPackets()663     public static long getLoopbackRxPackets() {
664         try {
665             return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_RX_PACKETS);
666         } catch (RemoteException e) {
667             throw e.rethrowFromSystemServer();
668         }
669     }
670 
671     /** {@hide} */
672     @TestApi
getLoopbackTxBytes()673     public static long getLoopbackTxBytes() {
674         try {
675             return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_TX_BYTES);
676         } catch (RemoteException e) {
677             throw e.rethrowFromSystemServer();
678         }
679     }
680 
681     /** {@hide} */
682     @TestApi
getLoopbackRxBytes()683     public static long getLoopbackRxBytes() {
684         try {
685             return getStatsService().getIfaceStats(LOOPBACK_IFACE, TYPE_RX_BYTES);
686         } catch (RemoteException e) {
687             throw e.rethrowFromSystemServer();
688         }
689     }
690 
691     /**
692      * Return number of packets transmitted since device boot. Counts packets
693      * across all network interfaces, and always increases monotonically since
694      * device boot. Statistics are measured at the network layer, so they
695      * include both TCP and UDP usage.
696      * <p>
697      * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
698      * return {@link #UNSUPPORTED} on devices where statistics aren't available.
699      */
getTotalTxPackets()700     public static long getTotalTxPackets() {
701         try {
702             return getStatsService().getTotalStats(TYPE_TX_PACKETS);
703         } catch (RemoteException e) {
704             throw e.rethrowFromSystemServer();
705         }
706     }
707 
708     /**
709      * Return number of packets received since device boot. Counts packets
710      * across all network interfaces, and always increases monotonically since
711      * device boot. Statistics are measured at the network layer, so they
712      * include both TCP and UDP usage.
713      * <p>
714      * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
715      * return {@link #UNSUPPORTED} on devices where statistics aren't available.
716      */
getTotalRxPackets()717     public static long getTotalRxPackets() {
718         try {
719             return getStatsService().getTotalStats(TYPE_RX_PACKETS);
720         } catch (RemoteException e) {
721             throw e.rethrowFromSystemServer();
722         }
723     }
724 
725     /**
726      * Return number of bytes transmitted since device boot. Counts packets
727      * across all network interfaces, and always increases monotonically since
728      * device boot. Statistics are measured at the network layer, so they
729      * include both TCP and UDP usage.
730      * <p>
731      * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
732      * return {@link #UNSUPPORTED} on devices where statistics aren't available.
733      */
getTotalTxBytes()734     public static long getTotalTxBytes() {
735         try {
736             return getStatsService().getTotalStats(TYPE_TX_BYTES);
737         } catch (RemoteException e) {
738             throw e.rethrowFromSystemServer();
739         }
740     }
741 
742     /**
743      * Return number of bytes received since device boot. Counts packets across
744      * all network interfaces, and always increases monotonically since device
745      * boot. Statistics are measured at the network layer, so they include both
746      * TCP and UDP usage.
747      * <p>
748      * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
749      * return {@link #UNSUPPORTED} on devices where statistics aren't available.
750      */
getTotalRxBytes()751     public static long getTotalRxBytes() {
752         try {
753             return getStatsService().getTotalStats(TYPE_RX_BYTES);
754         } catch (RemoteException e) {
755             throw e.rethrowFromSystemServer();
756         }
757     }
758 
759     /**
760      * Return number of bytes transmitted by the given UID since device boot.
761      * Counts packets across all network interfaces, and always increases
762      * monotonically since device boot. Statistics are measured at the network
763      * layer, so they include both TCP and UDP usage.
764      * <p>
765      * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
766      * return {@link #UNSUPPORTED} on devices where statistics aren't available.
767      * <p>
768      * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
769      * report traffic statistics for the calling UID. It will return
770      * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
771      * historical network statistics belonging to other UIDs, use
772      * {@link NetworkStatsManager}.
773      *
774      * @see android.os.Process#myUid()
775      * @see android.content.pm.ApplicationInfo#uid
776      */
getUidTxBytes(int uid)777     public static long getUidTxBytes(int uid) {
778         try {
779             return getStatsService().getUidStats(uid, TYPE_TX_BYTES);
780         } catch (RemoteException e) {
781             throw e.rethrowFromSystemServer();
782         }
783     }
784 
785     /**
786      * Return number of bytes received by the given UID since device boot.
787      * Counts packets across all network interfaces, and always increases
788      * monotonically since device boot. Statistics are measured at the network
789      * layer, so they include both TCP and UDP usage.
790      * <p>
791      * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
792      * {@link #UNSUPPORTED} on devices where statistics aren't available.
793      * <p>
794      * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
795      * report traffic statistics for the calling UID. It will return
796      * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
797      * historical network statistics belonging to other UIDs, use
798      * {@link NetworkStatsManager}.
799      *
800      * @see android.os.Process#myUid()
801      * @see android.content.pm.ApplicationInfo#uid
802      */
getUidRxBytes(int uid)803     public static long getUidRxBytes(int uid) {
804         try {
805             return getStatsService().getUidStats(uid, TYPE_RX_BYTES);
806         } catch (RemoteException e) {
807             throw e.rethrowFromSystemServer();
808         }
809     }
810 
811     /**
812      * Return number of packets transmitted by the given UID since device boot.
813      * Counts packets across all network interfaces, and always increases
814      * monotonically since device boot. Statistics are measured at the network
815      * layer, so they include both TCP and UDP usage.
816      * <p>
817      * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
818      * {@link #UNSUPPORTED} on devices where statistics aren't available.
819      * <p>
820      * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
821      * report traffic statistics for the calling UID. It will return
822      * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
823      * historical network statistics belonging to other UIDs, use
824      * {@link NetworkStatsManager}.
825      *
826      * @see android.os.Process#myUid()
827      * @see android.content.pm.ApplicationInfo#uid
828      */
getUidTxPackets(int uid)829     public static long getUidTxPackets(int uid) {
830         try {
831             return getStatsService().getUidStats(uid, TYPE_TX_PACKETS);
832         } catch (RemoteException e) {
833             throw e.rethrowFromSystemServer();
834         }
835     }
836 
837     /**
838      * Return number of packets received by the given UID since device boot.
839      * Counts packets across all network interfaces, and always increases
840      * monotonically since device boot. Statistics are measured at the network
841      * layer, so they include both TCP and UDP usage.
842      * <p>
843      * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
844      * {@link #UNSUPPORTED} on devices where statistics aren't available.
845      * <p>
846      * Starting in {@link android.os.Build.VERSION_CODES#N} this will only
847      * report traffic statistics for the calling UID. It will return
848      * {@link #UNSUPPORTED} for all other UIDs for privacy reasons. To access
849      * historical network statistics belonging to other UIDs, use
850      * {@link NetworkStatsManager}.
851      *
852      * @see android.os.Process#myUid()
853      * @see android.content.pm.ApplicationInfo#uid
854      */
getUidRxPackets(int uid)855     public static long getUidRxPackets(int uid) {
856         try {
857             return getStatsService().getUidStats(uid, TYPE_RX_PACKETS);
858         } catch (RemoteException e) {
859             throw e.rethrowFromSystemServer();
860         }
861     }
862 
863     /**
864      * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
865      *             transport layer statistics are no longer available, and will
866      *             always return {@link #UNSUPPORTED}.
867      * @see #getUidTxBytes(int)
868      */
869     @Deprecated
getUidTcpTxBytes(int uid)870     public static long getUidTcpTxBytes(int uid) {
871         return UNSUPPORTED;
872     }
873 
874     /**
875      * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
876      *             transport layer statistics are no longer available, and will
877      *             always return {@link #UNSUPPORTED}.
878      * @see #getUidRxBytes(int)
879      */
880     @Deprecated
getUidTcpRxBytes(int uid)881     public static long getUidTcpRxBytes(int uid) {
882         return UNSUPPORTED;
883     }
884 
885     /**
886      * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
887      *             transport layer statistics are no longer available, and will
888      *             always return {@link #UNSUPPORTED}.
889      * @see #getUidTxBytes(int)
890      */
891     @Deprecated
getUidUdpTxBytes(int uid)892     public static long getUidUdpTxBytes(int uid) {
893         return UNSUPPORTED;
894     }
895 
896     /**
897      * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
898      *             transport layer statistics are no longer available, and will
899      *             always return {@link #UNSUPPORTED}.
900      * @see #getUidRxBytes(int)
901      */
902     @Deprecated
getUidUdpRxBytes(int uid)903     public static long getUidUdpRxBytes(int uid) {
904         return UNSUPPORTED;
905     }
906 
907     /**
908      * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
909      *             transport layer statistics are no longer available, and will
910      *             always return {@link #UNSUPPORTED}.
911      * @see #getUidTxPackets(int)
912      */
913     @Deprecated
getUidTcpTxSegments(int uid)914     public static long getUidTcpTxSegments(int uid) {
915         return UNSUPPORTED;
916     }
917 
918     /**
919      * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
920      *             transport layer statistics are no longer available, and will
921      *             always return {@link #UNSUPPORTED}.
922      * @see #getUidRxPackets(int)
923      */
924     @Deprecated
getUidTcpRxSegments(int uid)925     public static long getUidTcpRxSegments(int uid) {
926         return UNSUPPORTED;
927     }
928 
929     /**
930      * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
931      *             transport layer statistics are no longer available, and will
932      *             always return {@link #UNSUPPORTED}.
933      * @see #getUidTxPackets(int)
934      */
935     @Deprecated
getUidUdpTxPackets(int uid)936     public static long getUidUdpTxPackets(int uid) {
937         return UNSUPPORTED;
938     }
939 
940     /**
941      * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
942      *             transport layer statistics are no longer available, and will
943      *             always return {@link #UNSUPPORTED}.
944      * @see #getUidRxPackets(int)
945      */
946     @Deprecated
getUidUdpRxPackets(int uid)947     public static long getUidUdpRxPackets(int uid) {
948         return UNSUPPORTED;
949     }
950 
951     /**
952      * Return detailed {@link NetworkStats} for the current UID. Requires no
953      * special permission.
954      */
getDataLayerSnapshotForUid(Context context)955     private static NetworkStats getDataLayerSnapshotForUid(Context context) {
956         // TODO: take snapshot locally, since proc file is now visible
957         final int uid = android.os.Process.myUid();
958         try {
959             return getStatsService().getDataLayerSnapshotForUid(uid);
960         } catch (RemoteException e) {
961             throw e.rethrowFromSystemServer();
962         }
963     }
964 
965     /**
966      * Return set of any ifaces associated with mobile networks since boot.
967      * Interfaces are never removed from this list, so counters should always be
968      * monotonic.
969      */
970     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
getMobileIfaces()971     private static String[] getMobileIfaces() {
972         try {
973             return getStatsService().getMobileIfaces();
974         } catch (RemoteException e) {
975             throw e.rethrowFromSystemServer();
976         }
977     }
978 
979     // NOTE: keep these in sync with android_net_TrafficStats.cpp
980     private static final int TYPE_RX_BYTES = 0;
981     private static final int TYPE_RX_PACKETS = 1;
982     private static final int TYPE_TX_BYTES = 2;
983     private static final int TYPE_TX_PACKETS = 3;
984     private static final int TYPE_TCP_RX_PACKETS = 4;
985     private static final int TYPE_TCP_TX_PACKETS = 5;
986 }
987