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

Commit

Permalink
[Windows] Cleanup camera on app suspension
Browse files Browse the repository at this point in the history
Save/restore scanning state on suspend/resume
  • Loading branch information
daserge committed Jun 22, 2016
1 parent c3bcd87 commit 49489db
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 @@ -499,6 +506,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 @@ -509,14 +517,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 @@ -534,7 +547,7 @@ module.exports = {
}
}

WinJS.Promise.wrap(createPreview())
BarcodeReader.scanPromise = WinJS.Promise.wrap(createPreview())
.then(function () {
checkCancelled();
return startPreview();
Expand All @@ -552,7 +565,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 @@ -570,6 +588,12 @@ module.exports = {
fail(error);
}
});

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

BarcodeReader.destroyPreview = destroyPreview;
},

/**
Expand All @@ -583,4 +607,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 49489db

Please sign in to comment.