summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/asynctask
diff options
context:
space:
mode:
authordaniel oeh <daniel.oeh@gmail.com>2014-03-29 11:09:47 +0100
committerdaniel oeh <daniel.oeh@gmail.com>2014-03-29 11:09:47 +0100
commitae849077c78770f096f40e9360250b9e32d14f40 (patch)
treecbc030c6c942d9ad42d633415a1cb0e1d79b5196 /src/de/danoeh/antennapod/asynctask
parentb5a3c72ac8cfa5b5c5f2af4321f851b5bb293d35 (diff)
parentb2de7512f4c871e882760bfe8bffa27d10c1c68a (diff)
downloadAntennaPod-ae849077c78770f096f40e9360250b9e32d14f40.zip
Merge branch 'develop'0.9.8.3
Diffstat (limited to 'src/de/danoeh/antennapod/asynctask')
-rw-r--r--src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java7
-rw-r--r--src/de/danoeh/antennapod/asynctask/DownloadObserver.java8
-rw-r--r--src/de/danoeh/antennapod/asynctask/FlattrClickWorker.java7
-rw-r--r--src/de/danoeh/antennapod/asynctask/FlattrStatusFetcher.java6
-rw-r--r--src/de/danoeh/antennapod/asynctask/FlattrTokenFetcher.java11
-rw-r--r--src/de/danoeh/antennapod/asynctask/ImageDiskCache.java8
-rw-r--r--src/de/danoeh/antennapod/asynctask/ImageLoader.java446
-rw-r--r--src/de/danoeh/antennapod/asynctask/OpmlExportWorker.java13
-rw-r--r--src/de/danoeh/antennapod/asynctask/OpmlFeedQueuer.java6
-rw-r--r--src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java17
10 files changed, 270 insertions, 259 deletions
diff --git a/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java b/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java
index ef70c1e64..43118c3af 100644
--- a/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java
+++ b/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java
@@ -1,6 +1,5 @@
package de.danoeh.antennapod.asynctask;
-import android.content.res.TypedArray;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
@@ -8,7 +7,7 @@ import android.graphics.drawable.TransitionDrawable;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
-import de.danoeh.antennapod.AppConfig;
+import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.ImageLoader.ImageWorkerTaskResource;
@@ -65,7 +64,7 @@ public class BitmapDecodeWorkerTask extends Thread {
target.setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(FADE_DURATION);
} else {
- if (AppConfig.DEBUG)
+ if (BuildConfig.DEBUG)
Log.d(TAG, "Not displaying image");
}
}
@@ -82,7 +81,7 @@ public class BitmapDecodeWorkerTask extends Thread {
target.getResources(), defaultCoverResource),
PREFERRED_LENGTH);
}
- if (AppConfig.DEBUG)
+ if (BuildConfig.DEBUG)
Log.d(TAG, "Finished loading bitmaps");
endBackgroundTask();
diff --git a/src/de/danoeh/antennapod/asynctask/DownloadObserver.java b/src/de/danoeh/antennapod/asynctask/DownloadObserver.java
index 26e405615..40388cde5 100644
--- a/src/de/danoeh/antennapod/asynctask/DownloadObserver.java
+++ b/src/de/danoeh/antennapod/asynctask/DownloadObserver.java
@@ -5,7 +5,7 @@ import android.content.*;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
-import de.danoeh.antennapod.AppConfig;
+import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.service.download.DownloadService;
import de.danoeh.antennapod.service.download.Downloader;
@@ -55,13 +55,13 @@ public class DownloadObserver {
}
public void onResume() {
- if (AppConfig.DEBUG) Log.d(TAG, "DownloadObserver resumed");
+ if (BuildConfig.DEBUG) Log.d(TAG, "DownloadObserver resumed");
activity.registerReceiver(contentChangedReceiver, new IntentFilter(DownloadService.ACTION_DOWNLOADS_CONTENT_CHANGED));
activity.bindService(new Intent(activity, DownloadService.class), mConnection, 0);
}
public void onPause() {
- if (AppConfig.DEBUG) Log.d(TAG, "DownloadObserver paused");
+ if (BuildConfig.DEBUG) Log.d(TAG, "DownloadObserver paused");
activity.unregisterReceiver(contentChangedReceiver);
activity.unbindService(mConnection);
stopRefresher();
@@ -93,7 +93,7 @@ public class DownloadObserver {
downloadService = ((DownloadService.LocalBinder) service)
.getService();
mIsBound.set(true);
- if (AppConfig.DEBUG)
+ if (BuildConfig.DEBUG)
Log.d(TAG, "Connection to service established");
List<Downloader> downloaderList = downloadService.getDownloads();
if (downloaderList != null && !downloaderList.isEmpty()) {
diff --git a/src/de/danoeh/antennapod/asynctask/FlattrClickWorker.java b/src/de/danoeh/antennapod/asynctask/FlattrClickWorker.java
index 5f483625a..e9aa79ac1 100644
--- a/src/de/danoeh/antennapod/asynctask/FlattrClickWorker.java
+++ b/src/de/danoeh/antennapod/asynctask/FlattrClickWorker.java
@@ -13,7 +13,7 @@ import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
-import de.danoeh.antennapod.AppConfig;
+import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.storage.DBReader;
@@ -23,7 +23,6 @@ import de.danoeh.antennapod.util.flattr.FlattrUtils;
import java.util.ArrayList;
import java.util.List;
-import java.util.concurrent.Executor;
/**
* Performs a click action in a background thread.
@@ -200,7 +199,7 @@ public class FlattrClickWorker extends AsyncTask<Void, String, Void> {
@Override
protected void onPostExecute(Void result) {
- if (AppConfig.DEBUG) Log.d(TAG, "Exit code was " + exitCode);
+ if (BuildConfig.DEBUG) Log.d(TAG, "Exit code was " + exitCode);
switch (exitCode) {
case NO_TOKEN:
@@ -234,7 +233,7 @@ public class FlattrClickWorker extends AsyncTask<Void, String, Void> {
@Override
protected Void doInBackground(Void... params) {
- if (AppConfig.DEBUG) Log.d(TAG, "Starting background work");
+ if (BuildConfig.DEBUG) Log.d(TAG, "Starting background work");
exitCode = EXIT_DEFAULT;
diff --git a/src/de/danoeh/antennapod/asynctask/FlattrStatusFetcher.java b/src/de/danoeh/antennapod/asynctask/FlattrStatusFetcher.java
index 4974c6b56..04d349671 100644
--- a/src/de/danoeh/antennapod/asynctask/FlattrStatusFetcher.java
+++ b/src/de/danoeh/antennapod/asynctask/FlattrStatusFetcher.java
@@ -2,7 +2,7 @@ package de.danoeh.antennapod.asynctask;
import android.content.Context;
import android.util.Log;
-import de.danoeh.antennapod.AppConfig;
+import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.storage.DBWriter;
import de.danoeh.antennapod.util.flattr.FlattrUtils;
import org.shredzone.flattr4j.exception.FlattrException;
@@ -26,7 +26,7 @@ public class FlattrStatusFetcher extends Thread {
@Override
public void run() {
- if (AppConfig.DEBUG) Log.d(TAG, "Starting background work: Retrieving Flattr status");
+ if (BuildConfig.DEBUG) Log.d(TAG, "Starting background work: Retrieving Flattr status");
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
@@ -42,6 +42,6 @@ public class FlattrStatusFetcher extends Thread {
e.printStackTrace();
}
- if (AppConfig.DEBUG) Log.d(TAG, "Finished background work: Retrieved Flattr status");
+ if (BuildConfig.DEBUG) Log.d(TAG, "Finished background work: Retrieved Flattr status");
}
}
diff --git a/src/de/danoeh/antennapod/asynctask/FlattrTokenFetcher.java b/src/de/danoeh/antennapod/asynctask/FlattrTokenFetcher.java
index b56e83d25..0dcf832f7 100644
--- a/src/de/danoeh/antennapod/asynctask/FlattrTokenFetcher.java
+++ b/src/de/danoeh/antennapod/asynctask/FlattrTokenFetcher.java
@@ -1,20 +1,19 @@
package de.danoeh.antennapod.asynctask;
-import org.shredzone.flattr4j.exception.FlattrException;
-import org.shredzone.flattr4j.oauth.AccessToken;
-import org.shredzone.flattr4j.oauth.AndroidAuthenticator;
-
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
-import de.danoeh.antennapod.AppConfig;
+import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.FlattrAuthActivity;
import de.danoeh.antennapod.util.flattr.FlattrUtils;
+import org.shredzone.flattr4j.exception.FlattrException;
+import org.shredzone.flattr4j.oauth.AccessToken;
+import org.shredzone.flattr4j.oauth.AndroidAuthenticator;
/** Fetches the access token in the background in order to avoid networkOnMainThread exception. */
@@ -76,7 +75,7 @@ public class FlattrTokenFetcher extends AsyncTask<Void, Void, AccessToken> {
return null;
}
if (token != null) {
- if (AppConfig.DEBUG) Log.d(TAG, "Successfully got token");
+ if (BuildConfig.DEBUG) Log.d(TAG, "Successfully got token");
return token;
} else {
Log.w(TAG, "Flattr token was null");
diff --git a/src/de/danoeh/antennapod/asynctask/ImageDiskCache.java b/src/de/danoeh/antennapod/asynctask/ImageDiskCache.java
index ae8bf8b87..b90d78c14 100644
--- a/src/de/danoeh/antennapod/asynctask/ImageDiskCache.java
+++ b/src/de/danoeh/antennapod/asynctask/ImageDiskCache.java
@@ -4,7 +4,7 @@ import android.os.Handler;
import android.util.Log;
import android.util.Pair;
import android.widget.ImageView;
-import de.danoeh.antennapod.AppConfig;
+import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.service.download.DownloadRequest;
@@ -89,7 +89,7 @@ public class ImageDiskCache {
private synchronized void initCacheFolder() {
if (diskCache == null) {
- if (AppConfig.DEBUG) Log.d(TAG, "Initializing cache folder");
+ if (BuildConfig.DEBUG) Log.d(TAG, "Initializing cache folder");
File cacheFile = new File(cacheFolder, CACHE_FILE_NAME);
if (cacheFile.exists()) {
try {
@@ -242,7 +242,7 @@ public class ImageDiskCache {
if (diskCache == null) {
initCacheFolder();
}
- if (AppConfig.DEBUG) Log.d(TAG, "Adding new image to disk cache: " + url);
+ if (BuildConfig.DEBUG) Log.d(TAG, "Adding new image to disk cache: " + url);
diskCache.put(url, obj);
cacheSize += obj.size;
if (cacheSize > maxCacheSize) {
@@ -313,7 +313,7 @@ public class ImageDiskCache {
dco = new DiskCacheObject(newFile.getAbsolutePath(), size);
addToDiskCache(downloadUrl, dco);
- if (AppConfig.DEBUG) Log.d(TAG, "Image was downloaded");
+ if (BuildConfig.DEBUG) Log.d(TAG, "Image was downloaded");
} else {
Log.w(TAG, "Download of url " + downloadUrl + " failed. Reason: " + result.getResult().getReasonDetailed() + "(" + result.getResult().getReason() + ")");
}
diff --git a/src/de/danoeh/antennapod/asynctask/ImageLoader.java b/src/de/danoeh/antennapod/asynctask/ImageLoader.java
index a4a9bc823..6c60b7b1f 100644
--- a/src/de/danoeh/antennapod/asynctask/ImageLoader.java
+++ b/src/de/danoeh/antennapod/asynctask/ImageLoader.java
@@ -1,230 +1,246 @@
package de.danoeh.antennapod.asynctask;
-import java.io.InputStream;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ThreadFactory;
-
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
-import android.content.res.TypedArray;
import android.os.Handler;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.widget.ImageView;
-import de.danoeh.antennapod.AppConfig;
+import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
-/** Caches and loads FeedImage bitmaps in the background */
+import java.io.InputStream;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
+
+/**
+ * Caches and loads FeedImage bitmaps in the background
+ */
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;
-
- private Handler handler;
- private ExecutorService executor;
-
- /**
- * Stores references to loaded bitmaps. Bitmaps can be accessed by the id of
- * the FeedImage the bitmap belongs to.
- */
-
- final int memClass = ((ActivityManager) PodcastApp.getInstance()
- .getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
-
- // Use 1/8th of the available memory for this memory cache.
- final int thumbnailCacheSize = 1024 * 1024 * memClass / 8;
-
- private LruCache<String, CachedBitmap> coverCache;
- private LruCache<String, CachedBitmap> thumbnailCache;
-
- private ImageLoader() {
- handler = new Handler();
- executor = createExecutor();
-
- coverCache = new LruCache<String, CachedBitmap>(1);
-
- thumbnailCache = new LruCache<String, CachedBitmap>(thumbnailCacheSize) {
-
- @SuppressLint("NewApi")
- @Override
- protected int sizeOf(String key, CachedBitmap value) {
- if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) >= 12)
- return value.getBitmap().getByteCount();
- else
- return (value.getBitmap().getRowBytes() * value.getBitmap()
- .getHeight());
-
- }
-
- };
- }
-
- private ExecutorService createExecutor() {
- return Executors.newFixedThreadPool(Runtime.getRuntime()
- .availableProcessors(), new ThreadFactory() {
-
- @Override
- public Thread newThread(Runnable r) {
- Thread t = new Thread(r);
- t.setPriority(Thread.MIN_PRIORITY);
- return t;
- }
- });
- }
-
- public static synchronized ImageLoader getInstance() {
- if (singleton == null) {
- singleton = new ImageLoader();
- }
- return singleton;
- }
-
- /**
- * Load a bitmap from the cover cache. If the bitmap is not in the cache, it
- * will be loaded from the disk. This method should either be 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 loadCoverBitmap(ImageWorkerTaskResource source, ImageView target) {
- loadCoverBitmap(source, target, target.getHeight());
- }
-
- /**
- * Load a bitmap from the cover cache. If the bitmap is not in the cache, it
- * will be loaded from the disk. This method should either be 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 loadCoverBitmap(ImageWorkerTaskResource source,
- ImageView target, int length) {
- final int defaultCoverResource = getDefaultCoverResource(target
- .getContext());
-
- if (source != null && source.getImageLoaderCacheKey() != null) {
- target.setTag(R.id.imageloader_key, source.getImageLoaderCacheKey());
- CachedBitmap cBitmap = getBitmapFromCoverCache(source.getImageLoaderCacheKey());
- if (cBitmap != null && cBitmap.getLength() >= length) {
- target.setImageBitmap(cBitmap.getBitmap());
- } else {
- target.setImageResource(defaultCoverResource);
- BitmapDecodeWorkerTask worker = new BitmapDecodeWorkerTask(
- handler, target, source, length, IMAGE_TYPE_COVER);
- executor.submit(worker);
- }
- } else {
- target.setImageResource(defaultCoverResource);
- }
- }
-
- /**
- * Load a bitmap from the thumbnail cache. If the bitmap is not in the
- * cache, it will be loaded from the disk. This method should either be
- * 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(ImageWorkerTaskResource source,
- ImageView target) {
- loadThumbnailBitmap(source, target, target.getHeight());
- }
-
- /**
- * Load a bitmap from the thumbnail cache. If the bitmap is not in the
- * cache, it will be loaded from the disk. This method should either be
- * 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(ImageWorkerTaskResource source,
- ImageView target, int length) {
- final int defaultCoverResource = getDefaultCoverResource(target
- .getContext());
-
- if (source != null && source.getImageLoaderCacheKey() != null) {
- target.setTag(R.id.imageloader_key, source.getImageLoaderCacheKey());
- CachedBitmap cBitmap = getBitmapFromThumbnailCache(source.getImageLoaderCacheKey());
- if (cBitmap != null && cBitmap.getLength() >= length) {
- target.setImageBitmap(cBitmap.getBitmap());
- } else {
- target.setImageResource(defaultCoverResource);
- BitmapDecodeWorkerTask worker = new BitmapDecodeWorkerTask(
- handler, target, source, length, IMAGE_TYPE_THUMBNAIL);
- executor.submit(worker);
- }
- } else {
- target.setImageResource(defaultCoverResource);
- }
- }
-
- public void clearExecutorQueue() {
- executor.shutdownNow();
- if (AppConfig.DEBUG)
- Log.d(TAG, "Executor was shut down.");
- executor = createExecutor();
-
- }
-
- public void wipeImageCache() {
- coverCache.evictAll();
- thumbnailCache.evictAll();
- }
-
- public boolean isInThumbnailCache(String fileUrl) {
- return thumbnailCache.get(fileUrl) != null;
- }
-
- private CachedBitmap getBitmapFromThumbnailCache(String key) {
- return thumbnailCache.get(key);
- }
-
- public void addBitmapToThumbnailCache(String key, CachedBitmap bitmap) {
- thumbnailCache.put(key, bitmap);
- }
-
- public boolean isInCoverCache(String fileUrl) {
- return coverCache.get(fileUrl) != null;
- }
-
- private CachedBitmap getBitmapFromCoverCache(String key) {
- return coverCache.get(key);
- }
-
- public void addBitmapToCoverCache(String key, CachedBitmap bitmap) {
- coverCache.put(key, bitmap);
- }
-
- private int getDefaultCoverResource(Context context) {
- return android.R.color.transparent;
- }
-
- /**
- * Used by the BitmapDecodeWorker task to retrieve the source of the bitmap.
- */
- public interface ImageWorkerTaskResource {
- /**
- * Opens a new InputStream that can be decoded as a bitmap by the
- * BitmapFactory.
- */
- public InputStream openImageInputStream();
-
- /**
- * Returns an InputStream that points to the beginning of the image
- * resource. Implementations can either create a new InputStream or
- * reset the existing one, depending on their implementation of
- * openInputStream. If a new InputStream is returned, the one given as a
- * parameter MUST be closed.
- * @param input The input stream that was returned by openImageInputStream()
- * */
- public InputStream reopenImageInputStream(InputStream input);
-
- /**
- * Returns a string that identifies the image resource. Example: file
- * path of an image
- */
- public String getImageLoaderCacheKey();
- }
+ 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;
+
+ /**
+ * Used by loadThumbnailBitmap and loadCoverBitmap to denote an ImageView that displays the default image resource.
+ * This is the case if the given source to load the image from was null or did not return any image data.
+ */
+ private static final Object DEFAULT_IMAGE_RESOURCE_TAG = new Object();
+
+ private Handler handler;
+ private ExecutorService executor;
+
+ /**
+ * Stores references to loaded bitmaps. Bitmaps can be accessed by the id of
+ * the FeedImage the bitmap belongs to.
+ */
+
+ final int memClass = ((ActivityManager) PodcastApp.getInstance()
+ .getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
+
+ // Use 1/8th of the available memory for this memory cache.
+ final int thumbnailCacheSize = 1024 * 1024 * memClass / 8;
+
+ private LruCache<String, CachedBitmap> coverCache;
+ private LruCache<String, CachedBitmap> thumbnailCache;
+
+ private ImageLoader() {
+ handler = new Handler();
+ executor = createExecutor();
+
+ coverCache = new LruCache<String, CachedBitmap>(1);
+
+ thumbnailCache = new LruCache<String, CachedBitmap>(thumbnailCacheSize) {
+
+ @SuppressLint("NewApi")
+ @Override
+ protected int sizeOf(String key, CachedBitmap value) {
+ if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) >= 12)
+ return value.getBitmap().getByteCount();
+ else
+ return (value.getBitmap().getRowBytes() * value.getBitmap()
+ .getHeight());
+
+ }
+
+ };
+ }
+
+ private ExecutorService createExecutor() {
+ return Executors.newFixedThreadPool(Runtime.getRuntime()
+ .availableProcessors(), new ThreadFactory() {
+
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(r);
+ t.setPriority(Thread.MIN_PRIORITY);
+ return t;
+ }
+ });
+ }
+
+ public static synchronized ImageLoader getInstance() {
+ if (singleton == null) {
+ singleton = new ImageLoader();
+ }
+ return singleton;
+ }
+
+ /**
+ * Load a bitmap from the cover cache. If the bitmap is not in the cache, it
+ * will be loaded from the disk. This method should either be 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 loadCoverBitmap(ImageWorkerTaskResource source, ImageView target) {
+ loadCoverBitmap(source, target, target.getHeight());
+ }
+
+ /**
+ * Load a bitmap from the cover cache. If the bitmap is not in the cache, it
+ * will be loaded from the disk. This method should either be 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 loadCoverBitmap(ImageWorkerTaskResource source,
+ ImageView target, int length) {
+ final int defaultCoverResource = getDefaultCoverResource(target
+ .getContext());
+ final String cacheKey;
+ if (source != null && (cacheKey = source.getImageLoaderCacheKey()) != null) {
+ final Object currentTag = target.getTag(R.id.imageloader_key);
+ if (currentTag == null || !cacheKey.equals(currentTag)) {
+ target.setTag(R.id.imageloader_key, cacheKey);
+ CachedBitmap cBitmap = getBitmapFromCoverCache(cacheKey);
+ if (cBitmap != null && cBitmap.getLength() >= length) {
+ target.setImageBitmap(cBitmap.getBitmap());
+ } else {
+ target.setImageResource(defaultCoverResource);
+ BitmapDecodeWorkerTask worker = new BitmapDecodeWorkerTask(
+ handler, target, source, length, IMAGE_TYPE_COVER);
+ executor.submit(worker);
+ }
+ }
+ } else {
+ target.setImageResource(defaultCoverResource);
+ target.setTag(R.id.imageloader_key, DEFAULT_IMAGE_RESOURCE_TAG);
+ }
+ }
+
+ /**
+ * Load a bitmap from the thumbnail cache. If the bitmap is not in the
+ * cache, it will be loaded from the disk. This method should either be
+ * 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(ImageWorkerTaskResource source,
+ ImageView target) {
+ loadThumbnailBitmap(source, target, target.getHeight());
+ }
+
+ /**
+ * Load a bitmap from the thumbnail cache. If the bitmap is not in the
+ * cache, it will be loaded from the disk. This method should either be
+ * 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(ImageWorkerTaskResource source,
+ ImageView target, int length) {
+ final int defaultCoverResource = getDefaultCoverResource(target
+ .getContext());
+ final String cacheKey;
+ if (source != null && (cacheKey = source.getImageLoaderCacheKey()) != null) {
+ final Object currentTag = target.getTag(R.id.imageloader_key);
+ if (currentTag == null || !cacheKey.equals(currentTag)) {
+ target.setTag(R.id.imageloader_key, cacheKey);
+ CachedBitmap cBitmap = getBitmapFromThumbnailCache(cacheKey);
+ if (cBitmap != null && cBitmap.getLength() >= length) {
+ target.setImageBitmap(cBitmap.getBitmap());
+ } else {
+ target.setImageResource(defaultCoverResource);
+ BitmapDecodeWorkerTask worker = new BitmapDecodeWorkerTask(
+ handler, target, source, length, IMAGE_TYPE_THUMBNAIL);
+ executor.submit(worker);
+ }
+ }
+ } else {
+ target.setImageResource(defaultCoverResource);
+ target.setTag(R.id.imageloader_key, DEFAULT_IMAGE_RESOURCE_TAG);
+ }
+ }
+
+ public void clearExecutorQueue() {
+ executor.shutdownNow();
+ if (BuildConfig.DEBUG)
+ Log.d(TAG, "Executor was shut down.");
+ executor = createExecutor();
+
+ }
+
+ public void wipeImageCache() {
+ coverCache.evictAll();
+ thumbnailCache.evictAll();
+ }
+
+ public boolean isInThumbnailCache(String fileUrl) {
+ return thumbnailCache.get(fileUrl) != null;
+ }
+
+ private CachedBitmap getBitmapFromThumbnailCache(String key) {
+ return thumbnailCache.get(key);
+ }
+
+ public void addBitmapToThumbnailCache(String key, CachedBitmap bitmap) {
+ thumbnailCache.put(key, bitmap);
+ }
+
+ public boolean isInCoverCache(String fileUrl) {
+ return coverCache.get(fileUrl) != null;
+ }
+
+ private CachedBitmap getBitmapFromCoverCache(String key) {
+ return coverCache.get(key);
+ }
+
+ public void addBitmapToCoverCache(String key, CachedBitmap bitmap) {
+ coverCache.put(key, bitmap);
+ }
+
+ private int getDefaultCoverResource(Context context) {
+ return android.R.color.transparent;
+ }
+
+ /**
+ * Used by the BitmapDecodeWorker task to retrieve the source of the bitmap.
+ */
+ public interface ImageWorkerTaskResource {
+ /**
+ * Opens a new InputStream that can be decoded as a bitmap by the
+ * BitmapFactory.
+ */
+ public InputStream openImageInputStream();
+
+ /**
+ * Returns an InputStream that points to the beginning of the image
+ * resource. Implementations can either create a new InputStream or
+ * reset the existing one, depending on their implementation of
+ * openInputStream. If a new InputStream is returned, the one given as a
+ * parameter MUST be closed.
+ *
+ * @param input The input stream that was returned by openImageInputStream()
+ */
+ public InputStream reopenImageInputStream(InputStream input);
+
+ /**
+ * Returns a string that identifies the image resource. Example: file
+ * path of an image
+ */
+ public String getImageLoaderCacheKey();
+ }
}
diff --git a/src/de/danoeh/antennapod/asynctask/OpmlExportWorker.java b/src/de/danoeh/antennapod/asynctask/OpmlExportWorker.java
index 745bc7079..4abb1a67d 100644
--- a/src/de/danoeh/antennapod/asynctask/OpmlExportWorker.java
+++ b/src/de/danoeh/antennapod/asynctask/OpmlExportWorker.java
@@ -1,11 +1,5 @@
package de.danoeh.antennapod.asynctask;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStreamWriter;
-import java.util.Arrays;
-
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
@@ -17,8 +11,13 @@ import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.opml.OpmlWriter;
import de.danoeh.antennapod.preferences.UserPreferences;
-import de.danoeh.antennapod.util.LangUtils;
import de.danoeh.antennapod.storage.DBReader;
+import de.danoeh.antennapod.util.LangUtils;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
/** Writes an OPML file into the export directory in the background. */
public class OpmlExportWorker extends AsyncTask<Void, Void, Void> {
diff --git a/src/de/danoeh/antennapod/asynctask/OpmlFeedQueuer.java b/src/de/danoeh/antennapod/asynctask/OpmlFeedQueuer.java
index 64e678086..038b8dcc5 100644
--- a/src/de/danoeh/antennapod/asynctask/OpmlFeedQueuer.java
+++ b/src/de/danoeh/antennapod/asynctask/OpmlFeedQueuer.java
@@ -1,8 +1,5 @@
package de.danoeh.antennapod.asynctask;
-import java.util.Arrays;
-import java.util.Date;
-
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
@@ -14,6 +11,9 @@ import de.danoeh.antennapod.opml.OpmlElement;
import de.danoeh.antennapod.storage.DownloadRequestException;
import de.danoeh.antennapod.storage.DownloadRequester;
+import java.util.Arrays;
+import java.util.Date;
+
/** Queues items for download in the background. */
public class OpmlFeedQueuer extends AsyncTask<Void, Void, Void> {
private Context context;
diff --git a/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java b/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java
index 4816c25ab..13534fa64 100644
--- a/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java
+++ b/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java
@@ -1,11 +1,5 @@
package de.danoeh.antennapod.asynctask;
-import java.io.IOException;
-import java.io.Reader;
-import java.util.ArrayList;
-
-import org.xmlpull.v1.XmlPullParserException;
-
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
@@ -14,10 +8,15 @@ import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.util.Log;
-import de.danoeh.antennapod.AppConfig;
+import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.opml.OpmlElement;
import de.danoeh.antennapod.opml.OpmlReader;
+import org.xmlpull.v1.XmlPullParserException;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.util.ArrayList;
public class OpmlImportWorker extends
AsyncTask<Void, Void, ArrayList<OpmlElement>> {
@@ -38,7 +37,7 @@ public class OpmlImportWorker extends
@Override
protected ArrayList<OpmlElement> doInBackground(Void... params) {
- if (AppConfig.DEBUG)
+ if (BuildConfig.DEBUG)
Log.d(TAG, "Starting background work");
if (mReader==null) {
@@ -73,7 +72,7 @@ public class OpmlImportWorker extends
}
progDialog.dismiss();
if (exception != null) {
- if (AppConfig.DEBUG)
+ if (BuildConfig.DEBUG)
Log.d(TAG,
"An error occured while trying to parse the opml document");
AlertDialog.Builder alert = new AlertDialog.Builder(context);