• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
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 package com.google.android.libraries.mobiledatadownload.downloader.offroad;
17 
18 import android.net.TrafficStats;
19 import java.io.IOException;
20 import java.net.InetAddress;
21 import java.net.Socket;
22 import javax.net.SocketFactory;
23 
24 /** A custom SocketFactory that tags the traffic going through the socket created by it. */
25 // TODO(b/141362798): Make package-private when OkHttpFileDownloaderModule supports non-framework
26 // apps.
27 public final class TrafficStatsSocketFactory extends SocketFactory {
28 
29   private final SocketFactory delegate;
30   private final int trafficTag;
31 
TrafficStatsSocketFactory(SocketFactory delegate, int trafficTag)32   public TrafficStatsSocketFactory(SocketFactory delegate, int trafficTag) {
33     this.delegate = delegate;
34     this.trafficTag = trafficTag;
35   }
36 
37   @Override
createSocket()38   public Socket createSocket() throws IOException {
39     Socket socket = delegate.createSocket();
40     TrafficStats.setThreadStatsTag(trafficTag);
41     TrafficStats.tagSocket(socket);
42     return socket;
43   }
44 
45   @Override
createSocket(String host, int port)46   public Socket createSocket(String host, int port) throws IOException {
47     Socket socket = delegate.createSocket(host, port);
48     TrafficStats.setThreadStatsTag(trafficTag);
49     TrafficStats.tagSocket(socket);
50     return socket;
51   }
52 
53   @Override
createSocket(String host, int port, InetAddress localHost, int localPort)54   public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
55       throws IOException {
56     Socket socket = delegate.createSocket(host, port, localHost, localPort);
57     TrafficStats.setThreadStatsTag(trafficTag);
58     TrafficStats.tagSocket(socket);
59     return socket;
60   }
61 
62   @Override
createSocket(InetAddress host, int port)63   public Socket createSocket(InetAddress host, int port) throws IOException {
64     Socket socket = delegate.createSocket(host, port);
65     TrafficStats.setThreadStatsTag(trafficTag);
66     TrafficStats.tagSocket(socket);
67     return socket;
68   }
69 
70   @Override
createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)71   public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
72       throws IOException {
73     Socket socket = delegate.createSocket(address, port, localAddress, localPort);
74     TrafficStats.setThreadStatsTag(trafficTag);
75     TrafficStats.tagSocket(socket);
76     return socket;
77   }
78 }
79