From 40662f4b39e795d9c64502b13036e7c37fa2d373 Mon Sep 17 00:00:00 2001 From: Sam Blitzstein Date: Wed, 16 Oct 2013 12:12:54 -0700 Subject: Change BitmapRequestKey to be more cleanly implementable. Change-Id: I831586688605e6c6c2f2f7a879c6be23175f71de --- src/com/android/bitmap/BitmapCache.java | 2 +- src/com/android/bitmap/DecodeAggregator.java | 6 +-- src/com/android/bitmap/DecodeTask.java | 28 +++------- src/com/android/bitmap/RequestKey.java | 59 +++++++++++++++++++++ src/com/android/bitmap/UnrefedBitmapCache.java | 11 ++-- .../bitmap/drawable/BasicBitmapDrawable.java | 16 +++--- .../android/bitmap/drawable/BitmapRequestKey.java | 60 ---------------------- .../bitmap/drawable/ExtendedBitmapDrawable.java | 20 ++++---- 8 files changed, 92 insertions(+), 110 deletions(-) create mode 100644 src/com/android/bitmap/RequestKey.java delete mode 100644 src/com/android/bitmap/drawable/BitmapRequestKey.java diff --git a/src/com/android/bitmap/BitmapCache.java b/src/com/android/bitmap/BitmapCache.java index 4695d72..e1cf6e3 100644 --- a/src/com/android/bitmap/BitmapCache.java +++ b/src/com/android/bitmap/BitmapCache.java @@ -16,7 +16,7 @@ package com.android.bitmap; -public interface BitmapCache extends PooledCache { +public interface BitmapCache extends PooledCache { /** * Notify the cache when it should start and stop blocking for cache misses. diff --git a/src/com/android/bitmap/DecodeAggregator.java b/src/com/android/bitmap/DecodeAggregator.java index ec88efc..a2e1b07 100644 --- a/src/com/android/bitmap/DecodeAggregator.java +++ b/src/com/android/bitmap/DecodeAggregator.java @@ -16,11 +16,9 @@ package com.android.bitmap; -import com.android.bitmap.DecodeTask.Request; +public class DecodeAggregator extends ContiguousFIFOAggregator { -public class DecodeAggregator extends ContiguousFIFOAggregator { - - public interface Callback extends ContiguousFIFOAggregator.Callback { + public interface Callback extends ContiguousFIFOAggregator.Callback { } } diff --git a/src/com/android/bitmap/DecodeTask.java b/src/com/android/bitmap/DecodeTask.java index ab2a994..9dbcea9 100644 --- a/src/com/android/bitmap/DecodeTask.java +++ b/src/com/android/bitmap/DecodeTask.java @@ -46,7 +46,7 @@ import java.io.InputStream; */ public class DecodeTask extends AsyncTask { - private final Request mKey; + private final RequestKey mKey; private final int mDestW; private final int mDestH; private final DecodeCallback mDecodeCallback; @@ -60,20 +60,6 @@ public class DecodeTask extends AsyncTask { private static final String TAG = DecodeTask.class.getSimpleName(); private static final boolean DEBUG = false; - /** - * The decode task uses this class to get input to decode. You must implement at least one of - * {@link #createFd()} or {@link #createInputStream()}. {@link DecodeTask} will prioritize - * {@link #createFd()} before falling back to {@link #createInputStream()}. - *

- * When {@link DecodeTask} is used in conjunction with a {@link BitmapCache}, objects of this - * type will also serve as cache keys to fetch cached data. - */ - public interface Request { - AssetFileDescriptor createFd() throws IOException; - InputStream createInputStream() throws IOException; - boolean hasOrientationExif() throws IOException; - } - /** * Callback interface for clients to be notified of decode state changes and completion. */ @@ -84,20 +70,20 @@ public class DecodeTask extends AsyncTask { *

* N.B. this method runs on the UI thread. */ - void onDecodeBegin(Request key); + void onDecodeBegin(RequestKey key); /** * The task is now complete and the ReusableBitmap is available for use. Clients should * double check that the request matches what the client is expecting. */ - void onDecodeComplete(Request key, ReusableBitmap result); + void onDecodeComplete(RequestKey key, ReusableBitmap result); /** - * The task has been canceled, and {@link #onDecodeComplete(Request, ReusableBitmap)} will - * not be called. + * The task has been canceled, and {@link #onDecodeComplete(RequestKey, ReusableBitmap)} + * will not be called. */ - void onDecodeCancel(Request key); + void onDecodeCancel(RequestKey key); } - public DecodeTask(Request key, int w, int h, DecodeCallback view, + public DecodeTask(RequestKey key, int w, int h, DecodeCallback view, BitmapCache cache) { mKey = key; mDestW = w; diff --git a/src/com/android/bitmap/RequestKey.java b/src/com/android/bitmap/RequestKey.java new file mode 100644 index 0000000..eadb42d --- /dev/null +++ b/src/com/android/bitmap/RequestKey.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.bitmap; + +import android.content.res.AssetFileDescriptor; + + +import java.io.IOException; +import java.io.InputStream; + +/** + * The decode task uses this class to get input to decode. You must implement at least one of + * {@link #createFd()} or {@link #createInputStream()}. {@link DecodeTask} will prioritize + * {@link #createFd()} before falling back to {@link #createInputStream()}. + *

+ * Objects of this type will also serve as cache keys to fetch cached data for {@link PooledCache}s, + * so they must implement {@link #equals(Object)} and {@link #hashCode()}. + */ + +public interface RequestKey { + + @Override + public boolean equals(Object o); + + @Override + public int hashCode(); + + /** + * Create an {@link AssetFileDescriptor} for a local file stored on the device. This method will + * be called first; if it returns null, {@link #createInputStream()} will be called. + */ + public AssetFileDescriptor createFd() throws IOException; + + /** + * Create an {@link InputStream} for a file. This method will be called if {@link #createFd()} + * returns null. + */ + public InputStream createInputStream() throws IOException; + + /** + * Return true if the image source may have be oriented in either portrait or landscape, and + * will need to be automatically re-oriented based on accompanying Exif metadata. + */ + public boolean hasOrientationExif() throws IOException; +} \ No newline at end of file diff --git a/src/com/android/bitmap/UnrefedBitmapCache.java b/src/com/android/bitmap/UnrefedBitmapCache.java index 6c9db73..428880e 100644 --- a/src/com/android/bitmap/UnrefedBitmapCache.java +++ b/src/com/android/bitmap/UnrefedBitmapCache.java @@ -19,7 +19,6 @@ package com.android.bitmap; import android.util.Log; import android.util.LruCache; -import com.android.bitmap.DecodeTask.Request; import com.android.bitmap.ReusableBitmap.NullReusableBitmap; import com.android.bitmap.util.Trace; @@ -33,12 +32,12 @@ import com.android.bitmap.util.Trace; * when the same key is used to retrieve the value, a {@link NullReusableBitmap} singleton will * be returned. */ -public class UnrefedBitmapCache extends UnrefedPooledCache +public class UnrefedBitmapCache extends UnrefedPooledCache implements BitmapCache { private boolean mBlocking = false; private final Object mLock = new Object(); - private LruCache mNullRequests; + private LruCache mNullRequests; private final static boolean DEBUG = false; private final static String TAG = UnrefedBitmapCache.class.getSimpleName(); @@ -48,7 +47,7 @@ public class UnrefedBitmapCache extends UnrefedPooledCache 0) { - mNullRequests = new LruCache(nullCapacity); + mNullRequests = new LruCache(nullCapacity); } } @@ -118,7 +117,7 @@ public class UnrefedBitmapCache extends UnrefedPooledCache