From 8ae8e9a5c4975dfe06e09134da7fef1eb6928433 Mon Sep 17 00:00:00 2001 From: Michael Gangolf Date: Wed, 1 Jan 2025 17:36:41 +0100 Subject: [PATCH 1/3] fix(android): webview input file select dialog --- .../ui/widget/webview/TiWebChromeClient.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebChromeClient.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebChromeClient.java index 9dc644f4e28..707e621dca3 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebChromeClient.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebChromeClient.java @@ -561,7 +561,7 @@ public void onError(Activity activity, int requestCode, Exception ex) private Intent createFileChooserIntentFrom(WebChromeClient.FileChooserParams chooserParams) { // Create the intent. - Intent intent = new Intent(); + Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); // Set the intent's category, if needed. if (!chooserParams.isCaptureEnabled()) { @@ -646,10 +646,17 @@ private Intent createFileChooserIntentFrom(WebChromeClient.FileChooserParams cho } // If multiple apps can handle the intent, then let the end-user choose which one to use. - intent = Intent.createChooser(intent, chooserParams.getTitle()); + + Intent chooser = new Intent(Intent.ACTION_CHOOSER); + chooser.putExtra(Intent.EXTRA_INTENT, intent); + chooser.putExtra(Intent.EXTRA_TITLE, chooserParams.getTitle()); + + Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); + Intent[] intentArray = { cameraIntent }; + chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); // Return the final intent for file selection or image/video capturing. - return intent; + return chooser; } /** From 8a6e97b42f3437d7c2c735b41ea291b68e1b9359 Mon Sep 17 00:00:00 2001 From: Michael Gangolf Date: Wed, 1 Jan 2025 20:26:28 +0100 Subject: [PATCH 2/3] camera fix --- .../ui/widget/webview/TiWebChromeClient.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebChromeClient.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebChromeClient.java index 707e621dca3..120ea0a8c22 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebChromeClient.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebChromeClient.java @@ -561,7 +561,7 @@ public void onError(Activity activity, int requestCode, Exception ex) private Intent createFileChooserIntentFrom(WebChromeClient.FileChooserParams chooserParams) { // Create the intent. - Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); + Intent intent = new Intent(); // Set the intent's category, if needed. if (!chooserParams.isCaptureEnabled()) { @@ -623,17 +623,14 @@ private Intent createFileChooserIntentFrom(WebChromeClient.FileChooserParams cho } // If capturing a photo/video, create a file for it in the gallery and get a "content://" URI to it. - switch (actionName) { - case MediaStore.ACTION_IMAGE_CAPTURE: - mCaptureFileUri = MediaModule.createExternalPictureContentUri(true); - break; - case MediaStore.ACTION_VIDEO_CAPTURE: - mCaptureFileUri = MediaModule.createExternalVideoContentUri(true); - break; - default: - mCaptureFileUri = null; - break; + if (hasImageMimeType) { + mCaptureFileUri = MediaModule.createExternalPictureContentUri(true); + } else if (hasVideoMimeType) { + mCaptureFileUri = MediaModule.createExternalVideoContentUri(true); + } else { + mCaptureFileUri = null; } + if (mCaptureFileUri != null) { intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setClipData(ClipData.newRawUri("", mCaptureFileUri)); From 9ae55c27405bcfd64002877c977721dcc7b9b4ee Mon Sep 17 00:00:00 2001 From: Michael Gangolf Date: Sat, 4 Jan 2025 20:48:09 +0100 Subject: [PATCH 3/3] webview download --- .../ui/widget/webview/TiUIWebView.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiUIWebView.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiUIWebView.java index ef191355591..b0f4213b51f 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiUIWebView.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiUIWebView.java @@ -6,18 +6,23 @@ */ package ti.modules.titanium.ui.widget.webview; +import android.app.DownloadManager; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.FeatureInfo; import android.graphics.Color; import android.graphics.Rect; import android.net.Uri; +import android.os.Environment; import android.view.ActionMode; import android.view.Menu; import android.view.MenuInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewParent; +import android.webkit.CookieManager; +import android.webkit.DownloadListener; +import android.webkit.URLUtil; import android.webkit.WebSettings; import android.webkit.WebView; import androidx.annotation.RequiresApi; @@ -28,6 +33,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Method; +import java.net.URLConnection; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -378,6 +384,39 @@ public TiUIWebView(TiViewProxy proxy) params.height = TiCompositeLayout.LayoutParams.MATCH_PARENT; params.width = TiCompositeLayout.LayoutParams.MATCH_PARENT; + /* + enable file download from tag + it starts download automatically in the background and + shows a notification + */ + webView.setDownloadListener(new DownloadListener() { + @Override + public void onDownloadStart(String url, String userAgent, + String contentDisposition, + String mimeType, long contentLength) + { + String guessedMimeType = URLConnection.guessContentTypeFromName(url); + DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); + request.setMimeType(guessedMimeType); + String cookies = CookieManager.getInstance().getCookie(url); + request.addRequestHeader("cookie", cookies); + request.addRequestHeader("User-Agent", userAgent); + request.setDescription("Downloading..."); + request.setTitle(URLUtil.guessFileName(url, contentDisposition, guessedMimeType)); + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, + contentDisposition, guessedMimeType)); + Context context = TiApplication.getInstance().getApplicationContext(); + DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); + dm.enqueue(request); + KrollDict kd = new KrollDict(); + kd.put("status", "started"); + kd.put("url", url); + kd.put("target", Environment.DIRECTORY_DOWNLOADS); + fireEvent("download", kd); + } + }); + setNativeView(webView); }