/** * Load an image from primary or secondary storage. * * @param url the image URL * @param pseudoGeocode the geocode or the shared name * @param forceKeep keep the image if it is there, without checking its freshness * @return <code>true</code> if the image was there and is fresh enough, <code>false</code> * otherwise */ @NonNull private Pair<Bitmap, Boolean> loadImageFromStorage( final String url, final String pseudoGeocode, final boolean forceKeep) { try { final File file = LocalStorage.getStorageFile(pseudoGeocode, url, true, false); final Pair<Bitmap, Boolean> image = loadCachedImage(file, forceKeep); if (image.getRight() || image.getLeft() != null) { return image; } final File fileSec = LocalStorage.getStorageSecFile(pseudoGeocode, url, true); return loadCachedImage(fileSec, forceKeep); } catch (Exception e) { Log.w("HtmlImage.loadImageFromStorage", e); } return new ImmutablePair<Bitmap, Boolean>(null, false); }
/** * 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. * * <p>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"); if (file.renameTo(tempFile)) { LocalStorage.copy(tempFile, file); FileUtils.deleteIgnoringFailure(tempFile); } else { Log.e("Could not reset timestamp of file " + file.getAbsolutePath()); } }
/** * Download or refresh the copy of <code>url</code> in <code>file</code>. * * @param url the url of the document * @param file the file to save the document in * @return <code>true</code> if the existing file was up-to-date, <code>false</code> otherwise */ private boolean downloadOrRefreshCopy(final String url, final File file) { final String absoluteURL = makeAbsoluteURL(url); if (absoluteURL != null) { try { final HttpResponse httpResponse = Network.getRequest(absoluteURL, null, file); if (httpResponse != null) { final int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == 200) { LocalStorage.saveEntityToFile(httpResponse, file); } else if (statusCode == 304) { if (!file.setLastModified(System.currentTimeMillis())) { makeFreshCopy(file); } return true; } } } catch (Exception e) { Log.e("HtmlImage.downloadOrRefreshCopy", e); } } return false; }