Java Code Examples for android.net.TrafficStats#tagSocket()

The following examples show how to use android.net.TrafficStats#tagSocket() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AndroidNetworkLibrary.java    From cronet with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Tag socket referenced by {@code ifd} with {@code tag} for UID {@code uid}.
 *
 * Assumes thread UID tag isn't set upon entry, and ensures thread UID tag isn't set upon exit.
 * Unfortunately there is no TrafficStatis.getThreadStatsUid().
 */
@CalledByNative
private static void tagSocket(int ifd, int uid, int tag) throws IOException {
    // Set thread tags.
    int oldTag = TrafficStats.getThreadStatsTag();
    if (tag != oldTag) {
        TrafficStats.setThreadStatsTag(tag);
    }
    if (uid != TrafficStatsUid.UNSET) {
        ThreadStatsUid.set(uid);
    }

    // Apply thread tags to socket.

    // First, convert integer file descriptor (ifd) to FileDescriptor.
    final ParcelFileDescriptor pfd;
    final FileDescriptor fd;
    // The only supported way to generate a FileDescriptor from an integer file
    // descriptor is via ParcelFileDescriptor.adoptFd(). Unfortunately prior to Android
    // Marshmallow ParcelFileDescriptor.detachFd() didn't actually detach from the
    // FileDescriptor, so use reflection to set {@code fd} into the FileDescriptor for
    // versions prior to Marshmallow. Here's the fix that went into Marshmallow:
    // https://android.googlesource.com/platform/frameworks/base/+/b30ad6f
    if (Build.VERSION.SDK_INT < VERSION_CODES.M) {
        pfd = null;
        fd = SetFileDescriptor.createWithFd(ifd);
    } else {
        pfd = ParcelFileDescriptor.adoptFd(ifd);
        fd = pfd.getFileDescriptor();
    }
    // Second, convert FileDescriptor to Socket.
    Socket s = new SocketFd(fd);
    // Third, tag the Socket.
    TrafficStats.tagSocket(s);
    s.close(); // No-op but always good to close() Closeables.
    // Have ParcelFileDescriptor relinquish ownership of the file descriptor.
    if (pfd != null) {
        pfd.detachFd();
    }

    // Restore prior thread tags.
    if (tag != oldTag) {
        TrafficStats.setThreadStatsTag(oldTag);
    }
    if (uid != TrafficStatsUid.UNSET) {
        ThreadStatsUid.clear();
    }
}
 
Example 2
Source File: o.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
public static void a(Socket socket)
{
    TrafficStats.tagSocket(socket);
}
 
Example 3
Source File: TrafficStatsCompatIcs.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
public static void tagSocket(Socket socket) throws SocketException {
    TrafficStats.tagSocket(socket);
}
 
Example 4
Source File: TrafficStatsCompatIcs.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
public static void tagSocket(Socket socket) throws SocketException {
    TrafficStats.tagSocket(socket);
}
 
Example 5
Source File: TrafficStatsCompatIcs.java    From V.FlyoutTest with MIT License 4 votes vote down vote up
public static void tagSocket(Socket socket) throws SocketException {
    TrafficStats.tagSocket(socket);
}
 
Example 6
Source File: TrafficStatsCompatIcs.java    From guideshow with MIT License 4 votes vote down vote up
public static void tagSocket(Socket socket) throws SocketException {
    TrafficStats.tagSocket(socket);
}