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
This commit is contained in:
Greyson Parrelli 2018-05-03 08:09:57 -07:00
parent ca8fecea9c
commit 17dbdbd0a9
2 changed files with 15 additions and 3 deletions

View File

@ -620,12 +620,24 @@ public class ConversationFragment extends Fragment
new AsyncTask<Void, Void, Integer>() { new AsyncTask<Void, Void, Integer>() {
@Override @Override
protected Integer doInBackground(Void... voids) { 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()) return DatabaseFactory.getMmsSmsDatabase(getContext())
.getQuotedMessagePosition(threadId, messageRecord.getQuote().getId(), messageRecord.getQuote().getAuthor()); .getQuotedMessagePosition(threadId,
messageRecord.getQuote().getId(),
messageRecord.getQuote().getAuthor(),
getListAdapter().getItemCount());
} }
@Override @Override
protected void onPostExecute(Integer position) { 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()) { if (position >= 0 && position < getListAdapter().getItemCount()) {
list.scrollToPosition(position); list.scrollToPosition(position);
getListAdapter().pulseHighlightItem(position); getListAdapter().pulseHighlightItem(position);

View File

@ -158,11 +158,11 @@ public class MmsSmsDatabase extends Database {
DatabaseFactory.getMmsDatabase(context).incrementReceiptCount(syncMessageId, timestamp, false, true); 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 order = MmsSmsColumns.NORMALIZED_DATE_RECEIVED + " DESC";
String selection = MmsSmsColumns.THREAD_ID + " = " + threadId; 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(); String serializedAddress = address.serialize();
boolean isOwnNumber = Util.isOwnNumber(context, address); boolean isOwnNumber = Util.isOwnNumber(context, address);