From a5f9ec4efbf3bad0bcd8d86bee16c68abb9e601c Mon Sep 17 00:00:00 2001 From: Jakob Nixdorf Date: Mon, 31 Jul 2017 08:43:45 +0200 Subject: [PATCH] Move the theme setup to a base activity class --- .../flocke/andotp/AboutActivity.java | 20 ++-------------- .../flocke/andotp/BackupActivity.java | 17 ++------------ .../shadowice/flocke/andotp/BaseActivity.java | 22 ++++++++++++++++++ .../shadowice/flocke/andotp/MainActivity.java | 23 ++++--------------- .../flocke/andotp/SettingsActivity.java | 19 ++------------- 5 files changed, 33 insertions(+), 68 deletions(-) create mode 100644 app/src/main/java/org/shadowice/flocke/andotp/BaseActivity.java diff --git a/app/src/main/java/org/shadowice/flocke/andotp/AboutActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/AboutActivity.java index 66428c58..fe4fc550 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/AboutActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/AboutActivity.java @@ -23,13 +23,10 @@ package org.shadowice.flocke.andotp; import android.content.Intent; -import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; -import android.preference.PreferenceManager; -import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.ViewStub; @@ -38,7 +35,7 @@ import android.widget.TextView; import de.psdev.licensesdialog.LicensesDialog; -public class AboutActivity extends AppCompatActivity { +public class AboutActivity extends BaseActivity { private static final String GITHUB_URI = "https://github.com/flocke/andOTP"; private static final String CHANGELOG_URI = GITHUB_URI + "/blob/master/CHANGELOG.md"; private static final String MIT_URI = GITHUB_URI + "/blob/master/LICENSE.txt"; @@ -51,25 +48,13 @@ public class AboutActivity extends AppCompatActivity { private static final String BUGREPORT_URI = GITHUB_URI + "/issues"; - private void setThemeFromPrefs() { - SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); - String theme = sharedPref.getString(getString(R.string.settings_key_theme), getString(R.string.settings_default_theme)); - - if (theme.equals("light")) { - setTheme(R.style.AppTheme_NoActionBar); - } else if (theme.equals("dark")) { - setTheme(R.style.AppTheme_Dark_NoActionBar); - } - } - @Override protected void onCreate(Bundle savedInstanceState) { - setThemeFromPrefs(); - super.onCreate(savedInstanceState); setTitle(R.string.about_activity_title); setContentView(R.layout.activity_container); + Toolbar toolbar = (Toolbar) findViewById(R.id.container_toolbar); setSupportActionBar(toolbar); @@ -160,7 +145,6 @@ public class AboutActivity extends AppCompatActivity { openURI(BUGREPORT_URI); } }); - } // Go back to the main activity diff --git a/app/src/main/java/org/shadowice/flocke/andotp/BackupActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/BackupActivity.java index ec770ce9..f0b2c361 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/BackupActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/BackupActivity.java @@ -36,7 +36,6 @@ import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; -import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.text.TextUtils; import android.util.Log; @@ -59,7 +58,7 @@ import java.util.ArrayList; import javax.crypto.SecretKey; -public class BackupActivity extends AppCompatActivity { +public class BackupActivity extends BaseActivity { private final static int INTENT_OPEN_DOCUMENT_PLAIN = 100; private final static int INTENT_SAVE_DOCUMENT_PLAIN = 101; private final static int INTENT_OPEN_DOCUMENT_CRYPT = 102; @@ -93,25 +92,13 @@ public class BackupActivity extends AppCompatActivity { private boolean reload = false; - private void setThemeFromPrefs() { - SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); - String theme = sharedPref.getString(getString(R.string.settings_key_theme), getString(R.string.settings_default_theme)); - - if (theme.equals("light")) { - setTheme(R.style.AppTheme_NoActionBar); - } else if (theme.equals("dark")) { - setTheme(R.style.AppTheme_Dark_NoActionBar); - } - } - @Override protected void onCreate(Bundle savedInstanceState) { - setThemeFromPrefs(); - super.onCreate(savedInstanceState); setTitle(R.string.backup_activity_title); setContentView(R.layout.activity_container); + Toolbar toolbar = (Toolbar) findViewById(R.id.container_toolbar); setSupportActionBar(toolbar); diff --git a/app/src/main/java/org/shadowice/flocke/andotp/BaseActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/BaseActivity.java new file mode 100644 index 00000000..87054520 --- /dev/null +++ b/app/src/main/java/org/shadowice/flocke/andotp/BaseActivity.java @@ -0,0 +1,22 @@ +package org.shadowice.flocke.andotp; + +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.support.v7.app.AppCompatActivity; + +public class BaseActivity extends AppCompatActivity { + @Override + protected void onCreate(Bundle savedInstanceState) { + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + String theme = sharedPref.getString(getString(R.string.settings_key_theme), getString(R.string.settings_default_theme)); + + if (theme.equals("light")) { + setTheme(R.style.AppTheme_NoActionBar); + } else if (theme.equals("dark")) { + setTheme(R.style.AppTheme_Dark_NoActionBar); + } + + super.onCreate(savedInstanceState); + } +} diff --git a/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java index f5340517..2d7d0660 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java @@ -35,7 +35,6 @@ import android.preference.PreferenceManager; import android.provider.Settings; import android.support.constraint.ConstraintLayout; import android.support.v4.view.MenuItemCompat; -import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.SearchView; @@ -58,7 +57,7 @@ import com.google.zxing.integration.android.IntentResult; import org.shadowice.flocke.andotp.ItemTouchHelper.SimpleItemTouchHelperCallback; -public class MainActivity extends AppCompatActivity +public class MainActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener { private EntriesCardAdapter adapter; @@ -141,25 +140,9 @@ public class MainActivity extends AppCompatActivity .show(); } - private void setThemeFromPrefs() { - String theme = sharedPref.getString(getString(R.string.settings_key_theme), getString(R.string.settings_default_theme)); - - if (theme.equals("light")) { - setTheme(R.style.AppTheme_NoActionBar); - } else if (theme.equals("dark")) { - setTheme(R.style.AppTheme_Dark_NoActionBar); - } - } - // Initialize the main application @Override protected void onCreate(Bundle savedInstanceState) { - PreferenceManager.setDefaultValues(this, R.xml.preferences, false); - sharedPref = PreferenceManager.getDefaultSharedPreferences(this); - sharedPref.registerOnSharedPreferenceChangeListener(this); - - setThemeFromPrefs(); - super.onCreate(savedInstanceState); setTitle(R.string.app_name); @@ -169,6 +152,10 @@ public class MainActivity extends AppCompatActivity Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); + PreferenceManager.setDefaultValues(this, R.xml.preferences, false); + sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + sharedPref.registerOnSharedPreferenceChangeListener(this); + if (savedInstanceState == null) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { if(sharedPref.getBoolean(getString(R.string.settings_key_auth_device), false)) { diff --git a/app/src/main/java/org/shadowice/flocke/andotp/SettingsActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/SettingsActivity.java index 1bd51bf0..5c890338 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/SettingsActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/SettingsActivity.java @@ -24,40 +24,25 @@ package org.shadowice.flocke.andotp; import android.app.KeyguardManager; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceFragment; -import android.preference.PreferenceManager; -import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.ViewStub; import org.openintents.openpgp.util.OpenPgpAppPreference; import org.openintents.openpgp.util.OpenPgpKeyPreference; -public class SettingsActivity extends AppCompatActivity { +public class SettingsActivity extends BaseActivity { SettingsFragment fragement; - private void setThemeFromPrefs() { - SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); - String theme = sharedPref.getString(getString(R.string.settings_key_theme), getString(R.string.settings_default_theme)); - - if (theme.equals("light")) { - setTheme(R.style.AppTheme_NoActionBar); - } else if (theme.equals("dark")) { - setTheme(R.style.AppTheme_Dark_NoActionBar); - } - } - @Override protected void onCreate(Bundle savedInstanceState) { - setThemeFromPrefs(); - super.onCreate(savedInstanceState); setTitle(R.string.settings_activity_title); setContentView(R.layout.activity_container); + Toolbar toolbar = (Toolbar) findViewById(R.id.container_toolbar); setSupportActionBar(toolbar);