1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-11-25 22:02:08 +01:00

Made SettingsActivity use correct sharedPrefs, made FontSize and loadImages preferences work, removed unused code, moved webview initialization code in own method

This commit is contained in:
vanitasvitae 2016-06-16 22:55:06 +02:00
parent 8466c279bb
commit 766a854a92
5 changed files with 66 additions and 152 deletions

View file

@ -53,7 +53,6 @@ import android.text.method.LinkMovementMethod;
import android.text.util.Linkify; import android.text.util.Linkify;
import android.view.Gravity; import android.view.Gravity;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -69,8 +68,6 @@ import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -119,6 +116,7 @@ public class MainActivity extends AppCompatActivity
private PodUserProfile podUserProfile; private PodUserProfile podUserProfile;
private final Handler uiHandler = new Handler(); private final Handler uiHandler = new Handler();
private CustomWebViewClient webViewClient; private CustomWebViewClient webViewClient;
private Snackbar snackbarExitApp;
/** /**
* UI Bindings * UI Bindings
@ -163,9 +161,6 @@ public class MainActivity extends AppCompatActivity
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT >= 21)
WebView.enableSlowWholeDocumentDraw();
// Bind UI // Bind UI
setContentView(R.layout.main__activity); setContentView(R.layout.main__activity);
ButterKnife.bind(this); ButterKnife.bind(this);
@ -176,9 +171,7 @@ public class MainActivity extends AppCompatActivity
podUserProfile.setCallbackHandler(uiHandler); podUserProfile.setCallbackHandler(uiHandler);
podUserProfile.setListener(this); podUserProfile.setListener(this);
this.registerForContextMenu(webView); setupWebView(savedInstanceState);
webView.setParentActivity(this);
webView.setOverScrollMode(WebView.OVER_SCROLL_ALWAYS);
// Setup toolbar // Setup toolbar
setSupportActionBar(toolbarTop); setSupportActionBar(toolbarTop);
@ -190,6 +183,16 @@ public class MainActivity extends AppCompatActivity
}); });
setTitle(R.string.app_name); 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 // Load app settings
setupNavigationSlider(); setupNavigationSlider();
@ -199,22 +202,44 @@ public class MainActivity extends AppCompatActivity
swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary,
R.color.fab_big); R.color.fab_big);
// Setup WebView if (savedInstanceState == null) {
webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidBridge"); if (Helpers.isOnline(MainActivity.this)) {
if (savedInstanceState != null) { webView.loadData("", "text/html", null);
webView.restoreState(savedInstanceState); webView.loadUrl("https://" + podDomain);
} else {
Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show();
}
} }
}
private void setupWebView(Bundle savedInstanceState) {
webSettings = webView.getSettings(); webSettings = webView.getSettings();
//TODO: Dangerous on API < 17. Can we do anything about this?
webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true); webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true); webSettings.setLoadWithOverviewMode(true);
webSettings.setDomStorageEnabled(true); webSettings.setDomStorageEnabled(true);
webSettings.setMinimumFontSize(appSettings.getMinimumFontSize()); webSettings.setMinimumFontSize(appSettings.getMinimumFontSize());
webSettings.setLoadsImagesAutomatically(appSettings.isLoadImages()); 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); 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 * WebViewClient
@ -308,16 +333,6 @@ public class MainActivity extends AppCompatActivity
return true; 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() { private void setupNavigationSlider() {
@ -420,15 +435,8 @@ public class MainActivity extends AppCompatActivity
if (webView.canGoBack()) { if (webView.canGoBack()) {
webView.goBack(); webView.goBack();
} else { } else {
Snackbar snackbar = Snackbar if(!snackbarExitApp.isShown())
.make(swipeRefreshLayout, R.string.confirm_exit, Snackbar.LENGTH_LONG) snackbarExitApp.show();
.setAction(android.R.string.yes, new View.OnClickListener() {
@Override
public void onClick(View view) {
moveTaskToBack(true);
}
});
snackbar.show();
} }
} }
@ -691,53 +699,6 @@ public class MainActivity extends AppCompatActivity
return true; 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 @Override
public void onUserProfileNameChanged(String name) { public void onUserProfileNameChanged(String name) {
navheaderTitle.setText(name); navheaderTitle.setText(name);
@ -945,47 +906,6 @@ public class MainActivity extends AppCompatActivity
break; break;
case R.id.nav_settings_app: { 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)); startActivity(new Intent(this, SettingsActivity.class));
} }
break; break;
@ -1051,7 +971,7 @@ public class MainActivity extends AppCompatActivity
} }
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main__layout); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main__layout);
drawer.closeDrawer(GravityCompat.START); if(drawer != null) drawer.closeDrawer(GravityCompat.START);
return true; return true;
} }

View file

@ -2,12 +2,12 @@ package com.github.dfa.diaspora_android.activity;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.preference.EditTextPreference; import android.preference.EditTextPreference;
import android.preference.ListPreference; import android.preference.ListPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.util.Log; import android.util.Log;
@ -20,20 +20,19 @@ import com.github.dfa.diaspora_android.data.AppSettings;
*/ */
public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
private Intent settingsChangedIntent;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName("app");
addPreferencesFromResource(R.xml.preferences); addPreferencesFromResource(R.xml.preferences);
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
setEditTextPreferenceSummaries(); setPreferenceSummaries();
settingsChangedIntent = new Intent("SettingsChanged");
} }
private void setEditTextPreferenceSummaries() { private void setPreferenceSummaries() {
String[] prefKeys = new String[]{"pref_key_proxy_host", "pref_key_proxy_port"}; String[] editTextKeys = new String[]{"pref_key_proxy_host", "pref_key_proxy_port"};
for(String key : prefKeys) { for(String key : editTextKeys) {
EditTextPreference p = (EditTextPreference) findPreference(key); EditTextPreference p = (EditTextPreference) findPreference(key);
p.setSummary(p.getText()); p.setSummary(p.getText());
} }
@ -42,12 +41,6 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
updatePreference(findPreference(key), 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) { private void updatePreference(Preference preference, String key) {
@ -63,12 +56,4 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
return; return;
} }
} }
@Override
public void finish() {
Log.d(App.TAG, "finish()"); //TODO: remove
LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
broadcastManager.sendBroadcast(settingsChangedIntent);
super.finish();
}
} }

View file

@ -2,6 +2,9 @@ package com.github.dfa.diaspora_android.data;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; 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. * Created by gsantner on 20.03.16. Part of Diaspora for Android.
@ -95,11 +98,17 @@ public class AppSettings {
} }
public int getMinimumFontSize() { public int getMinimumFontSize() {
return prefApp.getInt(PREF.MINIMUM_FONT_SIZE, 8); switch (prefApp.getString(PREF.MINIMUM_FONT_SIZE, "")) {
} case "huge":
return 20;
public void setMinimumFontSize(int minimumFontSize) { case "large":
setInt(prefApp, PREF.MINIMUM_FONT_SIZE, minimumFontSize); return 16;
case "normal":
return 8;
default:
prefApp.edit().putString(PREF.MINIMUM_FONT_SIZE, "normal").apply();
return 8;
}
} }
public String getAvatarUrl() { public String getAvatarUrl() {

View file

@ -14,9 +14,9 @@
</array> </array>
<!-- prefix 's' is needed to make this a string array. Otherwise ListPreference would crash --> <!-- prefix 's' is needed to make this a string array. Otherwise ListPreference would crash -->
<array name="pref_entry_values_font_size" translatable="false"> <array name="pref_entry_values_font_size" translatable="false">
<item>s8</item> <item>normal</item>
<item>s16</item> <item>large</item>
<item>s20</item> <item>huge</item>
</array> </array>
<!-- Mobile/Desktop view --> <!-- Mobile/Desktop view -->
<string name="pref_title_desktop_view">Desktop mode</string> <string name="pref_title_desktop_view">Desktop mode</string>

View file

@ -6,8 +6,8 @@
<ListPreference <ListPreference
android:title="@string/pref_title_font_size" android:title="@string/pref_title_font_size"
android:key="pref_key_font_size" android:key="pref_key_font_size"
android:summary="%s"
android:dialogTitle="@string/pref_title_font_size" android:dialogTitle="@string/pref_title_font_size"
android:summary="%s"
android:entries="@array/pref_entries_font_size" android:entries="@array/pref_entries_font_size"
android:entryValues="@array/pref_entry_values_font_size" /> android:entryValues="@array/pref_entry_values_font_size" />
<CheckBoxPreference <CheckBoxPreference