From 5d360e177a35df119c8bef14edab7edbaf63800d Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Fri, 8 Jul 2022 20:25:34 +0600 Subject: [PATCH 1/7] 5445-improve_encryption_ux issue: https://gitlab.e.foundation/e/backlog/-/issues/5445 Update the encryption enable flow for messageCompose stage --- .../com/fsck/k9/activity/MessageCompose.java | 11 +- .../activity/compose/ComposeCryptoStatus.kt | 9 +- .../compose/OpenPgpConfigureListener.java | 22 ++ .../activity/compose/PgpKeySetupDialog.java | 68 +++++ .../compose/PgpUnconfiguredDialog.java | 68 +++++ .../k9/activity/compose/RecipientMvpView.kt | 17 +- .../k9/activity/compose/RecipientPresenter.kt | 94 ++++++- .../res/drawable/status_lock_error_open.xml | 15 ++ .../res/layout/message_compose_recipients.xml | 8 + .../openpgp_enable_encryption_dialog.xml | 45 ++++ .../openpgp_setup_encryption_key_dialog.xml | 45 ++++ app/ui/legacy/src/main/res/values/strings.xml | 7 + .../openpgp/util/OpenPgpKeySetupUtil.java | 233 ++++++++++++++++++ .../openpgp/util/OpenPgpProviderUtil.java | 11 + 14 files changed, 640 insertions(+), 13 deletions(-) create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/OpenPgpConfigureListener.java create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpKeySetupDialog.java create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java create mode 100644 app/ui/legacy/src/main/res/drawable/status_lock_error_open.xml create mode 100644 app/ui/legacy/src/main/res/layout/openpgp_enable_encryption_dialog.xml create mode 100644 app/ui/legacy/src/main/res/layout/openpgp_setup_encryption_key_dialog.xml create mode 100644 plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeySetupUtil.java diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java index 510b4e405c..a9b575108a 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java @@ -62,6 +62,7 @@ import com.fsck.k9.activity.compose.ComposeCryptoStatus; import com.fsck.k9.activity.compose.ComposeCryptoStatus.SendErrorState; import com.fsck.k9.activity.compose.IdentityAdapter; import com.fsck.k9.activity.compose.IdentityAdapter.IdentityContainer; +import com.fsck.k9.activity.compose.OpenPgpConfigureListener; import com.fsck.k9.activity.compose.PgpEnabledErrorDialog.OnOpenPgpDisableListener; import com.fsck.k9.activity.compose.PgpInlineDialog.OnOpenPgpInlineChangeListener; import com.fsck.k9.activity.compose.PgpSignOnlyDialog.OnOpenPgpSignOnlyChangeListener; @@ -126,7 +127,8 @@ import timber.log.Timber; public class MessageCompose extends K9Activity implements OnClickListener, CancelListener, AttachmentDownloadCancelListener, OnFocusChangeListener, OnOpenPgpInlineChangeListener, OnOpenPgpSignOnlyChangeListener, MessageBuilder.Callback, - AttachmentPresenter.AttachmentsChangedListener, OnOpenPgpDisableListener, PermissionUiHelper { + AttachmentPresenter.AttachmentsChangedListener, OnOpenPgpDisableListener, PermissionUiHelper, + OpenPgpConfigureListener { private static final int DIALOG_SAVE_OR_DISCARD_DRAFT_MESSAGE = 1; private static final int DIALOG_CONFIRM_DISCARD_ON_BACK = 2; @@ -321,7 +323,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, recipientPresenter = new RecipientPresenter(getApplicationContext(), getSupportLoaderManager(), openPgpApiManager, recipientMvpView, account, composePgpInlineDecider, composePgpEnableByDefaultDecider, AutocryptStatusInteractor.getInstance(), new ReplyToParser(), - DI.get(AutocryptDraftStateHeaderParser.class)); + DI.get(AutocryptDraftStateHeaderParser.class), preferences); recipientPresenter.asyncUpdateCryptoStatus(); @@ -1455,6 +1457,11 @@ public class MessageCompose extends K9Activity implements OnClickListener, quotedMessagePresenter.processDraftMessage(messageViewInfo, k9identity); } + @Override + public void initializePgpSetup() { + recipientPresenter.initializeOpenPgpSetup(); + } + static class SendMessageTask extends AsyncTask { final MessagingController messagingController; final Preferences preferences; diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ComposeCryptoStatus.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ComposeCryptoStatus.kt index 5fcb1c64ac..c646dd964f 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ComposeCryptoStatus.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ComposeCryptoStatus.kt @@ -7,7 +7,6 @@ import com.fsck.k9.message.AutocryptStatusInteractor import com.fsck.k9.message.AutocryptStatusInteractor.RecipientAutocryptStatus import com.fsck.k9.message.CryptoStatus import com.fsck.k9.view.RecipientSelectView.Recipient -import org.openintents.openpgp.OpenPgpApiManager import org.openintents.openpgp.OpenPgpApiManager.OpenPgpProviderState /** This is an immutable object which contains all relevant metadata entered @@ -70,10 +69,10 @@ data class ComposeCryptoStatus( val recipientAddressesAsArray = recipientAddresses.toTypedArray() private val displayTypeFromProviderError = when (openPgpProviderState) { - OpenPgpApiManager.OpenPgpProviderState.OK -> null - OpenPgpApiManager.OpenPgpProviderState.UNCONFIGURED -> CryptoStatusDisplayType.UNCONFIGURED - OpenPgpApiManager.OpenPgpProviderState.UNINITIALIZED -> CryptoStatusDisplayType.UNINITIALIZED - OpenPgpApiManager.OpenPgpProviderState.ERROR, OpenPgpApiManager.OpenPgpProviderState.UI_REQUIRED -> CryptoStatusDisplayType.ERROR + OpenPgpProviderState.OK -> if(openPgpKeyId == null) CryptoStatusDisplayType.KEY_MISSING else null + OpenPgpProviderState.UNCONFIGURED -> CryptoStatusDisplayType.UNCONFIGURED + OpenPgpProviderState.UNINITIALIZED -> CryptoStatusDisplayType.UNINITIALIZED + OpenPgpProviderState.ERROR, OpenPgpProviderState.UI_REQUIRED -> CryptoStatusDisplayType.ERROR } private val displayTypeFromAutocryptError = when (recipientAutocryptStatusType) { diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/OpenPgpConfigureListener.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/OpenPgpConfigureListener.java new file mode 100644 index 0000000000..cc20700f51 --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/OpenPgpConfigureListener.java @@ -0,0 +1,22 @@ +/* + * Copyright ECORP SAS 2022 + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.fsck.k9.activity.compose; + +public interface OpenPgpConfigureListener { + + void initializePgpSetup(); +} diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpKeySetupDialog.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpKeySetupDialog.java new file mode 100644 index 0000000000..ee9eae97a3 --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpKeySetupDialog.java @@ -0,0 +1,68 @@ +/* + * Copyright ECORP SAS 2022 + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.fsck.k9.activity.compose; + + +import android.annotation.SuppressLint; +import android.app.AlertDialog.Builder; +import android.app.Dialog; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; + +import androidx.annotation.IdRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import com.fsck.k9.ui.R; +import com.fsck.k9.view.HighlightDialogFragment; + + +public class PgpKeySetupDialog extends HighlightDialogFragment { + + public static PgpKeySetupDialog newInstance(@IdRes int showcaseView) { + PgpKeySetupDialog dialog = new PgpKeySetupDialog(); + + Bundle args = new Bundle(); + args.putInt(ARG_HIGHLIGHT_VIEW, showcaseView); + dialog.setArguments(args); + + return dialog; + } + + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + @SuppressLint("InflateParams") + View view = LayoutInflater.from(getActivity()).inflate(R.layout.openpgp_setup_encryption_key_dialog, null); + + Builder builder = new Builder(getActivity()) + .setView(view); + + builder.setPositiveButton(R.string.openpgp_dialog_create_key_pair, (dialog, which) -> { + if (!(getActivity() instanceof OpenPgpConfigureListener)) { + return; + } + + ((OpenPgpConfigureListener) getActivity()).initializePgpSetup(); + dialog.dismiss(); + }); + + builder.setNegativeButton(R.string.openpgp_dialog_cancel, (dialog, which) -> dialog.dismiss()); + + return builder.create(); + } +} diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java new file mode 100644 index 0000000000..6cd685085e --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java @@ -0,0 +1,68 @@ +/* + * Copyright ECORP SAS 2022 + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.fsck.k9.activity.compose; + + +import android.annotation.SuppressLint; +import android.app.AlertDialog.Builder; +import android.app.Dialog; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; + +import androidx.annotation.IdRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import com.fsck.k9.ui.R; +import com.fsck.k9.view.HighlightDialogFragment; + + +public class PgpUnconfiguredDialog extends HighlightDialogFragment { + + public static PgpUnconfiguredDialog newInstance(@IdRes int showcaseView) { + PgpUnconfiguredDialog dialog = new PgpUnconfiguredDialog(); + + Bundle args = new Bundle(); + args.putInt(ARG_HIGHLIGHT_VIEW, showcaseView); + dialog.setArguments(args); + + return dialog; + } + + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + @SuppressLint("InflateParams") + View view = LayoutInflater.from(getActivity()).inflate(R.layout.openpgp_enable_encryption_dialog, null); + + Builder builder = new Builder(getActivity()) + .setView(view); + + builder.setPositiveButton(R.string.openpgp_dialog_proceed, (dialog, which) -> { + if (!(getActivity() instanceof OpenPgpConfigureListener)) { + return; + } + + ((OpenPgpConfigureListener) getActivity()).initializePgpSetup(); + dialog.dismiss(); + }); + + builder.setNegativeButton(R.string.openpgp_dialog_cancel, (dialog, which) -> dialog.dismiss()); + + return builder.create(); + } +} diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientMvpView.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientMvpView.kt index 60f5b84f62..9e20089948 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientMvpView.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientMvpView.kt @@ -371,6 +371,16 @@ class RecipientMvpView(private val activity: MessageCompose) : View.OnFocusChang dialog.show(activity.supportFragmentManager, "openpgp_description") } + fun showOpenPgpUncofiguredDialog() { + val dialog = PgpUnconfiguredDialog.newInstance(R.id.crypto_status_anchor) + dialog.show(activity.supportFragmentManager, "openpgp_unconfigure") + } + + fun showOpenPgpUserKeySetupDialog() { + val dialog = PgpKeySetupDialog.newInstance(R.id.crypto_status_anchor) + dialog.show(activity.supportFragmentManager, "openpgp_key_setup") + } + fun launchUserInteractionPendingIntent(pendingIntent: PendingIntent?, requestCode: Int) { activity.launchUserInteractionPendingIntent(pendingIntent, requestCode) } @@ -382,15 +392,16 @@ class RecipientMvpView(private val activity: MessageCompose) : View.OnFocusChang } enum class CryptoStatusDisplayType(val childIdToDisplay: Int) { - UNCONFIGURED(VIEW_INDEX_HIDDEN), + UNCONFIGURED(R.id.crypto_status_disabled), UNINITIALIZED(VIEW_INDEX_HIDDEN), SIGN_ONLY(R.id.crypto_status_disabled), - UNAVAILABLE(VIEW_INDEX_HIDDEN), + UNAVAILABLE(R.id.crypto_status_disabled), ENABLED(R.id.crypto_status_enabled), ENABLED_ERROR(R.id.crypto_status_error), ENABLED_TRUSTED(R.id.crypto_status_trusted), AVAILABLE(R.id.crypto_status_disabled), - ERROR(R.id.crypto_status_error); + ERROR(R.id.crypto_status_error), + KEY_MISSING(R.id.crypto_status_key_missing); } enum class CryptoSpecialModeDisplayType(val childIdToDisplay: Int) { diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt index 9b0e6d2ed3..581be4ee6f 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt @@ -2,6 +2,7 @@ package com.fsck.k9.activity.compose import android.Manifest import android.app.Activity +import android.app.PendingIntent import android.content.Context import android.content.Intent import android.content.pm.PackageManager @@ -14,10 +15,12 @@ import androidx.loader.app.LoaderManager import com.fsck.k9.Account import com.fsck.k9.Identity import com.fsck.k9.K9 +import com.fsck.k9.Preferences import com.fsck.k9.activity.compose.ComposeCryptoStatus.AttachErrorState import com.fsck.k9.activity.compose.ComposeCryptoStatus.SendErrorState import com.fsck.k9.autocrypt.AutocryptDraftStateHeader import com.fsck.k9.autocrypt.AutocryptDraftStateHeaderParser +import com.fsck.k9.crypto.OpenPgpApiHelper import com.fsck.k9.helper.Contacts import com.fsck.k9.helper.MailTo import com.fsck.k9.helper.ReplyToParser @@ -37,6 +40,8 @@ import org.openintents.openpgp.OpenPgpApiManager import org.openintents.openpgp.OpenPgpApiManager.OpenPgpApiManagerCallback import org.openintents.openpgp.OpenPgpApiManager.OpenPgpProviderError import org.openintents.openpgp.OpenPgpApiManager.OpenPgpProviderState +import org.openintents.openpgp.util.OpenPgpKeySetupUtil +import org.openintents.openpgp.util.OpenPgpProviderUtil import timber.log.Timber private const val STATE_KEY_CC_SHOWN = "state:ccShown" @@ -63,7 +68,8 @@ class RecipientPresenter( private val composePgpEnableByDefaultDecider: ComposePgpEnableByDefaultDecider, private val autocryptStatusInteractor: AutocryptStatusInteractor, private val replyToParser: ReplyToParser, - private val draftStateHeaderParser: AutocryptDraftStateHeaderParser + private val draftStateHeaderParser: AutocryptDraftStateHeaderParser, + private val preferences: Preferences ) { private lateinit var account: Account private var alwaysBccAddresses: Array
? = null @@ -91,6 +97,8 @@ class RecipientPresenter( private val allRecipients: List get() = with(recipientMvpView) { toRecipients + ccRecipients + bccRecipients } + private var openPgpKeySetupUtil : OpenPgpKeySetupUtil? = null + init { recipientMvpView.setPresenter(this) recipientMvpView.setLoaderManager(loaderManager) @@ -460,6 +468,11 @@ class RecipientPresenter( } fun onCryptoModeChanged(cryptoMode: CryptoMode) { + if (cryptoMode == CryptoMode.CHOICE_ENABLED && isInPgpKeySetupState()) { + recipientMvpView.showOpenPgpUserKeySetupDialog() + return + } + currentCryptoMode = cryptoMode asyncUpdateCryptoStatus() } @@ -517,6 +530,10 @@ class RecipientPresenter( } fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + if (openPgpKeySetupUtil != null && openPgpKeySetupUtil?.handleOnActivityResult(requestCode, resultCode, data) == true) { + return + } + when (requestCode) { CONTACT_PICKER_TO, CONTACT_PICKER_CC, CONTACT_PICKER_BCC -> { if (resultCode != Activity.RESULT_OK || data == null) return @@ -526,6 +543,7 @@ class RecipientPresenter( } OPENPGP_USER_INTERACTION -> { openPgpApiManager.onUserInteractionResult() + redrawCachedCryptoStatusIcon() } REQUEST_CODE_AUTOCRYPT -> { asyncUpdateCryptoStatus() @@ -540,9 +558,10 @@ class RecipientPresenter( } fun onClickCryptoStatus() { + when (openPgpApiManager.openPgpProviderState) { OpenPgpProviderState.UNCONFIGURED -> { - Timber.e("click on crypto status while unconfigured - this should not really happen?!") + recipientMvpView.showOpenPgpUncofiguredDialog() } OpenPgpProviderState.OK -> { toggleEncryptionState(false) @@ -558,6 +577,75 @@ class RecipientPresenter( } } + + fun initializeOpenPgpSetup() { + if(openPgpApiManager.openPgpProviderState == OpenPgpProviderState.UNCONFIGURED) { + configureOpenPgp() + } else if (isInPgpKeySetupState()) { + initializeUserPgpKeySetup() + } + } + + private fun configureOpenPgp() { + setUpPgpProvider() + + account.openPgpProvider?.let { + recipientMvpView.setCryptoProvider(it) + openPgpApiManager.setOpenPgpProvider(it, object : OpenPgpApiManagerCallback { + override fun onOpenPgpProviderStatusChanged() { + if (openPgpApiManager.openPgpProviderState == OpenPgpProviderState.UI_REQUIRED) { + val pendingIntent = openPgpApiManager.userInteractionPendingIntent + recipientMvpView.launchUserInteractionPendingIntent(pendingIntent, OPENPGP_USER_INTERACTION) + return + } + + if (isInPgpKeySetupState()) { + initializeUserPgpKeySetup() + return + } + + asyncUpdateCryptoStatus() + } + + override fun onOpenPgpProviderError(error: OpenPgpProviderError) { + openPgpCallback.onOpenPgpProviderError(error) + } + }) + } + } + + private fun isInPgpKeySetupState() = (openPgpApiManager.openPgpProviderState == OpenPgpProviderState.OK && account.openPgpKey == Account.NO_OPENPGP_KEY) + + private fun setUpPgpProvider() { + if (account.openPgpProvider == null) { + account.openPgpProvider = OpenPgpProviderUtil.getOpenPgpProviderPackage(context) + preferences.saveAccount(account) + } + } + + private fun initializeUserPgpKeySetup() { + if (!isInPgpKeySetupState()) { + return + } + val userId = OpenPgpApiHelper.buildUserId(account.getIdentity(0)) + openPgpKeySetupUtil = OpenPgpKeySetupUtil(openPgpApiManager, account.openPgpProvider, userId, true) + openPgpKeySetupUtil?.setCallBack(object : OpenPgpKeySetupUtil.OpenPgpKeySetupCallBack { + override fun startPendingIntentForResult( pendingIntent: PendingIntent?, requestCode: Int) { + recipientMvpView.launchUserInteractionPendingIntent(pendingIntent, requestCode) + } + + override fun saveAndPersist(key: Long) { + account.openPgpKey = key + preferences.saveAccount(account) + openPgpKeySetupUtil = null + openPgpApiManager.onUserInteractionResult() + asyncUpdateCryptoStatus() + } + }) + + openPgpKeySetupUtil?.startSetup() + } + private fun toggleEncryptionState(showGotIt: Boolean) { val currentCryptoStatus = currentCachedCryptoStatus if (currentCryptoStatus == null) { @@ -622,7 +710,7 @@ class RecipientPresenter( when (sendErrorState) { SendErrorState.ENABLED_ERROR -> recipientMvpView.showOpenPgpEnabledErrorDialog(false) SendErrorState.PROVIDER_ERROR -> recipientMvpView.showErrorOpenPgpConnection() - SendErrorState.KEY_CONFIG_ERROR -> recipientMvpView.showErrorNoKeyConfigured() + SendErrorState.KEY_CONFIG_ERROR -> recipientMvpView.showOpenPgpUserKeySetupDialog() else -> throw AssertionError("not all error states handled, this is a bug!") } } diff --git a/app/ui/legacy/src/main/res/drawable/status_lock_error_open.xml b/app/ui/legacy/src/main/res/drawable/status_lock_error_open.xml new file mode 100644 index 0000000000..625d4129c2 --- /dev/null +++ b/app/ui/legacy/src/main/res/drawable/status_lock_error_open.xml @@ -0,0 +1,15 @@ + + + + + diff --git a/app/ui/legacy/src/main/res/layout/message_compose_recipients.xml b/app/ui/legacy/src/main/res/layout/message_compose_recipients.xml index 2d93d6e237..dea72da38d 100644 --- a/app/ui/legacy/src/main/res/layout/message_compose_recipients.xml +++ b/app/ui/legacy/src/main/res/layout/message_compose_recipients.xml @@ -154,6 +154,14 @@ app:srcCompat="@drawable/status_lock_error" app:tint="?attr/openpgp_red" /> + + diff --git a/app/ui/legacy/src/main/res/layout/openpgp_enable_encryption_dialog.xml b/app/ui/legacy/src/main/res/layout/openpgp_enable_encryption_dialog.xml new file mode 100644 index 0000000000..e3c4048b7f --- /dev/null +++ b/app/ui/legacy/src/main/res/layout/openpgp_enable_encryption_dialog.xml @@ -0,0 +1,45 @@ + + + + + + + + + + diff --git a/app/ui/legacy/src/main/res/layout/openpgp_setup_encryption_key_dialog.xml b/app/ui/legacy/src/main/res/layout/openpgp_setup_encryption_key_dialog.xml new file mode 100644 index 0000000000..44d9d428c0 --- /dev/null +++ b/app/ui/legacy/src/main/res/layout/openpgp_setup_encryption_key_dialog.xml @@ -0,0 +1,45 @@ + + + + + + + + + + diff --git a/app/ui/legacy/src/main/res/values/strings.xml b/app/ui/legacy/src/main/res/values/strings.xml index a5575231c2..fba9354fb7 100644 --- a/app/ui/legacy/src/main/res/values/strings.xml +++ b/app/ui/legacy/src/main/res/values/strings.xml @@ -1094,6 +1094,9 @@ Signatures may break when sent to mailing lists. Signatures may be displayed as \'signature.asc\' attachments in some clients. Encrypted messages always include a signature. + PROCEED + CANCEL + CREATE KEY PAIR Plaintext end-to-end signature contained an error @@ -1171,6 +1174,10 @@ Back Disable Encryption OpenPGP Encryption + Enable encryption + End-to-end encryption is the safest way to keep other people from spying on communications or accessing data transferred from one end device to another.By proceeding, OpenPGP support will be enabled.\n\nRequired: To send an encrypted mail, a pair of keys is required so that the data encrypted on the sender\'s device can be decrypted by the intended recipient. For example, you will need the public key of the person you are sending to and vice versa. + Set up encryption keys + End-to-end encryption is the safest way to keep other people from spying on communications or accessing data transferred from one end device to another.\n\nTo send an encrypted mail, a pair of keys is required so that the data encrypted on the sender\'s device can be decrypted by the intended recipient. For example, you will need the public key of the person you are sending to and vice versa. Autocrypt mutual mode Autocrypt mutual mode diff --git a/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeySetupUtil.java b/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeySetupUtil.java new file mode 100644 index 0000000000..4ca3bc8425 --- /dev/null +++ b/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeySetupUtil.java @@ -0,0 +1,233 @@ +/* + * Copyright ECORP SAS 2022 + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.openintents.openpgp.util; + + +import android.app.Activity; +import android.app.PendingIntent; +import android.content.Intent; +import android.content.IntentSender; + +import org.openintents.openpgp.OpenPgpApiManager; +import org.openintents.openpgp.OpenPgpApiManager.OpenPgpApiManagerCallback; +import org.openintents.openpgp.OpenPgpApiManager.OpenPgpProviderError; +import org.openintents.openpgp.OpenPgpApiManager.OpenPgpProviderState; +import org.openintents.openpgp.OpenPgpError; +import org.openintents.openpgp.util.OpenPgpApi.IOpenPgpCallback; +import timber.log.Timber; + + +public class OpenPgpKeySetupUtil implements OpenPgpApiManagerCallback { + + private static final int NO_KEY = 0; + private static final int REQUEST_CODE_API_MANAGER = 6; + private static final int REQUEST_CODE_KEY_PREFERENCE = 7; + + private final boolean showAutocryptHint; + private final String userId; + private final OpenPgpApiManager openPgpApiManager; + + private long keyId; + private boolean pendingIntentRunImmediately; + private Intent cachedActivityResultData; + private PendingIntent pendingIntentSelectKey; + private OpenPgpKeySetupCallBack callBack; + + public OpenPgpKeySetupUtil(OpenPgpApiManager openPgpApiManager, String openPgpProvider, String userId, boolean showAutocryptHint) { + this.openPgpApiManager = openPgpApiManager; + this.openPgpApiManager.setOpenPgpProvider(openPgpProvider, this); + this.userId = userId; + this.showAutocryptHint = showAutocryptHint; + } + + public void setCallBack(OpenPgpKeySetupCallBack callBack) { + this.callBack = callBack; + } + + public void startSetup() { + switch (openPgpApiManager.getOpenPgpProviderState()) { + // The GET_SIGN_KEY action is special, in that it can be used as an implicit registration + // to the API. Therefore, we can ignore the UI_REQUIRED here. If it comes up regardless, + // it will also work as a regular pending intent. + case UI_REQUIRED: + case OK: { + apiGetOrStartPendingIntent(); + break; + } + default: { + openPgpApiManager.refreshConnection(); + break; + } + } + } + + @Override + public void onOpenPgpProviderStatusChanged() { + if (openPgpApiManager.getOpenPgpProviderState() == OpenPgpProviderState.OK) { + apiRetrievePendingIntentAndKeyInfo(); + } else { + pendingIntentSelectKey = null; + pendingIntentRunImmediately = false; + cachedActivityResultData = null; + } + } + + @Override + public void onOpenPgpProviderError(OpenPgpProviderError error) { + if (error == OpenPgpProviderError.ConnectionLost) { + openPgpApiManager.refreshConnection(); + } + } + + private void apiRetrievePendingIntentAndKeyInfo() { + Intent data; + + if (cachedActivityResultData != null) { + data = cachedActivityResultData; + cachedActivityResultData = null; + } else { + data = new Intent(); + } + + apiRetrievePendingIntentAndKeyInfo(data); + } + + private void apiRetrievePendingIntentAndKeyInfo(Intent data) { + data.setAction(OpenPgpApi.ACTION_GET_SIGN_KEY_ID); + data.putExtra(OpenPgpApi.EXTRA_USER_ID, userId); + data.putExtra(OpenPgpApi.EXTRA_PRESELECT_KEY_ID, keyId); + data.putExtra(OpenPgpApi.EXTRA_SHOW_AUTOCRYPT_HINT, showAutocryptHint); + OpenPgpApi api = openPgpApiManager.getOpenPgpApi(); + api.executeApiAsync(data, null, null, openPgpCallback); + } + + private final IOpenPgpCallback openPgpCallback = result -> { + int resultCode = result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR); + switch (resultCode) { + case OpenPgpApi.RESULT_CODE_SUCCESS: + case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: { + PendingIntent pendingIntentSelectKey = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); + + if (result.hasExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID)) { + long keyId = result.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, NO_KEY); + + updateWidgetData(keyId, pendingIntentSelectKey); + } else { + updateWidgetData(pendingIntentSelectKey); + } + + break; + } + + case OpenPgpApi.RESULT_CODE_ERROR: { + OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR); + Timber.e("RESULT_CODE_ERROR: %s", error.getMessage()); + break; + } + } + }; + + private void apiGetOrStartPendingIntent() { + if (pendingIntentSelectKey != null) { + apiStartPendingIntent(); + return; + } + + pendingIntentRunImmediately = true; + apiRetrievePendingIntentAndKeyInfo(); + } + + private void apiStartPendingIntent() { + if (pendingIntentSelectKey == null) { + Timber.e("Tried to launch pending intent but didn't have any?"); + return; + } + + try { + if (callBack != null) { + callBack.startPendingIntentForResult(pendingIntentSelectKey, REQUEST_CODE_KEY_PREFERENCE); + } + } catch (IntentSender.SendIntentException e) { + Timber.e(e,"Error launching pending intent"); + } finally { + pendingIntentSelectKey = null; + } + } + + private void updateWidgetData(PendingIntent pendingIntentSelectKey) { + this.pendingIntentSelectKey = pendingIntentSelectKey; + maybeRunPendingIntentImmediately(); + } + + private void updateWidgetData(long keyId, PendingIntent pendingIntentSelectKey) { + setAndPersist(keyId); + this.pendingIntentSelectKey = pendingIntentSelectKey; + + maybeRunPendingIntentImmediately(); + } + + private void maybeRunPendingIntentImmediately() { + if (!pendingIntentRunImmediately) { + return; + } + + pendingIntentRunImmediately = false; + apiStartPendingIntent(); + } + + private void setAndPersist(long newValue) { + if (newValue == 0L) { + return; + } + + keyId = newValue; + + if (callBack != null) { + callBack.saveAndPersist(keyId); + } + } + + public boolean handleOnActivityResult(int requestCode, int resultCode, Intent data) { + switch (requestCode) { + case REQUEST_CODE_API_MANAGER: + openPgpApiManager.onUserInteractionResult(); + return true; + + case REQUEST_CODE_KEY_PREFERENCE: + if (resultCode == Activity.RESULT_OK) { + cachedActivityResultData = data; + // this might happen early in the lifecycle (e.g. before onResume). if the provider isn't connected + // here, apiRetrievePendingIntentAndKeyInfo() will be called as soon as it is. + OpenPgpProviderState openPgpProviderState = openPgpApiManager.getOpenPgpProviderState(); + + if (openPgpProviderState == OpenPgpProviderState.OK || openPgpProviderState == OpenPgpProviderState.UI_REQUIRED) { + apiRetrievePendingIntentAndKeyInfo(); + } + } + return true; + } + return false; + } + + + public interface OpenPgpKeySetupCallBack { + + void startPendingIntentForResult(PendingIntent pendingIntent, int requestCode) throws IntentSender.SendIntentException; + + void saveAndPersist(long key); + } +} diff --git a/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpProviderUtil.java b/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpProviderUtil.java index b5e9b1e802..ddcecec54c 100644 --- a/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpProviderUtil.java +++ b/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpProviderUtil.java @@ -9,6 +9,8 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; +import androidx.annotation.Nullable; + public class OpenPgpProviderUtil { private static final String PACKAGE_NAME_APG = "org.thialfihar.android.apg"; @@ -59,4 +61,13 @@ public class OpenPgpProviderUtil { public static boolean isProviderAllowed(String packageName) { return !DISALLOWED_PROVIDERS.contains(packageName); } + + @Nullable + public static String getOpenPgpProviderPackage(Context context) { + List openPgpProviderPackages = getOpenPgpProviderPackages(context); + if (openPgpProviderPackages.size() > 0) { + return openPgpProviderPackages.get(0); + } + return null; + } } -- GitLab From 70c22896078532e58136103ebf29978715d0037c Mon Sep 17 00:00:00 2001 From: Vincent Bourgmayer Date: Mon, 1 Aug 2022 17:34:11 +0000 Subject: [PATCH 2/7] Apply 7 suggestion(s) to 4 file(s) --- .../com/fsck/k9/activity/compose/PgpKeySetupDialog.java | 8 ++++---- .../fsck/k9/activity/compose/PgpUnconfiguredDialog.java | 8 ++++++-- .../com/fsck/k9/activity/compose/RecipientPresenter.kt | 2 +- .../org/openintents/openpgp/util/OpenPgpProviderUtil.java | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpKeySetupDialog.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpKeySetupDialog.java index ee9eae97a3..35a0fec2d4 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpKeySetupDialog.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpKeySetupDialog.java @@ -34,9 +34,9 @@ import com.fsck.k9.view.HighlightDialogFragment; public class PgpKeySetupDialog extends HighlightDialogFragment { public static PgpKeySetupDialog newInstance(@IdRes int showcaseView) { - PgpKeySetupDialog dialog = new PgpKeySetupDialog(); + final PgpKeySetupDialog dialog = new PgpKeySetupDialog(); - Bundle args = new Bundle(); + final Bundle args = new Bundle(); args.putInt(ARG_HIGHLIGHT_VIEW, showcaseView); dialog.setArguments(args); @@ -47,9 +47,9 @@ public class PgpKeySetupDialog extends HighlightDialogFragment { @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { @SuppressLint("InflateParams") - View view = LayoutInflater.from(getActivity()).inflate(R.layout.openpgp_setup_encryption_key_dialog, null); + final View view = LayoutInflater.from(getActivity()).inflate(R.layout.openpgp_setup_encryption_key_dialog, null); - Builder builder = new Builder(getActivity()) + final Builder builder = new Builder(getActivity()) .setView(view); builder.setPositiveButton(R.string.openpgp_dialog_create_key_pair, (dialog, which) -> { diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java index 6cd685085e..9df519fd9d 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java @@ -36,7 +36,9 @@ public class PgpUnconfiguredDialog extends HighlightDialogFragment { public static PgpUnconfiguredDialog newInstance(@IdRes int showcaseView) { PgpUnconfiguredDialog dialog = new PgpUnconfiguredDialog(); - Bundle args = new Bundle(); + final PgpUnconfiguredDialog dialog = new PgpUnconfiguredDialog(); + + final Bundle args = new Bundle(); args.putInt(ARG_HIGHLIGHT_VIEW, showcaseView); dialog.setArguments(args); @@ -49,7 +51,9 @@ public class PgpUnconfiguredDialog extends HighlightDialogFragment { @SuppressLint("InflateParams") View view = LayoutInflater.from(getActivity()).inflate(R.layout.openpgp_enable_encryption_dialog, null); - Builder builder = new Builder(getActivity()) + final View view = LayoutInflater.from(getActivity()).inflate(R.layout.openpgp_enable_encryption_dialog, null); + + final Builder builder = new Builder(getActivity()) .setView(view); builder.setPositiveButton(R.string.openpgp_dialog_proceed, (dialog, which) -> { diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt index 581be4ee6f..da7b6d29cb 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt @@ -579,7 +579,7 @@ class RecipientPresenter( fun initializeOpenPgpSetup() { - if(openPgpApiManager.openPgpProviderState == OpenPgpProviderState.UNCONFIGURED) { + if (openPgpApiManager.openPgpProviderState == OpenPgpProviderState.UNCONFIGURED) { configureOpenPgp() } else if (isInPgpKeySetupState()) { initializeUserPgpKeySetup() diff --git a/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpProviderUtil.java b/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpProviderUtil.java index ddcecec54c..c2a30e9387 100644 --- a/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpProviderUtil.java +++ b/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpProviderUtil.java @@ -64,7 +64,7 @@ public class OpenPgpProviderUtil { @Nullable public static String getOpenPgpProviderPackage(Context context) { - List openPgpProviderPackages = getOpenPgpProviderPackages(context); + final List openPgpProviderPackages = getOpenPgpProviderPackages(context); if (openPgpProviderPackages.size() > 0) { return openPgpProviderPackages.get(0); } -- GitLab From 97b5b8d8309a1abd0f8ca602cdd9fa71bb452538 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Mon, 1 Aug 2022 23:36:44 +0600 Subject: [PATCH 3/7] fix error on suggestion application --- .../com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java index 9df519fd9d..77c12a2714 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java @@ -34,8 +34,6 @@ import com.fsck.k9.view.HighlightDialogFragment; public class PgpUnconfiguredDialog extends HighlightDialogFragment { public static PgpUnconfiguredDialog newInstance(@IdRes int showcaseView) { - PgpUnconfiguredDialog dialog = new PgpUnconfiguredDialog(); - final PgpUnconfiguredDialog dialog = new PgpUnconfiguredDialog(); final Bundle args = new Bundle(); @@ -49,8 +47,6 @@ public class PgpUnconfiguredDialog extends HighlightDialogFragment { @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { @SuppressLint("InflateParams") - View view = LayoutInflater.from(getActivity()).inflate(R.layout.openpgp_enable_encryption_dialog, null); - final View view = LayoutInflater.from(getActivity()).inflate(R.layout.openpgp_enable_encryption_dialog, null); final Builder builder = new Builder(getActivity()) -- GitLab From 22a547ecf5bd5cc05303bda26f873ee147649dda Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Tue, 2 Aug 2022 16:54:41 +0600 Subject: [PATCH 4/7] improve method naming for OpenPgpKeySetupUtil --- .../k9/activity/compose/RecipientPresenter.kt | 2 +- .../openpgp/util/OpenPgpKeySetupUtil.java | 59 ++++++++++--------- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt index da7b6d29cb..daeacc5b9d 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt @@ -634,7 +634,7 @@ class RecipientPresenter( recipientMvpView.launchUserInteractionPendingIntent(pendingIntent, requestCode) } - override fun saveAndPersist(key: Long) { + override fun saveKeyId(key: Long) { account.openPgpKey = key preferences.saveAccount(account) openPgpKeySetupUtil = null diff --git a/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeySetupUtil.java b/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeySetupUtil.java index 4ca3bc8425..fd59d045ca 100644 --- a/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeySetupUtil.java +++ b/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeySetupUtil.java @@ -44,7 +44,7 @@ public class OpenPgpKeySetupUtil implements OpenPgpApiManagerCallback { private long keyId; private boolean pendingIntentRunImmediately; private Intent cachedActivityResultData; - private PendingIntent pendingIntentSelectKey; + private PendingIntent pendingIntent; private OpenPgpKeySetupCallBack callBack; public OpenPgpKeySetupUtil(OpenPgpApiManager openPgpApiManager, String openPgpProvider, String userId, boolean showAutocryptHint) { @@ -65,7 +65,7 @@ public class OpenPgpKeySetupUtil implements OpenPgpApiManagerCallback { // it will also work as a regular pending intent. case UI_REQUIRED: case OK: { - apiGetOrStartPendingIntent(); + initializePendingIntent(); break; } default: { @@ -78,9 +78,9 @@ public class OpenPgpKeySetupUtil implements OpenPgpApiManagerCallback { @Override public void onOpenPgpProviderStatusChanged() { if (openPgpApiManager.getOpenPgpProviderState() == OpenPgpProviderState.OK) { - apiRetrievePendingIntentAndKeyInfo(); + retrievePendingIntentInfo(); } else { - pendingIntentSelectKey = null; + pendingIntent = null; pendingIntentRunImmediately = false; cachedActivityResultData = null; } @@ -93,7 +93,7 @@ public class OpenPgpKeySetupUtil implements OpenPgpApiManagerCallback { } } - private void apiRetrievePendingIntentAndKeyInfo() { + private void retrievePendingIntentInfo() { Intent data; if (cachedActivityResultData != null) { @@ -103,10 +103,10 @@ public class OpenPgpKeySetupUtil implements OpenPgpApiManagerCallback { data = new Intent(); } - apiRetrievePendingIntentAndKeyInfo(data); + retrievePendingIntentInfo(data); } - private void apiRetrievePendingIntentAndKeyInfo(Intent data) { + private void retrievePendingIntentInfo(Intent data) { data.setAction(OpenPgpApi.ACTION_GET_SIGN_KEY_ID); data.putExtra(OpenPgpApi.EXTRA_USER_ID, userId); data.putExtra(OpenPgpApi.EXTRA_PRESELECT_KEY_ID, keyId); @@ -120,14 +120,14 @@ public class OpenPgpKeySetupUtil implements OpenPgpApiManagerCallback { switch (resultCode) { case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: { - PendingIntent pendingIntentSelectKey = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); + PendingIntent pendingIntent = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); if (result.hasExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID)) { long keyId = result.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, NO_KEY); - updateWidgetData(keyId, pendingIntentSelectKey); + updateData(keyId, pendingIntent); } else { - updateWidgetData(pendingIntentSelectKey); + updateData(pendingIntent); } break; @@ -141,41 +141,44 @@ public class OpenPgpKeySetupUtil implements OpenPgpApiManagerCallback { } }; - private void apiGetOrStartPendingIntent() { - if (pendingIntentSelectKey != null) { - apiStartPendingIntent(); + /** + * if pendingIntent present, then start it, else retrieve pendingIntent info by calling openPgpApi + */ + private void initializePendingIntent() { + if (pendingIntent != null) { + startPendingIntent(); return; } pendingIntentRunImmediately = true; - apiRetrievePendingIntentAndKeyInfo(); + retrievePendingIntentInfo(); } - private void apiStartPendingIntent() { - if (pendingIntentSelectKey == null) { + private void startPendingIntent() { + if (pendingIntent == null) { Timber.e("Tried to launch pending intent but didn't have any?"); return; } try { if (callBack != null) { - callBack.startPendingIntentForResult(pendingIntentSelectKey, REQUEST_CODE_KEY_PREFERENCE); + callBack.startPendingIntentForResult(pendingIntent, REQUEST_CODE_KEY_PREFERENCE); } } catch (IntentSender.SendIntentException e) { Timber.e(e,"Error launching pending intent"); } finally { - pendingIntentSelectKey = null; + pendingIntent = null; } } - private void updateWidgetData(PendingIntent pendingIntentSelectKey) { - this.pendingIntentSelectKey = pendingIntentSelectKey; + private void updateData(PendingIntent pendingIntent) { + this.pendingIntent = pendingIntent; maybeRunPendingIntentImmediately(); } - private void updateWidgetData(long keyId, PendingIntent pendingIntentSelectKey) { - setAndPersist(keyId); - this.pendingIntentSelectKey = pendingIntentSelectKey; + private void updateData(long keyId, PendingIntent pendingIntent) { + setKeyId(keyId); + this.pendingIntent = pendingIntent; maybeRunPendingIntentImmediately(); } @@ -186,10 +189,10 @@ public class OpenPgpKeySetupUtil implements OpenPgpApiManagerCallback { } pendingIntentRunImmediately = false; - apiStartPendingIntent(); + startPendingIntent(); } - private void setAndPersist(long newValue) { + private void setKeyId(long newValue) { if (newValue == 0L) { return; } @@ -197,7 +200,7 @@ public class OpenPgpKeySetupUtil implements OpenPgpApiManagerCallback { keyId = newValue; if (callBack != null) { - callBack.saveAndPersist(keyId); + callBack.saveKeyId(keyId); } } @@ -215,7 +218,7 @@ public class OpenPgpKeySetupUtil implements OpenPgpApiManagerCallback { OpenPgpProviderState openPgpProviderState = openPgpApiManager.getOpenPgpProviderState(); if (openPgpProviderState == OpenPgpProviderState.OK || openPgpProviderState == OpenPgpProviderState.UI_REQUIRED) { - apiRetrievePendingIntentAndKeyInfo(); + retrievePendingIntentInfo(); } } return true; @@ -228,6 +231,6 @@ public class OpenPgpKeySetupUtil implements OpenPgpApiManagerCallback { void startPendingIntentForResult(PendingIntent pendingIntent, int requestCode) throws IntentSender.SendIntentException; - void saveAndPersist(long key); + void saveKeyId(long key); } } -- GitLab From 14954b32636acdfb4af8c0e62880e68b5f5d7d94 Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Tue, 2 Aug 2022 15:17:04 +0000 Subject: [PATCH 5/7] Apply 1 suggestion(s) to 1 file(s) --- .../java/com/fsck/k9/activity/compose/RecipientPresenter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt index daeacc5b9d..324670c8da 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt @@ -630,7 +630,7 @@ class RecipientPresenter( val userId = OpenPgpApiHelper.buildUserId(account.getIdentity(0)) openPgpKeySetupUtil = OpenPgpKeySetupUtil(openPgpApiManager, account.openPgpProvider, userId, true) openPgpKeySetupUtil?.setCallBack(object : OpenPgpKeySetupUtil.OpenPgpKeySetupCallBack { - override fun startPendingIntentForResult( pendingIntent: PendingIntent?, requestCode: Int) { + override fun startPendingIntentForResult(pendingIntent: PendingIntent?, requestCode: Int) { recipientMvpView.launchUserInteractionPendingIntent(pendingIntent, requestCode) } -- GitLab From 7cd352f0b85c9c882bf3e695bb11f78f60ea1e5a Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Tue, 2 Aug 2022 21:19:46 +0600 Subject: [PATCH 6/7] apply code review suggestions --- .../com/fsck/k9/activity/MessageCompose.java | 4 +- ...ner.java => OpenPgpConfigureCallBack.java} | 2 +- .../activity/compose/PgpKeySetupDialog.java | 4 +- .../compose/PgpUnconfiguredDialog.java | 4 +- .../k9/activity/compose/RecipientPresenter.kt | 42 ++++++++++--------- 5 files changed, 29 insertions(+), 27 deletions(-) rename app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/{OpenPgpConfigureListener.java => OpenPgpConfigureCallBack.java} (94%) diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java index a9b575108a..db03b45906 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java @@ -62,7 +62,7 @@ import com.fsck.k9.activity.compose.ComposeCryptoStatus; import com.fsck.k9.activity.compose.ComposeCryptoStatus.SendErrorState; import com.fsck.k9.activity.compose.IdentityAdapter; import com.fsck.k9.activity.compose.IdentityAdapter.IdentityContainer; -import com.fsck.k9.activity.compose.OpenPgpConfigureListener; +import com.fsck.k9.activity.compose.OpenPgpConfigureCallBack; import com.fsck.k9.activity.compose.PgpEnabledErrorDialog.OnOpenPgpDisableListener; import com.fsck.k9.activity.compose.PgpInlineDialog.OnOpenPgpInlineChangeListener; import com.fsck.k9.activity.compose.PgpSignOnlyDialog.OnOpenPgpSignOnlyChangeListener; @@ -128,7 +128,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, CancelListener, AttachmentDownloadCancelListener, OnFocusChangeListener, OnOpenPgpInlineChangeListener, OnOpenPgpSignOnlyChangeListener, MessageBuilder.Callback, AttachmentPresenter.AttachmentsChangedListener, OnOpenPgpDisableListener, PermissionUiHelper, - OpenPgpConfigureListener { + OpenPgpConfigureCallBack { private static final int DIALOG_SAVE_OR_DISCARD_DRAFT_MESSAGE = 1; private static final int DIALOG_CONFIRM_DISCARD_ON_BACK = 2; diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/OpenPgpConfigureListener.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/OpenPgpConfigureCallBack.java similarity index 94% rename from app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/OpenPgpConfigureListener.java rename to app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/OpenPgpConfigureCallBack.java index cc20700f51..3b23077c45 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/OpenPgpConfigureListener.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/OpenPgpConfigureCallBack.java @@ -16,7 +16,7 @@ package com.fsck.k9.activity.compose; -public interface OpenPgpConfigureListener { +public interface OpenPgpConfigureCallBack { void initializePgpSetup(); } diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpKeySetupDialog.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpKeySetupDialog.java index 35a0fec2d4..d9fe12ee44 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpKeySetupDialog.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpKeySetupDialog.java @@ -53,11 +53,11 @@ public class PgpKeySetupDialog extends HighlightDialogFragment { .setView(view); builder.setPositiveButton(R.string.openpgp_dialog_create_key_pair, (dialog, which) -> { - if (!(getActivity() instanceof OpenPgpConfigureListener)) { + if (!(getActivity() instanceof OpenPgpConfigureCallBack)) { return; } - ((OpenPgpConfigureListener) getActivity()).initializePgpSetup(); + ((OpenPgpConfigureCallBack) getActivity()).initializePgpSetup(); dialog.dismiss(); }); diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java index 77c12a2714..f1a69c8662 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/PgpUnconfiguredDialog.java @@ -53,11 +53,11 @@ public class PgpUnconfiguredDialog extends HighlightDialogFragment { .setView(view); builder.setPositiveButton(R.string.openpgp_dialog_proceed, (dialog, which) -> { - if (!(getActivity() instanceof OpenPgpConfigureListener)) { + if (!(getActivity() instanceof OpenPgpConfigureCallBack)) { return; } - ((OpenPgpConfigureListener) getActivity()).initializePgpSetup(); + ((OpenPgpConfigureCallBack) getActivity()).initializePgpSetup(); dialog.dismiss(); }); diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt index 324670c8da..51429325c2 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt @@ -589,29 +589,31 @@ class RecipientPresenter( private fun configureOpenPgp() { setUpPgpProvider() - account.openPgpProvider?.let { - recipientMvpView.setCryptoProvider(it) - openPgpApiManager.setOpenPgpProvider(it, object : OpenPgpApiManagerCallback { - override fun onOpenPgpProviderStatusChanged() { - if (openPgpApiManager.openPgpProviderState == OpenPgpProviderState.UI_REQUIRED) { - val pendingIntent = openPgpApiManager.userInteractionPendingIntent - recipientMvpView.launchUserInteractionPendingIntent(pendingIntent, OPENPGP_USER_INTERACTION) - return - } - - if (isInPgpKeySetupState()) { - initializeUserPgpKeySetup() - return - } - - asyncUpdateCryptoStatus() + if(account.openPgpProvider == null) { + return + } + + recipientMvpView.setCryptoProvider(account.openPgpProvider) + openPgpApiManager.setOpenPgpProvider(account.openPgpProvider, object : OpenPgpApiManagerCallback { + override fun onOpenPgpProviderStatusChanged() { + if (openPgpApiManager.openPgpProviderState == OpenPgpProviderState.UI_REQUIRED) { + val pendingIntent = openPgpApiManager.userInteractionPendingIntent + recipientMvpView.launchUserInteractionPendingIntent(pendingIntent, OPENPGP_USER_INTERACTION) + return } - override fun onOpenPgpProviderError(error: OpenPgpProviderError) { - openPgpCallback.onOpenPgpProviderError(error) + if (isInPgpKeySetupState()) { + initializeUserPgpKeySetup() + return } - }) - } + + asyncUpdateCryptoStatus() + } + + override fun onOpenPgpProviderError(error: OpenPgpProviderError) { + openPgpCallback.onOpenPgpProviderError(error) + } + }) } private fun isInPgpKeySetupState() = (openPgpApiManager.openPgpProviderState == OpenPgpProviderState.OK && account.openPgpKey == Account.NO_OPENPGP_KEY) -- GitLab From ef2c0dbe0a8668c6c610521f445971976fd69a1d Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 3 Aug 2022 12:20:55 +0600 Subject: [PATCH 7/7] rename setCallback method to registerCallBack --- .../java/com/fsck/k9/activity/compose/RecipientPresenter.kt | 2 +- .../java/org/openintents/openpgp/util/OpenPgpKeySetupUtil.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt index 51429325c2..03bd933f13 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.kt @@ -631,7 +631,7 @@ class RecipientPresenter( } val userId = OpenPgpApiHelper.buildUserId(account.getIdentity(0)) openPgpKeySetupUtil = OpenPgpKeySetupUtil(openPgpApiManager, account.openPgpProvider, userId, true) - openPgpKeySetupUtil?.setCallBack(object : OpenPgpKeySetupUtil.OpenPgpKeySetupCallBack { + openPgpKeySetupUtil?.registerCallBack(object : OpenPgpKeySetupUtil.OpenPgpKeySetupCallBack { override fun startPendingIntentForResult(pendingIntent: PendingIntent?, requestCode: Int) { recipientMvpView.launchUserInteractionPendingIntent(pendingIntent, requestCode) } diff --git a/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeySetupUtil.java b/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeySetupUtil.java index fd59d045ca..d591504522 100644 --- a/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeySetupUtil.java +++ b/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeySetupUtil.java @@ -54,7 +54,7 @@ public class OpenPgpKeySetupUtil implements OpenPgpApiManagerCallback { this.showAutocryptHint = showAutocryptHint; } - public void setCallBack(OpenPgpKeySetupCallBack callBack) { + public void registerCallBack(OpenPgpKeySetupCallBack callBack) { this.callBack = callBack; } -- GitLab