From aeaa58071c38d6f1378e586422e9fb8ba7330662 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 30 Mar 2018 21:14:06 +0200 Subject: [PATCH] only mark visible messages as read --- .../conversations/entities/Conversation.java | 15 ++++---- .../services/XmppConnectionService.java | 35 +++++++++++-------- .../ui/ConversationFragment.java | 35 ++++++++++++++++--- .../ui/ConversationsActivity.java | 4 +-- .../ui/interfaces/OnConversationRead.java | 2 +- 5 files changed, 63 insertions(+), 28 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index d33e411cd..bc7afb8b9 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -443,7 +443,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl return (this.messages.size() == 0) || this.messages.get(this.messages.size() - 1).isRead(); } - public List markRead() { + public List markRead(String upToUuid) { final List unread = new ArrayList<>(); synchronized (this.messages) { for (Message message : this.messages) { @@ -451,22 +451,23 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl message.markRead(); unread.add(message); } + if (message.getUuid().equals(upToUuid)) { + return unread; + } } } return unread; } - public Message getLatestMarkableMessage(boolean isPrivateAndNonAnonymousMuc) { - synchronized (this.messages) { - for (int i = this.messages.size() - 1; i >= 0; --i) { - final Message message = this.messages.get(i); + public static Message getLatestMarkableMessage(final List messages, boolean isPrivateAndNonAnonymousMuc) { + for (int i = messages.size() - 1; i >= 0; --i) { + final Message message = messages.get(i); if (message.getStatus() <= Message.STATUS_RECEIVED && (message.markable || isPrivateAndNonAnonymousMuc) && message.getType() != Message.TYPE_PRIVATE) { - return message.isRead() ? null : message; + return message; } } - } return null; } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 312319c43..fbad5a797 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -607,7 +607,7 @@ public class XmppConnectionService extends Service { } try { restoredFromDatabaseLatch.await(); - sendReadMarker(c); + sendReadMarker(c,null); } catch (InterruptedException e) { Log.d(Config.LOGTAG, "unable to process notification read marker for conversation " + c.getName()); } @@ -3116,9 +3116,11 @@ public class XmppConnectionService extends Service { public void markMessage(Message message, int status, String errorMessage) { - if (status == Message.STATUS_SEND_FAILED - && (message.getStatus() == Message.STATUS_SEND_RECEIVED || message - .getStatus() == Message.STATUS_SEND_DISPLAYED)) { + final int c = message.getStatus(); + if (status == Message.STATUS_SEND_FAILED && (c == Message.STATUS_SEND_RECEIVED || c == Message.STATUS_SEND_DISPLAYED)) { + return; + } + if (status == Message.STATUS_SEND_RECEIVED && c == Message.STATUS_SEND_DISPLAYED) { return; } message.setErrorMessage(errorMessage); @@ -3262,15 +3264,19 @@ public class XmppConnectionService extends Service { return null; } - public boolean markRead(final Conversation conversation) { - return markRead(conversation, true); + public boolean markRead(final Conversation conversation, boolean dismiss) { + return markRead(conversation,null,dismiss).size() > 0; } - public boolean markRead(final Conversation conversation, boolean clear) { - if (clear) { + public boolean markRead(final Conversation conversation) { + return markRead(conversation, null, true).size() > 0; + } + + public List markRead(final Conversation conversation, String upToUuid, boolean dismiss) { + if (dismiss) { mNotificationService.clear(conversation); } - final List readMessages = conversation.markRead(); + final List readMessages = conversation.markRead(upToUuid); if (readMessages.size() > 0) { Runnable runnable = () -> { for (Message message : readMessages) { @@ -3279,9 +3285,9 @@ public class XmppConnectionService extends Service { }; mDatabaseWriterExecutor.execute(runnable); updateUnreadCountBadge(); - return true; + return readMessages; } else { - return false; + return readMessages; } } @@ -3298,12 +3304,13 @@ public class XmppConnectionService extends Service { } } - public void sendReadMarker(final Conversation conversation) { + public void sendReadMarker(final Conversation conversation, String upToUuid) { final boolean isPrivateAndNonAnonymousMuc = conversation.getMode() == Conversation.MODE_MULTI && conversation.isPrivateAndNonAnonymous(); - final Message markable = conversation.getLatestMarkableMessage(isPrivateAndNonAnonymousMuc); - if (this.markRead(conversation)) { + final List readMessages = this.markRead(conversation,upToUuid,true); + if (readMessages.size() > 0) { updateConversationUi(); } + final Message markable = Conversation.getLatestMarkableMessage(readMessages, isPrivateAndNonAnonymousMuc); if (confirmMessages() && markable != null && (markable.trusted() || isPrivateAndNonAnonymousMuc) diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 58e23df2c..fecd0715a 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -213,7 +213,9 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke @Override public void onScrollStateChanged(AbsListView view, int scrollState) { - + if (AbsListView.OnScrollListener.SCROLL_STATE_IDLE == scrollState) { + fireReadEvent(); + } } @Override @@ -1547,11 +1549,35 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke getActivity().invalidateOptionsMenu(); }); super.onResume(); + binding.messagesView.post(this::fireReadEvent); + } + + private void fireReadEvent() { if (activity != null && this.conversation != null) { - activity.onConversationRead(this.conversation); + String uuid = getLastVisibleMessageUuid(); + if (uuid != null) { + activity.onConversationRead(this.conversation, uuid); + } } } + private String getLastVisibleMessageUuid() { + if (binding == null) { + return null; + } + int pos = binding.messagesView.getLastVisiblePosition(); + if (pos >= 0) { + Message message = (Message) binding.messagesView.getItemAtPosition(pos); + if (message != null) { + while(message.next() != null && message.next().wasMergedIntoPrevious()) { + message = message.next(); + } + return message.getUuid(); + } + } + return null; + } + private void showErrorMessage(final Message message) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.error_message); @@ -1884,7 +1910,8 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } } - activity.onConversationRead(this.conversation); + + this.binding.messagesView.post(this::fireReadEvent); //TODO if we only do this when this fragment is running on main it won't *bing* in tablet layout which might be unnecessary since we can *see* it activity.xmppConnectionService.getNotificationService().setOpenConversation(this.conversation); return true; @@ -2076,7 +2103,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke this.messageListAdapter.notifyDataSetChanged(); updateChatMsgHint(); if (notifyConversationRead && activity != null) { - activity.onConversationRead(this.conversation); + binding.messagesView.post(this::fireReadEvent); } updateSendButton(); updateEditablity(); diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java index 2528f8a2a..ecd7660c3 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java @@ -592,9 +592,9 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } @Override - public void onConversationRead(Conversation conversation) { + public void onConversationRead(Conversation conversation, String upToUuid) { if (!mActivityPaused && pendingViewIntent.peek() == null) { - xmppConnectionService.sendReadMarker(conversation); + xmppConnectionService.sendReadMarker(conversation, upToUuid); } else { Log.d(Config.LOGTAG, "ignoring read callback. mActivityPaused=" + Boolean.toString(mActivityPaused)); } diff --git a/src/main/java/eu/siacs/conversations/ui/interfaces/OnConversationRead.java b/src/main/java/eu/siacs/conversations/ui/interfaces/OnConversationRead.java index c82b4ca1d..d8e5d8623 100644 --- a/src/main/java/eu/siacs/conversations/ui/interfaces/OnConversationRead.java +++ b/src/main/java/eu/siacs/conversations/ui/interfaces/OnConversationRead.java @@ -32,5 +32,5 @@ package eu.siacs.conversations.ui.interfaces; import eu.siacs.conversations.entities.Conversation; public interface OnConversationRead { - void onConversationRead(Conversation conversation); + void onConversationRead(Conversation conversation, String upToUuid); }