You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ripple.apache.org by ti...@apache.org on 2015/10/19 18:52:24 UTC

incubator-ripple git commit: RIPPLE-100 Fix battery status event emulation

Repository: incubator-ripple
Updated Branches:
  refs/heads/master 08af2c844 -> 1dacde265


RIPPLE-100 Fix battery status event emulation

Fixed the logic so that you get a batterycritical event when the battery status changes from above 5 to 5 or below, not just when the battery status changes to exactly 5.  Similarly for batterylow event.  Note that you must test for batterycritical before checking for batterylow, e.g. slider move from 25 to 2 causes batterycritical, not batterylow. Seemed like overkill to trigger both events at the same time.

This closes #74


Project: http://git-wip-us.apache.org/repos/asf/incubator-ripple/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ripple/commit/1dacde26
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ripple/tree/1dacde26
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ripple/diff/1dacde26

Branch: refs/heads/master
Commit: 1dacde26583715bdd513288b46e08ef9d5df5292
Parents: 08af2c8
Author: Julian Horn <ju...@intel.com>
Authored: Tue Sep 8 15:25:51 2015 -0400
Committer: Tim Barham <ti...@microsoft.com>
Committed: Mon Oct 19 09:52:08 2015 -0700

----------------------------------------------------------------------
 lib/client/ui/plugins/batteryStatus.js | 34 ++++++++++++++---------------
 1 file changed, 17 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ripple/blob/1dacde26/lib/client/ui/plugins/batteryStatus.js
----------------------------------------------------------------------
diff --git a/lib/client/ui/plugins/batteryStatus.js b/lib/client/ui/plugins/batteryStatus.js
index f543900..5978b2e 100644
--- a/lib/client/ui/plugins/batteryStatus.js
+++ b/lib/client/ui/plugins/batteryStatus.js
@@ -21,6 +21,7 @@
 
 var constants = ripple('constants'),
     db = ripple('db'),
+    lastLevel,
     batteryLevel = document.getElementById(constants.BATTERY_STATUS.LEVEL_VALUE),
     batteryLevelLabel = document.getElementById(constants.BATTERY_STATUS.LEVEL_LABEL),
     isPlugged = document.getElementById(constants.BATTERY_STATUS.IS_PLUGGED_CHECKBOX);
@@ -37,6 +38,7 @@ function _saveStatus(status) {
     if (status) {
         db.save(constants.BATTERY_STATUS.BATTERY_STATUS_KEY, status.level);
         db.save(constants.BATTERY_STATUS.IS_PLUGGED_KEY, status.isPlugged);
+        lastLevel = status.level;
     }
 }
 
@@ -48,29 +50,31 @@ function _updateUI(status) {
     }
 }
 
-function _fireBatteryEvent(status) {
+function _fireBatteryEvent(status, previousLevel) {
     var win = ripple('emulatorBridge').window();
 
     if (win && win.cordova) {
         // Do nothing if we aren't emulating a valid Cordova application
         win.cordova.fireWindowEvent("batterystatus", status);
 
-        var level = parseInt(status.level);
-        if (level === 20 || level === 5) {
-            if (level === 20) {
-                win.cordova.fireWindowEvent("batterylow", status);
-            } else {
-                win.cordova.fireWindowEvent("batterycritical", status);
-            }
+        var newLevel = parseInt(status.level);
+        var oldLevel = parseInt(previousLevel);
+        // Trigger batterycritical/batterylow event if level drops below threshold
+        // Must test threshold values in ascending order for this to work right
+        if ((oldLevel > 5) && (newLevel <= 5)) {
+            win.cordova.fireWindowEvent("batterycritical", status);
+        } else if ((oldLevel > 20) && (newLevel <= 20)) {
+            win.cordova.fireWindowEvent("batterylow", status);
         }
     }
 }
 
 function _processStatusChanged() {
     var status = _getCurrentStatus();
+    var previousLevel = lastLevel; // Read lastLevel BEFORE calling _saveStatus
     _saveStatus(status);
     _updateUI(status);
-    _fireBatteryEvent(status);
+    _fireBatteryEvent(status, previousLevel);
 }
 
 module.exports = {
@@ -81,18 +85,14 @@ module.exports = {
     },
 
     initialize: function() {
-        jQuery("#" + constants.BATTERY_STATUS.LEVEL_VALUE).bind("mouseup", function() {
-            _processStatusChanged();
-        });
-
-        jQuery("#" + constants.BATTERY_STATUS.IS_PLUGGED_CHECKBOX).bind("click", function() {
-            _processStatusChanged();
-        });
+        jQuery("#" + constants.BATTERY_STATUS.LEVEL_VALUE).unbind("mouseup").bind("mouseup", _processStatusChanged);
+        jQuery("#" + constants.BATTERY_STATUS.IS_PLUGGED_CHECKBOX).unbind("click").bind("click", _processStatusChanged);
 
         var status = {
             level: db.retrieve(constants.BATTERY_STATUS.BATTERY_STATUS_KEY) || 100,
-            isPlugged: db.retrieve(constants.BATTERY_STATUS.IS_PLUGGED_KEY) || false,
+            isPlugged: db.retrieve(constants.BATTERY_STATUS.IS_PLUGGED_KEY) || false
         };
+        lastLevel = status.level;
 
         _updateUI(status);
     }