Skip to content

Commit

Permalink
fix(android): fixes the enableLight and disableLight methods
Browse files Browse the repository at this point in the history
Enable and disableLight now prepare the camera if not already prepared. Also light is no longer
enabled if enableLight was called, camera access denied, and scan was called and camera permission
accepted.
  • Loading branch information
Will Hay committed Aug 11, 2016
1 parent 1648766 commit 21add2f
Showing 1 changed file with 67 additions and 52 deletions.
119 changes: 67 additions & 52 deletions src/android/QRScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public class QRScanner extends CordovaPlugin implements BarcodeCallback {
//Preview started or paused
private boolean previewing = false;
private BarcodeView mBarcodeView;
private boolean switchFlashOn;
private boolean switchFlashOn = false;
private boolean switchFlashOff = false;
private boolean cameraPreviewing;
private boolean scanning = false;
private CallbackContext nextScanCallback;
Expand Down Expand Up @@ -164,8 +165,15 @@ public void run() {
else if (action.equals("disableLight")) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
switchFlashOn = false;
disableLight(callbackContext);
switchFlashOff = true;
if (hasFlash()) {
if (!hasPermission()) {
requestPermission(33);
} else
disableLight(callbackContext);
} else {
callbackContext.error(QRScannerError.LIGHT_UNAVAILABLE);
}
}
});
return true;
Expand Down Expand Up @@ -246,29 +254,31 @@ private String boolToNumberString(Boolean bool) {
return "0";
}

private void doswitchFlash(final boolean toggleLight, final CallbackContext callbackContext) throws IOException, CameraAccessException {
if (mBarcodeView == null) {
lightOn = true;
private void doswitchFlash(final boolean toggleLight, final CallbackContext callbackContext) throws IOException, CameraAccessException { //No flash for front facing cameras
if (getCurrentCameraId() == Camera.CameraInfo.CAMERA_FACING_FRONT) {
callbackContext.error(QRScannerError.LIGHT_UNAVAILABLE);
return;
}
if (!prepared) {
if (toggleLight)
lightOn = true;
else
lightOn = false;
prepare(callbackContext);
}
//No flash for front facing cameras
if (getCurrentCameraId() == Camera.CameraInfo.CAMERA_FACING_FRONT) {
callbackContext.error(QRScannerError.LIGHT_UNAVAILABLE);
return;
}
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (mBarcodeView != null) {
mBarcodeView.setTorch(toggleLight);
if (toggleLight)
lightOn = true;
else
lightOn = false;
}
getStatus(callbackContext);
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (mBarcodeView != null) {
mBarcodeView.setTorch(toggleLight);
if (toggleLight)
lightOn = true;
else
lightOn = false;
}
});
getStatus(callbackContext);
}
});
}

public int getCurrentCameraId() {
Expand Down Expand Up @@ -344,8 +354,10 @@ public void onRequestPermissionResult(int requestCode, String[] permissions,
denied = false;
switch (requestCode) {
case 33:
if(switchFlashOn)
if(switchFlashOn && !scanning && !switchFlashOff)
switchFlash(true, callbackContext);
else if(switchFlashOff && !scanning)
switchFlash(false, callbackContext);
else {
setupCamera(callbackContext);
if(!scanning)
Expand Down Expand Up @@ -533,42 +545,42 @@ public void run() {
}

private void scan(final CallbackContext callbackContext) {
scanning = true;
if (!prepared) {
shouldScanAgain = true;
if (hasCamera()) {
if (!hasPermission()) {
requestPermission(33);
} else {
setupCamera(callbackContext);
}
scanning = true;
if (!prepared) {
shouldScanAgain = true;
if (hasCamera()) {
if (!hasPermission()) {
requestPermission(33);
} else {
setupCamera(callbackContext);
}
} else {
if(!previewing) {
this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if(mBarcodeView != null) {
mBarcodeView.resume();
previewing = true;
if(switchFlashOn)
lightOn = true;
}
}
});
}
shouldScanAgain = false;
this.nextScanCallback = callbackContext;
final BarcodeCallback b = this;
}
} else {
if(!previewing) {
this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (mBarcodeView != null) {
mBarcodeView.decodeSingle(b);
if(mBarcodeView != null) {
mBarcodeView.resume();
previewing = true;
if(switchFlashOn)
lightOn = true;
}
}
});
}
shouldScanAgain = false;
this.nextScanCallback = callbackContext;
final BarcodeCallback b = this;
this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (mBarcodeView != null) {
mBarcodeView.decodeSingle(b);
}
}
});
}
}

private void cancelScan(final CallbackContext callbackContext) {
Expand Down Expand Up @@ -634,12 +646,15 @@ public void run() {
}

private void enableLight(CallbackContext callbackContext) {
lightOn = true;
if(hasPermission())
switchFlash(true, callbackContext);
else callbackContext.error(QRScannerError.CAMERA_ACCESS_DENIED);
}

private void disableLight(CallbackContext callbackContext) {
lightOn = false;
switchFlashOn = false;
if(hasPermission())
switchFlash(false, callbackContext);
else callbackContext.error(QRScannerError.CAMERA_ACCESS_DENIED);
Expand Down

0 comments on commit 21add2f

Please sign in to comment.