Skip to content

Commit

Permalink
Add pauseAllRequests()
Browse files Browse the repository at this point in the history
pauseAllRequests() can be called when the activity is in the background to reclaim memory used by the active bitmaps. As the caches are unaffected by this, returning to the activity and loading is still fast.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=177660869
  • Loading branch information
kurtn authored and sjudd committed Dec 4, 2017
1 parent 5d760e2 commit 09e33a2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
25 changes: 25 additions & 0 deletions library/src/main/java/com/bumptech/glide/RequestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ public boolean isPaused() {
/**
* Cancels any in progress loads, but does not clear resources of completed loads.
*
* <p>Note #{@link #resumeRequests()} must be called for any requests made before or while the
* manager is paused to complete. RequestManagers attached to Fragments and Activities
* automatically resume onStart().
*
* @see #isPaused()
* @see #resumeRequests()
*/
Expand All @@ -213,6 +217,27 @@ public void pauseRequests() {
requestTracker.pauseRequests();
}

/**
* Cancels any in progress loads and clears resources of completed loads.
*
* <p>Note #{@link #resumeRequests()} must be called for any requests made before or while the
* manager is paused to complete. RequestManagers attached to Fragments and Activities
* automatically resume onStart().
*
* <p>This will release the memory used by completed bitmaps but leaves them in any configured
* caches. When an #{@link android.app.Activity} receives #{@link
* android.app.Activity#onTrimMemory(int)} at a level of #{@link
* android.content.ComponentCallbacks2#TRIM_MEMORY_BACKGROUND} this is desirable in order to keep
* your process alive longer.
*
* @see #isPaused()
* @see #resumeRequests()
*/
public void pauseAllRequests() {
Util.assertMainThread();
requestTracker.pauseAllRequests();
}

/**
* Performs {@link #pauseRequests()} recursively for all managers that are contextually
* descendant to this manager based on the Activity/Fragment hierarchy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ public void pauseRequests() {
}
}

/** Stops any in progress requests and releases bitmaps associated with completed requests. */
public void pauseAllRequests() {
isPaused = true;
for (Request request : Util.getSnapshot(requests)) {
if (request.isRunning() || request.isComplete()) {
request.pause();
pendingRequests.add(request);
}
}
}

/**
* Starts any not yet completed or failed requests.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,26 @@ public void testReturnsFalseFromIsPausedWhenResumed() {
assertFalse(tracker.isPaused());
}

@Test
public void testPauseAllRequests_returnsTrueFromIsPaused() {
tracker.pauseAllRequests();
assertTrue(tracker.isPaused());
}

@Test
public void testPauseAllRequests_whenRequestComplete_pausesRequest() {
Request request = mock(Request.class);
when(request.isFailed()).thenReturn(false);
when(request.isComplete()).thenReturn(true);
tracker.addRequest(request);
tracker.pauseAllRequests();
verify(request).pause();

when(request.isComplete()).thenReturn(false);
tracker.resumeRequests();
verify(request).begin();
}

private class ClearAndRemoveRequest implements Answer<Void> {

private final Request toRemove;
Expand Down

0 comments on commit 09e33a2

Please sign in to comment.