Move the theme setup to a base activity class

This commit is contained in:
Jakob Nixdorf 2017-07-31 08:43:45 +02:00
parent 3244ed3887
commit a5f9ec4efb
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
5 changed files with 33 additions and 68 deletions

View file

@ -23,13 +23,10 @@
package org.shadowice.flocke.andotp; package org.shadowice.flocke.andotp;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.View; import android.view.View;
import android.view.ViewStub; import android.view.ViewStub;
@ -38,7 +35,7 @@ import android.widget.TextView;
import de.psdev.licensesdialog.LicensesDialog; 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 GITHUB_URI = "https://github.com/flocke/andOTP";
private static final String CHANGELOG_URI = GITHUB_URI + "/blob/master/CHANGELOG.md"; private static final String CHANGELOG_URI = GITHUB_URI + "/blob/master/CHANGELOG.md";
private static final String MIT_URI = GITHUB_URI + "/blob/master/LICENSE.txt"; 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 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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
setThemeFromPrefs();
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setTitle(R.string.about_activity_title); setTitle(R.string.about_activity_title);
setContentView(R.layout.activity_container); setContentView(R.layout.activity_container);
Toolbar toolbar = (Toolbar) findViewById(R.id.container_toolbar); Toolbar toolbar = (Toolbar) findViewById(R.id.container_toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
@ -160,7 +145,6 @@ public class AboutActivity extends AppCompatActivity {
openURI(BUGREPORT_URI); openURI(BUGREPORT_URI);
} }
}); });
} }
// Go back to the main activity // Go back to the main activity

View file

@ -36,7 +36,6 @@ import android.preference.PreferenceManager;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@ -59,7 +58,7 @@ import java.util.ArrayList;
import javax.crypto.SecretKey; 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_OPEN_DOCUMENT_PLAIN = 100;
private final static int INTENT_SAVE_DOCUMENT_PLAIN = 101; private final static int INTENT_SAVE_DOCUMENT_PLAIN = 101;
private final static int INTENT_OPEN_DOCUMENT_CRYPT = 102; private final static int INTENT_OPEN_DOCUMENT_CRYPT = 102;
@ -93,25 +92,13 @@ public class BackupActivity extends AppCompatActivity {
private boolean reload = false; 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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
setThemeFromPrefs();
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setTitle(R.string.backup_activity_title); setTitle(R.string.backup_activity_title);
setContentView(R.layout.activity_container); setContentView(R.layout.activity_container);
Toolbar toolbar = (Toolbar) findViewById(R.id.container_toolbar); Toolbar toolbar = (Toolbar) findViewById(R.id.container_toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);

View file

@ -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);
}
}

View file

@ -35,7 +35,6 @@ import android.preference.PreferenceManager;
import android.provider.Settings; import android.provider.Settings;
import android.support.constraint.ConstraintLayout; import android.support.constraint.ConstraintLayout;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView;
@ -58,7 +57,7 @@ import com.google.zxing.integration.android.IntentResult;
import org.shadowice.flocke.andotp.ItemTouchHelper.SimpleItemTouchHelperCallback; import org.shadowice.flocke.andotp.ItemTouchHelper.SimpleItemTouchHelperCallback;
public class MainActivity extends AppCompatActivity public class MainActivity extends BaseActivity
implements SharedPreferences.OnSharedPreferenceChangeListener { implements SharedPreferences.OnSharedPreferenceChangeListener {
private EntriesCardAdapter adapter; private EntriesCardAdapter adapter;
@ -141,25 +140,9 @@ public class MainActivity extends AppCompatActivity
.show(); .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 // Initialize the main application
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
sharedPref.registerOnSharedPreferenceChangeListener(this);
setThemeFromPrefs();
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setTitle(R.string.app_name); setTitle(R.string.app_name);
@ -169,6 +152,10 @@ public class MainActivity extends AppCompatActivity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
sharedPref.registerOnSharedPreferenceChangeListener(this);
if (savedInstanceState == null) { if (savedInstanceState == null) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
if(sharedPref.getBoolean(getString(R.string.settings_key_auth_device), false)) { if(sharedPref.getBoolean(getString(R.string.settings_key_auth_device), false)) {

View file

@ -24,40 +24,25 @@ package org.shadowice.flocke.andotp;
import android.app.KeyguardManager; import android.app.KeyguardManager;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.ViewStub; import android.view.ViewStub;
import org.openintents.openpgp.util.OpenPgpAppPreference; import org.openintents.openpgp.util.OpenPgpAppPreference;
import org.openintents.openpgp.util.OpenPgpKeyPreference; import org.openintents.openpgp.util.OpenPgpKeyPreference;
public class SettingsActivity extends AppCompatActivity { public class SettingsActivity extends BaseActivity {
SettingsFragment fragement; 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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
setThemeFromPrefs();
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setTitle(R.string.settings_activity_title); setTitle(R.string.settings_activity_title);
setContentView(R.layout.activity_container); setContentView(R.layout.activity_container);
Toolbar toolbar = (Toolbar) findViewById(R.id.container_toolbar); Toolbar toolbar = (Toolbar) findViewById(R.id.container_toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);