diff --git a/com.hdrdevs.turnosxpress/app/src/main/java/com/hdrdevs/turnosxpress/MainActivity.java b/com.hdrdevs.turnosxpress/app/src/main/java/com/hdrdevs/turnosxpress/MainActivity.java index 90e8679..585b10b 100644 --- a/com.hdrdevs.turnosxpress/app/src/main/java/com/hdrdevs/turnosxpress/MainActivity.java +++ b/com.hdrdevs.turnosxpress/app/src/main/java/com/hdrdevs/turnosxpress/MainActivity.java @@ -3,6 +3,8 @@ package com.hdrdevs.turnosxpress; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; +import android.os.Message; +import android.content.ActivityNotFoundException; import android.content.Intent; import android.net.Uri; import android.util.Log; @@ -70,11 +72,10 @@ public class MainActivity extends AppCompatActivity { webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView vw, WebResourceRequest request) { - if (request.getUrl().toString().contains(home.getHost())) { + if (shouldLoadInsideApp(request.getUrl(), home)) { vw.loadUrl(request.getUrl().toString()); } else { - Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl()); - vw.getContext().startActivity(intent); + openExternalUri(request.getUrl()); } return true; @@ -95,7 +96,7 @@ public class MainActivity extends AppCompatActivity { @Override public boolean onShowFileChooser(WebView vw, ValueCallback filePathCallback, - FileChooserParams fileChooserParams) { + FileChooserParams fileChooserParams) { if (fileChooserCallback != null) { fileChooserCallback.onReceiveValue(null); } @@ -112,6 +113,25 @@ public class MainActivity extends AppCompatActivity { return true; } + @Override + public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) { + WebView popupWebView = new WebView(MainActivity.this); + popupWebView.setWebViewClient(new WebViewClient() { + @Override + public boolean shouldOverrideUrlLoading(WebView popupView, WebResourceRequest request) { + openExternalUri(request.getUrl()); + popupWebView.destroy(); + return true; + } + }); + + WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj; + transport.setWebView(popupWebView); + resultMsg.sendToTarget(); + + return true; + } + }); webView.setOnKeyListener((v, keyCode, event) -> @@ -163,10 +183,40 @@ public class MainActivity extends AppCompatActivity { private void handleURI(String uri) { if (uri != null) { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse(uri.replaceFirst("^blob:", ""))); + openExternalUri(Uri.parse(uri.replaceFirst("^blob:", ""))); + } + } - startActivity(i); + private boolean shouldLoadInsideApp(Uri uri, Uri home) { + return uri != null + && uri.getHost() != null + && uri.getHost().equals(home.getHost()) + && ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme())); + } + + private void openExternalUri(Uri uri) { + if (uri == null) { + return; + } + + try { + Intent intent; + + if ("intent".equals(uri.getScheme())) { + intent = Intent.parseUri(uri.toString(), Intent.URI_INTENT_SCHEME); + intent.addCategory(Intent.CATEGORY_BROWSABLE); + intent.setComponent(null); + } else { + intent = new Intent(Intent.ACTION_VIEW, uri); + } + + startActivity(intent); + } catch (ActivityNotFoundException e) { + Log.w("WEBVIEW", "No app can handle URI: " + uri, e); + Toast.makeText(this, "No hay una aplicación disponible para abrir este enlace", Toast.LENGTH_LONG).show(); + } catch (Exception e) { + Log.e("WEBVIEW", "Error opening URI: " + uri, e); + Toast.makeText(this, "No se pudo abrir el enlace", Toast.LENGTH_LONG).show(); } }