summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Blitzstein <sblitz@google.com>2013-10-16 12:12:54 -0700
committerSam Blitzstein <sblitz@google.com>2013-10-16 15:16:43 -0700
commit40662f4b39e795d9c64502b13036e7c37fa2d373 (patch)
tree9caf8aa9e550d9d03e167a6fd519bcf5b6b5a138
parent93a35b93dc582e38ff8ee5979754a16b4bf4da0c (diff)
downloadbitmap-40662f4b39e795d9c64502b13036e7c37fa2d373.tar.gz
Change BitmapRequestKey to be more cleanly implementable.
Change-Id: I831586688605e6c6c2f2f7a879c6be23175f71de
-rw-r--r--src/com/android/bitmap/BitmapCache.java2
-rw-r--r--src/com/android/bitmap/DecodeAggregator.java6
-rw-r--r--src/com/android/bitmap/DecodeTask.java28
-rw-r--r--src/com/android/bitmap/RequestKey.java59
-rw-r--r--src/com/android/bitmap/UnrefedBitmapCache.java11
-rw-r--r--src/com/android/bitmap/drawable/BasicBitmapDrawable.java16
-rw-r--r--src/com/android/bitmap/drawable/BitmapRequestKey.java60
-rw-r--r--src/com/android/bitmap/drawable/ExtendedBitmapDrawable.java20
8 files changed, 92 insertions, 110 deletions
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<DecodeTask.Request, ReusableBitmap> {
+public interface BitmapCache extends PooledCache<RequestKey, ReusableBitmap> {
/**
* 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<RequestKey> {
-public class DecodeAggregator extends ContiguousFIFOAggregator<Request> {
-
- public interface Callback extends ContiguousFIFOAggregator.Callback<Request> {
+ public interface Callback extends ContiguousFIFOAggregator.Callback<RequestKey> {
}
}
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<Void, Void, ReusableBitmap> {
- private final Request mKey;
+ private final RequestKey mKey;
private final int mDestW;
private final int mDestH;
private final DecodeCallback mDecodeCallback;
@@ -61,20 +61,6 @@ public class DecodeTask extends AsyncTask<Void, Void, ReusableBitmap> {
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()}.
- * <p>
- * 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.
*/
public interface DecodeCallback {
@@ -84,20 +70,20 @@ public class DecodeTask extends AsyncTask<Void, Void, ReusableBitmap> {
* <p>
* 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()}.
+ * <p>
+ * 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<Request, ReusableBitmap>
+public class UnrefedBitmapCache extends UnrefedPooledCache<RequestKey, ReusableBitmap>
implements BitmapCache {
private boolean mBlocking = false;
private final Object mLock = new Object();
- private LruCache<Request, NullReusableBitmap> mNullRequests;
+ private LruCache<RequestKey, NullReusableBitmap> mNullRequests;
private final static boolean DEBUG = false;
private final static String TAG = UnrefedBitmapCache.class.getSimpleName();
@@ -48,7 +47,7 @@ public class UnrefedBitmapCache extends UnrefedPooledCache<Request, ReusableBitm
super(targetSizeBytes, nonPooledFraction);
if (nullCapacity > 0) {
- mNullRequests = new LruCache<Request, NullReusableBitmap>(nullCapacity);
+ mNullRequests = new LruCache<RequestKey, NullReusableBitmap>(nullCapacity);
}
}
@@ -118,7 +117,7 @@ public class UnrefedBitmapCache extends UnrefedPooledCache<Request, ReusableBitm
}
@Override
- public ReusableBitmap get(final Request key, final boolean incrementRefCount) {
+ public ReusableBitmap get(final RequestKey key, final boolean incrementRefCount) {
if (mNullRequests != null && mNullRequests.get(key) != null) {
return NullReusableBitmap.getInstance();
}
@@ -129,7 +128,7 @@ public class UnrefedBitmapCache extends UnrefedPooledCache<Request, ReusableBitm
* Note: The cache only supports same-sized bitmaps.
*/
@Override
- public ReusableBitmap put(final Request key, final ReusableBitmap value) {
+ public ReusableBitmap put(final RequestKey key, final ReusableBitmap value) {
if (mNullRequests != null && (value == null || value == NullReusableBitmap.getInstance())) {
mNullRequests.put(key, NullReusableBitmap.getInstance());
return null;
diff --git a/src/com/android/bitmap/drawable/BasicBitmapDrawable.java b/src/com/android/bitmap/drawable/BasicBitmapDrawable.java
index b16618b..2aa6011 100644
--- a/src/com/android/bitmap/drawable/BasicBitmapDrawable.java
+++ b/src/com/android/bitmap/drawable/BasicBitmapDrawable.java
@@ -27,7 +27,7 @@ import android.util.Log;
import com.android.bitmap.BitmapCache;
import com.android.bitmap.DecodeTask;
-import com.android.bitmap.DecodeTask.Request;
+import com.android.bitmap.RequestKey;
import com.android.bitmap.ReusableBitmap;
import com.android.bitmap.util.BitmapUtils;
import com.android.bitmap.util.RectUtils;
@@ -47,7 +47,7 @@ import java.util.concurrent.TimeUnit;
public class BasicBitmapDrawable extends Drawable implements DecodeTask.DecodeCallback,
Drawable.Callback {
- private BitmapRequestKey mCurrKey;
+ private RequestKey mCurrKey;
private ReusableBitmap mBitmap;
private final BitmapCache mCache;
private DecodeTask mTask;
@@ -81,7 +81,7 @@ public class BasicBitmapDrawable extends Drawable implements DecodeTask.DecodeCa
mPaint.setFilterBitmap(true);
}
- public DecodeTask.Request getKey() {
+ public RequestKey getKey() {
return mCurrKey;
}
@@ -98,11 +98,11 @@ public class BasicBitmapDrawable extends Drawable implements DecodeTask.DecodeCa
setImage(null);
}
- public void bind(BitmapRequestKey key) {
+ public void bind(RequestKey key) {
setImage(key);
}
- private void setImage(final BitmapRequestKey key) {
+ private void setImage(final RequestKey key) {
if (mCurrKey != null && mCurrKey.equals(key)) {
return;
}
@@ -201,10 +201,10 @@ public class BasicBitmapDrawable extends Drawable implements DecodeTask.DecodeCa
}
@Override
- public void onDecodeBegin(final Request key) { }
+ public void onDecodeBegin(final RequestKey key) { }
@Override
- public void onDecodeComplete(final Request key, final ReusableBitmap result) {
+ public void onDecodeComplete(final RequestKey key, final ReusableBitmap result) {
if (key.equals(mCurrKey)) {
setBitmap(result);
} else {
@@ -217,7 +217,7 @@ public class BasicBitmapDrawable extends Drawable implements DecodeTask.DecodeCa
}
@Override
- public void onDecodeCancel(final Request key) { }
+ public void onDecodeCancel(final RequestKey key) { }
private void setBitmap(ReusableBitmap bmp) {
if (mBitmap != null && mBitmap != bmp) {
diff --git a/src/com/android/bitmap/drawable/BitmapRequestKey.java b/src/com/android/bitmap/drawable/BitmapRequestKey.java
deleted file mode 100644
index be3a7a1..0000000
--- a/src/com/android/bitmap/drawable/BitmapRequestKey.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * 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.drawable;
-
-import android.content.res.AssetFileDescriptor;
-import android.text.TextUtils;
-
-import com.android.bitmap.DecodeTask;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-/*
- * Extend this base class to return either createFd() or createInputStream().
- */
-public abstract class BitmapRequestKey implements DecodeTask.Request {
- public final String mUriString;
-
- public BitmapRequestKey(String uriString) {
- this.mUriString = uriString;
- }
-
- @Override
- public abstract boolean equals(Object o);
-
- @Override
- public abstract int hashCode();
-
- @Override
- public abstract String toString();
-
- @Override
- public AssetFileDescriptor createFd() throws IOException {
- return null;
- }
-
- @Override
- public InputStream createInputStream() throws IOException {
- return null;
- }
-
- @Override
- public boolean hasOrientationExif() throws IOException {
- return false;
- }
-} \ No newline at end of file
diff --git a/src/com/android/bitmap/drawable/ExtendedBitmapDrawable.java b/src/com/android/bitmap/drawable/ExtendedBitmapDrawable.java
index f3a3301..01f5638 100644
--- a/src/com/android/bitmap/drawable/ExtendedBitmapDrawable.java
+++ b/src/com/android/bitmap/drawable/ExtendedBitmapDrawable.java
@@ -36,7 +36,7 @@ import com.android.bitmap.R;
import com.android.bitmap.BitmapCache;
import com.android.bitmap.DecodeAggregator;
import com.android.bitmap.DecodeTask;
-import com.android.bitmap.DecodeTask.Request;
+import com.android.bitmap.RequestKey;
import com.android.bitmap.ReusableBitmap;
import com.android.bitmap.util.BitmapUtils;
import com.android.bitmap.util.RectUtils;
@@ -58,7 +58,7 @@ import java.util.concurrent.TimeUnit;
public class ExtendedBitmapDrawable extends Drawable implements DecodeTask.DecodeCallback,
Drawable.Callback, Runnable, Parallaxable, DecodeAggregator.Callback {
- private BitmapRequestKey mCurrKey;
+ private RequestKey mCurrKey;
private ReusableBitmap mBitmap;
private final BitmapCache mCache;
private DecodeAggregator mDecodeAggregator;
@@ -122,7 +122,7 @@ public class ExtendedBitmapDrawable extends Drawable implements DecodeTask.Decod
mProgress.setCallback(this);
}
- public DecodeTask.Request getKey() {
+ public RequestKey getKey() {
return mCurrKey;
}
@@ -149,11 +149,11 @@ public class ExtendedBitmapDrawable extends Drawable implements DecodeTask.Decod
setImage(null);
}
- public void bind(BitmapRequestKey key) {
+ public void bind(RequestKey key) {
setImage(key);
}
- private void setImage(final BitmapRequestKey key) {
+ private void setImage(final RequestKey key) {
if (mCurrKey != null && mCurrKey.equals(key)) {
return;
}
@@ -284,7 +284,7 @@ public class ExtendedBitmapDrawable extends Drawable implements DecodeTask.Decod
}
@Override
- public void onDecodeBegin(final Request key) {
+ public void onDecodeBegin(final RequestKey key) {
if (mDecodeAggregator != null) {
mDecodeAggregator.expect(key, this);
} else {
@@ -293,7 +293,7 @@ public class ExtendedBitmapDrawable extends Drawable implements DecodeTask.Decod
}
@Override
- public void onBecomeFirstExpected(final Request key) {
+ public void onBecomeFirstExpected(final RequestKey key) {
if (!key.equals(mCurrKey)) {
return;
}
@@ -310,7 +310,7 @@ public class ExtendedBitmapDrawable extends Drawable implements DecodeTask.Decod
}
@Override
- public void onDecodeComplete(final Request key, final ReusableBitmap result) {
+ public void onDecodeComplete(final RequestKey key, final ReusableBitmap result) {
if (mDecodeAggregator != null) {
mDecodeAggregator.execute(key, new Runnable() {
@Override
@@ -328,7 +328,7 @@ public class ExtendedBitmapDrawable extends Drawable implements DecodeTask.Decod
}
}
- private void onDecodeCompleteImpl(final Request key, final ReusableBitmap result) {
+ private void onDecodeCompleteImpl(final RequestKey key, final ReusableBitmap result) {
if (key.equals(mCurrKey)) {
setBitmap(result);
} else {
@@ -341,7 +341,7 @@ public class ExtendedBitmapDrawable extends Drawable implements DecodeTask.Decod
}
@Override
- public void onDecodeCancel(final Request key) {
+ public void onDecodeCancel(final RequestKey key) {
if (mDecodeAggregator != null) {
mDecodeAggregator.forget(key);
}