Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2012 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 androidx.core.net;
     18 
     19 import android.net.TrafficStats;
     20 import android.os.Build;
     21 import android.os.ParcelFileDescriptor;
     22 
     23 import androidx.annotation.NonNull;
     24 
     25 import java.net.DatagramSocket;
     26 import java.net.Socket;
     27 import java.net.SocketException;
     28 
     29 /**
     30  * Helper for accessing features in {@link TrafficStats}.
     31  */
     32 public final class TrafficStatsCompat {
     33     /**
     34      * Clear active tag used when accounting {@link Socket} traffic originating
     35      * from the current thread.
     36      *
     37      * @deprecated Use {@link TrafficStats#clearThreadStatsTag()} directly.
     38      */
     39     @Deprecated
     40     public static void clearThreadStatsTag() {
     41         TrafficStats.clearThreadStatsTag();
     42     }
     43 
     44     /**
     45      * Get the active tag used when accounting {@link Socket} traffic originating
     46      * from the current thread. Only one active tag per thread is supported.
     47      * {@link #tagSocket(Socket)}.
     48      *
     49      * @deprecated Use {@link TrafficStats#getThreadStatsTag()} directly.
     50      */
     51     @Deprecated
     52     public static int getThreadStatsTag() {
     53         return TrafficStats.getThreadStatsTag();
     54     }
     55 
     56     /**
     57      * Increment count of network operations performed under the accounting tag
     58      * currently active on the calling thread. This can be used to derive
     59      * bytes-per-operation.
     60      *
     61      * @param operationCount Number of operations to increment count by.
     62      *
     63      * @deprecated Use {@link TrafficStats#incrementOperationCount(int)} directly.
     64      */
     65     @Deprecated
     66     public static void incrementOperationCount(int operationCount) {
     67         TrafficStats.incrementOperationCount(operationCount);
     68     }
     69 
     70     /**
     71      * Increment count of network operations performed under the given
     72      * accounting tag. This can be used to derive bytes-per-operation.
     73      *
     74      * @param tag Accounting tag used in {@link #setThreadStatsTag(int)}.
     75      * @param operationCount Number of operations to increment count by.
     76      *
     77      * @deprecated Use {@link TrafficStats#incrementOperationCount(int, int)} directly.
     78      */
     79     @Deprecated
     80     public static void incrementOperationCount(int tag, int operationCount) {
     81         TrafficStats.incrementOperationCount(tag, operationCount);
     82     }
     83 
     84     /**
     85      * Set active tag to use when accounting {@link Socket} traffic originating
     86      * from the current thread. Only one active tag per thread is supported.
     87      * <p>
     88      * Changes only take effect during subsequent calls to
     89      * {@link #tagSocket(Socket)}.
     90      * <p>
     91      * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
     92      * used internally by system services like DownloadManager when performing
     93      * traffic on behalf of an application.
     94      *
     95      * @deprecated Use {@link TrafficStats#setThreadStatsTag(int)} directly.
     96      */
     97     @Deprecated
     98     public static void setThreadStatsTag(int tag) {
     99         TrafficStats.setThreadStatsTag(tag);
    100     }
    101 
    102     /**
    103      * Tag the given {@link Socket} with any statistics parameters active for
    104      * the current thread. Subsequent calls always replace any existing
    105      * parameters. When finished, call {@link #untagSocket(Socket)} to remove
    106      * statistics parameters.
    107      *
    108      * @see #setThreadStatsTag(int)
    109      *
    110      * @deprecated Use {@link TrafficStats#tagSocket(Socket)} directly.
    111      */
    112     @Deprecated
    113     public static void tagSocket(Socket socket) throws SocketException {
    114         TrafficStats.tagSocket(socket);
    115     }
    116 
    117     /**
    118      * Remove any statistics parameters from the given {@link Socket}.
    119      *
    120      * @deprecated Use {@link TrafficStats#untagSocket(Socket)} directly.
    121      */
    122     @Deprecated
    123     public static void untagSocket(Socket socket) throws SocketException {
    124         TrafficStats.untagSocket(socket);
    125     }
    126 
    127     /**
    128      * Tag the given {@link DatagramSocket} with any statistics parameters
    129      * active for the current thread. Subsequent calls always replace any
    130      * existing parameters. When finished, call
    131      * {@link #untagDatagramSocket(DatagramSocket)} to remove statistics
    132      * parameters.
    133      *
    134      * @see #setThreadStatsTag(int)
    135      */
    136     public static void tagDatagramSocket(@NonNull DatagramSocket socket) throws SocketException {
    137         if (Build.VERSION.SDK_INT >= 24) {
    138             TrafficStats.tagDatagramSocket(socket);
    139         } else {
    140             final ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(socket);
    141             TrafficStats.tagSocket(new DatagramSocketWrapper(socket, pfd.getFileDescriptor()));
    142             // The developer is still using the FD, so we need to detach it to
    143             // prevent the PFD finalizer from closing it in their face. We had to
    144             // wait until after the tagging call above, since detaching clears out
    145             // the getFileDescriptor() result which tagging depends on.
    146             pfd.detachFd();
    147         }
    148     }
    149 
    150     /**
    151      * Remove any statistics parameters from the given {@link DatagramSocket}.
    152      */
    153     public static void untagDatagramSocket(@NonNull DatagramSocket socket) throws SocketException {
    154         if (Build.VERSION.SDK_INT >= 24) {
    155             TrafficStats.untagDatagramSocket(socket);
    156         } else {
    157             final ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(socket);
    158             TrafficStats.untagSocket(new DatagramSocketWrapper(socket, pfd.getFileDescriptor()));
    159             // The developer is still using the FD, so we need to detach it to
    160             // prevent the PFD finalizer from closing it in their face. We had to
    161             // wait until after the tagging call above, since detaching clears out
    162             // the getFileDescriptor() result which tagging depends on.
    163             pfd.detachFd();
    164         }
    165     }
    166 
    167     private TrafficStatsCompat() {}
    168 }
    169