battery.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. /**
  22. * This class contains information about the current battery status.
  23. * @constructor
  24. */
  25. var cordova = require('cordova');
  26. var exec = require('cordova/exec');
  27. var STATUS_CRITICAL = 5;
  28. var STATUS_LOW = 20;
  29. var Battery = function () {
  30. this._level = null;
  31. this._isPlugged = null;
  32. // Create new event handlers on the window (returns a channel instance)
  33. this.channels = {
  34. batterystatus: cordova.addWindowEventHandler('batterystatus'),
  35. batterylow: cordova.addWindowEventHandler('batterylow'),
  36. batterycritical: cordova.addWindowEventHandler('batterycritical')
  37. };
  38. for (var key in this.channels) {
  39. this.channels[key].onHasSubscribersChange = Battery.onHasSubscribersChange;
  40. }
  41. };
  42. function handlers () {
  43. return battery.channels.batterystatus.numHandlers +
  44. battery.channels.batterylow.numHandlers +
  45. battery.channels.batterycritical.numHandlers;
  46. }
  47. /**
  48. * Event handlers for when callbacks get registered for the battery.
  49. * Keep track of how many handlers we have so we can start and stop the native battery listener
  50. * appropriately (and hopefully save on battery life!).
  51. */
  52. Battery.onHasSubscribersChange = function () {
  53. // If we just registered the first handler, make sure native listener is started.
  54. if (this.numHandlers === 1 && handlers() === 1) {
  55. exec(battery._status, battery._error, 'Battery', 'start', []);
  56. } else if (handlers() === 0) {
  57. exec(null, null, 'Battery', 'stop', []);
  58. }
  59. };
  60. /**
  61. * Callback for battery status
  62. *
  63. * @param {Object} info keys: level, isPlugged
  64. */
  65. Battery.prototype._status = function (info) {
  66. if (info) {
  67. if (battery._level !== info.level || battery._isPlugged !== info.isPlugged) {
  68. if (info.level === null && battery._level !== null) {
  69. return; // special case where callback is called because we stopped listening to the native side.
  70. }
  71. // Something changed. Fire batterystatus event
  72. cordova.fireWindowEvent('batterystatus', info);
  73. if (!info.isPlugged) { // do not fire low/critical if we are charging. issue: CB-4520
  74. // note the following are NOT exact checks, as we want to catch a transition from
  75. // above the threshold to below. issue: CB-4519
  76. if (battery._level > STATUS_CRITICAL && info.level <= STATUS_CRITICAL) {
  77. // Fire critical battery event
  78. cordova.fireWindowEvent('batterycritical', info);
  79. } else if (battery._level > STATUS_LOW && info.level <= STATUS_LOW) {
  80. // Fire low battery event
  81. cordova.fireWindowEvent('batterylow', info);
  82. }
  83. }
  84. battery._level = info.level;
  85. battery._isPlugged = info.isPlugged;
  86. }
  87. }
  88. };
  89. /**
  90. * Error callback for battery start
  91. */
  92. Battery.prototype._error = function (e) {
  93. console.log('Error initializing Battery: ' + e);
  94. };
  95. var battery = new Battery(); // jshint ignore:line
  96. module.exports = battery;