Skip to content
This repository was archived by the owner on Oct 15, 2018. It is now read-only.

Commit 4b94da8

Browse files
author
Chris Banes
committed
Add getSource() to CacheableBitmapDrawable
1 parent b373472 commit 4b94da8

File tree

6 files changed

+95
-21
lines changed

6 files changed

+95
-21
lines changed

library/src/uk/co/senab/bitmapcache/BitmapLruCache.java

+22-13
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,9 @@ public CacheableBitmapDrawable getFromDiskCache(final String url,
269269
try {
270270
final String key = transformUrlForDiskCacheKey(url);
271271
// Try and decode bitmap
272-
Bitmap bitmap = decodeBitmap(new SnapshotInputStreamProvider(key), decodeOpts);
272+
result = decodeBitmap(new SnapshotInputStreamProvider(key), url, decodeOpts);
273273

274-
if (null != bitmap) {
275-
result = new CacheableBitmapDrawable(url, mResources, bitmap,
276-
mRecyclePolicy);
274+
if (null != result) {
277275
if (null != mMemoryCache) {
278276
mMemoryCache.put(result);
279277
}
@@ -362,7 +360,7 @@ public CacheableBitmapDrawable put(final String url, final Bitmap bitmap,
362360
Bitmap.CompressFormat compressFormat, int compressQuality) {
363361

364362
CacheableBitmapDrawable d = new CacheableBitmapDrawable(url, mResources, bitmap,
365-
mRecyclePolicy);
363+
mRecyclePolicy, CacheableBitmapDrawable.SOURCE_UNKNOWN);
366364

367365
if (null != mMemoryCache) {
368366
mMemoryCache.put(d);
@@ -448,11 +446,9 @@ public CacheableBitmapDrawable put(final String url, final InputStream inputStre
448446

449447
if (null != tmpFile) {
450448
// Try and decode File
451-
Bitmap bitmap = decodeBitmap(new FileInputStreamProvider(tmpFile), decodeOpts);
452-
453-
if (null != bitmap) {
454-
d = new CacheableBitmapDrawable(url, mResources, bitmap, mRecyclePolicy);
449+
d = decodeBitmap(new FileInputStreamProvider(tmpFile), url, decodeOpts);
455450

451+
if (d != null) {
456452
if (null != mMemoryCache) {
457453
d.setCached(true);
458454
mMemoryCache.put(d.getUrl(), d);
@@ -553,9 +549,12 @@ private void scheduleDiskCacheFlush() {
553549
TimeUnit.SECONDS);
554550
}
555551

556-
private Bitmap decodeBitmap(InputStreamProvider ip, BitmapFactory.Options opts) {
552+
private CacheableBitmapDrawable decodeBitmap(InputStreamProvider ip, String url,
553+
BitmapFactory.Options opts) {
554+
557555
Bitmap bm = null;
558556
InputStream is = null;
557+
int source = CacheableBitmapDrawable.SOURCE_NEW;
559558

560559
try {
561560
if (mRecyclePolicy.canInBitmap()) {
@@ -566,7 +565,10 @@ private Bitmap decodeBitmap(InputStreamProvider ip, BitmapFactory.Options opts)
566565

567566
if (opts.inSampleSize <= 1) {
568567
opts.inSampleSize = 1;
569-
addInBitmapOptions(ip, opts);
568+
569+
if (addInBitmapOptions(ip, opts)) {
570+
source = CacheableBitmapDrawable.SOURCE_INBITMAP;
571+
}
570572
}
571573
}
572574

@@ -579,10 +581,14 @@ private Bitmap decodeBitmap(InputStreamProvider ip, BitmapFactory.Options opts)
579581
} finally {
580582
IoUtils.closeStream(is);
581583
}
582-
return bm;
584+
585+
if (bm != null) {
586+
return new CacheableBitmapDrawable(url, mResources, bm, mRecyclePolicy, source);
587+
}
588+
return null;
583589
}
584590

585-
private void addInBitmapOptions(InputStreamProvider ip, BitmapFactory.Options opts) {
591+
private boolean addInBitmapOptions(InputStreamProvider ip, BitmapFactory.Options opts) {
586592
// Create InputStream for decoding the bounds
587593
final InputStream is = ip.getInputStream();
588594
// Decode the bounds so we know what size Bitmap to look for
@@ -602,7 +608,10 @@ private void addInBitmapOptions(InputStreamProvider ip, BitmapFactory.Options op
602608
Log.i(Constants.LOG_TAG, "Using inBitmap");
603609
}
604610
SDK11.addInBitmapOption(opts, reusableBm);
611+
return true;
605612
}
613+
614+
return false;
606615
}
607616

608617
/**

library/src/uk/co/senab/bitmapcache/CacheableBitmapDrawable.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
public class CacheableBitmapDrawable extends BitmapDrawable {
2828

29+
public static final int SOURCE_UNKNOWN = -1;
30+
public static final int SOURCE_NEW = 0;
31+
public static final int SOURCE_INBITMAP = 1;
32+
2933
static final String LOG_TAG = "CacheableBitmapDrawable";
3034

3135
// URL Associated with this Bitmap
@@ -53,15 +57,18 @@ public class CacheableBitmapDrawable extends BitmapDrawable {
5357

5458
private final int mMemorySize;
5559

60+
private final int mSource;
61+
5662
CacheableBitmapDrawable(String url, Resources resources, Bitmap bitmap,
57-
BitmapLruCache.RecyclePolicy recyclePolicy) {
63+
BitmapLruCache.RecyclePolicy recyclePolicy, int source) {
5864
super(resources, bitmap);
5965

6066
mMemorySize = null != bitmap ? (bitmap.getRowBytes() * bitmap.getHeight()) : 0;
6167
mUrl = url;
6268
mRecyclePolicy = recyclePolicy;
6369
mDisplayingCount = 0;
6470
mCacheCount = 0;
71+
mSource = source;
6572
}
6673

6774
@Override
@@ -94,6 +101,14 @@ public String getUrl() {
94101
return mUrl;
95102
}
96103

104+
/**
105+
* @return One of {@link #SOURCE_NEW}, {@link #SOURCE_INBITMAP} or {@link #SOURCE_UNKNOWN}
106+
* depending on how this Bitmap was created.
107+
*/
108+
public int getSource() {
109+
return mSource;
110+
}
111+
97112
/**
98113
* Returns true when this wrapper has a bitmap and the bitmap has not been recycled.
99114
*

sample/res/values/colors.xml

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
<color name="translucent_red">#CCff4444</color>
55
<color name="translucent_green">#CC99cc00</color>
6+
<color name="translucent_blue">#CC33B5E5</color>
67

78
</resources>

sample/src/uk/co/senab/bitmapcache/samples/NetworkedCacheableImageView.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
*/
4747
public class NetworkedCacheableImageView extends CacheableImageView {
4848

49+
public interface OnImageLoadedListener {
50+
void onImageLoaded(CacheableBitmapDrawable result);
51+
}
52+
4953
/**
5054
* This task simply fetches an Bitmap from the specified URL and wraps it in a wrapper. This
5155
* implementation is NOT 'best practice' or production ready code.
@@ -56,13 +60,15 @@ private static class ImageUrlAsyncTask
5660
private final BitmapLruCache mCache;
5761

5862
private final WeakReference<ImageView> mImageViewRef;
63+
private final OnImageLoadedListener mListener;
5964

6065
private final BitmapFactory.Options mDecodeOpts;
6166

6267
ImageUrlAsyncTask(ImageView imageView, BitmapLruCache cache,
63-
BitmapFactory.Options decodeOpts) {
68+
BitmapFactory.Options decodeOpts, OnImageLoadedListener listener) {
6469
mCache = cache;
6570
mImageViewRef = new WeakReference<ImageView>(imageView);
71+
mListener = listener;
6672
mDecodeOpts = decodeOpts;
6773
}
6874

@@ -109,6 +115,10 @@ protected void onPostExecute(CacheableBitmapDrawable result) {
109115
if (null != iv) {
110116
iv.setImageDrawable(result);
111117
}
118+
119+
if (null != mListener) {
120+
mListener.onImageLoaded(result);
121+
}
112122
}
113123
}
114124

@@ -128,7 +138,7 @@ public NetworkedCacheableImageView(Context context, AttributeSet attrs) {
128138
* @param fullSize - Whether the image should be kept at the original size
129139
* @return true if the bitmap was found in the cache
130140
*/
131-
public boolean loadImage(String url, final boolean fullSize) {
141+
public boolean loadImage(String url, final boolean fullSize, OnImageLoadedListener listener) {
132142
// First check whether there's already a task running, if so cancel it
133143
if (null != mCurrentTask) {
134144
mCurrentTask.cancel(true);
@@ -154,7 +164,7 @@ public boolean loadImage(String url, final boolean fullSize) {
154164
//decodeOpts.inSampleSize = 2;
155165
}
156166

157-
mCurrentTask = new ImageUrlAsyncTask(this, mCache, decodeOpts);
167+
mCurrentTask = new ImageUrlAsyncTask(this, mCache, decodeOpts, listener);
158168

159169
try {
160170
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

sample/src/uk/co/senab/bitmapcache/samples/PugListAdapter.java

+42-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
import android.widget.BaseAdapter;
2323
import android.widget.TextView;
2424

25+
import java.lang.ref.WeakReference;
2526
import java.util.ArrayList;
2627

28+
import uk.co.senab.bitmapcache.CacheableBitmapDrawable;
29+
2730
public class PugListAdapter extends BaseAdapter {
2831

2932
private final ArrayList<String> mPugUrls;
@@ -61,17 +64,53 @@ public View getView(int position, View convertView, ViewGroup parent) {
6164
.findViewById(R.id.nciv_pug);
6265
TextView status = (TextView) convertView.findViewById(R.id.tv_status);
6366

64-
final boolean fromCache = imageView.loadImage(mPugUrls.get(position), false);
67+
final boolean fromCache = imageView
68+
.loadImage(mPugUrls.get(position), false, new UpdateTextViewListener(status));
6569

6670
if (fromCache) {
6771
status.setText("From Memory Cache");
6872
status.setBackgroundColor(mContext.getResources().getColor(R.color.translucent_green));
6973
} else {
70-
status.setText("From Disk/Network");
71-
status.setBackgroundColor(mContext.getResources().getColor(R.color.translucent_red));
74+
status.setText("Loading...");
75+
status.setBackgroundDrawable(null);
7276
}
7377

7478
return convertView;
7579
}
7680

81+
static class UpdateTextViewListener
82+
implements NetworkedCacheableImageView.OnImageLoadedListener {
83+
private final WeakReference<TextView> mTextViewRef;
84+
85+
public UpdateTextViewListener(TextView tv) {
86+
mTextViewRef = new WeakReference<TextView>(tv);
87+
}
88+
89+
@Override
90+
public void onImageLoaded(CacheableBitmapDrawable result) {
91+
final TextView tv = mTextViewRef.get();
92+
if (tv == null) {
93+
return;
94+
}
95+
96+
if (result == null) {
97+
tv.setText("Failed");
98+
tv.setBackgroundDrawable(null);
99+
return;
100+
}
101+
102+
switch (result.getSource()) {
103+
case CacheableBitmapDrawable.SOURCE_UNKNOWN:
104+
case CacheableBitmapDrawable.SOURCE_NEW:
105+
tv.setText("From Disk/Network");
106+
tv.setBackgroundColor(tv.getResources().getColor(R.color.translucent_red));
107+
break;
108+
case CacheableBitmapDrawable.SOURCE_INBITMAP:
109+
tv.setText("Reused Bitmap");
110+
tv.setBackgroundColor(tv.getResources().getColor(R.color.translucent_blue));
111+
break;
112+
}
113+
}
114+
}
115+
77116
}

sample/src/uk/co/senab/bitmapcache/samples/PugPagerAdapter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public View instantiateItem(ViewGroup container, int position) {
4545
NetworkedCacheableImageView imageView = new NetworkedCacheableImageView(mContext, null);
4646

4747
String pugUrl = mPugUrls.get(position);
48-
imageView.loadImage(pugUrl, true);
48+
imageView.loadImage(pugUrl, true, null);
4949

5050
imageView.setScaleType(ScaleType.FIT_CENTER);
5151
container.addView(imageView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

0 commit comments

Comments
 (0)