[Android] AsyncImageLoader: refactor, don't use a Caller
Thomas Guillem
git at videolan.org
Wed Oct 7 12:11:50 CEST 2015
vlc-ports/android | branch: master | Thomas Guillem <thomas at gllm.fr> | Wed Oct 7 11:40:07 2015 +0200| [1e6544adcb38c9a3ea47989addc9de91bf37daa6] | committer: Thomas Guillem
AsyncImageLoader: refactor, don't use a Caller
> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=1e6544adcb38c9a3ea47989addc9de91bf37daa6
---
.../src/org/videolan/vlc/gui/AsyncImageLoader.java | 53 ++++++++++++--------
.../src/org/videolan/vlc/gui/tv/CardPresenter.java | 10 ++--
2 files changed, 35 insertions(+), 28 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/gui/AsyncImageLoader.java b/vlc-android/src/org/videolan/vlc/gui/AsyncImageLoader.java
index 6084c99..de7aa67 100644
--- a/vlc-android/src/org/videolan/vlc/gui/AsyncImageLoader.java
+++ b/vlc-android/src/org/videolan/vlc/gui/AsyncImageLoader.java
@@ -34,17 +34,14 @@ import org.videolan.vlc.BR;
import org.videolan.vlc.VLCApplication;
import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class AsyncImageLoader {
- public interface ImageUpdater {
+ public interface Callbacks {
+ Bitmap getImage();
void updateImage(Bitmap bitmap, View target);
}
@@ -54,17 +51,28 @@ public class AsyncImageLoader {
static ThreadPoolExecutor sThreadPool = new ThreadPoolExecutor(0, 1, 2, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());
- public static void LoadImage(final Callable<Bitmap> loader, final ImageUpdater updater, final View target){
+ public static void LoadImage(final Callbacks cbs, final View target){
sThreadPool.execute(new Runnable() {
@Override
public void run() {
- handleImage(updater, loader, target);
+ final Bitmap bitmap = cbs.getImage();
+ cbs.updateImage(bitmap, target);
}
});
}
- public static void LoadAudioCover(Callable<Bitmap> loader, final ViewDataBinding binding, final Activity activity){
- ImageUpdater updater = new ImageUpdater() {
+ public static void LoadAudioCover(final Callable<Bitmap> loader, final ViewDataBinding binding, final Activity activity){
+ final Callbacks updater = new Callbacks() {
+
+ @Override
+ public Bitmap getImage() {
+ try {
+ return loader.call();
+ } catch (Exception ignored) {
+ return null;
+ }
+ }
+
@Override
public void updateImage(final Bitmap bitmap, View target) {
if (bitmap != null && activity != null) {
@@ -79,11 +87,21 @@ public class AsyncImageLoader {
}
};
- LoadImage(loader, updater, null);
+ LoadImage(updater, null);
}
- public static void LoadVideoCover(Callable<Bitmap> loader, final ViewDataBinding binding, final Activity activity){
- ImageUpdater updater = new ImageUpdater() {
+ public static void LoadVideoCover(final Callable<Bitmap> loader, final ViewDataBinding binding, final Activity activity){
+ final Callbacks updater = new Callbacks() {
+
+ @Override
+ public Bitmap getImage() {
+ try {
+ return loader.call();
+ } catch (Exception ignored) {
+ return null;
+ }
+ }
+
@Override
public void updateImage(final Bitmap bitmap, View target) {
if (bitmap != null && activity != null &&
@@ -100,15 +118,6 @@ public class AsyncImageLoader {
}
};
- LoadImage(loader, updater, null);
- }
-
- private static void handleImage(final ImageUpdater updater, Callable<Bitmap> loader, View target){
- try {
- final Bitmap bitmap = loader.call();
- updater.updateImage(bitmap, target);
- } catch (Exception e) {
- e.printStackTrace();
- }
+ LoadImage(updater, null);
}
}
diff --git a/vlc-android/tv/src/org/videolan/vlc/gui/tv/CardPresenter.java b/vlc-android/tv/src/org/videolan/vlc/gui/tv/CardPresenter.java
index 58149cf..241b69a 100644
--- a/vlc-android/tv/src/org/videolan/vlc/gui/tv/CardPresenter.java
+++ b/vlc-android/tv/src/org/videolan/vlc/gui/tv/CardPresenter.java
@@ -75,7 +75,7 @@ public class CardPresenter extends Presenter {
protected void updateCardViewImage(MediaWrapper mediaWrapper) {
mCardView.getMainImageView().setScaleType(ImageView.ScaleType.CENTER);
- AsyncImageLoader.LoadImage(new CoverFetcher(mediaWrapper), sImageUpdater, mCardView);
+ AsyncImageLoader.LoadImage(new CoverFetcher(mediaWrapper), mCardView);
}
protected void updateCardViewImage(Drawable image) {
@@ -168,7 +168,7 @@ public class CardPresenter extends Presenter {
}
}
- public static class CoverFetcher implements Callable<Bitmap>{
+ public static class CoverFetcher implements AsyncImageLoader.Callbacks{
MediaWrapper mediaWrapper;
CoverFetcher(MediaWrapper mediaWrapper){
@@ -176,7 +176,7 @@ public class CardPresenter extends Presenter {
}
@Override
- public Bitmap call() throws Exception {
+ public Bitmap getImage() {
Bitmap picture = null;
if (mediaWrapper.getType() == mediaWrapper.TYPE_AUDIO) {
picture = AudioUtil.getCover(sContext, mediaWrapper, 320);
@@ -192,9 +192,7 @@ public class CardPresenter extends Presenter {
picture = BitmapFactory.decodeResource(mRes, R.drawable.ic_browser_unknown_big_normal);
return picture;
}
- }
- public static AsyncImageLoader.ImageUpdater sImageUpdater = new AsyncImageLoader.ImageUpdater() {
@Override
public void updateImage(final Bitmap picture, final View target) {
target.post(new Runnable() {
@@ -208,5 +206,5 @@ public class CardPresenter extends Presenter {
}
});
}
- };
+ }
}
More information about the Android
mailing list