Merge pull request #11250 from nextcloud/bugfix/11215

fix(EditorWebView): Move local image upload to the editor web view
This commit is contained in:
Álvaro Brey 2023-01-17 10:34:56 +01:00 committed by GitHub
commit d93634aac8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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) {