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_marginLeft="1dip"
android:layout_marginRight="4dip"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="@drawable/default_cover" />
<LinearLayout

View File

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

View File

@ -1,6 +1,5 @@
package de.danoeh.antennapod.asynctask;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
@ -8,17 +7,11 @@ import java.util.concurrent.ThreadFactory;
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.os.Handler;
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.AppConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
@ -93,7 +86,7 @@ public class FeedImageLoader {
private ExecutorService createExecutor() {
return Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors(), new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
@ -102,7 +95,7 @@ public class FeedImageLoader {
}
});
}
public static FeedImageLoader getInstance() {
if (singleton == null) {
singleton = new FeedImageLoader();
@ -110,22 +103,6 @@ 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.getFile_url() != null) {
Bitmap bitmap = getBitmapFromCoverCache(image.getFile_url());
@ -163,25 +140,12 @@ public class FeedImageLoader {
Bitmap bitmap = getBitmapFromThumbnailCache(channel
.getThumbnailUrl());
if (bitmap == null) {
boolean isInDiskCache = false;
/*
try {
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");
if (AppConfig.DEBUG)
Log.d(TAG, "Starting new thumbnail download");
target.setImageResource(R.drawable.default_cover);
executor.submit(new MiroGuideThumbnailDownloader(handler,
target, channel, LENGTH_BASE_THUMBNAIL));
}
executor.submit(new MiroGuideThumbnailDownloader(handler,
target, channel, LENGTH_BASE_THUMBNAIL));
} else {
target.setImageBitmap(bitmap);
}
@ -189,12 +153,13 @@ public class FeedImageLoader {
target.setImageResource(R.drawable.default_cover);
}
}
public void clearExecutorQueue() {
executor.shutdownNow();
if (AppConfig.DEBUG) Log.d(TAG, "Executor was shut down.");
if (AppConfig.DEBUG)
Log.d(TAG, "Executor was shut down.");
executor = createExecutor();
}
public void wipeImageCache() {
@ -226,11 +191,6 @@ public class FeedImageLoader {
coverCache.put(key, bitmap);
}
public boolean isInThumbnailDiskCache(String key) throws IOException {
DiskLruCache cache = openThubmnailDiskCache();
return cache.get(key) != null;
}
class FeedImageDecodeWorkerTask extends BitmapDecodeWorkerTask {
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;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import com.jakewharton.DiskLruCache;
import com.jakewharton.DiskLruCache.Editor;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.miroguide.model.MiroChannel;
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 */
public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
private static final String TAG = "MiroGuideThumbnailDownloader";
@ -61,12 +51,8 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
File destination = new File(PodcastApp.getInstance().getCacheDir(),
Integer.toString(fileUrl.hashCode()));
try {
/*
* DiskLruCache diskCache =
* FeedImageLoader.openThubmnailDiskCache(); Editor editor =
* diskCache.edit(fileUrl);
*/
if (AppConfig.DEBUG) Log.d(TAG, "Downloading " + fileUrl);
if (AppConfig.DEBUG)
Log.d(TAG, "Downloading " + fileUrl);
URLConnection connection = url.openConnection();
connection.connect();
byte inputBuffer[] = new byte[1024];
@ -77,8 +63,6 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
int count = 0;
while ((count = input.read(inputBuffer)) != -1) {
output.write(inputBuffer, 0, count);
if (AppConfig.DEBUG)
Log.d(TAG, "" + count);
}
output.close();
if (AppConfig.DEBUG)
@ -86,12 +70,9 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
// Get a smaller version of the bitmap and store it inside the
// LRU
// Cache
Bitmap bitmap = BitmapDecoder.decodeBitmap(PREFERRED_LENGTH,
bitmap = BitmapDecoder.decodeBitmap(PREFERRED_LENGTH,
destination.getPath());
if (bitmap != null) {
// OutputStream imageOut = editor.newOutputStream(0);
// bitmap.compress(Bitmap.CompressFormat.PNG, 80, imageOut);
// editor.commit();
storeBitmapInCache(bitmap);
}
@ -109,6 +90,6 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
@Override
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.inSampleSize = sampleSize;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = false;
options.inScaled = false;
Bitmap decodedBitmap = BitmapFactory.decodeFile(fileUrl, options);
if (decodedBitmap == null) {
@ -52,12 +50,10 @@ public class BitmapDecoder {
decodedBitmap = BitmapFactory.decodeFile(fileUrl);
}
if (decodedBitmap != null) {
if (preferredLength > srcWidth || preferredLength > srcHeight) {
return decodedBitmap;
} else {
return Bitmap.createScaledBitmap(decodedBitmap,
preferredLength, preferredLength, false);
}
} else {
return null;
}