removed proxy selector

This commit is contained in:
Mariotaku Lee 2016-03-26 11:51:51 +08:00
parent 493cb008ad
commit f418c689c5
4 changed files with 1 additions and 113 deletions

View File

@ -9,8 +9,6 @@ import org.mariotaku.restfu.http.RestHttpClient;
import org.mariotaku.restfu.okhttp3.OkHttpRestClient;
import org.mariotaku.twidere.Constants;
import org.mariotaku.twidere.util.dagger.DependencyHolder;
import org.mariotaku.twidere.util.net.TwidereDns;
import org.mariotaku.twidere.util.net.TwidereProxySelector;
import java.io.IOException;
import java.net.InetSocketAddress;
@ -55,8 +53,6 @@ public class HttpClientFactory implements Constants {
final ConnectionPool connectionPool) {
final boolean enableProxy = prefs.getBoolean(KEY_ENABLE_PROXY, false);
builder.connectTimeout(prefs.getInt(KEY_CONNECTION_TIMEOUT, 10), TimeUnit.SECONDS);
final boolean retryOnConnectionFailure = prefs.getBoolean(KEY_RETRY_ON_NETWORK_ISSUE);
builder.retryOnConnectionFailure(retryOnConnectionFailure);
builder.connectionPool(connectionPool);
if (enableProxy) {
final String proxyType = prefs.getString(KEY_PROXY_TYPE, null);
@ -66,11 +62,7 @@ public class HttpClientFactory implements Constants {
TwidereMathUtils.RANGE_INCLUSIVE_INCLUSIVE)) {
final Proxy.Type type = getProxyType(proxyType);
if (type != Proxy.Type.DIRECT) {
if (TwidereDns.isValidIpAddress(proxyHost) && !retryOnConnectionFailure) {
builder.proxy(new Proxy(type, InetSocketAddress.createUnresolved(proxyHost, proxyPort)));
} else {
builder.proxySelector(new TwidereProxySelector(context, type, proxyHost, proxyPort));
}
builder.proxy(new Proxy(type, InetSocketAddress.createUnresolved(proxyHost, proxyPort)));
}
}
final String username = prefs.getString(KEY_PROXY_USERNAME, null);

View File

@ -55,7 +55,6 @@ import org.mariotaku.twidere.task.twitter.GetStatusesTask;
import org.mariotaku.twidere.text.util.EmojiEditableFactory;
import org.mariotaku.twidere.text.util.EmojiSpannableFactory;
import org.mariotaku.twidere.util.MultiSelectEventHandler;
import org.mariotaku.twidere.util.net.TwidereProxySelector;
import javax.inject.Singleton;
@ -113,8 +112,6 @@ public interface GeneralComponent {
void inject(AccountsListPreference.AccountItemPreference object);
void inject(TwidereProxySelector object);
void inject(MessagesConversationFragment.SetReadStateTask object);
void inject(DependencyHolder object);

View File

@ -1,96 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util.net;
import android.content.Context;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.util.Log;
import org.mariotaku.twidere.BuildConfig;
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
/**
* Created by mariotaku on 15/12/31.
*/
public class TwidereProxySelector extends ProxySelector {
@Inject
TwidereDns dns;
private final Proxy.Type type;
@NonNull
private final String host;
private final int port;
private List<Proxy> proxy;
public TwidereProxySelector(Context context, Proxy.Type type, @NonNull final String host, int port) {
GeneralComponentHelper.build(context).inject(this);
this.type = type;
this.host = host;
this.port = port;
}
@Override
public List<Proxy> select(URI uri) {
if (proxy != null) return proxy;
final InetSocketAddress address;
if (Looper.myLooper() != Looper.getMainLooper()) {
address = createResolved(host, port);
} else {
// If proxy host is an IP address, create unresolved directly.
if (TwidereDns.isValidIpAddress(host)) {
address = InetSocketAddress.createUnresolved(host, port);
} else {
address = new InetSocketAddress(host, port);
}
}
return proxy = Collections.singletonList(new Proxy(type, address));
}
private InetSocketAddress createResolved(String host, int port) {
try {
//noinspection LoopStatementThatDoesntLoop
for (InetAddress inetAddress : dns.lookup(host)) {
return new InetSocketAddress(inetAddress, port);
}
} catch (IOException e) {
return InetSocketAddress.createUnresolved(host, port);
}
return InetSocketAddress.createUnresolved(host, port);
}
@Override
public void connectFailed(URI uri, SocketAddress address, IOException failure) {
if (BuildConfig.DEBUG) {
Log.w("TwidereProxy", String.format("%s: proxy %s connect failed", uri, address), failure);
}
}
}

View File

@ -35,11 +35,6 @@
android:summary="@string/custom_host_mapping_summary"
android:title="@string/custom_host_mapping"/>
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="retry_on_network_issue"
android:summary="@string/retry_on_network_issue_summary"
android:title="@string/retry_on_network_issue"/>
</PreferenceCategory>
<PreferenceCategory
android:key="category_proxy"