Fix potential issues with ConversationDataSource boundaries.

This commit is contained in:
Greyson Parrelli 2020-05-15 11:11:27 -04:00 committed by Alex Hart
parent 35a0162d5c
commit e714cb6423
2 changed files with 28 additions and 2 deletions

View File

@ -357,7 +357,7 @@ public class ConversationAdapter<V extends View & BindableConversationItem>
* Momentarily highlights a row at the requested position.
*/
void pulseHighlightItem(int position) {
if (position < getItemCount()) {
if (position >= 0 && position < getItemCount()) {
recordToPulseHighlight = getItem(position);
notifyItemChanged(position);
}
@ -430,7 +430,10 @@ public class ConversationAdapter<V extends View & BindableConversationItem>
pool.setMaxRecycledViews(MESSAGE_TYPE_UPDATE, 5);
}
@MainThread
private void cleanFastRecords() {
Util.assertMainThread();
synchronized (releasedFastRecords) {
Iterator<MessageRecord> recordIterator = fastRecords.iterator();
while (recordIterator.hasNext()) {

View File

@ -16,6 +16,7 @@ import org.thoughtcrime.securesms.util.Util;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Core data source for loading an individual conversation.
@ -58,7 +59,20 @@ class ConversationDataSource extends PositionalDataSource<MessageRecord> {
}
}
callback.onResult(records, params.requestedStartPosition, db.getConversationCount(threadId));
int effectiveCount = records.size() + params.requestedStartPosition;
int totalCount = db.getConversationCount(threadId);
if (effectiveCount > totalCount) {
Log.w(TAG, String.format(Locale.ENGLISH, "Miscalculation! Records: %d, Start Position: %d, Total: %d. Adjusting total.",
records.size(),
params.requestedStartPosition,
totalCount));
totalCount = effectiveCount;
}
records = ensureMultipleOfPageSize(records, params.pageSize, totalCount);
callback.onResult(records, params.requestedStartPosition, totalCount);
Util.runOnMain(dataUpdateCallback::onDataUpdated);
Log.d(TAG, "[Initial Load] " + (System.currentTimeMillis() - start) + " ms" + (isInvalid() ? " -- invalidated" : ""));
@ -84,6 +98,15 @@ class ConversationDataSource extends PositionalDataSource<MessageRecord> {
Log.d(TAG, "[Update] " + (System.currentTimeMillis() - start) + " ms" + (isInvalid() ? " -- invalidated" : ""));
}
private static @NonNull List<MessageRecord> ensureMultipleOfPageSize(@NonNull List<MessageRecord> records, int pageSize, int total) {
if (records.size() != total && records.size() % pageSize != 0) {
int overflow = records.size() % pageSize;
return records.subList(0, records.size() - overflow);
} else {
return records;
}
}
interface DataUpdatedCallback {
void onDataUpdated();
}