Skip to content

fix#6 support for torch state checking #11

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
58 changes: 45 additions & 13 deletions android/src/main/java/com/cubicphuse/RCTTorch/RCTTorchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,74 @@
* Created by Ludo van den Boom <[email protected]> on 06/04/2017.
*/

package com.cubicphuse.RCTTorch;
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.content.Context;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

public class RCTTorchModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext myReactContext;
private Boolean isTorchOn = false;
private Camera camera;
private CameraManager cameraManager;

public RCTTorchModule(ReactApplicationContext reactContext) {
super(reactContext);

// Need access to reactContext to check for camera
this.myReactContext = reactContext;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cameraManager =
(CameraManager) this.myReactContext.getSystemService(Context.CAMERA_SERVICE);
}
}

CameraManager.TorchCallback torchCallback = new CameraManager.TorchCallback() {
@Override
public void onTorchModeUnavailable(String cameraId) {
super.onTorchModeUnavailable(cameraId);
}

@Override
public void onTorchModeChanged(String cameraId, boolean enabled) {
super.onTorchModeChanged(cameraId, enabled);
isTorchOn = enabled;
}
};

@Override
public String getName() {
return "RCTTorch";
}

@ReactMethod
public void switchState(Boolean newState, Callback successCallback, Callback failureCallback) {
public void getTorchStatus(Callback successCallback, Callback failureCallback) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
CameraManager cameraManager =
(CameraManager) this.myReactContext.getSystemService(Context.CAMERA_SERVICE);
cameraManager.registerTorchCallback(torchCallback, null);
try {
successCallback.invoke(isTorchOn);
} catch (Exception e) {
String errorMessage = e.getMessage();
failureCallback.invoke("Error: " + errorMessage);
}
}
}

@ReactMethod
public void switchState(Boolean newState, Callback successCallback, Callback failureCallback) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, newState);
Expand Down
19 changes: 17 additions & 2 deletions index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ async function requestCameraPermission(
}
}

async function getStatus(): Promise<boolean> {
let done;
let failure;

const result = new Promise((resolve, reject) => {
done = resolve;
failure = reject;
});

Torch.getTorchStatus(done, failure);

return result;
}

async function switchState(newState: boolean): Promise<boolean> {
let done;
let failure;
Expand All @@ -73,8 +87,9 @@ async function switchState(newState: boolean): Promise<boolean> {

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

export default TorchWithPermissionCheck;
25 changes: 21 additions & 4 deletions ios/RCTTorch.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,39 @@ @implementation RCTTorch

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(switchState:(BOOL *)newState)
RCT_EXPORT_METHOD(switchState:(nonnull NSNumber*)newState)
{
if ([AVCaptureDevice class]) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]){
[device lockForConfiguration:nil];

if (newState) {
if ([newState boolValue]) {
[device setTorchMode:AVCaptureTorchModeOn];
} else {
[device setTorchMode:AVCaptureTorchModeOff];
}

[device unlockForConfiguration];
}
}
}

RCT_EXPORT_METHOD(getStatus:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
if ([AVCaptureDevice class]) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]){
BOOL isOn = device.torchMode == AVCaptureTorchModeOn;
resolve([NSNumber numberWithBool:isOn]);
} else {
NSError *error = [[NSError alloc] initWithDomain:@"torch" code:0 userInfo:nil];
reject(@"no_torch_available", @"This device has no torch", error);
}
} else {
NSError *error = [[NSError alloc] initWithDomain:@"torch" code:0 userInfo:nil];
reject(@"no_torch_available", @"This device has no torch", error);
}
}

@end