Created
November 13, 2014 12:15
-
-
Save daserge/881f03bcb5d0b451804b to your computer and use it in GitHub Desktop.
Cordova Windows plugin: Winmd + event subscription in JS
This file contains hidden or 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
| cordova.define("org.apache.cordova.battery-status.Battery", function(require, exports, module) { /* | |
| * | |
| * 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 eventHandlerWrapper; | |
| 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 === true) { | |
| function getBatteryStatus(success, error) { | |
| handleResponse(success, error, BatteryStatus.BatteryStatus.getBatteryStatus()); | |
| } | |
| getBatteryStatus(win, fail); | |
| eventHandlerWrapper = function(json) { | |
| handleResponse(win, fail, json); | |
| }; | |
| // Issue: This event callback is not being called | |
| BatteryStatus.BatteryStatus.addEventListener("remainingchargepercentchanged", eventHandlerWrapper); | |
| } else { | |
| fail("The operation is unsupported by this platform."); | |
| } | |
| } catch(e) { | |
| fail(e); | |
| } | |
| }, | |
| stop: function () { | |
| stopped = true; | |
| BatteryStatus.BatteryStatus.removeEventListener("remainingchargepercentchanged", eventHandlerWrapper); | |
| } | |
| }; | |
| require("cordova/exec/proxy").add("Battery", Battery); | |
| }); |
This file contains hidden or 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
| using System; | |
| using System.IO; | |
| using System.Runtime.Serialization; | |
| using System.Runtime.Serialization.Json; | |
| using Windows.Phone.Devices.Power; | |
| namespace BatteryStatus | |
| { | |
| public sealed class BatteryStatus | |
| { | |
| private static Battery battery = Battery.GetDefault(); | |
| static BatteryStatus() | |
| { | |
| battery.RemainingChargePercentChanged += BatteryOnRemainingChargePercentChanged; | |
| } | |
| public static event EventHandler<string> RemainingChargePercentChanged; | |
| 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 }); | |
| } | |
| } | |
| private static void BatteryOnRemainingChargePercentChanged(object sender, object o) | |
| { | |
| if(RemainingChargePercentChanged != null) | |
| RemainingChargePercentChanged(null, 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; | |
| }; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment