Skip to content

Commit 56f4df8

Browse files
committed
1) Issue nostra13#356 - Fixed loadImage(...) bug in 1.8.6 (onLoadingCancelled() is always fired)
2) New API: ImageLoader.displayImage(String, ImageAware, DisplayImageOptions, ImageLoadingListener) - allows display images not only in ImageView but in any view which implements ImageAware interface - avoid creating fake ImageView in loadImage(...) 3) Changed API: BitmapDisplayer.display(Bitmap, ImageView, LoadedFrom) -> .display(Bitmap, ImageAware, LoadedFrom). Passed ImageView is available by ImageAware.getWrappedView() 4) Not keep context in configuration
1 parent dcb5e79 commit 56f4df8

18 files changed

+677
-340
lines changed

library/src/com/nostra13/universalimageloader/core/DefaultConfigurationFactory.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ public static DiscCacheAware createDiscCache(Context context, FileNameGenerator
7676
}
7777

7878
/** Creates reserve disc cache which will be used if primary disc cache becomes unavailable */
79-
public static DiscCacheAware createReserveDiscCache(Context context) {
80-
File cacheDir = context.getCacheDir();
79+
public static DiscCacheAware createReserveDiscCache(File cacheDir) {
8180
File individualDir = new File(cacheDir, "uil-images");
8281
if (individualDir.exists() || individualDir.mkdir()) {
8382
cacheDir = individualDir;

library/src/com/nostra13/universalimageloader/core/DisplayBitmapTask.java

+18-19
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919
import android.widget.ImageView;
2020
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
2121
import com.nostra13.universalimageloader.core.assist.LoadedFrom;
22+
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
2223
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
2324
import com.nostra13.universalimageloader.utils.L;
2425

25-
import java.lang.ref.Reference;
26-
2726
/**
2827
* Displays bitmap in {@link ImageView}. Must be called on UI thread.
2928
*
@@ -34,13 +33,13 @@
3433
*/
3534
final class DisplayBitmapTask implements Runnable {
3635

37-
private static final String LOG_DISPLAY_IMAGE_IN_IMAGEVIEW = "Display image in ImageView (loaded from %1$s) [%2$s]";
38-
private static final String LOG_TASK_CANCELLED_IMAGEVIEW_REUSED = "ImageView is reused for another image. Task is cancelled. [%s]";
39-
private static final String LOG_TASK_CANCELLED_IMAGEVIEW_LOST = "ImageView was collected by GC. Task is cancelled. [%s]";
36+
private static final String LOG_DISPLAY_IMAGE_IN_IMAGEVIEW = "Display image in ImageAware (loaded from %1$s) [%2$s]";
37+
private static final String LOG_TASK_CANCELLED_IMAGEVIEW_REUSED = "ImageAware is reused for another image. Task is cancelled. [%s]";
38+
private static final String LOG_TASK_CANCELLED_IMAGEVIEW_LOST = "ImageAware was collected by GC. Task is cancelled. [%s]";
4039

4140
private final Bitmap bitmap;
4241
private final String imageUri;
43-
private final Reference<ImageView> imageViewRef;
42+
private final ImageAware imageAware;
4443
private final String memoryCacheKey;
4544
private final BitmapDisplayer displayer;
4645
private final ImageLoadingListener listener;
@@ -49,10 +48,11 @@ final class DisplayBitmapTask implements Runnable {
4948

5049
private boolean loggingEnabled;
5150

52-
public DisplayBitmapTask(Bitmap bitmap, ImageLoadingInfo imageLoadingInfo, ImageLoaderEngine engine, LoadedFrom loadedFrom) {
51+
public DisplayBitmapTask(Bitmap bitmap, ImageLoadingInfo imageLoadingInfo, ImageLoaderEngine engine,
52+
LoadedFrom loadedFrom) {
5353
this.bitmap = bitmap;
5454
imageUri = imageLoadingInfo.uri;
55-
imageViewRef = imageLoadingInfo.imageViewRef;
55+
imageAware = imageLoadingInfo.imageAware;
5656
memoryCacheKey = imageLoadingInfo.memoryCacheKey;
5757
displayer = imageLoadingInfo.options.getDisplayer();
5858
listener = imageLoadingInfo.listener;
@@ -61,24 +61,23 @@ public DisplayBitmapTask(Bitmap bitmap, ImageLoadingInfo imageLoadingInfo, Image
6161
}
6262

6363
public void run() {
64-
ImageView imageView = imageViewRef.get();
65-
if (imageView == null) {
64+
if (imageAware.isCollected()) {
6665
if (loggingEnabled) L.d(LOG_TASK_CANCELLED_IMAGEVIEW_LOST, memoryCacheKey);
67-
listener.onLoadingCancelled(imageUri, imageView);
68-
} else if (isViewWasReused(imageView)) {
66+
listener.onLoadingCancelled(imageUri, imageAware.getWrappedView());
67+
} else if (isViewWasReused()) {
6968
if (loggingEnabled) L.d(LOG_TASK_CANCELLED_IMAGEVIEW_REUSED, memoryCacheKey);
70-
listener.onLoadingCancelled(imageUri, imageView);
69+
listener.onLoadingCancelled(imageUri, imageAware.getWrappedView());
7170
} else {
7271
if (loggingEnabled) L.d(LOG_DISPLAY_IMAGE_IN_IMAGEVIEW, loadedFrom, memoryCacheKey);
73-
Bitmap displayedBitmap = displayer.display(bitmap, imageView, loadedFrom);
74-
listener.onLoadingComplete(imageUri, imageView, displayedBitmap);
75-
engine.cancelDisplayTaskFor(imageView);
72+
Bitmap displayedBitmap = displayer.display(bitmap, imageAware, loadedFrom);
73+
listener.onLoadingComplete(imageUri, imageAware.getWrappedView(), displayedBitmap);
74+
engine.cancelDisplayTaskFor(imageAware);
7675
}
7776
}
7877

79-
/** Checks whether memory cache key (image URI) for current ImageView is actual */
80-
private boolean isViewWasReused(ImageView imageView) {
81-
String currentCacheKey = engine.getLoadingUriForView(imageView);
78+
/** Checks whether memory cache key (image URI) for current ImageAware is actual */
79+
private boolean isViewWasReused() {
80+
String currentCacheKey = engine.getLoadingUriForView(imageAware);
8281
return !memoryCacheKey.equals(currentCacheKey);
8382
}
8483

library/src/com/nostra13/universalimageloader/core/DisplayImageOptions.java

+10-33
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*******************************************************************************/
1616
package com.nostra13.universalimageloader.core;
1717

18+
import android.content.res.Resources;
1819
import android.graphics.Bitmap;
1920
import android.graphics.BitmapFactory.Options;
2021
import android.graphics.drawable.Drawable;
@@ -97,28 +98,16 @@ private DisplayImageOptions(Builder builder) {
9798
handler = builder.handler;
9899
}
99100

100-
public boolean shouldShowImageResOnLoading() {
101-
return imageResOnLoading != 0;
102-
}
103-
104101
public boolean shouldShowImageOnLoading() {
105-
return imageOnLoading != null;
106-
}
107-
108-
public boolean shouldShowImageResForEmptyUri() {
109-
return imageResForEmptyUri != 0;
102+
return imageOnLoading != null || imageResOnLoading != 0;
110103
}
111104

112105
public boolean shouldShowImageForEmptyUri() {
113-
return imageForEmptyUri != null;
114-
}
115-
116-
public boolean shouldShowImageResOnFail() {
117-
return imageResOnFail != 0;
106+
return imageForEmptyUri != null || imageResForEmptyUri != 0;
118107
}
119108

120109
public boolean shouldShowImageOnFail() {
121-
return imageOnFail != null;
110+
return imageOnFail != null || imageResOnFail != 0;
122111
}
123112

124113
public boolean shouldPreProcess() {
@@ -133,28 +122,16 @@ public boolean shouldDelayBeforeLoading() {
133122
return delayBeforeLoading > 0;
134123
}
135124

136-
public int getImageResOnLoading() {
137-
return imageResOnLoading;
138-
}
139-
140-
public Drawable getImageOnLoading() {
141-
return imageOnLoading;
142-
}
143-
144-
public int getImageResForEmptyUri() {
145-
return imageResForEmptyUri;
146-
}
147-
148-
public Drawable getImageForEmptyUri() {
149-
return imageForEmptyUri;
125+
public Drawable getImageOnLoading(Resources res) {
126+
return imageResOnFail != 0 ? res.getDrawable(imageResOnLoading) : imageOnLoading;
150127
}
151128

152-
public int getImageResOnFail() {
153-
return imageResOnFail;
129+
public Drawable getImageForEmptyUri(Resources res) {
130+
return imageResForEmptyUri != 0 ? res.getDrawable(imageResForEmptyUri) : imageForEmptyUri;
154131
}
155132

156-
public Drawable getImageOnFail() {
157-
return imageOnFail;
133+
public Drawable getImageOnFail(Resources res) {
134+
return imageResOnFail != 0 ? res.getDrawable(imageResOnFail) : imageOnFail;
158135
}
159136

160137
public boolean isResetViewBeforeLoading() {

0 commit comments

Comments
 (0)