diff --git a/src/org/thoughtcrime/securesms/ConversationFragment.java b/src/org/thoughtcrime/securesms/ConversationFragment.java index 1d978e0407..7868f11c85 100644 --- a/src/org/thoughtcrime/securesms/ConversationFragment.java +++ b/src/org/thoughtcrime/securesms/ConversationFragment.java @@ -620,12 +620,24 @@ public class ConversationFragment extends Fragment new AsyncTask() { @Override protected Integer doInBackground(Void... voids) { + if (getActivity() == null || getActivity().isFinishing()) { + Log.w(TAG, "Task to retrieve quote position started after the fragment was detached."); + return 0; + } return DatabaseFactory.getMmsSmsDatabase(getContext()) - .getQuotedMessagePosition(threadId, messageRecord.getQuote().getId(), messageRecord.getQuote().getAuthor()); + .getQuotedMessagePosition(threadId, + messageRecord.getQuote().getId(), + messageRecord.getQuote().getAuthor(), + getListAdapter().getItemCount()); } @Override protected void onPostExecute(Integer position) { + if (getActivity() == null || getActivity().isFinishing()) { + Log.w(TAG, "Task to retrieve quote position finished after the fragment was detached."); + return; + } + if (position >= 0 && position < getListAdapter().getItemCount()) { list.scrollToPosition(position); getListAdapter().pulseHighlightItem(position); diff --git a/src/org/thoughtcrime/securesms/database/MmsSmsDatabase.java b/src/org/thoughtcrime/securesms/database/MmsSmsDatabase.java index 7b688c794d..49215742bc 100644 --- a/src/org/thoughtcrime/securesms/database/MmsSmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/MmsSmsDatabase.java @@ -158,11 +158,11 @@ public class MmsSmsDatabase extends Database { DatabaseFactory.getMmsDatabase(context).incrementReceiptCount(syncMessageId, timestamp, false, true); } - public int getQuotedMessagePosition(long threadId, long quoteId, @NonNull Address address) { + public int getQuotedMessagePosition(long threadId, long quoteId, @NonNull Address address, int limit) { String order = MmsSmsColumns.NORMALIZED_DATE_RECEIVED + " DESC"; String selection = MmsSmsColumns.THREAD_ID + " = " + threadId; - try (Cursor cursor = queryTables(new String[]{ MmsSmsColumns.NORMALIZED_DATE_SENT, MmsSmsColumns.ADDRESS }, selection, order, null)) { + try (Cursor cursor = queryTables(new String[]{ MmsSmsColumns.NORMALIZED_DATE_SENT, MmsSmsColumns.ADDRESS }, selection, order, String.valueOf(limit))) { String serializedAddress = address.serialize(); boolean isOwnNumber = Util.isOwnNumber(context, address);