From 40ee1a0bfcf2ef15063838a087c9f051c004dc0f Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 25 Jan 2017 18:35:22 +0100 Subject: [PATCH] reset messagesLoaded when changing retention settings --- .../eu/siacs/conversations/entities/Conversation.java | 2 ++ .../conversations/services/MessageArchiveService.java | 1 - .../conversations/services/XmppConnectionService.java | 8 ++++++++ .../siacs/conversations/ui/ConversationActivity.java | 7 ------- .../siacs/conversations/ui/ConversationFragment.java | 11 ++--------- .../eu/siacs/conversations/ui/SettingsActivity.java | 2 ++ .../conversations/ui/adapter/MessageAdapter.java | 2 +- 7 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index c686a676f..b22d9f275 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -22,6 +22,7 @@ import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Locale; +import java.util.concurrent.atomic.AtomicBoolean; import eu.siacs.conversations.Config; import eu.siacs.conversations.crypto.PgpDecryptionService; @@ -91,6 +92,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl private String mLastReceivedOtrMessageId = null; private String mFirstMamReference = null; private Message correctingMessage; + public AtomicBoolean messagesLoaded = new AtomicBoolean(true); public boolean hasMessagesLeftOnServer() { return messagesLeftOnServer; diff --git a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java index 92ba7591e..8e4b95b73 100644 --- a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java +++ b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java @@ -113,7 +113,6 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { query.reference = conversation.getFirstMamReference(); Log.d(Config.LOGTAG,"setting mam reference"); } - Log.d(Config.LOGTAG,"checking max of "+start+" end "+mXmppConnectionService.getAutomaticMessageDeletionDate()); query.start = Math.max(start,mXmppConnectionService.getAutomaticMessageDeletionDate()); if (start > end) { 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 9005d8c35..f53d62d3d 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -933,6 +933,10 @@ public class XmppConnectionService extends Service { } private void expireOldMessages() { + expireOldMessages(false); + } + + public void expireOldMessages(final boolean resetHasMessagesLeftOnServer) { mLastExpiryRun.set(SystemClock.elapsedRealtime()); mDatabaseExecutor.execute(new Runnable() { @Override @@ -943,6 +947,10 @@ public class XmppConnectionService extends Service { synchronized (XmppConnectionService.this.conversations) { for (Conversation conversation : XmppConnectionService.this.conversations) { conversation.expireOldMessages(timestamp); + if (resetHasMessagesLeftOnServer) { + conversation.messagesLoaded.set(true); + conversation.setHasMessagesLeftOnServer(true); + } } } updateConversationUi(); diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index 65b817322..25cb0d2d3 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -1787,11 +1787,4 @@ public class ConversationActivity extends XmppActivity public boolean highlightSelectedConversations() { return !isConversationsOverviewHideable() || this.conversationWasSelectedByKeyboard; } - - public void setMessagesLoaded() { - if (mConversationFragment != null) { - mConversationFragment.setMessagesLoaded(); - mConversationFragment.updateMessages(); - } - } } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index e6d995798..dfe2c2704 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -120,7 +120,6 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa private RelativeLayout snackbar; private TextView snackbarMessage; private TextView snackbarAction; - private boolean messagesLoaded = true; private Toast messageLoaderToast; private OnScrollListener mOnScrollListener = new OnScrollListener() { @@ -135,14 +134,13 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { synchronized (ConversationFragment.this.messageList) { - if (firstVisibleItem < 5 && messagesLoaded && messageList.size() > 0) { + if (firstVisibleItem < 5 && conversation != null && conversation.messagesLoaded.compareAndSet(true,false) && messageList.size() > 0) { long timestamp; if (messageList.get(0).getType() == Message.TYPE_STATUS && messageList.size() >= 2) { timestamp = messageList.get(1).getTimeSent(); } else { timestamp = messageList.get(0).getTimeSent(); } - messagesLoaded = false; activity.xmppConnectionService.loadMoreMessages(conversation, timestamp, new XmppConnectionService.OnMoreMessagesLoaded() { @Override public void onMoreMessagesLoaded(final int c, Conversation conversation) { @@ -171,7 +169,6 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa messageListAdapter.notifyDataSetChanged(); int pos = Math.max(getIndexOf(uuid,messageList),0); messagesView.setSelectionFromTop(pos, pxOffset); - messagesLoaded = true; if (messageLoaderToast != null) { messageLoaderToast.cancel(); } @@ -376,10 +373,6 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa private ConversationActivity activity; private Message selectedMessage; - public void setMessagesLoaded() { - this.messagesLoaded = true; - } - private void sendMessage() { final String body = mEditMessage.getText().toString(); if (body.length() == 0 || this.conversation == null) { @@ -913,7 +906,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa messageListAdapter.updatePreferences(); this.messagesView.setAdapter(messageListAdapter); updateMessages(); - this.messagesLoaded = true; + this.conversation.messagesLoaded.set(true); synchronized (this.messageList) { final Message first = conversation.getFirstUnreadMessage(); final int bottom = Math.max(0, this.messageList.size() - 1); diff --git a/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java b/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java index 4126bb3af..da12266ea 100644 --- a/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java @@ -372,6 +372,8 @@ public class SettingsActivity extends XmppActivity implements reconnectAccounts(); } else if (name.equals("use_tor")) { reconnectAccounts(); + } else if (name.equals(AUTOMATIC_MESSAGE_DELETION)) { + xmppConnectionService.expireOldMessages(true); } } 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 4f58059ed..3dbd9f3f9 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -565,7 +565,7 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie if (timestamp == 0) { timestamp = System.currentTimeMillis(); } - activity.setMessagesLoaded(); + conversation.messagesLoaded.set(true); MessageArchiveService.Query query = activity.xmppConnectionService.getMessageArchiveService().query(conversation, 0, timestamp); if (query != null) { Toast.makeText(activity, R.string.fetching_history_from_server, Toast.LENGTH_LONG).show();