kopia lustrzana https://github.com/ryukoposting/Signal-Android
Enqueue cached layout inflation on background thread.
rodzic
7efd8be238
commit
221cf56ddc
|
@ -13,12 +13,15 @@ import androidx.annotation.Nullable;
|
||||||
import androidx.asynclayoutinflater.view.AsyncLayoutInflater;
|
import androidx.asynclayoutinflater.view.AsyncLayoutInflater;
|
||||||
|
|
||||||
import org.thoughtcrime.securesms.logging.Log;
|
import org.thoughtcrime.securesms.logging.Log;
|
||||||
|
import org.thoughtcrime.securesms.util.concurrent.SerialExecutor;
|
||||||
|
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class that can be used to pre-cache layouts. Usage flow:
|
* A class that can be used to pre-cache layouts. Usage flow:
|
||||||
|
@ -82,6 +85,7 @@ public class CachedInflater {
|
||||||
private static class ViewCache {
|
private static class ViewCache {
|
||||||
|
|
||||||
private static final ViewCache INSTANCE = new ViewCache();
|
private static final ViewCache INSTANCE = new ViewCache();
|
||||||
|
private static final Executor ENQUEUING_EXECUTOR = new SerialExecutor(SignalExecutors.BOUNDED);
|
||||||
|
|
||||||
private final Map<Integer, List<View>> cache = new HashMap<>();
|
private final Map<Integer, List<View>> cache = new HashMap<>();
|
||||||
|
|
||||||
|
@ -109,10 +113,17 @@ public class CachedInflater {
|
||||||
|
|
||||||
int existingCount = Util.getOrDefault(cache, layoutRes, Collections.emptyList()).size();
|
int existingCount = Util.getOrDefault(cache, layoutRes, Collections.emptyList()).size();
|
||||||
int inflateCount = Math.max(limit - existingCount, 0);
|
int inflateCount = Math.max(limit - existingCount, 0);
|
||||||
|
long enqueueTime = System.currentTimeMillis();
|
||||||
|
|
||||||
for (int i = 0; i < inflateCount; i++) {
|
// Calling AsyncLayoutInflator#inflate can block the calling thread when there's a large number of requests.
|
||||||
final long enqueueTime = System.currentTimeMillis();
|
// The method is annotated @UiThread, but unnecessarily so.
|
||||||
inflater.inflate(layoutRes, parent, (view, resId, p) -> {
|
ENQUEUING_EXECUTOR.execute(() -> {
|
||||||
|
if (enqueueTime < lastClearTime) {
|
||||||
|
Log.d(TAG, "Prefetch is no longer valid. Ignoring " + inflateCount + " inflates.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
AsyncLayoutInflater.OnInflateFinishedListener onInflateFinishedListener = (view, resId, p) -> {
|
||||||
Util.assertMainThread();
|
Util.assertMainThread();
|
||||||
if (enqueueTime < lastClearTime) {
|
if (enqueueTime < lastClearTime) {
|
||||||
Log.d(TAG, "Prefetch is no longer valid. Ignoring.");
|
Log.d(TAG, "Prefetch is no longer valid. Ignoring.");
|
||||||
|
@ -125,8 +136,12 @@ public class CachedInflater {
|
||||||
views.add(view);
|
views.add(view);
|
||||||
|
|
||||||
cache.put(resId, views);
|
cache.put(resId, views);
|
||||||
});
|
};
|
||||||
|
|
||||||
|
for (int i = 0; i < inflateCount; i++) {
|
||||||
|
inflater.inflate(layoutRes, parent, onInflateFinishedListener);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainThread
|
@MainThread
|
||||||
|
|
Ładowanie…
Reference in New Issue