Skip to content

Commit

Permalink
Automatically register Glide as ComponentCallbacks
Browse files Browse the repository at this point in the history
Applications will no longer have to manually call
callbacks methods.
  • Loading branch information
sjudd committed Aug 7, 2017
1 parent 23dd41a commit 6b137c2
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 94 deletions.
8 changes: 6 additions & 2 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ private static void initializeGlide(Context context) {
if (annotationGeneratedModule != null) {
annotationGeneratedModule.registerComponents(applicationContext, glide, glide.registry);
}
context.getApplicationContext().registerComponentCallbacks(glide);
Glide.glide = glide;
}

Expand Down Expand Up @@ -370,8 +371,9 @@ private static GeneratedAppGlideModule getAnnotationGeneratedGlideModules() {
.register(GifDrawable.class, byte[].class, new GifDrawableBytesTranscoder());

ImageViewTargetFactory imageViewTargetFactory = new ImageViewTargetFactory();
glideContext = new GlideContext(context, registry, imageViewTargetFactory,
defaultRequestOptions, engine, this, logLevel);
glideContext =
new GlideContext(
context, registry, imageViewTargetFactory, defaultRequestOptions, engine, logLevel);
}

/**
Expand Down Expand Up @@ -466,6 +468,7 @@ public void clearMemory() {
* @see android.content.ComponentCallbacks2#onTrimMemory(int)
*/
public void trimMemory(int level) {
Log.d("TEST", "trimMemory: " + level);
// Engine asserts this anyway when removing resources, fail faster and consistently
Util.assertMainThread();
// memory cache needs to be trimmed before bitmap pool to trim re-pooled Bitmaps too. See #687.
Expand Down Expand Up @@ -669,6 +672,7 @@ public void onConfigurationChanged(Configuration newConfig) {

@Override
public void onLowMemory() {
Log.d("TEST", "onLowMemory");
clearMemory();
}
}
23 changes: 2 additions & 21 deletions library/src/main/java/com/bumptech/glide/GlideContext.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.bumptech.glide;

import android.annotation.TargetApi;
import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
Expand All @@ -19,24 +17,22 @@
* required to load resources.
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class GlideContext extends ContextWrapper implements ComponentCallbacks2 {
public class GlideContext extends ContextWrapper {
private final Handler mainHandler;
private final Registry registry;
private final ImageViewTargetFactory imageViewTargetFactory;
private final RequestOptions defaultRequestOptions;
private final Engine engine;
private final ComponentCallbacks2 componentCallbacks;
private final int logLevel;

public GlideContext(Context context, Registry registry,
ImageViewTargetFactory imageViewTargetFactory, RequestOptions defaultRequestOptions,
Engine engine, ComponentCallbacks2 componentCallbacks, int logLevel) {
Engine engine, int logLevel) {
super(context.getApplicationContext());
this.registry = registry;
this.imageViewTargetFactory = imageViewTargetFactory;
this.defaultRequestOptions = defaultRequestOptions;
this.engine = engine;
this.componentCallbacks = componentCallbacks;
this.logLevel = logLevel;

mainHandler = new Handler(Looper.getMainLooper());
Expand Down Expand Up @@ -65,19 +61,4 @@ public Registry getRegistry() {
public int getLogLevel() {
return logLevel;
}

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

@Override
public void onConfigurationChanged(Configuration newConfig) {
componentCallbacks.onConfigurationChanged(newConfig);
}

@Override
public void onLowMemory() {
componentCallbacks.onLowMemory();
}
}
12 changes: 10 additions & 2 deletions library/src/main/java/com/bumptech/glide/RequestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,24 @@ public RequestManager setDefaultRequestOptions(RequestOptions requestOptions) {

/**
* @see android.content.ComponentCallbacks2#onTrimMemory(int)
*
* @deprecated This method is called automatically by Glide's internals and shouldn't be called
* externally.
*/
@Deprecated
public void onTrimMemory(int level) {
glide.getGlideContext().onTrimMemory(level);
glide.onTrimMemory(level);
}

/**
* @see android.content.ComponentCallbacks2#onLowMemory()
*
* @deprecated This method is called automatically by Glide's internals and shouldn't be called
* externally.
*/
@Deprecated
public void onLowMemory() {
glide.getGlideContext().onLowMemory();
glide.onLowMemory();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,26 +197,6 @@ public void onDestroy() {
unregisterFragmentWithRoot();
}

@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
// If an activity is re-created, onTrimMemory may be called before a manager is ever put.
// See #329.
if (requestManager != null) {
requestManager.onTrimMemory(level);
}
}

@Override
public void onLowMemory() {
super.onLowMemory();
// If an activity is re-created, onLowMemory may be called before a manager is ever put.
// See #329.
if (requestManager != null) {
requestManager.onLowMemory();
}
}

@Override
public String toString() {
return super.toString() + "{parent=" + getParentFragmentUsingHint() + "}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,6 @@ public void onDestroy() {
unregisterFragmentWithRoot();
}

@Override
public void onLowMemory() {
super.onLowMemory();
// If an activity is re-created, onLowMemory may be called before a manager is ever put.
// See #329.
if (requestManager != null) {
requestManager.onLowMemory();
}
}

@Override
public String toString() {
return super.toString() + "{parent=" + getParentFragmentUsingHint() + "}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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;
Expand Down Expand Up @@ -99,29 +98,6 @@ 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.onLowMemory();
verify(requestManager).onLowMemory();
}
});
}

@Test
public void testNonSupportFragmentCallsOnTrimMemory() {
RequestManagerHarness requestManagerHarness = new RequestManagerHarness();
int level = 100;
RequestManager requestManager = mock(RequestManager.class);
requestManagerHarness.setRequestManager(requestManager);
requestManagerHarness.onTrimMemory(level);
verify(requestManager).onTrimMemory(eq(level));
}

@Test
public void testOnLowMemoryCallOnNullRequestManagerDoesNotCrash() {
runTest(new TestCase() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.bumptech.glide.samples.flickr;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
Expand Down Expand Up @@ -187,19 +185,6 @@ protected void onDestroy() {
}
}

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
GlideApp.get(this).trimMemory(level);
}

@Override
public void onLowMemory() {
super.onLowMemory();
GlideApp.get(this).clearMemory();
}

private void executeSearch(String searchString) {
Query query = TextUtils.isEmpty(searchString) ? null : new SearchQuery(searchString);
executeQuery(query);
Expand Down

0 comments on commit 6b137c2

Please sign in to comment.