diff --git a/src/org/thoughtcrime/securesms/ConversationItem.java b/src/org/thoughtcrime/securesms/ConversationItem.java index 131324045e..422a7503e1 100644 --- a/src/org/thoughtcrime/securesms/ConversationItem.java +++ b/src/org/thoughtcrime/securesms/ConversationItem.java @@ -55,6 +55,7 @@ import org.thoughtcrime.securesms.mms.Slide; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.util.DateUtils; +import java.util.List; import java.util.Locale; import java.util.Set; @@ -369,22 +370,17 @@ public class ConversationItem extends LinearLayout { contactPhoto.setAvatar(recipient, true); contactPhoto.setVisibility(View.VISIBLE); } - - private IdentityKeyMismatch getKeyMismatch(final MessageRecord record) { - if (record.isIdentityMismatchFailure()) { - for (final IdentityKeyMismatch mismatch : record.getIdentityKeyMismatches()) { - if (mismatch.getRecipientId() == record.getIndividualRecipient().getRecipientId()) { - return mismatch; - } - } - } - return null; - } - + /// Event handlers - private void handleKeyExchangeClicked() { - new ConfirmIdentityDialog(context, masterSecret, messageRecord, getKeyMismatch(messageRecord)).show(); + private void handleApproveIdentity() { + List mismatches = messageRecord.getIdentityKeyMismatches(); + + if (mismatches.size() != 1) { + throw new AssertionError("Identity mismatch count: " + mismatches.size()); + } + + new ConfirmIdentityDialog(context, masterSecret, messageRecord, mismatches.get(0)).show(); } private class ThumbnailClickListener implements ThumbnailView.ThumbnailClickListener { @@ -462,12 +458,8 @@ public class ConversationItem extends LinearLayout { intent.putExtra(MessageDetailsActivity.TYPE_EXTRA, messageRecord.isMms() ? MmsSmsDatabase.MMS_TRANSPORT : MmsSmsDatabase.SMS_TRANSPORT); intent.putExtra(MessageDetailsActivity.IS_PUSH_GROUP_EXTRA, groupThread && pushDestination); context.startActivity(intent); - } else if (messageRecord.isKeyExchange() && - !messageRecord.isOutgoing() && - !messageRecord.isProcessedKeyExchange() && - !messageRecord.isStaleKeyExchange()) - { - handleKeyExchangeClicked(); + } else if (!messageRecord.isOutgoing() && messageRecord.isIdentityMismatchFailure()) { + handleApproveIdentity(); } else if (messageRecord.isPendingInsecureSmsFallback()) { handleMessageApproval(); } diff --git a/src/org/thoughtcrime/securesms/database/MessagingDatabase.java b/src/org/thoughtcrime/securesms/database/MessagingDatabase.java index 5bfbe6d179..494e981224 100644 --- a/src/org/thoughtcrime/securesms/database/MessagingDatabase.java +++ b/src/org/thoughtcrime/securesms/database/MessagingDatabase.java @@ -30,6 +30,27 @@ public abstract class MessagingDatabase extends Database implements MmsSmsColumn protected abstract String getTableName(); + public void setMismatchedIdentity(long messageId, final long recipientId, final IdentityKey identityKey) { + List items = new ArrayList() {{ + add(new IdentityKeyMismatch(recipientId, identityKey)); + }}; + + IdentityKeyMismatchList document = new IdentityKeyMismatchList(items); + + SQLiteDatabase database = databaseHelper.getWritableDatabase(); + database.beginTransaction(); + + try { + setDocument(database, messageId, MISMATCHED_IDENTITIES, document); + + database.setTransactionSuccessful(); + } catch (IOException ioe) { + Log.w(TAG, ioe); + } finally { + database.endTransaction(); + } + } + public void addMismatchedIdentity(long messageId, long recipientId, IdentityKey identityKey) { try { addToDocument(messageId, MISMATCHED_IDENTITIES, @@ -133,7 +154,9 @@ public abstract class MessagingDatabase extends Database implements MmsSmsColumn try { return clazz.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + } catch (InstantiationException e) { + throw new AssertionError(e); + } catch (IllegalAccessException e) { throw new AssertionError(e); } diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 5dfad5bfae..ac1f503a65 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -292,12 +292,12 @@ public class PushDecryptJob extends MasterSecretJob { IncomingPreKeyBundleMessage bundleMessage = new IncomingPreKeyBundleMessage(textMessage, encoded); Pair messageAndThreadId = database.insertMessageInbox(masterSecret, bundleMessage); - database.addMismatchedIdentity(messageAndThreadId.first, recipientId, identityKey); + database.setMismatchedIdentity(messageAndThreadId.first, recipientId, identityKey); MessageNotifier.updateNotification(context, masterSecret, messageAndThreadId.second); } else { database.updateMessageBody(masterSecret, smsMessageId.get(), encoded); database.markAsPreKeyBundle(smsMessageId.get()); - database.addMismatchedIdentity(smsMessageId.get(), recipientId, identityKey); + database.setMismatchedIdentity(smsMessageId.get(), recipientId, identityKey); } } catch (InvalidMessageException | InvalidVersionException e) { throw new AssertionError(e);