Bug 1043920 - Reader mode (ambient light detection) prevents device from sleeping r=margaret a=sylvestre
authorMark Finkle <mfinkle@mozilla.com>
Mon, 28 Jul 2014 17:16:30 -0400
changeset 208222 a59d3af0c000
parent 208221 c110771b033f
child 208223 faf3b10d4868
push id3780
push usermfinkle@mozilla.com
push date2014-08-04 14:14 +0000
treeherdermozilla-beta@a59d3af0c000 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret, sylvestre
bugs1043920
milestone32.0
Bug 1043920 - Reader mode (ambient light detection) prevents device from sleeping r=margaret a=sylvestre
mobile/android/chrome/content/aboutReader.js
--- a/mobile/android/chrome/content/aboutReader.js
+++ b/mobile/android/chrome/content/aboutReader.js
@@ -55,16 +55,18 @@ let AboutReader = function(doc, win) {
   body.addEventListener("touchstart", this, false);
   body.addEventListener("click", this, false);
 
   win.addEventListener("unload", this, false);
   win.addEventListener("scroll", this, false);
   win.addEventListener("popstate", this, false);
   win.addEventListener("resize", this, false);
 
+  doc.addEventListener("visibilitychange", this, false);
+
   this._setupAllDropdowns();
   this._setupButton("toggle-button", this._onReaderToggle.bind(this));
   this._setupButton("share-button", this._onShare.bind(this));
 
   let colorSchemeOptions = [
     { name: gStrings.GetStringFromName("aboutReader.colorSchemeDark"),
       value: "dark"},
     { name: gStrings.GetStringFromName("aboutReader.colorSchemeLight"),
@@ -252,16 +254,20 @@ AboutReader.prototype = {
       case "resize":
         this._updateImageMargins();
         break;
 
       case "devicelight":
         this._handleDeviceLight(aEvent.value);
         break;
 
+      case "visibilitychange":
+        this._handleVisibilityChange();
+        break;
+
       case "unload":
         Services.obs.removeObserver(this, "Reader:Add");
         Services.obs.removeObserver(this, "Reader:Remove");
         Services.obs.removeObserver(this, "Reader:ListStatusReturn");
         break;
     }
   },
 
@@ -368,16 +374,39 @@ AboutReader.prototype = {
 
     this._updateColorScheme(averageLuxValue);
     // Pop the oldest value off the array.
     let oldLux = this._luxValues.pop();
     // Subtract oldLux since it has been discarded from the array.
     this._totalLux -= oldLux;
   },
 
+  _handleVisibilityChange: function Reader_handleVisibilityChange() {
+    let colorScheme = Services.prefs.getCharPref("reader.color_scheme");
+    if (colorScheme != "auto") {
+      return;
+    }
+
+    // Turn off the ambient light sensor if the page is hidden
+    this._enableAmbientLighting(!this._doc.hidden);
+  },
+
+  // Setup or teardown the ambient light tracking system.
+  _enableAmbientLighting: function Reader_enableAmbientLighting(enable) {
+    if (enable) {
+      this._win.addEventListener("devicelight", this, false);
+      this._luxValues = [];
+      this._totalLux = 0;
+    } else {
+      this._win.removeEventListener("devicelight", this, false);
+      delete this._luxValues;
+      delete this._totalLux;
+    }
+  },
+
   _updateColorScheme: function Reader_updateColorScheme(luxValue) {
     // Upper bound value for "dark" color scheme beyond which it changes to "light".
     let upperBoundDark = 50;
     // Lower bound value for "light" color scheme beyond which it changes to "dark".
     let lowerBoundLight = 10;
     // Threshold for color scheme change.
     let colorChangeThreshold = 20;
 
@@ -388,41 +417,34 @@ AboutReader.prototype = {
 
     if (luxValue < colorChangeThreshold)
       this._setColorScheme("dark");
     else
       this._setColorScheme("light");
   },
 
   _setColorScheme: function Reader_setColorScheme(newColorScheme) {
-    if (this._colorScheme === newColorScheme)
+    // "auto" is not a real color scheme
+    if (this._colorScheme === newColorScheme || newColorScheme === "auto")
       return;
 
     let bodyClasses = this._doc.body.classList;
 
     if (this._colorScheme)
       bodyClasses.remove(this._colorScheme);
 
     this._colorScheme = newColorScheme;
     bodyClasses.add(this._colorScheme);
   },
 
   // Pref values include "dark", "light", and "auto", which automatically switches
   // between light and dark color schemes based on the ambient light level.
   _setColorSchemePref: function Reader_setColorSchemePref(colorSchemePref) {
-    if (colorSchemePref === "auto") {
-      this._win.addEventListener("devicelight", this, false);
-      this._luxValues = [];
-      this._totalLux = 0;
-    } else {
-      this._win.removeEventListener("devicelight", this, false);
-      this._setColorScheme(colorSchemePref);
-      delete this._luxValues;
-      delete this._totalLux;
-    }
+    this._enableAmbientLighting(colorSchemePref === "auto");
+    this._setColorScheme(colorSchemePref);
 
     Services.prefs.setCharPref("reader.color_scheme", colorSchemePref);
   },
 
   _setFontType: function Reader_setFontType(newFontType) {
     if (this._fontType === newFontType)
       return;