diff options
| author | Harsh Shandilya <msfjarvis@gmail.com> | 2019-11-12 21:55:59 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-11-12 21:55:59 +0530 |
| commit | 5749c97d7c597b01ecfc483ad5d387fc025e4653 (patch) | |
| tree | 6eb792bfa9a112cfd1c0f2bc2bf84743c9d82989 /app/src/main/java/com/zeapo/pwdstore/ui | |
| parent | 4c7f46aa8a2eeffc5a2ad05c9b4cb6d6950323fc (diff) | |
| download | HardenedPass-5749c97d7c597b01ecfc483ad5d387fc025e4653.tar.gz HardenedPass-5749c97d7c597b01ecfc483ad5d387fc025e4653.zip | |
PasswordGeneratorDialogFragment: Rewrite in Kotlin (#569)
Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/ui')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/ui/dialogs/PasswordGeneratorDialogFragment.kt | 104 |
1 files changed, 104 insertions, 0 deletions
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 new file mode 100644 index 00000000..48e92e88 --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/PasswordGeneratorDialogFragment.kt @@ -0,0 +1,104 @@ +/* + * Copyright © 2014-2019 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ +package com.zeapo.pwdstore.ui.dialogs + +import android.app.Activity +import android.app.Dialog +import android.content.Context +import android.graphics.Typeface +import android.os.Bundle +import android.widget.CheckBox +import android.widget.EditText +import android.widget.Toast +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.widget.AppCompatEditText +import androidx.appcompat.widget.AppCompatTextView +import androidx.fragment.app.DialogFragment +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import com.zeapo.pwdstore.R +import com.zeapo.pwdstore.pwgen.PasswordGenerator.PasswordGeneratorExeption +import com.zeapo.pwdstore.pwgen.PasswordGenerator.generate +import com.zeapo.pwdstore.pwgen.PasswordGenerator.setPrefs + +/** A placeholder fragment containing a simple view. */ +class PasswordGeneratorDialogFragment : DialogFragment() { + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val builder = MaterialAlertDialogBuilder(requireContext()) + val callingActivity: Activity = requireActivity() + val inflater = callingActivity.layoutInflater + val view = inflater.inflate(R.layout.fragment_pwgen, null) + val monoTypeface = Typeface.createFromAsset(callingActivity.assets, "fonts/sourcecodepro.ttf") + builder.setView(view) + val prefs = requireActivity().applicationContext + .getSharedPreferences("PasswordGenerator", Context.MODE_PRIVATE) + + view.findViewById<CheckBox>(R.id.numerals)?.isChecked = !prefs.getBoolean("0", false) + view.findViewById<CheckBox>(R.id.symbols)?.isChecked = prefs.getBoolean("y", false) + view.findViewById<CheckBox>(R.id.uppercase)?.isChecked = !prefs.getBoolean("A", false) + view.findViewById<CheckBox>(R.id.lowercase)?.isChecked = !prefs.getBoolean("L", false) + view.findViewById<CheckBox>(R.id.ambiguous)?.isChecked = !prefs.getBoolean("B", false) + view.findViewById<CheckBox>(R.id.pronounceable)?.isChecked = !prefs.getBoolean("s", true) + + val textView: AppCompatEditText = view.findViewById(R.id.lengthNumber) + textView.setText(prefs.getInt("length", 20).toString()) + val passwordText: AppCompatTextView = view.findViewById(R.id.passwordText) + passwordText.typeface = monoTypeface + builder.setPositiveButton(resources.getString(R.string.dialog_ok)) { _, _ -> + val edit = callingActivity.findViewById<EditText>(R.id.crypto_password_edit) + edit.setText(passwordText.text) + } + builder.setNegativeButton(resources.getString(R.string.dialog_cancel)) { _, _ -> } + builder.setNeutralButton(resources.getString(R.string.pwgen_generate), null) + val dialog = builder.setTitle(this.resources.getString(R.string.pwgen_title)).create() + dialog.setOnShowListener { + setPreferences() + try { + passwordText.text = generate(requireActivity().applicationContext)[0] + } catch (e: PasswordGeneratorExeption) { + Toast.makeText(requireActivity(), e.message, Toast.LENGTH_SHORT).show() + passwordText.text = "" + } + dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener { + setPreferences() + try { + passwordText.text = generate(callingActivity.applicationContext)[0] + } catch (e: PasswordGeneratorExeption) { + Toast.makeText(requireActivity(), e.message, Toast.LENGTH_SHORT).show() + passwordText.text = "" + } + } + } + return dialog + } + + private fun setPreferences() { + val preferences = ArrayList<String>() + if (!(dialog!!.findViewById<CheckBox>(R.id.numerals)).isChecked) { + preferences.add("0") + } + if ((dialog!!.findViewById<CheckBox>(R.id.symbols)).isChecked) { + preferences.add("y") + } + if (!(dialog!!.findViewById<CheckBox>(R.id.uppercase)).isChecked) { + preferences.add("A") + } + if (!(dialog!!.findViewById<CheckBox>(R.id.ambiguous)).isChecked) { + preferences.add("B") + } + if (!(dialog!!.findViewById<CheckBox>(R.id.pronounceable)).isChecked) { + preferences.add("s") + } + if (!(dialog!!.findViewById<CheckBox>(R.id.lowercase)).isChecked) { + preferences.add("L") + } + val editText = dialog!!.findViewById<EditText>(R.id.lengthNumber) + try { + val length = Integer.valueOf(editText.text.toString()) + setPrefs(requireActivity().applicationContext, preferences, length) + } catch (e: NumberFormatException) { + setPrefs(requireActivity().applicationContext, preferences) + } + } +} |
