Skip to content

Commit 966221e

Browse files
robxyyTitozzz
andauthored
feat(android): Add support for the capture attribute (react-native-webview#2954)
Co-authored-by: Thibault Malbranche <thibault@brigad.co>
1 parent 0a5bbe7 commit 966221e

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

android/src/main/java/com/reactnativecommunity/webview/RNCWebChromeClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathC
311311
String[] acceptTypes = fileChooserParams.getAcceptTypes();
312312
boolean allowMultiple = fileChooserParams.getMode() == WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE;
313313

314-
return this.mWebView.getThemedReactContext().getNativeModule(RNCWebViewModule.class).startPhotoPickerIntent(filePathCallback, acceptTypes, allowMultiple);
314+
return this.mWebView.getThemedReactContext().getNativeModule(RNCWebViewModule.class).startPhotoPickerIntent(filePathCallback, acceptTypes, allowMultiple, fileChooserParams.isCaptureEnabled());
315315
}
316316

317317
@Override

android/src/main/java/com/reactnativecommunity/webview/RNCWebViewModuleImpl.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,15 @@ public void startPhotoPickerIntent(String acceptType, ValueCallback<Uri> callbac
262262
}
263263
}
264264

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

269269
ArrayList<Parcelable> extraIntents = new ArrayList<>();
270+
Intent photoIntent = null;
270271
if (!needsCameraPermission()) {
271272
if (acceptsImages(acceptTypes)) {
272-
Intent photoIntent = getPhotoIntent();
273+
photoIntent = getPhotoIntent();
273274
if (photoIntent != null) {
274275
extraIntents.add(photoIntent);
275276
}
@@ -282,16 +283,24 @@ public boolean startPhotoPickerIntent(final String[] acceptTypes, final boolean
282283
}
283284
}
284285

285-
Intent fileSelectionIntent = getFileChooserIntent(acceptTypes, allowMultiple);
286-
287286
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
288-
chooserIntent.putExtra(Intent.EXTRA_INTENT, fileSelectionIntent);
289-
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents.toArray(new Parcelable[]{}));
287+
if (isCaptureEnabled) {
288+
chooserIntent = photoIntent;
289+
} else {
290+
Intent fileSelectionIntent = getFileChooserIntent(acceptTypes, allowMultiple);
290291

291-
if (chooserIntent.resolveActivity(activity.getPackageManager()) != null) {
292-
activity.startActivityForResult(chooserIntent, PICKER);
292+
chooserIntent.putExtra(Intent.EXTRA_INTENT, fileSelectionIntent);
293+
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents.toArray(new Parcelable[]{}));
294+
}
295+
296+
if (chooserIntent != null) {
297+
if (chooserIntent.resolveActivity(activity.getPackageManager()) != null) {
298+
activity.startActivityForResult(chooserIntent, PICKER);
299+
} else {
300+
Log.w("RNCWebViewModule", "there is no Activity to handle this Intent");
301+
}
293302
} else {
294-
Log.w("RNCWebViewModule", "there is no Activity to handle this Intent");
303+
Log.w("RNCWebViewModule", "there is no Camera permission");
295304
}
296305

297306
return true;

android/src/newarch/com/reactnativecommunity/webview/RNCWebViewModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public void startPhotoPickerIntent(ValueCallback<Uri> filePathCallback, String a
3333
mRNCWebViewModuleImpl.startPhotoPickerIntent(acceptType, filePathCallback);
3434
}
3535

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

4040
public void setDownloadRequest(DownloadManager.Request request) {

android/src/oldarch/com/reactnativecommunity/webview/RNCWebViewModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public void startPhotoPickerIntent(ValueCallback<Uri> filePathCallback, String a
3535
mRNCWebViewModuleImpl.startPhotoPickerIntent(acceptType, filePathCallback);
3636
}
3737

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

4242
public void setDownloadRequest(DownloadManager.Request request) {

0 commit comments

Comments
 (0)