Skip to content

Commit

Permalink
Changed API: Added params to ImageLoadingListener callbacks
Browse files Browse the repository at this point in the history
(String imageUri, Object extra) (#130)
Introduced DisplayImageOptions.extraForListener(Object extra)
  • Loading branch information
nostra13 committed Feb 9, 2013
1 parent 7b0b574 commit 19bf3a9
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,33 @@
final class DisplayBitmapTask implements Runnable {

private final Bitmap bitmap;
private final String imageUri;
private final ImageView imageView;
private final String memoryCacheKey;
private final BitmapDisplayer bitmapDisplayer;
private final DisplayImageOptions options;
private final ImageLoadingListener listener;
private final ImageLoaderEngine engine;

private boolean loggingEnabled;

public DisplayBitmapTask(Bitmap bitmap, ImageLoadingInfo imageLoadingInfo, ImageLoaderEngine engine) {
this.bitmap = bitmap;
imageUri = imageLoadingInfo.uri;
imageView = imageLoadingInfo.imageView;
memoryCacheKey = imageLoadingInfo.memoryCacheKey;
bitmapDisplayer = imageLoadingInfo.options.getDisplayer();
options = imageLoadingInfo.options;
listener = imageLoadingInfo.listener;
this.engine = engine;
}

public void run() {
if (isViewWasReused()) {
if (loggingEnabled) L.i(LOG_TASK_CANCELLED, memoryCacheKey);
listener.onLoadingCancelled();
listener.onLoadingCancelled(imageUri, options.getExtraForListener());
} else {
if (loggingEnabled) L.i(LOG_DISPLAY_IMAGE_IN_IMAGEVIEW, memoryCacheKey);
Bitmap displayedBitmap = bitmapDisplayer.display(bitmap, imageView);
listener.onLoadingComplete(displayedBitmap);
Bitmap displayedBitmap = options.getDisplayer().display(bitmap, imageView);
listener.onLoadingComplete(imageUri, options.getExtraForListener(), displayedBitmap);
engine.cancelDisplayTaskFor(imageView);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
Expand Down Expand Up @@ -46,6 +47,7 @@ public final class DisplayImageOptions {
private final Bitmap.Config bitmapConfig;
private final int delayBeforeLoading;
private final Object extraForDownloader;
private final Object extraForListener;
private final BitmapProcessor preProcessor;
private final BitmapProcessor postProcessor;
private final BitmapDisplayer displayer;
Expand All @@ -61,6 +63,7 @@ private DisplayImageOptions(Builder builder) {
bitmapConfig = builder.bitmapConfig;
delayBeforeLoading = builder.delayBeforeLoading;
extraForDownloader = builder.extraForDownloader;
extraForListener = builder.extraForListener;
preProcessor = builder.preProcessor;
postProcessor = builder.postProcessor;
displayer = builder.displayer;
Expand Down Expand Up @@ -130,6 +133,10 @@ Object getExtraForDownloader() {
return extraForDownloader;
}

Object getExtraForListener() {
return extraForListener;
}

BitmapProcessor getPreProcessor() {
return preProcessor;
}
Expand Down Expand Up @@ -158,6 +165,7 @@ public static class Builder {
private Bitmap.Config bitmapConfig = Bitmap.Config.ARGB_8888;
private int delayBeforeLoading = 0;
private Object extraForDownloader = null;
private Object extraForListener = null;
private BitmapProcessor preProcessor = null;
private BitmapProcessor postProcessor = null;
private BitmapDisplayer displayer = DefaultConfigurationFactory.createBitmapDisplayer();
Expand Down Expand Up @@ -239,6 +247,15 @@ public Builder extraForDownloader(Object extra) {
return this;
}

/**
* Sets auxiliary object which will be passed to {@link ImageLoadingListener
* ImageLoadingListener.onLoading***(...)} callbacks
*/
public Builder extraForListener(Object extra) {
this.extraForListener = extra;
return this;
}

/**
* Sets bitmap processor which will be process bitmaps before they will be cached in memory. So memory cache
* will contain bitmap processed by incoming preProcessor.<br />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,21 @@ public void displayImage(String uri, ImageView imageView, DisplayImageOptions op

if (uri == null || uri.length() == 0) {
engine.cancelDisplayTaskFor(imageView);
listener.onLoadingStarted();
listener.onLoadingStarted(uri, options.getExtraForListener());
if (options.shouldShowImageForEmptyUri()) {
imageView.setImageResource(options.getImageForEmptyUri());
} else {
imageView.setImageBitmap(null);
}
listener.onLoadingComplete(null);
listener.onLoadingComplete(uri, options.getExtraForListener(), null);
return;
}

ImageSize targetSize = getImageSizeScaleTo(imageView);
String memoryCacheKey = MemoryCacheUtil.generateKey(uri, targetSize);
engine.prepareDisplayTaskFor(imageView, memoryCacheKey);

listener.onLoadingStarted();
listener.onLoadingStarted(uri, options.getExtraForListener());
Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);
if (bmp != null && !bmp.isRecycled()) {
if (configuration.loggingEnabled) L.i(LOG_LOAD_IMAGE_FROM_MEMORY_CACHE, memoryCacheKey);
Expand All @@ -208,7 +208,7 @@ public void displayImage(String uri, ImageView imageView, DisplayImageOptions op
engine.submit(displayTask);
} else {
options.getDisplayer().display(bmp, imageView);
listener.onLoadingComplete(bmp);
listener.onLoadingComplete(uri, options.getExtraForListener(), bmp);
}
} else {
if (options.shouldShowStubImage()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ final class LoadAndDisplayImageTask implements Runnable {
private final ImageLoaderConfiguration configuration;
private final ImageDownloader downloader;
private final boolean loggingEnabled;
private final String uri;
final String uri;
private final String memoryCacheKey;
private final ImageView imageView;
final ImageView imageView;
private final ImageSize targetSize;
private final DisplayImageOptions options;
final DisplayImageOptions options;
final ImageLoadingListener listener;

public LoadAndDisplayImageTask(ImageLoaderEngine engine, ImageLoadingInfo imageLoadingInfo, Handler handler) {
Expand Down Expand Up @@ -172,7 +172,7 @@ private boolean checkTaskIsNotActual() {
handler.post(new Runnable() {
@Override
public void run() {
listener.onLoadingCancelled();
listener.onLoadingCancelled(uri, options.getExtraForListener());
}
});
}
Expand Down Expand Up @@ -335,7 +335,7 @@ public void run() {
if (options.shouldShowImageOnFail()) {
imageView.setImageResource(options.getImageOnFail());
}
listener.onLoadingFailed(failReason);
listener.onLoadingFailed(uri, options.getExtraForListener(), failReason);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.nostra13.universalimageloader.core.assist;

/**
* Presents the reason why image loading and displaying was failed
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public enum FailReason {
IO_ERROR, OUT_OF_MEMORY, UNKNOWN
package com.nostra13.universalimageloader.core.assist;

/**
* Presents the reason why image loading and displaying was failed
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public enum FailReason {
IO_ERROR, OUT_OF_MEMORY, UNKNOWN
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,55 @@
package com.nostra13.universalimageloader.core.assist;

import android.graphics.Bitmap;
import android.widget.ImageView;

/**
* Listener for image loading process.<br />
* You can use {@link SimpleImageLoadingListener} for implementing only needed methods.
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
* @see SimpleImageLoadingListener
* @see FailReason
*/
public interface ImageLoadingListener {

/** Is called when image loading task was started */
void onLoadingStarted();

/** Is called when an error was occurred during image loading */
void onLoadingFailed(FailReason failReason);

/** Is called when image is loaded successfully and displayed in {@link ImageView} */
void onLoadingComplete(Bitmap loadedImage);

/** Is called when image loading task was cancelled because {@link ImageView} was reused in newer task */
void onLoadingCancelled();
}
package com.nostra13.universalimageloader.core.assist;

import android.graphics.Bitmap;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;

/**
* Listener for image loading process.<br />
* You can use {@link SimpleImageLoadingListener} for implementing only needed methods.
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
* @see SimpleImageLoadingListener
* @see FailReason
*/
public interface ImageLoadingListener {

/**
* Is called when image loading task was started
*
* @param imageUri Loading image URI
* @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForListener(Object)
* DisplayImageOptions.extraForListener(Object)}
*/
void onLoadingStarted(String imageUri, Object extra);

/**
* Is called when an error was occurred during image loading
*
* @param imageUri Loading image URI
* @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForListener(Object)
* DisplayImageOptions.extraForListener(Object)}
* @param failReason {@linkplain FailReason The reason} why image loading was failed
*/
void onLoadingFailed(String imageUri, Object extra, FailReason failReason);

/**
* Is called when image is loaded successfully (and displayed in {@link ImageView} if one was specified)
*
* @param imageUri Loaded image URI
* @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForListener(Object)
* DisplayImageOptions.extraForListener(Object)}
* @param loadedImage Bitmap of loaded and decoded image
*/
void onLoadingComplete(String imageUri, Object extra, Bitmap loadedImage);

/**
* Is called when image loading task was cancelled because {@link ImageView} was reused in newer task
*
* @param imageUri Loading image URI
* @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForListener(Object)
* DisplayImageOptions.extraForListener(Object)}
*/
void onLoadingCancelled(String imageUri, Object extra);
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package com.nostra13.universalimageloader.core.assist;

import android.graphics.Bitmap;

/**
* A convenience class to extend when you only want to listen for a subset of all the image loading events. This
* implements all methods in the {@link ImageLoadingListener} but does nothing.
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public class SimpleImageLoadingListener implements ImageLoadingListener {
@Override
public void onLoadingStarted() {
// Empty implementation
}

@Override
public void onLoadingFailed(FailReason failReason) {
// Empty implementation
}

@Override
public void onLoadingComplete(Bitmap loadedImage) {
// Empty implementation
}

@Override
public void onLoadingCancelled() {
// Empty implementation
}
}
package com.nostra13.universalimageloader.core.assist;

import android.graphics.Bitmap;

/**
* A convenient class to extend when you only want to listen for a subset of all the image loading events. This
* implements all methods in the {@link ImageLoadingListener} but does nothing.
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public class SimpleImageLoadingListener implements ImageLoadingListener {
@Override
public void onLoadingStarted(String imageUri, Object extra) {
// Empty implementation
}

@Override
public void onLoadingFailed(String imageUri, Object extra, FailReason failReason) {
// Empty implementation
}

@Override
public void onLoadingComplete(String imageUri, Object extra, Bitmap loadedImage) {
// Empty implementation
}

@Override
public void onLoadingCancelled(String imageUri, Object extra) {
// Empty implementation
}
}

0 comments on commit 19bf3a9

Please sign in to comment.