Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
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
- Loading branch information
Showing
9 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
BIN
+9 KB
src/windows/BatteryStatus.winmd
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/bin/ | ||
/obj/ | ||
*.suo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")] |