diff options
author | Aditya Wasan <adityawasan55@gmail.com> | 2020-07-01 00:00:02 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-01 00:00:02 +0530 |
commit | 82a9a6125473dbffa6a76f33c6ff83b4a1bd69b3 (patch) | |
tree | 965c887adfb0c5603f6e8f9dbcbd04d4d1011904 /app/src/main/java | |
parent | e11ef1ca1d504bcccf73e75c3782cce2c8fc4bec (diff) |
Use PreferenceKeys file to manage SharedPreferences keys. (#891)
* Use PreferenceKeys file to manage SharedPreferences keys.
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* Use PreferenceKeys in all files
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* Divide PreferenceKeys into multiple regions
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* Fix build error
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* Use PreferenceKeys in more files
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* Reformat code
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* Fix build error
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* Fix merge issues
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
Diffstat (limited to 'app/src/main/java')
25 files changed, 237 insertions, 147 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/Application.kt b/app/src/main/java/com/zeapo/pwdstore/Application.kt index c66c6486..3ccf37fe 100644 --- a/app/src/main/java/com/zeapo/pwdstore/Application.kt +++ b/app/src/main/java/com/zeapo/pwdstore/Application.kt @@ -14,6 +14,7 @@ import androidx.preference.PreferenceManager import com.github.ajalt.timberkt.Timber.DebugTree import com.github.ajalt.timberkt.Timber.plant import com.zeapo.pwdstore.git.config.setUpBouncyCastleForSshj +import com.zeapo.pwdstore.utils.PreferenceKeys @Suppress("Unused") class Application : android.app.Application(), SharedPreferences.OnSharedPreferenceChangeListener { @@ -23,7 +24,8 @@ class Application : android.app.Application(), SharedPreferences.OnSharedPrefere override fun onCreate() { super.onCreate() prefs = PreferenceManager.getDefaultSharedPreferences(this) - if (BuildConfig.ENABLE_DEBUG_FEATURES || prefs?.getBoolean("enable_debug_logging", false) == true) { + if (BuildConfig.ENABLE_DEBUG_FEATURES || prefs?.getBoolean(PreferenceKeys.ENABLE_DEBUG_LOGGING, false) == + true) { plant(DebugTree()) } prefs?.registerOnSharedPreferenceChangeListener(this) @@ -37,13 +39,13 @@ class Application : android.app.Application(), SharedPreferences.OnSharedPrefere } override fun onSharedPreferenceChanged(prefs: SharedPreferences, key: String) { - if (key == "app_theme") { + if (key == PreferenceKeys.APP_THEME) { setNightMode() } } private fun setNightMode() { - AppCompatDelegate.setDefaultNightMode(when (prefs?.getString("app_theme", getString(R.string.app_theme_def))) { + AppCompatDelegate.setDefaultNightMode(when (prefs?.getString(PreferenceKeys.APP_THEME, getString(R.string.app_theme_def))) { "light" -> MODE_NIGHT_NO "dark" -> MODE_NIGHT_YES "follow_system" -> MODE_NIGHT_FOLLOW_SYSTEM diff --git a/app/src/main/java/com/zeapo/pwdstore/ClipboardService.kt b/app/src/main/java/com/zeapo/pwdstore/ClipboardService.kt index ceb84020..ef0cc459 100644 --- a/app/src/main/java/com/zeapo/pwdstore/ClipboardService.kt +++ b/app/src/main/java/com/zeapo/pwdstore/ClipboardService.kt @@ -17,6 +17,7 @@ import androidx.core.app.NotificationCompat import androidx.core.content.getSystemService import androidx.preference.PreferenceManager import com.github.ajalt.timberkt.d +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.clipboard import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -44,7 +45,7 @@ class ClipboardService : Service() { ACTION_START -> { val time = try { - Integer.parseInt(settings.getString("general_show_time", "45") as String) + Integer.parseInt(settings.getString(PreferenceKeys.GENERAL_SHOW_TIME, "45") as String) } catch (e: NumberFormatException) { 45 } @@ -82,7 +83,7 @@ class ClipboardService : Service() { } private fun clearClipboard() { - val deepClear = settings.getBoolean("clear_clipboard_20x", false) + val deepClear = settings.getBoolean(PreferenceKeys.CLEAR_CLIPBOARD_20X, false) val clipboard = clipboard if (clipboard != null) { diff --git a/app/src/main/java/com/zeapo/pwdstore/LaunchActivity.kt b/app/src/main/java/com/zeapo/pwdstore/LaunchActivity.kt index b452f521..e143657e 100644 --- a/app/src/main/java/com/zeapo/pwdstore/LaunchActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/LaunchActivity.kt @@ -12,20 +12,21 @@ import androidx.core.content.edit import androidx.preference.PreferenceManager import com.zeapo.pwdstore.crypto.DecryptActivity import com.zeapo.pwdstore.utils.BiometricAuthenticator +import com.zeapo.pwdstore.utils.PreferenceKeys class LaunchActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val prefs = PreferenceManager.getDefaultSharedPreferences(this) - if (prefs.getBoolean("biometric_auth", false)) { + if (prefs.getBoolean(PreferenceKeys.BIOMETRIC_AUTH, false)) { BiometricAuthenticator.authenticate(this) { when (it) { is BiometricAuthenticator.Result.Success -> { startTargetActivity(false) } is BiometricAuthenticator.Result.HardwareUnavailableOrDisabled -> { - prefs.edit { remove("biometric_auth") } + prefs.edit { remove(PreferenceKeys.BIOMETRIC_AUTH) } startTargetActivity(false) } is BiometricAuthenticator.Result.Failure, BiometricAuthenticator.Result.Cancelled -> { diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt index d811f341..ca53b320 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt @@ -31,6 +31,7 @@ import com.zeapo.pwdstore.ui.adapters.PasswordItemRecyclerAdapter import com.zeapo.pwdstore.ui.dialogs.ItemCreationBottomSheet import com.zeapo.pwdstore.utils.PasswordItem import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.viewBinding import me.zhanghai.android.fastscroll.FastScrollerBuilder import java.io.File @@ -78,7 +79,8 @@ class PasswordFragment : Fragment(R.layout.password_recycler_view) { } else { // When authentication is set to ConnectionMode.None then the only git operation we // can run is a pull, so automatically fallback to that. - val operationId = when (ConnectionMode.fromString(settings.getString("git_remote_auth", null))) { + val operationId = when (ConnectionMode.fromString(settings.getString + (PreferenceKeys.GIT_REMOTE_AUTH, null))) { ConnectionMode.None -> BaseGitActivity.REQUEST_PULL else -> BaseGitActivity.REQUEST_SYNC } diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt index c20be674..ee258b80 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt @@ -65,6 +65,7 @@ import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirect import com.zeapo.pwdstore.utils.PasswordRepository.Companion.initialize import com.zeapo.pwdstore.utils.PasswordRepository.Companion.isInitialized import com.zeapo.pwdstore.utils.PasswordRepository.PasswordSortOrder.Companion.getSortOrder +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.commitChange import com.zeapo.pwdstore.utils.isInsideRepository import com.zeapo.pwdstore.utils.listFilesRecursively @@ -121,7 +122,7 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) { // If user opens app with permission granted then revokes and returns, // prevent attempt to create password list fragment var savedInstance = savedInstanceState - if (savedInstanceState != null && (!settings.getBoolean("git_external", false) || + if (savedInstanceState != null && (!settings.getBoolean(PreferenceKeys.GIT_EXTERNAL, false) || ContextCompat.checkSelfPermission( activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) { @@ -187,12 +188,12 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) { public override fun onResume() { super.onResume() // do not attempt to checkLocalRepository() if no storage permission: immediate crash - if (settings.getBoolean("git_external", false)) { + if (settings.getBoolean(PreferenceKeys.GIT_EXTERNAL, false)) { hasRequiredStoragePermissions(true) } else { checkLocalRepository() } - if (settings.getBoolean("search_on_start", false) && ::searchItem.isInitialized) { + if (settings.getBoolean(PreferenceKeys.SEARCH_ON_START, false) && ::searchItem.isInitialized) { if (!searchItem.isActionViewExpanded) { searchItem.expandActionView() } @@ -211,7 +212,7 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) { override fun onCreateOptionsMenu(menu: Menu?): Boolean { val menuRes = when { - ConnectionMode.fromString(settings.getString("git_remote_auth", null)) + ConnectionMode.fromString(settings.getString(PreferenceKeys.GIT_REMOTE_AUTH, null)) == ConnectionMode.None -> R.menu.main_menu_no_auth PasswordRepository.isGitRepo() -> R.menu.main_menu_git else -> R.menu.main_menu_non_git @@ -261,7 +262,7 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) { return true } }) - if (settings.getBoolean("search_on_start", false)) { + if (settings.getBoolean(PreferenceKeys.SEARCH_ON_START, false)) { searchItem.expandActionView() } return super.onPrepareOptionsMenu(menu) @@ -346,7 +347,7 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) { check(localDir.mkdir()) { "Failed to create directory!" } createRepository(localDir) if (File(localDir.absolutePath + "/.gpg-id").createNewFile()) { - settings.edit { putBoolean("repository_initialized", true) } + settings.edit { putBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, true) } } else { throw IllegalStateException("Failed to initialize repository state.") } @@ -361,8 +362,8 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) { } private fun initializeRepositoryInfo() { - val externalRepo = settings.getBoolean("git_external", false) - val externalRepoPath = settings.getString("git_external_repo", null) + val externalRepo = settings.getBoolean(PreferenceKeys.GIT_EXTERNAL, false) + val externalRepoPath = settings.getString(PreferenceKeys.GIT_EXTERNAL_REPO, null) if (externalRepo && !hasRequiredStoragePermissions()) { return } @@ -375,7 +376,7 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) { return // if not empty, just show me the passwords! } } - val keyIds = settings.getStringSet("openpgp_key_ids_set", HashSet()) + val keyIds = settings.getStringSet(PreferenceKeys.OPENPGP_KEY_IDS_SET, HashSet()) if (keyIds != null && keyIds.isEmpty()) { MaterialAlertDialogBuilder(this) .setMessage(resources.getString(R.string.key_dialog_text)) @@ -431,12 +432,12 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) { } private fun checkLocalRepository(localDir: File?) { - if (localDir != null && settings.getBoolean("repository_initialized", false)) { + if (localDir != null && settings.getBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, false)) { d { "Check, dir: ${localDir.absolutePath}" } // do not push the fragment if we already have it if (supportFragmentManager.findFragmentByTag("PasswordsList") == null || - settings.getBoolean("repo_changed", false)) { - settings.edit { putBoolean("repo_changed", false) } + settings.getBoolean(PreferenceKeys.REPO_CHANGED, false)) { + settings.edit { putBoolean(PreferenceKeys.REPO_CHANGED, false) } plist = PasswordFragment() val args = Bundle() args.putString(REQUEST_ARG_PATH, getRepositoryDirectory(applicationContext).absolutePath) @@ -535,7 +536,7 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) { .show() return false } - if (settings.getStringSet("openpgp_key_ids_set", HashSet()).isNullOrEmpty()) { + if (settings.getStringSet(PreferenceKeys.OPENPGP_KEY_IDS_SET, HashSet()).isNullOrEmpty()) { MaterialAlertDialogBuilder(this) .setTitle(resources.getString(R.string.no_key_selected_dialog_title)) .setMessage(resources.getString(R.string.no_key_selected_dialog_text)) @@ -751,7 +752,7 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) { if (resultCode == Activity.RESULT_OK) { when (requestCode) { // if we get here with a RESULT_OK then it's probably OK :) - BaseGitActivity.REQUEST_CLONE -> settings.edit { putBoolean("repository_initialized", true) } + BaseGitActivity.REQUEST_CLONE -> settings.edit { putBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, true) } // if went from decrypt->edit and user saved changes, we need to commitChange REQUEST_CODE_DECRYPT_AND_VERIFY -> { if (data != null && data.getBooleanExtra("needCommit", false)) { @@ -770,9 +771,9 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) { HOME -> checkLocalRepository() // duplicate code CLONE_REPO_BUTTON -> { - if (settings.getBoolean("git_external", false) && - settings.getString("git_external_repo", null) != null) { - val externalRepoPath = settings.getString("git_external_repo", null) + if (settings.getBoolean(PreferenceKeys.GIT_EXTERNAL, false) && + settings.getString(PreferenceKeys.GIT_EXTERNAL_REPO, null) != null) { + val externalRepoPath = settings.getString(PreferenceKeys.GIT_EXTERNAL_REPO, null) val dir = externalRepoPath?.let { File(it) } if (dir != null && dir.exists() && @@ -832,7 +833,7 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) { .setTitle(resources.getString(R.string.location_dialog_title)) .setMessage(resources.getString(R.string.location_dialog_text)) .setPositiveButton(resources.getString(R.string.location_hidden)) { _, _ -> - settings.edit { putBoolean("git_external", false) } + settings.edit { putBoolean(PreferenceKeys.GIT_EXTERNAL, false) } when (operation) { NEW_REPO_BUTTON -> initializeRepositoryInfo() CLONE_REPO_BUTTON -> { @@ -843,8 +844,8 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) { } } .setNegativeButton(resources.getString(R.string.location_sdcard)) { _, _ -> - settings.edit { putBoolean("git_external", true) } - val externalRepo = settings.getString("git_external_repo", null) + settings.edit { putBoolean(PreferenceKeys.GIT_EXTERNAL, true) } + val externalRepo = settings.getString(PreferenceKeys.GIT_EXTERNAL_REPO, null) if (externalRepo == null) { val intent = Intent(activity, UserPreference::class.java) intent.putExtra("operation", "git_external") diff --git a/app/src/main/java/com/zeapo/pwdstore/SearchableRepositoryViewModel.kt b/app/src/main/java/com/zeapo/pwdstore/SearchableRepositoryViewModel.kt index 05ccf28f..860676e4 100644 --- a/app/src/main/java/com/zeapo/pwdstore/SearchableRepositoryViewModel.kt +++ b/app/src/main/java/com/zeapo/pwdstore/SearchableRepositoryViewModel.kt @@ -30,6 +30,7 @@ import com.zeapo.pwdstore.autofill.oreo.AutofillPreferences import com.zeapo.pwdstore.autofill.oreo.DirectoryStructure import com.zeapo.pwdstore.utils.PasswordItem import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.FlowPreview @@ -140,9 +141,9 @@ class SearchableRepositoryViewModel(application: Application) : AndroidViewModel get() = PasswordRepository.getRepositoryDirectory(getApplication()) private val settings = PreferenceManager.getDefaultSharedPreferences(getApplication()) private val showHiddenDirs - get() = settings.getBoolean("show_hidden_folders", false) + get() = settings.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, false) private val defaultSearchMode - get() = if (settings.getBoolean("filter_recursively", true)) { + get() = if (settings.getBoolean(PreferenceKeys.FILTER_RECURSIVELY, true)) { SearchMode.RecursivelyInSubdirectories } else { SearchMode.InCurrentDirectoryOnly diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt index fb27ba6a..03e314f6 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt @@ -52,6 +52,7 @@ import com.zeapo.pwdstore.sshkeygen.ShowSshKeyFragment import com.zeapo.pwdstore.sshkeygen.SshKeyGenActivity import com.zeapo.pwdstore.utils.BiometricAuthenticator import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.autofillManager import com.zeapo.pwdstore.utils.getEncryptedPrefs import me.msfjarvis.openpgpktx.util.OpenPgpUtils @@ -88,16 +89,16 @@ class UserPreference : AppCompatActivity() { addPreferencesFromResource(R.xml.preference) // Git preferences - val gitServerPreference = findPreference<Preference>("git_server_info") - val openkeystoreIdPreference = findPreference<Preference>("ssh_openkeystore_clear_keyid") - val gitConfigPreference = findPreference<Preference>("git_config") - val sshKeyPreference = findPreference<Preference>("ssh_key") - val sshKeygenPreference = findPreference<Preference>("ssh_keygen") - clearSavedPassPreference = findPreference("clear_saved_pass") - val viewSshKeyPreference = findPreference<Preference>("ssh_see_key") - val deleteRepoPreference = findPreference<Preference>("git_delete_repo") - val externalGitRepositoryPreference = findPreference<Preference>("git_external") - val selectExternalGitRepositoryPreference = findPreference<Preference>("pref_select_external") + val gitServerPreference = findPreference<Preference>(PreferenceKeys.GIT_SERVER_INFO) + val openkeystoreIdPreference = findPreference<Preference>(PreferenceKeys.SSH_OPENKEYSTORE_CLEAR_KEY_ID) + val gitConfigPreference = findPreference<Preference>(PreferenceKeys.GIT_CONFIG) + val sshKeyPreference = findPreference<Preference>(PreferenceKeys.SSH_KEY) + val sshKeygenPreference = findPreference<Preference>(PreferenceKeys.SSH_KEYGEN) + clearSavedPassPreference = findPreference(PreferenceKeys.CLEAR_SAVED_PASS) + val viewSshKeyPreference = findPreference<Preference>(PreferenceKeys.SSH_SEE_KEY) + val deleteRepoPreference = findPreference<Preference>(PreferenceKeys.GIT_DELETE_REPO) + val externalGitRepositoryPreference = findPreference<Preference>(PreferenceKeys.GIT_EXTERNAL) + val selectExternalGitRepositoryPreference = findPreference<Preference>(PreferenceKeys.PREF_SELECT_EXTERNAL) if (!PasswordRepository.isGitRepo()) { listOfNotNull( @@ -109,21 +110,21 @@ class UserPreference : AppCompatActivity() { } // Crypto preferences - val keyPreference = findPreference<Preference>("openpgp_key_id_pref") + val keyPreference = findPreference<Preference>(PreferenceKeys.OPENPGP_KEY_ID_PREF) // General preferences - val showTimePreference = findPreference<Preference>("general_show_time") - val clearClipboard20xPreference = findPreference<CheckBoxPreference>("clear_clipboard_20x") + val showTimePreference = findPreference<Preference>(PreferenceKeys.GENERAL_SHOW_TIME) + val clearClipboard20xPreference = findPreference<CheckBoxPreference>(PreferenceKeys.CLEAR_CLIPBOARD_20X) // Autofill preferences - autoFillEnablePreference = findPreference("autofill_enable") - val oreoAutofillDirectoryStructurePreference = findPreference<ListPreference>("oreo_autofill_directory_structure") - val oreoAutofillDefaultUsername = findPreference<EditTextPreference>("oreo_autofill_default_username") - val oreoAutofillCustomPublixSuffixes = findPreference<EditTextPreference>("oreo_autofill_custom_public_suffixes") - val autoFillAppsPreference = findPreference<Preference>("autofill_apps") - val autoFillDefaultPreference = findPreference<CheckBoxPreference>("autofill_default") - val autoFillAlwaysShowDialogPreference = findPreference<CheckBoxPreference>("autofill_always") - val autoFillShowFullNamePreference = findPreference<CheckBoxPreference>("autofill_full_path") + autoFillEnablePreference = findPreference(PreferenceKeys.AUTOFILL_ENABLE) + val oreoAutofillDirectoryStructurePreference = findPreference<ListPreference>(PreferenceKeys.OREO_AUTOFILL_DIRECTORY_STRUCTURE) + val oreoAutofillDefaultUsername = findPreference<EditTextPreference>(PreferenceKeys.OREO_AUTOFILL_DEFAULT_USERNAME) + val oreoAutofillCustomPublixSuffixes = findPreference<EditTextPreference>(PreferenceKeys.OREO_AUTOFILL_CUSTOM_PUBLIC_SUFFIXES) + val autoFillAppsPreference = findPreference<Preference>(PreferenceKeys.AUTOFILL_APPS) + val autoFillDefaultPreference = findPreference<CheckBoxPreference>(PreferenceKeys.AUTOFILL_DEFAULT) + val autoFillAlwaysShowDialogPreference = findPreference<CheckBoxPreference>(PreferenceKeys.AUTOFILL_ALWAYS) + val autoFillShowFullNamePreference = findPreference<CheckBoxPreference>(PreferenceKeys.AUTOFILL_FULL_PATH) autofillDependencies = listOfNotNull( autoFillAppsPreference, autoFillDefaultPreference, @@ -143,13 +144,13 @@ class UserPreference : AppCompatActivity() { } // Misc preferences - val appVersionPreference = findPreference<Preference>("app_version") + val appVersionPreference = findPreference<Preference>(PreferenceKeys.APP_VERSION) - selectExternalGitRepositoryPreference?.summary = sharedPreferences.getString("git_external_repo", getString(R.string.no_repo_selected)) - viewSshKeyPreference?.isVisible = sharedPreferences.getBoolean("use_generated_key", false) - deleteRepoPreference?.isVisible = !sharedPreferences.getBoolean("git_external", false) - clearClipboard20xPreference?.isVisible = sharedPreferences.getString("general_show_time", "45")?.toInt() != 0 - openkeystoreIdPreference?.isVisible = sharedPreferences.getString("ssh_openkeystore_keyid", null)?.isNotEmpty() + selectExternalGitRepositoryPreference?.summary = sharedPreferences.getString(PreferenceKeys.GIT_EXTERNAL_REPO, getString(R.string.no_repo_selected)) + viewSshKeyPreference?.isVisible = sharedPreferences.getBoolean(PreferenceKeys.USE_GENERATED_KEY, false) + deleteRepoPreference?.isVisible = !sharedPreferences.getBoolean(PreferenceKeys.GIT_EXTERNAL, false) + clearClipboard20xPreference?.isVisible = sharedPreferences.getString(PreferenceKeys.GENERAL_SHOW_TIME, "45")?.toInt() != 0 + openkeystoreIdPreference?.isVisible = sharedPreferences.getString(PreferenceKeys.SSH_OPENKEYSTORE_KEYID, null)?.isNotEmpty() ?: false updateAutofillSettings() @@ -160,7 +161,7 @@ class UserPreference : AppCompatActivity() { keyPreference?.let { pref -> updateKeyIDsSummary(pref) pref.onPreferenceClickListener = ClickListener { - val providerPackageName = requireNotNull(sharedPreferences.getString("openpgp_provider_list", "")) + val providerPackageName = requireNotNull(sharedPreferences.getString(PreferenceKeys.OPENPGP_PROVIDER_LIST, "")) if (providerPackageName.isEmpty()) { Snackbar.make(requireView(), resources.getString(R.string.provider_toast_text), Snackbar.LENGTH_LONG).show() false @@ -193,17 +194,17 @@ class UserPreference : AppCompatActivity() { clearSavedPassPreference?.onPreferenceClickListener = ClickListener { encryptedPreferences.edit { - if (encryptedPreferences.getString("https_password", null) != null) - remove("https_password") - else if (encryptedPreferences.getString("ssh_key_local_passphrase", null) != null) - remove("ssh_key_local_passphrase") + if (encryptedPreferences.getString(PreferenceKeys.HTTPS_PASSWORD, null) != null) + remove(PreferenceKeys.HTTPS_PASSWORD) + else if (encryptedPreferences.getString(PreferenceKeys.SSH_KEY_LOCAL_PASSPHRASE, null) != null) + remove(PreferenceKeys.SSH_KEY_LOCAL_PASSPHRASE) } updateClearSavedPassphrasePrefs() true } openkeystoreIdPreference?.onPreferenceClickListener = ClickListener { - sharedPreferences.edit { putString("ssh_openkeystore_keyid", null) } + sharedPreferences.edit { putString(PreferenceKeys.SSH_OPENKEYSTORE_KEYID, null) } it.isVisible = false true } @@ -237,7 +238,7 @@ class UserPreference : AppCompatActivity() { removeDynamicShortcuts(dynamicShortcuts.map { it.id }.toMutableList()) } } - sharedPreferences.edit { putBoolean("repository_initialized", false) } + sharedPreferences.edit { putBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, false) } dialogInterface.cancel() callingActivity.finish() } @@ -248,7 +249,7 @@ class UserPreference : AppCompatActivity() { } selectExternalGitRepositoryPreference?.summary = - sharedPreferences.getString("git_external_repo", context.getString(R.string.no_repo_selected)) + sharedPreferences.getString(PreferenceKeys.GIT_EXTERNAL_REPO, context.getString(R.string.no_repo_selected)) selectExternalGitRepositoryPreference?.onPreferenceClickListener = ClickListener { callingActivity.selectExternalGitRepository() true @@ -257,7 +258,7 @@ class UserPreference : AppCompatActivity() { val resetRepo = Preference.OnPreferenceChangeListener { _, o -> deleteRepoPreference?.isVisible = !(o as Boolean) PasswordRepository.closeRepository() - sharedPreferences.edit { putBoolean("repo_changed", true) } + sharedPreferences.edit { putBoolean(PreferenceKeys.REPO_CHANGED, true) } true } @@ -275,8 +276,8 @@ class UserPreference : AppCompatActivity() { true } - findPreference<Preference>("export_passwords")?.apply { - isVisible = sharedPreferences.getBoolean("repository_initialized", false) + findPreference<Preference>(PreferenceKeys.EXPORT_PASSWORDS)?.apply { + isVisible = sharedPreferences.getBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, false) onPreferenceClickListener = Preference.OnPreferenceClickListener { callingActivity.exportPasswords() true @@ -294,12 +295,13 @@ class UserPreference : AppCompatActivity() { } showTimePreference?.summaryProvider = Preference.SummaryProvider<Preference> { - getString(R.string.pref_clipboard_timeout_summary, sharedPreferences.getString("general_show_time", "45")) + getString(R.string.pref_clipboard_timeout_summary, sharedPreferences.getString + (PreferenceKeys.GENERAL_SHOW_TIME, "45")) } - findPreference<CheckBoxPreference>("enable_debug_logging")?.isVisible = !BuildConfig.ENABLE_DEBUG_FEATURES + findPreference<CheckBoxPreference>(PreferenceKeys.ENABLE_DEBUG_LOGGING)?.isVisible = !BuildConfig.ENABLE_DEBUG_FEATURES - findPreference<CheckBoxPreference>("biometric_auth")?.apply { + findPreference<CheckBoxPreference>(PreferenceKeys.BIOMETRIC_AUTH)?.apply { val isFingerprintSupported = BiometricManager.from(requireContext()).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS if (!isFingerprintSupported) { isEnabled = false @@ -314,13 +316,13 @@ class UserPreference : AppCompatActivity() { when (result) { is BiometricAuthenticator.Result.Success -> { // Apply the changes - putBoolean("biometric_auth", checked) + putBoolean(PreferenceKeys.BIOMETRIC_AUTH, checked) isEnabled = true } else -> { // If any error occurs, revert back to the previous state. This // catch-all clause includes the cancellation case. - putBoolean("biometric_auth", !checked) + putBoolean(PreferenceKeys.BIOMETRIC_AUTH, !checked) isChecked = !checked isEnabled = true } @@ -337,20 +339,20 @@ class UserPreference : AppCompatActivity() { } } - val prefCustomXkpwdDictionary = findPreference<Preference>("pref_key_custom_dict") + val prefCustomXkpwdDictionary = findPreference<Preference>(PreferenceKeys.PREF_KEY_CUSTOM_DICT) prefCustomXkpwdDictionary?.onPreferenceClickListener = ClickListener { callingActivity.storeCustomDictionaryPath() true } - val dictUri = sharedPreferences.getString("pref_key_custom_dict", "") + val dictUri = sharedPreferences.getString(PreferenceKeys.PREF_KEY_CUSTOM_DICT, "") if (!TextUtils.isEmpty(dictUri)) { setCustomDictSummary(prefCustomXkpwdDictionary, Uri.parse(dictUri)) } - val prefIsCustomDict = findPreference<CheckBoxPreference>("pref_key_is_custom_dict") - val prefCustomDictPicker = findPreference<Preference>("pref_key_custom_dict") - val prefPwgenType = findPreference<ListPreference>("pref_key_pwgen_type") + val prefIsCustomDict = findPreference<CheckBoxPreference>(PreferenceKeys.PREF_KEY_IS_CUSTOM_DICT) + val prefCustomDictPicker = findPreference<Preference>(PreferenceKeys.PREF_KEY_CUSTOM_DICT) + val prefPwgenType = findPreference<ListPreference>(PreferenceKeys.PREF_KEY_PWGEN_TYPE) updateXkPasswdPrefsVisibility(prefPwgenType?.value, prefIsCustomDict, prefCustomDictPicker) prefPwgenType?.onPreferenceChangeListener = ChangeListener { _, newValue -> @@ -371,7 +373,7 @@ class UserPreference : AppCompatActivity() { } private fun updateKeyIDsSummary(preference: Preference) { - val selectedKeys = (sharedPreferences.getStringSet("openpgp_key_ids_set", null) + val selectedKeys = (sharedPreferences.getStringSet(PreferenceKeys.OPENPGP_KEY_IDS_SET, null) ?: HashSet()).toTypedArray() preference.summary = if (selectedKeys.isEmpty()) { resources.getString(R.string.pref_no_key_selected) @@ -410,8 +412,8 @@ class UserPreference : AppCompatActivity() { private fun updateClearSavedPassphrasePrefs() { clearSavedPassPreference?.apply { - val sshPass = encryptedPreferences.getString("ssh_key_local_passphrase", null) - val httpsPass = encryptedPreferences.getString("https_password", null) + val sshPass = encryptedPreferences.getString(PreferenceKeys.SSH_KEY_LOCAL_PASSPHRASE, null) + val httpsPass = encryptedPreferences.getString(PreferenceKeys.HTTPS_PASSWORD, null) if (sshPass == null && httpsPass == null) { isVisible = false return@apply @@ -654,8 +656,8 @@ class UserPreference : AppCompatActivity() { ).show() val prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext) - prefs.edit { putBoolean("use_generated_key", false) } - getEncryptedPrefs("git_operation").edit { remove("ssh_key_local_passphrase") } + prefs.edit { putBoolean(PreferenceKeys.USE_GENERATED_KEY, false) } + getEncryptedPrefs("git_operation").edit { remove(PreferenceKeys.SSH_KEY_LOCAL_PASSPHRASE) } // Delete the public key from generation File("""$filesDir/.ssh_key.pub""").delete() @@ -688,12 +690,12 @@ class UserPreference : AppCompatActivity() { .setTitle(getString(R.string.sdcard_root_warning_title)) .setMessage(getString(R.string.sdcard_root_warning_message)) .setPositiveButton("Remove everything") { _, _ -> - prefs.edit { putString("git_external_repo", uri?.path) } + prefs.edit { putString(PreferenceKeys.GIT_EXTERNAL_REPO, uri?.path) } } .setNegativeButton(R.string.dialog_cancel, null) .show() } - prefs.edit { putString("git_external_repo", repoPath) } + prefs.edit { putString(PreferenceKeys.GIT_EXTERNAL_REPO, repoPath) } } EXPORT_PASSWORDS -> { val uri = data.data @@ -716,9 +718,9 @@ class UserPreference : AppCompatActivity() { ).show() val prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext) - prefs.edit { putString("pref_key_custom_dict", uri.toString()) } + prefs.edit { putString(PreferenceKeys.PREF_KEY_CUSTOM_DICT, uri.toString()) } - val customDictPref = prefsFragment.findPreference<Preference>("pref_key_custom_dict") + val customDictPref = prefsFragment.findPreference<Preference>(PreferenceKeys.PREF_KEY_CUSTOM_DICT) setCustomDictSummary(customDictPref, uri) // copy user selected file to internal storage val inputStream = contentResolver.openInputStream(uri) diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.kt b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.kt index a58bde69..177233a8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.kt +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.kt @@ -31,6 +31,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.zeapo.pwdstore.R import com.zeapo.pwdstore.model.PasswordEntry import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.splitLines import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -228,7 +229,7 @@ class AutofillService : AccessibilityService(), CoroutineScope by CoroutineScope // if autofill_always checked, show dialog even if no matches (automatic // or otherwise) - if (items.isEmpty() && !settings!!.getBoolean("autofill_always", false)) { + if (items.isEmpty() && !settings!!.getBoolean(PreferenceKeys.AUTOFILL_ALWAYS, false)) { return } showSelectPasswordDialog(packageName, appName, isWeb) @@ -268,7 +269,7 @@ class AutofillService : AccessibilityService(), CoroutineScope by CoroutineScope var settingsURL = webViewURL // if autofill_default is checked and prefs.getString DNE, 'Automatically match with password'/"first" otherwise "never" - val defValue = if (settings!!.getBoolean("autofill_default", true)) "/first" else "/never" + val defValue = if (settings!!.getBoolean(PreferenceKeys.AUTOFILL_DEFAULT, true)) "/first" else "/never" val prefs: SharedPreferences = getSharedPreferences("autofill_web", Context.MODE_PRIVATE) var preference: String @@ -305,7 +306,7 @@ class AutofillService : AccessibilityService(), CoroutineScope by CoroutineScope private fun setAppMatchingPasswords(appName: String, packageName: String) { // if autofill_default is checked and prefs.getString DNE, 'Automatically match with password'/"first" otherwise "never" - val defValue = if (settings!!.getBoolean("autofill_default", true)) "/first" else "/never" + val defValue = if (settings!!.getBoolean(PreferenceKeys.AUTOFILL_DEFAULT, true)) "/first" else "/never" val prefs: SharedPreferences = getSharedPreferences("autofill", Context.MODE_PRIVATE) val preference: String? @@ -414,7 +415,7 @@ class AutofillService : AccessibilityService(), CoroutineScope by CoroutineScope // make it optional (or make height a setting for the same effect) val itemNames = arrayOfNulls<CharSequence>(items.size + 2) val passwordDirectory = PasswordRepository.getRepositoryDirectory(applicationContext).toString() - val autofillFullPath = settings!!.getBoolean("autofill_full_path", false) + val autofillFullPath = settings!!.getBoolean(PreferenceKeys.AUTOFILL_FULL_PATH, false) for (i in items.indices) { if (autofillFullPath) { itemNames[i] = items[i].path.replace(".gpg", "") @@ -518,7 +519,7 @@ class AutofillService : AccessibilityService(), CoroutineScope by CoroutineScope // save password entry for pasting the username as well if (entry?.hasUsername() == true) { lastPassword = entry - val ttl = Integer.parseInt(settings!!.getString("general_show_time", "45")!!) + val ttl = Integer.parseInt(settings!!.getString(PreferenceKeys.GENERAL_SHOW_TIME, "45")!!) withContext(Dispatchers.Main) { Toast.makeText(applicationContext, getString(R.string.autofill_toast_username, ttl), Toast.LENGTH_LONG).show() } lastPasswordMaxDate = System.currentTimeMillis() + ttl * 1000L } diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/AutofillHelper.kt b/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/AutofillHelper.kt index 2a8443ba..838b7a05 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/AutofillHelper.kt +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/AutofillHelper.kt @@ -22,6 +22,7 @@ import com.github.ajalt.timberkt.e import com.zeapo.pwdstore.R import com.zeapo.pwdstore.model.PasswordEntry import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import java.io.File import java.security.MessageDigest @@ -39,7 +40,7 @@ private fun ByteArray.base64(): String { private fun Context.getDefaultUsername(): String? { return PreferenceManager .getDefaultSharedPreferences(this) - .getString("oreo_autofill_default_username", null) + .getString(PreferenceKeys.OREO_AUTOFILL_DEFAULT_USERNAME, null) } private fun stableHash(array: Collection<ByteArray>): String { diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/PublicSuffixListCache.kt b/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/PublicSuffixListCache.kt index f1ce6bce..349f0a1b 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/PublicSuffixListCache.kt +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/PublicSuffixListCache.kt @@ -7,6 +7,7 @@ package com.zeapo.pwdstore.autofill.oreo import android.content.Context import android.util.Patterns import androidx.preference.PreferenceManager +import com.zeapo.pwdstore.utils.PreferenceKeys import kotlinx.coroutines.runBlocking import mozilla.components.lib.publicsuffixlist.PublicSuffixList @@ -67,7 +68,7 @@ fun getSuffixPlusUpToOne(domain: String, suffix: String): String? { fun getCustomSuffixes(context: Context): Sequence<String> { val prefs = PreferenceManager.getDefaultSharedPreferences(context) - return prefs.getString("oreo_autofill_custom_public_suffixes", "")!! + return prefs.getString(PreferenceKeys.OREO_AUTOFILL_CUSTOM_PUBLIC_SUFFIXES, "")!! .splitToSequence('\n') .filter { it.isNotBlank() && it.first() != '.' && it.last() != '.' } } diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/BasePgpActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/BasePgpActivity.kt index fcadf119..88ba20f1 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/BasePgpActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/BasePgpActivity.kt @@ -27,6 +27,7 @@ import com.google.android.material.snackbar.Snackbar import com.zeapo.pwdstore.ClipboardService import com.zeapo.pwdstore.R import com.zeapo.pwdstore.UserPreference +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.clipboard import com.zeapo.pwdstore.utils.snackbar import me.msfjarvis.openpgpktx.util.OpenPgpApi @@ -94,7 +95,7 @@ open class BasePgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBou window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE) tag(TAG) - keyIDs = settings.getStringSet("openpgp_key_ids_set", null) ?: emptySet() + keyIDs = settings.getStringSet(PreferenceKeys.OPENPGP_KEY_IDS_SET, null) ?: emptySet() } /** @@ -133,7 +134,7 @@ open class BasePgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBou * [startActivityForResult]. */ fun bindToOpenKeychain(onBoundListener: OpenPgpServiceConnection.OnBound, activityResult: ActivityResultLauncher<Intent>) { - val providerPackageName = settings.getString("openpgp_provider_list", "") + val providerPackageName = settings.getString(PreferenceKeys.OPENPGP_PROVIDER_LIST, "") if (providerPackageName.isNullOrEmpty()) { Toast.makeText(this, resources.getString(R.string.provider_toast_text), Toast.LENGTH_LONG).show() activityResult.launch(Intent(this, UserPreference::class.java)) @@ -215,7 +216,8 @@ open class BasePgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBou var clearAfter = 45 try { - clearAfter = (settings.getString("general_show_time", "45") ?: "45").toInt() + clearAfter = (settings.getString(PreferenceKeys.GENERAL_SHOW_TIME, "45") + ?: "45").toInt() } catch (_: NumberFormatException) { } diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/DecryptActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/DecryptActivity.kt index 52353318..3c31a518 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/DecryptActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/DecryptActivity.kt @@ -21,6 +21,7 @@ import com.zeapo.pwdstore.R import com.zeapo.pwdstore.databinding.DecryptLayoutBinding import com.zeapo.pwdstore.model.PasswordEntry import com.zeapo.pwdstore.utils.Otp +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.viewBinding import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay @@ -148,8 +149,8 @@ class DecryptActivity : BasePgpActivity(), OpenPgpServiceConnection.OnBound { when (result?.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { OpenPgpApi.RESULT_CODE_SUCCESS -> { try { - val showPassword = settings.getBoolean("show_password", true) - val showExtraContent = settings.getBoolean("show_extra_content", true) + val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true) + val showExtraContent = settings.getBoolean(PreferenceKeys.SHOW_EXTRA_CONTENT, true) val monoTypeface = Typeface.createFromAsset(assets, "fonts/sourcecodepro.ttf") val entry = PasswordEntry(outputStream) @@ -211,7 +212,7 @@ class DecryptActivity : BasePgpActivity(), OpenPgpServiceConnection.OnBound { } } - if (settings.getBoolean("copy_on_decrypt", true)) { + if (settings.getBoolean(PreferenceKeys.COPY_ON_DECRYPT, true)) { copyPasswordToClipboard(entry.password) } } catch (e: Exception) { diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/GetKeyIdsActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/GetKeyIdsActivity.kt index 94d5b68c..97f6bab2 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/GetKeyIdsActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/GetKeyIdsActivity.kt @@ -14,6 +14,7 @@ import androidx.core.content.edit import androidx.lifecycle.lifecycleScope import com.github.ajalt.timberkt.Timber import com.github.ajalt.timberkt.e +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.snackbar import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -69,7 +70,7 @@ class GetKeyIdsActivity : BasePgpActivity() { ?: LongArray(0) val keys = ids.map { it.toString() }.toSet() // use Long - settings.edit { putStringSet("openpgp_key_ids_set", keys) } + settings.edit { putStringSet(PreferenceKeys.OPENPGP_KEY_IDS_SET, keys) } snackbar(message = "PGP keys selected") setResult(RESULT_OK) finish() diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt index d1d538ef..13f9add3 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt @@ -27,6 +27,7 @@ import com.zeapo.pwdstore.model.PasswordEntry import com.zeapo.pwdstore.ui.dialogs.PasswordGeneratorDialogFragment import com.zeapo.pwdstore.ui.dialogs.XkPasswordGeneratorDialogFragment import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.commitChange import com.zeapo.pwdstore.utils.isInsideRepository import com.zeapo.pwdstore.utils.snackbar @@ -175,7 +176,7 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB } private fun generatePassword() { - when (settings.getString("pref_key_pwgen_type", KEY_PWGEN_TYPE_CLASSIC)) { + when (settings.getString(PreferenceKeys.PREF_KEY_PWGEN_TYPE, KEY_PWGEN_TYPE_CLASSIC)) { KEY_PWGEN_TYPE_CLASSIC -> PasswordGeneratorDialogFragment() .show(supportFragmentManager, "generator") KEY_PWGEN_TYPE_XKPASSWD -> XkPasswordGeneratorDialogFragment() diff --git a/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt b/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt index c75f7ad3..ae2bbc07 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt @@ -22,6 +22,7 @@ import com.zeapo.pwdstore.git.config.ConnectionMode import com.zeapo.pwdstore.git.config.Protocol import com.zeapo.pwdstore.git.config.SshApiSessionFactory import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.getEncryptedPrefs import java.io.File import java.net.URI @@ -53,14 +54,14 @@ abstract class BaseGitActivity : AppCompatActivity() { settings = PreferenceManager.getDefaultSharedPreferences(this) encryptedSettings = getEncryptedPrefs("git_operation") - protocol = Protocol.fromString(settings.getString("git_remote_protocol", null)) - connectionMode = ConnectionMode.fromString(settings.getString("git_remote_auth", null)) - serverHostname = settings.getString("git_remote_server", null) ?: "" - serverPort = settings.getString("git_remote_port", null) ?: "" - serverUser = settings.getString("git_remote_username", null) ?: "" - serverPath = settings.getString("git_remote_location", null) ?: "" - username = settings.getString("git_config_user_name", null) ?: "" - email = settings.getString("git_config_user_email", null) ?: "" + protocol = Protocol.fromString(settings.getString(PreferenceKeys.GIT_REMOTE_PROTOCOL, null)) + connectionMode = ConnectionMode.fromString(settings.getString(PreferenceKeys.GIT_REMOTE_AUTH, null)) + serverHostname = settings.getString(PreferenceKeys.GIT_REMOTE_SERVER, null) ?: "" + serverPort = settings.getString(PreferenceKeys.GIT_REMOTE_PORT, null) ?: "" + serverUser = settings.getString(PreferenceKeys.GIT_REMOTE_USERNAME, null) ?: "" + serverPath = settings.getString(PreferenceKeys.GIT_REMOTE_LOCATION, null) ?: "" + username = settings.getString(PreferenceKeys.GIT_CONFIG_USER_NAME, null) ?: "" + email = settings.getString(PreferenceKeys.GIT_CONFIG_USER_EMAIL, null) ?: "" updateUrl() } @@ -148,7 +149,7 @@ abstract class BaseGitActivity : AppCompatActivity() { PasswordRepository.addRemote("origin", newUrl, true) // When the server changes, remote password and host key file should be deleted. if (previousUrl.isNotEmpty() && newUrl != previousUrl) { - encryptedSettings.edit { remove("https_password") } + encryptedSettings.edit { remove(PreferenceKeys.HTTPS_PASSWORD) } File("$filesDir/.host_key").delete() } url = newUrl diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt index be911a3a..35cbc68a 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt @@ -14,6 +14,7 @@ import com.google.android.material.snackbar.Snackbar import com.zeapo.pwdstore.R import com.zeapo.pwdstore.databinding.ActivityGitConfigBinding import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.viewBinding import org.eclipse.jgit.lib.Constants @@ -58,8 +59,8 @@ class GitConfigActivity : BaseGitActivity() { .show() } else { settings.edit { - putString("git_config_user_email", email) - putString("git_config_user_name", name) + putString(PreferenceKeys.GIT_CONFIG_USER_EMAIL, email) + putString(PreferenceKeys.GIT_CONFIG_USER_NAME, name) } PasswordRepository.setUserName(name) PasswordRepository.setUserEmail(email) diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt index 33a08fa4..48c28920 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt @@ -23,6 +23,7 @@ import com.zeapo.pwdstore.git.config.SshApiSessionFactory import com.zeapo.pwdstore.git.config.SshAuthData import com.zeapo.pwdstore.git.config.SshjSessionFactory import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.getEncryptedPrefs import com.zeapo.pwdstore.utils.requestInputFocusOnView import net.schmizz.sshj.userauth.password.PasswordFinder @@ -50,7 +51,7 @@ private class GitOperationCredentialFinder(val callingActivity: Activity, val co @StringRes val errorRes: Int when (connectionMode) { ConnectionMode.SshKey -> { - credentialPref = "ssh_key_local_passphrase" + credentialPref = PreferenceKeys.SSH_KEY_LOCAL_PASSPHRASE messageRes = R.string.passphrase_dialog_text hintRes = R.string.ssh_keygen_passphrase rememberRes = R.string.git_operation_remember_passphrase @@ -58,7 +59,7 @@ private class GitOperationCredentialFinder(val callingActivity: Activity, val co } ConnectionMode.Password -> { // Could be either an SSH or an HTTPS password - credentialPref = "https_password" + credentialPref = PreferenceKeys.HTTPS_PASSWORD messageRes = R.string.password_dialog_text hintRes = R.string.git_operation_hint_password rememberRes = R.string.git_operation_remember_password @@ -222,14 +223,14 @@ abstract class GitOperation(gitDir: File, internal val callingActivity: Activity when (SshSessionFactory.getInstance()) { is SshApiSessionFactory -> { PreferenceManager.getDefaultSharedPreferences(callingActivity.applicationContext) - .edit { remove("ssh_openkeystore_keyid") } + .edit { remove(PreferenceKeys.SSH_OPENKEYSTORE_KEYID) } } is SshjSessionFactory -> { callingActivity.applicationContext .getEncryptedPrefs("git_operation") .edit { - remove("ssh_key_local_passphrase") - remove("https_password") + remove(PreferenceKeys.SSH_KEY_LOCAL_PASSPHRASE) + remove(PreferenceKeys.HTTPS_PASSWORD) } } } diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt index 7a3978ba..10f44960 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt @@ -17,6 +17,7 @@ import com.zeapo.pwdstore.databinding.ActivityGitCloneBinding import com.zeapo.pwdstore.git.config.ConnectionMode import com.zeapo.pwdstore.git.config.Protocol import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.viewBinding import java.io.IOException @@ -107,12 +108,12 @@ class GitServerConfigActivity : BaseGitActivity() { when (val result = updateUrl()) { GitUpdateUrlResult.Ok -> { settings.edit { - putString("git_remote_protocol", protocol.pref) - putString("git_remote_auth", connectionMode.pref) - putString("git_remote_server", serverHostname) - putString("git_remote_port", serverPort) - putString("git_remote_username", serverUser) - putString("git_remote_location", serverPath) + putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, protocol.pref) + putString(PreferenceKeys.GIT_REMOTE_AUTH, connectionMode.pref) + putString(PreferenceKeys.GIT_REMOTE_SERVER, serverHostname) + putString(PreferenceKeys.GIT_REMOTE_PORT, serverPort) + putString(PreferenceKeys.GIT_REMOTE_USERNAME, serverUser) + putString(PreferenceKeys.GIT_REMOTE_LOCATION, serverPath) } if (!isClone) { Snackbar.make(binding.root, getString(R.string.git_server_config_save_success), Snackbar.LENGTH_SHORT).show() diff --git a/app/src/main/java/com/zeapo/pwdstore/git/config/SshApiSessionFactory.java b/app/src/main/java/com/zeapo/pwdstore/git/config/SshApiSessionFactory.java index e5a5fd17..5ad12ef8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/config/SshApiSessionFactory.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/config/SshApiSessionFactory.java @@ -21,6 +21,7 @@ import com.jcraft.jsch.Session; import com.jcraft.jsch.UserInfo; import com.zeapo.pwdstore.R; import com.zeapo.pwdstore.git.BaseGitActivity; +import com.zeapo.pwdstore.utils.PreferenceKeys; import org.eclipse.jgit.errors.UnsupportedCredentialItem; import org.eclipse.jgit.transport.CredentialItem; @@ -51,8 +52,8 @@ public class SshApiSessionFactory extends JschConfigSessionFactory { */ public static final int POST_SIGNATURE = 301; - private String username; - private Identity identity; + private final String username; + private final Identity identity; public SshApiSessionFactory(String username, Identity identity) { this.username = username; @@ -108,12 +109,12 @@ public class SshApiSessionFactory extends JschConfigSessionFactory { * build. */ public static class IdentityBuilder { - private SshAuthenticationConnection connection; + private final SshAuthenticationConnection connection; private SshAuthenticationApi api; private String keyId, description, alg; private byte[] publicKey; - private BaseGitActivity callingActivity; - private SharedPreferences settings; + private final BaseGitActivity callingActivity; + private final SharedPreferences settings; /** * Construct a new IdentityBuilder @@ -137,7 +138,7 @@ public class SshApiSessionFactory extends JschConfigSessionFactory { settings = PreferenceManager.getDefaultSharedPreferences( callingActivity.getApplicationContext()); - keyId = settings.getString("ssh_openkeystore_keyid", null); + keyId = settings.getString(PreferenceKeys.SSH_OPENKEYSTORE_KEYID, null); } /** @@ -163,7 +164,7 @@ public class SshApiSessionFactory extends JschConfigSessionFactory { SshAuthenticationApiError error = result.getParcelableExtra(SshAuthenticationApi.EXTRA_ERROR); // On an OpenKeychain SSH API error, clear out the stored keyid - settings.edit().putString("ssh_openkeystore_keyid", null).apply(); + settings.edit().putString(PreferenceKeys.SSH_OPENKEYSTORE_KEYID, null).apply(); switch (error.getError()) { // If the problem was just a bad keyid, reset to allow them to choose a @@ -214,7 +215,7 @@ public class SshApiSessionFactory extends JschConfigSessionFactory { if (intent.hasExtra(SshAuthenticationApi.EXTRA_KEY_ID)) { keyId = intent.getStringExtra(SshAuthenticationApi.EXTRA_KEY_ID); description = intent.getStringExtra(SshAuthenticationApi.EXTRA_KEY_DESCRIPTION); - settings.edit().putString("ssh_openkeystore_keyid", keyId).apply(); + settings.edit().putString(PreferenceKeys.SSH_OPENKEYSTORE_KEYID, keyId).apply(); } if (intent.hasExtra(SshAuthenticationApi.EXTRA_SSH_PUBLIC_KEY)) { @@ -284,10 +285,12 @@ public class SshApiSessionFactory extends JschConfigSessionFactory { * A Jsch identity that delegates key operations via the OpenKeychain SSH API */ public static class ApiIdentity implements Identity { - private String keyId, description, alg; - private byte[] publicKey; - private Activity callingActivity; - private SshAuthenticationApi api; + private final String keyId; + private final String description; + private final String alg; + private final byte[] publicKey; + private final Activity callingActivity; + private final SshAuthenticationApi api; private CountDownLatch latch; private byte[] signature; diff --git a/app/src/main/java/com/zeapo/pwdstore/pwgen/PasswordGenerator.kt b/app/src/main/java/com/zeapo/pwdstore/pwgen/PasswordGenerator.kt index b4accec7..38c82d4a 100644 --- a/app/src/main/java/com/zeapo/pwdstore/pwgen/PasswordGenerator.kt +++ b/app/src/main/java/com/zeapo/pwdstore/pwgen/PasswordGenerator.kt @@ -7,6 +7,7 @@ package com.zeapo.pwdstore.pwgen import android.content.Context import androidx.core.content.edit import com.zeapo.pwdstore.R +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.clearFlag import com.zeapo.pwdstore.utils.hasFlag @@ -102,7 +103,7 @@ object PasswordGenerator { } } - val length = prefs.getInt("length", DEFAULT_LENGTH) + val length = prefs.getInt(PreferenceKeys.LENGTH, DEFAULT_LENGTH) if (pwgenFlags.clearFlag(NO_AMBIGUOUS) == 0) { throw PasswordGeneratorException(ctx.resources.getString(R.string.pwgen_no_chars_error)) } diff --git a/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/XkpwdDictionary.kt b/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/XkpwdDictionary.kt index 92438ed0..37878b70 100644 --- a/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/XkpwdDictionary.kt +++ b/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/XkpwdDictionary.kt @@ -7,6 +7,7 @@ package com.zeapo.pwdstore.pwgenxkpwd import android.content.Context import androidx.preference.PreferenceManager import com.zeapo.pwdstore.R +import com.zeapo.pwdstore.utils.PreferenceKeys import java.io.File class XkpwdDictionary(context: Context) { @@ -14,10 +15,10 @@ class XkpwdDictionary(context: Context) { init { val prefs = PreferenceManager.getDefaultSharedPreferences(context) - val uri = prefs.getString("pref_key_custom_dict", "")!! + val uri = prefs.getString(PreferenceKeys.PREF_KEY_CUSTOM_DICT, "")!! val customDictFile = File(context.filesDir, XKPWD_CUSTOM_DICT_FILE) - val lines = if (prefs.getBoolean("pref_key_is_custom_dict", false) && + val lines = if (prefs.getBoolean(PreferenceKeys.PREF_KEY_IS_CUSTOM_DICT, false) && uri.isNotEmpty() && customDictFile.canRead()) { customDictFile.readLines() } else { diff --git a/app/src/main/java/com/zeapo/pwdstore/ui/adapters/PasswordItemRecyclerAdapter.kt b/app/src/main/java/com/zeapo/pwdstore/ui/adapters/PasswordItemRecyclerAdapter.kt index 91926640..fd96b7a8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/ui/adapters/PasswordItemRecyclerAdapter.kt +++ b/app/src/main/java/com/zeapo/pwdstore/ui/adapters/PasswordItemRecyclerAdapter.kt @@ -18,6 +18,7 @@ import com.zeapo.pwdstore.R import com.zeapo.pwdstore.SearchableRepositoryAdapter import com.zeapo.pwdstore.stableId import com.zeapo.pwdstore.utils.PasswordItem +import com.zeapo.pwdstore.utils.PreferenceKeys import java.io.File open class PasswordItemRecyclerAdapter : @@ -50,7 +51,7 @@ open class PasswordItemRecyclerAdapter : fun bind(item: PasswordItem) { val settings = PreferenceManager.getDefaultSharedPreferences(itemView.context.applicationContext) - val showHidden = settings.getBoolean("show_hidden_folders", false) + val showHidden = settings.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, false) name.text = item.toString() if (item.type == PasswordItem.TYPE_CATEGORY) { typeImage.setImageResource(R.drawable.ic_multiple_files_24dp) diff --git a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/PasswordGeneratorDialogFragment.kt b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/PasswordGeneratorDialogFragment.kt index 160f388b..5528348f 100644 --- a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/PasswordGeneratorDialogFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/PasswordGeneratorDialogFragment.kt @@ -24,6 +24,7 @@ import com.zeapo.pwdstore.pwgen.PasswordGenerator.PasswordGeneratorException import com.zeapo.pwdstore.pwgen.PasswordGenerator.generate import com.zeapo.pwdstore.pwgen.PasswordGenerator.setPrefs import com.zeapo.pwdstore.pwgen.PasswordOption +import com.zeapo.pwdstore.utils.PreferenceKeys class PasswordGeneratorDialogFragment : DialogFragment() { @@ -45,7 +46,7 @@ class PasswordGeneratorDialogFragment : DialogFragment() { view.findViewById<CheckBox>(R.id.pronounceable)?.isChecked = !prefs.getBoolean(PasswordOption.FullyRandom.key, true) val textView: AppCompatEditText = view.findViewById(R.id.lengthNumber) - textView.setText(prefs.getInt("length", 20).toString()) + textView.setText(prefs.getInt(PreferenceKeys.LENGTH, 20).toString()) val passwordText: AppCompatTextView = view.findViewById(R.id.passwordText) passwordText.typeface = monoTypeface return MaterialAlertDialogBuilder(requireContext()).run { diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt index 794630be..65109d99 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt @@ -39,7 +39,8 @@ open class PasswordRepository protected constructor() { companion object { @JvmStatic fun getSortOrder(settings: SharedPreferences): PasswordSortOrder { - return valueOf(settings.getString("sort_order", null) ?: FOLDER_FIRST.name) + return valueOf(settings.getString(PreferenceKeys.SORT_ORDER, null) + ?: FOLDER_FIRST.name) } } } @@ -154,8 +155,8 @@ open class PasswordRepository protected constructor() { if (!::settings.isInitialized) { settings = PreferenceManager.getDefaultSharedPreferences(context.applicationContext) } - return if (settings.getBoolean("git_external", false)) { - val externalRepo = settings.getString("git_external_repo", null) + return if (settings.getBoolean(PreferenceKeys.GIT_EXTERNAL, false)) { + val externalRepo = settings.getString(PreferenceKeys.GIT_EXTERNAL_REPO, null) if (externalRepo != null) File(externalRepo) else @@ -174,9 +175,9 @@ open class PasswordRepository protected constructor() { // uninitialize the repo if the dir does not exist or is absolutely empty settings.edit { if (!dir.exists() || !dir.isDirectory || dir.listFiles()!!.isEmpty()) { - putBoolean("repository_initialized", false) + putBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, false) } else { - putBoolean("repository_initialized", true) + putBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, true) } } @@ -217,7 +218,7 @@ open class PasswordRepository protected constructor() { // We need to recover the passwords then parse the files val passList = getFilesList(path).also { it.sortBy { f -> f.name } } val passwordList = ArrayList<PasswordItem>() - val showHiddenDirs = settings.getBoolean("show_hidden_folders", false) + val showHiddenDirs = settings.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, false) if (passList.size == 0) return passwordList if (showHiddenDirs) { diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PreferenceKeys.kt b/app/src/main/java/com/zeapo/pwdstore/utils/PreferenceKeys.kt new file mode 100644 index 00000000..05f9c741 --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PreferenceKeys.kt @@ -0,0 +1,60 @@ +package com.zeapo.pwdstore.utils + +object PreferenceKeys { + + const val APP_THEME = "app_theme" + const val APP_VERSION = "app_version" + const val AUTOFILL_APPS = "autofill_apps" + const val AUTOFILL_ALWAYS = "autofill_always" + const val AUTOFILL_DEFAULT = "autofill_default" + const val AUTOFILL_ENABLE = "autofill_enable" + const val AUTOFILL_FULL_PATH = "autofill_full_path" + const val BIOMETRIC_AUTH = "biometric_auth" + const val CLEAR_CLIPBOARD_20X = "clear_clipboard_20x" + const val CLEAR_SAVED_PASS = "clear_saved_pass" + const val COPY_ON_DECRYPT = "copy_on_decrypt" + const val ENABLE_DEBUG_LOGGING = "enable_debug_logging" + const val EXPORT_PASSWORDS = "export_passwords" + const val FILTER_RECURSIVELY = "filter_recursively" + const val GENERAL_SHOW_TIME = "general_show_time" + const val GIT_CONFIG = "git_config" + const val GIT_CONFIG_USER_EMAIL = "git_config_user_email" + const val GIT_CONFIG_USER_NAME = "git_config_user_name" + const val GIT_EXTERNAL = "git_external" + const val GIT_EXTERNAL_REPO = "git_external_repo" + const val GIT_REMOTE_AUTH = "git_remote_auth" + const val GIT_REMOTE_LOCATION = "git_remote_location" + const val GIT_REMOTE_PORT = "git_remote_port" + const val GIT_REMOTE_PROTOCOL = "git_remote_protocol" + const val GIT_DELETE_REPO = "git_delete_repo" + const val GIT_REMOTE_SERVER = "git_remote_server" + const val GIT_REMOTE_USERNAME = "git_remote_username" + const val GIT_SERVER_INFO = "git_server_info" + const val HTTPS_PASSWORD = "https_password" + const val LENGTH = "length" + const val OPENPGP_KEY_IDS_SET = "openpgp_key_ids_set" + const val OPENPGP_KEY_ID_PREF = "openpgp_key_id_pref" + const val OPENPGP_PROVIDER_LIST = "openpgp_provider_list" + const val OREO_AUTOFILL_CUSTOM_PUBLIC_SUFFIXES = "oreo_autofill_custom_public_suffixes" + const val OREO_AUTOFILL_DEFAULT_USERNAME = "oreo_autofill_default_username" + const val OREO_AUTOFILL_DIRECTORY_STRUCTURE = "oreo_autofill_directory_structure" + const val PREF_KEY_CUSTOM_DICT = "pref_key_custom_dict" + const val PREF_KEY_IS_CUSTOM_DICT = "pref_key_is_custom_dict" + const val PREF_KEY_PWGEN_TYPE = "pref_key_pwgen_type" + const val PREF_SELECT_EXTERNAL = "pref_select_external" + const val REPOSITORY_INITIALIZED = "repository_initialized" + const val REPO_CHANGED = "repo_changed" + const val SEARCH_ON_START = "search_on_start" + const val SHOW_EXTRA_CONTENT = "show_extra_content" + const val SHOW_HIDDEN_FOLDERS = "show_hidden_folders" + const val SORT_ORDER = "sort_order" + const val SHOW_PASSWORD = "show_password" + const val SSH_KEY = "ssh_key" + const val SSH_KEYGEN = "ssh_keygen" + const val SSH_KEY_LOCAL_PASSPHRASE = "ssh_key_local_passphrase" + const val SSH_OPENKEYSTORE_CLEAR_KEY_ID = "ssh_openkeystore_clear_keyid" + const val SSH_OPENKEYSTORE_KEYID = "ssh_openkeystore_keyid" + const val SSH_SEE_KEY = "ssh_see_key" + const val USE_GENERATED_KEY = "use_generated_key" + +} |