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 | |
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')
-rw-r--r-- | main/src/cgeo/geocaching/CacheDetailActivity.java | 5 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/apps/AbstractApp.java | 22 | ||||
-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 |
5 files changed, 102 insertions, 23 deletions
diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java index 5a11677..0c7febc 100644 --- a/main/src/cgeo/geocaching/CacheDetailActivity.java +++ b/main/src/cgeo/geocaching/CacheDetailActivity.java @@ -25,6 +25,7 @@ import cgeo.geocaching.utils.CancellableHandler; import cgeo.geocaching.utils.ClipboardUtils; import cgeo.geocaching.utils.CryptUtils; import cgeo.geocaching.utils.GeoDirHandler; +import cgeo.geocaching.utils.HtmlUtils; import cgeo.geocaching.utils.ImageHelper; import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.TranslationUtils; @@ -479,10 +480,10 @@ public class CacheDetailActivity extends AbstractActivity { showToast(res.getString(R.string.clipboard_copy_ok)); return true; case MENU_FIELD_TRANSLATE: - TranslationUtils.startActivityTranslate(this, Locale.getDefault().getLanguage(), clickedItemText.toString()); + TranslationUtils.startActivityTranslate(this, Locale.getDefault().getLanguage(), HtmlUtils.extractText(clickedItemText)); return true; case MENU_FIELD_TRANSLATE_EN: - TranslationUtils.startActivityTranslate(this, Locale.ENGLISH.getLanguage(), clickedItemText.toString()); + TranslationUtils.startActivityTranslate(this, Locale.ENGLISH.getLanguage(), HtmlUtils.extractText(clickedItemText)); return true; case MENU_FIELD_SHARE: final Intent intent = new Intent(Intent.ACTION_SEND); diff --git a/main/src/cgeo/geocaching/apps/AbstractApp.java b/main/src/cgeo/geocaching/apps/AbstractApp.java index 5bed2d9..678b98c 100644 --- a/main/src/cgeo/geocaching/apps/AbstractApp.java +++ b/main/src/cgeo/geocaching/apps/AbstractApp.java @@ -3,9 +3,9 @@ package cgeo.geocaching.apps; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgeo; import cgeo.geocaching.cgeoapplication; +import cgeo.geocaching.utils.ProcessUtils; import android.content.Intent; -import android.content.pm.PackageManager; public abstract class AbstractApp implements App { @@ -24,28 +24,18 @@ public abstract class AbstractApp implements App { this(name, intent, null); } - protected Intent getLaunchIntent() { - 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; - } - } - @Override public boolean isInstalled() { - if (getLaunchIntent() != null) { + if (ProcessUtils.isInstalled(packageName)) { return true; } return cgeo.isIntentAvailable(intent); } + protected Intent getLaunchIntent() { + return ProcessUtils.getLaunchIntent(packageName); + } + @Override public boolean isDefaultNavigationApp() { return true; 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 |