Created AsyncTask for loading miroguide thumbnails from the disk cache
This commit is contained in:
parent
7ada4c2efb
commit
24775b9163
|
@ -90,7 +90,14 @@ public abstract class BitmapDecodeWorkerTask extends
|
|||
R.drawable.default_cover);
|
||||
}
|
||||
|
||||
protected abstract void storeBitmapInCache(Bitmap bitmap);
|
||||
protected void storeBitmapInCache(Bitmap bitmap) {
|
||||
FeedImageLoader loader = FeedImageLoader.getInstance();
|
||||
if (baseLength == LENGTH_BASE_COVER) {
|
||||
loader.addBitmapToCoverCache(fileUrl, bitmap);
|
||||
} else if (baseLength == LENGTH_BASE_THUMBNAIL) {
|
||||
loader.addBitmapToThumbnailCache(fileUrl, bitmap);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
public void executeAsync() {
|
||||
|
|
|
@ -1,15 +1,25 @@
|
|||
package de.danoeh.antennapod.asynctask;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.ActivityManager;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.support.v4.util.LruCache;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.jakewharton.DiskLruCache;
|
||||
import com.jakewharton.DiskLruCache.Snapshot;
|
||||
|
||||
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.miroguide.model.MiroChannel;
|
||||
import de.danoeh.antennapod.storage.DownloadRequester;
|
||||
|
||||
/** Caches and loads FeedImage bitmaps in the background */
|
||||
|
@ -20,6 +30,10 @@ public class FeedImageLoader {
|
|||
public static final int LENGTH_BASE_COVER = 200;
|
||||
public static final int LENGTH_BASE_THUMBNAIL = 100;
|
||||
|
||||
private static final String CACHE_DIR = "miroguide_thumbnails";
|
||||
private static final int CACHE_SIZE = 20 * 1024 * 1024;
|
||||
private static final int VALUE_SIZE = 500 * 1024;
|
||||
|
||||
/**
|
||||
* Stores references to loaded bitmaps. Bitmaps can be accessed by the id of
|
||||
* the FeedImage the bitmap belongs to.
|
||||
|
@ -32,15 +46,15 @@ public class FeedImageLoader {
|
|||
final int coverCacheSize = 1024 * 1024 * memClass / 10;
|
||||
final int thumbnailCacheSize = 1024 * 1024 * memClass / 6;
|
||||
|
||||
private LruCache<Long, Bitmap> coverCache;
|
||||
private LruCache<Long, Bitmap> thumbnailCache;
|
||||
private LruCache<String, Bitmap> coverCache;
|
||||
private LruCache<String, Bitmap> thumbnailCache;
|
||||
|
||||
private FeedImageLoader() {
|
||||
coverCache = new LruCache<Long, Bitmap>(coverCacheSize) {
|
||||
coverCache = new LruCache<String, Bitmap>(coverCacheSize) {
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
@Override
|
||||
protected int sizeOf(Long key, Bitmap value) {
|
||||
protected int sizeOf(String key, Bitmap value) {
|
||||
if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) >= 12)
|
||||
return value.getByteCount();
|
||||
else
|
||||
|
@ -50,11 +64,11 @@ public class FeedImageLoader {
|
|||
|
||||
};
|
||||
|
||||
thumbnailCache = new LruCache<Long, Bitmap>(thumbnailCacheSize) {
|
||||
thumbnailCache = new LruCache<String, Bitmap>(thumbnailCacheSize) {
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
@Override
|
||||
protected int sizeOf(Long key, Bitmap value) {
|
||||
protected int sizeOf(String key, Bitmap value) {
|
||||
if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) >= 12)
|
||||
return value.getByteCount();
|
||||
else
|
||||
|
@ -72,9 +86,25 @@ public class FeedImageLoader {
|
|||
return singleton;
|
||||
}
|
||||
|
||||
public static DiskLruCache openThubmnailDiskCache() throws IOException {
|
||||
|
||||
Context appContext = PodcastApp.getInstance();
|
||||
DiskLruCache cache = null;
|
||||
try {
|
||||
cache = DiskLruCache.open(
|
||||
appContext.getExternalFilesDir(CACHE_DIR),
|
||||
appContext.getPackageManager().getPackageInfo(
|
||||
appContext.getPackageName(), 0).versionCode,
|
||||
VALUE_SIZE, CACHE_SIZE);
|
||||
} catch (NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
public void loadCoverBitmap(FeedImage image, ImageView target) {
|
||||
if (image != null) {
|
||||
Bitmap bitmap = getBitmapFromCoverCache(image.getId());
|
||||
if (image.getFile_url() != null) {
|
||||
Bitmap bitmap = getBitmapFromCoverCache(image.getFile_url());
|
||||
if (bitmap != null) {
|
||||
target.setImageBitmap(bitmap);
|
||||
} else {
|
||||
|
@ -89,8 +119,8 @@ public class FeedImageLoader {
|
|||
}
|
||||
|
||||
public void loadThumbnailBitmap(FeedImage image, ImageView target) {
|
||||
if (image != null) {
|
||||
Bitmap bitmap = getBitmapFromThumbnailCache(image.getId());
|
||||
if (image.getFile_url() != null) {
|
||||
Bitmap bitmap = getBitmapFromThumbnailCache(image.getFile_url());
|
||||
if (bitmap != null) {
|
||||
target.setImageBitmap(bitmap);
|
||||
} else {
|
||||
|
@ -110,27 +140,27 @@ public class FeedImageLoader {
|
|||
}
|
||||
|
||||
public boolean isInThumbnailCache(FeedImage image) {
|
||||
return thumbnailCache.get(image.getId()) != null;
|
||||
return thumbnailCache.get(image.getFile_url()) != null;
|
||||
}
|
||||
|
||||
public Bitmap getBitmapFromThumbnailCache(long id) {
|
||||
return thumbnailCache.get(id);
|
||||
public Bitmap getBitmapFromThumbnailCache(String key) {
|
||||
return thumbnailCache.get(key);
|
||||
}
|
||||
|
||||
public void addBitmapToThumbnailCache(long id, Bitmap bitmap) {
|
||||
thumbnailCache.put(id, bitmap);
|
||||
public void addBitmapToThumbnailCache(String key, Bitmap bitmap) {
|
||||
thumbnailCache.put(key, bitmap);
|
||||
}
|
||||
|
||||
public boolean isInCoverCache(FeedImage image) {
|
||||
return coverCache.get(image.getId()) != null;
|
||||
return coverCache.get(image.getFile_url()) != null;
|
||||
}
|
||||
|
||||
public Bitmap getBitmapFromCoverCache(long id) {
|
||||
return coverCache.get(id);
|
||||
public Bitmap getBitmapFromCoverCache(String key) {
|
||||
return coverCache.get(key);
|
||||
}
|
||||
|
||||
public void addBitmapToCoverCache(long id, Bitmap bitmap) {
|
||||
coverCache.put(id, bitmap);
|
||||
public void addBitmapToCoverCache(String key, Bitmap bitmap) {
|
||||
coverCache.put(key, bitmap);
|
||||
}
|
||||
|
||||
class FeedImageDecodeWorkerTask extends BitmapDecodeWorkerTask {
|
||||
|
@ -150,15 +180,6 @@ public class FeedImageLoader {
|
|||
return target.getTag() == null || target.getTag() == image;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void storeBitmapInCache(Bitmap bitmap) {
|
||||
if (baseLength == LENGTH_BASE_COVER) {
|
||||
addBitmapToCoverCache(image.getId(), bitmap);
|
||||
} else if (baseLength == LENGTH_BASE_THUMBNAIL) {
|
||||
addBitmapToThumbnailCache(image.getId(), bitmap);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onInvalidFileUrl() {
|
||||
super.onInvalidFileUrl();
|
||||
|
@ -173,4 +194,46 @@ public class FeedImageLoader {
|
|||
|
||||
}
|
||||
|
||||
class MiroGuideDiskCacheLoader extends BitmapDecodeWorkerTask {
|
||||
private static final String TAG = "MiroGuideDiskCacheLoader";
|
||||
private Exception exception;
|
||||
|
||||
private MiroChannel channel;
|
||||
|
||||
public MiroGuideDiskCacheLoader(ImageView target, MiroChannel channel,
|
||||
int length) {
|
||||
super(target, channel.getThumbnailUrl(), length);
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
try {
|
||||
DiskLruCache cache = openThubmnailDiskCache();
|
||||
Snapshot snapshot = cache.get(fileUrl);
|
||||
storeBitmapInCache(BitmapFactory.decodeStream(snapshot
|
||||
.getInputStream(0)));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
exception = e;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
if (exception != null) {
|
||||
super.onPostExecute(result);
|
||||
} else {
|
||||
Log.e(TAG, "Failed to load bitmap from disk cache");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean tagsMatching(ImageView target) {
|
||||
return target.getTag() == null || target.getTag() == channel;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,10 +18,7 @@ import de.danoeh.antennapod.PodcastApp;
|
|||
import de.danoeh.antennapod.miroguide.model.MiroChannel;
|
||||
import de.danoeh.antennapod.util.BitmapDecoder;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
|
@ -29,12 +26,8 @@ import android.widget.ImageView;
|
|||
public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
|
||||
private static final String TAG = "MiroGuideThumbnailDownloader";
|
||||
|
||||
private static final String CACHE_DIR = "miroguide_thumbnails";
|
||||
private static final int CACHE_SIZE = 20 * 1024 * 1024;
|
||||
private static final int VALUE_SIZE = 500 * 1024;
|
||||
|
||||
private Exception exception;
|
||||
|
||||
|
||||
private MiroChannel miroChannel;
|
||||
|
||||
public MiroGuideThumbnailDownloader(ImageView target,
|
||||
|
@ -43,14 +36,13 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
|
|||
this.miroChannel = miroChannel;
|
||||
}
|
||||
|
||||
private static DiskLruCache openThubmnailDiskCache()
|
||||
throws NameNotFoundException, IOException {
|
||||
Context appContext = PodcastApp.getInstance();
|
||||
return DiskLruCache.open(
|
||||
appContext.getExternalFilesDir(CACHE_DIR),
|
||||
appContext.getPackageManager().getPackageInfo(
|
||||
appContext.getPackageName(), 0).versionCode,
|
||||
VALUE_SIZE, CACHE_SIZE);
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
if (exception != null) {
|
||||
super.onPostExecute(result);
|
||||
} else {
|
||||
Log.e(TAG, "Failed to download thumbnail");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -66,7 +58,7 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
|
|||
File destination = new File(PodcastApp.getInstance().getCacheDir(),
|
||||
Integer.toString(fileUrl.hashCode()));
|
||||
try {
|
||||
DiskLruCache diskCache = openThubmnailDiskCache();
|
||||
DiskLruCache diskCache = FeedImageLoader.openThubmnailDiskCache();
|
||||
Editor editor = diskCache.edit(fileUrl);
|
||||
if (editor != null) {
|
||||
HttpURLConnection connection = (HttpURLConnection) url
|
||||
|
@ -100,9 +92,6 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
|
|||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
if (destination.exists()) {
|
||||
destination.delete();
|
||||
|
@ -115,9 +104,4 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
|
|||
protected boolean tagsMatching(ImageView target) {
|
||||
return target.getTag() == null || target.getTag() == miroChannel;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void storeBitmapInCache(Bitmap bitmap) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue