Don't forget proxy settings when disabling proxy (#5471)

This commit is contained in:
vbh 2021-10-24 09:42:00 +00:00 committed by GitHub
parent b860ee33d0
commit bf95bf172d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 26 deletions

View File

@ -67,6 +67,7 @@ public class ProxyDialog {
.setView(content)
.setNegativeButton(R.string.cancel_label, null)
.setPositiveButton(R.string.proxy_test_label, null)
.setNeutralButton(R.string.reset, null)
.show();
// To prevent cancelling the dialog on button click
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener((view) -> {
@ -75,36 +76,19 @@ public class ProxyDialog {
test();
return;
}
String type = (String) spType.getSelectedItem();
ProxyConfig proxy;
if (Proxy.Type.valueOf(type) == Proxy.Type.DIRECT) {
proxy = ProxyConfig.direct();
} else {
String host = etHost.getText().toString();
String port = etPort.getText().toString();
String username = etUsername.getText().toString();
if (TextUtils.isEmpty(username)) {
username = null;
}
String password = etPassword.getText().toString();
if (TextUtils.isEmpty(password)) {
password = null;
}
int portValue = 0;
if (!TextUtils.isEmpty(port)) {
portValue = Integer.parseInt(port);
}
if (Proxy.Type.valueOf(type) == Proxy.Type.SOCKS) {
proxy = ProxyConfig.socks(host, portValue, username, password);
} else {
proxy = ProxyConfig.http(host, portValue, username, password);
}
}
UserPreferences.setProxyConfig(proxy);
setProxyConfig();
AntennapodHttpClient.reinit();
dialog.dismiss();
});
dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener((view) -> {
etHost.getText().clear();
etPort.getText().clear();
etUsername.getText().clear();
etPassword.getText().clear();
setProxyConfig();
});
List<String> types = new ArrayList<>();
types.add(Proxy.Type.DIRECT.name());
types.add(Proxy.Type.HTTP.name());
@ -144,6 +128,11 @@ public class ProxyDialog {
spType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setVisibility(View.GONE);
} else {
dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setVisibility(View.VISIBLE);
}
enableSettings(position > 0);
setTestRequired(position > 0);
}
@ -158,6 +147,35 @@ public class ProxyDialog {
return dialog;
}
private void setProxyConfig() {
String type = (String) spType.getSelectedItem();
ProxyConfig proxy;
if (Proxy.Type.valueOf(type) == Proxy.Type.DIRECT) {
proxy = ProxyConfig.direct();
} else {
String host = etHost.getText().toString();
String port = etPort.getText().toString();
String username = etUsername.getText().toString();
if (TextUtils.isEmpty(username)) {
username = null;
}
String password = etPassword.getText().toString();
if (TextUtils.isEmpty(password)) {
password = null;
}
int portValue = 0;
if (!TextUtils.isEmpty(port)) {
portValue = Integer.parseInt(port);
}
if (Proxy.Type.valueOf(type) == Proxy.Type.SOCKS) {
proxy = ProxyConfig.socks(host, portValue, username, password);
} else {
proxy = ProxyConfig.http(host, portValue, username, password);
}
}
UserPreferences.setProxyConfig(proxy);
}
private final TextWatcher requireTestOnChange = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

View File

@ -610,6 +610,11 @@ public class UserPreferences {
public static void setProxyConfig(ProxyConfig config) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(PREF_PROXY_TYPE, config.type.name());
Proxy.Type type = Proxy.Type.valueOf(config.type.name());
if (type == Proxy.Type.DIRECT) {
editor.apply();
return;
}
if(TextUtils.isEmpty(config.host)) {
editor.remove(PREF_PROXY_HOST);
} else {