diff options
Diffstat (limited to 'src')
4 files changed, 75 insertions, 26 deletions
diff --git a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java index 27689027..1d80690c 100644 --- a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java +++ b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java @@ -9,6 +9,7 @@ import java.util.Iterator; import java.util.List; import eu.siacs.conversations.Config; +import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.generator.AbstractGenerator; @@ -217,7 +218,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { public class Query { private int totalCount = 0; - private int count = 0; + private int messageCount = 0; private long start; private long end; private Jid with = null; @@ -295,7 +296,10 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { public void callback() { if (this.callback != null) { - this.callback.onMoreMessagesLoaded(count,conversation); + this.callback.onMoreMessagesLoaded(messageCount,conversation); + if (messageCount==0) { + this.callback.informUser(R.string.no_more_history_on_server); + } } } @@ -316,7 +320,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { } public void incrementMessageCount() { - this.count++; + this.messageCount++; } public int getTotalCount() { diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 4b09ac9d..39babece 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -961,29 +961,33 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa }); } - public void loadMoreMessages(Conversation conversation, long timestamp, final OnMoreMessagesLoaded callback) { - if (this.getMessageArchiveService().queryInProgress(conversation)) { - Log.d(Config.LOGTAG,"query in progress"); + public void loadMoreMessages(final Conversation conversation, final long timestamp, final OnMoreMessagesLoaded callback) { + Log.d(Config.LOGTAG,"load more messages for "+conversation.getName() + " prior to "+MessageGenerator.getTimestamp(timestamp)); + if (XmppConnectionService.this.getMessageArchiveService().queryInProgress(conversation)) { return; } - List<Message> messages = databaseBackend.getMessages(conversation, 50,timestamp); - if (messages.size() == 0 && (conversation.getAccount().getXmppConnection() != null && conversation.getAccount().getXmppConnection().getFeatures().mam())) { - Log.d(Config.LOGTAG,"load more messages with mam"); - MessageArchiveService.Query query = getMessageArchiveService().query(conversation,0,timestamp - 1); - if (query != null) { - query.setCallback(callback); + new Thread(new Runnable() { + @Override + public void run() { + final Account account = conversation.getAccount(); + List<Message> messages = databaseBackend.getMessages(conversation, 50,timestamp); + if (messages.size() > 0) { + conversation.addAll(0, messages); + callback.onMoreMessagesLoaded(messages.size(), conversation); + } else if (account.getStatus() == Account.State.ONLINE && account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) { + MessageArchiveService.Query query = getMessageArchiveService().query(conversation,0,timestamp - 1); + if (query != null) { + query.setCallback(callback); + } + callback.informUser(R.string.fetching_history_from_server); + } } - return; - } - for (Message message : messages) { - message.setConversation(conversation); - } - conversation.addAll(0, messages); - callback.onMoreMessagesLoaded(messages.size(),conversation); + }).start(); } public interface OnMoreMessagesLoaded { public void onMoreMessagesLoaded(int count,Conversation conversation); + public void informUser(int r); } public List<Account> getAccounts() { diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index c333bab7..418d9bbf 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -105,7 +105,8 @@ public class ConversationFragment extends Fragment { private RelativeLayout snackbar; private TextView snackbarMessage; private TextView snackbarAction; - private boolean messagesLoaded = false; + private boolean messagesLoaded = true; + private Toast messageLoaderToast; private OnScrollListener mOnScrollListener = new OnScrollListener() { @@ -119,10 +120,9 @@ public class ConversationFragment extends Fragment { public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { synchronized (ConversationFragment.this.messageList) { - if (firstVisibleItem == 0 && messagesLoaded) { + if (firstVisibleItem < 5 && messagesLoaded && messageList.size() > 0) { long timestamp = ConversationFragment.this.messageList.get(0).getTimeSent(); messagesLoaded = false; - Log.d(Config.LOGTAG,"load more messages"); activity.xmppConnectionService.loadMoreMessages(conversation, timestamp, new XmppConnectionService.OnMoreMessagesLoaded() { @Override public void onMoreMessagesLoaded(final int count, Conversation conversation) { @@ -132,18 +132,53 @@ public class ConversationFragment extends Fragment { activity.runOnUiThread(new Runnable() { @Override public void run() { - int firstItem = messagesView.getFirstVisiblePosition(); - Log.d(Config.LOGTAG, "done loading more messages. first item: " + firstItem); + final int oldPosition = messagesView.getFirstVisiblePosition(); + View v = messagesView.getChildAt(0); + final int pxOffset = (v == null) ? 0 : v.getTop(); ConversationFragment.this.conversation.populateWithMessages(ConversationFragment.this.messageList); updateStatusMessages(); messageListAdapter.notifyDataSetChanged(); if (count != 0) { + final int newPosition = oldPosition + count; + int offset = 0; + try { + Message tmpMessage = messageList.get(newPosition); + + while(tmpMessage.wasMergedIntoPrevious()) { + offset++; + tmpMessage = tmpMessage.prev(); + } + } catch (final IndexOutOfBoundsException ignored) { + + } + messagesView.setSelectionFromTop(newPosition - offset, pxOffset); messagesLoaded = true; + if (messageLoaderToast != null) { + messageLoaderToast.cancel(); + } } - messagesView.setSelectionFromTop(firstItem + count, 0); } }); } + + @Override + public void informUser(final int resId) { + + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + if (messageLoaderToast != null) { + messageLoaderToast.cancel(); + } + if (ConversationFragment.this.conversation != conversation) { + return; + } + messageLoaderToast = Toast.makeText(activity,resId,Toast.LENGTH_LONG); + messageLoaderToast.show(); + } + }); + + } }); } @@ -531,6 +566,11 @@ public class ConversationFragment extends Fragment { this.mEditMessage.append(this.conversation.getNextMessage()); this.messagesView.invalidate(); updateMessages(); + this.messagesLoaded = true; + int size = this.messageList.size(); + if (size > 0) { + messagesView.setSelection(size - 1); + } } public void updateMessages() { @@ -599,7 +639,6 @@ public class ConversationFragment extends Fragment { } } conversation.populateWithMessages(ConversationFragment.this.messageList); - this.messagesLoaded = this.messageList.size() > 0; for (Message message : this.messageList) { if (message.getEncryption() == Message.ENCRYPTION_PGP && (message.getStatus() == Message.STATUS_RECEIVED || message diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 02be6348..ec761d6e 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -366,4 +366,6 @@ <string name="reset">Reset</string> <string name="account_image_description">Account avatar</string> <string name="copy_otr_clipboard_description">Copy OTR fingerprint to clipboard</string> + <string name="fetching_history_from_server">Fetching history from server</string> + <string name="no_more_history_on_server">No more history on server</string> </resources> |