From e532cbc2150f7792f436c0e9cdedd2d6e755aafd Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Thu, 16 Jun 2016 23:36:52 +0200 Subject: [PATCH] Connected proxy settings to SettingsActivity. --- .../activity/MainActivity.java | 16 ++++++------ .../activity/SettingsActivity.java | 6 ++++- .../diaspora_android/data/AppSettings.java | 26 ++++++++++++------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java index 104ec5ce..ff2c7012 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java @@ -34,7 +34,6 @@ import android.content.IntentFilter; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.net.Uri; -import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.os.Handler; @@ -50,12 +49,12 @@ import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.ActionMenuView; -import android.support.v7.widget.SwitchCompat; import android.support.v7.widget.Toolbar; import android.text.Html; import android.text.SpannableString; import android.text.method.LinkMovementMethod; import android.text.util.Linkify; +import android.util.Log; import android.view.Gravity; import android.view.KeyEvent; import android.view.Menu; @@ -70,7 +69,6 @@ import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.EditText; -import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; @@ -183,6 +181,8 @@ public class MainActivity extends AppCompatActivity if(!setProxy(appSettings.getProxyHost(), appSettings.getProxyPort())) { Toast.makeText(MainActivity.this, R.string.toast_set_proxy_failed,Toast.LENGTH_SHORT).show(); } + } else if(appSettings.wasProxyEnabled()) { + resetProxy(); } setupWebView(savedInstanceState); @@ -1033,8 +1033,7 @@ public class MainActivity extends AppCompatActivity } catch (Exception e) { /*Nothing we can do*/ } appSettings.setProxyEnabled(true); - appSettings.setProxyHost(host); - appSettings.setProxyPort(port); + appSettings.setProxyWasEnabled(true); StrictMode.setThreadPolicy(old); webView.reload(); @@ -1045,11 +1044,14 @@ public class MainActivity extends AppCompatActivity } private boolean setProxy() { + Log.d(App.TAG, "Enable Proxy"); return setProxy(appSettings.getProxyHost(), appSettings.getProxyPort()); } private void resetProxy() { + Log.d(App.TAG, "Reset Proxy"); appSettings.setProxyEnabled(false); + appSettings.setProxyWasEnabled(false); //Temporary change thread policy StrictMode.ThreadPolicy old = StrictMode.getThreadPolicy(); @@ -1059,9 +1061,7 @@ public class MainActivity extends AppCompatActivity NetCipher.clearProxy(); try{ WebkitProxy.resetProxy(MainActivity.class.getName(), this); - } catch (Exception e) { - //Nothing we can do. - } + } catch (Exception e) {/*Nothing*/} StrictMode.setThreadPolicy(old); diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/SettingsActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/SettingsActivity.java index a019db07..a2fb553e 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/SettingsActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/SettingsActivity.java @@ -4,6 +4,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; import android.os.Bundle; +import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; @@ -28,10 +29,13 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer addPreferencesFromResource(R.xml.preferences); getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); setPreferenceSummaries(); + SharedPreferences preferences = getPreferenceScreen().getSharedPreferences(); + preferences.edit().putBoolean(AppSettings.PREF.PROXY_WAS_ENABLED, + preferences.getBoolean(AppSettings.PREF.PROXY_ENABLED, false)).apply(); } private void setPreferenceSummaries() { - String[] editTextKeys = new String[]{"pref_key_proxy_host", "pref_key_proxy_port"}; + String[] editTextKeys = new String[]{AppSettings.PREF.PROXY_HOST, AppSettings.PREF.PROXY_PORT}; for(String key : editTextKeys) { EditTextPreference p = (EditTextPreference) findPreference(key); p.setSummary(p.getText()); diff --git a/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java b/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java index 172ffa27..e49b7ae0 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java @@ -74,9 +74,10 @@ public class AppSettings { public static final String PODDOMAIN = "podDomain"; public static final String PODUSERPROFILE_ASPECTS = "podUserProfile_aspects"; public static final String IS_LOAD_DESKTOP_PAGE = "pref_key_desktop_mode"; - public static final String PROXY_ENABLED = "isProxyEnabled"; - public static final String PROXY_HOST = "proxyHost"; - public static final String PROXY_PORT = "proxyPort"; + public static final String PROXY_ENABLED = "pref_key_proxy_enabled"; + public static final String PROXY_WAS_ENABLED = "wasProxyEnabled"; + public static final String PROXY_HOST = "pref_key_proxy_host"; + public static final String PROXY_PORT = "pref_key_proxy_port"; } @@ -176,8 +177,12 @@ public class AppSettings { return prefApp.getBoolean(PREF.PROXY_ENABLED, false); } - public void setProxyHost(String host) { - setString(prefApp, PREF.PROXY_HOST, host); + public boolean wasProxyEnabled() { + return prefApp.getBoolean(PREF.PROXY_WAS_ENABLED, false); + } + + public void setProxyWasEnabled(boolean b) { + prefApp.edit().putBoolean(PREF.PROXY_WAS_ENABLED, b).commit(); } /** @@ -188,15 +193,16 @@ public class AppSettings { return prefApp.getString(PREF.PROXY_HOST, ""); } - public void setProxyPort(int port) { - setInt(prefApp, PREF.PROXY_PORT, port); - } - /** * Default value: 0 * @return proxy port */ public int getProxyPort() { - return prefApp.getInt(PREF.PROXY_PORT, 0); + try { + return Integer.parseInt(prefApp.getString(PREF.PROXY_PORT, "0")); + } catch (Exception e) { + prefApp.edit().putString(PREF.PROXY_PORT, "0").apply(); + return 0; + } } }