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<>();