mirror of
https://github.com/android-password-store/Android-Password-Store
synced 2025-08-30 13:57:47 +00:00
PasswordBuilder: use runCatching to replace exception handling
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
@@ -5,12 +5,13 @@
|
|||||||
package com.zeapo.pwdstore.pwgenxkpwd
|
package com.zeapo.pwdstore.pwgenxkpwd
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import com.github.michaelbull.result.Result
|
||||||
|
import com.github.michaelbull.result.runCatching
|
||||||
import com.zeapo.pwdstore.R
|
import com.zeapo.pwdstore.R
|
||||||
import com.zeapo.pwdstore.pwgen.PasswordGenerator.PasswordGeneratorException
|
import com.zeapo.pwdstore.pwgen.PasswordGenerator.PasswordGeneratorException
|
||||||
import com.zeapo.pwdstore.pwgen.secureRandomCharacter
|
import com.zeapo.pwdstore.pwgen.secureRandomCharacter
|
||||||
import com.zeapo.pwdstore.pwgen.secureRandomElement
|
import com.zeapo.pwdstore.pwgen.secureRandomElement
|
||||||
import com.zeapo.pwdstore.pwgen.secureRandomNumber
|
import com.zeapo.pwdstore.pwgen.secureRandomNumber
|
||||||
import java.io.IOException
|
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
class PasswordBuilder(ctx: Context) {
|
class PasswordBuilder(ctx: Context) {
|
||||||
@@ -83,8 +84,7 @@ class PasswordBuilder(ctx: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalStdlibApi::class)
|
@OptIn(ExperimentalStdlibApi::class)
|
||||||
@Throws(PasswordGeneratorException::class)
|
fun create(): Result<String, Throwable> {
|
||||||
fun create(): String {
|
|
||||||
val wordBank = mutableListOf<String>()
|
val wordBank = mutableListOf<String>()
|
||||||
val password = StringBuilder()
|
val password = StringBuilder()
|
||||||
|
|
||||||
@@ -94,7 +94,7 @@ class PasswordBuilder(ctx: Context) {
|
|||||||
password.append(separator)
|
password.append(separator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
return runCatching {
|
||||||
val dictionary = XkpwdDictionary(context)
|
val dictionary = XkpwdDictionary(context)
|
||||||
val words = dictionary.words
|
val words = dictionary.words
|
||||||
for (wordLength in minWordLength..maxWordLength) {
|
for (wordLength in minWordLength..maxWordLength) {
|
||||||
@@ -119,9 +119,6 @@ class PasswordBuilder(ctx: Context) {
|
|||||||
password.append(separator)
|
password.append(separator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: IOException) {
|
|
||||||
throw PasswordGeneratorException("Failed generating password!")
|
|
||||||
}
|
|
||||||
if (numDigits != 0) {
|
if (numDigits != 0) {
|
||||||
if (isAppendNumberSeparator) {
|
if (isAppendNumberSeparator) {
|
||||||
password.append(separator)
|
password.append(separator)
|
||||||
@@ -134,7 +131,8 @@ class PasswordBuilder(ctx: Context) {
|
|||||||
}
|
}
|
||||||
password.append(generateRandomSymbolSequence(numSymbols))
|
password.append(generateRandomSymbolSequence(numSymbols))
|
||||||
}
|
}
|
||||||
return password.toString()
|
password.toString()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@@ -16,12 +16,15 @@ import androidx.appcompat.widget.AppCompatTextView
|
|||||||
import androidx.core.content.edit
|
import androidx.core.content.edit
|
||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
import com.github.ajalt.timberkt.Timber.tag
|
import com.github.ajalt.timberkt.Timber.tag
|
||||||
|
import com.github.michaelbull.result.fold
|
||||||
|
import com.github.michaelbull.result.getOr
|
||||||
|
import com.github.michaelbull.result.runCatching
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import com.zeapo.pwdstore.R
|
import com.zeapo.pwdstore.R
|
||||||
import com.zeapo.pwdstore.databinding.FragmentXkpwgenBinding
|
import com.zeapo.pwdstore.databinding.FragmentXkpwgenBinding
|
||||||
import com.zeapo.pwdstore.pwgen.PasswordGenerator
|
|
||||||
import com.zeapo.pwdstore.pwgenxkpwd.CapsType
|
import com.zeapo.pwdstore.pwgenxkpwd.CapsType
|
||||||
import com.zeapo.pwdstore.pwgenxkpwd.PasswordBuilder
|
import com.zeapo.pwdstore.pwgenxkpwd.PasswordBuilder
|
||||||
|
import com.zeapo.pwdstore.utils.getString
|
||||||
|
|
||||||
/** A placeholder fragment containing a simple view. */
|
/** A placeholder fragment containing a simple view. */
|
||||||
class XkPasswordGeneratorDialogFragment : DialogFragment() {
|
class XkPasswordGeneratorDialogFragment : DialogFragment() {
|
||||||
@@ -41,19 +44,13 @@ class XkPasswordGeneratorDialogFragment : DialogFragment() {
|
|||||||
|
|
||||||
prefs = callingActivity.getSharedPreferences("PasswordGenerator", Context.MODE_PRIVATE)
|
prefs = callingActivity.getSharedPreferences("PasswordGenerator", Context.MODE_PRIVATE)
|
||||||
|
|
||||||
val previousStoredCapStyle: String = try {
|
val previousStoredCapStyle: String = runCatching {
|
||||||
prefs.getString(PREF_KEY_CAPITALS_STYLE, DEFAULT_CAPS_STYLE)!!
|
prefs.getString(PREF_KEY_CAPITALS_STYLE)!!
|
||||||
} catch (e: Exception) {
|
}.getOr(DEFAULT_CAPS_STYLE)
|
||||||
tag("xkpw").e(e)
|
|
||||||
DEFAULT_CAPS_STYLE
|
|
||||||
}
|
|
||||||
|
|
||||||
val lastCapitalsStyleIndex: Int = try {
|
val lastCapitalsStyleIndex: Int = runCatching {
|
||||||
CapsType.valueOf(previousStoredCapStyle).ordinal
|
CapsType.valueOf(previousStoredCapStyle).ordinal
|
||||||
} catch (e: Exception) {
|
}.getOr(DEFAULT_CAPS_INDEX)
|
||||||
tag("xkpw").e(e)
|
|
||||||
DEFAULT_CAPS_INDEX
|
|
||||||
}
|
|
||||||
binding.xkCapType.setSelection(lastCapitalsStyleIndex)
|
binding.xkCapType.setSelection(lastCapitalsStyleIndex)
|
||||||
binding.xkNumWords.setText(prefs.getString(PREF_KEY_NUM_WORDS, DEFAULT_NUMBER_OF_WORDS))
|
binding.xkNumWords.setText(prefs.getString(PREF_KEY_NUM_WORDS, DEFAULT_NUMBER_OF_WORDS))
|
||||||
|
|
||||||
@@ -87,8 +84,7 @@ class XkPasswordGeneratorDialogFragment : DialogFragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun makeAndSetPassword(passwordText: AppCompatTextView) {
|
private fun makeAndSetPassword(passwordText: AppCompatTextView) {
|
||||||
try {
|
PasswordBuilder(requireContext())
|
||||||
passwordText.text = PasswordBuilder(requireContext())
|
|
||||||
.setNumberOfWords(Integer.valueOf(binding.xkNumWords.text.toString()))
|
.setNumberOfWords(Integer.valueOf(binding.xkNumWords.text.toString()))
|
||||||
.setMinimumWordLength(DEFAULT_MIN_WORD_LENGTH)
|
.setMinimumWordLength(DEFAULT_MIN_WORD_LENGTH)
|
||||||
.setMaximumWordLength(DEFAULT_MAX_WORD_LENGTH)
|
.setMaximumWordLength(DEFAULT_MAX_WORD_LENGTH)
|
||||||
@@ -96,11 +92,14 @@ class XkPasswordGeneratorDialogFragment : DialogFragment() {
|
|||||||
.appendNumbers(binding.xkNumberSymbolMask.text!!.count { c -> c == EXTRA_CHAR_PLACEHOLDER_DIGIT })
|
.appendNumbers(binding.xkNumberSymbolMask.text!!.count { c -> c == EXTRA_CHAR_PLACEHOLDER_DIGIT })
|
||||||
.appendSymbols(binding.xkNumberSymbolMask.text!!.count { c -> c == EXTRA_CHAR_PLACEHOLDER_SYMBOL })
|
.appendSymbols(binding.xkNumberSymbolMask.text!!.count { c -> c == EXTRA_CHAR_PLACEHOLDER_SYMBOL })
|
||||||
.setCapitalization(CapsType.valueOf(binding.xkCapType.selectedItem.toString())).create()
|
.setCapitalization(CapsType.valueOf(binding.xkCapType.selectedItem.toString())).create()
|
||||||
} catch (e: PasswordGenerator.PasswordGeneratorException) {
|
.fold(
|
||||||
|
success = { passwordText.text = it },
|
||||||
|
failure = { e ->
|
||||||
Toast.makeText(requireActivity(), e.message, Toast.LENGTH_SHORT).show()
|
Toast.makeText(requireActivity(), e.message, Toast.LENGTH_SHORT).show()
|
||||||
tag("xkpw").e(e, "failure generating xkpasswd")
|
tag("xkpw").e(e, "failure generating xkpasswd")
|
||||||
passwordText.text = FALLBACK_ERROR_PASS
|
passwordText.text = FALLBACK_ERROR_PASS
|
||||||
}
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setPreferences() {
|
private fun setPreferences() {
|
||||||
|
Reference in New Issue
Block a user