fix(EditorWebView): Move local image upload to the editor web view

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 <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2023-01-13 21:30:10 +01:00
parent bbedbc973a
commit 23d3dc22b5
No known key found for this signature in database
GPG key ID: 4C614C6ED2CDE6DF
2 changed files with 71 additions and 59 deletions

View file

@ -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<Uri[]> 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<Uri[]> 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;
}

View file

@ -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<Uri[]> 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<Uri[]> 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) {