From 14849b3eb406ec6f02073bf800931d3083bf4254 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 11 Jun 2015 12:36:09 +0300 Subject: [PATCH] cleaner implementation for restarting on key preference changes --- .../java/org/fox/ttrss/CommonActivity.java | 68 ++++--------------- .../java/org/fox/ttrss/MasterActivity.java | 10 --- 2 files changed, 14 insertions(+), 64 deletions(-) diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/CommonActivity.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/CommonActivity.java index 9f2fa13e..326b88f6 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/CommonActivity.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/CommonActivity.java @@ -4,7 +4,6 @@ package org.fox.ttrss; import android.annotation.SuppressLint; import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; -import android.graphics.Point; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v7.app.ActionBarActivity; @@ -14,7 +13,9 @@ import android.widget.Toast; import org.fox.ttrss.util.DatabaseHelper; -public class CommonActivity extends ActionBarActivity { +import java.util.Arrays; + +public class CommonActivity extends ActionBarActivity implements SharedPreferences.OnSharedPreferenceChangeListener { private final String TAG = this.getClass().getSimpleName(); public final static String FRAG_HEADLINES = "headlines"; @@ -38,22 +39,10 @@ public class CommonActivity extends ActionBarActivity { private boolean m_smallScreenMode = true; private String m_theme; + private boolean m_needRestart; protected SharedPreferences m_prefs; - /* protected void enableHttpCaching() { - // enable resource caching - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - try { - File httpCacheDir = new File(getApplicationContext().getCacheDir(), "http"); - long httpCacheSize = 10 * 1024 * 1024; // 10 MiB - HttpResponseCache.install(httpCacheDir, httpCacheSize); - } catch (IOException e) { - e.printStackTrace(); - } - } - } */ - protected void setSmallScreen(boolean smallScreen) { Log.d(TAG, "m_smallScreenMode=" + smallScreen); m_smallScreenMode = smallScreen; @@ -96,9 +85,8 @@ public class CommonActivity extends ActionBarActivity { public void onResume() { super.onResume(); - if (!m_theme.equals(m_prefs.getString("theme", CommonActivity.THEME_DEFAULT))) { - - Log.d(TAG, "theme changed, restarting"); + if (m_needRestart) { + Log.d(TAG, "restart requested"); finish(); startActivity(getIntent()); @@ -117,6 +105,8 @@ public class CommonActivity extends ActionBarActivity { m_prefs = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); + m_prefs.registerOnSharedPreferenceChangeListener(this); + if (savedInstanceState != null) { m_theme = savedInstanceState.getString("theme"); } else { @@ -126,11 +116,6 @@ public class CommonActivity extends ActionBarActivity { super.onCreate(savedInstanceState); } - public int dpToPx(int dp) { - float density = getResources().getDisplayMetrics().density; - return Math.round((float)dp * density); - } - @Override public void onSaveInstanceState(Bundle out) { super.onSaveInstanceState(out); @@ -167,48 +152,23 @@ public class CommonActivity extends ActionBarActivity { toast.show(); } - /* public boolean isDarkTheme() { - String theme = m_prefs.getString("theme", THEME_DEFAULT); - - return theme.equals(THEME_DARK); || theme.equals(THEME_AMBER); - } */ - protected void setAppTheme(SharedPreferences prefs) { String theme = prefs.getString("theme", CommonActivity.THEME_DEFAULT); if (theme.equals(THEME_DARK)) { setTheme(R.style.DarkTheme); - /* } else if (theme.equals(THEME_AMBER)) { - setTheme(R.style.AmberTheme); - } else if (theme.equals(THEME_SEPIA)) { - setTheme(R.style.SepiaTheme); */ } else { setTheme(R.style.LightTheme); } } - /* protected void setDarkAppTheme(SharedPreferences prefs) { - String theme = prefs.getString("theme", CommonActivity.THEME_DEFAULT); + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + Log.d(TAG, "onSharedPreferenceChanged:" + key); - if (theme.equals(THEME_AMBER)) { - setTheme(R.style.AmberTheme); - } else { - setTheme(R.style.DarkTheme); - } - } */ + String[] filter = new String[] { "theme", "enable_cats", "headline_mode" }; - @SuppressWarnings("deprecation") - @SuppressLint("NewApi") - protected int getScreenWidthInPixel() { - Display display = getWindowManager().getDefaultDisplay(); - - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) { - Point size = new Point(); - display.getSize(size); - int width = size.x; - return width; - } else { - return display.getWidth(); - } + m_needRestart = Arrays.asList(filter).indexOf(key) != -1; } } + diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/MasterActivity.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/MasterActivity.java index 2c3abf1a..908c839a 100644 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/MasterActivity.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/MasterActivity.java @@ -40,7 +40,6 @@ public class MasterActivity extends OnlineActivity implements HeadlinesEventList private boolean m_feedIsSelected = false; private boolean m_userFeedSelected = false; - private boolean m_catsEnabled = false; private ActionBarDrawerToggle m_drawerToggle; private DrawerLayout m_drawerLayout; @@ -62,8 +61,6 @@ public class MasterActivity extends OnlineActivity implements HeadlinesEventList Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); - m_catsEnabled = m_prefs.getBoolean("enable_cats", false); - Application.getInstance().load(savedInstanceState); m_drawerLayout = (DrawerLayout) findViewById(R.id.headlines_drawer); @@ -413,13 +410,6 @@ public class MasterActivity extends OnlineActivity implements HeadlinesEventList super.onResume(); invalidateOptionsMenu(); - if (!m_catsEnabled == m_prefs.getBoolean("enable_cats", false)) { - Log.d(TAG, "root view settings changed, restarting"); - - finish(); - startActivity(getIntent()); - } - } @Override