Repository navigation
Expand file tree
/
Copy pathactive_alarm.js
More file actions
112 lines (100 loc) · 3.39 KB
/
Copy pathactive_alarm.js
File metadata and controls
112 lines (100 loc) · 3.39 KB
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
define(function(require) {
'use strict';
var AlarmManager = require('alarm_manager');
var AlarmList = require('alarm_list');
var AlarmsDB = require('alarmsdb');
var Utils = require('utils');
var ActiveAlarm = {
/*
* We maintain an alarm's life cycle immediately when the alarm goes off.
* If user click the snooze button when the alarm goes off,
* we request a snooze alarm with snoozeAlarmId immediately.
*
* If multiple alarms goes off in a period of time(even if in the same time),
* we always stop the previous notification and handle it by its setting.
* Such as following case:
* An once alarm should be turned off.
* A repeat alarm should be requested its next alarm.
* A snooze alarm should be turned off.
*/
firedAlarm: null,
message: null,
childwindow: null,
init: function am_init() {
navigator.mozSetMessageHandler('alarm', this.handler.bind(this));
AlarmManager.updateAlarmStatusBar();
},
handler: function aac_handler(message) {
// Set a watchdog to avoid locking the CPU wake lock too long,
// because it'd exhaust the battery quickly which is very bad.
// This could probably happen if the app failed to launch or
// handle the alarm message due to any unexpected reasons.
Utils.safeCpuLock(30000, function(done) {
// receive and parse the alarm id from the message
var id = message.data.id;
var type = message.data.type;
// Unlock the CPU when these functions have been called
var finalizer = Utils.async.namedParallel([
'onReschedule',
'onReceivedAlarm'
], function(err) {
AlarmList.refresh();
AlarmManager.updateAlarmStatusBar();
done();
});
// If previous active alarm is showing,
// turn it off and stop its notification
if (this.childwindow !== null &&
typeof this.childwindow !== 'undefined' &&
!this.childwindow.closed) {
if (this.childwindow.RingView) {
this.childwindow.RingView.stopAlarmNotification();
}
}
AlarmsDB.getAlarm(id, function aac_gotAlarm(err, alarm) {
if (err) {
done();
return;
}
this.firedAlarm = alarm;
if (type === 'normal') {
alarm.schedule({
type: 'normal',
<
56E9
/div> first: false
}, alarm.saveCallback(finalizer.onReschedule));
} else {
alarm.cancel('snooze');
alarm.save(finalizer.onReschedule);
}
// prepare to pop out attention screen, ring the ringtone, vibrate
this.firedAlarm = alarm;
this.message = message;
var protocol = window.location.protocol;
var host = window.location.host;
this.childwindow =
window.open(protocol + '//' + host + '/onring.html',
'ring_screen', 'attention');
finalizer.onReceivedAlarm();
}.bind(this));
}.bind(this));
},
snoozeHandler: function aac_snoozeHandler() {
Utils.safeCpuLock(30000, function(done) {
var id = this.firedAlarm.id;
AlarmsDB.getAlarm(id, function aac_gotAlarm(err, alarm) {
if (err) {
return;
}
alarm.schedule({
type: 'snooze'
}, alarm.saveCallback(function(err, alarm) {
AlarmList.refreshItem(alarm);
AlarmManager.updateAlarmStatusBar();
done();
}.bind(this)));
});
}.bind(this));
}
};
return ActiveAlarm;
});