Skip to content

Commit

Permalink
feat(android): Add support for the capture attribute (react-native-…
Browse files Browse the repository at this point in the history
…webview#2954)

Co-authored-by: Thibault Malbranche <thibault@brigad.co>
  • Loading branch information
robxyy and Titozzz committed Jun 11, 2023
1 parent 0a5bbe7 commit 966221e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathC
String[] acceptTypes = fileChooserParams.getAcceptTypes();
boolean allowMultiple = fileChooserParams.getMode() == WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE;

return this.mWebView.getThemedReactContext().getNativeModule(RNCWebViewModule.class).startPhotoPickerIntent(filePathCallback, acceptTypes, allowMultiple);
return this.mWebView.getThemedReactContext().getNativeModule(RNCWebViewModule.class).startPhotoPickerIntent(filePathCallback, acceptTypes, allowMultiple, fileChooserParams.isCaptureEnabled());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,15 @@ public void startPhotoPickerIntent(String acceptType, ValueCallback<Uri> callbac
}
}

public boolean startPhotoPickerIntent(final String[] acceptTypes, final boolean allowMultiple, ValueCallback<Uri[]> callback) {
public boolean startPhotoPickerIntent(final String[] acceptTypes, final boolean allowMultiple, final ValueCallback<Uri[]> callback, final boolean isCaptureEnabled) {
mFilePathCallback = callback;
Activity activity = mContext.getCurrentActivity();

ArrayList<Parcelable> extraIntents = new ArrayList<>();
Intent photoIntent = null;
if (!needsCameraPermission()) {
if (acceptsImages(acceptTypes)) {
Intent photoIntent = getPhotoIntent();
photoIntent = getPhotoIntent();
if (photoIntent != null) {
extraIntents.add(photoIntent);
}
Expand All @@ -282,16 +283,24 @@ public boolean startPhotoPickerIntent(final String[] acceptTypes, final boolean
}
}

Intent fileSelectionIntent = getFileChooserIntent(acceptTypes, allowMultiple);

Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, fileSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents.toArray(new Parcelable[]{}));
if (isCaptureEnabled) {
chooserIntent = photoIntent;
} else {
Intent fileSelectionIntent = getFileChooserIntent(acceptTypes, allowMultiple);

if (chooserIntent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivityForResult(chooserIntent, PICKER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, fileSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents.toArray(new Parcelable[]{}));
}

if (chooserIntent != null) {
if (chooserIntent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivityForResult(chooserIntent, PICKER);
} else {
Log.w("RNCWebViewModule", "there is no Activity to handle this Intent");
}
} else {
Log.w("RNCWebViewModule", "there is no Activity to handle this Intent");
Log.w("RNCWebViewModule", "there is no Camera permission");
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public void startPhotoPickerIntent(ValueCallback<Uri> filePathCallback, String a
mRNCWebViewModuleImpl.startPhotoPickerIntent(acceptType, filePathCallback);
}

public boolean startPhotoPickerIntent(final ValueCallback<Uri[]> callback, final String[] acceptTypes, final boolean allowMultiple) {
return mRNCWebViewModuleImpl.startPhotoPickerIntent(acceptTypes, allowMultiple, callback);
public boolean startPhotoPickerIntent(final ValueCallback<Uri[]> callback, final String[] acceptTypes, final boolean allowMultiple, final boolean isCaptureEnabled) {
return mRNCWebViewModuleImpl.startPhotoPickerIntent(acceptTypes, allowMultiple, callback, isCaptureEnabled);
}

public void setDownloadRequest(DownloadManager.Request request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public void startPhotoPickerIntent(ValueCallback<Uri> filePathCallback, String a
mRNCWebViewModuleImpl.startPhotoPickerIntent(acceptType, filePathCallback);
}

public boolean startPhotoPickerIntent(final ValueCallback<Uri[]> callback, final String[] acceptTypes, final boolean allowMultiple) {
return mRNCWebViewModuleImpl.startPhotoPickerIntent(acceptTypes, allowMultiple, callback);
public boolean startPhotoPickerIntent(final ValueCallback<Uri[]> callback, final String[] acceptTypes, final boolean allowMultiple, final boolean isCaptureEnabled) {
return mRNCWebViewModuleImpl.startPhotoPickerIntent(acceptTypes, allowMultiple, callback, isCaptureEnabled);
}

public void setDownloadRequest(DownloadManager.Request request) {
Expand Down

0 comments on commit 966221e

Please sign in to comment.