From 9dc51f38bfbf70682d538399f5a8c4024c62f011 Mon Sep 17 00:00:00 2001 From: dgis Date: Mon, 1 Mar 2021 12:20:55 +0100 Subject: [PATCH] - Open an external web browser when you click an external links in the Help. --- .../emulator/calculator/InfoWebFragment.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/emulator/calculator/InfoWebFragment.java b/app/src/main/java/org/emulator/calculator/InfoWebFragment.java index f629ab9..014a559 100644 --- a/app/src/main/java/org/emulator/calculator/InfoWebFragment.java +++ b/app/src/main/java/org/emulator/calculator/InfoWebFragment.java @@ -16,8 +16,8 @@ package org.emulator.calculator; import android.annotation.SuppressLint; import android.app.Dialog; -import android.graphics.PorterDuff; -import android.graphics.drawable.Drawable; +import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -30,6 +30,8 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatDialogFragment; import androidx.appcompat.widget.Toolbar; +import java.util.Locale; + public class InfoWebFragment extends AppCompatDialogFragment { @Override @@ -62,9 +64,7 @@ public class InfoWebFragment extends AppCompatDialogFragment { Toolbar toolbar = view.findViewById(Utils.resId(this, "id", "my_toolbar")); toolbar.setTitle(title); Utils.colorizeDrawableWithColor(requireContext(), toolbar.getNavigationIcon(), android.R.attr.colorForeground); - toolbar.setNavigationOnClickListener(v -> { - dismiss(); - }); + toolbar.setNavigationOnClickListener(v -> dismiss()); WebView webView = view.findViewById(Utils.resId(this, "id", "webViewInfo")); webView.getSettings().setJavaScriptEnabled(true); @@ -77,6 +77,16 @@ public class InfoWebFragment extends AppCompatDialogFragment { // Inject a CSS style to wrap the table of content if needed view.evaluateJavascript("javascript:(function(){var css=document.createElement(\"style\");css.type=\"text/css\";css.innerHTML=\".nav1{overflow-wrap:break-word;}\";document.head.appendChild(css);})();", null); } + + @Override + public boolean shouldOverrideUrlLoading(WebView view, String url) { + if(url != null && url.toLowerCase(Locale.ENGLISH).matches("^https?://.*")) { + // External pages should be loaded in external web browser. + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); + return true; + } + return super.shouldOverrideUrlLoading(view, url); + } }); webView.loadUrl(getString(Utils.resId(this, "string", "help_url")));