diff options
Diffstat (limited to 'main/src/cgeo/geocaching/network/HtmlImage.java')
| -rw-r--r-- | main/src/cgeo/geocaching/network/HtmlImage.java | 70 |
1 files changed, 34 insertions, 36 deletions
diff --git a/main/src/cgeo/geocaching/network/HtmlImage.java b/main/src/cgeo/geocaching/network/HtmlImage.java index 1811110..7e4143e 100644 --- a/main/src/cgeo/geocaching/network/HtmlImage.java +++ b/main/src/cgeo/geocaching/network/HtmlImage.java @@ -1,10 +1,10 @@ package cgeo.geocaching.network; import cgeo.geocaching.R; -import cgeo.geocaching.Settings; import cgeo.geocaching.StoredList; import cgeo.geocaching.connector.ConnectorFactory; import cgeo.geocaching.files.LocalStorage; +import cgeo.geocaching.utils.Log; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpResponse; @@ -16,7 +16,6 @@ import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.text.Html; -import android.util.Log; import android.view.Display; import android.view.WindowManager; @@ -72,26 +71,26 @@ public class HtmlImage implements Html.ImageGetter { return new BitmapDrawable(getTransparent1x1Image()); } - Bitmap imagePre = null; - - // Load image from cache - if (!onlySave) { - imagePre = loadImageFromStorage(url); - } + Bitmap imagePre = loadImageFromStorage(url); // Download image and save it to the cache - if (imagePre == null || onlySave) { + if (imagePre == null) { final String absoluteURL = makeAbsoluteURL(url); if (absoluteURL != null) { try { - final HttpResponse httpResponse = Network.request(absoluteURL, null, false); + final File file = LocalStorage.getStorageFile(geocode, url, true, true); + final HttpResponse httpResponse = Network.getRequest(absoluteURL, null, file); if (httpResponse != null) { - final File file = LocalStorage.getStorageFile(geocode, url, true, true); - LocalStorage.saveEntityToFile(httpResponse, file); + final int statusCode = httpResponse.getStatusLine().getStatusCode(); + if (statusCode == 200) { + LocalStorage.saveEntityToFile(httpResponse, file); + } else if (statusCode == 304) { + file.setLastModified(System.currentTimeMillis()); + } } } catch (Exception e) { - Log.e(Settings.tag, "HtmlImage.getDrawable (downloading from web)", e); + Log.e("HtmlImage.getDrawable (downloading from web)", e); } } } @@ -107,7 +106,7 @@ public class HtmlImage implements Html.ImageGetter { // get image and return if (imagePre == null) { - Log.d(Settings.tag, "HtmlImage.getDrawable: Failed to obtain image"); + Log.d("HtmlImage.getDrawable: Failed to obtain image"); if (returnErrorImage) { imagePre = BitmapFactory.decodeResource(context.getResources(), R.drawable.image_not_loaded); @@ -130,7 +129,7 @@ public class HtmlImage implements Html.ImageGetter { try { imagePre = Bitmap.createScaledBitmap(imagePre, width, height, true); } catch (Exception e) { - Log.d(Settings.tag, "HtmlImage.getDrawable: Failed to scale image"); + Log.d("HtmlImage.getDrawable: Failed to scale image"); return null; } } else { @@ -158,32 +157,31 @@ public class HtmlImage implements Html.ImageGetter { final File fileSec = LocalStorage.getStorageSecFile(geocode, url, true); return loadCachedImage(fileSec); } catch (Exception e) { - Log.w(Settings.tag, "HtmlImage.getDrawable (reading cache): " + e.toString()); + Log.w("HtmlImage.getDrawable (reading cache): " + e.toString()); } return null; } - private final String makeAbsoluteURL(final String url) { - try { - // Check if uri is absolute or not, if not attach the connector hostname - // FIXME: that should also include the scheme - if (Uri.parse(url).isAbsolute()) { - return url; - } else { - final String host = ConnectorFactory.getConnector(geocode).getHost(); - if (StringUtils.isNotEmpty(host)) { - StringBuilder builder = new StringBuilder("http://"); - builder.append(host); - if (!StringUtils.startsWith(url, "/")) { - builder.append('/'); - } - builder.append(url); - return builder.toString(); - } + private String makeAbsoluteURL(final String url) { + // Check if uri is absolute or not, if not attach the connector hostname + // FIXME: that should also include the scheme + if (Uri.parse(url).isAbsolute()) { + return url; + } + + final String host = ConnectorFactory.getConnector(geocode).getHost(); + if (StringUtils.isNotEmpty(host)) { + final StringBuilder builder = new StringBuilder("http://"); + builder.append(host); + if (!StringUtils.startsWith(url, "/")) { + // FIXME: explain why the result URL would be valid if the path does not start with + // a '/', or signal an error. + builder.append('/'); } - } catch (Exception e) { - Log.e(Settings.tag, "HtmlImage.makeAbsoluteURL (parse URL)", e); + builder.append(url); + return builder.toString(); } + return null; } @@ -207,7 +205,7 @@ public class HtmlImage implements Html.ImageGetter { fis = new FileInputStream(file); BitmapFactory.decodeStream(fis, null, options); } catch (FileNotFoundException e) { - Log.e(Settings.tag, "HtmlImage.setSampleSize", e); + Log.e("HtmlImage.setSampleSize", e); } finally { if (fis != null) { try { |
