diff options
Diffstat (limited to 'app/src/main/java')
6 files changed, 103 insertions, 132 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt index a9629c6c..67a9bc2d 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt @@ -101,7 +101,6 @@ class UserPreference : AppCompatActivity() { // General preferences val showTimePreference = findPreference<Preference>("general_show_time") - val clearAfterCopyPreference = findPreference<CheckBoxPreference>("clear_after_copy") val clearClipboard20xPreference = findPreference<CheckBoxPreference>("clear_clipboard_20x") // Autofill preferences @@ -129,7 +128,6 @@ class UserPreference : AppCompatActivity() { viewSshKeyPreference?.isVisible = sharedPreferences.getBoolean("use_generated_key", false) deleteRepoPreference?.isVisible = !sharedPreferences.getBoolean("git_external", false) clearHotpIncrementPreference?.isVisible = sharedPreferences.getBoolean("hotp_remember_check", false) - clearAfterCopyPreference?.isVisible = sharedPreferences.getString("general_show_time", "45")?.toInt() != 0 clearClipboard20xPreference?.isVisible = sharedPreferences.getString("general_show_time", "45")?.toInt() != 0 val selectedKeys = (sharedPreferences.getStringSet("openpgp_key_ids_set", null) ?: HashSet()).toTypedArray() @@ -273,7 +271,6 @@ class UserPreference : AppCompatActivity() { showTimePreference?.onPreferenceChangeListener = ChangeListener { _, newValue: Any? -> try { val isEnabled = newValue.toString().toInt() != 0 - clearAfterCopyPreference?.isVisible = isEnabled clearClipboard20xPreference?.isVisible = isEnabled true } catch (e: NumberFormatException) { @@ -282,10 +279,10 @@ class UserPreference : AppCompatActivity() { } showTimePreference?.summaryProvider = Preference.SummaryProvider<Preference> { - getString(R.string.pref_show_time_summary, sharedPreferences.getString("general_show_time", "45")) + getString(R.string.pref_clipboard_timeout_summary, sharedPreferences.getString("general_show_time", "45")) } - findPreference<SwitchPreferenceCompat>("biometric_auth")?.apply { + findPreference<CheckBoxPreference>("biometric_auth")?.apply { val isFingerprintSupported = BiometricManager.from(requireContext()).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS if (!isFingerprintSupported) { isEnabled = false @@ -337,10 +334,10 @@ class UserPreference : AppCompatActivity() { 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) + updateXkPasswdPrefsVisibility(prefPwgenType?.value, prefIsCustomDict, prefCustomDictPicker) prefPwgenType?.onPreferenceChangeListener = ChangeListener { _, newValue -> - showHideDependentPrefs(newValue, prefIsCustomDict, prefCustomDictPicker) + updateXkPasswdPrefsVisibility(newValue, prefIsCustomDict, prefCustomDictPicker) true } @@ -356,7 +353,7 @@ class UserPreference : AppCompatActivity() { } } - private fun showHideDependentPrefs(newValue: Any?, prefIsCustomDict: CheckBoxPreference?, prefCustomDictPicker: Preference?) { + private fun updateXkPasswdPrefsVisibility(newValue: Any?, prefIsCustomDict: CheckBoxPreference?, prefCustomDictPicker: Preference?) { when (newValue as String) { PgpActivity.KEY_PWGEN_TYPE_CLASSIC -> { prefIsCustomDict?.isVisible = false 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 f6d2b169..c73adc9d 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt @@ -831,7 +831,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { // ignore and keep default } - if (settings.getBoolean("clear_after_copy", true) && clearAfter != 0) { + if (clearAfter != 0) { setTimer() showSnackbar(this.resources.getString(R.string.clipboard_password_toast_text, clearAfter)) } else { 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 8ad19618..dbf67d5f 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt @@ -5,9 +5,12 @@ package com.zeapo.pwdstore.git import android.os.Bundle +import android.os.Handler import android.util.Patterns import androidx.core.content.edit +import androidx.core.os.postDelayed import com.google.android.material.dialog.MaterialAlertDialogBuilder +import com.google.android.material.snackbar.Snackbar import com.zeapo.pwdstore.R import com.zeapo.pwdstore.databinding.ActivityGitConfigBinding import com.zeapo.pwdstore.utils.PasswordRepository @@ -23,7 +26,10 @@ class GitConfigActivity : BaseGitActivity() { setContentView(binding.root) supportActionBar?.setDisplayHomeAsUpEnabled(true) - binding.gitUserName.setText(username) + if (username.isEmpty()) + binding.gitUserName.requestFocus() + else + binding.gitUserName.setText(username) binding.gitUserEmail.setText(email) val repo = PasswordRepository.getRepository(PasswordRepository.getRepositoryDirectory(this)) if (repo != null) { @@ -55,8 +61,10 @@ class GitConfigActivity : BaseGitActivity() { putString("git_config_user_email", email) putString("git_config_user_name", name) } - PasswordRepository.setUserName(name) - PasswordRepository.setUserEmail(email) + PasswordRepository.setUserName(name) + PasswordRepository.setUserEmail(email) + Snackbar.make(binding.root, getString(R.string.git_server_config_save_success), Snackbar.LENGTH_SHORT).show() + Handler().postDelayed(500) { finish() } } } } diff --git a/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeyGenActivity.kt b/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeyGenActivity.kt index 7fad3c52..8885b038 100644 --- a/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeyGenActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeyGenActivity.kt @@ -13,7 +13,6 @@ class SshKeyGenActivity : AppCompatActivity() { public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) supportActionBar?.setDisplayHomeAsUpEnabled(true) - title = "Generate SSH Key" if (savedInstanceState == null) { supportFragmentManager .beginTransaction() diff --git a/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeyGenFragment.kt b/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeyGenFragment.kt index e07f71cd..da7099ad 100644 --- a/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeyGenFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeyGenFragment.kt @@ -10,81 +10,124 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.view.inputmethod.InputMethodManager -import android.widget.ArrayAdapter -import android.widget.Button -import android.widget.CheckBox -import android.widget.EditText -import android.widget.Spinner +import androidx.core.content.edit import androidx.core.content.getSystemService import androidx.fragment.app.Fragment -import com.google.android.material.textfield.TextInputEditText +import androidx.lifecycle.lifecycleScope +import androidx.preference.PreferenceManager +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import com.jcraft.jsch.JSch +import com.jcraft.jsch.KeyPair import com.zeapo.pwdstore.R +import com.zeapo.pwdstore.databinding.FragmentSshKeygenBinding +import java.io.File +import java.io.FileOutputStream +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class SshKeyGenFragment : Fragment() { - private lateinit var checkBox: CheckBox - private lateinit var comment: EditText - private lateinit var generate: Button - private lateinit var passphrase: TextInputEditText - private lateinit var spinner: Spinner - private lateinit var activity: SshKeyGenActivity + private var keyLength = 4096 + private var _binding: FragmentSshKeygenBinding? = null + private val binding get() = _binding!! override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { - return inflater.inflate(R.layout.fragment_ssh_keygen, container, false) + _binding = FragmentSshKeygenBinding.inflate(inflater, container, false) + return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - activity = requireActivity() as SshKeyGenActivity - findViews(view) - val lengths = arrayOf(2048, 4096) - val adapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, lengths) - spinner.adapter = adapter - generate.setOnClickListener { generate() } - checkBox.setOnCheckedChangeListener { _, isChecked: Boolean -> - val selection = passphrase.selectionEnd - if (isChecked) { - passphrase.inputType = ( - InputType.TYPE_CLASS_TEXT - or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) - } else { - passphrase.inputType = ( - InputType.TYPE_CLASS_TEXT - or InputType.TYPE_TEXT_VARIATION_PASSWORD) + with(binding) { + generate.setOnClickListener { + lifecycleScope.launch { generate(passphrase.text.toString(), comment.text.toString()) } + } + showPassphrase.setOnCheckedChangeListener { _, isChecked: Boolean -> + val selection = passphrase.selectionEnd + if (isChecked) { + passphrase.inputType = ( + InputType.TYPE_CLASS_TEXT + or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) + } else { + passphrase.inputType = ( + InputType.TYPE_CLASS_TEXT + or InputType.TYPE_TEXT_VARIATION_PASSWORD) + } + passphrase.setSelection(selection) + } + keyLengthGroup.check(R.id.key_length_4096) + keyLengthGroup.addOnButtonCheckedListener { _, checkedId, isChecked -> + if (isChecked) { + when (checkedId) { + R.id.key_length_2048 -> keyLength = 2048 + R.id.key_length_4096 -> keyLength = 4096 + } + } } - passphrase.setSelection(selection) } } - private fun findViews(view: View) { - checkBox = view.findViewById(R.id.show_passphrase) - comment = view.findViewById(R.id.comment) - generate = view.findViewById(R.id.generate) - passphrase = view.findViewById(R.id.passphrase) - spinner = view.findViewById(R.id.length) + override fun onDestroyView() { + super.onDestroyView() + _binding = null } // Invoked when 'Generate' button of SshKeyGenFragment clicked. Generates a // private and public key, then replaces the SshKeyGenFragment with a // ShowSshKeyFragment which displays the public key. - fun generate() { - val length = (spinner.selectedItem as Int).toString() - val passphrase = passphrase.text.toString() - val comment = comment.text.toString() - KeyGenerateTask(activity).execute(length, passphrase, comment) + private suspend fun generate(passphrase: String, comment: String) { + binding.generate.text = getString(R.string.ssh_key_gen_generating_progress) + val jsch = JSch() + val e = try { + withContext(Dispatchers.IO) { + val kp = KeyPair.genKeyPair(jsch, KeyPair.RSA, keyLength) + var file = File(requireActivity().filesDir, ".ssh_key") + var out = FileOutputStream(file, false) + if (passphrase.isNotEmpty()) { + kp?.writePrivateKey(out, passphrase.toByteArray()) + } else { + kp?.writePrivateKey(out) + } + file = File(requireActivity().filesDir, ".ssh_key.pub") + out = FileOutputStream(file, false) + kp?.writePublicKey(out, comment) + } + null + } catch (e: Exception) { + e.printStackTrace() + e + } + val activity = requireActivity() + binding.generate.text = getString(R.string.ssh_keygen_generating_done) + if (e == null) { + val df = ShowSshKeyFragment() + df.show(requireActivity().supportFragmentManager, "public_key") + val prefs = PreferenceManager.getDefaultSharedPreferences(activity) + prefs.edit { putBoolean("use_generated_key", true) } + } else { + MaterialAlertDialogBuilder(activity) + .setTitle(activity.getString(R.string.error_generate_ssh_key)) + .setMessage(activity.getString(R.string.ssh_key_error_dialog_text) + e.message) + .setPositiveButton(activity.getString(R.string.dialog_ok)) { _, _ -> + requireActivity().finish() + } + .show() + } hideKeyboard() } private fun hideKeyboard() { - val imm = activity.getSystemService<InputMethodManager>() + val activity = activity ?: return + val imm = activity.getSystemService<InputMethodManager>() ?: return var view = activity.currentFocus if (view == null) { view = View(activity) } - imm?.hideSoftInputFromWindow(view.windowToken, 0) + imm.hideSoftInputFromWindow(view.windowToken, 0) } } diff --git a/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeygenTask.kt b/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeygenTask.kt deleted file mode 100644 index 169a1309..00000000 --- a/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeygenTask.kt +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ -package com.zeapo.pwdstore.sshkeygen - -import android.app.ProgressDialog -import android.os.AsyncTask -import android.widget.Toast -import androidx.appcompat.app.AppCompatActivity -import androidx.core.content.edit -import androidx.fragment.app.DialogFragment -import androidx.preference.PreferenceManager -import com.google.android.material.dialog.MaterialAlertDialogBuilder -import com.jcraft.jsch.JSch -import com.jcraft.jsch.KeyPair -import com.zeapo.pwdstore.R -import java.io.File -import java.io.FileOutputStream -import java.lang.ref.WeakReference - -class KeyGenerateTask(activity: AppCompatActivity) : AsyncTask<String?, Void?, Exception?>() { - private var pd: ProgressDialog? = null - private val weakReference = WeakReference(activity) - override fun onPreExecute() { - super.onPreExecute() - pd = ProgressDialog.show(weakReference.get(), "", "Generating keys") - } - - override fun doInBackground(vararg strings: String?): Exception? { - val length = strings[0]?.toInt() - val passphrase = strings[1] - val comment = strings[2] - val jsch = JSch() - try { - val kp = length?.let { KeyPair.genKeyPair(jsch, KeyPair.RSA, it) } - var file = File(weakReference.get()!!.filesDir.toString() + "/.ssh_key") - var out = FileOutputStream(file, false) - if (passphrase?.isNotEmpty()!!) { - kp?.writePrivateKey(out, passphrase.toByteArray()) - } else { - kp?.writePrivateKey(out) - } - file = File(weakReference.get()!!.filesDir.toString() + "/.ssh_key.pub") - out = FileOutputStream(file, false) - kp?.writePublicKey(out, comment) - return null - } catch (e: Exception) { - e.printStackTrace() - return e - } - } - - override fun onPostExecute(e: Exception?) { - super.onPostExecute(e) - val activity = weakReference.get() - if (activity is AppCompatActivity) { - pd!!.dismiss() - if (e == null) { - Toast.makeText(activity, "SSH-key generated", Toast.LENGTH_LONG).show() - val df: DialogFragment = ShowSshKeyFragment() - df.show(activity.supportFragmentManager, "public_key") - val prefs = PreferenceManager.getDefaultSharedPreferences(weakReference.get()) - prefs.edit { putBoolean("use_generated_key", true) } - } else { - MaterialAlertDialogBuilder(activity) - .setTitle(activity.getString(R.string.error_generate_ssh_key)) - .setMessage(activity.getString(R.string.ssh_key_error_dialog_text) + e.message) - .setPositiveButton(activity.getString(R.string.dialog_ok), null) - .show() - } - } else { - // TODO: When activity is destroyed - } - } -} |