From cbb96397d1f5cffef7984b08a426646aaea93e5b Mon Sep 17 00:00:00 2001 From: Fabian Henneke Date: Tue, 1 Sep 2020 10:12:27 +0200 Subject: Add Keystore backend for SSH public key authentication (#1070) --- .../zeapo/pwdstore/sshkeygen/ShowSshKeyFragment.kt | 62 +++++++--------------- 1 file changed, 18 insertions(+), 44 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/sshkeygen/ShowSshKeyFragment.kt') diff --git a/app/src/main/java/com/zeapo/pwdstore/sshkeygen/ShowSshKeyFragment.kt b/app/src/main/java/com/zeapo/pwdstore/sshkeygen/ShowSshKeyFragment.kt index 109ebd01..bfa7e1c8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/sshkeygen/ShowSshKeyFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/sshkeygen/ShowSshKeyFragment.kt @@ -4,61 +4,35 @@ */ package com.zeapo.pwdstore.sshkeygen -import android.annotation.SuppressLint import android.app.Dialog -import android.content.ClipData +import android.content.Intent import android.os.Bundle -import android.view.View -import android.widget.TextView -import androidx.appcompat.app.AlertDialog import androidx.fragment.app.DialogFragment import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.zeapo.pwdstore.R -import com.zeapo.pwdstore.utils.clipboard -import java.io.File +import com.zeapo.pwdstore.git.sshj.SshKey class ShowSshKeyFragment : DialogFragment() { - private lateinit var builder: MaterialAlertDialogBuilder - private lateinit var publicKey: TextView - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - builder = MaterialAlertDialogBuilder(requireActivity()) - } - - @SuppressLint("InflateParams") override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val activity = requireActivity() - val view = activity.layoutInflater.inflate(R.layout.fragment_show_ssh_key, null) - publicKey = view.findViewById(R.id.public_key) - readKeyFromFile() - createMaterialDialog(view) - val ad = builder.create() - ad.setOnShowListener { - val b = ad.getButton(AlertDialog.BUTTON_POSITIVE) - b.setOnClickListener { - val clipboard = activity.clipboard ?: return@setOnClickListener - val clip = ClipData.newPlainText("public key", publicKey.text.toString()) - clipboard.setPrimaryClip(clip) + val publicKey = SshKey.sshPublicKey + return MaterialAlertDialogBuilder(requireActivity()).run { + setMessage(getString(R.string.ssh_keygen_message, publicKey)) + setTitle(R.string.your_public_key) + setNegativeButton(R.string.ssh_keygen_later) { _, _ -> + (activity as? SshKeyGenActivity)?.finish() } - } - return ad - } - - private fun createMaterialDialog(view: View) { - builder.setView(view) - builder.setTitle(getString(R.string.your_public_key)) - builder.setNegativeButton(R.string.dialog_ok) { _, _ -> requireActivity().finish() } - builder.setPositiveButton(R.string.ssh_keygen_copy, null) - } - - private fun readKeyFromFile() { - val file = File(requireActivity().filesDir.toString() + "/.ssh_key.pub") - try { - publicKey.text = file.readText() - } catch (e: Exception) { - e.printStackTrace() + setPositiveButton(R.string.ssh_keygen_share) { _, _ -> + val sendIntent = Intent().apply { + action = Intent.ACTION_SEND + type = "text/plain" + putExtra(Intent.EXTRA_TEXT, publicKey) + } + startActivity(Intent.createChooser(sendIntent, null)) + (activity as? SshKeyGenActivity)?.finish() + } + create() } } } -- cgit v1.2.3