feat(Android): Support Android file upload (#60)

Fixes #33 

I could really use some help from an Android developer on this one, because I just "made it work", don't know how to "make it work good".

Some things that should be reviewed:

- [ ] validate Android 5.0 devices (my emulator work, but outputs some weird sounds; a Galaxy 4 I tested on crashes)
- [ ] validate Android 5.1 devices (emulator works, couldn't find a real device)
- [ ] how to handle File Extensions? (https://www.w3schools.com/tags/att_input_accept.asp)

I'm sure that there's more refactoring to be done, so any help and advice would be appreciated.
This commit is contained in:
Andrei Pfeiffer
2018-11-21 11:46:43 +01:00
committed by Thibault Malbranche
parent f69942d70a
commit 752a5b295a
9 changed files with 438 additions and 9 deletions
@@ -85,6 +85,7 @@ import org.json.JSONObject;
public class RNCWebViewManager extends SimpleViewManager<WebView> {
protected static final String REACT_CLASS = "RNCWebView";
private RNCWebViewPackage aPackage;
protected static final String HTML_ENCODING = "UTF-8";
protected static final String HTML_MIME_TYPE = "text/html";
@@ -427,6 +428,25 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
protected void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType) {
getModule().startPhotoPickerIntent(filePathCallback, acceptType);
}
protected void openFileChooser(ValueCallback<Uri> filePathCallback) {
getModule().startPhotoPickerIntent(filePathCallback, "");
}
protected void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) {
getModule().startPhotoPickerIntent(filePathCallback, acceptType);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
String[] acceptTypes = fileChooserParams.getAcceptTypes();
boolean allowMultiple = fileChooserParams.getMode() == WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE;
Intent intent = fileChooserParams.createIntent();
return getModule().startPhotoPickerIntent(filePathCallback, intent, acceptTypes, allowMultiple);
}
});
reactContext.addLifecycleEventListener(webView);
mWebViewConfig.configWebView(webView);
@@ -747,4 +767,16 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
eventDispatcher.dispatchEvent(event);
}
public RNCWebViewPackage getPackage() {
return this.aPackage;
}
public void setPackage(RNCWebViewPackage aPackage) {
this.aPackage = aPackage;
}
public RNCWebViewModule getModule() {
return this.aPackage.getModule();
}
}