Added abilyty to open article links internally

This commit is contained in:
PaulStets 2018-04-14 13:05:27 +03:00
parent 4e2241ca33
commit 1c44ec042f
4 changed files with 95 additions and 0 deletions

View file

@ -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
-->
<string name="online_webpage_warning">This page is only available online. Do you wish to open it in a web browser?</string>
<string name="images_cache">Images cache</string>
<string name="delete_search_history">Delete search history</string>
<string name="show_images">Show images</string>

View file

@ -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;
}

View file

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

View file

@ -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<String> getArticleLangs(long cityId) {
ArrayList<String> res = new ArrayList<>();