From f98888d796728b5ef1fea1ad1db19ca60df21fbc Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 4 May 2017 13:02:46 +0200 Subject: [PATCH] display open pgp key id in account details and allow to delete. fixes #2470 --- .../siacs/conversations/crypto/PgpEngine.java | 3 - .../siacs/conversations/entities/Account.java | 6 +- .../ui/ContactDetailsActivity.java | 13 +---- .../ui/ConversationFragment.java | 2 + .../conversations/ui/EditAccountActivity.java | 58 +++++++++++++++++++ .../siacs/conversations/ui/XmppActivity.java | 12 ++++ src/main/res/layout/activity_edit_account.xml | 52 ++++++++++++++++- src/main/res/values/strings.xml | 2 + 8 files changed, 131 insertions(+), 17 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/crypto/PgpEngine.java b/src/main/java/eu/siacs/conversations/crypto/PgpEngine.java index 1a5367fd5..3c406973e 100644 --- a/src/main/java/eu/siacs/conversations/crypto/PgpEngine.java +++ b/src/main/java/eu/siacs/conversations/crypto/PgpEngine.java @@ -304,9 +304,6 @@ public class PgpEngine { } } - public PendingIntent getIntentForKey(Contact contact) { - return getIntentForKey(contact.getPgpKeyId()); - } public PendingIntent getIntentForKey(long pgpKeyId) { Intent params = new Intent(); diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java index 5728c87b2..ee8a7c55f 100644 --- a/src/main/java/eu/siacs/conversations/entities/Account.java +++ b/src/main/java/eu/siacs/conversations/entities/Account.java @@ -551,7 +551,11 @@ public class Account extends AbstractEntity { public boolean setPgpSignId(long pgpID) { synchronized (this.keys) { try { - keys.put(KEY_PGP_ID, pgpID); + if (pgpID == 0) { + keys.remove(KEY_PGP_ID); + } else { + keys.put(KEY_PGP_ID, pgpID); + } } catch (JSONException e) { return false; } diff --git a/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java b/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java index 313c7dea2..7c05bd960 100644 --- a/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java @@ -1,11 +1,9 @@ package eu.siacs.conversations.ui; import android.app.AlertDialog; -import android.app.PendingIntent; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.content.IntentSender.SendIntentException; import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; @@ -451,7 +449,7 @@ public class ContactDetailsActivity extends OmemoActivity implements OnAccountUp .findViewById(R.id.button_remove); removeButton.setVisibility(View.VISIBLE); key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint)); - if (otrFingerprint != null && otrFingerprint.equals(messageFingerprint)) { + if (otrFingerprint != null && otrFingerprint.equalsIgnoreCase(messageFingerprint)) { keyType.setText(R.string.otr_fingerprint_selected_message); keyType.setTextColor(ContextCompat.getColor(this, R.color.accent)); } else { @@ -509,14 +507,7 @@ public class ContactDetailsActivity extends OmemoActivity implements OnAccountUp @Override public void onClick(View v) { - PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService.getPgpEngine(); - try { - startIntentSenderForResult( - pgp.getIntentForKey(contact).getIntentSender(), 0, null, 0, - 0, 0); - } catch (Throwable e) { - Toast.makeText(ContactDetailsActivity.this,R.string.openpgp_error,Toast.LENGTH_SHORT).show(); - } + launchOpenKeyChain(contact.getPgpKeyId()); } }; view.setOnClickListener(openKey); diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index c180dd9c5..d5a765d81 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -514,6 +514,8 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { fingerprint = "pgp"; + } else if (message.getEncryption() == Message.ENCRYPTION_OTR) { + fingerprint = "otr"; } else { fingerprint = message.getFingerprint(); } diff --git a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java index 88fb59db4..aa9584f10 100644 --- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java @@ -36,6 +36,8 @@ import android.widget.TableRow; import android.widget.TextView; import android.widget.Toast; +import org.openintents.openpgp.util.OpenPgpUtils; + import java.util.Arrays; import java.util.List; import java.util.Set; @@ -93,13 +95,18 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat private TextView mSessionEst; private TextView mOtrFingerprint; private TextView mAxolotlFingerprint; + private TextView mPgpFingerprint; private TextView mOwnFingerprintDesc; + private TextView mOtrFingerprintDesc; + private TextView getmPgpFingerprintDesc; private TextView mAccountJidLabel; private ImageView mAvatar; private RelativeLayout mOtrFingerprintBox; private RelativeLayout mAxolotlFingerprintBox; + private RelativeLayout mPgpFingerprintBox; private ImageButton mOtrFingerprintToClipboardButton; private ImageButton mAxolotlFingerprintToClipboardButton; + private ImageButton mPgpDeleteFingerprintButton; private LinearLayout keys; private LinearLayout keysCard; private LinearLayout mNamePort; @@ -505,7 +512,12 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat this.mServerInfoHttpUpload = (TextView) findViewById(R.id.server_info_http_upload); this.mPushRow = (TableRow) findViewById(R.id.push_row); this.mServerInfoPush = (TextView) findViewById(R.id.server_info_push); + this.mPgpFingerprintBox = (RelativeLayout) findViewById(R.id.pgp_fingerprint_box); + this.mPgpFingerprint = (TextView) findViewById(R.id.pgp_fingerprint); + this.getmPgpFingerprintDesc = (TextView) findViewById(R.id.pgp_fingerprint_desc); + this.mPgpDeleteFingerprintButton = (ImageButton) findViewById(R.id.action_delete_pgp); this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint); + this.mOtrFingerprintDesc = (TextView) findViewById(R.id.otr_fingerprint_desc); this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box); this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard); this.mAxolotlFingerprint = (TextView) findViewById(R.id.axolotl_fingerprint); @@ -896,8 +908,36 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat } else { this.mServerInfoPush.setText(R.string.server_info_unavailable); } + final long pgpKeyId = this.mAccount.getPgpId(); + if (pgpKeyId != 0 && Config.supportOpenPgp()) { + OnClickListener openPgp = new OnClickListener() { + @Override + public void onClick(View view) { + launchOpenKeyChain(pgpKeyId); + } + }; + OnClickListener delete = new OnClickListener() { + @Override + public void onClick(View view) { + showDeletePgpDialog(); + } + }; + this.mPgpFingerprintBox.setVisibility(View.VISIBLE); + this.mPgpFingerprint.setText(OpenPgpUtils.convertKeyIdToHex(pgpKeyId)); + this.mPgpFingerprint.setOnClickListener(openPgp); + if ("pgp".equals(messageFingerprint)) { + this.getmPgpFingerprintDesc.setTextColor(ContextCompat.getColor(this, R.color.accent)); + } + this.getmPgpFingerprintDesc.setOnClickListener(openPgp); + this.mPgpDeleteFingerprintButton.setOnClickListener(delete); + } else { + this.mPgpFingerprintBox.setVisibility(View.GONE); + } final String otrFingerprint = this.mAccount.getOtrFingerprint(); if (otrFingerprint != null && Config.supportOtr()) { + if ("otr".equals(messageFingerprint)) { + this.mOtrFingerprintDesc.setTextColor(ContextCompat.getColor(this, R.color.accent)); + } this.mOtrFingerprintBox.setVisibility(View.VISIBLE); this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(otrFingerprint)); this.mOtrFingerprintToClipboardButton @@ -986,6 +1026,24 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat } } + private void showDeletePgpDialog() { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.unpublish_pgp); + builder.setMessage(R.string.unpublish_pgp_message); + builder.setNegativeButton(R.string.cancel,null); + builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + mAccount.setPgpSignId(0); + mAccount.unsetPgpSignature(); + xmppConnectionService.databaseBackend.updateAccount(mAccount); + xmppConnectionService.sendPresence(mAccount); + refreshUiReal(); + } + }); + builder.create().show(); + } + private void showOsOptimizationWarning(boolean showBatteryWarning, boolean showDataSaverWarning) { this.mOsOptimizations.setVisibility(showBatteryWarning || showDataSaverWarning ? View.VISIBLE : View.GONE); if (showDataSaverWarning) { diff --git a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java index f16582943..758913887 100644 --- a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java @@ -66,6 +66,7 @@ import java.util.concurrent.atomic.AtomicInteger; import eu.siacs.conversations.Config; import eu.siacs.conversations.R; +import eu.siacs.conversations.crypto.PgpEngine; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; @@ -1017,6 +1018,17 @@ public abstract class XmppActivity extends Activity { } } + protected void launchOpenKeyChain(long keyId) { + PgpEngine pgp = XmppActivity.this.xmppConnectionService.getPgpEngine(); + try { + startIntentSenderForResult( + pgp.getIntentForKey(keyId).getIntentSender(), 0, null, 0, + 0, 0); + } catch (Throwable e) { + Toast.makeText(XmppActivity.this,R.string.openpgp_error,Toast.LENGTH_SHORT).show(); + } + } + @Override public void onResume() { super.onResume(); diff --git a/src/main/res/layout/activity_edit_account.xml b/src/main/res/layout/activity_edit_account.xml index 6c26a162c..9919f9f3a 100644 --- a/src/main/res/layout/activity_edit_account.xml +++ b/src/main/res/layout/activity_edit_account.xml @@ -475,7 +475,7 @@ @@ -484,6 +484,52 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" + android:layout_centerVertical="true" + android:layout_toLeftOf="@+id/action_delete_pgp" + android:orientation="vertical"> + + + + + + + + + + + + @@ -497,6 +543,7 @@ android:typeface="monospace"/> + android:layout_marginTop="24dp"> diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index fe2f32abb..6ab64c84e 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -171,6 +171,8 @@ Temporarily disable Publish avatar Publish OpenPGP public key + Remove OpenPGP public key + Are you sure you want to remove your OpenPGP public key from your presence announcement?\nYour contacts will no longer be able to send you OpenPGP encrypted messages. OpenPGP public key has been published. Remember to republish your OpenPGP public keys! Enable account