Bug 1043920 - Reader mode (ambient light detection) prevents device from sleeping r=margaret

This commit is contained in:
Mark Finkle 2014-07-28 17:16:30 -04:00
Родитель 06e01b3bd0
Коммит 0d3f68895a
1 изменённых файлов: 33 добавлений и 11 удалений

Просмотреть файл

@ -61,6 +61,8 @@ let AboutReader = function(doc, win) {
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));
@ -271,6 +273,10 @@ AboutReader.prototype = {
this._handleDeviceLight(aEvent.value);
break;
case "visibilitychange":
this._handleVisibilityChange();
break;
case "unload":
Services.obs.removeObserver(this, "Reader:Add");
Services.obs.removeObserver(this, "Reader:Remove");
@ -399,6 +405,29 @@ AboutReader.prototype = {
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;
@ -419,7 +448,8 @@ AboutReader.prototype = {
},
_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;
@ -434,16 +464,8 @@ AboutReader.prototype = {
// 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);
},