Commit e04739d8f3fdc0b89dac15b93306bd434caea345

Authored by Pablo Navarro
Committed by Andrei Pfeiffer
1 parent 006c1fad
Exists in master

Check & Request camera permissions before using it (#15)

* Fixed camera permission. Now requesting permission before opening camera
* Updated README.md
README.md
... ... @@ -122,18 +122,6 @@ NOTE: this is a requirement for `sdk 26`. This approach should NOT require you t
122 122 </manifest>
123 123 ```
124 124  
125   -* !!! IMPORTANT !!!
126   -
127   -Remove the explicit Camera permission from your `AndroidManifest.xml`, in case you have it:
128   -
129   -```diff
130   -<manifest ...>
131   -
132   -- <uses-permission android:name="android.permission.CAMERA" />
133   -
134   -</manifest>
135   -```
136   -
137 125 ### Re-build your application
138 126  
139 127 Since you have changed native code, reloading the JS code alone won't work:
... ...
android/src/main/java/com/rncustomwebview/CustomWebViewModule.java
1 1 package com.rncustomwebview;
2 2  
  3 +import android.Manifest;
  4 +import android.annotation.TargetApi;
3 5 import android.app.Activity;
4 6 import android.content.DialogInterface;
5 7 import android.content.Intent;
  8 +import android.content.pm.PackageManager;
6 9 import android.net.Uri;
7 10 import android.os.Build;
8 11 import android.provider.MediaStore;
  12 +import android.support.v4.app.ActivityCompat;
9 13 import android.support.v4.content.FileProvider;
10 14 import android.util.Log;
11 15 import android.webkit.ValueCallback;
12 16 import android.webkit.WebChromeClient;
13 17  
  18 +import com.facebook.react.ReactActivity;
14 19 import com.facebook.react.bridge.ActivityEventListener;
15 20 import com.facebook.react.bridge.ReactApplicationContext;
16 21 import com.facebook.react.bridge.ReactContextBaseJavaModule;
  22 +import com.facebook.react.modules.core.PermissionAwareActivity;
  23 +import com.facebook.react.modules.core.PermissionListener;
17 24  
18 25 import java.io.File;
19 26 import java.io.IOException;
... ... @@ -159,21 +166,58 @@ public class CustomWebViewModule extends ReactContextBaseJavaModule implements A
159 166 this.aPackage = aPackage;
160 167 }
161 168  
162   - private void startCamera(String intentType, String prefix, String suffix) {
  169 + private PermissionListener listener = new PermissionListener() {
  170 + public boolean onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  171 + switch (requestCode) {
  172 + case REQUEST_CAMERA: {
  173 + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  174 + startCamera(MediaStore.ACTION_IMAGE_CAPTURE, "image-", ".jpg");
  175 + break;
  176 + }
  177 + else {
  178 + filePathCallback.onReceiveValue(null);
  179 + break;
  180 + }
  181 + }
  182 + }
  183 + return false;
  184 + }
  185 + };
163 186  
164   - // bring up a camera picker intent
165   - // we need to pass a filename for the file to be saved to
166   - Intent intent = new Intent(intentType);
167   -
168   - // Create the File where the photo should go
169   - try {
170   - String packageName = getReactApplicationContext().getPackageName();
171   - File capturedFile = createCapturedFile(prefix, suffix);
172   - outputFileUri = FileProvider.getUriForFile(getReactApplicationContext(), packageName+".fileprovider", capturedFile);
173   - intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
174   - getCurrentActivity().startActivityForResult(intent, REQUEST_CAMERA);
175   - } catch (IOException ex) {
176   - Log.e("CREATE FILE", "Error occurred while creating the File", ex);
  187 + @TargetApi(Build.VERSION_CODES.M)
  188 + private void requestPermissions() {
  189 + if (getCurrentActivity() instanceof ReactActivity) {
  190 + ((ReactActivity) getCurrentActivity()).requestPermissions(new String[]{ Manifest.permission.CAMERA}, REQUEST_CAMERA, listener);
  191 + }
  192 + else {
  193 + ((PermissionAwareActivity) getCurrentActivity()).requestPermissions(new String[]{ Manifest.permission.CAMERA}, REQUEST_CAMERA, listener);
  194 + }
  195 + }
  196 +
  197 + @TargetApi(Build.VERSION_CODES.M)
  198 + private boolean permissionsGranted() {
  199 + return ActivityCompat.checkSelfPermission(getCurrentActivity(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
  200 + }
  201 +
  202 +
  203 + private void startCamera(String intentType, String prefix, String suffix) {
  204 + if (permissionsGranted()) {
  205 + // bring up a camera picker intent
  206 + // we need to pass a filename for the file to be saved to
  207 + Intent intent = new Intent(intentType);
  208 +
  209 + // Create the File where the photo should go
  210 + try {
  211 + String packageName = getReactApplicationContext().getPackageName();
  212 + File capturedFile = createCapturedFile(prefix, suffix);
  213 + outputFileUri = FileProvider.getUriForFile(getReactApplicationContext(), packageName+".fileprovider", capturedFile);
  214 + intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
  215 + getCurrentActivity().startActivityForResult(intent, REQUEST_CAMERA);
  216 + } catch (IOException ex) {
  217 + Log.e("CREATE FILE", "Error occurred while creating the File", ex);
  218 + }
  219 + } else {
  220 + requestPermissions();
177 221 }
178 222 }
179 223  
... ...