Skip to content

Commit

Permalink
allow file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobar79 committed Aug 20, 2019
1 parent 7f58566 commit 0e4ee04
Show file tree
Hide file tree
Showing 8 changed files with 485 additions and 6 deletions.
8 changes: 7 additions & 1 deletion Web3Webview.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
StyleSheet,
UIManager,
View,
findNodeHandle
findNodeHandle,
NativeModules
} from "react-native";

const styles = StyleSheet.create({
Expand Down Expand Up @@ -260,6 +261,11 @@ export default class WebView extends React.Component {
openNewWindowInWebView: true
};

static isFileUploadSupported = async () => {
// native implementation should return "true" only for Android 5+
return NativeModules.Web3Webview.isFileUploadSupported();
}

state = {
viewState: WebViewState.IDLE,
lastErrorEvent: null,
Expand Down
6 changes: 6 additions & 0 deletions Web3Webview.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ class Web3Webview extends React.Component {
openNewWindowInWebView: true
};

static isFileUploadSupported = async () => {
// no native implementation for iOS, depends only on permissions
return true;
}


UNSAFE_componentWillMount() {
if (this.props.startInLoadingState) {
this.setState({ viewState: WebViewState.LOADING });
Expand Down
12 changes: 11 additions & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.web3webview">

<application>
<provider
android:name=".Web3WebviewFileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
</application>
</manifest>
13 changes: 13 additions & 0 deletions android/src/main/java/com/web3webview/Web3WebviewFileProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.web3webview;
import androidx.core.content.FileProvider;

/**
* Providing a custom {@code FileProvider} prevents manifest {@code <provider>} name collisions.
* <p>
* See https://developer.android.com/guide/topics/manifest/provider-element.html for details.
*/
public class Web3WebviewFileProvider extends FileProvider {

// This class intentionally left blank.

}
30 changes: 29 additions & 1 deletion android/src/main/java/com/web3webview/Web3WebviewManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.webkit.JavascriptInterface;
import android.webkit.ServiceWorkerClient;
import android.webkit.ServiceWorkerController;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
Expand Down Expand Up @@ -530,7 +531,7 @@ protected Web3Webview createWeb3WebviewInstance(ThemedReactContext reactContext)

@Override
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected WebView createViewInstance(ThemedReactContext reactContext) {
protected WebView createViewInstance(final ThemedReactContext reactContext) {
final Web3Webview webView = createWeb3WebviewInstance(reactContext);


Expand All @@ -555,6 +556,29 @@ public void onProgressChanged(WebView view, int progress) {
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}

protected void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType) {
getModule(reactContext).startPhotoPickerIntent(filePathCallback, acceptType);
}

protected void openFileChooser(ValueCallback<Uri> filePathCallback) {
getModule(reactContext).startPhotoPickerIntent(filePathCallback, "");
}

protected void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) {
getModule(reactContext).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(reactContext).startPhotoPickerIntent(filePathCallback, intent, acceptTypes, allowMultiple);
}


});
reactContext.addLifecycleEventListener(webView);
mWebViewConfig.configWebView(webView);
Expand Down Expand Up @@ -843,6 +867,10 @@ public void onDropViewInstance(WebView webView) {
w.cleanupCallbacksAndDestroy();
}

public static Web3WebviewModule getModule(ReactContext reactContext) {
return reactContext.getNativeModule(Web3WebviewModule.class);
}

protected WebView.PictureListener getPictureListener() {
if (mPictureListener == null) {
mPictureListener = new WebView.PictureListener() {
Expand Down
Loading

0 comments on commit 0e4ee04

Please sign in to comment.