From 74ab36fda2d977e1b97b755a8298c488e6d74ba2 Mon Sep 17 00:00:00 2001 From: Andreas Straub Date: Thu, 30 Jul 2015 19:16:58 +0200 Subject: Fix session logic: enforce same type of encryption --- .../eu/siacs/conversations/entities/Message.java | 31 ++++++++++++++++++++++ .../conversations/ui/adapter/MessageAdapter.java | 28 +++---------------- 2 files changed, 34 insertions(+), 25 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index 99b78d14..e6687701 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -709,4 +709,35 @@ public class Message extends AbstractEntity { return conversation.getAccount().getAxolotlService().getFingerprintTrust(axolotlFingerprint) == SQLiteAxolotlStore.Trust.TRUSTED; } + + private int getPreviousEncryption() { + for (Message iterator = this.prev(); iterator != null; iterator = iterator.prev()){ + if( iterator.isCarbon() || iterator.getStatus() == STATUS_RECEIVED ) { + continue; + } + return iterator.getEncryption(); + } + return ENCRYPTION_NONE; + } + + private int getNextEncryption() { + for (Message iterator = this.next(); iterator != null; iterator = iterator.next()){ + if( iterator.isCarbon() || iterator.getStatus() == STATUS_RECEIVED ) { + continue; + } + return iterator.getEncryption(); + } + return conversation.getNextEncryption(false); + } + + public boolean isValidInSession() { + int pastEncryption = this.getPreviousEncryption(); + int futureEncryption = this.getNextEncryption(); + + boolean inUnencryptedSession = pastEncryption == ENCRYPTION_NONE + || futureEncryption == ENCRYPTION_NONE + || pastEncryption != futureEncryption; + + return inUnencryptedSession || this.getEncryption() == pastEncryption; + } } diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index 4d1260e8..76da42c4 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -569,32 +569,10 @@ public class MessageAdapter extends ArrayAdapter { } if (type == RECEIVED) { - boolean wasEncrypted = false; - for (Message iterator = message.prev(); iterator != null; iterator = iterator.prev()){ - if (iterator.getEncryption() != Message.ENCRYPTION_NONE) { - wasEncrypted = true; - break; - } - if (!iterator.isCarbon() && iterator.getType() == SENT) { - break; - } - } - boolean willBeEncrypted = conversation.getNextEncryption(false) != Message.ENCRYPTION_NONE; - for (Message iterator = message.next(); iterator != null; iterator = iterator.next()){ - if (iterator.getEncryption() != Message.ENCRYPTION_NONE) { - willBeEncrypted = true; - break; - } - if (!iterator.isCarbon() && iterator.getType() == SENT) { - break; - } - } - - if ( willBeEncrypted && wasEncrypted - && message.getEncryption() == Message.ENCRYPTION_NONE) { - viewHolder.message_box.setBackgroundResource(R.drawable.message_bubble_received_warning); - } else { + if(message.isValidInSession()) { viewHolder.message_box.setBackgroundResource(R.drawable.message_bubble_received); + } else { + viewHolder.message_box.setBackgroundResource(R.drawable.message_bubble_received_warning); } } -- cgit v1.2.3