This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
common.js
234 lines (203 loc) · 6.75 KB
/
common.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
'use strict';
// Checks for a SIM change
function checkSIMChange(callback) {
asyncStorage.getItem('lastSIM', function _compareWithCurrent(lastSIM) {
var currentSIM = window.navigator.mozMobileConnection.iccInfo.iccid;
if (lastSIM !== currentSIM) {
debug('SIM change!');
MindGap.updateTagList(currentSIM);
}
ConfigManager.requestSettings(function _onSettings(settings) {
if (settings.nextReset) {
setNextReset(settings.nextReset, callback);
return;
}
if (callback) {
callback();
}
});
});
}
function checkDataUsageNotification(settings, usage, callback) {
var proxy = document.getElementById('message-handler').contentWindow;
var f = proxy ? proxy.checkDataUsageNotification :
window.checkDataUsageNotification;
return f(settings, usage, callback);
}
// Waits for DOMContentLoaded and messagehandlerready, then call the callback
function waitForDOMAndMessageHandler(window, callback) {
var remainingSteps = 2;
function checkReady(evt) {
debug(evt.type, 'event received!');
remainingSteps--;
// Once all events are received, execute the callback
if (!remainingSteps) {
window.removeEventListener('DOMContentLoaded', checkReady);
window.removeEventListener('messagehandlerready', checkReady);
debug('DOMContentLoaded and messagehandlerready received. Starting');
callback();
}
}
window.addEventListener('DOMContentLoaded', checkReady);
window.addEventListener('messagehandlerready', checkReady);
}
function addAlarmTimeout(type, delay) {
var proxy = document.getElementById('message-handler').contentWindow;
return proxy.addAlarmTimeout(type, delay);
}
function setNextReset(when, callback) {
var proxy = document.getElementById('message-handler');
return proxy ? proxy.contentWindow.setNextReset(when, callback) :
window.setNextReset(when, callback);
}
function getTopUpTimeout(callback) {
var proxy = document.getElementById('message-handler');
return proxy ? proxy.contentWindow.getTopUpTimeout(callback) :
window.getTopUpTimeout(callback);
}
// Next automatic reset date based on user preferences
function updateNextReset(trackingPeriod, value, callback) {
if (trackingPeriod === 'never') {
setNextReset(null, callback); // remove any alarm
return;
}
var nextReset, today = new Date();
// Recalculate with month period
if (trackingPeriod === 'monthly') {
var month, year;
var monthday = parseInt(value, 10);
month = today.getMonth();
year = today.getFullYear();
if (today.getDate() >= monthday) {
month = (month + 1) % 12;
if (month === 0) {
year++;
}
}
nextReset = new Date(year, month, monthday);
// Recalculate with week period
} else if (trackingPeriod === 'weekly') {
var oneDay = 24 * 60 * 60 * 1000;
var weekday = parseInt(value, 10);
var daysToTarget = weekday - today.getDay();
if (daysToTarget <= 0) {
daysToTarget = 7 + daysToTarget;
}
nextReset = new Date();
nextReset.setTime(nextReset.getTime() + oneDay * daysToTarget);
toMidnight(nextReset);
}
// remove oldAlarm and set the new one
setNextReset(nextReset, callback);
}
function resetData() {
// Sets the fixing value for the current SIM
asyncStorage.getItem('dataUsageTags', function _updateTags(tags) {
if (!tags || !tags.length) {
console.error('dataUsageTags does not exists!');
return;
}
// Get current mobile data
var now = new Date();
var mobileRequest = window.navigator.mozNetworkStats.getNetworkStats({
start: now,
end: now,
connectionType: 'mobile'
});
mobileRequest.onsuccess = function _onMobileForToday() {
var data = mobileRequest.result.data;
debug('Data length should be 1 and it is', data.length);
var currentDataUsage = 0;
if (data[0].rxBytes) {
currentDataUsage += data[0].rxBytes;
}
if (data[0].txBytes) {
currentDataUsage += data[0].txBytes;
}
// Adds the fixing
var tag = tags[tags.length - 1];
tag.fixing.push([now, currentDataUsage]);
// Remove the previous ones
for (var i = tags.length - 2; i >= 0; i--) {
var ctag = tags[i];
if (ctag.sim === tag.sim) {
tags.splice(i, 1);
}
}
debug('After reset', tags);
asyncStorage.setItem('dataUsageTags', tags, function _done() {
ConfigManager.setOption({ lastDataReset: now });
});
};
var wifiRequest = window.navigator.mozNetworkStats.getNetworkStats({
start: now,
end: now,
connectionType: 'wifi'
});
wifiRequest.onsuccess = function _onWiFiForToday() {
var data = wifiRequest.result.data;
debug('Data length should be 1 and it is', data.length);
var currentWifiUsage = 0;
if (data[0].rxBytes) {
currentWifiUsage += data[0].rxBytes;
}
if (data[0].txBytes) {
currentWifiUsage += data[0].txBytes;
}
ConfigManager.setOption({ wifiFixing: currentWifiUsage });
};
});
}
function resetTelephony() {
ConfigManager.setOption({
lastTelephonyReset: new Date(),
lastTelephonyActivity: {
calltime: 0,
smscount: 0,
timestamp: new Date()
}
});
}
function resetAll() {
resetData();
resetTelephony();
}
function getDataLimit(settings) {
var multiplier = (settings.dataLimitUnit === 'MB') ?
1000000 : 1000000000;
return settings.dataLimitValue * multiplier;
}
function formatTimeHTML(timestampA, timestampB) {
// No interval
if (typeof timestampB === 'undefined') {
return '<time>' + formatTime(timestampA) + '</time>';
}
// Same day case
var dateA = new Date(timestampA);
var dateB = new Date(timestampB);
if (dateA.getFullYear() === dateB.getFullYear() &&
dateA.getMonth() === dateB.getMonth() &&
dateA.getDay() === dateB.getDay()) {
return formatTimeHTML(timestampB);
}
// Interval
return '<time>' + formatTime(timestampA) + '</time> – ' +
'<time>' + formatTime(timestampB) + '</time>';
}
function localizeWeekdaySelector(selector) {
var weekStartsOnMonday =
!!parseInt(navigator.mozL10n.get('weekStartsOnMonday'), 10);
debug('Week starts on monday?', weekStartsOnMonday);
var monday = selector.querySelector('.monday');
var sunday = selector.querySelector('.sunday');
var list = monday.parentNode;
if (weekStartsOnMonday) {
debug('Monday, Tuesday...');
list.insertBefore(monday, list.childNodes[0]); // monday is the first
list.appendChild(sunday); // sunday is the last
} else {
debug('Sunday, Monday...');
list.insertBefore(sunday, list.childNodes[0]); // sunday is the first
list.insertBefore(monday, sunday.nextSibling); // monday is the second
}
}