CB-8929 Use PowerManager to get battery state on Win 10 This closes #42
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-battery-status/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-battery-status/commit/85c6f507 Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-battery-status/tree/85c6f507 Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-battery-status/diff/85c6f507 Branch: refs/heads/1.2.x Commit: 85c6f507a3e4b8640ae81eac3be953e6d7cf19ab Parents: e7c1a16 Author: Vladimir Kotikov <[email protected]> Authored: Mon Nov 7 14:46:20 2016 +0300 Committer: Vladimir Kotikov <[email protected]> Committed: Tue Nov 8 17:09:07 2016 +0300 ---------------------------------------------------------------------- README.md | 6 +++--- src/windows/BatteryProxy.js | 42 +++++++++++++++++++++++++++++++++++++++- tests/tests.js | 40 ++++++++++++++++++++++---------------- 3 files changed, 67 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-battery-status/blob/85c6f507/README.md ---------------------------------------------------------------------- diff --git a/README.md b/README.md index 8301f45..30ace99 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Fires when the battery charge percentage changes by at least 1 percent, or when - Android - BlackBerry 10 - Windows Phone 7 and 8 -- Windows (Windows Phone 8.1 only) +- Windows (Windows Phone 8.1 and Windows 10) - Firefox OS - Browser (Chrome, Firefox, Opera) @@ -100,7 +100,7 @@ Fires when the battery charge percentage reaches the low charge threshold. This - Android - BlackBerry 10 - Firefox OS -- Windows (Windows Phone 8.1 only) +- Windows (Windows Phone 8.1 and Windows 10) - Browser (Chrome, Firefox, Opera) ### Quirks: Windows Phone 8.1 @@ -126,7 +126,7 @@ Fires when the battery charge percentage reaches the critical charge threshold. - Android - BlackBerry 10 - Firefox OS -- Windows (Windows Phone 8.1 only) +- Windows (Windows Phone 8.1 and Windows 10) - Browser (Chrome, Firefox, Opera) ### Quirks: Windows Phone 8.1 http://git-wip-us.apache.org/repos/asf/cordova-plugin-battery-status/blob/85c6f507/src/windows/BatteryProxy.js ---------------------------------------------------------------------- diff --git a/src/windows/BatteryProxy.js b/src/windows/BatteryProxy.js index ed91bcb..9f5ac66 100644 --- a/src/windows/BatteryProxy.js +++ b/src/windows/BatteryProxy.js @@ -19,7 +19,47 @@ * */ -/* global WinJS, BatteryStatus */ +/* global Windows, WinJS, BatteryStatus */ + +var PowerManager = Windows && Windows.System && + Windows.System.Power && Windows.System.Power.PowerManager; + +if (PowerManager) { + var pluginCallback; + var reportStatus = function () { + if (!pluginCallback) { + return; + } + + pluginCallback({ + level: PowerManager.remainingChargePercent, + isPlugged: PowerManager.powerSupplyStatus !== Windows.System.Power.PowerSupplyStatus.notPresent + }, { keepCallback: true }); + }; + + var BatteryWin10 = { + start: function (win, fail) { + pluginCallback = win; + PowerManager.addEventListener('remainingchargepercentchanged', reportStatus); + PowerManager.addEventListener('powersupplystatuschanged', reportStatus); + + reportStatus(); + }, + + stop: function () { + if (pluginCallback) { + PowerManager.removeEventListener('remainingchargepercentchanged', reportStatus); + PowerManager.removeEventListener('powersupplystatuschanged', reportStatus); + } + + pluginCallback = null; + } + }; + + require("cordova/exec/proxy").add("Battery", BatteryWin10); + return; +} + var stopped; http://git-wip-us.apache.org/repos/asf/cordova-plugin-battery-status/blob/85c6f507/tests/tests.js ---------------------------------------------------------------------- diff --git a/tests/tests.js b/tests/tests.js index ad21d3b..69211ad 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -20,16 +20,22 @@ */ /* jshint jasmine: true */ -/* global WinJS */ +/* global Windows, WinJS */ exports.defineAutoTests = function () { - var isWindowsStore = (cordova.platformId == "windows8") || (cordova.platformId == "windows" && !WinJS.Utilities.isPhone), - onEvent; + var hasPowerManagerAPI = Windows && Windows.System && + Windows.System.Power && Windows.System.Power.PowerManager; + + var batteryStatusUnsupported = cordova.platformId === "windows8" || + // We don't test battery status on Windows when there is no corresponding APIs available + cordova.platformId === "windows" && !(hasPowerManagerAPI || WinJS.Utilities.isPhone); + + var onEvent; describe('Battery (navigator.battery)', function () { it("battery.spec.1 should exist", function () { - if (isWindowsStore) { + if (batteryStatusUnsupported) { pending('Battery status is not supported on windows store'); } @@ -42,7 +48,7 @@ exports.defineAutoTests = function () { describe("batterystatus", function () { afterEach(function () { - if (!isWindowsStore) { + if (!batteryStatusUnsupported) { try { window.removeEventListener("batterystatus", onEvent, false); } @@ -53,7 +59,7 @@ exports.defineAutoTests = function () { }); it("battery.spec.2 should fire batterystatus events", function (done) { - if (isWindowsStore) { + if (batteryStatusUnsupported) { pending('Battery status is not supported on windows store'); } @@ -78,7 +84,7 @@ exports.defineAutoTests = function () { describe("batterylow", function () { afterEach(function () { - if (!isWindowsStore) { + if (!batteryStatusUnsupported) { try { window.removeEventListener("batterylow", onEvent, false); } @@ -89,7 +95,7 @@ exports.defineAutoTests = function () { }); it("battery.spec.3 should fire batterylow event (30 -> 20)", function (done) { - if (isWindowsStore) { + if (batteryStatusUnsupported) { pending('Battery status is not supported on windows store'); } @@ -116,7 +122,7 @@ exports.defineAutoTests = function () { }); it("battery.spec.3.1 should fire batterylow event (30 -> 19)", function (done) { - if (isWindowsStore) { + if (batteryStatusUnsupported) { pending('Battery status is not supported on windows store'); } @@ -142,7 +148,7 @@ exports.defineAutoTests = function () { }); it("battery.spec.3.2 should not fire batterylow event (5 -> 20)", function (done) { - if (isWindowsStore) { + if (batteryStatusUnsupported) { pending('Battery status is not supported on windows store'); } @@ -168,7 +174,7 @@ exports.defineAutoTests = function () { }); it("battery.spec.3.3 batterylow event(21 -> 20) should not fire if charging", function (done) { - if (isWindowsStore) { + if (batteryStatusUnsupported) { pending('Battery status is not supported on windows store'); } @@ -197,7 +203,7 @@ exports.defineAutoTests = function () { describe("batterycritical", function () { afterEach(function () { - if (!isWindowsStore) { + if (!batteryStatusUnsupported) { try { window.removeEventListener("batterycritical", onEvent, false); } @@ -208,7 +214,7 @@ exports.defineAutoTests = function () { }); it("battery.spec.4 should fire batterycritical event (19 -> 5)", function (done) { - if (isWindowsStore) { + if (batteryStatusUnsupported) { pending('Battery status is not supported on windows store'); } @@ -235,7 +241,7 @@ exports.defineAutoTests = function () { }); it("battery.spec.4.1 should fire batterycritical event (19 -> 4)", function (done) { - if (isWindowsStore) { + if (batteryStatusUnsupported) { pending('Battery status is not supported on windows store'); } @@ -262,7 +268,7 @@ exports.defineAutoTests = function () { }); it("battery.spec.4.2 should fire batterycritical event (100 -> 4) when decreases", function (done) { - if (isWindowsStore) { + if (batteryStatusUnsupported) { pending('Battery status is not supported on windows store'); } @@ -288,7 +294,7 @@ exports.defineAutoTests = function () { }); it("battery.spec.4.3 should not fire batterycritical event (4 -> 5) when increasing", function (done) { - if (isWindowsStore) { + if (batteryStatusUnsupported) { pending('Battery status is not supported on windows store'); } @@ -314,7 +320,7 @@ exports.defineAutoTests = function () { }); it("battery.spec.4.4 should not fire batterycritical event (6 -> 5) if charging", function (done) { - if (isWindowsStore) { + if (batteryStatusUnsupported) { pending('Battery status is not supported on windows store'); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
