diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2012-09-01 07:45:46 +0200 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2012-09-01 07:45:46 +0200 |
| commit | 214d8b23cbdf6245d23c64926ad2f075408aeeb6 (patch) | |
| tree | 20c6755e4d1984b8c91cbbde6f5a41b568fa6268 /main/src/cgeo/geocaching/utils | |
| parent | 0e0ca7c935ce0298855840511d77e008e2579cdf (diff) | |
| download | cgeo-214d8b23cbdf6245d23c64926ad2f075408aeeb6.zip cgeo-214d8b23cbdf6245d23c64926ad2f075408aeeb6.tar.gz cgeo-214d8b23cbdf6245d23c64926ad2f075408aeeb6.tar.bz2 | |
fix #1996: Translate is opening browser instead of translate app
Diffstat (limited to 'main/src/cgeo/geocaching/utils')
| -rw-r--r-- | main/src/cgeo/geocaching/utils/HtmlUtils.java | 56 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/ProcessUtils.java | 27 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/TranslationUtils.java | 15 |
3 files changed, 93 insertions, 5 deletions
diff --git a/main/src/cgeo/geocaching/utils/HtmlUtils.java b/main/src/cgeo/geocaching/utils/HtmlUtils.java new file mode 100644 index 0000000..a54ba57 --- /dev/null +++ b/main/src/cgeo/geocaching/utils/HtmlUtils.java @@ -0,0 +1,56 @@ +package cgeo.geocaching.utils; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.Pair; + +import android.text.Spanned; +import android.text.style.ImageSpan; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public class HtmlUtils { + + /** + * Extract the text from a HTML based string. This is similar to what HTML.fromHtml(...) does, but this method also + * removes the embedded images instead of replacing them by a small rectangular representation character. + * + * @param html + * @return + */ + public static String extractText(CharSequence html) { + String result = html.toString(); + + // recognize images in textview HTML contents + if (html instanceof Spanned) { + Spanned text = (Spanned) html; + Object[] styles = text.getSpans(0, text.length(), Object.class); + ArrayList<Pair<Integer, Integer>> removals = new ArrayList<Pair<Integer, Integer>>(); + for (Object style : styles) { + if (style instanceof ImageSpan) { + int start = text.getSpanStart(style); + int end = text.getSpanEnd(style); + removals.add(Pair.of(start, end)); + } + } + + // sort reversed and delete image spans + Collections.sort(removals, new Comparator<Pair<Integer, Integer>>() { + + @Override + public int compare(Pair<Integer, Integer> lhs, Pair<Integer, Integer> rhs) { + return rhs.getRight().compareTo(lhs.getRight()); + } + }); + result = text.toString(); + for (Pair<Integer, Integer> removal : removals) { + result = result.substring(0, removal.getLeft()) + result.substring(removal.getRight()); + } + } + + // some line breaks are still in the text, source is unknown + return StringUtils.replace(result, "<br />", "\n").trim(); + } + +} diff --git a/main/src/cgeo/geocaching/utils/ProcessUtils.java b/main/src/cgeo/geocaching/utils/ProcessUtils.java new file mode 100644 index 0000000..53991fb --- /dev/null +++ b/main/src/cgeo/geocaching/utils/ProcessUtils.java @@ -0,0 +1,27 @@ +package cgeo.geocaching.utils; + +import cgeo.geocaching.cgeoapplication; + +import android.content.Intent; +import android.content.pm.PackageManager; + +public class ProcessUtils { + + public static boolean isInstalled(final String packageName) { + return getLaunchIntent(packageName) != null; + } + + public static Intent getLaunchIntent(final String packageName) { + if (packageName == null) { + return null; + } + final PackageManager packageManager = cgeoapplication.getInstance().getPackageManager(); + try { + // This can throw an exception where the exception type is only defined on API Level > 3 + // therefore surround with try-catch + return packageManager.getLaunchIntentForPackage(packageName); + } catch (Exception e) { + return null; + } + } +} diff --git a/main/src/cgeo/geocaching/utils/TranslationUtils.java b/main/src/cgeo/geocaching/utils/TranslationUtils.java index 0066d8e..a29c5a7 100644 --- a/main/src/cgeo/geocaching/utils/TranslationUtils.java +++ b/main/src/cgeo/geocaching/utils/TranslationUtils.java @@ -4,7 +4,6 @@ import cgeo.geocaching.activity.AbstractActivity; import android.content.Intent; import android.net.Uri; -import android.text.Html; import java.net.URLEncoder; @@ -16,9 +15,10 @@ public final class TranslationUtils { private static final String translationWebsite = "http://translate.google.com/"; private static final String translationForceClassicMode = "?vi=c"; private static final String translationAutoSelect = "#auto"; - private static final String translationFieldSeparator = "/"; + private static final String translationFieldSeparator = "|"; public static final int translationTextLengthToWarn = 500; + private static final String TRANSLATION_APP = "com.google.android.apps.translate"; /** * Build a URI for Google Translate @@ -29,13 +29,18 @@ public final class TranslationUtils { * The text to be translated * @return URI ready to be parsed */ - public static String buildTranslationURI(final String toLang, final String text) { - return translationWebsite + translationForceClassicMode + translationAutoSelect + translationFieldSeparator + toLang + translationFieldSeparator + URLEncoder.encode(Html.fromHtml(text).toString()); + private static String buildTranslationURI(final String toLang, final String text) { + String content = URLEncoder.encode(text); + // the app works better without the "+", the website works better with "+", therefore assume using the app if installed + if (ProcessUtils.isInstalled(TRANSLATION_APP)) { + content = content.replace("+", "%20"); + } + return translationWebsite + translationForceClassicMode + translationAutoSelect + translationFieldSeparator + toLang + translationFieldSeparator + content; } /** * Send Intent for Google Translate. Can be caught by Google Translate App or browser - * + * * @param toLang * The two-letter lowercase ISO language codes as defined by ISO 639-1 * @param text |
