diff options
Diffstat (limited to 'app')
3 files changed, 109 insertions, 160 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordGeneratorDialogFragment.java b/app/src/main/java/com/zeapo/pwdstore/PasswordGeneratorDialogFragment.java deleted file mode 100644 index 1c5225ae..00000000 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordGeneratorDialogFragment.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright © 2014-2019 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ -package com.zeapo.pwdstore; - -import android.annotation.SuppressLint; -import android.app.Activity; -import android.app.Dialog; -import android.content.Context; -import android.content.SharedPreferences; -import android.graphics.Typeface; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.Button; -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.pwgen.PasswordGenerator; -import java.util.ArrayList; -import org.jetbrains.annotations.NotNull; - -/** A placeholder fragment containing a simple view. */ -public class PasswordGeneratorDialogFragment extends DialogFragment { - - public PasswordGeneratorDialogFragment() {} - - @NotNull - @SuppressLint("SetTextI18n") - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireContext()); - final Activity callingActivity = requireActivity(); - LayoutInflater inflater = callingActivity.getLayoutInflater(); - @SuppressLint("InflateParams") - final View view = inflater.inflate(R.layout.fragment_pwgen, null); - Typeface monoTypeface = - Typeface.createFromAsset(callingActivity.getAssets(), "fonts/sourcecodepro.ttf"); - - builder.setView(view); - - SharedPreferences prefs = - requireActivity() - .getApplicationContext() - .getSharedPreferences("PasswordGenerator", Context.MODE_PRIVATE); - - CheckBox checkBox = view.findViewById(R.id.numerals); - checkBox.setChecked(!prefs.getBoolean("0", false)); - - checkBox = view.findViewById(R.id.symbols); - checkBox.setChecked(prefs.getBoolean("y", false)); - - checkBox = view.findViewById(R.id.uppercase); - checkBox.setChecked(!prefs.getBoolean("A", false)); - - checkBox = view.findViewById(R.id.lowercase); - checkBox.setChecked(!prefs.getBoolean("L", false)); - - checkBox = view.findViewById(R.id.ambiguous); - checkBox.setChecked(!prefs.getBoolean("B", false)); - - checkBox = view.findViewById(R.id.pronounceable); - checkBox.setChecked(!prefs.getBoolean("s", true)); - - AppCompatEditText textView = view.findViewById(R.id.lengthNumber); - textView.setText(Integer.toString(prefs.getInt("length", 20))); - - AppCompatTextView passwordText = view.findViewById(R.id.passwordText); - passwordText.setTypeface(monoTypeface); - - builder.setPositiveButton( - getResources().getString(R.string.dialog_ok), - (dialog, which) -> { - EditText edit = callingActivity.findViewById(R.id.crypto_password_edit); - edit.setText(passwordText.getText()); - }); - - builder.setNegativeButton( - getResources().getString(R.string.dialog_cancel), (dialog, which) -> {}); - - builder.setNeutralButton(getResources().getString(R.string.pwgen_generate), null); - - final AlertDialog ad = - builder.setTitle(this.getResources().getString(R.string.pwgen_title)).create(); - ad.setOnShowListener( - dialog -> { - setPreferences(); - try { - passwordText.setText( - PasswordGenerator.generate( - requireActivity().getApplicationContext()) - .get(0)); - } catch (PasswordGenerator.PasswordGeneratorExeption e) { - Toast.makeText(requireActivity(), e.getMessage(), Toast.LENGTH_SHORT) - .show(); - passwordText.setText(""); - } - - Button b = ad.getButton(AlertDialog.BUTTON_NEUTRAL); - b.setOnClickListener( - v -> { - setPreferences(); - try { - passwordText.setText( - PasswordGenerator.generate( - callingActivity.getApplicationContext()) - .get(0)); - } catch (PasswordGenerator.PasswordGeneratorExeption e) { - Toast.makeText( - requireActivity(), - e.getMessage(), - Toast.LENGTH_SHORT) - .show(); - passwordText.setText(""); - } - }); - }); - return ad; - } - - private void setPreferences() { - ArrayList<String> preferences = new ArrayList<>(); - if (!((CheckBox) getDialog().findViewById(R.id.numerals)).isChecked()) { - preferences.add("0"); - } - if (((CheckBox) getDialog().findViewById(R.id.symbols)).isChecked()) { - preferences.add("y"); - } - if (!((CheckBox) getDialog().findViewById(R.id.uppercase)).isChecked()) { - preferences.add("A"); - } - if (!((CheckBox) getDialog().findViewById(R.id.ambiguous)).isChecked()) { - preferences.add("B"); - } - if (!((CheckBox) getDialog().findViewById(R.id.pronounceable)).isChecked()) { - preferences.add("s"); - } - if (!((CheckBox) getDialog().findViewById(R.id.lowercase)).isChecked()) { - preferences.add("L"); - } - - EditText editText = getDialog().findViewById(R.id.lengthNumber); - try { - int length = Integer.valueOf(editText.getText().toString()); - PasswordGenerator.setPrefs( - requireActivity().getApplicationContext(), preferences, length); - } catch (NumberFormatException e) { - PasswordGenerator.setPrefs(requireActivity().getApplicationContext(), preferences); - } - } -} diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt index 3a27a363..92c78d35 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt @@ -36,9 +36,9 @@ import androidx.preference.PreferenceManager import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.snackbar.Snackbar import com.zeapo.pwdstore.PasswordEntry -import com.zeapo.pwdstore.PasswordGeneratorDialogFragment import com.zeapo.pwdstore.R import com.zeapo.pwdstore.UserPreference +import com.zeapo.pwdstore.ui.dialogs.PasswordGeneratorDialogFragment import com.zeapo.pwdstore.utils.Otp import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream @@ -132,7 +132,8 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { setContentView(R.layout.encrypt_layout) generate_password?.setOnClickListener { - PasswordGeneratorDialogFragment().show(supportFragmentManager, "generator") + PasswordGeneratorDialogFragment() + .show(supportFragmentManager, "generator") } title = getString(R.string.new_password_title) @@ -490,7 +491,8 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { private fun editPassword() { setContentView(R.layout.encrypt_layout) generate_password?.setOnClickListener { - PasswordGeneratorDialogFragment().show(supportFragmentManager, "generator") + PasswordGeneratorDialogFragment() + .show(supportFragmentManager, "generator") } title = getString(R.string.edit_password_title) 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) + } + } +} |