Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add permission check on Android API >= 25 #2

Merged
merged 3 commits into from
Dec 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

A simple React Native plugin to switch a flashlight on/off.

Currently supports both iOS (>= 8.0) and Android.
Currently supports both iOS (>= 8.0) and Android (all versions).

Applies the permission checks on Android API >=25 devices. Below API 25, Android will automatically require the user to accept permissions on install / update.

## Install

Expand All @@ -15,12 +17,36 @@ react-native link react-native-torch

## Usage

### Without permissions check

```js
import Torch from 'react-native-torch';

Torch.switchState(true) // Turn ON
Torch.switchState(false) // Turn OFF
Torch.switchState(true); // Turn ON
Torch.switchState(false); // Turn OFF
```

### With extra permission check and dialog (Android only)

```js
import Torch from 'react-native-torch';
import { Platform } from 'react-native';

if (Platform.OS === 'ios') {
Torch.switchState(this.isTorchOn);
} else {
const cameraAllowed = await Torch.requestCameraPermission(
'Camera Permissions', // dialog title
'We require camera permissions to use the torch on the back of your phone.' // dialog body
);

if (cameraAllowed) {
Torch.switchState(this.isTorchOn);
}
}
```

A demo application [TorchDemo](https://github.com/ludo/TorchDemo) is also
available.

Android version has flow support.
26 changes: 9 additions & 17 deletions android/src/main/java/com/cubicphuse/RCTTorch/RCTTorchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,19 @@
package com.cubicphuse.RCTTorch;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.content.pm.PackageManager;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

public class RCTTorchModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext myReactContext;
public boolean isTorchOn = false;
protected Camera camera;
protected Parameters params;
private Boolean isTorchOn = false;
private Camera camera;

public RCTTorchModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand All @@ -49,20 +43,19 @@ public void switchState(Boolean newState) {
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
else {
} else {
Camera.Parameters params;

if (!isTorchOn) {
camera = Camera.open();
params = camera.getParameters();

params.setFlashMode(Parameters.FLASH_MODE_TORCH);
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isTorchOn = true;
}
else {
} else {
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);

camera.setParameters(params);
camera.stopPreview();
Expand All @@ -72,4 +65,3 @@ public void switchState(Boolean newState) {
}
}
}

58 changes: 58 additions & 0 deletions index.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// @flow
import { NativeModules, PermissionsAndroid, Alert } from 'react-native';

const { Torch } = NativeModules;

/* Private fucntion to create a dialog with an OK button
This is because the RN rationale dialog has no buttons, so isn't obvious to dismiss
NOTE: We always show this dialog, if cameraPermission not present */
async function showRationaleDialog(title: string, message: string): Promise<*> {
let done;
const result = new Promise(resolve => {
done = resolve;
});

Alert.alert(title, message, [
{
text: 'OK',
onPress: () => done()
}
]);

return result;
}

async function requestCameraPermission(
title: string,
message: string
): Promise<boolean> {
try {
const hasCameraPermission = await PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.CAMERA
);

if (hasCameraPermission) {
return true;
}

await showRationaleDialog(title, message);

const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA
);

if (granted === PermissionsAndroid.RESULTS.GRANTED) {
return true;
}
return false;
} catch (err) {
return false;
}
}

const TorchWithPermissionCheck = {
...Torch,
requestCameraPermission
};

export default TorchWithPermissionCheck;
1 change: 1 addition & 0 deletions index.js → index.ios.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NativeModules } from 'react-native';

const { Torch } = NativeModules;

/**
Expand Down
13 changes: 2 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
{
"name": "react-native-torch",
"version": "1.0.1",
"version": "1.1.0",
"description": "Torch/flashlight for react-native",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/ludo/react-native-torch"
},
"keywords": [
"react",
"react-component",
"react-native",
"ios",
"android",
"device",
"torch",
"flashlight"
],
"keywords": ["react", "react-component", "react-native", "ios", "android", "device", "torch", "flashlight"],
"author": "Ludo van den Boom <[email protected]> (https://github.com/ludo)",
"license": "MIT"
}