This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
power_save.js
187 lines (155 loc) · 4.84 KB
/
power_save.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'use strict';
/* global MozActivity, NotificationHelper, SettingsListener */
(function(exports) {
function PowerSave(batteryOverlay) {
this.batteryOverlay = batteryOverlay;
}
PowerSave.prototype = {
_powerSaveResume: {},
_powerSaveEnabled: false,
_states: {
'wifi.enabled': false,
'ril.data.enabled': false,
'bluetooth.enabled': false,
'geolocation.enabled': false
},
_powerSaveEnabledLock: false,
start: function() {
SettingsListener.observe('powersave.enabled', false, value => {
var enabled = value;
if (enabled) {
this.enablePowerSave();
} else {
this.disablePowerSave();
}
this._powerSaveEnabled = enabled;
});
SettingsListener.observe('powersave.threshold', -1, value => {
this.doCheckThreshold(value);
});
function getState(state, value) {
/* jshint validthis: true */
this._states[state] = value;
}
// Monitor the states of various modules
for (var j in this._states) {
SettingsListener.observe(j, true, getState.bind(this, j));
}
},
// XXX Break down obj keys in a for each loop because mozSettings
// does not currently supports multiple keys in one set()
// https://bugzilla.mozilla.org/show_bug.cgi?id=779381
setMozSettings: function(keypairs) {
var setlock = SettingsListener.getSettingsLock();
for (var key in keypairs) {
// not set bluetooth key because we'll handle it separately
// for API compatibility
if ('bluetooth.enabled' !== key) {
var obj = {};
obj[key] = keypairs[key];
setlock.set(obj);
}
}
},
enablePowerSave: function() {
// Keep the original states of various modules
for (var j in this._states) {
this._powerSaveResume[j] = this._states[j];
}
var settingsToSet = {
// Turn off Wifi
'wifi.enabled': false,
// Turn off Data
'ril.data.enabled': false,
// Turn off Geolocation
'geolocation.enabled': false
};
this.setMozSettings(settingsToSet);
// Turn off Bluetooth
window.dispatchEvent(new CustomEvent('request-disable-bluetooth'));
this._powerSaveEnabledLock = false;
},
disablePowerSave: function() {
var settingsToSet = {};
for (var state in this._powerSaveResume) {
if (this._powerSaveResume[state] === true) {
settingsToSet[state] = true;
}
}
if (this._powerSaveResume['bluetooth.enabled'] === true) {
// Turn on Bluetooth
window.dispatchEvent(new CustomEvent('request-enable-bluetooth'));
}
this.setMozSettings(settingsToSet);
},
showPowerSavingNotification: function() {
var clickCB = function() {
var activityRequest = new MozActivity({
name: 'configure',
data: {
target: 'device',
section: 'battery'
}
});
activityRequest.onsuccess = () => {};
};
NotificationHelper.send('notification-powersaving-mode-on-title', {
'bodyL10n': 'notification-powersaving-mode-on-description',
'icon': 'style/icons/Power_saving_mode.png',
'mozbehavior': {
showOnlyOnce: true
}
}).then(function(notification) {
notification.addEventListener('click', clickCB);
});
},
checkThreshold: function() {
var key = 'powersave.threshold';
var lock = navigator.mozSettings.createLock();
var req = lock.get(key);
req.onerror = function(e) {
console.error('Error while quering setting ' + key + ': ' + e);
};
req.onsuccess = (function() {
this.doCheckThreshold(req.result[key] || -1);
}).bind(this);
},
doCheckThreshold: function(value) {
var battery = this.batteryOverlay._battery;
// If 'turn on automatically' is set to 'never', don't change the
// power saving state
if (value == -1) {
return;
}
if (battery.level <= value && !this._powerSaveEnabled) {
this.setMozSettings({
'powersave.enabled': true
});
if (!this._powerSaveEnabledLock) {
this.showPowerSavingNotification();
this._powerSaveEnabledLock = true;
}
return;
}
if (battery.level > value && this._powerSaveEnabled) {
this.setMozSettings({
'powersave.enabled': false
});
return;
}
},
onBatteryChange: function() {
var battery = this.batteryOverlay._battery;
if (battery.charging) {
if (this._powerSaveEnabled) {
this.setMozSettings({
'powersave.enabled': false
});
}
return;
}
this.checkThreshold();
}
};
exports.PowerSave = PowerSave;
}(window));