@exodus/react-native-webview 11.26.1-exodus.9 → 13.16.0-exodus.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -63
- package/android/build.gradle +83 -110
- package/android/gradle.properties +3 -4
- package/android/src/main/AndroidManifest.xml +12 -0
- package/android/src/main/AndroidManifestNew.xml +26 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCBasicAuthCredential.java +11 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebChromeClient.java +407 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java +468 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewClient.java +330 -0
- package/android/src/main/java/com/reactnativecommunity/webview/{WebViewConfig.java → RNCWebViewConfig.java} +3 -4
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewFileProvider.java +1 -1
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt +746 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewMessagingModule.kt +9 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewModuleImpl.java +554 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewPackage.java +57 -12
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewWrapper.kt +39 -0
- package/android/src/main/java/com/reactnativecommunity/webview/events/SubResourceErrorEvent.kt +25 -0
- package/android/src/main/java/com/reactnativecommunity/webview/events/TopCustomMenuSelectionEvent.kt +24 -0
- package/android/src/main/java/com/reactnativecommunity/webview/events/TopHttpErrorEvent.kt +25 -0
- package/android/src/main/java/com/reactnativecommunity/webview/events/TopNewWindowEvent.kt +25 -0
- package/android/src/main/java/com/reactnativecommunity/webview/events/TopRenderProcessGoneEvent.kt +25 -0
- package/android/src/newarch/com/reactnativecommunity/webview/RNCWebViewManager.java +570 -0
- package/android/src/newarch/com/reactnativecommunity/webview/RNCWebViewModule.java +57 -0
- package/android/src/oldarch/com/reactnativecommunity/webview/RNCWebViewManager.java +341 -0
- package/android/src/oldarch/com/reactnativecommunity/webview/RNCWebViewModule.java +59 -0
- package/apple/RCTConvert+WKDataDetectorTypes.h +11 -0
- package/apple/RCTConvert+WKDataDetectorTypes.m +27 -0
- package/apple/RNCWebView.h +26 -100
- package/apple/RNCWebView.mm +555 -0
- package/apple/RNCWebViewDecisionManager.h +20 -0
- package/apple/RNCWebViewDecisionManager.m +47 -0
- package/apple/RNCWebViewImpl.h +164 -0
- package/apple/{RNCWebView.m → RNCWebViewImpl.m} +802 -225
- package/apple/RNCWebViewManager.h +4 -8
- package/apple/RNCWebViewManager.mm +221 -0
- package/apple/RNCWebViewModule.h +23 -0
- package/apple/RNCWebViewModule.mm +34 -0
- package/index.d.ts +2 -3
- package/lib/NativeRNCWebViewModule.d.ts +8 -0
- package/lib/NativeRNCWebViewModule.js +1 -0
- package/lib/RNCWebViewNativeComponent.d.ts +245 -0
- package/lib/RNCWebViewNativeComponent.js +1 -0
- package/lib/WebView.android.d.ts +0 -1
- package/lib/WebView.android.js +1 -135
- package/lib/WebView.d.ts +2 -3
- package/lib/WebView.ios.d.ts +0 -1
- package/lib/WebView.ios.js +1 -114
- package/lib/WebView.js +1 -11
- package/lib/WebView.macos.d.ts +6 -0
- package/lib/WebView.macos.js +1 -0
- package/lib/WebView.styles.d.ts +37 -11
- package/lib/WebView.styles.js +1 -33
- package/lib/WebView.windows.d.ts +17 -0
- package/lib/WebView.windows.js +1 -0
- package/lib/WebViewNativeComponent.macos.d.ts +3 -0
- package/lib/WebViewNativeComponent.macos.js +1 -0
- package/lib/WebViewNativeComponent.windows.d.ts +3 -0
- package/lib/WebViewNativeComponent.windows.js +1 -0
- package/lib/WebViewShared.d.ts +30 -9
- package/lib/WebViewShared.js +1 -174
- package/lib/WebViewTypes.d.ts +514 -98
- package/lib/WebViewTypes.js +1 -6
- package/lib/index.d.ts +0 -1
- package/lib/index.js +1 -3
- package/lib/validation.d.ts +3 -0
- package/lib/validation.js +1 -0
- package/package.json +57 -33
- package/react-native-webview.podspec +32 -5
- package/react-native.config.js +22 -18
- package/src/NativeRNCWebViewModule.ts +13 -0
- package/src/RNCWebViewNativeComponent.ts +348 -0
- package/src/WebView.android.tsx +345 -0
- package/src/WebView.ios.tsx +341 -0
- package/src/WebView.macos.tsx +252 -0
- package/src/WebView.styles.ts +41 -0
- package/src/WebView.tsx +25 -0
- package/src/WebView.windows.tsx +217 -0
- package/src/WebViewNativeComponent.macos.ts +7 -0
- package/src/WebViewNativeComponent.windows.ts +8 -0
- package/src/WebViewShared.tsx +476 -0
- package/src/WebViewTypes.ts +1402 -0
- package/src/__tests__/WebViewShared-test.js +323 -0
- package/src/__tests__/__snapshots__/WebViewShared-test.js.snap +8 -0
- package/src/__tests__/validation-test.js +38 -0
- package/src/index.ts +4 -0
- package/src/validation.ts +20 -0
- package/android/.editorconfig +0 -6
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java +0 -1408
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewModule.java +0 -506
- package/apple/RNCWebViewManager.m +0 -278
- package/lib/WebViewNativeComponent.android.d.ts +0 -4
- package/lib/WebViewNativeComponent.android.js +0 -3
- package/lib/WebViewNativeComponent.ios.d.ts +0 -4
- package/lib/WebViewNativeComponent.ios.js +0 -3
|
@@ -1,506 +0,0 @@
|
|
|
1
|
-
package com.reactnativecommunity.webview;
|
|
2
|
-
|
|
3
|
-
import android.Manifest;
|
|
4
|
-
import android.app.Activity;
|
|
5
|
-
import android.app.DownloadManager;
|
|
6
|
-
import android.content.Context;
|
|
7
|
-
import android.content.Intent;
|
|
8
|
-
import android.content.pm.PackageManager;
|
|
9
|
-
import android.net.Uri;
|
|
10
|
-
import android.os.Build;
|
|
11
|
-
import android.os.Environment;
|
|
12
|
-
import android.os.Parcelable;
|
|
13
|
-
import android.provider.MediaStore;
|
|
14
|
-
|
|
15
|
-
import androidx.annotation.Nullable;
|
|
16
|
-
import androidx.annotation.RequiresApi;
|
|
17
|
-
import androidx.core.content.ContextCompat;
|
|
18
|
-
import androidx.core.content.FileProvider;
|
|
19
|
-
import androidx.core.util.Pair;
|
|
20
|
-
|
|
21
|
-
import android.util.Log;
|
|
22
|
-
import android.webkit.MimeTypeMap;
|
|
23
|
-
import android.webkit.ValueCallback;
|
|
24
|
-
import android.webkit.WebChromeClient;
|
|
25
|
-
import android.widget.Toast;
|
|
26
|
-
|
|
27
|
-
import com.facebook.react.bridge.ActivityEventListener;
|
|
28
|
-
import com.facebook.react.bridge.Promise;
|
|
29
|
-
import com.facebook.react.bridge.ReactApplicationContext;
|
|
30
|
-
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
31
|
-
import com.facebook.react.bridge.ReactMethod;
|
|
32
|
-
import com.facebook.react.module.annotations.ReactModule;
|
|
33
|
-
import com.facebook.react.modules.core.PermissionAwareActivity;
|
|
34
|
-
import com.facebook.react.modules.core.PermissionListener;
|
|
35
|
-
|
|
36
|
-
import java.io.File;
|
|
37
|
-
import java.io.IOException;
|
|
38
|
-
import java.util.ArrayList;
|
|
39
|
-
import java.util.Arrays;
|
|
40
|
-
import java.util.HashMap;
|
|
41
|
-
import java.util.concurrent.atomic.AtomicReference;
|
|
42
|
-
|
|
43
|
-
import static android.app.Activity.RESULT_OK;
|
|
44
|
-
|
|
45
|
-
@ReactModule(name = RNCWebViewModule.MODULE_NAME)
|
|
46
|
-
public class RNCWebViewModule extends ReactContextBaseJavaModule implements ActivityEventListener {
|
|
47
|
-
public static final String MODULE_NAME = "RNCWebView";
|
|
48
|
-
private static final int PICKER = 1;
|
|
49
|
-
private static final int PICKER_LEGACY = 3;
|
|
50
|
-
private static final int FILE_DOWNLOAD_PERMISSION_REQUEST = 1;
|
|
51
|
-
private ValueCallback<Uri> filePathCallbackLegacy;
|
|
52
|
-
private ValueCallback<Uri[]> filePathCallback;
|
|
53
|
-
private File outputImage;
|
|
54
|
-
private File outputVideo;
|
|
55
|
-
private DownloadManager.Request downloadRequest;
|
|
56
|
-
|
|
57
|
-
protected static class ShouldOverrideUrlLoadingLock {
|
|
58
|
-
protected enum ShouldOverrideCallbackState {
|
|
59
|
-
UNDECIDED,
|
|
60
|
-
SHOULD_OVERRIDE,
|
|
61
|
-
DO_NOT_OVERRIDE,
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
private int nextLockIdentifier = 1;
|
|
65
|
-
private final HashMap<Integer, AtomicReference<ShouldOverrideCallbackState>> shouldOverrideLocks = new HashMap<>();
|
|
66
|
-
|
|
67
|
-
public synchronized Pair<Integer, AtomicReference<ShouldOverrideCallbackState>> getNewLock() {
|
|
68
|
-
final int lockIdentifier = nextLockIdentifier++;
|
|
69
|
-
final AtomicReference<ShouldOverrideCallbackState> shouldOverride = new AtomicReference<>(ShouldOverrideCallbackState.UNDECIDED);
|
|
70
|
-
shouldOverrideLocks.put(lockIdentifier, shouldOverride);
|
|
71
|
-
return new Pair<>(lockIdentifier, shouldOverride);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
@Nullable
|
|
75
|
-
public synchronized AtomicReference<ShouldOverrideCallbackState> getLock(Integer lockIdentifier) {
|
|
76
|
-
return shouldOverrideLocks.get(lockIdentifier);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public synchronized void removeLock(Integer lockIdentifier) {
|
|
80
|
-
shouldOverrideLocks.remove(lockIdentifier);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
protected static final ShouldOverrideUrlLoadingLock shouldOverrideUrlLoadingLock = new ShouldOverrideUrlLoadingLock();
|
|
85
|
-
|
|
86
|
-
private enum MimeType {
|
|
87
|
-
DEFAULT("*/*"),
|
|
88
|
-
IMAGE("image"),
|
|
89
|
-
VIDEO("video");
|
|
90
|
-
|
|
91
|
-
private final String value;
|
|
92
|
-
|
|
93
|
-
MimeType(String value) {
|
|
94
|
-
this.value = value;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
public RNCWebViewModule(ReactApplicationContext reactContext) {
|
|
99
|
-
super(reactContext);
|
|
100
|
-
reactContext.addActivityEventListener(this);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
@Override
|
|
104
|
-
public String getName() {
|
|
105
|
-
return MODULE_NAME;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
@ReactMethod
|
|
109
|
-
public void isFileUploadSupported(final Promise promise) {
|
|
110
|
-
Boolean result = false;
|
|
111
|
-
promise.resolve(result);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
@ReactMethod(isBlockingSynchronousMethod = true)
|
|
115
|
-
public void onShouldStartLoadWithRequestCallback(final boolean shouldStart, final int lockIdentifier) {
|
|
116
|
-
final AtomicReference<ShouldOverrideUrlLoadingLock.ShouldOverrideCallbackState> lockObject = shouldOverrideUrlLoadingLock.getLock(lockIdentifier);
|
|
117
|
-
if (lockObject != null) {
|
|
118
|
-
synchronized (lockObject) {
|
|
119
|
-
lockObject.set(shouldStart ? ShouldOverrideUrlLoadingLock.ShouldOverrideCallbackState.DO_NOT_OVERRIDE : ShouldOverrideUrlLoadingLock.ShouldOverrideCallbackState.SHOULD_OVERRIDE);
|
|
120
|
-
lockObject.notify();
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
|
|
126
|
-
|
|
127
|
-
if (filePathCallback == null && filePathCallbackLegacy == null) {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
boolean imageTaken = false;
|
|
132
|
-
boolean videoTaken = false;
|
|
133
|
-
|
|
134
|
-
if (outputImage != null && outputImage.length() > 0) {
|
|
135
|
-
imageTaken = true;
|
|
136
|
-
}
|
|
137
|
-
if (outputVideo != null && outputVideo.length() > 0) {
|
|
138
|
-
videoTaken = true;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// based off of which button was pressed, we get an activity result and a file
|
|
142
|
-
// the camera activity doesn't properly return the filename* (I think?) so we use
|
|
143
|
-
// this filename instead
|
|
144
|
-
switch (requestCode) {
|
|
145
|
-
case PICKER:
|
|
146
|
-
if (resultCode != RESULT_OK) {
|
|
147
|
-
if (filePathCallback != null) {
|
|
148
|
-
filePathCallback.onReceiveValue(null);
|
|
149
|
-
}
|
|
150
|
-
} else {
|
|
151
|
-
if (imageTaken) {
|
|
152
|
-
filePathCallback.onReceiveValue(new Uri[]{getOutputUri(outputImage)});
|
|
153
|
-
} else if (videoTaken) {
|
|
154
|
-
filePathCallback.onReceiveValue(new Uri[]{getOutputUri(outputVideo)});
|
|
155
|
-
} else {
|
|
156
|
-
filePathCallback.onReceiveValue(this.getSelectedFiles(data, resultCode));
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
break;
|
|
160
|
-
case PICKER_LEGACY:
|
|
161
|
-
if (resultCode != RESULT_OK) {
|
|
162
|
-
filePathCallbackLegacy.onReceiveValue(null);
|
|
163
|
-
} else {
|
|
164
|
-
if (imageTaken) {
|
|
165
|
-
filePathCallbackLegacy.onReceiveValue(getOutputUri(outputImage));
|
|
166
|
-
} else if (videoTaken) {
|
|
167
|
-
filePathCallbackLegacy.onReceiveValue(getOutputUri(outputVideo));
|
|
168
|
-
} else {
|
|
169
|
-
filePathCallbackLegacy.onReceiveValue(data.getData());
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
break;
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (outputImage != null && !imageTaken) {
|
|
177
|
-
outputImage.delete();
|
|
178
|
-
}
|
|
179
|
-
if (outputVideo != null && !videoTaken) {
|
|
180
|
-
outputVideo.delete();
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
filePathCallback = null;
|
|
184
|
-
filePathCallbackLegacy = null;
|
|
185
|
-
outputImage = null;
|
|
186
|
-
outputVideo = null;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
public void onNewIntent(Intent intent) {
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
private Uri[] getSelectedFiles(Intent data, int resultCode) {
|
|
193
|
-
if (data == null) {
|
|
194
|
-
return null;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// we have multiple files selected
|
|
198
|
-
if (data.getClipData() != null) {
|
|
199
|
-
final int numSelectedFiles = data.getClipData().getItemCount();
|
|
200
|
-
Uri[] result = new Uri[numSelectedFiles];
|
|
201
|
-
for (int i = 0; i < numSelectedFiles; i++) {
|
|
202
|
-
result[i] = data.getClipData().getItemAt(i).getUri();
|
|
203
|
-
}
|
|
204
|
-
return result;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// we have one file selected
|
|
208
|
-
if (data.getData() != null && resultCode == RESULT_OK) {
|
|
209
|
-
return WebChromeClient.FileChooserParams.parseResult(resultCode, data);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
return null;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
public void startPhotoPickerIntent(ValueCallback<Uri> filePathCallback, String acceptType) {
|
|
216
|
-
filePathCallbackLegacy = filePathCallback;
|
|
217
|
-
|
|
218
|
-
Intent fileChooserIntent = getFileChooserIntent(acceptType);
|
|
219
|
-
Intent chooserIntent = Intent.createChooser(fileChooserIntent, "");
|
|
220
|
-
|
|
221
|
-
ArrayList<Parcelable> extraIntents = new ArrayList<>();
|
|
222
|
-
if (acceptsImages(acceptType)) {
|
|
223
|
-
Intent photoIntent = getPhotoIntent();
|
|
224
|
-
if (photoIntent != null) {
|
|
225
|
-
extraIntents.add(photoIntent);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
if (acceptsVideo(acceptType)) {
|
|
229
|
-
Intent videoIntent = getVideoIntent();
|
|
230
|
-
if (videoIntent != null) {
|
|
231
|
-
extraIntents.add(videoIntent);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents.toArray(new Parcelable[]{}));
|
|
235
|
-
|
|
236
|
-
if (chooserIntent.resolveActivity(getCurrentActivity().getPackageManager()) != null) {
|
|
237
|
-
getCurrentActivity().startActivityForResult(chooserIntent, PICKER_LEGACY);
|
|
238
|
-
} else {
|
|
239
|
-
Log.w("RNCWebViewModule", "there is no Activity to handle this Intent");
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
244
|
-
public boolean startPhotoPickerIntent(final ValueCallback<Uri[]> callback, final String[] acceptTypes, final boolean allowMultiple) {
|
|
245
|
-
filePathCallback = callback;
|
|
246
|
-
|
|
247
|
-
ArrayList<Parcelable> extraIntents = new ArrayList<>();
|
|
248
|
-
if (!needsCameraPermission()) {
|
|
249
|
-
if (acceptsImages(acceptTypes)) {
|
|
250
|
-
Intent photoIntent = getPhotoIntent();
|
|
251
|
-
if (photoIntent != null) {
|
|
252
|
-
extraIntents.add(photoIntent);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
if (acceptsVideo(acceptTypes)) {
|
|
256
|
-
Intent videoIntent = getVideoIntent();
|
|
257
|
-
if (videoIntent != null) {
|
|
258
|
-
extraIntents.add(videoIntent);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
Intent fileSelectionIntent = getFileChooserIntent(acceptTypes, allowMultiple);
|
|
264
|
-
|
|
265
|
-
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
|
|
266
|
-
chooserIntent.putExtra(Intent.EXTRA_INTENT, fileSelectionIntent);
|
|
267
|
-
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents.toArray(new Parcelable[]{}));
|
|
268
|
-
|
|
269
|
-
if (chooserIntent.resolveActivity(getCurrentActivity().getPackageManager()) != null) {
|
|
270
|
-
getCurrentActivity().startActivityForResult(chooserIntent, PICKER);
|
|
271
|
-
} else {
|
|
272
|
-
Log.w("RNCWebViewModule", "there is no Activity to handle this Intent");
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
return true;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
public void setDownloadRequest(DownloadManager.Request request) {
|
|
279
|
-
this.downloadRequest = request;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
public void downloadFile(String downloadingMessage) {
|
|
283
|
-
DownloadManager dm = (DownloadManager) getCurrentActivity().getBaseContext().getSystemService(Context.DOWNLOAD_SERVICE);
|
|
284
|
-
|
|
285
|
-
try {
|
|
286
|
-
dm.enqueue(this.downloadRequest);
|
|
287
|
-
} catch (IllegalArgumentException e) {
|
|
288
|
-
Log.w("RNCWebViewModule", "Unsupported URI, aborting download", e);
|
|
289
|
-
return;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
Toast.makeText(getCurrentActivity().getApplicationContext(), downloadingMessage, Toast.LENGTH_LONG).show();
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
protected boolean needsCameraPermission() {
|
|
296
|
-
boolean needed = false;
|
|
297
|
-
|
|
298
|
-
PackageManager packageManager = getCurrentActivity().getPackageManager();
|
|
299
|
-
try {
|
|
300
|
-
String[] requestedPermissions = packageManager.getPackageInfo(getReactApplicationContext().getPackageName(), PackageManager.GET_PERMISSIONS).requestedPermissions;
|
|
301
|
-
if (Arrays.asList(requestedPermissions).contains(Manifest.permission.CAMERA)
|
|
302
|
-
&& ContextCompat.checkSelfPermission(getCurrentActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
|
303
|
-
needed = true;
|
|
304
|
-
}
|
|
305
|
-
} catch (PackageManager.NameNotFoundException e) {
|
|
306
|
-
needed = true;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
return needed;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
private Intent getPhotoIntent() {
|
|
313
|
-
Intent intent = null;
|
|
314
|
-
|
|
315
|
-
try {
|
|
316
|
-
outputImage = getCapturedFile(MimeType.IMAGE);
|
|
317
|
-
Uri outputImageUri = getOutputUri(outputImage);
|
|
318
|
-
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
|
319
|
-
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputImageUri);
|
|
320
|
-
} catch (IOException | IllegalArgumentException e) {
|
|
321
|
-
Log.e("CREATE FILE", "Error occurred while creating the File", e);
|
|
322
|
-
e.printStackTrace();
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
return intent;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
private Intent getVideoIntent() {
|
|
329
|
-
Intent intent = null;
|
|
330
|
-
|
|
331
|
-
try {
|
|
332
|
-
outputVideo = getCapturedFile(MimeType.VIDEO);
|
|
333
|
-
Uri outputVideoUri = getOutputUri(outputVideo);
|
|
334
|
-
intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
|
|
335
|
-
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputVideoUri);
|
|
336
|
-
} catch (IOException | IllegalArgumentException e) {
|
|
337
|
-
Log.e("CREATE FILE", "Error occurred while creating the File", e);
|
|
338
|
-
e.printStackTrace();
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
return intent;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
private Intent getFileChooserIntent(String acceptTypes) {
|
|
345
|
-
String _acceptTypes = acceptTypes;
|
|
346
|
-
if (acceptTypes.isEmpty()) {
|
|
347
|
-
_acceptTypes = MimeType.DEFAULT.value;
|
|
348
|
-
}
|
|
349
|
-
if (acceptTypes.matches("\\.\\w+")) {
|
|
350
|
-
_acceptTypes = getMimeTypeFromExtension(acceptTypes.replace(".", ""));
|
|
351
|
-
}
|
|
352
|
-
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
353
|
-
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
354
|
-
intent.setType(_acceptTypes);
|
|
355
|
-
return intent;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
private Intent getFileChooserIntent(String[] acceptTypes, boolean allowMultiple) {
|
|
359
|
-
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
360
|
-
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
361
|
-
intent.setType(MimeType.DEFAULT.value);
|
|
362
|
-
intent.putExtra(Intent.EXTRA_MIME_TYPES, getAcceptedMimeType(acceptTypes));
|
|
363
|
-
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, allowMultiple);
|
|
364
|
-
return intent;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
private Boolean acceptsImages(String types) {
|
|
368
|
-
String mimeType = types;
|
|
369
|
-
if (types.matches("\\.\\w+")) {
|
|
370
|
-
mimeType = getMimeTypeFromExtension(types.replace(".", ""));
|
|
371
|
-
}
|
|
372
|
-
return mimeType.isEmpty() || mimeType.toLowerCase().contains(MimeType.IMAGE.value);
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
private Boolean acceptsImages(String[] types) {
|
|
376
|
-
String[] mimeTypes = getAcceptedMimeType(types);
|
|
377
|
-
return arrayContainsString(mimeTypes, MimeType.DEFAULT.value) || arrayContainsString(mimeTypes, MimeType.IMAGE.value);
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
private Boolean acceptsVideo(String types) {
|
|
381
|
-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
|
382
|
-
return false;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
String mimeType = types;
|
|
386
|
-
if (types.matches("\\.\\w+")) {
|
|
387
|
-
mimeType = getMimeTypeFromExtension(types.replace(".", ""));
|
|
388
|
-
}
|
|
389
|
-
return mimeType.isEmpty() || mimeType.toLowerCase().contains(MimeType.VIDEO.value);
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
private Boolean acceptsVideo(String[] types) {
|
|
393
|
-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
|
394
|
-
return false;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
String[] mimeTypes = getAcceptedMimeType(types);
|
|
398
|
-
return arrayContainsString(mimeTypes, MimeType.DEFAULT.value) || arrayContainsString(mimeTypes, MimeType.VIDEO.value);
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
private Boolean arrayContainsString(String[] array, String pattern) {
|
|
402
|
-
for (String content : array) {
|
|
403
|
-
if (content.contains(pattern)) {
|
|
404
|
-
return true;
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
return false;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
private String[] getAcceptedMimeType(String[] types) {
|
|
411
|
-
if (noAcceptTypesSet(types)) {
|
|
412
|
-
return new String[]{MimeType.DEFAULT.value};
|
|
413
|
-
}
|
|
414
|
-
String[] mimeTypes = new String[types.length];
|
|
415
|
-
for (int i = 0; i < types.length; i++) {
|
|
416
|
-
String t = types[i];
|
|
417
|
-
// convert file extensions to mime types
|
|
418
|
-
if (t.matches("\\.\\w+")) {
|
|
419
|
-
String mimeType = getMimeTypeFromExtension(t.replace(".", ""));
|
|
420
|
-
if(mimeType != null) {
|
|
421
|
-
mimeTypes[i] = mimeType;
|
|
422
|
-
} else {
|
|
423
|
-
mimeTypes[i] = t;
|
|
424
|
-
}
|
|
425
|
-
} else {
|
|
426
|
-
mimeTypes[i] = t;
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
return mimeTypes;
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
private String getMimeTypeFromExtension(String extension) {
|
|
433
|
-
String type = null;
|
|
434
|
-
if (extension != null) {
|
|
435
|
-
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
|
436
|
-
}
|
|
437
|
-
return type;
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
private Uri getOutputUri(File capturedFile) {
|
|
441
|
-
// for versions below 6.0 (23) we use the old File creation & permissions model
|
|
442
|
-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
|
443
|
-
return Uri.fromFile(capturedFile);
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
// for versions 6.0+ (23) we use the FileProvider to avoid runtime permissions
|
|
447
|
-
String packageName = getReactApplicationContext().getPackageName();
|
|
448
|
-
return FileProvider.getUriForFile(getReactApplicationContext(), packageName + ".fileprovider", capturedFile);
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
private File getCapturedFile(MimeType mimeType) throws IOException {
|
|
452
|
-
String prefix = "";
|
|
453
|
-
String suffix = "";
|
|
454
|
-
String dir = "";
|
|
455
|
-
|
|
456
|
-
switch (mimeType) {
|
|
457
|
-
case IMAGE:
|
|
458
|
-
prefix = "image-";
|
|
459
|
-
suffix = ".jpg";
|
|
460
|
-
dir = Environment.DIRECTORY_PICTURES;
|
|
461
|
-
break;
|
|
462
|
-
case VIDEO:
|
|
463
|
-
prefix = "video-";
|
|
464
|
-
suffix = ".mp4";
|
|
465
|
-
dir = Environment.DIRECTORY_MOVIES;
|
|
466
|
-
break;
|
|
467
|
-
|
|
468
|
-
default:
|
|
469
|
-
break;
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
String filename = prefix + String.valueOf(System.currentTimeMillis()) + suffix;
|
|
473
|
-
File outputFile = null;
|
|
474
|
-
|
|
475
|
-
// for versions below 6.0 (23) we use the old File creation & permissions model
|
|
476
|
-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
|
477
|
-
// only this Directory works on all tested Android versions
|
|
478
|
-
// ctx.getExternalFilesDir(dir) was failing on Android 5.0 (sdk 21)
|
|
479
|
-
File storageDir = Environment.getExternalStoragePublicDirectory(dir);
|
|
480
|
-
outputFile = new File(storageDir, filename);
|
|
481
|
-
} else {
|
|
482
|
-
File storageDir = getReactApplicationContext().getExternalFilesDir(null);
|
|
483
|
-
outputFile = File.createTempFile(prefix, suffix, storageDir);
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
return outputFile;
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
private Boolean noAcceptTypesSet(String[] types) {
|
|
490
|
-
// when our array returned from getAcceptTypes() has no values set from the webview
|
|
491
|
-
// i.e. <input type="file" />, without any "accept" attr
|
|
492
|
-
// will be an array with one empty string element, afaik
|
|
493
|
-
|
|
494
|
-
return types.length == 0 || (types.length == 1 && types[0] != null && types[0].length() == 0);
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
private PermissionAwareActivity getPermissionAwareActivity() {
|
|
498
|
-
Activity activity = getCurrentActivity();
|
|
499
|
-
if (activity == null) {
|
|
500
|
-
throw new IllegalStateException("Tried to use permissions API while not attached to an Activity.");
|
|
501
|
-
} else if (!(activity instanceof PermissionAwareActivity)) {
|
|
502
|
-
throw new IllegalStateException("Tried to use permissions API but the host Activity doesn't implement PermissionAwareActivity.");
|
|
503
|
-
}
|
|
504
|
-
return (PermissionAwareActivity) activity;
|
|
505
|
-
}
|
|
506
|
-
}
|