diff --git a/twidere/src/main/java/org/mariotaku/twidere/app/TwidereApplication.java b/twidere/src/main/java/org/mariotaku/twidere/app/TwidereApplication.java index 354f468b6..6f919751d 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/app/TwidereApplication.java +++ b/twidere/src/main/java/org/mariotaku/twidere/app/TwidereApplication.java @@ -36,8 +36,6 @@ import android.support.annotation.NonNull; import android.support.multidex.MultiDexApplication; import org.apache.commons.lang3.ArrayUtils; -import org.mariotaku.restfu.http.RestHttpClient; -import org.mariotaku.restfu.okhttp.OkHttpRestClient; import org.mariotaku.twidere.BuildConfig; import org.mariotaku.twidere.Constants; import org.mariotaku.twidere.activity.AssistLauncherActivity; @@ -56,9 +54,6 @@ import org.mariotaku.twidere.util.dagger.ApplicationModule; import org.mariotaku.twidere.util.dagger.DependencyHolder; import org.mariotaku.twidere.util.net.TwidereDns; -import okhttp3.Dns; -import okhttp3.OkHttpClient; - import static org.mariotaku.twidere.util.Utils.initAccountColor; public class TwidereApplication extends MultiDexApplication implements Constants, @@ -220,7 +215,7 @@ public class TwidereApplication extends MultiDexApplication implements Constants case KEY_PROXY_TYPE: case KEY_PROXY_USERNAME: case KEY_PROXY_PASSWORD: { - reloadConnectivitySettings(); + HttpClientFactory.reloadConnectivitySettings(this); break; } case KEY_DNS_SERVER: @@ -250,24 +245,8 @@ public class TwidereApplication extends MultiDexApplication implements Constants private void reloadDnsSettings() { DependencyHolder holder = DependencyHolder.get(this); - final Dns dns = holder.getDns(); - if (dns instanceof TwidereDns) { - ((TwidereDns) dns).reloadDnsSettings(); - } - } - - public void reloadConnectivitySettings() { - DependencyHolder holder = DependencyHolder.get(this); - final RestHttpClient client = holder.getRestHttpClient(); - if (client instanceof OkHttpRestClient) { - final OkHttpClient.Builder builder = new OkHttpClient.Builder(); - HttpClientFactory.initOkHttpClient(this, holder.getPreferences(), builder, - holder.getDns()); - final OkHttpRestClient restClient = (OkHttpRestClient) client; - // Kill all connections - restClient.getClient().connectionPool().evictAll(); - restClient.setClient(builder.build()); - } + final TwidereDns dns = holder.getDns(); + dns.reloadDnsSettings(); } diff --git a/twidere/src/main/java/org/mariotaku/twidere/fragment/NetworkDiagnosticsFragment.java b/twidere/src/main/java/org/mariotaku/twidere/fragment/NetworkDiagnosticsFragment.java index 2a981a5d0..d0f2e9d29 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/fragment/NetworkDiagnosticsFragment.java +++ b/twidere/src/main/java/org/mariotaku/twidere/fragment/NetworkDiagnosticsFragment.java @@ -39,7 +39,6 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; -import okhttp3.Dns; /** * Created by mariotaku on 16/2/9. @@ -125,7 +124,7 @@ public class NetworkDiagnosticsFragment extends BaseFragment { publishProgress(new LogText("Text below may have personal information, BE CAREFUL TO MAKE IT PUBLIC")); publishProgress(LogText.LINEBREAK, LogText.LINEBREAK); DependencyHolder holder = DependencyHolder.get(mContext); - final Dns dns = holder.getDns(); + final TwidereDns dns = holder.getDns(); final SharedPreferencesWrapper prefs = holder.getPreferences(); publishProgress(new LogText("Network preferences"), LogText.LINEBREAK); publishProgress(new LogText("using_resolver: " + prefs.getBoolean(KEY_BUILTIN_DNS_RESOLVER)), LogText.LINEBREAK); @@ -206,15 +205,11 @@ public class NetworkDiagnosticsFragment extends BaseFragment { return null; } - private void testDns(Dns dns, final String host) { + private void testDns(TwidereDns dns, final String host) { publishProgress(new LogText(String.format("Lookup %s...", host))); try { final long start = SystemClock.uptimeMillis(); - if (dns instanceof TwidereDns) { - publishProgress(new LogText(String.valueOf(((TwidereDns) dns).lookupResolver(host)))); - } else { - publishProgress(new LogText(String.valueOf(dns.lookup(host)))); - } + publishProgress(new LogText(String.valueOf(dns.lookupResolver(host)))); publishProgress(new LogText(String.format(" OK (%d ms)", SystemClock.uptimeMillis() - start), LogText.State.GOOD)); } catch (UnknownHostException e) { diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/HttpClientFactory.java b/twidere/src/main/java/org/mariotaku/twidere/util/HttpClientFactory.java index d7875ed28..29723c69e 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/HttpClientFactory.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/HttpClientFactory.java @@ -9,10 +9,12 @@ import org.apache.commons.lang3.math.NumberUtils; import org.mariotaku.restfu.http.RestHttpClient; import org.mariotaku.restfu.okhttp.OkHttpRestClient; import org.mariotaku.twidere.Constants; +import org.mariotaku.twidere.util.dagger.DependencyHolder; import org.mariotaku.twidere.util.net.TwidereProxySelector; import java.io.IOException; import java.net.Proxy; +import java.util.concurrent.TimeUnit; import okhttp3.Authenticator; import okhttp3.Credentials; @@ -47,6 +49,9 @@ public class HttpClientFactory implements Constants { final SharedPreferences prefs, final Dns dns, final OkHttpClient.Builder builder) { final boolean enableProxy = prefs.getBoolean(KEY_ENABLE_PROXY, false); + builder.readTimeout(3, TimeUnit.SECONDS); + builder.writeTimeout(3, TimeUnit.SECONDS); + builder.connectTimeout(3, TimeUnit.SECONDS); if (enableProxy) { final String proxyType = prefs.getString(KEY_PROXY_TYPE, null); final String proxyHost = prefs.getString(KEY_PROXY_HOST, null); @@ -90,4 +95,18 @@ public class HttpClientFactory implements Constants { } return Proxy.Type.DIRECT; } + + public static void reloadConnectivitySettings(Context context) { + DependencyHolder holder = DependencyHolder.get(context); + final RestHttpClient client = holder.getRestHttpClient(); + if (client instanceof OkHttpRestClient) { + final OkHttpClient.Builder builder = new OkHttpClient.Builder(); + initOkHttpClient(context, holder.getPreferences(), builder, + holder.getDns()); + final OkHttpRestClient restClient = (OkHttpRestClient) client; + // Kill all connections + restClient.getClient().connectionPool().evictAll(); + restClient.setClient(builder.build()); + } + } } diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/OAuthPasswordAuthenticator.java b/twidere/src/main/java/org/mariotaku/twidere/util/OAuthPasswordAuthenticator.java index 55b5b0536..ab2a76838 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/OAuthPasswordAuthenticator.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/OAuthPasswordAuthenticator.java @@ -37,6 +37,7 @@ import org.mariotaku.restfu.http.Endpoint; import org.mariotaku.restfu.http.HttpRequest; import org.mariotaku.restfu.http.HttpResponse; import org.mariotaku.restfu.http.MultiValueMap; +import org.mariotaku.restfu.http.RestHttpClient; import org.mariotaku.restfu.http.mime.FormBody; import org.mariotaku.restfu.http.mime.SimpleBody; import org.mariotaku.restfu.okhttp.OkHttpRestClient; @@ -61,7 +62,7 @@ public class OAuthPasswordAuthenticator implements Constants { private static final IAttoParser PARSER = new MarkupAttoParser(); private final TwitterOAuth oauth; - private final OkHttpRestClient client; + private final RestHttpClient client; private final Endpoint endpoint; private final LoginVerificationCallback loginVerificationCallback; private final String userAgent; diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/TwitterAPIFactory.java b/twidere/src/main/java/org/mariotaku/twidere/util/TwitterAPIFactory.java index 215f41c57..2cf5a6b3f 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/TwitterAPIFactory.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/TwitterAPIFactory.java @@ -43,6 +43,7 @@ import org.mariotaku.twidere.model.ConsumerKeyType; import org.mariotaku.twidere.model.ParcelableCredentials; import org.mariotaku.twidere.provider.TwidereDataStore; import org.mariotaku.twidere.util.dagger.DependencyHolder; +import org.mariotaku.twidere.util.net.TwidereDns; import java.io.IOException; import java.util.HashMap; @@ -51,8 +52,6 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; -import okhttp3.Dns; - import static android.text.TextUtils.isEmpty; /** @@ -441,9 +440,9 @@ public class TwitterAPIFactory implements TwidereConstants { public static class TwidereExceptionFactory implements ExceptionFactory { - private final Dns dns; + private final TwidereDns dns; - TwidereExceptionFactory(Dns dns) { + TwidereExceptionFactory(TwidereDns dns) { this.dns = dns; }