From f8dcaec31f5c1c3bfaba8625dd4dcccd53b37337 Mon Sep 17 00:00:00 2001 From: DenBond7 Date: Tue, 2 Apr 2024 10:21:46 +0300 Subject: [PATCH 01/14] Added ChoosePrivateKeyDialogFragment.| #597 --- .../jetpack/viewmodel/PrivateKeysViewModel.kt | 2 + .../com/flowcrypt/email/model/KeysStorage.kt | 3 + .../email/security/KeysStorageImpl.kt | 3 + .../fragment/CreateMessageFragment.kt | 69 ++++++-- .../fragment/RecipientDetailsFragment.kt | 2 +- .../fragment/base/ListProgressBehaviour.kt | 6 +- .../dialog/ChoosePrivateKeyDialogFragment.kt | 150 ++++++++++++++++++ .../ui/adapter/PrivateKeysArrayAdapter.kt | 75 +++++++++ .../layout/fragment_choose_private_key.xml | 84 ++++++++++ .../res/layout/private_key_item_checkbox.xml | 51 ++++++ .../layout/private_key_item_radio_button.xml | 51 ++++++ .../choose_private_key_dialog_graph.xml | 37 +++++ .../main/res/navigation/create_msg_graph.xml | 1 + FlowCrypt/src/main/res/values-ru/strings.xml | 1 + FlowCrypt/src/main/res/values-uk/strings.xml | 1 + FlowCrypt/src/main/res/values/strings.xml | 1 + 16 files changed, 516 insertions(+), 21 deletions(-) create mode 100644 FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/dialog/ChoosePrivateKeyDialogFragment.kt create mode 100644 FlowCrypt/src/main/java/com/flowcrypt/email/ui/adapter/PrivateKeysArrayAdapter.kt create mode 100644 FlowCrypt/src/main/res/layout/fragment_choose_private_key.xml create mode 100644 FlowCrypt/src/main/res/layout/private_key_item_checkbox.xml create mode 100644 FlowCrypt/src/main/res/layout/private_key_item_radio_button.xml create mode 100644 FlowCrypt/src/main/res/navigation/choose_private_key_dialog_graph.xml diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/PrivateKeysViewModel.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/PrivateKeysViewModel.kt index abdc3a5a86..9b9a0e9854 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/PrivateKeysViewModel.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/PrivateKeysViewModel.kt @@ -100,6 +100,8 @@ class PrivateKeysViewModel(application: Application) : AccountViewModel(applicat } } + fun getActiveAccount(): AccountEntity? = keysStorage.getActiveAccount() + @ExperimentalCoroutinesApi val secretKeyRingsInfoStateFlow: StateFlow?>> = keysStorage.secretKeyRingsLiveData.asFlow().flatMapLatest { diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/model/KeysStorage.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/model/KeysStorage.kt index 1a10df4495..9c4dc4264b 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/model/KeysStorage.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/model/KeysStorage.kt @@ -6,6 +6,7 @@ package com.flowcrypt.email.model import androidx.annotation.Keep +import com.flowcrypt.email.database.entity.AccountEntity import com.flowcrypt.email.database.entity.KeyEntity import com.flowcrypt.email.security.model.PgpKeyRingDetails import kotlinx.coroutines.flow.Flow @@ -56,4 +57,6 @@ interface KeysStorage { fun getFirstUsableForEncryptionPGPSecretKeyRing(user: String): PGPSecretKeyRing? fun getPassPhrasesUpdatesFlow(): Flow + + fun getActiveAccount(): AccountEntity? } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/security/KeysStorageImpl.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/security/KeysStorageImpl.kt index deffe52c94..336b610547 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/security/KeysStorageImpl.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/security/KeysStorageImpl.kt @@ -12,6 +12,7 @@ import androidx.lifecycle.asFlow import androidx.lifecycle.liveData import androidx.lifecycle.switchMap import com.flowcrypt.email.database.FlowCryptRoomDatabase +import com.flowcrypt.email.database.entity.AccountEntity import com.flowcrypt.email.database.entity.KeyEntity import com.flowcrypt.email.extensions.org.bouncycastle.openpgp.toPgpKeyRingDetails import com.flowcrypt.email.extensions.org.pgpainless.key.info.usableForEncryption @@ -280,6 +281,8 @@ class KeysStorageImpl private constructor(context: Context) : KeysStorage { override fun getPassPhrasesUpdatesFlow(): Flow = passphrasesUpdatesLiveData.asFlow() + override fun getActiveAccount(): AccountEntity? = pureActiveAccountLiveData.value + private fun preparePassphrasesMap(keyEntityList: List) { val existedIdList = passPhraseMap.keys val refreshedIdList = keyEntityList.map { it.fingerprint } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt index b8b142e645..d0f767b180 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt @@ -69,6 +69,7 @@ import com.flowcrypt.email.extensions.navController import com.flowcrypt.email.extensions.org.bouncycastle.openpgp.toPgpKeyRingDetails import com.flowcrypt.email.extensions.showActionDialogFragment import com.flowcrypt.email.extensions.showChoosePublicKeyDialogFragment +import com.flowcrypt.email.extensions.showDialogFragment import com.flowcrypt.email.extensions.showInfoDialog import com.flowcrypt.email.extensions.showKeyboard import com.flowcrypt.email.extensions.showNeedPassphraseDialog @@ -90,6 +91,8 @@ import com.flowcrypt.email.security.model.PgpKeyRingDetails import com.flowcrypt.email.security.pgp.PgpDecryptAndOrVerify import com.flowcrypt.email.ui.activity.fragment.base.BaseFragment import com.flowcrypt.email.ui.activity.fragment.dialog.ActionsDialogFragment +import com.flowcrypt.email.ui.activity.fragment.dialog.ChoosePrivateKeyDialogFragment +import com.flowcrypt.email.ui.activity.fragment.dialog.ChoosePrivateKeyDialogFragmentArgs import com.flowcrypt.email.ui.activity.fragment.dialog.ChoosePublicKeyDialogFragment import com.flowcrypt.email.ui.activity.fragment.dialog.CreateOutgoingMessageDialogFragment import com.flowcrypt.email.ui.activity.fragment.dialog.NoPgpFoundDialogFragment @@ -332,6 +335,7 @@ class CreateMessageFragment : BaseFragment(), subscribeToActionsDialogFragment() subscribeToImportingAdditionalPrivateKeys() subscribeToChoosePublicKeyDialogFragment() + subscribeToChoosePrivateKeysDialogFragment() subscribeToCreateOutgoingMessageDialogFragment() val isEncryptedMode = @@ -1675,7 +1679,16 @@ class CreateMessageFragment : BaseFragment(), } RESULT_CODE_ADD_USER_ID_TO_EXISTING_PRIVATE_KEY -> { - //todo-denbond7 need to add realization in a separate PR + showDialogFragment(navController) { + return@showDialogFragment object : NavDirections { + override val actionId = R.id.choose_private_key_dialog_graph + override val arguments = ChoosePrivateKeyDialogFragmentArgs( + requestKey = REQUEST_KEY_CHOOSE_PRIVATE_KEYS, + choiceMode = ListView.CHOICE_MODE_SINGLE, + title = getString(R.string.please_choose_key_you_would_like_to_modify), + ).toBundle() + } + } } } } @@ -1707,6 +1720,19 @@ class CreateMessageFragment : BaseFragment(), } } + private fun subscribeToChoosePrivateKeysDialogFragment() { + setFragmentResultListener(REQUEST_KEY_CHOOSE_PRIVATE_KEYS) { _, bundle -> + val keyList = bundle.getStringArray(ChoosePrivateKeyDialogFragment.KEY_RESULT) + ?: return@setFragmentResultListener + + if (keyList.isEmpty()) { + toast(R.string.please_select_key) + } else { + toast(keyList.joinToString()) + } + } + } + private fun subscribeToCreateOutgoingMessageDialogFragment() { setFragmentResultListener(REQUEST_KEY_CREATE_OUTGOING_MESSAGE) { _, bundle -> val result: Result<*>? = @@ -1856,24 +1882,28 @@ class CreateMessageFragment : BaseFragment(), } private fun fixNoKeyAvailableIssue(text: String) { - showActionDialogFragment( - navController, - requestKey = REQUEST_KEY_FIX_NO_PRIVATE_KEY_AVAILABLE, - dialogTitle = text, - isCancelable = true, - items = listOf( - DialogItem( - iconResourceId = R.drawable.ic_import_user_public_key, - title = getString(R.string.import_private_key), - id = RESULT_CODE_IMPORT_PRIVATE_KEY - ), - DialogItem( - iconResourceId = R.drawable.ic_edit_key_add_user_id, - title = getString(R.string.add_email_to_existing_key), - id = RESULT_CODE_ADD_USER_ID_TO_EXISTING_PRIVATE_KEY + if (account?.clientConfiguration?.usesKeyManager() == true) { + toast(getString(R.string.no_prv_keys_ask_admin)) + } else { + showActionDialogFragment( + navController, + requestKey = REQUEST_KEY_FIX_NO_PRIVATE_KEY_AVAILABLE, + dialogTitle = text, + isCancelable = true, + items = listOf( + DialogItem( + iconResourceId = R.drawable.ic_import_user_public_key, + title = getString(R.string.import_private_key), + id = RESULT_CODE_IMPORT_PRIVATE_KEY + ), + DialogItem( + iconResourceId = R.drawable.ic_edit_key_add_user_id, + title = getString(R.string.add_email_to_existing_key), + id = RESULT_CODE_ADD_USER_ID_TO_EXISTING_PRIVATE_KEY + ) ) ) - ) + } } companion object { @@ -1922,6 +1952,11 @@ class CreateMessageFragment : BaseFragment(), CreateMessageFragment::class.java ) + private val REQUEST_KEY_CHOOSE_PRIVATE_KEYS = GeneralUtil.generateUniqueExtraKey( + "REQUEST_KEY_CHOOSE_PRIVATE_KEYS", + CreateMessageFragment::class.java + ) + private const val RESULT_CODE_IMPORT_PRIVATE_KEY = 1 private const val RESULT_CODE_ADD_USER_ID_TO_EXISTING_PRIVATE_KEY = 2 } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/RecipientDetailsFragment.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/RecipientDetailsFragment.kt index fef97c19b0..3d66a035da 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/RecipientDetailsFragment.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/RecipientDetailsFragment.kt @@ -78,7 +78,7 @@ class RecipientDetailsFragment : BaseFragment() recipientDetailsViewModel.recipientPubKeysFlow.collect { it ?: return@collect if (it.isEmpty()) { - showEmptyView(resourcesId = R.drawable.ic_no_result_grey_24dp) + showEmptyView(imageResourcesId = R.drawable.ic_no_result_grey_24dp) } else { pubKeysRecyclerViewAdapter.submitList(it) showContent() diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/base/ListProgressBehaviour.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/base/ListProgressBehaviour.kt index 6364d135cc..a4ad76b785 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/base/ListProgressBehaviour.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/base/ListProgressBehaviour.kt @@ -34,7 +34,7 @@ interface ListProgressBehaviour : ProgressBehaviour { super.showStatus(msg, resourcesId) } - fun showEmptyView(msg: String? = null, resourcesId: Int = 0) { + fun showEmptyView(msg: String? = null, imageResourcesId: Int = 0) { contentView?.visibility = View.GONE goneStatusView() goneProgressView() @@ -44,9 +44,9 @@ interface ListProgressBehaviour : ProgressBehaviour { tVEmpty?.text = it } - if (resourcesId > 0) { + if (imageResourcesId > 0) { val iVEmptyImg = emptyView?.findViewById(R.id.iVEmptyImg) - iVEmptyImg?.setImageResource(resourcesId) + iVEmptyImg?.setImageResource(imageResourcesId) } emptyView?.visibility = View.VISIBLE diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/dialog/ChoosePrivateKeyDialogFragment.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/dialog/ChoosePrivateKeyDialogFragment.kt new file mode 100644 index 0000000000..c18b43073e --- /dev/null +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/dialog/ChoosePrivateKeyDialogFragment.kt @@ -0,0 +1,150 @@ +/* + * © 2016-present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com + * Contributors: DenBond7 + */ + +package com.flowcrypt.email.ui.activity.fragment.dialog + +import android.app.Dialog +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.appcompat.app.AlertDialog +import androidx.core.os.bundleOf +import androidx.fragment.app.setFragmentResult +import androidx.fragment.app.viewModels +import androidx.navigation.fragment.navArgs +import com.flowcrypt.email.R +import com.flowcrypt.email.api.retrofit.response.base.Result +import com.flowcrypt.email.databinding.FragmentChoosePrivateKeyBinding +import com.flowcrypt.email.extensions.exceptionMsg +import com.flowcrypt.email.extensions.navController +import com.flowcrypt.email.extensions.toast +import com.flowcrypt.email.jetpack.viewmodel.PrivateKeysViewModel +import com.flowcrypt.email.security.model.PgpKeyRingDetails +import com.flowcrypt.email.ui.activity.fragment.base.ListProgressBehaviour +import com.flowcrypt.email.ui.adapter.PrivateKeysArrayAdapter +import com.flowcrypt.email.util.GeneralUtil + +/** + * This dialog can be used to pick imported private keys. + * + * @author Denys Bondarenko + */ +class ChoosePrivateKeyDialogFragment : BaseDialogFragment(), ListProgressBehaviour { + private var binding: FragmentChoosePrivateKeyBinding? = null + private val args by navArgs() + private val privateKeysViewModel: PrivateKeysViewModel by viewModels() + + override val emptyView: View? + get() = binding?.layoutEmpty?.root + override val progressView: View? + get() = binding?.layoutProgress?.root + override val contentView: View? + get() = binding?.groupContent + override val statusView: View? + get() = binding?.layoutStatus?.root + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setupPrivateKeysViewModel() + } + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + binding = FragmentChoosePrivateKeyBinding.inflate( + LayoutInflater.from(requireContext()), + if ((view != null) and (view is ViewGroup)) view as ViewGroup? else null, + false + ) + + return AlertDialog.Builder(requireContext()).apply { + setTitle(null) + binding?.textViewMessage?.text = args.title + binding?.buttonOk?.setOnClickListener { sendResult() } + setView(binding?.root) + }.create() + } + + private fun setupPrivateKeysViewModel() { + privateKeysViewModel.parseKeysResultLiveData.observe(this) { + when (it.status) { + Result.Status.LOADING -> { + showProgress() + } + + Result.Status.SUCCESS -> { + val pgpKeyDetailsList = it.data ?: emptyList() + + if (pgpKeyDetailsList.isEmpty()) { + showEmptyView( + msg = getString( + R.string.no_key_available, + privateKeysViewModel.getActiveAccount()?.email ?: "" + ), + imageResourcesId = R.drawable.ic_no_result_grey_24dp + ) + } else { + binding?.listViewKeys?.choiceMode = args.choiceMode + binding?.listViewKeys?.adapter = PrivateKeysArrayAdapter( + requireContext(), + pgpKeyDetailsList, + args.choiceMode + ) + + if (pgpKeyDetailsList.size == 1) { + if (args.returnResultImmediatelyIfSingle) { + sendResult(listOf(pgpKeyDetailsList.first().fingerprint)) + navController?.navigateUp() + } + } else { + binding?.listViewKeys?.setItemChecked(0, true) + showContent() + } + } + } + + Result.Status.EXCEPTION -> { + binding?.textViewMessage?.text = it.exceptionMsg + showContent() + } + + else -> {} + } + } + } + + private fun sendResult() { + val selectedKeys = mutableListOf() + val checkedItemPositions = binding?.listViewKeys?.checkedItemPositions + val pgpKeyDetailsList = privateKeysViewModel.parseKeysResultLiveData.value?.data ?: emptyList() + if (checkedItemPositions != null) { + for (i in 0 until checkedItemPositions.size()) { + val key = checkedItemPositions.keyAt(i) + if (checkedItemPositions.get(key)) { + selectedKeys.add(pgpKeyDetailsList[key]) + } + } + } + + if (selectedKeys.isEmpty()) { + toast(R.string.please_select_key) + } else { + navController?.navigateUp() + sendResult(selectedKeys.map { it.fingerprint }) + } + } + + private fun sendResult(fingerprints: List) { + setFragmentResult( + args.requestKey, + bundleOf(KEY_RESULT to fingerprints.toTypedArray()) + ) + } + + companion object { + val KEY_RESULT = GeneralUtil.generateUniqueExtraKey( + "KEY_RESULT", ChoosePrivateKeyDialogFragment::class.java + ) + } +} diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/adapter/PrivateKeysArrayAdapter.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/adapter/PrivateKeysArrayAdapter.kt new file mode 100644 index 0000000000..cf0c564de5 --- /dev/null +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/adapter/PrivateKeysArrayAdapter.kt @@ -0,0 +1,75 @@ +/* + * © 2016-present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com + * Contributors: DenBond7 + */ + +package com.flowcrypt.email.ui.adapter + +import android.content.Context +import android.graphics.Color +import android.text.Spanned +import android.text.style.ForegroundColorSpan +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ArrayAdapter +import android.widget.ListView +import android.widget.TextView +import androidx.core.text.toSpannable +import com.flowcrypt.email.R +import com.flowcrypt.email.security.model.PgpKeyRingDetails + +/** + * @author Denys Bondarenko + */ +class PrivateKeysArrayAdapter(context: Context, keys: List, choiceMode: Int) : + ArrayAdapter(context, R.layout.private_key_item_radio_button, keys) { + private val inflater: LayoutInflater = LayoutInflater.from(context) + private val layoutId = if (choiceMode == ListView.CHOICE_MODE_MULTIPLE) { + R.layout.private_key_item_checkbox + } else { + R.layout.private_key_item_radio_button + } + + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + var view = convertView + val item = getItem(position) + + val viewHolder: ViewHolder + if (view == null) { + viewHolder = ViewHolder() + view = inflater.inflate(layoutId, parent, false) + viewHolder.textViewEmail = view.findViewById(R.id.textViewEmail) + viewHolder.textViewFingerprint = view.findViewById(R.id.textViewFingerprint) + view.tag = viewHolder + } else { + viewHolder = view.tag as ViewHolder + } + + updateView(item, viewHolder) + + return requireNotNull(view) + } + + private fun updateView(pgpKeyRingDetails: PgpKeyRingDetails?, viewHolder: ViewHolder) { + val userIds = pgpKeyRingDetails?.users?.joinToString(separator = ",\n") ?: "" + viewHolder.textViewEmail?.text = userIds + if (userIds.contains("\n")) { + val spannable = viewHolder.textViewEmail?.text?.toSpannable() + spannable?.setSpan( + ForegroundColorSpan(Color.GRAY), + userIds.indexOfFirst { it == '\n' }, + userIds.length, + Spanned.SPAN_INCLUSIVE_INCLUSIVE + ) + viewHolder.textViewEmail?.text = spannable + } + + viewHolder.textViewFingerprint?.text = pgpKeyRingDetails?.fingerprint + } + + private class ViewHolder { + var textViewEmail: TextView? = null + var textViewFingerprint: TextView? = null + } +} diff --git a/FlowCrypt/src/main/res/layout/fragment_choose_private_key.xml b/FlowCrypt/src/main/res/layout/fragment_choose_private_key.xml new file mode 100644 index 0000000000..891ea668bc --- /dev/null +++ b/FlowCrypt/src/main/res/layout/fragment_choose_private_key.xml @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + +