From 1c44ec042f349cbb6fe6da684da3d35969b16ec5 Mon Sep 17 00:00:00 2001 From: PaulStets Date: Sat, 14 Apr 2018 13:05:27 +0300 Subject: [PATCH 1/2] Added abilyty to open article links internally --- OsmAnd/res/values/strings.xml | 1 + .../WikivoyageArticleDialogFragment.java | 2 + .../wikivoyage/data/CustomWebViewClient.java | 77 +++++++++++++++++++ .../wikivoyage/data/WikivoyageDbHelper.java | 15 ++++ 4 files changed, 95 insertions(+) create mode 100644 OsmAnd/src/net/osmand/plus/wikivoyage/data/CustomWebViewClient.java diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 261e7551e3..6c8e23e6cd 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -9,6 +9,7 @@ 3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy --> + This page is only available online. Do you wish to open it in a web browser? Images cache Delete search history Show images diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java index f358e3d0c0..dc929bca03 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java @@ -26,6 +26,7 @@ import net.osmand.IndexConstants; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.wikivoyage.WikivoyageBaseDialogFragment; +import net.osmand.plus.wikivoyage.data.CustomWebViewClient; import net.osmand.plus.wikivoyage.data.WikivoyageArticle; import net.osmand.plus.wikivoyage.data.WikivoyageLocalDataHelper; import net.osmand.util.Algorithms; @@ -122,6 +123,7 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen contentWebView = (WebView) mainView.findViewById(R.id.content_web_view); contentWebView.getSettings().setJavaScriptEnabled(true); contentWebView.getSettings().setCacheMode(showImages ? WebSettings.LOAD_DEFAULT : WebSettings.LOAD_CACHE_ONLY); + contentWebView.setWebViewClient(new CustomWebViewClient(getActivity(), getFragmentManager())); return mainView; } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/CustomWebViewClient.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/CustomWebViewClient.java new file mode 100644 index 0000000000..ea2325605e --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/CustomWebViewClient.java @@ -0,0 +1,77 @@ +package net.osmand.plus.wikivoyage.data; + + +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.support.v7.app.AlertDialog; +import android.webkit.WebView; +import android.webkit.WebViewClient; + +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.wikivoyage.article.WikivoyageArticleDialogFragment; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; + +/** + * Custom WebView client to handle the internal links. + */ + +public class CustomWebViewClient extends WebViewClient { + + private OsmandApplication app; + private FragmentManager mFragmentManager; + private Context mContext; + + private static final String PAGE_PREFIX = "https://"; + private static final String WEB_DOMAIN = ".wikivoyage.com/wiki/"; + + public CustomWebViewClient(FragmentActivity context, FragmentManager fm) { + app = (OsmandApplication) context.getApplication(); + mFragmentManager = fm; + mContext = context; + } + + @Override + public boolean shouldOverrideUrlLoading(WebView view, String url) { + if (url.contains(WEB_DOMAIN)) { + String lang = url.substring(url.indexOf(PAGE_PREFIX) + PAGE_PREFIX.length(), url.indexOf(".")); + String articleName = url.replace(PAGE_PREFIX + lang + WEB_DOMAIN, "") + .replaceAll("_", " "); + try { + articleName = URLDecoder.decode(articleName, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + long articleId = app.getWikivoyageDbHelper().getArticleId(articleName, lang); + if (articleId != 0) { + WikivoyageArticleDialogFragment.showInstance(app, mFragmentManager, + articleId, lang); + } else { + warnAboutExternalLoad(url); + } + return true; + } + warnAboutExternalLoad(url); + return true; + } + + private void warnAboutExternalLoad(final String url) { + AlertDialog.Builder builder = new AlertDialog.Builder(mContext); + builder.setMessage(R.string.online_webpage_warning); + builder.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); + mContext.startActivity(i); + } + }); + builder.setNegativeButton(R.string.shared_string_cancel, null); + builder.show(); + } +} diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java index 4cc56ae904..cfdf687ce2 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java @@ -223,6 +223,21 @@ public class WikivoyageDbHelper { return res; } + public long getArticleId(String title, String lang) { + long res = 0; + SQLiteConnection conn = openConnection(); + if (conn != null) { + SQLiteCursor cursor = conn.rawQuery("SELECT " + ARTICLES_COL_CITY_ID + " FROM " + + ARTICLES_TABLE_NAME + " WHERE " + ARTICLES_COL_TITLE + " = ? AND " + + ARTICLES_COL_LANG + " = ?", new String[]{title, lang}); + if (cursor.moveToFirst()) { + res = cursor.getLong(0); + } + cursor.close(); + } + return res; + } + @NonNull public ArrayList getArticleLangs(long cityId) { ArrayList res = new ArrayList<>(); From a27e0ca555a45eefefc55c30a7dc84cca4e05c92 Mon Sep 17 00:00:00 2001 From: PaulStets Date: Sat, 14 Apr 2018 14:05:58 +0300 Subject: [PATCH 2/2] Added a small improvement to url parsing --- .../net/osmand/plus/wikivoyage/data/CustomWebViewClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/CustomWebViewClient.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/CustomWebViewClient.java index ea2325605e..e26bf7cf53 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/CustomWebViewClient.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/CustomWebViewClient.java @@ -40,7 +40,7 @@ public class CustomWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.contains(WEB_DOMAIN)) { - String lang = url.substring(url.indexOf(PAGE_PREFIX) + PAGE_PREFIX.length(), url.indexOf(".")); + String lang = url.substring(url.startsWith(PAGE_PREFIX) ? PAGE_PREFIX.length() : 0, url.indexOf(".")); String articleName = url.replace(PAGE_PREFIX + lang + WEB_DOMAIN, "") .replaceAll("_", " "); try {