Ver código fonte

fix(Android): Don't show camera options for a file upload when they can not be used (#1210)

* Don't show camera options for a file upload that would result in nothing happening for the user.

On Android, if the application declares the camera permission, then even intents
that use the camera require permission to be granted. This is a problem for apps
that combine an in-app camera with a WebView that has file uploading and the user
has not given permission for the camera.

Note, this will not request permission for camera. This will simply prevent
showing the camera options that would be a no-op action for users. It does this
by checking if the camera permission is declared, and if so, checks that the
user has granted permission.

More information: https://blog.egorand.me/taking-photos-not-so-simply-how-i-got-bitten-by-action_image_capture/

* Add example and documentation about camera option availability in file uploads for Android.
Daniel Vicory 4 anos atrás
pai
commit
4093682e08
Nenhuma conta vinculada ao e-mail do autor do commit

+ 25
- 5
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewModule.java Ver arquivo

@@ -32,6 +32,7 @@ import com.facebook.react.modules.core.PermissionListener;
32 32
 import java.io.File;
33 33
 import java.io.IOException;
34 34
 import java.util.ArrayList;
35
+import java.util.Arrays;
35 36
 
36 37
 import static android.app.Activity.RESULT_OK;
37 38
 
@@ -180,11 +181,13 @@ public class RNCWebViewModule extends ReactContextBaseJavaModule implements Acti
180 181
     filePathCallback = callback;
181 182
 
182 183
     ArrayList<Parcelable> extraIntents = new ArrayList<>();
183
-    if (acceptsImages(acceptTypes)) {
184
-      extraIntents.add(getPhotoIntent());
185
-    }
186
-    if (acceptsVideo(acceptTypes)) {
187
-      extraIntents.add(getVideoIntent());
184
+    if (! needsCameraPermission()) {
185
+      if (acceptsImages(acceptTypes)) {
186
+        extraIntents.add(getPhotoIntent());
187
+      }
188
+      if (acceptsVideo(acceptTypes)) {
189
+        extraIntents.add(getVideoIntent());
190
+      }
188 191
     }
189 192
 
190 193
     Intent fileSelectionIntent = getFileChooserIntent(acceptTypes, allowMultiple);
@@ -233,6 +236,23 @@ public class RNCWebViewModule extends ReactContextBaseJavaModule implements Acti
233 236
     return result;
234 237
   }
235 238
 
239
+  protected boolean needsCameraPermission() {
240
+    boolean needed = false;
241
+
242
+    PackageManager packageManager = getCurrentActivity().getPackageManager();
243
+    try {
244
+      String[] requestedPermissions = packageManager.getPackageInfo(getReactApplicationContext().getPackageName(), PackageManager.GET_PERMISSIONS).requestedPermissions;
245
+      if (Arrays.asList(requestedPermissions).contains(Manifest.permission.CAMERA)
246
+        && ContextCompat.checkSelfPermission(getCurrentActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
247
+        needed = true;
248
+      }
249
+    } catch (PackageManager.NameNotFoundException e) {
250
+      needed = true;
251
+    }
252
+
253
+    return needed;
254
+  }
255
+
236 256
   private Intent getPhotoIntent() {
237 257
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
238 258
     outputFileUri = getOutputUri(MediaStore.ACTION_IMAGE_CAPTURE);

+ 8
- 2
docs/Guide.md Ver arquivo

@@ -191,6 +191,12 @@ Add permission in AndroidManifest.xml:
191 191
 </manifest>
192 192
 ```
193 193
 
194
+###### Camera option availability in uploading for Android
195
+
196
+If the file input indicates that images or video is desired with [`accept`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#accept), then the WebView will attempt to provide options to the user to use their camera to take a picture or video.
197
+
198
+Normally, apps that do not have permission to use the camera can prompt the user to use an external app so that the requesting app has no need for permission. However, Android has made a special exception for this around the camera to reduce confusion for users. If an app *can* request the camera permission because it has been declared, and the user has not granted the permission, it may not fire an intent that would use the camera (`MediaStore.ACTION_IMAGE_CAPTURE` or `MediaStore.ACTION_VIDEO_CAPTURE`). In this scenario, it is up to the developer to request camera permission before a file upload directly using the camera is necessary.
199
+
194 200
 ##### Check for File Upload support, with `static isFileUploadSupported()`
195 201
 
196 202
 File Upload using `<input type="file" />` is not supported for Android 4.4 KitKat (see [details](https://github.com/delight-im/Android-AdvancedWebView/issues/4#issuecomment-70372146)):
@@ -301,7 +307,7 @@ _Under the hood_
301 307
 
302 308
 #### The `injectedJavaScriptBeforeContentLoaded` prop
303 309
 
304
-This is a script that runs **before** the web page loads for the first time. It only runs once, even if the page is reloaded or navigated away. This is useful if you want to inject anything into the window, localstorage, or document prior to the web code executing. 
310
+This is a script that runs **before** the web page loads for the first time. It only runs once, even if the page is reloaded or navigated away. This is useful if you want to inject anything into the window, localstorage, or document prior to the web code executing.
305 311
 
306 312
 ```jsx
307 313
 import React, { Component } from 'react';
@@ -329,7 +335,7 @@ export default class App extends Component {
329 335
 }
330 336
 ```
331 337
 
332
-This runs the JavaScript in the `runFirst` string before the page is loaded. In this case, the value of `window.isNativeApp` will be set to true before the web code executes. 
338
+This runs the JavaScript in the `runFirst` string before the page is loaded. In this case, the value of `window.isNativeApp` will be set to true before the web code executes.
333 339
 
334 340
 #### The `injectJavaScript` method
335 341
 

+ 15
- 0
example/App.tsx Ver arquivo

@@ -7,11 +7,13 @@ import {
7 7
   View,
8 8
   Keyboard,
9 9
   Button,
10
+  Platform,
10 11
 } from 'react-native';
11 12
 
12 13
 import Alerts from './examples/Alerts';
13 14
 import Scrolling from './examples/Scrolling';
14 15
 import Background from './examples/Background';
16
+import Uploads from './examples/Uploads';
15 17
 
16 18
 const TESTS = {
17 19
   Alerts: {
@@ -38,6 +40,14 @@ const TESTS = {
38 40
       return <Background />;
39 41
     },
40 42
   },
43
+  Uploads: {
44
+    title: 'Uploads',
45
+    testId: 'uploads',
46
+    description: 'Upload test',
47
+    render() {
48
+      return <Uploads />;
49
+    },
50
+  },
41 51
 };
42 52
 
43 53
 type Props = {};
@@ -91,6 +101,11 @@ export default class App extends Component<Props, State> {
91 101
             title="Background"
92 102
             onPress={() => this._changeTest('Background')}
93 103
           />
104
+          {Platform.OS === 'android' && <Button
105
+            testID="testType_uploads"
106
+            title="Uploads"
107
+            onPress={() => this._changeTest('Uploads')}
108
+          />}
94 109
         </View>
95 110
 
96 111
         {restarting ? null : (

+ 1
- 0
example/android/app/src/main/AndroidManifest.xml Ver arquivo

@@ -2,6 +2,7 @@
2 2
   package="com.example">
3 3
 
4 4
     <uses-permission android:name="android.permission.INTERNET" />
5
+    <uses-permission android:name="android.permission.CAMERA" />
5 6
 
6 7
     <application
7 8
       android:name=".MainApplication"

+ 69
- 0
example/examples/Uploads.tsx Ver arquivo

@@ -0,0 +1,69 @@
1
+import React, {Component} from 'react';
2
+import {Button, Linking, Text, View} from 'react-native';
3
+
4
+import WebView from 'react-native-webview';
5
+
6
+const HTML = `
7
+<!DOCTYPE html>\n
8
+<html>
9
+  <head>
10
+    <title>Uploads</title>
11
+    <meta http-equiv="content-type" content="text/html; charset=utf-8">
12
+    <meta name="viewport" content="width=320, user-scalable=no">
13
+    <style type="text/css">
14
+      body {
15
+        margin: 0;
16
+        padding: 0;
17
+        font: 62.5% arial, sans-serif;
18
+        background: #ccc;
19
+      }
20
+    </style>
21
+  </head>
22
+  <body>
23
+    <p>
24
+      <label for="images-only">Images only file upload</label>
25
+      <input name="images-only" type="file" accept="image/*">
26
+    </p>
27
+    <p>
28
+      <label for="video-only">Video only file upload</label>
29
+      <input name="video-only" type="file" accept="video/*">
30
+    </p>
31
+    <p>
32
+      <label for="any-file">Any file upload</label>
33
+      <input name="any-file" type="file">
34
+    </p>
35
+  </body>
36
+</html>
37
+`;
38
+
39
+type Props = {};
40
+type State = {};
41
+
42
+export default class Uploads extends Component<Props, State> {
43
+  state = {};
44
+
45
+  render() {
46
+    return (
47
+      <View>
48
+        <View style={{ height: 120 }}>
49
+          <WebView
50
+            source={{html: HTML}}
51
+            automaticallyAdjustContentInsets={false}
52
+          />
53
+        </View>
54
+        <Text>
55
+            Android limitation: If the file input should show camera options for the user,
56
+            and the app has the ability to request the camera permission, then the user must
57
+            grant permission first in order to see the options. Since this example app does
58
+            have the permission declared, you must allow it in settings to be able to see
59
+            camera options. If your app does not have the camera permission declared, then
60
+            there is no restriction to showing the camera options.
61
+        </Text>
62
+        <Button
63
+          title="Open settings"
64
+          onPress={() => Linking.openSettings()}
65
+        />
66
+      </View>
67
+    );
68
+  }
69
+}