diff --git a/src/de/danoeh/antennapod/PodcastApp.java b/src/de/danoeh/antennapod/PodcastApp.java index 5b2f1c89d..d5ec9fede 100644 --- a/src/de/danoeh/antennapod/PodcastApp.java +++ b/src/de/danoeh/antennapod/PodcastApp.java @@ -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(); } /** diff --git a/src/de/danoeh/antennapod/activity/AudioplayerActivity.java b/src/de/danoeh/antennapod/activity/AudioplayerActivity.java index 48b09b92b..6f98777af 100644 --- a/src/de/danoeh/antennapod/activity/AudioplayerActivity.java +++ b/src/de/danoeh/antennapod/activity/AudioplayerActivity.java @@ -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); } diff --git a/src/de/danoeh/antennapod/activity/FeedInfoActivity.java b/src/de/danoeh/antennapod/activity/FeedInfoActivity.java index da6ba7e1c..c57a5794b 100644 --- a/src/de/danoeh/antennapod/activity/FeedInfoActivity.java +++ b/src/de/danoeh/antennapod/activity/FeedInfoActivity.java @@ -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); } }); diff --git a/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java b/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java index 26abdc23e..3eea78598 100644 --- a/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java +++ b/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java @@ -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( diff --git a/src/de/danoeh/antennapod/adapter/ExternalEpisodesListAdapter.java b/src/de/danoeh/antennapod/adapter/ExternalEpisodesListAdapter.java index 6d295d3e2..caf9e71b2 100644 --- a/src/de/danoeh/antennapod/adapter/ExternalEpisodesListAdapter.java +++ b/src/de/danoeh/antennapod/adapter/ExternalEpisodesListAdapter.java @@ -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( diff --git a/src/de/danoeh/antennapod/adapter/FeedlistAdapter.java b/src/de/danoeh/antennapod/adapter/FeedlistAdapter.java index 6316742a3..e3f07a277 100644 --- a/src/de/danoeh/antennapod/adapter/FeedlistAdapter.java +++ b/src/de/danoeh/antennapod/adapter/FeedlistAdapter.java @@ -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, diff --git a/src/de/danoeh/antennapod/adapter/SearchlistAdapter.java b/src/de/danoeh/antennapod/adapter/SearchlistAdapter.java index c7f42e7cb..129289d30 100644 --- a/src/de/danoeh/antennapod/adapter/SearchlistAdapter.java +++ b/src/de/danoeh/antennapod/adapter/SearchlistAdapter.java @@ -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 { 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 { holder.subtitle.setText(result.getSubtitle()); } - FeedImageLoader.getInstance().loadThumbnailBitmap( + ImageLoader.getInstance().loadThumbnailBitmap( item.getFeed().getImage(), holder.cover, (int) convertView.getResources().getDimension( diff --git a/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java b/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java index c23c4c66a..810a17165 100644 --- a/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java +++ b/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java @@ -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); } } diff --git a/src/de/danoeh/antennapod/asynctask/FeedImageLoader.java b/src/de/danoeh/antennapod/asynctask/ImageLoader.java similarity index 70% rename from src/de/danoeh/antennapod/asynctask/FeedImageLoader.java rename to src/de/danoeh/antennapod/asynctask/ImageLoader.java index 5e1994adb..d0f20d621 100644 --- a/src/de/danoeh/antennapod/asynctask/FeedImageLoader.java +++ b/src/de/danoeh/antennapod/asynctask/ImageLoader.java @@ -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 coverCache; private LruCache 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); - } - - } - - } - } diff --git a/src/de/danoeh/antennapod/fragment/CoverFragment.java b/src/de/danoeh/antennapod/fragment/CoverFragment.java index f1fde24d8..0ef19f818 100644 --- a/src/de/danoeh/antennapod/fragment/CoverFragment.java +++ b/src/de/danoeh/antennapod/fragment/CoverFragment.java @@ -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); } }); diff --git a/src/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java b/src/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java index d79b7de10..0eefd3103 100644 --- a/src/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java +++ b/src/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java @@ -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(