add webViewClient for work with external links
This commit is contained in:
parent
c8418eae4c
commit
516dc06012
2 changed files with 56 additions and 0 deletions
|
@ -136,6 +136,7 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
|
|||
contentWebView = (WebView) mainView.findViewById(R.id.content_web_view);
|
||||
WebSettings webSettings = contentWebView.getSettings();
|
||||
webSettings.setJavaScriptEnabled(true);
|
||||
contentWebView.setWebViewClient(new WikipediaWebViewClient(getActivity(), darkMode));
|
||||
|
||||
return mainView;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package net.osmand.plus.mapcontextmenu;
|
||||
|
||||
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.v7.app.AlertDialog;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import net.osmand.plus.R;
|
||||
|
||||
|
||||
public class WikipediaWebViewClient extends WebViewClient {
|
||||
|
||||
private static final String PAGE_PREFIX_HTTP = "http://";
|
||||
private static final String PAGE_PREFIX_HTTPS = "https://";
|
||||
|
||||
private Context context;
|
||||
private boolean nightMode;
|
||||
|
||||
public WikipediaWebViewClient(FragmentActivity context, boolean nightMode) {
|
||||
this.context = context;
|
||||
this.nightMode = nightMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||||
if (url.startsWith(PAGE_PREFIX_HTTP) || url.startsWith(PAGE_PREFIX_HTTPS)) {
|
||||
warnAboutExternalLoad(url, context, nightMode);
|
||||
} else {
|
||||
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
|
||||
context.startActivity(i);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void warnAboutExternalLoad(final String url, final Context context, final boolean nightMode) {
|
||||
if (context == null) {
|
||||
return;
|
||||
}
|
||||
new AlertDialog.Builder(context)
|
||||
.setTitle(url)
|
||||
.setMessage(R.string.online_webpage_warning)
|
||||
.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
WikipediaDialogFragment.showFullArticle(context, Uri.parse(url), nightMode);
|
||||
}
|
||||
})
|
||||
.setNegativeButton(R.string.shared_string_cancel, null)
|
||||
.show();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue