Commit a965e88e authored by Geoffrey Métais's avatar Geoffrey Métais

Async image loading for TV

parent 5cd9248a
......@@ -27,6 +27,7 @@ import android.app.Activity;
import android.databinding.ViewDataBinding;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import org.videolan.vlc.BR;
import org.videolan.vlc.VLCApplication;
......@@ -38,37 +39,52 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class AsyncImageLoader {
public final static String TAG = "VLC/AsyncImageLoader";
static ExecutorService executor = Executors.newSingleThreadExecutor();
public interface ImageUpdater {
void updateImage(Bitmap bitmap, View target);
}
public static void LoadImage(Callable<Bitmap> loader, final ViewDataBinding binding, Activity activity){
public final static String TAG = "VLC/AsyncImageLoader";
Future<Bitmap> future = executor.submit(loader);
handleImage(binding, activity, future);
}
static ExecutorService executor = Executors.newSingleThreadExecutor();
private static void handleImage(final ViewDataBinding binding, final Activity activity, final Future<Bitmap> future) {
public static void LoadImage(Callable<Bitmap> loader, final ImageUpdater updater, final View target){
final Future<Bitmap> future = executor.submit(loader);
new Thread(new Runnable() {
@Override
public void run() {
try {
final Bitmap bitmap = future.get();
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
binding.setVariable(BR.cover, new BitmapDrawable(VLCApplication.getAppResources(), bitmap));
binding.executePendingBindings();
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
handleImage(updater, future, target);
}
}).start();
}
public static void LoadAudioCover(Callable<Bitmap> loader, final ViewDataBinding binding, final Activity activity){
ImageUpdater updater = new ImageUpdater() {
@Override
public void updateImage(final Bitmap bitmap, View target) {
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
binding.setVariable(BR.cover, new BitmapDrawable(VLCApplication.getAppResources(), bitmap));
binding.executePendingBindings();
}
});
}
}
};
LoadImage(loader, updater, null);
}
private static void handleImage(final ImageUpdater updater, final Future<Bitmap> future, View target){
try {
final Bitmap bitmap = future.get();
updater.updateImage(bitmap, target);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
......@@ -329,7 +329,7 @@ public class AudioBrowserListAdapter extends BaseAdapter implements SectionIndex
if (mItemType == ITEM_WITH_COVER) {
AudioUtil.AudioCoverFetcher fetcher = new AudioUtil.AudioCoverFetcher(mContext, mItems.get(position).mMediaList);
AsyncImageLoader.LoadImage(fetcher, holder.binding, mContext);
AsyncImageLoader.LoadAudioCover(fetcher, holder.binding, mContext);
}
holder.binding.setVariable(BR.footer, !isMediaItemAboveASeparator(position));
......
......@@ -23,8 +23,8 @@ package org.videolan.vlc.gui.tv;
import org.videolan.vlc.MediaDatabase;
import org.videolan.vlc.MediaWrapper;
import org.videolan.vlc.R;
import org.videolan.vlc.gui.AsyncImageLoader;
import org.videolan.vlc.gui.audio.AudioUtil;
import org.videolan.vlc.gui.tv.browser.GridFragment;
import org.videolan.vlc.gui.tv.browser.MusicFragment;
import org.videolan.vlc.util.BitmapUtil;
......@@ -40,6 +40,8 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.concurrent.Callable;
public class CardPresenter extends Presenter {
private static final String TAG = "CardPresenter";
......@@ -72,24 +74,8 @@ public class CardPresenter extends Presenter {
}
protected void updateCardViewImage(MediaWrapper mediaWrapper) {
mCardView.getMainImageView().setScaleType(ImageView.ScaleType.CENTER);
Bitmap picture = null;
if (mediaWrapper.getType() == mediaWrapper.TYPE_AUDIO) {
picture = AudioUtil.getCover(sContext, mediaWrapper, 320);
if (picture == null)
picture = BitmapFactory.decodeResource(mRes, R.drawable.ic_browser_audio_big_normal);
} else if (mediaWrapper.getType() == mediaWrapper.TYPE_VIDEO) {
picture = BitmapUtil.getPictureFromCache(mediaWrapper);
if (picture == null)
picture = BitmapFactory.decodeResource(mRes, R.drawable.ic_browser_video_big_normal);
} else if (mediaWrapper.getType() == mediaWrapper.TYPE_DIR)
picture = BitmapFactory.decodeResource(mRes, R.drawable.ic_menu_network_big);
else
picture = BitmapFactory.decodeResource(mRes, R.drawable.ic_browser_unknown_big_normal);
if (picture != null && picture.getByteCount() > 4)
mCardView.setMainImage(new BitmapDrawable(mRes, picture));
else
mCardView.setMainImage(sDefaultCardImage);
mCardView.getMainImageView().setScaleType(ImageView.ScaleType.CENTER);
AsyncImageLoader.LoadImage(new CoverFetcher(mediaWrapper), sImageUpdater, mCardView);
}
protected void updateCardViewImage(Drawable image) {
......@@ -181,4 +167,46 @@ public class CardPresenter extends Presenter {
this.name = name;
}
}
public static class CoverFetcher implements Callable<Bitmap>{
MediaWrapper mediaWrapper;
CoverFetcher(MediaWrapper mediaWrapper){
this.mediaWrapper = mediaWrapper;
}
@Override
public Bitmap call() throws Exception {
Bitmap picture = null;
if (mediaWrapper.getType() == mediaWrapper.TYPE_AUDIO) {
picture = AudioUtil.getCover(sContext, mediaWrapper, 320);
if (picture == null)
picture = BitmapFactory.decodeResource(mRes, R.drawable.ic_browser_audio_big_normal);
} else if (mediaWrapper.getType() == mediaWrapper.TYPE_VIDEO) {
picture = BitmapUtil.getPictureFromCache(mediaWrapper);
if (picture == null)
picture = BitmapFactory.decodeResource(mRes, R.drawable.ic_browser_video_big_normal);
} else if (mediaWrapper.getType() == mediaWrapper.TYPE_DIR)
picture = BitmapFactory.decodeResource(mRes, R.drawable.ic_menu_network_big);
else
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() {
@Override
public void run() {
ImageCardView cardView = (ImageCardView) target;
if (picture != null && picture.getByteCount() > 4)
cardView.setMainImage(new BitmapDrawable(mRes, picture));
else
cardView.setMainImage(sDefaultCardImage);
}
});
}
};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment