From 8571e6ffad148690919166066e0ce47ac2818813 Mon Sep 17 00:00:00 2001 From: PaulStets Date: Tue, 24 Apr 2018 10:47:43 +0300 Subject: [PATCH] Missing Wiki links sow browser warning and article short description fix --- .../wikivoyage/WikivoyageWebViewClient.java | 17 +++++++++------ .../plus/wikivoyage/data/TravelArticle.java | 21 +++++++++++++++---- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java index 6bf78b3276..170f33bf92 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java @@ -88,14 +88,14 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall if (articleId != 0) { WikivoyageArticleDialogFragment.showInstance(app, fragmentManager, articleId, lang); } else { - warnAboutExternalLoad(url); + warnAboutExternalLoad(url, context, nightMode); } return true; } else if (url.contains(WIKI_DOMAIN)) { String articleName = getArticleNameFromUrl(url, getLang(url)); getWikiArticle(articleName, url); } else if (url.startsWith(PAGE_PREFIX_HTTP) || url.startsWith(PAGE_PREFIX_HTTPS)) { - warnAboutExternalLoad(url); + warnAboutExternalLoad(url, context, nightMode); } else if (url.startsWith(PREFIX_GEO)) { if (article != null) { List points = article.getGpxFile().getPoints(); @@ -157,7 +157,7 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall return articleName; } - private void warnAboutExternalLoad(final String url) { + private static void warnAboutExternalLoad(final String url, final Context context, final boolean nightMode) { new AlertDialog.Builder(context) .setTitle(url) .setMessage(R.string.online_webpage_warning) @@ -186,7 +186,7 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall WikivoyageArticleWikiLinkFragment.showInstance(fragmentManager, regionName == null ? "" : regionName, url); } else { - articleSearchTask = new WikiArticleSearchTask(name, indexes, (MapActivity) context); + articleSearchTask = new WikiArticleSearchTask(name, indexes, (MapActivity) context, nightMode, url); articleSearchTask.execute(); } @@ -266,12 +266,17 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall private String name; private List indexes; private WeakReference weakContext; + private boolean isNightMode; + private String url; - WikiArticleSearchTask(String articleName, List indexes, MapActivity context) { + WikiArticleSearchTask(String articleName, List indexes, + MapActivity context, boolean isNightMode, String url) { name = articleName; this.indexes = indexes; weakContext = new WeakReference<>(context); dialog = createProgressDialog(); + this.isNightMode = isNightMode; + this.url = url; } @Override @@ -309,7 +314,7 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall if (!found.isEmpty()) { WikipediaDialogFragment.showInstance(activity, found.get(0)); } else { - Toast.makeText(activity, R.string.wiki_article_not_found, Toast.LENGTH_LONG).show(); + warnAboutExternalLoad(url, weakContext.get(), isNightMode); } } } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java index 14b2e9e482..3cdc56b73a 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java @@ -91,14 +91,27 @@ public class TravelArticle { return null; } - int firstParagraphStart = content.indexOf("

"); - int firstParagraphEnd = content.indexOf("

"); + String pOpened = "

"; + String pClosed = "

"; + + int firstParagraphStart = content.indexOf(pOpened); + int firstParagraphEnd = content.indexOf(pClosed); if (firstParagraphStart == -1 || firstParagraphEnd == -1) { return null; } + int pClosedLength = pClosed.length(); + String firstParagraphHtml = content.substring(firstParagraphStart, firstParagraphEnd + pClosedLength); + while (firstParagraphHtml.length() == (pOpened.length() + pClosedLength) + && (firstParagraphEnd + pClosedLength) < content.length()) { + firstParagraphStart = content.indexOf(pOpened, firstParagraphEnd); + firstParagraphEnd = firstParagraphStart == -1 ? -1 : content.indexOf(pClosed, firstParagraphStart); + if (firstParagraphStart != -1 && firstParagraphEnd != -1) { + firstParagraphHtml = content.substring(firstParagraphStart, firstParagraphEnd + pClosedLength); + } else { + break; + } + } - // 4 is the length of

tag - String firstParagraphHtml = content.substring(firstParagraphStart, firstParagraphEnd + 4); String firstParagraphText = Html.fromHtml(firstParagraphHtml).toString().trim(); String[] phrases = firstParagraphText.split("\\. ");