Skip to content

Commit

Permalink
Add default signatures and cache strategies.
Browse files Browse the repository at this point in the history
Work toward bumptech#174.
  • Loading branch information
sjudd committed Oct 9, 2014
1 parent 7d7d03d commit 50c4683
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public class GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeT
this(other.context, other.model, loadProvider, transcodeClass, other.glide, other.requestTracker,
other.lifecycle);
this.signature = other.signature;
this.diskCacheStrategy = other.diskCacheStrategy;
this.isCacheable = other.isCacheable;
}

GenericRequestBuilder(Context context, ModelType model,
Expand Down
2 changes: 2 additions & 0 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.bumptech.glide.load.model.file_descriptor.FileDescriptorStringLoader;
import com.bumptech.glide.load.model.file_descriptor.FileDescriptorUriLoader;
import com.bumptech.glide.load.model.stream.HttpUrlGlideUrlLoader;
import com.bumptech.glide.load.model.stream.StreamByteArrayLoader;
import com.bumptech.glide.load.model.stream.StreamFileLoader;
import com.bumptech.glide.load.model.stream.StreamResourceLoader;
import com.bumptech.glide.load.model.stream.StreamStringLoader;
Expand Down Expand Up @@ -217,6 +218,7 @@ static void tearDown() {
register(Uri.class, InputStream.class, new StreamUriLoader.Factory());
register(URL.class, InputStream.class, new StreamUrlLoader.Factory());
register(GlideUrl.class, InputStream.class, new HttpUrlGlideUrlLoader.Factory());
register(byte[].class, InputStream.class, new StreamByteArrayLoader.Factory());

transcoderRegistry.register(Bitmap.class, GlideBitmapDrawable.class,
new GlideBitmapDrawableTranscoder(context.getResources(), bitmapPool));
Expand Down
37 changes: 28 additions & 9 deletions library/src/main/java/com/bumptech/glide/RequestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import android.os.Handler;
import android.os.Looper;
import android.os.ParcelFileDescriptor;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.signature.ApplicationVersionSignature;
import com.bumptech.glide.signature.MediaStoreSignature;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.file_descriptor.FileDescriptorModelLoader;
import com.bumptech.glide.load.model.stream.MediaStoreStreamLoader;
Expand All @@ -15,6 +19,7 @@
import com.bumptech.glide.manager.Lifecycle;
import com.bumptech.glide.manager.LifecycleListener;
import com.bumptech.glide.manager.RequestTracker;
import com.bumptech.glide.signature.StringSignature;
import com.bumptech.glide.util.Util;

import java.io.File;
Expand Down Expand Up @@ -283,8 +288,9 @@ public DrawableTypeRequest<Uri> load(Uri uri) {
* {@link android.provider.MediaStore.Images.ImageColumns#ORIENTATION}.
*/
public DrawableTypeRequest<Uri> loadFromMediaStore(Uri uri, String mimeType, long dateModified, int orientation) {
// TODO: create a signature from the given arguments.
return loadFromMediaStore(uri);
Key signature = new MediaStoreSignature(mimeType, dateModified, orientation);
return (DrawableTypeRequest<Uri>) loadFromMediaStore(uri)
.signature(signature);
}

/**
Expand Down Expand Up @@ -370,11 +376,14 @@ public DrawableTypeRequest<File> load(File file) {
*
* @see #load(Integer)
* @see #using(StreamModelLoader)
* @see com.bumptech.glide.signature.ApplicationVersionSignature
* @see com.bumptech.glide.GenericRequestBuilder#signature(com.bumptech.glide.load.Key)
*
* @param resourceId the id of the resource containing the image
*/
public DrawableTypeRequest<Integer> load(Integer resourceId) {
return loadGeneric(resourceId);
return (DrawableTypeRequest<Integer>) loadGeneric(resourceId)
.signature(ApplicationVersionSignature.obtain(context));
}

/**
Expand All @@ -394,6 +403,13 @@ public DrawableTypeRequest<URL> load(URL url) {
/**
* Returns a request builder that uses a {@link StreamByteArrayLoader} to load an image from the given byte array.
*
* @deprecated Use {@link #load(byte[])} along with
* {@link com.bumptech.glide.GenericRequestBuilder#signature(com.bumptech.glide.load.Key)}} instead.
*
* <p>
* Note - This method does not cache results in either the disk cache or the memory cache.
* </p>
*
* @see #load(byte[])
*
* @param model The data to load.
Expand All @@ -402,12 +418,12 @@ public DrawableTypeRequest<URL> load(URL url) {
* @return A {@link DrawableTypeRequest} to set options for the load and ultimately the target to load the image
* into.
*/
@Deprecated
public DrawableTypeRequest<byte[]> load(byte[] model, final String id) {
final StreamByteArrayLoader loader = new StreamByteArrayLoader(id);
// TODO: just use signature instead of id here?
return optionsApplier.apply(model,
new DrawableTypeRequest<byte[]>(model, loader, null, context, glide, requestTracker, lifecycle,
optionsApplier));
return (DrawableTypeRequest<byte[]>) loadGeneric(model)
.signature(new StringSignature(id))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true /*skipMemoryCache*/);
}

/**
Expand All @@ -421,7 +437,10 @@ public DrawableTypeRequest<byte[]> load(byte[] model, final String id) {
* into.
*/
public DrawableTypeRequest<byte[]> load(byte[] model) {
return load(model, UUID.randomUUID().toString());
return (DrawableTypeRequest<byte[]>) loadGeneric(model)
.signature(new StringSignature(UUID.randomUUID().toString()))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true /*skipMemoryCache*/);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package com.bumptech.glide.load.model.stream;

import android.content.Context;
import com.bumptech.glide.load.data.ByteArrayFetcher;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;

import java.io.InputStream;
import java.util.UUID;

/**
* A base class to convert byte arrays to input streams so they can be decoded. This class is abstract because there is
* no simple/quick way to generate an id from the bytes themselves, so subclass must include an id.
*/
public class StreamByteArrayLoader implements StreamModelLoader<byte[]> {
private String id;
private final String id;

public StreamByteArrayLoader() {
this(UUID.randomUUID().toString());
this("");
}

/**
* @deprecated Use {@link com.bumptech.glide.GenericRequestBuilder#signature(com.bumptech.glide.load.Key)}
* and the empty constructor instead.
*/
@Deprecated
public StreamByteArrayLoader(String id) {
this.id = id;
}
Expand All @@ -25,4 +33,20 @@ public StreamByteArrayLoader(String id) {
public DataFetcher<InputStream> getResourceFetcher(byte[] model, int width, int height) {
return new ByteArrayFetcher(model, id);
}

/**
* Factory for {@link com.bumptech.glide.load.model.stream.StreamByteArrayLoader}.
*/
public static class Factory implements ModelLoaderFactory<byte[], InputStream> {

@Override
public ModelLoader<byte[], InputStream> build(Context context, GenericLoaderFactory factories) {
return new StreamByteArrayLoader();
}

@Override
public void teardown() {
// Do nothing.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ private static Key obtainVersionSignature(Context context) {
// Should never happen.
e.printStackTrace();
}

final String versionCode;
if (pInfo != null) {
versionCode = String.valueOf(pInfo.versionCode);
Expand Down

0 comments on commit 50c4683

Please sign in to comment.