提交 50c46832 编写于 作者: S Sam Judd

Add default signatures and cache strategies.

Work toward #174.
上级 7d7d03dc
......@@ -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,
......
......@@ -30,6 +30,7 @@ import com.bumptech.glide.load.model.file_descriptor.FileDescriptorResourceLoade
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;
......@@ -217,6 +218,7 @@ public class Glide {
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));
......
......@@ -5,6 +5,10 @@ import android.net.Uri;
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;
......@@ -15,6 +19,7 @@ import com.bumptech.glide.manager.ConnectivityMonitorFactory;
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;
......@@ -283,8 +288,9 @@ public class RequestManager implements LifecycleListener {
* {@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);
}
/**
......@@ -370,11 +376,14 @@ public class RequestManager implements LifecycleListener {
*
* @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));
}
/**
......@@ -394,6 +403,13 @@ public class RequestManager implements LifecycleListener {
/**
* 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.
......@@ -402,12 +418,12 @@ public class RequestManager implements LifecycleListener {
* @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*/);
}
/**
......@@ -421,7 +437,10 @@ public class RequestManager implements LifecycleListener {
* 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*/);
}
/**
......
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;
}
......@@ -25,4 +33,20 @@ public class StreamByteArrayLoader implements StreamModelLoader<byte[]> {
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.
}
}
}
......@@ -47,7 +47,6 @@ public final class ApplicationVersionSignature {
// Should never happen.
e.printStackTrace();
}
final String versionCode;
if (pInfo != null) {
versionCode = String.valueOf(pInfo.versionCode);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册