Skip to content

Commit

Permalink
MOE automated commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed May 17, 2019
1 parent f8cafb3 commit fa7cb2f
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 72 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ JUNIT_VERSION=4.13-SNAPSHOT
# Matches the version in Google.
MOCKITO_VERSION=1.9.5
MOCKITO_ANDROID_VERSION=2.21.0
ROBOLECTRIC_VERSION=4.1
ROBOLECTRIC_VERSION=4.3-beta-1
MOCKWEBSERVER_VERSION=3.0.0-RC1
TRUTH_VERSION=0.36
TRUTH_VERSION=0.44
JSR_305_VERSION=3.0.2
AUTO_SERVICE_VERSION=1.0-rc3
JAVAPOET_VERSION=1.9.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public void asFile_withUrl_succeeds() {
assertThat(file).isNotNull();
}

@Test
public void asFile_withUrlAndDiskCacheStrategyAutomatic_succeeds() {
File file =
concurrency.get(
GlideApp.with(context)
.asFile()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.load(URL)
.submit());
assertThat(file).isNotNull();
}

@Test
public void asFile_withUrlAndDiskCacheStrategyData_succeeds() {
File file =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void load_withColorDrawable_fixedSize_unitBitmapTransform_recyclesInterme
Bitmap first = bitmapPool.get(width, height, Config.ARGB_8888);
Bitmap second = bitmapPool.get(width, height, Config.ARGB_8888);

assertThat(first).isNotSameAs(second);
assertThat(first).isNotSameInstanceAs(second);
}
@Test
public void load_withColorDrawable_fixedSize_functionalBitmapTransform_doesNotRecycleOutput()
Expand All @@ -232,6 +232,6 @@ public void load_withColorDrawable_fixedSize_functionalBitmapTransform_doesNotRe
Bitmap first = bitmapPool.get(width, height, Config.ARGB_8888);
Bitmap second = bitmapPool.get(width, height, Config.ARGB_8888);

assertThat(first).isNotSameAs(second);
assertThat(first).isNotSameInstanceAs(second);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void loadFromRequestManager_intoImageView_withDifferentByteArrays_loadsDi

// This assertion alone doesn't catch the case where the second Bitmap is loaded from the result
// cache of the data from the first Bitmap.
BitmapSubject.assertThat(firstBitmap).isNotSameAs(secondBitmap);
BitmapSubject.assertThat(firstBitmap).isNotSameInstanceAs(secondBitmap);

Bitmap expectedCanonicalBitmap =
BitmapFactory.decodeByteArray(canonicalBytes, /*offset=*/ 0, canonicalBytes.length);
Expand Down Expand Up @@ -127,7 +127,7 @@ public void loadFromRequestBuilder_intoImageView_withDifferentByteArrays_loadsDi

// This assertion alone doesn't catch the case where the second Bitmap is loaded from the result
// cache of the data from the first Bitmap.
BitmapSubject.assertThat(firstBitmap).isNotSameAs(secondBitmap);
BitmapSubject.assertThat(firstBitmap).isNotSameInstanceAs(secondBitmap);

Bitmap expectedCanonicalBitmap =
BitmapFactory.decodeByteArray(canonicalBytes, /*offset=*/ 0, canonicalBytes.length);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bumptech.glide.test;

import static com.google.common.truth.Fact.simpleFact;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
Expand Down Expand Up @@ -28,8 +30,11 @@ public BitmapSubject createSubject(
}
};

private final Bitmap actual;

private BitmapSubject(FailureMetadata failureMetadata, Bitmap subject) {
super(failureMetadata, subject);
this.actual = subject;
}

public static BitmapSubject assertThat(Drawable drawable) {
Expand All @@ -45,7 +50,7 @@ public static BitmapSubject assertThat(Bitmap bitmap) {

@Override
protected String actualCustomStringRepresentation() {
return getDisplayString(actual());
return getDisplayString(actual);
}

private static String getDisplayString(Bitmap bitmap) {
Expand All @@ -64,59 +69,52 @@ public void sameAs(@DrawableRes int resourceId) {
}

public void hasDimensions(int expectedWidth, int expectedHeight) {
int actualWidth = actual().getWidth();
int actualHeight = actual().getHeight();
String message;
int actualWidth = actual.getWidth();
int actualHeight = actual.getHeight();
if (expectedWidth != actualWidth && expectedHeight != actualHeight) {
message = "has dimensions of [" + expectedWidth + "x" + expectedHeight + "]";
failWithActual("expected to have dimensions", expectedWidth + "x" + expectedHeight);
} else if (expectedWidth != actualWidth) {
message = "has width of " + expectedWidth;
failWithActual("expected to have width", expectedWidth);
} else if (expectedHeight != actualHeight) {
message = "has height of " + expectedHeight;
} else {
message = null;
}

if (message != null) {
fail(message);
failWithActual("expected to have height", expectedHeight);
}
}

public void isMutable() {
if (!actual().isMutable()) {
fail("is mutable");
if (!actual.isMutable()) {
failWithActual(simpleFact("expected to be mutable"));
}
}

public void isImmutable() {
if (actual().isMutable()) {
fail("is immutable");
if (actual.isMutable()) {
failWithActual(simpleFact("expected to be immutable"));
}
}

public void isNotRecycled() {
if (actual().isRecycled()) {
fail("is not recycled");
if (actual.isRecycled()) {
failWithActual(simpleFact("expected not to be recycled"));
}
}

@SuppressWarnings({"unchecked", "ConstantConditions"})
public void sameAs(Drawable other) {
if (!(other instanceof BitmapDrawable)) {
fail("Not a BitmapDrawable");
failWithoutActual(simpleFact("The given expected value was not a BitmapDrawable."));
}
sameAs(((BitmapDrawable) other).getBitmap());
}

public void sameAs(Bitmap other) {
if (!actual().sameAs(other)) {
fail("is the same as " + getDisplayString(other));
if (!actual.sameAs(other)) {
failWithActual("expected to be the same as", getDisplayString(other));
}
}

public void isNotSameAs(Bitmap other) {
if (actual().sameAs(other)) {
fail("is not the same as " + getDisplayString(other));
if (actual.sameAs(other)) {
failWithActual("expected not to be the same as", getDisplayString(other));
}
}
}
47 changes: 35 additions & 12 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.MessageQueue.IdleHandler;
import android.os.ParcelFileDescriptor;
import android.support.annotation.GuardedBy;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
Expand All @@ -31,6 +33,7 @@
import com.bumptech.glide.load.engine.cache.MemoryCache;
import com.bumptech.glide.load.engine.prefill.BitmapPreFiller;
import com.bumptech.glide.load.engine.prefill.PreFillType;
import com.bumptech.glide.load.engine.prefill.PreFillType.Builder;
import com.bumptech.glide.load.model.AssetUriLoader;
import com.bumptech.glide.load.model.ByteArrayLoader;
import com.bumptech.glide.load.model.ByteBufferEncoder;
Expand Down Expand Up @@ -109,14 +112,17 @@ public class Glide implements ComponentCallbacks2 {
private final Engine engine;
private final BitmapPool bitmapPool;
private final MemoryCache memoryCache;
private final BitmapPreFiller bitmapPreFiller;
private final GlideContext glideContext;
private final Registry registry;
private final ArrayPool arrayPool;
private final RequestManagerRetriever requestManagerRetriever;
private final ConnectivityMonitorFactory connectivityMonitorFactory;
private final List<RequestManager> managers = new ArrayList<>();
private final RequestOptionsFactory defaultRequestOptionsFactory;
private MemoryCategory memoryCategory = MemoryCategory.NORMAL;
@GuardedBy("this")
@Nullable
private BitmapPreFiller bitmapPreFiller;

/**
* Returns a directory with a default name in the private cache directory of the application to
Expand Down Expand Up @@ -328,7 +334,7 @@ private static void throwIncorrectGlideModule(Exception e) {
@NonNull RequestManagerRetriever requestManagerRetriever,
@NonNull ConnectivityMonitorFactory connectivityMonitorFactory,
int logLevel,
@NonNull RequestOptions defaultRequestOptions,
@NonNull RequestOptionsFactory defaultRequestOptionsFactory,
@NonNull Map<Class<?>, TransitionOptions<?, ?>> defaultTransitionOptions,
@NonNull List<RequestListener<Object>> defaultRequestListeners,
boolean isLoggingRequestOriginsEnabled) {
Expand All @@ -338,9 +344,7 @@ private static void throwIncorrectGlideModule(Exception e) {
this.memoryCache = memoryCache;
this.requestManagerRetriever = requestManagerRetriever;
this.connectivityMonitorFactory = connectivityMonitorFactory;

DecodeFormat decodeFormat = defaultRequestOptions.getOptions().get(Downsampler.DECODE_FORMAT);
bitmapPreFiller = new BitmapPreFiller(memoryCache, bitmapPool, decodeFormat);
this.defaultRequestOptionsFactory = defaultRequestOptionsFactory;

final Resources resources = context.getResources();

Expand Down Expand Up @@ -525,7 +529,7 @@ Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitm
arrayPool,
registry,
imageViewTargetFactory,
defaultRequestOptions,
defaultRequestOptionsFactory,
defaultTransitionOptions,
defaultRequestListeners,
engine,
Expand Down Expand Up @@ -580,7 +584,7 @@ GlideContext getGlideContext() {
}

/**
* Pre-fills the {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool} using the given
* Pre-fills the {@link BitmapPool} using the given
* sizes.
*
* <p> Enough Bitmaps are added to completely fill the pool, so most or all of the Bitmaps
Expand All @@ -589,23 +593,30 @@ GlideContext getGlideContext() {
* </p>
*
* <p> Note - Pre-filling is done asynchronously using and
* {@link android.os.MessageQueue.IdleHandler}. Any currently running pre-fill will be cancelled
* {@link IdleHandler}. Any currently running pre-fill will be cancelled
* and replaced by a call to this method. </p>
*
* <p> This method should be used with caution, overly aggressive pre-filling is substantially
* worse than not pre-filling at all. Pre-filling should only be started in onCreate to avoid
* constantly clearing and re-filling the
* {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool}. Rotation should be carefully
* {@link BitmapPool}. Rotation should be carefully
* considered as well. It may be worth calling this method only when no saved instance state
* exists so that pre-filling only happens when the Activity is first created, rather than on
* every rotation. </p>
*
* @param bitmapAttributeBuilders The list of
* {@link com.bumptech.glide.load.engine.prefill.PreFillType.Builder Builders} representing
* individual sizes and configurations of {@link android.graphics.Bitmap}s to be pre-filled.
* {@link Builder Builders} representing
* individual sizes and configurations of {@link Bitmap}s to be pre-filled.
*/
@SuppressWarnings("unused") // Public API
public void preFillBitmapPool(@NonNull PreFillType.Builder... bitmapAttributeBuilders) {
public synchronized void preFillBitmapPool(
@NonNull PreFillType.Builder... bitmapAttributeBuilders) {
if (bitmapPreFiller == null) {
DecodeFormat decodeFormat =
defaultRequestOptionsFactory.build().getOptions().get(Downsampler.DECODE_FORMAT);
bitmapPreFiller = new BitmapPreFiller(memoryCache, bitmapPool, decodeFormat);
}

bitmapPreFiller.preFill(bitmapAttributeBuilders);
}

Expand Down Expand Up @@ -858,4 +869,16 @@ public void onConfigurationChanged(Configuration newConfig) {
public void onLowMemory() {
clearMemory();
}

/**
* Creates a new instance of {@link RequestOptions}.
*/
public interface RequestOptionsFactory {

/**
* Returns a non-null {@link RequestOptions} object.
*/
@NonNull
RequestOptions build();
}
}
42 changes: 38 additions & 4 deletions library/src/main/java/com/bumptech/glide/GlideBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.util.Log;
import com.bumptech.glide.Glide.RequestOptionsFactory;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.Engine;
import com.bumptech.glide.load.engine.GlideException;
Expand All @@ -28,6 +29,7 @@
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.util.Preconditions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -48,7 +50,13 @@ public final class GlideBuilder {
private MemorySizeCalculator memorySizeCalculator;
private ConnectivityMonitorFactory connectivityMonitorFactory;
private int logLevel = Log.INFO;
private RequestOptions defaultRequestOptions = new RequestOptions();
private RequestOptionsFactory defaultRequestOptionsFactory = new RequestOptionsFactory() {
@NonNull
@Override
public RequestOptions build() {
return new RequestOptions();
}
};
@Nullable
private RequestManagerFactory requestManagerFactory;
private GlideExecutor animationExecutor;
Expand Down Expand Up @@ -209,12 +217,38 @@ public GlideBuilder setAnimationExecutor(@Nullable GlideExecutor service) {
* RequestBuilder#apply(BaseRequestOptions)} will override defaults
* set here.
*
* @see #setDefaultRequestOptions(RequestOptionsFactory)
*
* @param requestOptions The options to use by default.
* @return This builder.
*/
@NonNull
public GlideBuilder setDefaultRequestOptions(@Nullable RequestOptions requestOptions) {
this.defaultRequestOptions = requestOptions;
public GlideBuilder setDefaultRequestOptions(@Nullable final RequestOptions requestOptions) {
return setDefaultRequestOptions(new RequestOptionsFactory() {
@NonNull
@Override
public RequestOptions build() {
return requestOptions != null ? requestOptions : new RequestOptions();
}
});
}

/**
* Sets a factory for the default {@link RequestOptions} to use for all loads across the app and
* returns this {@code GlideBuilder}.
*
* <p>This factory will <em>NOT</em> be called once per load. Instead it will be called a handful
* of times and memoized. It's not safe to assume that this factory will be called again for
* every new load.
*
* <p>Applying additional options with {@link RequestBuilder#apply(BaseRequestOptions)} will
* override defaults set here.
*
* @see #setDefaultRequestOptions(RequestOptionsFactory)
*/
@NonNull
public GlideBuilder setDefaultRequestOptions(@NonNull RequestOptionsFactory factory) {
this.defaultRequestOptionsFactory = Preconditions.checkNotNull(factory);
return this;
}

Expand Down Expand Up @@ -506,7 +540,7 @@ Glide build(@NonNull Context context) {
requestManagerRetriever,
connectivityMonitorFactory,
logLevel,
defaultRequestOptions.lock(),
defaultRequestOptionsFactory,
defaultTransitionOptions,
defaultRequestListeners,
isLoggingRequestOriginsEnabled);
Expand Down
Loading

0 comments on commit fa7cb2f

Please sign in to comment.