Refactor theme changing to avoid code repetition
This commit is contained in:
@@ -33,7 +33,6 @@ import android.preference.PreferenceFragment;
|
|||||||
import android.preference.PreferenceScreen;
|
import android.preference.PreferenceScreen;
|
||||||
import android.support.v7.widget.Toolbar;
|
import android.support.v7.widget.Toolbar;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.view.ContextThemeWrapper;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
@@ -42,6 +41,7 @@ import org.sufficientlysecure.keychain.Constants;
|
|||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.compatibility.AppCompatPreferenceActivity;
|
import org.sufficientlysecure.keychain.compatibility.AppCompatPreferenceActivity;
|
||||||
import org.sufficientlysecure.keychain.ui.util.Notify;
|
import org.sufficientlysecure.keychain.ui.util.Notify;
|
||||||
|
import org.sufficientlysecure.keychain.ui.util.ThemeChanger;
|
||||||
import org.sufficientlysecure.keychain.ui.widget.IntegerListPreference;
|
import org.sufficientlysecure.keychain.ui.widget.IntegerListPreference;
|
||||||
import org.sufficientlysecure.keychain.util.Log;
|
import org.sufficientlysecure.keychain.util.Log;
|
||||||
import org.sufficientlysecure.keychain.util.Preferences;
|
import org.sufficientlysecure.keychain.util.Preferences;
|
||||||
@@ -60,13 +60,14 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
|
|||||||
|
|
||||||
private PreferenceScreen mKeyServerPreference = null;
|
private PreferenceScreen mKeyServerPreference = null;
|
||||||
private static Preferences sPreferences;
|
private static Preferences sPreferences;
|
||||||
private String mCurrentTheme = null;
|
private ThemeChanger mThemeChanger;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
sPreferences = Preferences.getPreferences(this);
|
sPreferences = Preferences.getPreferences(this);
|
||||||
|
|
||||||
changeTheme();
|
mThemeChanger = new ThemeChanger(this);
|
||||||
|
mThemeChanger.changeTheme();
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
setupToolbar();
|
setupToolbar();
|
||||||
@@ -122,31 +123,13 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
|
|||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
if (changeTheme()) {
|
if (mThemeChanger.changeTheme()) {
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
finish();
|
finish();
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean changeTheme() {
|
|
||||||
String newTheme = sPreferences.getTheme();
|
|
||||||
if (mCurrentTheme != null && mCurrentTheme.equals(newTheme)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int themeId = R.style.LightTheme;
|
|
||||||
if (Constants.Pref.Theme.DARK.equals(newTheme)) {
|
|
||||||
themeId = R.style.DarkTheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
ContextThemeWrapper w = new ContextThemeWrapper(this, themeId);
|
|
||||||
getTheme().setTo(w.getTheme());
|
|
||||||
mCurrentTheme = newTheme;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hack to get Toolbar in PreferenceActivity. See http://stackoverflow.com/a/26614696
|
* Hack to get Toolbar in PreferenceActivity. See http://stackoverflow.com/a/26614696
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -23,16 +23,14 @@ import android.os.Bundle;
|
|||||||
import android.support.v7.app.ActionBar;
|
import android.support.v7.app.ActionBar;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.support.v7.widget.Toolbar;
|
import android.support.v7.widget.Toolbar;
|
||||||
import android.view.ContextThemeWrapper;
|
|
||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import org.sufficientlysecure.keychain.Constants;
|
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.util.Preferences;
|
import org.sufficientlysecure.keychain.ui.util.ThemeChanger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setups Toolbar
|
* Setups Toolbar
|
||||||
@@ -40,14 +38,12 @@ import org.sufficientlysecure.keychain.util.Preferences;
|
|||||||
public abstract class BaseActivity extends AppCompatActivity {
|
public abstract class BaseActivity extends AppCompatActivity {
|
||||||
protected Toolbar mToolbar;
|
protected Toolbar mToolbar;
|
||||||
protected View mStatusBar;
|
protected View mStatusBar;
|
||||||
private static Preferences sPreferences;
|
protected ThemeChanger mThemeChanger;
|
||||||
private String mCurrentTheme = null;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
sPreferences = Preferences.getPreferences(this);
|
mThemeChanger = new ThemeChanger(this);
|
||||||
|
mThemeChanger.changeTheme();
|
||||||
changeTheme();
|
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
initLayout();
|
initLayout();
|
||||||
initToolbar();
|
initToolbar();
|
||||||
@@ -57,37 +53,13 @@ public abstract class BaseActivity extends AppCompatActivity {
|
|||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
if (changeTheme()) {
|
if (mThemeChanger.changeTheme()) {
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
finish();
|
finish();
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Apply the theme set in preferences if it isn't equal to mCurrentTheme
|
|
||||||
* anymore or mCurrentTheme hasn't been set yet.
|
|
||||||
* If a new theme is applied in this method, then return true, so
|
|
||||||
* the caller can re-create the activity, if need be.
|
|
||||||
*/
|
|
||||||
protected boolean changeTheme() {
|
|
||||||
String newTheme = sPreferences.getTheme();
|
|
||||||
if (mCurrentTheme != null && mCurrentTheme.equals(newTheme)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int themeId = R.style.LightTheme;
|
|
||||||
if (Constants.Pref.Theme.DARK.equals(newTheme)) {
|
|
||||||
themeId = R.style.DarkTheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
ContextThemeWrapper w = new ContextThemeWrapper(this, themeId);
|
|
||||||
getTheme().setTo(w.getTheme());
|
|
||||||
mCurrentTheme = newTheme;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void initLayout() {
|
protected void initLayout() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2015 Thialfihar <thi@thialfihar.org>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.sufficientlysecure.keychain.ui.util;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.view.ContextThemeWrapper;
|
||||||
|
|
||||||
|
import org.sufficientlysecure.keychain.Constants;
|
||||||
|
import org.sufficientlysecure.keychain.R;
|
||||||
|
import org.sufficientlysecure.keychain.util.Preferences;
|
||||||
|
|
||||||
|
public class ThemeChanger {
|
||||||
|
private Context mContext;
|
||||||
|
private Preferences mPreferences;
|
||||||
|
private String mCurrentTheme = null;
|
||||||
|
|
||||||
|
public ThemeChanger(Context context) {
|
||||||
|
mContext = context;
|
||||||
|
mPreferences = Preferences.getPreferences(mContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply the theme set in preferences if it isn't equal to mCurrentTheme
|
||||||
|
* anymore or mCurrentTheme hasn't been set yet.
|
||||||
|
* If a new theme is applied in this method, then return true, so
|
||||||
|
* the caller can re-create the activity, if need be.
|
||||||
|
*/
|
||||||
|
public boolean changeTheme() {
|
||||||
|
String newTheme = mPreferences.getTheme();
|
||||||
|
if (mCurrentTheme != null && mCurrentTheme.equals(newTheme)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int themeId = R.style.LightTheme;
|
||||||
|
if (Constants.Pref.Theme.DARK.equals(newTheme)) {
|
||||||
|
themeId = R.style.DarkTheme;
|
||||||
|
}
|
||||||
|
|
||||||
|
ContextThemeWrapper w = new ContextThemeWrapper(mContext, themeId);
|
||||||
|
mContext.getTheme().setTo(w.getTheme());
|
||||||
|
mCurrentTheme = newTheme;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user