This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
ring_view.js
316 lines (289 loc) · 8.51 KB
/
ring_view.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
define(function(require) {
'use strict';
var Utils = require('utils');
var mozL10n = require('l10n');
var _ = mozL10n.get;
var messageHandlerMapping;
function RingView(opts = {}) {
Utils.extend(this, {
ringtonePlayer: null,
vibrateInterval: null,
screenLock: null,
message: {},
started: false,
notificationOptions: {
sound: 'ac_classic_clock_alarm.opus',
vibrate: true,
label: _('alarm'),
time: new Date()
},
stopActions: [],
callback: null
}, opts);
window.addEventListener('message',
this.handleMessage.bind(this), false);
}
RingView.singleton = Utils.singleton(RingView);
RingView.prototype = {};
RingView.prototype.handleMessage = function rv_handleMessage(ev) {
Utils.safeWakeLock({type: 'cpu', timeoutMs: 5000}, function(done) {
var err = [];
var data = ev.data;
var messageTypes = (data.type || '') .split(' ');
var gen = Utils.async.generator(done);
var lp = gen();
for (var type of messageTypes) {
var cb = gen();
try { // Ensure all handlers get called
if (typeof messageHandlerMapping[type] === 'function') {
messageHandlerMapping[type].call(this, ev, cb);
}
} catch (e) {
err.push(e);
cb();
}
}
lp();
if (err.length !== 0) {
err.forEach(function(e) {
console.error('Error in onring handler:', e.message, '\n', e.stack);
});
done();
}
}.bind(this));
};
function isVisibleWorkaround(callback) {
/*jshint validthis:true */
// See https://bugzilla.mozilla.org/show_bug.cgi?id=810431
document.addEventListener('visibilitychange', this);
if (!document.hidden) {
callback();
} else {
// The setTimeout() is used to workaround
// https://bugzilla.mozilla.org/show_bug.cgi?id=810431
// The workaround is used in screen off mode.
// hidden will be true in init() state.
window.setTimeout(function rv_checkHidden() {
// If hidden is true in init state,
// it means that the incoming call happens before the alarm.
// We should just put a "silent" alarm screen
// underneath the oncall screen
if (!document.hidden) {
callback();
}
// Our final chance is to rely on visibilitychange event handler.
}.bind(this), 0);
}
}
RingView.prototype.alarm = function rv_alarm(ev, wakelock) {
this.type = 'alarm';
this.alarm = ev.data.alarm;
var date;
try {
date = new Date(ev.data.date);
} catch (err) {
date = new Date();
}
this.snoozeButton.addEventListener('click', this);
this.closeButton.addEventListener('click', this);
this.notificationOptions = {
sound: ev.data.alarm.sound,
// TODO: normalize this throughout all layers
vibrate: ev.data.alarm.vibrate && ev.data.alarm.vibrate !== '0',
label: ev.data.alarm.label,
time: date
};
this.stopActions.push(function() {
window.opener.postMessage({
type: 'close-' + this.type
}, window.location.origin);
this.type = null;
this.snoozeButton.removeEventListener('click', this);
this.closeButton.removeEventListener('click', this);
this.notificationOptions = {};
}.bind(this));
isVisibleWorkaround.call(this, function() {
mozL10n.ready(function rv_waitLocalized() {
this.startNotify();
this.ringDisplay.dataset.ringType = 'alarm';
wakelock();
}.bind(this));
}.bind(this));
};
RingView.prototype.timer = function rv_timer(ev, wakelock) {
this.type = 'timer';
this.closeButton.addEventListener('click', this);
this.notificationOptions = {
sound: ev.data.timer.sound,
// TODO: normalize this throughout all layers
vibrate: ev.data.timer.vibrate && ev.data.timer.vibrate !== '0',
label: _('timer'),
time: new Date(ev.data.timer.startTime + ev.data.timer.duration)
};
this.stopActions.push(function() {
window.opener.postMessage({
type: 'close-' + this.type
}, window.location.origin);
this.type = null;
this.closeButton.removeEventListener('click', this);
this.notificationOptions = {};
}.bind(this));
isVisibleWorkaround.call(this, function() {
this.startNotify();
this.ringDisplay.dataset.ringType = 'timer';
wakelock();
}.bind(this));
};
RingView.prototype.display = function rv_display() {
var label = this.notificationOptions.label;
this.ringLabel.textContent = (label === '') ? _('alarm') : label;
var time = Utils.getLocaleTime(this.notificationOptions.time);
this.time.textContent = time.t;
this.hourState.textContent = time.p;
};
RingView.prototype.ring = function rv_ring(state) {
if (state) {
if (this.notificationOptions.sound) {
var ringtonePlayer = this.ringtonePlayer = new Audio();
ringtonePlayer.addEventListener('mozinterruptbegin', this);
ringtonePlayer.mozAudioChannelType = 'alarm';
ringtonePlayer.loop = true;
ringtonePlayer.src = 'shared/resources/media/alarms/' +
this.notificationOptions.sound;
ringtonePlayer.play();
}
} else if (this.ringtonePlayer) {
this.ringtonePlayer.pause();
}
};
RingView.prototype.vibrate = function rv_vibrate(state) {
if (state) {
if ('vibrate' in navigator) {
clearInterval(this.vibrateInterval);
// TODO: this should vibrate immediately, not after 2s
this.vibrateInterval = window.setInterval(function vibrate() {
navigator.vibrate([1000]);
}, 2000);
}
} else {
clearInterval(this.vibrateInterval);
this.vibrateInterval = null;
}
};
RingView.prototype.startNotify = function rv_startNotify(timeout) {
timeout = timeout || 60000 * 10;
// Ensure called only once.
if (this.started) {
return;
}
var unique = {};
Utils.safeWakeLock({type: 'screen', timeoutMs: timeout + 5000},
function(done) {
this.started = unique;
this.wakelock = done;
this.display();
this.ring(true);
this.vibrate(this.notificationOptions.vibrate);
if (document.hidden) {
window.focus();
}
setTimeout(function rv_clearVibration() {
if (this.started === unique) {
this.vibrate(false);
this.ring(false);
}
done();
}.bind(this), timeout);
}.bind(this));
};
RingView.prototype.stopNotify = function rv_stopNotify(temporary, done) {
this.started = null;
this.ring(false);
this.vibrate(false);
if (typeof this.wakelock === 'function') {
this.wakelock();
}
this.wakelock = null;
while (!temporary) {
var el = this.stopActions.shift();
if (el) {
if (typeof el === 'function') {
el();
}
} else {
break;
}
}
window.opener.postMessage({
type: 'ringer',
status: 'READY'
}, window.location.origin);
done && done();
};
var domEventMap = {};
RingView.prototype.handleEvent = function rv_handleEvent(evt) {
try {
domEventMap[evt.type].call(this, evt);
} catch (err) {
console.error('Error handling DOM event', evt);
throw err;
}
};
RingView.prototype.onVisibilityChange = domEventMap.visibilitychange =
function rv_onVisibilityChange(evt) {
// There's chance to miss the hidden state when initiated,
// before setVisible take effects, there may be a latency.
if (!document.hidden) {
this.startNotify();
}
};
RingView.prototype.onMozInterruptBegin = domEventMap.mozinterruptbegin =
function rv_onMozInterruptBegin(evt) {
// Only ringer/telephony channel audio could trigger 'mozinterruptbegin'
// event on the 'alarm' channel audio element.
// If the incoming call happens after the alarm rings,
// we need to close ourselves.
this.stopNotify(true);
};
RingView.prototype.onClick = domEventMap.click =
function rv_onClick(evt) {
var input = evt.target;
if (!input) {
return;
}
switch (input.id) {
case 'ring-button-snooze':
this.stopNotify();
window.opener.postMessage({
type: 'scheduleSnooze',
id: this.alarm.id
}, window.location.origin);
window.close();
break;
case 'ring-button-stop':
this.stopNotify();
window.close();
break;
}
};
var domMap = {
time: '#ring-clock-time',
hourState: '#ring-clock-hour24-state',
ringLabel: '#ring-label',
snoozeButton: '#ring-button-snooze',
closeButton: '#ring-button-stop',
ringDisplay: '.ring-display'
};
for (var i in domMap) {
Object.defineProperty(RingView.prototype, i,
Utils.memoizedDomPropertyDescriptor(domMap[i]));
}
messageHandlerMapping = {
timer: RingView.prototype.timer,
alarm: RingView.prototype.alarm,
stop: function(msg, done) {
RingView.prototype.stopNotify.call(this, false, done);
}
};
return RingView;
});