Skip to content

Commit

Permalink
Workaround for Strict mode socket issue: (#443)
Browse files Browse the repository at this point in the history
Add custom socket factory and set TrafficStats.setThreadStatsTag(1000) for configuration client(okhttpClient).
  • Loading branch information
harvsu authored and Harsha Sura committed Mar 17, 2020
1 parent 3c32ef5 commit 39ce200
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
libcore/1.4.0-SNAPSHOT
libcore/1.4.1-SNAPSHOT
v1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.support.v4.content.LocalBroadcastManager;

import android.util.Log;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -225,8 +226,16 @@ private boolean isUserAgentValid(String userAgent) {

private void initializeTelemetryClient() {
if (configurationClient == null) {
this.configurationClient = new ConfigurationClient(applicationContext,
TelemetryUtils.createFullUserAgent(userAgent, applicationContext), sAccessToken.get(), new OkHttpClient());
if (BuildConfig.DEBUG) {
// Strict mode work around : https://github.com/square/okhttp/issues/3537
this.configurationClient = new ConfigurationClient(applicationContext,
TelemetryUtils.createFullUserAgent(userAgent, applicationContext), sAccessToken.get(),
TelemetryUtils.createOkHttpClientWithStrictModeWorkAround());
} else {
this.configurationClient = new ConfigurationClient(applicationContext,
TelemetryUtils.createFullUserAgent(userAgent, applicationContext), sAccessToken.get(),
new OkHttpClient());
}
}

if (certificateBlacklist == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
libtelemetry/4.7.0-SNAPSHOT
libtelemetry/4.7.1-SNAPSHOT
v1
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.TrafficStats;
import android.os.BatteryManager;
import android.os.Build;
import android.support.annotation.Nullable;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
Expand All @@ -26,6 +31,9 @@

import com.mapbox.android.core.MapboxSdkInfoForUserAgentGenerator;

import javax.net.SocketFactory;

import okhttp3.OkHttpClient;
import okio.Buffer;

import static com.mapbox.android.telemetry.MapboxTelemetryConstants.MAPBOX_SHARED_PREFERENCES;
Expand Down Expand Up @@ -292,4 +300,39 @@ static boolean adjustWakeUpMode(Context context) {
}
return false;
}

static OkHttpClient createOkHttpClientWithStrictModeWorkAround() {
return new OkHttpClient().newBuilder()
.socketFactory(new SocketFactory() {
SocketFactory socketFactory = SocketFactory.getDefault();
private static final int THREAD_ID = 10000;

@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
TrafficStats.setThreadStatsTag(THREAD_ID);
return socketFactory.createSocket(host, port);
}

@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws
IOException, UnknownHostException {
TrafficStats.setThreadStatsTag(THREAD_ID);
return socketFactory.createSocket(host, port, localHost, localPort);
}

@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
TrafficStats.setThreadStatsTag(THREAD_ID);
return socketFactory.createSocket(host, port);
}

@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws
IOException {
TrafficStats.setThreadStatsTag(THREAD_ID);
return socketFactory.createSocket(address, port, localAddress, localPort);
}
})
.build();
}
}

0 comments on commit 39ce200

Please sign in to comment.