FeedImageLoader now works independently from FeedImages

This commit is contained in:
daniel oeh 2013-02-25 18:15:02 +01:00
parent 30ba44b17a
commit 56db791e7d
11 changed files with 93 additions and 97 deletions

View File

@ -5,7 +5,7 @@ import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.preference.PreferenceManager;
import android.util.Log;
import de.danoeh.antennapod.asynctask.FeedImageLoader;
import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.feed.EventDistributor;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.feed.FeedMedia;
@ -50,7 +50,7 @@ public class PodcastApp extends Application implements
public void onLowMemory() {
super.onLowMemory();
Log.w(TAG, "Received onLowOnMemory warning. Cleaning image cache...");
FeedImageLoader.getInstance().wipeImageCache();
ImageLoader.getInstance().wipeImageCache();
}
/**

View File

@ -20,7 +20,7 @@ import com.actionbarsherlock.view.Window;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.ChapterListAdapter;
import de.danoeh.antennapod.asynctask.FeedImageLoader;
import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.feed.Chapter;
import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.feed.SimpleChapter;
@ -181,7 +181,7 @@ public class AudioplayerActivity extends MediaplayerActivity {
@Override
public void run() {
FeedImageLoader.getInstance().loadThumbnailBitmap(
ImageLoader.getInstance().loadThumbnailBitmap(
media.getItem().getFeed().getImage(),
butNavLeft);
}
@ -194,7 +194,7 @@ public class AudioplayerActivity extends MediaplayerActivity {
@Override
public void run() {
FeedImageLoader.getInstance().loadThumbnailBitmap(
ImageLoader.getInstance().loadThumbnailBitmap(
media.getItem().getFeed().getImage(),
butNavLeft);
}

View File

@ -12,7 +12,7 @@ import com.actionbarsherlock.view.MenuItem;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.FeedImageLoader;
import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator;
import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.feed.FeedManager;
@ -58,7 +58,7 @@ public class FeedInfoActivity extends SherlockActivity {
@Override
public void run() {
FeedImageLoader.getInstance().loadThumbnailBitmap(
ImageLoader.getInstance().loadThumbnailBitmap(
feed.getImage(), imgvCover);
}
});

View File

@ -16,7 +16,7 @@ import com.actionbarsherlock.view.MenuItem;
import com.mobeta.android.dslv.DragSortListView;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.FeedImageLoader;
import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.feed.EventDistributor;
import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.feed.FeedManager;
@ -146,7 +146,7 @@ public class OrganizeQueueActivity extends SherlockListActivity {
holder.feedTitle.setText(item.getFeed().getTitle());
holder.feedImage.setTag(item.getFeed().getImage());
FeedImageLoader.getInstance().loadThumbnailBitmap(
ImageLoader.getInstance().loadThumbnailBitmap(
item.getFeed().getImage(),
holder.feedImage,
(int) convertView.getResources().getDimension(

View File

@ -11,7 +11,7 @@ import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.FeedImageLoader;
import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.feed.FeedMedia;
@ -162,7 +162,7 @@ public class ExternalEpisodesListAdapter extends BaseExpandableListAdapter {
}
holder.feedImage.setTag(item.getFeed().getImage());
FeedImageLoader.getInstance().loadThumbnailBitmap(
ImageLoader.getInstance().loadThumbnailBitmap(
item.getFeed().getImage(),
holder.feedImage,
(int) convertView.getResources().getDimension(

View File

@ -11,7 +11,7 @@ import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.FeedImageLoader;
import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.storage.DownloadRequester;
@ -24,14 +24,14 @@ public class FeedlistAdapter extends BaseAdapter {
private FeedManager manager = FeedManager.getInstance();
private int selectedItemIndex;
private FeedImageLoader imageLoader;
private ImageLoader imageLoader;
public static final int SELECTION_NONE = -1;
public FeedlistAdapter(Context context) {
super();
this.context = context;
selectedItemIndex = SELECTION_NONE;
imageLoader = FeedImageLoader.getInstance();
imageLoader = ImageLoader.getInstance();
}
@Override
@ -109,7 +109,8 @@ public class FeedlistAdapter extends BaseAdapter {
}
holder.image.setTag(feed.getImage());
final String imageUrl = (feed.getImage() != null) ?
feed.getImage().getFile_url() : null;
imageLoader.loadThumbnailBitmap(
feed.getImage(),
holder.image,

View File

@ -10,7 +10,7 @@ import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.FeedImageLoader;
import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.feed.FeedComponent;
import de.danoeh.antennapod.feed.FeedItem;
@ -51,7 +51,7 @@ public class SearchlistAdapter extends ArrayAdapter<SearchResult> {
final Feed feed = (Feed) component;
holder.title.setText(feed.getTitle());
holder.subtitle.setVisibility(View.GONE);
FeedImageLoader.getInstance().loadThumbnailBitmap(feed.getImage(),
ImageLoader.getInstance().loadThumbnailBitmap(feed.getImage(),
holder.cover, (int) convertView.getResources().getDimension(R.dimen.thumbnail_length));
} else if (component.getClass() == FeedItem.class) {
final FeedItem item = (FeedItem) component;
@ -61,7 +61,7 @@ public class SearchlistAdapter extends ArrayAdapter<SearchResult> {
holder.subtitle.setText(result.getSubtitle());
}
FeedImageLoader.getInstance().loadThumbnailBitmap(
ImageLoader.getInstance().loadThumbnailBitmap(
item.getFeed().getImage(),
holder.cover,
(int) convertView.getResources().getDimension(

View File

@ -11,10 +11,10 @@ import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.util.BitmapDecoder;
public abstract class BitmapDecodeWorkerTask extends Thread {
public class BitmapDecodeWorkerTask extends Thread {
protected int PREFERRED_LENGTH;
/** Can be thumbnail or cover */
protected int imageType;
@ -25,7 +25,7 @@ public abstract class BitmapDecodeWorkerTask extends Thread {
protected String fileUrl;
private Handler handler;
private final int defaultCoverResource;
public BitmapDecodeWorkerTask(Handler handler, ImageView target,
@ -36,7 +36,8 @@ public abstract class BitmapDecodeWorkerTask extends Thread {
this.fileUrl = fileUrl;
this.PREFERRED_LENGTH = length;
this.imageType = imageType;
TypedArray res = target.getContext().obtainStyledAttributes(new int[] {R.attr.default_cover});
TypedArray res = target.getContext().obtainStyledAttributes(
new int[] { R.attr.default_cover });
this.defaultCoverResource = res.getResourceId(0, 0);
res.recycle();
}
@ -45,7 +46,9 @@ public abstract class BitmapDecodeWorkerTask extends Thread {
* Should return true if tag of the imageview is still the same it was
* before the bitmap was decoded
*/
abstract protected boolean tagsMatching(ImageView target);
protected boolean tagsMatching(ImageView target) {
return target.getTag() == null || target.getTag() == fileUrl;
}
protected void onPostExecute() {
// check if imageview is still supposed to display this image
@ -64,13 +67,15 @@ public abstract class BitmapDecodeWorkerTask extends Thread {
f = new File(fileUrl);
}
if (fileUrl != null && f.exists()) {
cBitmap = new CachedBitmap(BitmapDecoder.decodeBitmap(PREFERRED_LENGTH, fileUrl), PREFERRED_LENGTH);
cBitmap = new CachedBitmap(BitmapDecoder.decodeBitmap(
PREFERRED_LENGTH, fileUrl), PREFERRED_LENGTH);
if (cBitmap.getBitmap() != null) {
storeBitmapInCache(cBitmap);
} else {
Log.w(TAG, "Could not load bitmap. Using default image.");
cBitmap = new CachedBitmap(BitmapFactory.decodeResource(target.getResources(),
defaultCoverResource), PREFERRED_LENGTH);
cBitmap = new CachedBitmap(BitmapFactory.decodeResource(
target.getResources(), defaultCoverResource),
PREFERRED_LENGTH);
}
if (AppConfig.DEBUG)
Log.d(TAG, "Finished loading bitmaps");
@ -98,15 +103,15 @@ public abstract class BitmapDecodeWorkerTask extends Thread {
protected void onInvalidFileUrl() {
Log.e(TAG, "FeedImage has no valid file url. Using default image");
cBitmap = new CachedBitmap(BitmapFactory.decodeResource(target.getResources(),
defaultCoverResource), PREFERRED_LENGTH);
cBitmap = new CachedBitmap(BitmapFactory.decodeResource(
target.getResources(), defaultCoverResource), PREFERRED_LENGTH);
}
protected void storeBitmapInCache(CachedBitmap cb) {
FeedImageLoader loader = FeedImageLoader.getInstance();
if (imageType == FeedImageLoader.IMAGE_TYPE_COVER) {
ImageLoader loader = ImageLoader.getInstance();
if (imageType == ImageLoader.IMAGE_TYPE_COVER) {
loader.addBitmapToCoverCache(fileUrl, cb);
} else if (imageType == FeedImageLoader.IMAGE_TYPE_THUMBNAIL) {
} else if (imageType == ImageLoader.IMAGE_TYPE_THUMBNAIL) {
loader.addBitmapToThumbnailCache(fileUrl, cb);
}
}

View File

@ -16,13 +16,11 @@ import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.feed.FeedImage;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.storage.DownloadRequester;
/** Caches and loads FeedImage bitmaps in the background */
public class FeedImageLoader {
private static final String TAG = "FeedImageLoader";
private static FeedImageLoader singleton;
public class ImageLoader {
private static final String TAG = "ImageLoader";
private static ImageLoader singleton;
public static final int IMAGE_TYPE_THUMBNAIL = 0;
public static final int IMAGE_TYPE_COVER = 1;
@ -44,7 +42,7 @@ public class FeedImageLoader {
private LruCache<String, CachedBitmap> coverCache;
private LruCache<String, CachedBitmap> thumbnailCache;
private FeedImageLoader() {
private ImageLoader() {
handler = new Handler();
executor = createExecutor();
@ -79,9 +77,9 @@ public class FeedImageLoader {
});
}
public static FeedImageLoader getInstance() {
public static ImageLoader getInstance() {
if (singleton == null) {
singleton = new FeedImageLoader();
singleton = new ImageLoader();
}
return singleton;
}
@ -92,8 +90,18 @@ public class FeedImageLoader {
* ImageView's size has already been set or inside a Runnable which is
* posted to the ImageView's message queue.
*/
public void loadCoverBitmap(String fileUrl, ImageView target) {
loadCoverBitmap(fileUrl, target, target.getHeight());
}
public void loadCoverBitmap(FeedImage image, ImageView target) {
loadCoverBitmap(image, target, target.getHeight());
loadCoverBitmap((image != null) ? image.getFile_url() : null, target,
target.getHeight());
}
public void loadCoverBitmap(FeedImage image, ImageView target, int length) {
loadCoverBitmap((image != null) ? image.getFile_url() : null, target,
length);
}
/**
@ -102,17 +110,18 @@ public class FeedImageLoader {
* ImageView's size has already been set or inside a Runnable which is
* posted to the ImageView's message queue.
*/
public void loadCoverBitmap(FeedImage image, ImageView target, int length) {
final int defaultCoverResource = getDefaultCoverResource(target.getContext());
if (image != null && image.getFile_url() != null) {
CachedBitmap cBitmap = getBitmapFromCoverCache(image.getFile_url());
public void loadCoverBitmap(String fileUrl, ImageView target, int length) {
final int defaultCoverResource = getDefaultCoverResource(target
.getContext());
if (fileUrl != null) {
CachedBitmap cBitmap = getBitmapFromCoverCache(fileUrl);
if (cBitmap != null && cBitmap.getLength() >= length) {
target.setImageBitmap(cBitmap.getBitmap());
} else {
target.setImageResource(defaultCoverResource);
FeedImageDecodeWorkerTask worker = new FeedImageDecodeWorkerTask(
handler, target, image, length, IMAGE_TYPE_COVER);
BitmapDecodeWorkerTask worker = new BitmapDecodeWorkerTask(
handler, target, fileUrl, length, IMAGE_TYPE_COVER);
executor.submit(worker);
}
} else {
@ -126,8 +135,19 @@ public class FeedImageLoader {
* called if the ImageView's size has already been set or inside a Runnable
* which is posted to the ImageView's message queue.
*/
public void loadThumbnailBitmap(String fileUrl, ImageView target) {
loadThumbnailBitmap(fileUrl, target, target.getHeight());
}
public void loadThumbnailBitmap(FeedImage image, ImageView target) {
loadThumbnailBitmap(image, target, target.getHeight());
loadThumbnailBitmap((image != null) ? image.getFile_url() : null,
target, target.getHeight());
}
public void loadThumbnailBitmap(FeedImage image, ImageView target,
int length) {
loadThumbnailBitmap((image != null) ? image.getFile_url() : null,
target, length);
}
/**
@ -136,18 +156,18 @@ public class FeedImageLoader {
* called if the ImageView's size has already been set or inside a Runnable
* which is posted to the ImageView's message queue.
*/
public void loadThumbnailBitmap(FeedImage image, ImageView target,
int length) {
final int defaultCoverResource = getDefaultCoverResource(target.getContext());
if (image != null && image.getFile_url() != null) {
CachedBitmap cBitmap = getBitmapFromThumbnailCache(image.getFile_url());
public void loadThumbnailBitmap(String fileUrl, ImageView target, int length) {
final int defaultCoverResource = getDefaultCoverResource(target
.getContext());
if (fileUrl != null) {
CachedBitmap cBitmap = getBitmapFromThumbnailCache(fileUrl);
if (cBitmap != null && cBitmap.getLength() >= length) {
target.setImageBitmap(cBitmap.getBitmap());
} else {
target.setImageResource(defaultCoverResource);
FeedImageDecodeWorkerTask worker = new FeedImageDecodeWorkerTask(
handler, target, image, length, IMAGE_TYPE_THUMBNAIL);
BitmapDecodeWorkerTask worker = new BitmapDecodeWorkerTask(
handler, target, fileUrl, length, IMAGE_TYPE_THUMBNAIL);
executor.submit(worker);
}
} else {
@ -168,8 +188,8 @@ public class FeedImageLoader {
thumbnailCache.evictAll();
}
public boolean isInThumbnailCache(FeedImage image) {
return thumbnailCache.get(image.getFile_url()) != null;
public boolean isInThumbnailCache(String fileUrl) {
return thumbnailCache.get(fileUrl) != null;
}
private CachedBitmap getBitmapFromThumbnailCache(String key) {
@ -180,8 +200,8 @@ public class FeedImageLoader {
thumbnailCache.put(key, bitmap);
}
public boolean isInCoverCache(FeedImage image) {
return coverCache.get(image.getFile_url()) != null;
public boolean isInCoverCache(String fileUrl) {
return coverCache.get(fileUrl) != null;
}
private CachedBitmap getBitmapFromCoverCache(String key) {
@ -191,43 +211,13 @@ public class FeedImageLoader {
public void addBitmapToCoverCache(String key, CachedBitmap bitmap) {
coverCache.put(key, bitmap);
}
private int getDefaultCoverResource(Context context) {
TypedArray res = context.obtainStyledAttributes(new int[] {R.attr.default_cover});
TypedArray res = context
.obtainStyledAttributes(new int[] { R.attr.default_cover });
final int defaultCoverResource = res.getResourceId(0, 0);
res.recycle();
return defaultCoverResource;
}
class FeedImageDecodeWorkerTask extends BitmapDecodeWorkerTask {
private static final String TAG = "FeedImageDecodeWorkerTask";
protected FeedImage image;
public FeedImageDecodeWorkerTask(Handler handler, ImageView target,
FeedImage image, int length, int imageType) {
super(handler, target, image.getFile_url(), length, imageType);
this.image = image;
}
@Override
protected boolean tagsMatching(ImageView target) {
return target.getTag() == null || target.getTag() == image;
}
@Override
protected void onInvalidFileUrl() {
super.onInvalidFileUrl();
if (image.getFile_url() != null
&& !DownloadRequester.getInstance()
.isDownloadingFile(image)) {
FeedManager.getInstance().notifyInvalidImageFile(
PodcastApp.getInstance(), image);
}
}
}
}

View File

@ -12,7 +12,7 @@ import com.actionbarsherlock.app.SherlockFragment;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.AudioplayerActivity.AudioplayerContentFragment;
import de.danoeh.antennapod.asynctask.FeedImageLoader;
import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.feed.FeedManager;
@ -79,7 +79,7 @@ public class CoverFragment extends SherlockFragment implements
@Override
public void run() {
FeedImageLoader.getInstance().loadCoverBitmap(
ImageLoader.getInstance().loadCoverBitmap(
media.getItem().getFeed().getImage(), imgvCover);
}
});

View File

@ -14,7 +14,7 @@ import com.actionbarsherlock.app.SherlockFragment;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.FeedImageLoader;
import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.service.PlaybackService;
import de.danoeh.antennapod.util.Converter;
@ -196,7 +196,7 @@ public class ExternalPlayerFragment extends SherlockFragment {
FeedMedia media = controller.getMedia();
if (media != null) {
txtvTitle.setText(media.getItem().getTitle());
FeedImageLoader.getInstance().loadThumbnailBitmap(
ImageLoader.getInstance().loadThumbnailBitmap(
media.getItem().getFeed().getImage(),
imgvCover,
(int) getActivity().getResources().getDimension(