Abstract preference queries away into a Preferences class

This commit is contained in:
Alexander Bakker 2018-05-11 20:08:51 +02:00
parent 9b6da0d3e3
commit 1ade4a3c4f
5 changed files with 53 additions and 21 deletions

View file

@ -2,12 +2,10 @@ package me.impy.aegis;
import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.annotation.RequiresApi;
import java.util.Collections;
@ -18,6 +16,7 @@ import me.impy.aegis.ui.MainActivity;
public class AegisApplication extends Application {
private boolean _running = false;
private DatabaseManager _manager = new DatabaseManager(this);
private Preferences _prefs = new Preferences(this);
@Override
public void onCreate() {
@ -32,8 +31,8 @@ public class AegisApplication extends Application {
return _manager;
}
public SharedPreferences getPreferences() {
return PreferenceManager.getDefaultSharedPreferences(this);
public Preferences getPreferences() {
return _prefs;
}
public boolean isRunning() {

View file

@ -0,0 +1,37 @@
package me.impy.aegis;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class Preferences {
private SharedPreferences _prefs;
public Preferences(Context context) {
_prefs = PreferenceManager.getDefaultSharedPreferences(context);
}
public boolean isDarkModeEnabled() {
return _prefs.getBoolean("pref_dark_mode", false);
}
public boolean isSecureScreenEnabled() {
return _prefs.getBoolean("pref_secure_screen", true);
}
public boolean isIssuerVisible() {
return _prefs.getBoolean("pref_issuer", false);
}
public boolean isIntroDone() {
return _prefs.getBoolean("pref_intro", false);
}
public void setIntroDone(boolean done) {
_prefs.edit().putBoolean("pref_intro", done).apply();
}
public int getTimeout() {
return _prefs.getInt("pref_timeout", -1);
}
}

View file

@ -5,10 +5,10 @@ import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import me.impy.aegis.AegisApplication;
import me.impy.aegis.Preferences;
public abstract class AegisActivity extends AppCompatActivity {
private AegisApplication _app;
private boolean _darkMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -16,21 +16,20 @@ public abstract class AegisActivity extends AppCompatActivity {
_app = (AegisApplication) getApplication();
// set FLAG_SECURE on the window of every AegisActivity
if (_app.getPreferences().getBoolean("pref_secure_screen", true)) {
if (getPreferences().isSecureScreenEnabled()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
}
// set the theme
_darkMode = _app.getPreferences().getBoolean("pref_dark_mode", false);
setPreferredTheme(_darkMode);
setPreferredTheme(getPreferences().isDarkModeEnabled());
}
protected AegisApplication getApp() {
return _app;
}
protected boolean isDark() {
return _darkMode;
protected Preferences getPreferences() {
return _app.getPreferences();
}
protected abstract void setPreferredTheme(boolean darkMode);

View file

@ -15,9 +15,8 @@ import org.json.JSONObject;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import me.impy.aegis.AegisApplication;
import me.impy.aegis.Preferences;
import me.impy.aegis.R;
import me.impy.aegis.crypto.CryptResult;
import me.impy.aegis.crypto.MasterKey;
import me.impy.aegis.db.DatabaseException;
import me.impy.aegis.db.DatabaseFileException;
@ -47,15 +46,15 @@ public class IntroActivity extends AppIntro implements DerivationTask.Callback {
private PasswordSlot _passwordSlot;
private Cipher _passwordCipher;
private AegisApplication _app;
private Preferences _prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_app = (AegisApplication) getApplication();
// set FLAG_SECURE on the window of every IntroActivity
if (_app.getPreferences().getBoolean("pref_secure_screen", true)) {
_prefs = new Preferences(this);
if (_prefs.isSecureScreenEnabled()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
}
@ -196,7 +195,7 @@ public class IntroActivity extends AppIntro implements DerivationTask.Callback {
setResult(RESULT_OK, result);
// skip the intro from now on
_app.getPreferences().edit().putBoolean("pref_intro", true).apply();
_prefs.setIntroDone(true);
finish();
}

View file

@ -11,8 +11,6 @@ import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
@ -63,7 +61,7 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
// set up the key profile view
_keyProfileView = (KeyProfileView) getSupportFragmentManager().findFragmentById(R.id.key_profiles);
_keyProfileView.setListener(this);
_keyProfileView.setShowIssuer(_app.getPreferences().getBoolean("pref_issuer", false));
_keyProfileView.setShowIssuer(getPreferences().isIssuerVisible());
// set up the floating action button
_fabMenu = findViewById(R.id.fab);
@ -80,7 +78,7 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
if (!_app.isRunning() && _db.isLocked()) {
if (!_db.fileExists()) {
// the db doesn't exist, start the intro
if (_app.getPreferences().getBoolean("pref_intro", false)) {
if (getPreferences().isIntroDone()) {
Toast.makeText(this, "Database file not found, starting over...", Toast.LENGTH_SHORT).show();
}
Intent intro = new Intent(this, IntroActivity.class);
@ -195,7 +193,7 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
if (data.getBooleanExtra("needsRecreate", false)) {
recreate();
} else if (data.getBooleanExtra("needsRefresh", false)) {
boolean showIssuer = _app.getPreferences().getBoolean("pref_issuer", false);
boolean showIssuer = getPreferences().isIssuerVisible();
_keyProfileView.setShowIssuer(showIssuer);
}
}