Images in podcast directory should now be displayed correctly

This commit is contained in:
daniel oeh 2013-08-31 15:42:48 +02:00
parent a60ff4dd36
commit 7df102daa3
5 changed files with 55 additions and 27 deletions

View File

@ -28,8 +28,7 @@
<color name="status_playing">#E0EE5F52</color>
<color name="overlay_dark">#262C31</color>
<color name="overlay_light">#DDDDDD</color>
<color name="default_image_color">#858585</color>
<!-- Use Gingerbread-orange -->
<color name="selection_background_color_dark">#FEBB20</color>
<color name="selection_background_color_light">#FEBB20</color>

View File

@ -16,5 +16,6 @@
<item name="drag_handle" type="id"/>
<item name="skip_episode_item" type="id"/>
<item name="image_disk_cache_key" type="id"/>
<item name="imageloader_key" type="id"/>
</resources>

View File

@ -46,8 +46,8 @@ public class BitmapDecodeWorkerTask extends Thread {
* before the bitmap was decoded
*/
protected boolean tagsMatching(ImageView target) {
return target.getTag() == null
|| target.getTag().equals(imageResource.getImageLoaderCacheKey());
return target.getTag(R.id.imageloader_key) == null
|| target.getTag(R.id.imageloader_key).equals(imageResource.getImageLoaderCacheKey());
}
protected void onPostExecute() {

View File

@ -1,16 +1,17 @@
package de.danoeh.antennapod.asynctask;
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.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.service.download.DownloadRequest;
import de.danoeh.antennapod.service.download.HttpDownloader;
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@ -71,6 +72,7 @@ public class ImageDiskCache {
private final long maxCacheSize;
private int cacheSize;
private final File cacheFolder;
private Handler handler;
private ImageDiskCache(String path, long maxCacheSize) {
this.maxCacheSize = maxCacheSize;
@ -80,7 +82,8 @@ public class ImageDiskCache {
}
executor = Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors() * 2);
.availableProcessors());
handler = new Handler();
}
private synchronized void initCacheFolder() {
@ -194,7 +197,7 @@ public class ImageDiskCache {
}
}
target.setTag(R.id.image_disk_cache_key, url);
target.setImageResource(R.color.default_image_color);
target.setImageResource(android.R.color.transparent);
executor.submit(new ImageDownloader(url) {
@Override
protected void onImageLoaded(DiskCacheObject diskCacheObject) {
@ -221,7 +224,7 @@ public class ImageDiskCache {
}
}
target.setTag(R.id.image_disk_cache_key, url);
target.setImageResource(R.color.default_image_color);
target.setImageResource(android.R.color.transparent);
executor.submit(new ImageDownloader(url) {
@Override
protected void onImageLoaded(DiskCacheObject diskCacheObject) {
@ -269,6 +272,8 @@ public class ImageDiskCache {
return dco;
}
ConcurrentHashMap<String, File> runningDownloads = new ConcurrentHashMap<String, File>();
private abstract class ImageDownloader implements Runnable {
private String downloadUrl;
@ -285,32 +290,54 @@ public class ImageDiskCache {
return;
}
InputStream input = null;
OutputStream output = null;
try {
URL url = new URL(downloadUrl);
input = url.openStream();
DiskCacheObject dco = null;
File newFile = new File(cacheFolder, Integer.toString(downloadUrl.hashCode()));
synchronized (ImageDiskCache.this) {
if (runningDownloads.containsKey(newFile.getAbsolutePath())) {
Log.d(TAG, "Download is already running: " + newFile.getAbsolutePath());
return;
} else {
runningDownloads.put(newFile.getAbsolutePath(), newFile);
}
}
if (newFile.exists()) {
newFile.delete();
}
File newFile = new File(cacheFolder, Integer.toString(downloadUrl.hashCode()));
output = new FileOutputStream(newFile);
long size = IOUtils.copy(input, output);
HttpDownloader result = downloadFile(newFile.getAbsolutePath(), downloadUrl);
if (result.getResult().isSuccessful()) {
long size = result.getDownloadRequest().getSoFar();
final DiskCacheObject dco = new DiskCacheObject(newFile.getAbsolutePath(), size);
dco = new DiskCacheObject(newFile.getAbsolutePath(), size);
addToDiskCache(downloadUrl, dco);
if (AppConfig.DEBUG) Log.d(TAG, "Image was downloaded");
onImageLoaded(dco);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(output);
} else {
Log.w(TAG, "Download of url " + downloadUrl + " failed. Reason: " + result.getResult().getReasonDetailed() + "(" + result.getResult().getReason() + ")");
}
if (dco != null) {
final DiskCacheObject dcoRef = dco;
handler.post(new Runnable() {
@Override
public void run() {
onImageLoaded(dcoRef);
}
});
}
runningDownloads.remove(newFile.getAbsolutePath());
}
private HttpDownloader downloadFile(String destination, String source) {
DownloadRequest request = new DownloadRequest(destination, source, "", 0, 0);
HttpDownloader downloader = new HttpDownloader(request);
downloader.call();
return downloader;
}
}
private static class DiskCacheObject implements Serializable{
private static class DiskCacheObject implements Serializable {
private final String fileUrl;
/**

View File

@ -148,6 +148,7 @@ public class ImageLoader {
target.setImageBitmap(cBitmap.getBitmap());
} else {
target.setImageResource(defaultCoverResource);
target.setTag(R.id.imageloader_key, source.getImageLoaderCacheKey());
BitmapDecodeWorkerTask worker = new BitmapDecodeWorkerTask(
handler, target, source, length, IMAGE_TYPE_THUMBNAIL);
executor.submit(worker);