From 8a094e972e686ddd1223371c268f0a4758e0b1ef Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Tue, 13 Aug 2019 13:47:44 -0700 Subject: [PATCH] Adds support to more aggressively pause in progress and completed requests in Glide in response to an onTrimMemory event. PiperOrigin-RevId: 263206711 --- .../main/java/com/bumptech/glide/Glide.java | 5 ++ .../com/bumptech/glide/RequestManager.java | 46 ++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/bumptech/glide/Glide.java b/library/src/main/java/com/bumptech/glide/Glide.java index 4ef954d7ed..20a2be95a0 100644 --- a/library/src/main/java/com/bumptech/glide/Glide.java +++ b/library/src/main/java/com/bumptech/glide/Glide.java @@ -648,6 +648,11 @@ public void clearMemory() { public void trimMemory(int level) { // Engine asserts this anyway when removing resources, fail faster and consistently Util.assertMainThread(); + // Request managers need to be trimmed before the caches and pools, in order for the latter to + // have the most benefit. + for (RequestManager manager : managers) { + manager.onTrimMemory(level); + } // memory cache needs to be trimmed before bitmap pool to trim re-pooled Bitmaps too. See #687. memoryCache.trimMemory(level); bitmapPool.trimMemory(level); diff --git a/library/src/main/java/com/bumptech/glide/RequestManager.java b/library/src/main/java/com/bumptech/glide/RequestManager.java index a33e2b9ec4..cd6b528c5c 100644 --- a/library/src/main/java/com/bumptech/glide/RequestManager.java +++ b/library/src/main/java/com/bumptech/glide/RequestManager.java @@ -4,7 +4,9 @@ import static com.bumptech.glide.request.RequestOptions.diskCacheStrategyOf; import static com.bumptech.glide.request.RequestOptions.skipMemoryCacheOf; +import android.content.ComponentCallbacks2; import android.content.Context; +import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.net.Uri; @@ -54,7 +56,8 @@ * @see Glide#with(androidx.fragment.app.Fragment) * @see Glide#with(Context) */ -public class RequestManager implements LifecycleListener, ModelTypes> { +public class RequestManager + implements ComponentCallbacks2, LifecycleListener, ModelTypes> { private static final RequestOptions DECODE_TYPE_BITMAP = decodeTypeOf(Bitmap.class).lock(); private static final RequestOptions DECODE_TYPE_GIF = decodeTypeOf(GifDrawable.class).lock(); private static final RequestOptions DOWNLOAD_ONLY_OPTIONS = @@ -93,6 +96,8 @@ public void run() { @GuardedBy("this") private RequestOptions requestOptions; + private boolean pauseAllRequestsOnTrimMemoryModerate; + public RequestManager( @NonNull Glide glide, @NonNull Lifecycle lifecycle, @@ -221,6 +226,14 @@ public RequestManager addDefaultRequestListener(RequestListener requestL return this; } + /** + * If {@code true} then clear all in-progress and completed requests when the platform sends + * {@code onTrimMemory} with level = {@code TRIM_MEMORY_MODERATE}. + */ + public void setPauseAllRequestsOnTrimMemoryModerate(boolean pauseAllOnTrim) { + pauseAllRequestsOnTrimMemoryModerate = pauseAllOnTrim; + } + /** * Returns true if loads for this {@link RequestManager} are currently paused. * @@ -265,6 +278,22 @@ public synchronized void pauseAllRequests() { requestTracker.pauseAllRequests(); } + /** + * Performs {@link #pauseAllRequests()} recursively for all managers that are contextually + * descendant to this manager based on the Activity/Fragment hierarchy. + * + *

Similar to {@link #pauseRequestsRecursive()} with the exception that it also clears + * resources of completed loads. + */ + // Public API. + @SuppressWarnings({"WeakerAccess", "unused"}) + public synchronized void pauseAllRequestsRecursive() { + pauseAllRequests(); + for (RequestManager requestManager : treeNode.getDescendants()) { + requestManager.pauseAllRequests(); + } + } + /** * Performs {@link #pauseRequests()} recursively for all managers that are contextually descendant * to this manager based on the Activity/Fragment hierarchy: @@ -663,6 +692,21 @@ public synchronized String toString() { return super.toString() + "{tracker=" + requestTracker + ", treeNode=" + treeNode + "}"; } + @Override + public void onTrimMemory(int level) { + if (level == TRIM_MEMORY_MODERATE && pauseAllRequestsOnTrimMemoryModerate) { + pauseAllRequestsRecursive(); + } + } + + @Override + public void onLowMemory() { + // Nothing to add conditionally. See Glide#onTrimMemory for unconditional behavior. + } + + @Override + public void onConfigurationChanged(Configuration newConfig) {} + private class RequestManagerConnectivityListener implements ConnectivityMonitor.ConnectivityListener { @GuardedBy("RequestManager.this")