CB-7971 Add cordova-plugin-battery-status support for Windows Phone 8.1
Added Windows Phone 8.1 support
Updated the documentation
github close #19
diff --git a/README.md b/README.md
index 829d40d..3c6247f 100644
--- a/README.md
+++ b/README.md
@@ -55,6 +55,7 @@
- Android
- BlackBerry 10
- Windows Phone 7 and 8
+- Windows (Windows Phone 8.1 only)
- Tizen
- Firefox OS
@@ -64,6 +65,11 @@
level, so the `level` property is unavailable. The `isPlugged`
parameter _is_ supported.
+### Windows Quirks
+
+Windows Phone 8.1 does not support `isPlugged` parameter.
+The `level` parameter _is_ supported.
+
### Example
window.addEventListener("batterystatus", onBatteryStatus, false);
@@ -96,6 +102,11 @@
- BlackBerry 10
- Tizen
- Firefox OS
+- Windows (Windows Phone 8.1 only)
+
+### Windows Quirks
+
+Windows Phone 8.1 will fire `batterycritical` event regardless of plugged state as it is not supported.
### Example
@@ -129,6 +140,11 @@
- BlackBerry 10
- Tizen
- Firefox OS
+- Windows (Windows Phone 8.1 only)
+
+### Windows Quirks
+
+Windows Phone 8.1 will fire `batterylow` event regardless of plugged state as it is not supported.
### Example
diff --git a/plugin.xml b/plugin.xml
index 63e72f4..4c7cff6 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -101,6 +101,15 @@
<source-file src="src/wp/Battery.cs" />
</platform>
+ <!-- windows -->
+ <platform name="windows">
+ <js-module src="src/windows/BatteryProxy.js" name="Battery">
+ <runs />
+ </js-module>
+
+ <framework src="src/windows/BatteryStatus.winmd" custom="true"/>
+ </platform>
+
<!-- tizen -->
<platform name="tizen">
<js-module src="src/tizen/BatteryStatusProxy.js" name="BatteryStatusProxy">
diff --git a/src/windows/BatteryProxy.js b/src/windows/BatteryProxy.js
new file mode 100644
index 0000000..b6644c0
--- /dev/null
+++ b/src/windows/BatteryProxy.js
@@ -0,0 +1,76 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+var stopped;
+
+function handleResponse(successCb, errorCb, jsonResponse) {
+ var info = JSON.parse(jsonResponse);
+
+ if (info.hasOwnProperty("exceptionMessage")) {
+ errorCb(info.exceptionMessage);
+ return;
+ }
+
+ successCb(info, { keepCallback: true });
+}
+
+var Battery = {
+ start: function (win, fail, args, env) {
+ stopped = false;
+ try {
+ if (!WinJS.Utilities.isPhone) {
+ fail("The operation is not supported by this platform.");
+ return;
+ }
+
+ function getBatteryStatus(success, error) {
+ handleResponse(success, error, BatteryStatus.BatteryStatus.start());
+ }
+
+ function getBatteryStatusLevelChangeEvent(success, error) {
+ return BatteryStatus.BatteryStatus.getBatteryStatusChangeEvent().done(function (result) {
+ if (stopped) {
+ return;
+ }
+
+ handleResponse(success, error, result);
+
+ setTimeout(function() { getBatteryStatusLevelChangeEvent(success, error); }, 0);
+ }, function(err) {
+ fail(err);
+ });
+ }
+
+ getBatteryStatus(win, fail);
+
+ getBatteryStatusLevelChangeEvent(win, fail);
+ } catch(e) {
+ fail(e);
+ }
+ },
+
+ stop: function () {
+ stopped = true;
+ BatteryStatus.BatteryStatus.stop();
+ }
+};
+
+require("cordova/exec/proxy").add("Battery", Battery);
diff --git a/src/windows/BatteryStatus.winmd b/src/windows/BatteryStatus.winmd
new file mode 100644
index 0000000..5000045
--- /dev/null
+++ b/src/windows/BatteryStatus.winmd
Binary files differ
diff --git a/src/windows/BatteryStatus/BatteryStatus.sln b/src/windows/BatteryStatus/BatteryStatus.sln
new file mode 100644
index 0000000..1b69524
--- /dev/null
+++ b/src/windows/BatteryStatus/BatteryStatus.sln
@@ -0,0 +1,22 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2013
+VisualStudioVersion = 12.0.30723.0
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BatteryStatus", "BatteryStatus\BatteryStatus.csproj", "{9749E0FB-CDCF-4D80-8953-AAB577B44234}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {9749E0FB-CDCF-4D80-8953-AAB577B44234}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {9749E0FB-CDCF-4D80-8953-AAB577B44234}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {9749E0FB-CDCF-4D80-8953-AAB577B44234}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {9749E0FB-CDCF-4D80-8953-AAB577B44234}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
diff --git a/src/windows/BatteryStatus/BatteryStatus/.gitignore b/src/windows/BatteryStatus/BatteryStatus/.gitignore
new file mode 100644
index 0000000..b84aa9d
--- /dev/null
+++ b/src/windows/BatteryStatus/BatteryStatus/.gitignore
@@ -0,0 +1,3 @@
+/bin/
+/obj/
+*.suo
\ No newline at end of file
diff --git a/src/windows/BatteryStatus/BatteryStatus/BatteryStatus.cs b/src/windows/BatteryStatus/BatteryStatus/BatteryStatus.cs
new file mode 100644
index 0000000..4887d76
--- /dev/null
+++ b/src/windows/BatteryStatus/BatteryStatus/BatteryStatus.cs
@@ -0,0 +1,90 @@
+using System;
+using System.IO;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Json;
+using System.Threading;
+using System.Threading.Tasks;
+using Windows.Foundation;
+using Windows.Phone.Devices.Power;
+
+namespace BatteryStatus
+{
+ public sealed class BatteryStatus
+ {
+ private static Battery battery = Battery.GetDefault();
+ private static TaskCompletionSource<string> levelCompletionSource = new TaskCompletionSource<string>();
+
+ public static string start()
+ {
+ battery.RemainingChargePercentChanged += BatteryOnRemainingChargePercentChanged;
+
+ return getBatteryStatus();
+ }
+
+ public static void stop()
+ {
+ battery.RemainingChargePercentChanged -= BatteryOnRemainingChargePercentChanged;
+ }
+
+ public static string getBatteryStatus()
+ {
+ try
+ {
+ return Serialize(typeof(BatteryInfo), new BatteryInfo
+ {
+ Level = battery.RemainingChargePercent
+ });
+ }
+ catch (Exception ex)
+ {
+ return Serialize(typeof(ExceptionInfo), new ExceptionInfo { Message = ex.Message });
+ }
+ }
+
+ public static IAsyncOperation<string> getBatteryStatusChangeEvent()
+ {
+ return GetBatteryStatusChangeEvent().AsAsyncOperation();
+ }
+
+ private static async Task<string> GetBatteryStatusChangeEvent()
+ {
+ levelCompletionSource = new TaskCompletionSource<string>();
+
+ return await levelCompletionSource.Task;
+ }
+
+ private static void BatteryOnRemainingChargePercentChanged(object sender, object o)
+ {
+ levelCompletionSource.SetResult(getBatteryStatus());
+ }
+
+ private static string Serialize(Type type, object obj)
+ {
+ using (var stream = new MemoryStream())
+ {
+ var jsonSer = new DataContractJsonSerializer(type);
+ jsonSer.WriteObject(stream, obj);
+ stream.Position = 0;
+ return new StreamReader(stream).ReadToEnd();
+ }
+ }
+
+ [DataContract]
+ private class BatteryInfo
+ {
+ [DataMember(Name = "level")]
+ public int Level;
+
+ // Not supported by native API
+ [DataMember(Name = "isPlugged")]
+ public string IsPlugged;
+ };
+
+ [DataContract]
+ private class ExceptionInfo
+ {
+ [DataMember(Name = "exceptionMessage")]
+ public string Message = string.Empty;
+ };
+ }
+}
diff --git a/src/windows/BatteryStatus/BatteryStatus/BatteryStatus.csproj b/src/windows/BatteryStatus/BatteryStatus/BatteryStatus.csproj
new file mode 100644
index 0000000..ef6cc65
--- /dev/null
+++ b/src/windows/BatteryStatus/BatteryStatus/BatteryStatus.csproj
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <MinimumVisualStudioVersion>12.0</MinimumVisualStudioVersion>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{9749E0FB-CDCF-4D80-8953-AAB577B44234}</ProjectGuid>
+ <OutputType>winmdobj</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>BatteryStatus</RootNamespace>
+ <AssemblyName>BatteryStatus</AssemblyName>
+ <DefaultLanguage>en-US</DefaultLanguage>
+ <FileAlignment>512</FileAlignment>
+ <ProjectTypeGuids>{76F1466A-8B6D-4E39-A767-685A06062A39};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <TargetPlatformIdentifier>WindowsPhoneApp</TargetPlatformIdentifier>
+ <TargetPlatformVersion>8.1</TargetPlatformVersion>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_PHONE_APP</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE;NETFX_CORE;WINDOWS_PHONE_APP</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <!-- A reference to the entire .NET Framework is automatically included -->
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="BatteryStatus.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
+ <PropertyGroup>
+ <PostBuildEvent>xcopy /Y /Q "$(TargetPath)" "$(SolutionDir).."</PostBuildEvent>
+ </PropertyGroup>
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
\ No newline at end of file
diff --git a/src/windows/BatteryStatus/BatteryStatus/Properties/AssemblyInfo.cs b/src/windows/BatteryStatus/BatteryStatus/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000..d3ebea5
--- /dev/null
+++ b/src/windows/BatteryStatus/BatteryStatus/Properties/AssemblyInfo.cs
@@ -0,0 +1,30 @@
+using System.Resources;
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("BatteryStatus")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("BatteryStatus")]
+[assembly: AssemblyCopyright("Copyright © 2014")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+[assembly: NeutralResourcesLanguage("en")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]