diff options
Diffstat (limited to 'main')
| -rw-r--r-- | main/res/drawable-mdpi/helper_gpsstatus.png | bin | 59328 -> 83529 bytes | |||
| -rw-r--r-- | main/src/cgeo/geocaching/connector/gc/GCParser.java | 4 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/LocalStorage.java | 9 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/network/HtmlImage.java | 54 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/HtmlUtils.java | 5 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/ImageUtils.java | 16 |
6 files changed, 64 insertions, 24 deletions
diff --git a/main/res/drawable-mdpi/helper_gpsstatus.png b/main/res/drawable-mdpi/helper_gpsstatus.png Binary files differindex 07c6419..1cd111e 100644 --- a/main/res/drawable-mdpi/helper_gpsstatus.png +++ b/main/res/drawable-mdpi/helper_gpsstatus.png diff --git a/main/src/cgeo/geocaching/connector/gc/GCParser.java b/main/src/cgeo/geocaching/connector/gc/GCParser.java index 34e48ab..b373cb1 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCParser.java +++ b/main/src/cgeo/geocaching/connector/gc/GCParser.java @@ -34,8 +34,8 @@ import cgeo.geocaching.utils.MatcherWrapper; import cgeo.geocaching.utils.TextUtils; import ch.boye.httpclientandroidlib.HttpResponse; - import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.ListUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; @@ -1660,7 +1660,7 @@ public abstract class GCParser { public static List<LogType> parseTypes(String page) { if (StringUtils.isEmpty(page)) { - return null; + return ListUtils.EMPTY_LIST; } final List<LogType> types = new ArrayList<LogType>(); diff --git a/main/src/cgeo/geocaching/files/LocalStorage.java b/main/src/cgeo/geocaching/files/LocalStorage.java index fc82409..de1908d 100644 --- a/main/src/cgeo/geocaching/files/LocalStorage.java +++ b/main/src/cgeo/geocaching/files/LocalStorage.java @@ -99,7 +99,14 @@ public final class LocalStorage { * @return the file extension, including the leading dot, or the empty string if none could be determined */ static String getExtension(final String url) { - final String urlExt = StringUtils.substringAfterLast(url, "."); + String urlExt; + if (url.startsWith("data:")) { + // "data:image/png;base64,i53…" -> ".png" + urlExt = StringUtils.substringAfter(StringUtils.substringBefore(url, ";"), "/"); + } else { + // "http://example.com/foo/bar.png" -> ".png" + urlExt = StringUtils.substringAfterLast(url, "."); + } return urlExt.length() >= 1 && urlExt.length() <= 4 ? "." + urlExt : ""; } diff --git a/main/src/cgeo/geocaching/network/HtmlImage.java b/main/src/cgeo/geocaching/network/HtmlImage.java index 797e67d..079e254 100644 --- a/main/src/cgeo/geocaching/network/HtmlImage.java +++ b/main/src/cgeo/geocaching/network/HtmlImage.java @@ -12,7 +12,6 @@ import cgeo.geocaching.utils.ImageUtils; import cgeo.geocaching.utils.Log; import ch.boye.httpclientandroidlib.HttpResponse; - import org.apache.commons.lang3.StringUtils; import android.content.res.Resources; @@ -22,11 +21,15 @@ import android.graphics.Point; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.text.Html; +import android.util.Base64; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.util.Date; public class HtmlImage implements Html.ImageGetter { @@ -91,24 +94,43 @@ public class HtmlImage implements Html.ImageGetter { // Download image and save it to the cache if (imagePre == null) { - final String absoluteURL = makeAbsoluteURL(url); - - if (absoluteURL != null) { - try { - final File file = LocalStorage.getStorageFile(pseudoGeocode, url, true, true); - 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); + final File file = LocalStorage.getStorageFile(pseudoGeocode, url, true, true); + if (url.startsWith("data:image/")) { + if (url.contains(";base64,")) { + byte[] decoded = Base64.decode(StringUtils.substringAfter(url, ";base64,"), Base64.DEFAULT); + OutputStream out = null; + try { + out = new FileOutputStream(file); + out.write(decoded); + } catch (final IOException e) { + Log.e("HtmlImage.getDrawable: cannot write file for decoded inline image", e); + return null; + } finally { + IOUtils.closeQuietly(out); + } + } else { + Log.e("HtmlImage.getDrawable: unable to decode non-base64 inline image"); + return null; + } + } else { + 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); + } } } + } catch (Exception e) { + Log.e("HtmlImage.getDrawable (downloading from web)", e); } - } catch (Exception e) { - Log.e("HtmlImage.getDrawable (downloading from web)", e); } } } diff --git a/main/src/cgeo/geocaching/utils/HtmlUtils.java b/main/src/cgeo/geocaching/utils/HtmlUtils.java index 5717a37..37e20ec 100644 --- a/main/src/cgeo/geocaching/utils/HtmlUtils.java +++ b/main/src/cgeo/geocaching/utils/HtmlUtils.java @@ -3,6 +3,7 @@ package cgeo.geocaching.utils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; +import android.text.Html; import android.text.Spanned; import android.text.style.ImageSpan; @@ -56,7 +57,7 @@ public final class HtmlUtils { } } - // some line breaks are still in the text, source is unknown - return StringUtils.replace(result, "<br />", "\n").trim(); + // now that images are gone, do a normal html to text conversion + return Html.fromHtml(result).toString().trim(); } } diff --git a/main/src/cgeo/geocaching/utils/ImageUtils.java b/main/src/cgeo/geocaching/utils/ImageUtils.java index 6851241..478be1f 100644 --- a/main/src/cgeo/geocaching/utils/ImageUtils.java +++ b/main/src/cgeo/geocaching/utils/ImageUtils.java @@ -93,7 +93,7 @@ public final class ImageUtils { * Image to read * @param maxXY * boundings - * @return String filename and path, NULL if something fails + * @return filename and path, <tt>null</tt> if something fails */ public static String readScaleAndWriteImage(final String filePath, final int maxXY) { if (maxXY <= 0) { @@ -116,12 +116,21 @@ public final class ImageUtils { return null; } final BitmapDrawable scaledImage = scaleBitmapTo(image, maxXY, maxXY); - final String uploadFilename = ImageUtils.getOutputImageFile().getPath(); + final File tempImageFile = ImageUtils.getOutputImageFile(); + if (tempImageFile == null) { + Log.e("ImageUtils.readScaleAndWriteImage: unable to write scaled image"); + return null; + } + final String uploadFilename = tempImageFile.getPath(); storeBitmap(scaledImage.getBitmap(), Bitmap.CompressFormat.JPEG, 75, uploadFilename); return uploadFilename; } - /** Create a File for saving an image or video */ + /** Create a File for saving an image or video + * + * @return the temporary image file to use, or <tt>null</tt> if the media directory could + * not be created. + * */ public static File getOutputImageFile() { // To be safe, you should check that the SDCard is mounted // using Environment.getExternalStorageState() before doing this. @@ -133,6 +142,7 @@ public final class ImageUtils { // Create the storage directory if it does not exist if (!mediaStorageDir.exists()) { if (!FileUtils.mkdirs(mediaStorageDir)) { + Log.e("ImageUtils.getOutputImageFile: cannot create media storage directory"); return null; } } |
