Skip to content

Commit

Permalink
Adds support to more aggressively pause in progress and completed req…
Browse files Browse the repository at this point in the history
…uests in

Glide in response to an onTrimMemory event.

PiperOrigin-RevId: 263206711
  • Loading branch information
sjudd authored and glide-copybara-robot committed Aug 13, 2019
1 parent 5cccdfb commit 8a094e9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
46 changes: 45 additions & 1 deletion library/src/main/java/com/bumptech/glide/RequestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +56,8 @@
* @see Glide#with(androidx.fragment.app.Fragment)
* @see Glide#with(Context)
*/
public class RequestManager implements LifecycleListener, ModelTypes<RequestBuilder<Drawable>> {
public class RequestManager
implements ComponentCallbacks2, LifecycleListener, ModelTypes<RequestBuilder<Drawable>> {
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 =
Expand Down Expand Up @@ -93,6 +96,8 @@ public void run() {
@GuardedBy("this")
private RequestOptions requestOptions;

private boolean pauseAllRequestsOnTrimMemoryModerate;

public RequestManager(
@NonNull Glide glide,
@NonNull Lifecycle lifecycle,
Expand Down Expand Up @@ -221,6 +226,14 @@ public RequestManager addDefaultRequestListener(RequestListener<Object> 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.
*
Expand Down Expand Up @@ -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.
*
* <p>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:
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 8a094e9

Please sign in to comment.