diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2012-06-17 13:16:02 +0200 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2012-06-17 13:16:02 +0200 |
commit | 903ac4252151004f27e975384f71defbfb11f1e1 (patch) | |
tree | 14432e7aaa19df950a324e35f3aeb4ceedfa6cab /main/src | |
parent | 8821d6ace1a2dee3645e21a31d5756480b00f1f3 (diff) | |
download | cgeo-903ac4252151004f27e975384f71defbfb11f1e1.zip cgeo-903ac4252151004f27e975384f71defbfb11f1e1.tar.gz cgeo-903ac4252151004f27e975384f71defbfb11f1e1.tar.bz2 |
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.
Diffstat (limited to 'main/src')
-rw-r--r-- | main/src/cgeo/geocaching/network/HtmlImage.java | 19 |
1 files changed, 18 insertions, 1 deletions
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); |