diff options
-rw-r--r-- | res/values/colors.xml | 3 | ||||
-rw-r--r-- | res/values/ids.xml | 1 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java | 4 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/asynctask/ImageDiskCache.java | 73 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/asynctask/ImageLoader.java | 1 |
5 files changed, 55 insertions, 27 deletions
diff --git a/res/values/colors.xml b/res/values/colors.xml index a666f8967..7ce05bc13 100644 --- a/res/values/colors.xml +++ b/res/values/colors.xml @@ -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> diff --git a/res/values/ids.xml b/res/values/ids.xml index 7cbc4699a..995cebbad 100644 --- a/res/values/ids.xml +++ b/res/values/ids.xml @@ -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>
\ No newline at end of file diff --git a/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java b/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java index 7ba68ae22..1f68c17e1 100644 --- a/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java +++ b/src/de/danoeh/antennapod/asynctask/BitmapDecodeWorkerTask.java @@ -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() { diff --git a/src/de/danoeh/antennapod/asynctask/ImageDiskCache.java b/src/de/danoeh/antennapod/asynctask/ImageDiskCache.java index 0179260cc..216fbf545 100644 --- a/src/de/danoeh/antennapod/asynctask/ImageDiskCache.java +++ b/src/de/danoeh/antennapod/asynctask/ImageDiskCache.java @@ -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; /** diff --git a/src/de/danoeh/antennapod/asynctask/ImageLoader.java b/src/de/danoeh/antennapod/asynctask/ImageLoader.java index 45a99e704..6531a7e50 100644 --- a/src/de/danoeh/antennapod/asynctask/ImageLoader.java +++ b/src/de/danoeh/antennapod/asynctask/ImageLoader.java @@ -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); |