Removed DiskLruCache dependency

This commit is contained in:
daniel oeh 2012-08-03 20:57:42 +02:00
parent b6b6129feb
commit ef7a20f056
5 changed files with 24 additions and 126 deletions

View File

@ -12,6 +12,8 @@
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:layout_marginLeft="1dip" android:layout_marginLeft="1dip"
android:layout_marginRight="4dip" android:layout_marginRight="4dip"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="@drawable/default_cover" /> android:src="@drawable/default_cover" />
<LinearLayout <LinearLayout

View File

@ -23,7 +23,7 @@ public abstract class BitmapDecodeWorkerTask extends Thread {
private static final String TAG = "BitmapDecodeWorkerTask"; private static final String TAG = "BitmapDecodeWorkerTask";
private ImageView target; private ImageView target;
private Bitmap bitmap; protected Bitmap bitmap;
private Bitmap decodedBitmap; private Bitmap decodedBitmap;
protected int baseLength; protected int baseLength;
@ -50,7 +50,7 @@ public abstract class BitmapDecodeWorkerTask extends Thread {
protected void onPostExecute() { protected void onPostExecute() {
// check if imageview is still supposed to display this image // check if imageview is still supposed to display this image
if (tagsMatching(target)) { if (tagsMatching(target) && bitmap != null) {
target.setImageBitmap(bitmap); target.setImageBitmap(bitmap);
} else { } else {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)

View File

@ -1,6 +1,5 @@
package de.danoeh.antennapod.asynctask; package de.danoeh.antennapod.asynctask;
import java.io.IOException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
@ -8,17 +7,11 @@ import java.util.concurrent.ThreadFactory;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler; import android.os.Handler;
import android.support.v4.util.LruCache; import android.support.v4.util.LruCache;
import android.util.Log; import android.util.Log;
import android.widget.ImageView; import android.widget.ImageView;
import com.jakewharton.DiskLruCache;
import com.jakewharton.DiskLruCache.Snapshot;
import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
@ -93,7 +86,7 @@ public class FeedImageLoader {
private ExecutorService createExecutor() { private ExecutorService createExecutor() {
return Executors.newFixedThreadPool(Runtime.getRuntime() return Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors(), new ThreadFactory() { .availableProcessors(), new ThreadFactory() {
@Override @Override
public Thread newThread(Runnable r) { public Thread newThread(Runnable r) {
Thread t = new Thread(r); Thread t = new Thread(r);
@ -102,7 +95,7 @@ public class FeedImageLoader {
} }
}); });
} }
public static FeedImageLoader getInstance() { public static FeedImageLoader getInstance() {
if (singleton == null) { if (singleton == null) {
singleton = new FeedImageLoader(); singleton = new FeedImageLoader();
@ -110,22 +103,6 @@ public class FeedImageLoader {
return singleton; 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) { public void loadCoverBitmap(FeedImage image, ImageView target) {
if (image.getFile_url() != null) { if (image.getFile_url() != null) {
Bitmap bitmap = getBitmapFromCoverCache(image.getFile_url()); Bitmap bitmap = getBitmapFromCoverCache(image.getFile_url());
@ -163,25 +140,12 @@ public class FeedImageLoader {
Bitmap bitmap = getBitmapFromThumbnailCache(channel Bitmap bitmap = getBitmapFromThumbnailCache(channel
.getThumbnailUrl()); .getThumbnailUrl());
if (bitmap == null) { if (bitmap == null) {
boolean isInDiskCache = false; if (AppConfig.DEBUG)
/* Log.d(TAG, "Starting new thumbnail download");
try { target.setImageResource(R.drawable.default_cover);
isInDiskCache = isInThumbnailDiskCache(channel
.getThumbnailUrl());
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Error when trying to read disk cache");
}
*/
if (isInDiskCache) {
executor.submit(new MiroGuideDiskCacheLoader(handler,
target, channel, LENGTH_BASE_THUMBNAIL));
} else {
if (AppConfig.DEBUG) Log.d(TAG, "Starting new thumbnail download");
executor.submit(new MiroGuideThumbnailDownloader(handler, executor.submit(new MiroGuideThumbnailDownloader(handler,
target, channel, LENGTH_BASE_THUMBNAIL)); target, channel, LENGTH_BASE_THUMBNAIL));
}
} else { } else {
target.setImageBitmap(bitmap); target.setImageBitmap(bitmap);
} }
@ -189,12 +153,13 @@ public class FeedImageLoader {
target.setImageResource(R.drawable.default_cover); target.setImageResource(R.drawable.default_cover);
} }
} }
public void clearExecutorQueue() { public void clearExecutorQueue() {
executor.shutdownNow(); executor.shutdownNow();
if (AppConfig.DEBUG) Log.d(TAG, "Executor was shut down."); if (AppConfig.DEBUG)
Log.d(TAG, "Executor was shut down.");
executor = createExecutor(); executor = createExecutor();
} }
public void wipeImageCache() { public void wipeImageCache() {
@ -226,11 +191,6 @@ public class FeedImageLoader {
coverCache.put(key, bitmap); coverCache.put(key, bitmap);
} }
public boolean isInThumbnailDiskCache(String key) throws IOException {
DiskLruCache cache = openThubmnailDiskCache();
return cache.get(key) != null;
}
class FeedImageDecodeWorkerTask extends BitmapDecodeWorkerTask { class FeedImageDecodeWorkerTask extends BitmapDecodeWorkerTask {
private static final String TAG = "FeedImageDecodeWorkerTask"; private static final String TAG = "FeedImageDecodeWorkerTask";
@ -262,45 +222,4 @@ public class FeedImageLoader {
} }
class MiroGuideDiskCacheLoader extends BitmapDecodeWorkerTask {
private static final String TAG = "MiroGuideDiskCacheLoader";
private Exception exception;
private MiroChannel channel;
public MiroGuideDiskCacheLoader(Handler handler, ImageView target,
MiroChannel channel, int length) {
super(handler, target, channel.getThumbnailUrl(), length);
this.channel = channel;
}
public void run() {
try {
DiskLruCache cache = openThubmnailDiskCache();
Snapshot snapshot = cache.get(fileUrl);
storeBitmapInCache(BitmapFactory.decodeStream(snapshot
.getInputStream(0)));
} catch (IOException e) {
e.printStackTrace();
exception = e;
}
endBackgroundTask();
}
@Override
protected void onPostExecute() {
if (exception != null) {
super.onPostExecute();
} else {
Log.e(TAG, "Failed to load bitmap from disk cache");
}
}
@Override
protected boolean tagsMatching(ImageView target) {
return target.getTag() == null || target.getTag() == channel;
}
}
} }

View File

@ -1,31 +1,21 @@
package de.danoeh.antennapod.asynctask; package de.danoeh.antennapod.asynctask;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import com.jakewharton.DiskLruCache; import android.os.Handler;
import com.jakewharton.DiskLruCache.Editor; import android.util.Log;
import android.widget.ImageView;
import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.miroguide.model.MiroChannel; import de.danoeh.antennapod.miroguide.model.MiroChannel;
import de.danoeh.antennapod.util.BitmapDecoder; import de.danoeh.antennapod.util.BitmapDecoder;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
/** Downlods thumbnails from the MiroGuide and stores them in a DiskLruCache */ /** Downlods thumbnails from the MiroGuide and stores them in a DiskLruCache */
public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask { public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
private static final String TAG = "MiroGuideThumbnailDownloader"; private static final String TAG = "MiroGuideThumbnailDownloader";
@ -61,12 +51,8 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
File destination = new File(PodcastApp.getInstance().getCacheDir(), File destination = new File(PodcastApp.getInstance().getCacheDir(),
Integer.toString(fileUrl.hashCode())); Integer.toString(fileUrl.hashCode()));
try { try {
/* if (AppConfig.DEBUG)
* DiskLruCache diskCache = Log.d(TAG, "Downloading " + fileUrl);
* FeedImageLoader.openThubmnailDiskCache(); Editor editor =
* diskCache.edit(fileUrl);
*/
if (AppConfig.DEBUG) Log.d(TAG, "Downloading " + fileUrl);
URLConnection connection = url.openConnection(); URLConnection connection = url.openConnection();
connection.connect(); connection.connect();
byte inputBuffer[] = new byte[1024]; byte inputBuffer[] = new byte[1024];
@ -77,8 +63,6 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
int count = 0; int count = 0;
while ((count = input.read(inputBuffer)) != -1) { while ((count = input.read(inputBuffer)) != -1) {
output.write(inputBuffer, 0, count); output.write(inputBuffer, 0, count);
if (AppConfig.DEBUG)
Log.d(TAG, "" + count);
} }
output.close(); output.close();
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
@ -86,12 +70,9 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
// Get a smaller version of the bitmap and store it inside the // Get a smaller version of the bitmap and store it inside the
// LRU // LRU
// Cache // Cache
Bitmap bitmap = BitmapDecoder.decodeBitmap(PREFERRED_LENGTH, bitmap = BitmapDecoder.decodeBitmap(PREFERRED_LENGTH,
destination.getPath()); destination.getPath());
if (bitmap != null) { if (bitmap != null) {
// OutputStream imageOut = editor.newOutputStream(0);
// bitmap.compress(Bitmap.CompressFormat.PNG, 80, imageOut);
// editor.commit();
storeBitmapInCache(bitmap); storeBitmapInCache(bitmap);
} }
@ -109,6 +90,6 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
@Override @Override
protected boolean tagsMatching(ImageView target) { protected boolean tagsMatching(ImageView target) {
return target.getTag() == null || target.getTag() == miroChannel; return target.getTag() == miroChannel;
} }
} }

View File

@ -41,8 +41,6 @@ public class BitmapDecoder {
options.inJustDecodeBounds = false; options.inJustDecodeBounds = false;
options.inSampleSize = sampleSize; options.inSampleSize = sampleSize;
options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = false;
options.inScaled = false;
Bitmap decodedBitmap = BitmapFactory.decodeFile(fileUrl, options); Bitmap decodedBitmap = BitmapFactory.decodeFile(fileUrl, options);
if (decodedBitmap == null) { if (decodedBitmap == null) {
@ -52,12 +50,10 @@ public class BitmapDecoder {
decodedBitmap = BitmapFactory.decodeFile(fileUrl); decodedBitmap = BitmapFactory.decodeFile(fileUrl);
} }
if (decodedBitmap != null) { if (decodedBitmap != null) {
if (preferredLength > srcWidth || preferredLength > srcHeight) {
return decodedBitmap;
} else {
return Bitmap.createScaledBitmap(decodedBitmap, return Bitmap.createScaledBitmap(decodedBitmap,
preferredLength, preferredLength, false); preferredLength, preferredLength, false);
}
} else { } else {
return null; return null;
} }