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
17package android.support.v4.net;
18
19import android.net.TrafficStats;
20import android.os.ParcelFileDescriptor;
21
22import java.net.DatagramSocket;
23import java.net.Socket;
24import java.net.SocketException;
25
26/**
27 * Implementation of TrafficStatsCompat that can call ICS APIs.
28 */
29class TrafficStatsCompatIcs {
30    public static void clearThreadStatsTag() {
31        TrafficStats.clearThreadStatsTag();
32    }
33
34    public static int getThreadStatsTag() {
35        return TrafficStats.getThreadStatsTag();
36    }
37
38    public static void incrementOperationCount(int operationCount) {
39        TrafficStats.incrementOperationCount(operationCount);
40    }
41
42    public static void incrementOperationCount(int tag, int operationCount) {
43        TrafficStats.incrementOperationCount(tag, operationCount);
44    }
45
46    public static void setThreadStatsTag(int tag) {
47        TrafficStats.setThreadStatsTag(tag);
48    }
49
50    public static void tagSocket(Socket socket) throws SocketException {
51        TrafficStats.tagSocket(socket);
52    }
53
54    public static void untagSocket(Socket socket) throws SocketException {
55        TrafficStats.untagSocket(socket);
56    }
57
58    public static void tagDatagramSocket(DatagramSocket socket) throws SocketException {
59        final ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(socket);
60        TrafficStats.tagSocket(new DatagramSocketWrapper(socket, pfd.getFileDescriptor()));
61        // The developer is still using the FD, so we need to detach it to
62        // prevent the PFD finalizer from closing it in their face. We had to
63        // wait until after the tagging call above, since detaching clears out
64        // the getFileDescriptor() result which tagging depends on.
65        pfd.detachFd();
66    }
67
68    public static void untagDatagramSocket(DatagramSocket socket) throws SocketException {
69        final ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(socket);
70        TrafficStats.untagSocket(new DatagramSocketWrapper(socket, pfd.getFileDescriptor()));
71        // The developer is still using the FD, so we need to detach it to
72        // prevent the PFD finalizer from closing it in their face. We had to
73        // wait until after the tagging call above, since detaching clears out
74        // the getFileDescriptor() result which tagging depends on.
75        pfd.detachFd();
76    }
77}
78