diff options
| author | glowinthedark <48893368+glowinthedark@users.noreply.github.com> | 2020-02-29 21:46:31 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-01 02:16:31 +0530 |
| commit | 947e41105b3fdfb7ce9734fb25119c46695f7bba (patch) | |
| tree | 74c63b130b262a06ea068e10ffc20c1dd69d4897 /app/src/main/java/com/zeapo/pwdstore/UserPreference.kt | |
| parent | bea3cd5457e331f04a9aeb5c99267f14908f5eaa (diff) | |
| download | HardenedPass-947e41105b3fdfb7ce9734fb25119c46695f7bba.tar.gz HardenedPass-947e41105b3fdfb7ce9734fb25119c46695f7bba.zip | |
Add xkpasswd-style password generator (#633)
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/UserPreference.kt')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/UserPreference.kt | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt index 50d5bcad..24ed62df 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt @@ -15,6 +15,7 @@ import android.os.Bundle import android.os.Environment import android.provider.DocumentsContract import android.provider.Settings +import android.text.TextUtils import android.view.MenuItem import android.view.accessibility.AccessibilityManager import android.widget.Toast @@ -23,6 +24,7 @@ import androidx.biometric.BiometricManager import androidx.core.content.getSystemService import androidx.documentfile.provider.DocumentFile import androidx.preference.CheckBoxPreference +import androidx.preference.ListPreference import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceManager @@ -32,6 +34,7 @@ import com.google.android.material.snackbar.Snackbar import com.zeapo.pwdstore.autofill.AutofillPreferenceActivity import com.zeapo.pwdstore.crypto.PgpActivity import com.zeapo.pwdstore.git.GitActivity +import com.zeapo.pwdstore.pwgenxkpwd.XkpwdDictionary import com.zeapo.pwdstore.sshkeygen.ShowSshKeyFragment import com.zeapo.pwdstore.sshkeygen.SshKeyGenActivity import com.zeapo.pwdstore.utils.PasswordRepository @@ -312,6 +315,51 @@ class UserPreference : AppCompatActivity() { } } } + + val prefCustomXkpwdDictionary = findPreference<Preference>("pref_key_custom_dict") + prefCustomXkpwdDictionary?.onPreferenceClickListener = ClickListener { + callingActivity.storeCustomDictionaryPath() + true + } + val dictUri = sharedPreferences.getString("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") + showHideDependentPrefs(prefPwgenType?.value, prefIsCustomDict, prefCustomDictPicker) + + prefPwgenType?.onPreferenceChangeListener = ChangeListener() { _, newValue -> + showHideDependentPrefs(newValue, prefIsCustomDict, prefCustomDictPicker) + true + } + + prefIsCustomDict?.onPreferenceChangeListener = ChangeListener() { _, newValue -> + if (!(newValue as Boolean)) { + val customDictFile = File(context.filesDir, XkpwdDictionary.XKPWD_CUSTOM_DICT_FILE) + if (customDictFile.exists()) { + FileUtils.deleteQuietly(customDictFile) + } + prefCustomDictPicker?.setSummary(R.string.xkpwgen_pref_custom_dict_picker_summary) + } + true + } + } + + private fun showHideDependentPrefs(newValue: Any?, prefIsCustomDict: CheckBoxPreference?, prefCustomDictPicker: Preference?) { + when (newValue as String) { + PgpActivity.KEY_PWGEN_TYPE_CLASSIC -> { + prefIsCustomDict?.isVisible = false + prefCustomDictPicker?.isVisible = false + } + PgpActivity.KEY_PWGEN_TYPE_XKPASSWD -> { + prefIsCustomDict?.isVisible = true + prefCustomDictPicker?.isVisible = true + } + } } override fun onResume() { @@ -396,6 +444,17 @@ class UserPreference : AppCompatActivity() { } } + /** + * Pick custom xkpwd dictionary from sdcard + */ + private fun storeCustomDictionaryPath() { + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply { + addCategory(Intent.CATEGORY_OPENABLE) + type = "*/*" + } + startActivityForResult(intent, SET_CUSTOM_XKPWD_DICT) + } + @Throws(IOException::class) private fun copySshKey(uri: Uri) { // TODO: Check if valid SSH Key before import @@ -516,6 +575,27 @@ class UserPreference : AppCompatActivity() { } } } + SET_CUSTOM_XKPWD_DICT -> { + val uri: Uri = data.data ?: throw IOException("Unable to open file") + + Toast.makeText( + this, + this.resources.getString(R.string.xkpwgen_custom_dict_imported, uri.path), + Toast.LENGTH_SHORT + ).show() + val prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext) + + prefs.edit().putString("pref_key_custom_dict", uri.toString()).apply() + + val customDictPref = prefsFragment.findPreference<Preference>("pref_key_custom_dict") + setCustomDictSummary(customDictPref, uri) + // copy user selected file to internal storage + val inputStream = this.contentResolver.openInputStream(uri) + val customDictFile = File(this.filesDir.toString(), XkpwdDictionary.XKPWD_CUSTOM_DICT_FILE) + FileUtils.copyInputStreamToFile(inputStream, customDictFile) + + setResult(Activity.RESULT_OK) + } } } super.onActivityResult(requestCode, resultCode, data) @@ -599,6 +679,16 @@ class UserPreference : AppCompatActivity() { private const val SELECT_GIT_DIRECTORY = 4 private const val EXPORT_PASSWORDS = 5 private const val EDIT_GIT_CONFIG = 6 + private const val SET_CUSTOM_XKPWD_DICT = 7 private const val TAG = "UserPreference" + + /** + * Set custom dictionary summary + */ + @JvmStatic + private fun setCustomDictSummary(customDictPref: Preference?, uri: Uri) { + val fileName = uri.path?.substring(uri.path?.lastIndexOf(":")!! + 1) + customDictPref?.setSummary("Selected dictionary: " + fileName) + } } } |
