diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/AuthenticateActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/AuthenticateActivity.java index ea857de7..1ccdc1c0 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/AuthenticateActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/AuthenticateActivity.java @@ -23,9 +23,7 @@ package org.shadowice.flocke.andotp.Activities; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.design.widget.TextInputEditText; import android.support.design.widget.TextInputLayout; import android.support.v7.widget.Toolbar; @@ -42,10 +40,10 @@ import android.widget.Toast; import org.shadowice.flocke.andotp.R; +import static org.shadowice.flocke.andotp.Utilities.Settings.AuthMethod; + public class AuthenticateActivity extends BaseActivity implements EditText.OnEditorActionListener { - - private SharedPreferences settings; private String password; @Override @@ -67,11 +65,10 @@ public class AuthenticateActivity extends BaseActivity TextInputLayout passwordLayout = (TextInputLayout) v.findViewById(R.id.passwordLayout); TextInputEditText passwordInput = (TextInputEditText) v.findViewById(R.id.passwordEdit); - settings = PreferenceManager.getDefaultSharedPreferences(this); - String authMethod = settings.getString(getString(R.string.settings_key_auth), getString(R.string.settings_default_auth)); + AuthMethod authMethod = settings.getAuthMethod(); - if (authMethod.equals("password")) { - password = settings.getString(getString(R.string.settings_key_auth_password), ""); + if (authMethod == AuthMethod.PASSWORD) { + password = settings.getAuthPassword(); if (password.isEmpty()) { Toast.makeText(this, R.string.auth_toast_password_missing, Toast.LENGTH_LONG).show(); @@ -81,8 +78,8 @@ public class AuthenticateActivity extends BaseActivity passwordLayout.setHint(getString(R.string.auth_hint_password)); passwordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); } - } else if (authMethod.equals("pin")) { - password = settings.getString(getString(R.string.settings_key_auth_pin), ""); + } else if (authMethod == AuthMethod.PIN) { + password = settings.getAuthPIN(); if (password.isEmpty()) { Toast.makeText(this, R.string.auth_toast_pin_missing, Toast.LENGTH_LONG).show(); diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/BackupActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/BackupActivity.java index d5de47fb..0a903d17 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/BackupActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/BackupActivity.java @@ -28,11 +28,9 @@ import android.app.PendingIntent; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentSender; -import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; @@ -65,30 +63,28 @@ import java.util.ArrayList; import javax.crypto.SecretKey; 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; - private final static int INTENT_SAVE_DOCUMENT_CRYPT = 103; - private final static int INTENT_OPEN_DOCUMENT_PGP = 104; - private final static int INTENT_SAVE_DOCUMENT_PGP = 105; - private final static int INTENT_ENCRYPT_PGP = 106; - private final static int INTENT_DECRYPT_PGP = 107; + private final static int INTENT_OPEN_DOCUMENT_PLAIN = 200; + private final static int INTENT_SAVE_DOCUMENT_PLAIN = 201; + private final static int INTENT_OPEN_DOCUMENT_CRYPT = 202; + private final static int INTENT_SAVE_DOCUMENT_CRYPT = 203; + private final static int INTENT_OPEN_DOCUMENT_PGP = 204; + private final static int INTENT_SAVE_DOCUMENT_PGP = 205; + private final static int INTENT_ENCRYPT_PGP = 206; + private final static int INTENT_DECRYPT_PGP = 207; - private final static int PERMISSIONS_REQUEST_READ_IMPORT_PLAIN = 110; - private final static int PERMISSIONS_REQUEST_WRITE_EXPORT_PLAIN = 111; - private final static int PERMISSIONS_REQUEST_READ_IMPORT_CRYPT = 112; - private final static int PERMISSIONS_REQUEST_WRITE_EXPORT_CRYPT = 113; - private final static int PERMISSIONS_REQUEST_READ_IMPORT_PGP = 114; - private final static int PERMISSIONS_REQUEST_WRITE_EXPORT_PGP = 115; + private final static int PERMISSIONS_REQUEST_READ_IMPORT_PLAIN = 210; + private final static int PERMISSIONS_REQUEST_WRITE_EXPORT_PLAIN = 211; + private final static int PERMISSIONS_REQUEST_READ_IMPORT_CRYPT = 212; + private final static int PERMISSIONS_REQUEST_WRITE_EXPORT_CRYPT = 213; + private final static int PERMISSIONS_REQUEST_READ_IMPORT_PGP = 214; + private final static int PERMISSIONS_REQUEST_WRITE_EXPORT_PGP = 215; - private static final String DEFAULT_BACKUP_FILENAME_PLAIN = "otp_accounts.json"; - private static final String DEFAULT_BACKUP_FILENAME_CRYPT = "otp_accounts.json.aes"; - private static final String DEFAULT_BACKUP_FILENAME_PGP = "otp_accounts.json.gpg"; - private static final String DEFAULT_BACKUP_MIMETYPE_PLAIN = "application/json"; - private static final String DEFAULT_BACKUP_MIMETYPE_CRYPT = "binary/aes"; - private static final String DEFAULT_BACKUP_MIMETYPE_PGP = "application/pgp-encrypted"; - - private SharedPreferences settings; + private static final String DEFAULT_BACKUP_FILENAME_PLAIN = "otp_accounts.json"; + private static final String DEFAULT_BACKUP_FILENAME_CRYPT = "otp_accounts.json.aes"; + private static final String DEFAULT_BACKUP_FILENAME_PGP = "otp_accounts.json.gpg"; + private static final String DEFAULT_BACKUP_MIMETYPE_PLAIN = "application/json"; + private static final String DEFAULT_BACKUP_MIMETYPE_CRYPT = "binary/aes"; + private static final String DEFAULT_BACKUP_MIMETYPE_PGP = "application/pgp-encrypted"; private OpenPgpServiceConnection pgpServiceConnection; private long pgpKeyId; @@ -112,8 +108,6 @@ public class BackupActivity extends BaseActivity { stub.setLayoutResource(R.layout.content_backup); View v = stub.inflate(); - settings = PreferenceManager.getDefaultSharedPreferences(this); - // Plain-text LinearLayout backupPlain = (LinearLayout) v.findViewById(R.id.button_backup_plain); @@ -139,7 +133,7 @@ public class BackupActivity extends BaseActivity { LinearLayout backupCrypt = (LinearLayout) v.findViewById(R.id.button_backup_crypt); LinearLayout restoreCrypt = (LinearLayout) v.findViewById(R.id.button_restore_crypt); - if (settings.getString(getString(R.string.settings_key_backup_password), "").isEmpty()) { + if (settings.getBackupPassword().isEmpty()) { cryptSetup.setVisibility(View.VISIBLE); backupCrypt.setVisibility(View.GONE); restoreCrypt.setVisibility(View.GONE); @@ -165,8 +159,8 @@ public class BackupActivity extends BaseActivity { // OpenPGP - String PGPProvider = settings.getString(getString(R.string.settings_key_openpgp_provider), ""); - pgpKeyId = settings.getLong(getString(R.string.settings_key_openpgp_keyid), 0); + String PGPProvider = settings.getOpenPGPProvider(); + pgpKeyId = settings.getOpenPGPKey(); TextView setupPGP = (TextView) v.findViewById(R.id.msg_openpgp_setup); LinearLayout backupPGP = (LinearLayout) v.findViewById(R.id.button_backup_openpgp); @@ -398,7 +392,7 @@ public class BackupActivity extends BaseActivity { /* Encrypted backup functions */ private void doRestoreCrypt(Uri uri) { - String password = settings.getString(getString(R.string.settings_key_backup_password), ""); + String password = settings.getBackupPassword(); if (! password.isEmpty()) { if (StorageHelper.isExternalStorageReadable()) { @@ -434,7 +428,7 @@ public class BackupActivity extends BaseActivity { } private void doBackupCrypt(Uri uri) { - String password = settings.getString(getString(R.string.settings_key_backup_password), ""); + String password = settings.getBackupPassword(); if (! password.isEmpty()) { if (StorageHelper.isExternalStorageWritable()) { @@ -531,7 +525,7 @@ public class BackupActivity extends BaseActivity { if (encryptIntent == null) { encryptIntent = new Intent(); - if (settings.getBoolean(getString(R.string.settings_key_openpgp_sign), false)) { + if (settings.getOpenPGPSign()) { encryptIntent.setAction(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT); encryptIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, pgpKeyId); } else { @@ -573,7 +567,7 @@ public class BackupActivity extends BaseActivity { doBackupEncrypted(file, outputStreamToString(os)); } else if (requestCode == INTENT_DECRYPT_PGP) { if (os != null) { - if (settings.getBoolean(getString(R.string.settings_key_openpgp_verify), false)) { + if (settings.getOpenPGPVerify()) { OpenPgpSignatureResult sigResult = result.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE); if (sigResult.getResult() == OpenPgpSignatureResult.RESULT_VALID_KEY_CONFIRMED) { diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/BaseActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/BaseActivity.java index c1a58c44..25759958 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/BaseActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/BaseActivity.java @@ -22,18 +22,27 @@ package org.shadowice.flocke.andotp.Activities; -import android.content.SharedPreferences; +import android.app.KeyguardManager; +import android.content.Intent; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; +import android.widget.Toast; import org.shadowice.flocke.andotp.R; +import org.shadowice.flocke.andotp.Utilities.Settings; + +import static org.shadowice.flocke.andotp.Utilities.Settings.AuthMethod; public class BaseActivity extends AppCompatActivity { + private static final int INTENT_INTERNAL_AUTHENTICATE = 1; + + public Settings settings; + @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)); + settings = new Settings(this); + + String theme = settings.getTheme(); if (theme.equals("light")) { setTheme(R.style.AppTheme_NoActionBar); @@ -43,4 +52,34 @@ public class BaseActivity extends AppCompatActivity { super.onCreate(savedInstanceState); } + + public void authenticate() { + AuthMethod authMethod = settings.getAuthMethod(); + + if (authMethod == AuthMethod.DEVICE) { + KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP && km.isKeyguardSecure()) { + Intent authIntent = km.createConfirmDeviceCredentialIntent(getString(R.string.dialog_title_auth), getString(R.string.dialog_msg_auth)); + startActivityForResult(authIntent, INTENT_INTERNAL_AUTHENTICATE); + } + } else if (authMethod == AuthMethod.PASSWORD || authMethod == AuthMethod.PIN) { + Intent authIntent = new Intent(this, AuthenticateActivity.class); + startActivityForResult(authIntent, INTENT_INTERNAL_AUTHENTICATE); + } + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent intent) { + super.onActivityResult(requestCode, resultCode, intent); + + if (requestCode == INTENT_INTERNAL_AUTHENTICATE && resultCode != RESULT_OK) { + Toast.makeText(getBaseContext(), R.string.toast_auth_failed, Toast.LENGTH_LONG).show(); + + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { + finishAndRemoveTask(); + } else { + finish(); + } + } + } } diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java index 853ed99c..8a52a01e 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java @@ -25,14 +25,12 @@ package org.shadowice.flocke.andotp.Activities; import android.animation.ObjectAnimator; import android.app.AlertDialog; -import android.app.KeyguardManager; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; -import android.provider.Settings; import android.support.constraint.ConstraintLayout; import android.support.v4.view.MenuItemCompat; import android.support.v7.widget.LinearLayoutManager; @@ -62,8 +60,12 @@ import org.shadowice.flocke.andotp.View.ItemTouchHelper.SimpleItemTouchHelperCal import org.shadowice.flocke.andotp.R; import org.shadowice.flocke.andotp.Utilities.TokenCalculator; +import static org.shadowice.flocke.andotp.Utilities.Settings.SortMode; + public class MainActivity extends BaseActivity - implements SharedPreferences.OnSharedPreferenceChangeListener { + implements SharedPreferences.OnSharedPreferenceChangeListener { + private static final int INTENT_INTERNAL_SETTINGS = 101; + private static final int INTENT_INTERNAL_BACKUP = 102; private EntriesCardAdapter adapter; private FloatingActionMenu floatingActionMenu; @@ -71,15 +73,9 @@ public class MainActivity extends BaseActivity private MenuItem sortMenu; private SimpleItemTouchHelperCallback touchHelperCallback; - private SharedPreferences sharedPref; - private Handler handler; private Runnable handlerTask; - private static final int INTENT_AUTHENTICATE = 1; - private static final int INTENT_INTERNAL_SETTINGS = 2; - private static final int INTENT_INTERNAL_BACKUP = 3; - // QR code scanning private void scanQRCode(){ new IntentIntegrator(MainActivity.this) @@ -142,9 +138,7 @@ public class MainActivity extends BaseActivity .setPositiveButton(R.string.button_warned, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { - sharedPref.edit() - .putBoolean(getString(R.string.settings_key_security_backup_warning), true) - .apply(); + settings.setFirstTimeWarningShown(true); } }) .create() @@ -152,35 +146,13 @@ public class MainActivity extends BaseActivity } private void restoreSortMode(EntriesCardAdapter adapter) { - if (sharedPref != null) { - String modeStr = sharedPref.getString(getString(R.string.settings_key_sort_mode), EntriesCardAdapter.SortMode.UNSORTED.toString()); - EntriesCardAdapter.SortMode mode = EntriesCardAdapter.SortMode.valueOf(modeStr); - - adapter.setSortMode(mode); - } + if (settings != null) + adapter.setSortMode(settings.getSortMode()); } - private void saveSortMode(EntriesCardAdapter.SortMode mode) { - if (sharedPref != null) { - sharedPref.edit() - .putString(getString(R.string.settings_key_sort_mode), mode.toString()) - .apply(); - } - } - - public void authenticate() { - String authMethod = sharedPref.getString(getString(R.string.settings_key_auth), getString(R.string.settings_default_auth)); - - if (authMethod.equals("device")) { - KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP && km.isKeyguardSecure()) { - Intent authIntent = km.createConfirmDeviceCredentialIntent(getString(R.string.dialog_title_auth), getString(R.string.dialog_msg_auth)); - startActivityForResult(authIntent, INTENT_AUTHENTICATE); - } - } else if (authMethod.equals("password") || authMethod.equals("pin")) { - Intent authIntent = new Intent(this, AuthenticateActivity.class); - startActivityForResult(authIntent, INTENT_AUTHENTICATE); - } + private void saveSortMode(SortMode mode) { + if (settings != null) + settings.setSortMode(mode); } // Initialize the main application @@ -196,13 +168,12 @@ public class MainActivity extends BaseActivity setSupportActionBar(toolbar); PreferenceManager.setDefaultValues(this, R.xml.preferences, false); - sharedPref = PreferenceManager.getDefaultSharedPreferences(this); - sharedPref.registerOnSharedPreferenceChangeListener(this); + settings.registerPreferenceChangeListener(this); if (savedInstanceState == null) authenticate(); - if (! sharedPref.getBoolean(getString(R.string.settings_key_security_backup_warning), false)) { + if (! settings.getFirstTimeWarningShown()) { showFirstTimeWarning(); } @@ -250,7 +221,7 @@ public class MainActivity extends BaseActivity ItemTouchHelper touchHelper = new ItemTouchHelper(touchHelperCallback); touchHelper.attachToRecyclerView(recList); - float durationScale = Settings.Global.getFloat(this.getContentResolver(), Settings.Global.ANIMATOR_DURATION_SCALE, 0); + float durationScale = android.provider.Settings.Global.getFloat(this.getContentResolver(), android.provider.Settings.Global.ANIMATOR_DURATION_SCALE, 0); final long animatorDuration = (long) (1000 / durationScale); adapter.setCallback(new EntriesCardAdapter.Callback() { @@ -342,14 +313,6 @@ public class MainActivity extends BaseActivity } else if (requestCode == INTENT_INTERNAL_BACKUP && resultCode == RESULT_OK) { if (intent.getBooleanExtra("reload", false)) adapter.loadEntries(); - } else if (requestCode == INTENT_AUTHENTICATE && resultCode != RESULT_OK) { - Toast.makeText(getBaseContext(), R.string.toast_auth_failed, Toast.LENGTH_LONG).show(); - - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { - finishAndRemoveTask(); - } else { - finish(); - } } } @@ -361,12 +324,12 @@ public class MainActivity extends BaseActivity sortMenu = menu.findItem(R.id.menu_sort); if (adapter != null) { - EntriesCardAdapter.SortMode mode = adapter.getSortMode(); + SortMode mode = adapter.getSortMode(); - if (mode == EntriesCardAdapter.SortMode.UNSORTED) { + if (mode == SortMode.UNSORTED) { sortMenu.setIcon(R.drawable.ic_sort_inverted_white); menu.findItem(R.id.menu_sort_none).setChecked(true); - } else if (mode == EntriesCardAdapter.SortMode.LABEL) { + } else if (mode == SortMode.LABEL) { sortMenu.setIcon(R.drawable.ic_sort_inverted_label_white); menu.findItem(R.id.menu_sort_label).setChecked(true); } @@ -400,7 +363,7 @@ public class MainActivity extends BaseActivity @Override public boolean onMenuItemActionCollapse(MenuItem item) { floatingActionMenu.show(); - if (adapter == null || adapter.getSortMode() == EntriesCardAdapter.SortMode.UNSORTED) + if (adapter == null || adapter.getSortMode() == SortMode.UNSORTED) touchHelperCallback.setDragEnabled(true); if (sortMenu != null) sortMenu.setVisible(true); @@ -428,17 +391,17 @@ public class MainActivity extends BaseActivity } else if (id == R.id.menu_sort_none) { item.setChecked(true); sortMenu.setIcon(R.drawable.ic_sort_inverted_white); - saveSortMode(EntriesCardAdapter.SortMode.UNSORTED); + saveSortMode(SortMode.UNSORTED); if (adapter != null) { - adapter.setSortMode(EntriesCardAdapter.SortMode.UNSORTED); + adapter.setSortMode(SortMode.UNSORTED); touchHelperCallback.setDragEnabled(true); } } else if (id == R.id.menu_sort_label) { item.setChecked(true); sortMenu.setIcon(R.drawable.ic_sort_inverted_label_white); - saveSortMode(EntriesCardAdapter.SortMode.LABEL); + saveSortMode(SortMode.LABEL); if (adapter != null) { - adapter.setSortMode(EntriesCardAdapter.SortMode.LABEL); + adapter.setSortMode(SortMode.LABEL); touchHelperCallback.setDragEnabled(false); } } diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/SettingsActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/SettingsActivity.java index 23bbc073..e5b52696 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/SettingsActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/SettingsActivity.java @@ -42,7 +42,7 @@ import org.shadowice.flocke.andotp.R; public class SettingsActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener{ - SettingsFragment fragement; + SettingsFragment fragment; @Override protected void onCreate(Bundle savedInstanceState) { @@ -57,10 +57,10 @@ public class SettingsActivity extends BaseActivity ViewStub stub = (ViewStub) findViewById(R.id.container_stub); stub.inflate(); - fragement = new SettingsFragment(); + fragment = new SettingsFragment(); getFragmentManager().beginTransaction() - .replace(R.id.container_content, fragement) + .replace(R.id.container_content, fragment) .commit(); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); @@ -93,7 +93,7 @@ public class SettingsActivity extends BaseActivity @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); - if (fragement.pgpKey.handleOnActivityResult(requestCode, resultCode, data)) { + if (fragment.pgpKey.handleOnActivityResult(requestCode, resultCode, data)) { // handled by OpenPgpKeyPreference return; } diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java new file mode 100644 index 00000000..fb75c4d9 --- /dev/null +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java @@ -0,0 +1,141 @@ +/* + * Copyright (C) 2017 Jakob Nixdorf + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.shadowice.flocke.andotp.Utilities; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import org.shadowice.flocke.andotp.R; + +public class Settings { + private Context context; + private SharedPreferences settings; + + public enum AuthMethod { + NONE, PASSWORD, PIN, DEVICE + } + + public enum SortMode { + UNSORTED, LABEL + } + + public Settings(Context context) { + this.context = context; + this.settings = PreferenceManager.getDefaultSharedPreferences(context); + } + + private String getResString(int resId) { + return context.getString(resId); + } + + private String getString(int keyId, int defaultId) { + return settings.getString(getResString(keyId), getResString(defaultId)); + } + + private String getString(int keyId, String defaultValue) { + return settings.getString(getResString(keyId), defaultValue); + } + + private boolean getBoolean(int keyId, boolean defaultValue) { + return settings.getBoolean(getResString(keyId), defaultValue); + } + + private long getLong(int keyId, long defaultValue) { + return settings.getLong(getResString(keyId), defaultValue); + } + + private void setBoolean(int keyId, boolean value) { + settings.edit() + .putBoolean(getResString(keyId), value) + .apply(); + } + + private void setString(int keyId, String value) { + settings.edit() + .putString(getResString(keyId), value) + .apply(); + } + + + + public void registerPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) { + settings.registerOnSharedPreferenceChangeListener(listener); + } + + + + public AuthMethod getAuthMethod() { + String authString = getString(R.string.settings_key_auth, R.string.settings_default_auth); + return AuthMethod.valueOf(authString.toUpperCase()); + } + + public String getAuthPassword() { + return getString(R.string.settings_key_auth_password, ""); + } + + public String getAuthPIN() { + return getString(R.string.settings_key_auth_pin, ""); + } + + public String getTheme() { + return getString(R.string.settings_key_theme, R.string.settings_default_theme); + } + + public boolean getFirstTimeWarningShown() { + return getBoolean(R.string.settings_key_security_backup_warning, false); + } + + public void setFirstTimeWarningShown(boolean value) { + setBoolean(R.string.settings_key_security_backup_warning, value); + } + + public SortMode getSortMode() { + String modeStr = getString(R.string.settings_key_sort_mode, SortMode.UNSORTED.toString()); + return SortMode.valueOf(modeStr); + } + + public void setSortMode(SortMode value) { + setString(R.string.settings_key_sort_mode, value.toString()); + } + + public String getBackupPassword() { + return getString(R.string.settings_key_backup_password, ""); + } + + public String getOpenPGPProvider() { + return getString(R.string.settings_key_openpgp_provider, ""); + } + + public long getOpenPGPKey() { + return getLong(R.string.settings_key_openpgp_keyid, 0); + } + + public boolean getOpenPGPSign() { + return getBoolean(R.string.settings_key_openpgp_sign, false); + } + + public boolean getOpenPGPVerify() { + return getBoolean(R.string.settings_key_openpgp_verify, false); + } +} diff --git a/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java b/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java index a8784bc9..aa30341c 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java @@ -47,17 +47,14 @@ import org.shadowice.flocke.andotp.Utilities.DatabaseHelper; import org.shadowice.flocke.andotp.View.ItemTouchHelper.ItemTouchHelperAdapter; import org.shadowice.flocke.andotp.R; +import static org.shadowice.flocke.andotp.Utilities.Settings.SortMode; + import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class EntriesCardAdapter extends RecyclerView.Adapter implements ItemTouchHelperAdapter, Filterable { - - public enum SortMode { - UNSORTED, LABEL - } - private Context context; private SharedPreferences sharedPrefs; private EntryFilter filter;