From 17dbdbd0a9da7214d93fb2ca872dd6b971346867 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Thu, 3 May 2018 08:09:57 -0700 Subject: [PATCH] Handle quote position task finishing after fragment detaches. There's a chance that the AsyncTask that retrieves a quoted message's position could finish after the fragment is detached, which would cause a crash. I've changed it so if this case occurs, the result is ignored. Also, I noticed that when searching the message table, if a quote can't be found, we'd end up traversing the entire table. To prevent this from taking forever on large message tables, I've limited it to searchin only what is currently present in the adapter. Fixes #7756 --- .../securesms/ConversationFragment.java | 14 +++++++++++++- .../securesms/database/MmsSmsDatabase.java | 4 ++-- 2 files changed, 15 insertions(+), 3 deletions(-) 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);