Skip to content

Commit

Permalink
Call trim/lowMemory in request manager fragments.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Jan 9, 2015
1 parent a2e6e60 commit 9063f6c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import android.app.Activity;
import android.content.ComponentCallbacks;
import android.support.v4.app.FragmentActivity;

import com.bumptech.glide.RequestManager;
Expand Down Expand Up @@ -103,6 +105,30 @@ public void runTest(Harness harness) {
});
}

@Test
public void testCallsRequestManagerOnLowMemory() {
runTest(new TestCase() {
@Override
public void runTest(Harness harness) {
RequestManager requestManager = mock(RequestManager.class);
harness.setRequestManager(requestManager);
harness.getCallbacks().onLowMemory();
verify(requestManager).onLowMemory();
}
});
}

@Test
public void testNonSupportFragmentCallsRequestManagerOnTrimMemory() {
RequestManagerHarness requestManagerHarness = new RequestManagerHarness();
RequestManager requestManager = mock(RequestManager.class);
requestManagerHarness.setRequestManager(requestManager);
int level = 123;
requestManagerHarness.fragment.onTrimMemory(level);

verify(requestManager).onTrimMemory(eq(level));
}

private void runTest(TestCase testCase) {
for (Harness harness : harnesses) {
try {
Expand All @@ -127,6 +153,8 @@ private interface Harness {
public ActivityFragmentLifecycle getFragmentLifecycle();

public ActivityController getController();

public ComponentCallbacks getCallbacks();
}

private static class RequestManagerHarness implements Harness {
Expand Down Expand Up @@ -174,6 +202,11 @@ public ActivityFragmentLifecycle getFragmentLifecycle() {
public ActivityController getController() {
return controller;
}

@Override
public ComponentCallbacks getCallbacks() {
return fragment;
}
}

private static class SupportRequestManagerHarness implements Harness {
Expand Down Expand Up @@ -222,5 +255,10 @@ public ActivityFragmentLifecycle getFragmentLifecycle() {
public ActivityController getController() {
return supportController;
}

@Override
public ComponentCallbacks getCallbacks() {
return supportFragment;
}
}
}
1 change: 1 addition & 0 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ public void preFillBitmapPool(PreFillType.Builder... bitmapAttributeBuilders) {
/**
* Clears as much memory as possible.
*
* @see android.content.ComponentCallbacks#onLowMemory()
* @see android.content.ComponentCallbacks2#onLowMemory()
*/
public void clearMemory() {
Expand Down
22 changes: 21 additions & 1 deletion library/src/main/java/com/bumptech/glide/RequestManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.bumptech.glide;

import android.annotation.SuppressLint;
import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
Expand Down Expand Up @@ -39,7 +42,9 @@
* @see Glide#with(android.support.v4.app.Fragment)
* @see Glide#with(Context)
*/
public class RequestManager implements LifecycleListener {
// It's safe to implement ComponentCallbacks2.
@SuppressLint("NewApi")
public class RequestManager implements LifecycleListener, ComponentCallbacks2 {
private final Context context;
private final Lifecycle lifecycle;
private final RequestTracker requestTracker;
Expand Down Expand Up @@ -78,6 +83,21 @@ public void run() {
lifecycle.addListener(connectivityMonitor);
}

@Override
public void onTrimMemory(int level) {
glide.trimMemory(level);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
// Do nothing.
}

@Override
public void onLowMemory() {
glide.clearMemory();
}

/**
* An interface that allows a default set of options to be applied to all requests started from an
* {@link com.bumptech.glide.RequestManager}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,14 @@ public void onDestroy() {
super.onDestroy();
lifecycle.onDestroy();
}

@Override
public void onTrimMemory(int level) {
requestManager.onTrimMemory(level);
}

@Override
public void onLowMemory() {
requestManager.onLowMemory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@ public void onDestroy() {
super.onDestroy();
lifecycle.onDestroy();
}

@Override
public void onLowMemory() {
super.onLowMemory();
requestManager.onLowMemory();
}
}

0 comments on commit 9063f6c

Please sign in to comment.