mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-04-21 22:39:12 +00:00
Abstract preference queries away into a Preferences class
This commit is contained in:
parent
9b6da0d3e3
commit
1ade4a3c4f
5 changed files with 53 additions and 21 deletions
|
@ -2,12 +2,10 @@ package me.impy.aegis;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.content.pm.ShortcutInfo;
|
import android.content.pm.ShortcutInfo;
|
||||||
import android.content.pm.ShortcutManager;
|
import android.content.pm.ShortcutManager;
|
||||||
import android.graphics.drawable.Icon;
|
import android.graphics.drawable.Icon;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.preference.PreferenceManager;
|
|
||||||
import android.support.annotation.RequiresApi;
|
import android.support.annotation.RequiresApi;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -18,6 +16,7 @@ import me.impy.aegis.ui.MainActivity;
|
||||||
public class AegisApplication extends Application {
|
public class AegisApplication extends Application {
|
||||||
private boolean _running = false;
|
private boolean _running = false;
|
||||||
private DatabaseManager _manager = new DatabaseManager(this);
|
private DatabaseManager _manager = new DatabaseManager(this);
|
||||||
|
private Preferences _prefs = new Preferences(this);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
|
@ -32,8 +31,8 @@ public class AegisApplication extends Application {
|
||||||
return _manager;
|
return _manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SharedPreferences getPreferences() {
|
public Preferences getPreferences() {
|
||||||
return PreferenceManager.getDefaultSharedPreferences(this);
|
return _prefs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isRunning() {
|
public boolean isRunning() {
|
||||||
|
|
37
app/src/main/java/me/impy/aegis/Preferences.java
Normal file
37
app/src/main/java/me/impy/aegis/Preferences.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,10 +5,10 @@ import android.support.v7.app.AppCompatActivity;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
|
||||||
import me.impy.aegis.AegisApplication;
|
import me.impy.aegis.AegisApplication;
|
||||||
|
import me.impy.aegis.Preferences;
|
||||||
|
|
||||||
public abstract class AegisActivity extends AppCompatActivity {
|
public abstract class AegisActivity extends AppCompatActivity {
|
||||||
private AegisApplication _app;
|
private AegisApplication _app;
|
||||||
private boolean _darkMode;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -16,21 +16,20 @@ public abstract class AegisActivity extends AppCompatActivity {
|
||||||
_app = (AegisApplication) getApplication();
|
_app = (AegisApplication) getApplication();
|
||||||
|
|
||||||
// set FLAG_SECURE on the window of every AegisActivity
|
// 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);
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the theme
|
// set the theme
|
||||||
_darkMode = _app.getPreferences().getBoolean("pref_dark_mode", false);
|
setPreferredTheme(getPreferences().isDarkModeEnabled());
|
||||||
setPreferredTheme(_darkMode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected AegisApplication getApp() {
|
protected AegisApplication getApp() {
|
||||||
return _app;
|
return _app;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean isDark() {
|
protected Preferences getPreferences() {
|
||||||
return _darkMode;
|
return _app.getPreferences();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void setPreferredTheme(boolean darkMode);
|
protected abstract void setPreferredTheme(boolean darkMode);
|
||||||
|
|
|
@ -15,9 +15,8 @@ import org.json.JSONObject;
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.Cipher;
|
||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
|
|
||||||
import me.impy.aegis.AegisApplication;
|
import me.impy.aegis.Preferences;
|
||||||
import me.impy.aegis.R;
|
import me.impy.aegis.R;
|
||||||
import me.impy.aegis.crypto.CryptResult;
|
|
||||||
import me.impy.aegis.crypto.MasterKey;
|
import me.impy.aegis.crypto.MasterKey;
|
||||||
import me.impy.aegis.db.DatabaseException;
|
import me.impy.aegis.db.DatabaseException;
|
||||||
import me.impy.aegis.db.DatabaseFileException;
|
import me.impy.aegis.db.DatabaseFileException;
|
||||||
|
@ -47,15 +46,15 @@ public class IntroActivity extends AppIntro implements DerivationTask.Callback {
|
||||||
private PasswordSlot _passwordSlot;
|
private PasswordSlot _passwordSlot;
|
||||||
private Cipher _passwordCipher;
|
private Cipher _passwordCipher;
|
||||||
|
|
||||||
private AegisApplication _app;
|
private Preferences _prefs;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
_app = (AegisApplication) getApplication();
|
|
||||||
|
|
||||||
// set FLAG_SECURE on the window of every IntroActivity
|
// 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);
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,7 +195,7 @@ public class IntroActivity extends AppIntro implements DerivationTask.Callback {
|
||||||
setResult(RESULT_OK, result);
|
setResult(RESULT_OK, result);
|
||||||
|
|
||||||
// skip the intro from now on
|
// skip the intro from now on
|
||||||
_app.getPreferences().edit().putBoolean("pref_intro", true).apply();
|
_prefs.setIntroDone(true);
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,6 @@ import android.os.Bundle;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
@ -63,7 +61,7 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
|
||||||
// set up the key profile view
|
// set up the key profile view
|
||||||
_keyProfileView = (KeyProfileView) getSupportFragmentManager().findFragmentById(R.id.key_profiles);
|
_keyProfileView = (KeyProfileView) getSupportFragmentManager().findFragmentById(R.id.key_profiles);
|
||||||
_keyProfileView.setListener(this);
|
_keyProfileView.setListener(this);
|
||||||
_keyProfileView.setShowIssuer(_app.getPreferences().getBoolean("pref_issuer", false));
|
_keyProfileView.setShowIssuer(getPreferences().isIssuerVisible());
|
||||||
|
|
||||||
// set up the floating action button
|
// set up the floating action button
|
||||||
_fabMenu = findViewById(R.id.fab);
|
_fabMenu = findViewById(R.id.fab);
|
||||||
|
@ -80,7 +78,7 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
|
||||||
if (!_app.isRunning() && _db.isLocked()) {
|
if (!_app.isRunning() && _db.isLocked()) {
|
||||||
if (!_db.fileExists()) {
|
if (!_db.fileExists()) {
|
||||||
// the db doesn't exist, start the intro
|
// 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();
|
Toast.makeText(this, "Database file not found, starting over...", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
Intent intro = new Intent(this, IntroActivity.class);
|
Intent intro = new Intent(this, IntroActivity.class);
|
||||||
|
@ -195,7 +193,7 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
|
||||||
if (data.getBooleanExtra("needsRecreate", false)) {
|
if (data.getBooleanExtra("needsRecreate", false)) {
|
||||||
recreate();
|
recreate();
|
||||||
} else if (data.getBooleanExtra("needsRefresh", false)) {
|
} else if (data.getBooleanExtra("needsRefresh", false)) {
|
||||||
boolean showIssuer = _app.getPreferences().getBoolean("pref_issuer", false);
|
boolean showIssuer = getPreferences().isIssuerVisible();
|
||||||
_keyProfileView.setShowIssuer(showIssuer);
|
_keyProfileView.setShowIssuer(showIssuer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue