From 766a854a9255d267703b55a8f1e8b44a52ce1188 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Thu, 16 Jun 2016 22:55:06 +0200 Subject: [PATCH] Made SettingsActivity use correct sharedPrefs, made FontSize and loadImages preferences work, removed unused code, moved webview initialization code in own method --- .../activity/MainActivity.java | 164 +++++------------- .../activity/SettingsActivity.java | 27 +-- .../diaspora_android/data/AppSettings.java | 19 +- .../main/res/values/strings-preferences.xml | 6 +- app/src/main/res/xml/preferences.xml | 2 +- 5 files changed, 66 insertions(+), 152 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 53ba7329..ead96cff 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 @@ -53,7 +53,6 @@ import android.text.method.LinkMovementMethod; import android.text.util.Linkify; import android.view.Gravity; import android.view.KeyEvent; -import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -69,8 +68,6 @@ import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; -import android.widget.RadioButton; -import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; @@ -119,6 +116,7 @@ public class MainActivity extends AppCompatActivity private PodUserProfile podUserProfile; private final Handler uiHandler = new Handler(); private CustomWebViewClient webViewClient; + private Snackbar snackbarExitApp; /** * UI Bindings @@ -163,9 +161,6 @@ public class MainActivity extends AppCompatActivity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (android.os.Build.VERSION.SDK_INT >= 21) - WebView.enableSlowWholeDocumentDraw(); - // Bind UI setContentView(R.layout.main__activity); ButterKnife.bind(this); @@ -176,9 +171,7 @@ public class MainActivity extends AppCompatActivity podUserProfile.setCallbackHandler(uiHandler); podUserProfile.setListener(this); - this.registerForContextMenu(webView); - webView.setParentActivity(this); - webView.setOverScrollMode(WebView.OVER_SCROLL_ALWAYS); + setupWebView(savedInstanceState); // Setup toolbar setSupportActionBar(toolbarTop); @@ -190,6 +183,16 @@ public class MainActivity extends AppCompatActivity }); setTitle(R.string.app_name); + //Setup snackbar + snackbarExitApp = Snackbar + .make(swipeRefreshLayout, R.string.confirm_exit, Snackbar.LENGTH_LONG) + .setAction(android.R.string.yes, new View.OnClickListener() { + @Override + public void onClick(View view) { + moveTaskToBack(true); + } + }); + // Load app settings setupNavigationSlider(); @@ -199,22 +202,44 @@ public class MainActivity extends AppCompatActivity swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.fab_big); - // Setup WebView - webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidBridge"); - if (savedInstanceState != null) { - webView.restoreState(savedInstanceState); + if (savedInstanceState == null) { + if (Helpers.isOnline(MainActivity.this)) { + webView.loadData("", "text/html", null); + webView.loadUrl("https://" + podDomain); + } else { + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + } } + } + + private void setupWebView(Bundle savedInstanceState) { webSettings = webView.getSettings(); + //TODO: Dangerous on API < 17. Can we do anything about this? webSettings.setJavaScriptEnabled(true); webSettings.setUseWideViewPort(true); webSettings.setLoadWithOverviewMode(true); webSettings.setDomStorageEnabled(true); webSettings.setMinimumFontSize(appSettings.getMinimumFontSize()); webSettings.setLoadsImagesAutomatically(appSettings.isLoadImages()); + webSettings.setAppCacheEnabled(true); - if (android.os.Build.VERSION.SDK_INT >= 21) + if (savedInstanceState != null) { + webView.restoreState(savedInstanceState); + } + + if (android.os.Build.VERSION.SDK_INT >= 21) { + WebView.enableSlowWholeDocumentDraw(); webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); + } + + this.registerForContextMenu(webView); + webView.setParentActivity(this); + webView.setOverScrollMode(WebView.OVER_SCROLL_ALWAYS); + + // Setup WebView + //TODO: Dangerous on API < 17. Can we do anything about this? + webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidBridge"); /* * WebViewClient @@ -308,16 +333,6 @@ public class MainActivity extends AppCompatActivity return true; } }); - - - if (savedInstanceState == null) { - if (Helpers.isOnline(MainActivity.this)) { - webView.loadData("", "text/html", null); - webView.loadUrl("https://" + podDomain); - } else { - Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); - } - } } private void setupNavigationSlider() { @@ -420,15 +435,8 @@ public class MainActivity extends AppCompatActivity if (webView.canGoBack()) { webView.goBack(); } else { - Snackbar snackbar = Snackbar - .make(swipeRefreshLayout, R.string.confirm_exit, Snackbar.LENGTH_LONG) - .setAction(android.R.string.yes, new View.OnClickListener() { - @Override - public void onClick(View view) { - moveTaskToBack(true); - } - }); - snackbar.show(); + if(!snackbarExitApp.isShown()) + snackbarExitApp.show(); } } @@ -691,53 +699,6 @@ public class MainActivity extends AppCompatActivity return true; } - - private void alertFormElements() { - - /* - * Inflate the XML view. activity_main is in - * res/layout/form_elements.xml - */ - LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); - final View formElementsView = inflater.inflate(R.layout.ui__font_size_chooser, - null, false); - - final RadioGroup rgFontSize = (RadioGroup) formElementsView - .findViewById(R.id.genderRadioGroup); - - // the alert dialog - new AlertDialog.Builder(MainActivity.this).setView(formElementsView) - .setTitle("Set Font Size") - .setNegativeButton("OK", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - int selectedId = rgFontSize - .getCheckedRadioButtonId(); - - // find the radiobutton by returned id - RadioButton selectedRadioButton = (RadioButton) formElementsView - .findViewById(selectedId); - - if (selectedRadioButton.getId() == R.id.radNormal) { - appSettings.setMinimumFontSize(8); - } else if (selectedRadioButton.getId() == R.id.radLarge) { - appSettings.setMinimumFontSize(16); - } else if (selectedRadioButton.getId() == R.id.radLarger) { - appSettings.setMinimumFontSize(20); - } - - webSettings.setMinimumFontSize(appSettings.getMinimumFontSize()); - - if (Helpers.isOnline(MainActivity.this)) { - webView.loadUrl(webView.getUrl()); - setTitle(R.string.app_name); - } else { - Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); - } - - } - }).show(); - } - @Override public void onUserProfileNameChanged(String name) { navheaderTitle.setText(name); @@ -945,47 +906,6 @@ public class MainActivity extends AppCompatActivity break; case R.id.nav_settings_app: { - /* - final CharSequence[] options = {getString(R.string.settings_font), getString(R.string.settings_view), appSettings.isLoadImages() ? - getString(R.string.settings_images_switch_off) : getString(R.string.settings_images_switch_on), getString(R.string.jb_pod)}; - - if (Helpers.isOnline(MainActivity.this)) { - new AlertDialog.Builder(MainActivity.this) - .setItems(options, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int item) { - switch (item) { - case 0: - alertFormElements(); - break; - case 1: - webView.loadUrl("https://" + podDomain + "/mobile/toggle"); - break; - case 2: - webSettings.setLoadsImagesAutomatically(!appSettings.isLoadImages()); - appSettings.setLoadImages(!appSettings.isLoadImages()); - webView.loadUrl(webView.getUrl()); - break; - case 3: - new AlertDialog.Builder(MainActivity.this) - .setTitle(getString(R.string.confirmation)) - .setMessage(getString(R.string.change_pod_warning)) - .setNegativeButton(android.R.string.no, null) - .setPositiveButton(android.R.string.yes, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - app.resetPodData(webView); - Helpers.animateToActivity(MainActivity.this, PodSelectionActivity.class, true); - } - }) - .show(); - break; - } - } - }).show(); - } else { - Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); - } */ startActivity(new Intent(this, SettingsActivity.class)); } break; @@ -1051,7 +971,7 @@ public class MainActivity extends AppCompatActivity } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main__layout); - drawer.closeDrawer(GravityCompat.START); + if(drawer != null) drawer.closeDrawer(GravityCompat.START); return true; } 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 27af6bd7..a019db07 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 @@ -2,12 +2,12 @@ package com.github.dfa.diaspora_android.activity; import android.content.Intent; import android.content.SharedPreferences; +import android.content.res.Resources; import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; -import android.preference.PreferenceFragment; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; @@ -20,20 +20,19 @@ import com.github.dfa.diaspora_android.data.AppSettings; */ public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { - private Intent settingsChangedIntent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + getPreferenceManager().setSharedPreferencesName("app"); addPreferencesFromResource(R.xml.preferences); getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); - setEditTextPreferenceSummaries(); - settingsChangedIntent = new Intent("SettingsChanged"); + setPreferenceSummaries(); } - private void setEditTextPreferenceSummaries() { - String[] prefKeys = new String[]{"pref_key_proxy_host", "pref_key_proxy_port"}; - for(String key : prefKeys) { + private void setPreferenceSummaries() { + String[] editTextKeys = new String[]{"pref_key_proxy_host", "pref_key_proxy_port"}; + for(String key : editTextKeys) { EditTextPreference p = (EditTextPreference) findPreference(key); p.setSummary(p.getText()); } @@ -42,12 +41,6 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { updatePreference(findPreference(key), key); - settingsChangedIntent.putExtra(key, true); - switch (key) { - case AppSettings.PREF.MINIMUM_FONT_SIZE: - int newFontSize = Integer.parseInt(((ListPreference)findPreference(key)).getValue().substring(1)); - Log.d(App.TAG, newFontSize+""); - } } private void updatePreference(Preference preference, String key) { @@ -63,12 +56,4 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer return; } } - - @Override - public void finish() { - Log.d(App.TAG, "finish()"); //TODO: remove - LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this); - broadcastManager.sendBroadcast(settingsChangedIntent); - super.finish(); - } } \ No newline at end of file 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 b191a422..4477e1cf 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 @@ -2,6 +2,9 @@ package com.github.dfa.diaspora_android.data; import android.content.Context; import android.content.SharedPreferences; +import android.util.Log; + +import com.github.dfa.diaspora_android.App; /** * Created by gsantner on 20.03.16. Part of Diaspora for Android. @@ -95,11 +98,17 @@ public class AppSettings { } public int getMinimumFontSize() { - return prefApp.getInt(PREF.MINIMUM_FONT_SIZE, 8); - } - - public void setMinimumFontSize(int minimumFontSize) { - setInt(prefApp, PREF.MINIMUM_FONT_SIZE, minimumFontSize); + switch (prefApp.getString(PREF.MINIMUM_FONT_SIZE, "")) { + case "huge": + return 20; + case "large": + return 16; + case "normal": + return 8; + default: + prefApp.edit().putString(PREF.MINIMUM_FONT_SIZE, "normal").apply(); + return 8; + } } public String getAvatarUrl() { diff --git a/app/src/main/res/values/strings-preferences.xml b/app/src/main/res/values/strings-preferences.xml index 90589312..15f2706c 100644 --- a/app/src/main/res/values/strings-preferences.xml +++ b/app/src/main/res/values/strings-preferences.xml @@ -14,9 +14,9 @@ - s8 - s16 - s20 + normal + large + huge Desktop mode diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 1a78c306..c891b6c7 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -6,8 +6,8 @@