From 23d3dc22b5d3653a00573aeb4fb3d0a9a16d5a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Fri, 13 Jan 2023 21:30:10 +0100 Subject: [PATCH] fix(EditorWebView): Move local image upload to the editor web view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes sure that direct editing apps like text can also use regular upload image methods through file input elements in the webview fixes #11215 fixes #10744 Signed-off-by: Julius Härtl --- .../android/ui/activity/EditorWebView.java | 69 +++++++++++++++++++ .../activity/RichDocumentsEditorWebView.java | 61 +--------------- 2 files changed, 71 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/ui/activity/EditorWebView.java b/app/src/main/java/com/owncloud/android/ui/activity/EditorWebView.java index d7c0e88702..cf7cb07085 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/EditorWebView.java +++ b/app/src/main/java/com/owncloud/android/ui/activity/EditorWebView.java @@ -23,6 +23,7 @@ package com.owncloud.android.ui.activity; import android.app.DownloadManager; +import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; @@ -31,6 +32,8 @@ import android.net.Uri; import android.os.Handler; import android.view.View; import android.webkit.JavascriptInterface; +import android.webkit.ValueCallback; +import android.webkit.WebChromeClient; import android.webkit.WebView; import android.widget.Toast; @@ -45,6 +48,8 @@ import com.owncloud.android.utils.DisplayUtils; import com.owncloud.android.utils.MimeTypeUtil; public abstract class EditorWebView extends ExternalSiteWebView { + public static final int REQUEST_LOCAL_FILE = 101; + public ValueCallback uploadMessage; protected Snackbar loadingSnackbar; protected String fileName; @@ -104,6 +109,34 @@ public abstract class EditorWebView extends ExternalSiteWebView { protected void postOnCreate() { super.postOnCreate(); + getWebView().setWebChromeClient(new WebChromeClient() { + EditorWebView activity = EditorWebView.this; + + @Override + public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, + FileChooserParams fileChooserParams) { + if (uploadMessage != null) { + uploadMessage.onReceiveValue(null); + uploadMessage = null; + } + + activity.uploadMessage = filePathCallback; + + Intent intent = fileChooserParams.createIntent(); + intent.setType("image/*"); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); + try { + activity.startActivityForResult(intent, REQUEST_LOCAL_FILE); + } catch (ActivityNotFoundException e) { + uploadMessage = null; + Toast.makeText(getBaseContext(), "Cannot open file chooser", Toast.LENGTH_LONG).show(); + return false; + } + + return true; + } + }); + setFile(getIntent().getParcelableExtra(ExternalSiteWebView.EXTRA_FILE)); if (getFile() == null) { @@ -124,6 +157,42 @@ public abstract class EditorWebView extends ExternalSiteWebView { initLoadingScreen(user.get()); } + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (RESULT_OK != resultCode) { + if (requestCode == REQUEST_LOCAL_FILE) { + this.uploadMessage.onReceiveValue(null); + this.uploadMessage = null; + } + return; + } + + handleActivityResult(requestCode, resultCode, data); + + super.onActivityResult(requestCode, resultCode, data); + } + + protected void handleActivityResult(int requestCode, int resultCode, Intent data) { + switch (requestCode) { + case REQUEST_LOCAL_FILE: + handleLocalFile(data, resultCode); + break; + + default: + // unexpected, do nothing + break; + } + } + + protected void handleLocalFile(Intent data, int resultCode) { + if (uploadMessage == null) { + return; + } + + uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data)); + uploadMessage = null; + } + protected WebView getWebView() { return binding.webView; } diff --git a/app/src/main/java/com/owncloud/android/ui/activity/RichDocumentsEditorWebView.java b/app/src/main/java/com/owncloud/android/ui/activity/RichDocumentsEditorWebView.java index b4c12faee2..ecd03151d1 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/RichDocumentsEditorWebView.java +++ b/app/src/main/java/com/owncloud/android/ui/activity/RichDocumentsEditorWebView.java @@ -24,7 +24,6 @@ package com.owncloud.android.ui.activity; -import android.content.ActivityNotFoundException; import android.content.Intent; import android.net.Uri; import android.os.Build; @@ -32,10 +31,6 @@ import android.os.Bundle; import android.text.TextUtils; import android.view.KeyEvent; import android.webkit.JavascriptInterface; -import android.webkit.ValueCallback; -import android.webkit.WebChromeClient; -import android.webkit.WebView; -import android.widget.Toast; import com.nextcloud.client.account.CurrentAccountProvider; import com.nextcloud.client.account.User; @@ -67,7 +62,6 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; * Opens document for editing via Richdocuments app in a web view */ public class RichDocumentsEditorWebView extends EditorWebView { - public static final int REQUEST_LOCAL_FILE = 101; private static final int REQUEST_REMOTE_FILE = 100; private static final String URL = "URL"; private static final String HYPERLINK = "Url"; @@ -76,8 +70,6 @@ public class RichDocumentsEditorWebView extends EditorWebView { private static final String SLIDESHOW = "slideshow"; private static final String NEW_NAME = "NewName"; - public ValueCallback uploadMessage; - @Inject protected CurrentAccountProvider currentAccountProvider; @@ -91,34 +83,6 @@ public class RichDocumentsEditorWebView extends EditorWebView { getWebView().addJavascriptInterface(new RichDocumentsMobileInterface(), "RichDocumentsMobileInterface"); - getWebView().setWebChromeClient(new WebChromeClient() { - RichDocumentsEditorWebView activity = RichDocumentsEditorWebView.this; - - @Override - public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, - FileChooserParams fileChooserParams) { - if (uploadMessage != null) { - uploadMessage.onReceiveValue(null); - uploadMessage = null; - } - - activity.uploadMessage = filePathCallback; - - Intent intent = fileChooserParams.createIntent(); - intent.setType("image/*"); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); - try { - activity.startActivityForResult(intent, REQUEST_LOCAL_FILE); - } catch (ActivityNotFoundException e) { - uploadMessage = null; - Toast.makeText(getBaseContext(), "Cannot open file chooser", Toast.LENGTH_LONG).show(); - return false; - } - - return true; - } - }); - // load url in background loadUrl(getIntent().getStringExtra(EXTRA_URL)); } @@ -135,20 +99,8 @@ public class RichDocumentsEditorWebView extends EditorWebView { } @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (RESULT_OK != resultCode) { - if (requestCode == REQUEST_LOCAL_FILE) { - this.uploadMessage.onReceiveValue(null); - this.uploadMessage = null; - } - return; - } - + protected void handleActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { - case REQUEST_LOCAL_FILE: - handleLocalFile(data, resultCode); - break; - case REQUEST_REMOTE_FILE: handleRemoteFile(data); break; @@ -158,16 +110,7 @@ public class RichDocumentsEditorWebView extends EditorWebView { break; } - super.onActivityResult(requestCode, resultCode, data); - } - - private void handleLocalFile(Intent data, int resultCode) { - if (uploadMessage == null) { - return; - } - - uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data)); - uploadMessage = null; + super.handleActivityResult(requestCode, resultCode, data); } private void handleRemoteFile(Intent data) {