Adding support for open in browser.

This commit is contained in:
Sergiotarxz 2023-08-30 23:52:25 +02:00
parent ff852c695c
commit 9c5475d134
2 changed files with 34 additions and 0 deletions

View File

@ -4,6 +4,8 @@ import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebResourceRequest;
import android.webkit.JavascriptInterface;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.util.Log;
import android.view.ViewTreeObserver;
@ -20,6 +22,7 @@ import android.graphics.Bitmap;
import android.graphics.drawable.Icon;
import java.io.InputStream;
import java.net.URL;
import android.content.pm.ApplicationInfo;
import androidx.appcompat.app.AppCompatActivity;
@ -78,6 +81,24 @@ public class MainActivity extends AppCompatActivity {
class WebCallsAndroidCode {
@JavascriptInterface
public void openInBrowser(String url) {
Uri targetUri = Uri.parse(url);
Intent emptyBrowserIntent = new Intent();
emptyBrowserIntent.setAction(Intent.ACTION_VIEW);
emptyBrowserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
emptyBrowserIntent.setData(Uri.fromParts("http", "", null));
Intent targetIntent = new Intent();
targetIntent.setAction(Intent.ACTION_VIEW);
targetIntent.addCategory(Intent.CATEGORY_BROWSABLE);
targetIntent.setData(targetUri);
targetIntent.setSelector(emptyBrowserIntent);
startActivity(targetIntent);
}
@JavascriptInterface
public void pinPage(String url, String shortLabel, String iconUrl) {
ShortcutManager shortcutManager =
@ -115,6 +136,19 @@ public class MainActivity extends AppCompatActivity {
webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)) {
webView.setWebContentsDebuggingEnabled(true);
}
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Log.w("BurguillosInfo", consoleMessage.message() + " -- From line " +
consoleMessage.lineNumber() + " of " + consoleMessage.sourceId());
return true;
}
});
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);