Skip to content

Commit

Permalink
Merge pull request #21 from willhay/fix/android-light-methods
Browse files Browse the repository at this point in the history
Fix android light methods
  • Loading branch information
bitjson authored Aug 12, 2016
2 parents 4e2863d + 21add2f commit f4869f3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 53 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var callback = function(err, contents){
QRScanner.scan(callback);
```

Sets QRScanner to "watch" for valid QR codes. Once a valid code is detected, it's contents are passed to the callback, and scanning is toggled off. If `QRScanner.prepare()` has not been called, this method performs that setup as well. On platforms other than iOS, the video preview must be visible for scanning to function.
Sets QRScanner to "watch" for valid QR codes. Once a valid code is detected, it's contents are passed to the callback, and scanning is toggled off. If `QRScanner.prepare()` has not been called, this method performs that setup as well. On platforms other than iOS and Android, the video preview must be visible for scanning to function.

```js
QRScanner.cancelScan(function(status){
Expand Down
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 f4869f3

Please sign in to comment.