Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #256 from daserge/cleanup-on-events
Browse files Browse the repository at this point in the history
[Windows] Cleanup camera on app suspension
  • Loading branch information
Sergey Shakhnazarov authored Jun 24, 2016
2 parents ae9f6e2 + 49489db commit d3344f2
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions src/windows/BarcodeScannerProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ module.exports = {
capture,
reader;

// Save call state for suspend/resume
BarcodeReader.scanCallArgs = {
success: success,
fail: fail,
args: args
};

function updatePreviewForRotation(evt) {
if (!capture) {
return;
Expand Down Expand Up @@ -508,6 +515,7 @@ module.exports = {
* Removes preview frame and corresponding objects from window
*/
function destroyPreview() {
var promise = WinJS.Promise.as();

Windows.Graphics.Display.DisplayInformation.getForCurrentView().removeEventListener("orientationchanged", updatePreviewForRotation, false);
document.removeEventListener('backbutton', cancelPreview);
Expand All @@ -518,14 +526,19 @@ module.exports = {
if (capturePreviewFrame) {
document.body.removeChild(capturePreviewFrame);
}
capturePreviewFrame = null;

reader && reader.stop();
reader = null;

capture && capture.stopRecordAsync();
if (capture) {
promise = capture.stopRecordAsync();
}
capture = null;

enableZoomAndScroll();

return promise;
}

/**
Expand All @@ -543,7 +556,7 @@ module.exports = {
}
}

WinJS.Promise.wrap(createPreview())
BarcodeReader.scanPromise = WinJS.Promise.wrap(createPreview())
.then(function () {
checkCancelled();
return startPreview();
Expand All @@ -561,7 +574,12 @@ module.exports = {
return reader.readCode();
});
})
.done(function (result) {
.then(function (result) {
// Suppress null result (cancel) on suspending
if (BarcodeReader.suspended) {
return;
}

destroyPreview();
success({
text: result && result.text,
Expand All @@ -579,6 +597,12 @@ module.exports = {
fail(error);
}
});

BarcodeReader.videoPreviewIsVisible = function () {
return capturePreviewFrame !== null;
}

BarcodeReader.destroyPreview = destroyPreview;
},

/**
Expand All @@ -592,4 +616,27 @@ module.exports = {
}
};

var app = WinJS.Application;

function waitForScanEnd() {
return BarcodeReader.scanPromise || WinJS.Promise.as();
}

// About to be suspended
app.addEventListener('checkpoint', function (args) {
if (BarcodeReader.videoPreviewIsVisible && BarcodeReader.videoPreviewIsVisible() && BarcodeReader.destroyPreview) {
BarcodeReader.suspended = true;
args.setPromise(BarcodeReader.destroyPreview()
.then(waitForScanEnd, waitForScanEnd));
}
});

// Resuming from a user suspension
Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", function () {
if (BarcodeReader.suspended) {
BarcodeReader.suspended = false;
module.exports.scan(BarcodeReader.scanCallArgs.success, BarcodeReader.scanCallArgs.fail, BarcodeReader.scanCallArgs.args);
}
}, false);

require("cordova/exec/proxy").add("BarcodeScanner", module.exports);

0 comments on commit d3344f2

Please sign in to comment.