From 903ac4252151004f27e975384f71defbfb11f1e1 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Sun, 17 Jun 2012 13:16:02 +0200 Subject: Fix #1790: use correct freshness information for pictures On some filesystems and Android versions, the "last modified" data on files cannot be modified after the file has been initially created. Since we use this information to indicate that a cached image has been refreshed, this could fail on some devices. Now, we use a degraded version of File#setLastModified if it returns false. This goes through an intermediate copy, but is always much more faster than getting the file from the network connection and saving it on the local storage. --- main/src/cgeo/geocaching/network/HtmlImage.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'main/src') diff --git a/main/src/cgeo/geocaching/network/HtmlImage.java b/main/src/cgeo/geocaching/network/HtmlImage.java index 3648d80..eac7063 100644 --- a/main/src/cgeo/geocaching/network/HtmlImage.java +++ b/main/src/cgeo/geocaching/network/HtmlImage.java @@ -92,7 +92,9 @@ public class HtmlImage implements Html.ImageGetter { if (statusCode == 200) { LocalStorage.saveEntityToFile(httpResponse, file); } else if (statusCode == 304) { - file.setLastModified(System.currentTimeMillis()); + if (!file.setLastModified(System.currentTimeMillis())) { + makeFreshCopy(file); + } } } } catch (Exception e) { @@ -124,6 +126,21 @@ public class HtmlImage implements Html.ImageGetter { return imagePre != null ? ImageHelper.scaleBitmapToFitDisplay(imagePre) : null; } + /** + * Make a fresh copy of the file to reset its timestamp. On some storage, it is impossible + * to modify the modified time after the fact, in which case a brand new file must be + * created if we want to be able to use the time as validity hint. + * + * See Android issue 1699. + * + * @param file the file to refresh + */ + private static void makeFreshCopy(final File file) { + final File tempFile = new File(file.getParentFile(), file.getName() + "-temp"); + file.renameTo(tempFile); + LocalStorage.copy(tempFile, file); + tempFile.delete(); + } private Bitmap getTransparent1x1Image() { return BitmapFactory.decodeResource(resources, R.drawable.image_no_placement); -- cgit v1.1