forked from mozilla-b2g/gaia
/
emergency_callback_manager.js
182 lines (158 loc) · 6.05 KB
/
emergency_callback_manager.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
'use strict';
/*
* This emergency callback mode manager is for:
* - Initialize the manager if network type is cdma and regist an event handler
* for emergency callback mode change.
* - Display a notification to showing the system is entered the callback
* mode with count down timer.
* - Popup a dialog for user to notify the mode will block data network and
* let user decide callback mode should exit or not.
*
*/
var EmergencyCbManager = {
timer: 0,
timeoutController: null,
TOASTER_TIMEOUT: 2000,
notification: null,
message: null,
toaster: null,
toasterMessage: null,
warningDialog: null,
okButton: null,
cancelButton: null,
init: function ecm_init() {
// XXX: check bug-926169
// this is used to keep all tests passing while introducing multi-sim APIs
this._conn = navigator.mozMobileConnection ||
navigator.mozMobileConnections &&
navigator.mozMobileConnections[0];
if (!this._conn) {
return;
}
// Dom elements
this.notification =
document.getElementById('emergency-callback-notification');
this.message = this.notification.querySelector('.title-container');
this.notificationTimer = this.notification.querySelector('.timer');
this.toaster = document.getElementById('emergency-callback-toaster');
this.toasterMessage = this.toaster.querySelector('.message');
this.toasterTimer = this.toaster.querySelector('.timer');
this.warningDialog = document.getElementById('emergency-callback-dialog');
this.dialogTimer = this.warningDialog.querySelector('small');
this.okButton = document.getElementById('emergency-callback-exit-button');
this.cancelButton =
document.getElementById('emergency-callback-stay-button');
this.warningTimer = this.warningDialog.querySelector('p');
// Event handler
this._conn.addEventListener('emergencycbmodechange',
this.onEmergencyCbModeChange.bind(this));
this.notification.addEventListener('click',
this.notificationClicked.bind(this));
this.toaster.addEventListener('click', this.toasterClicked.bind(this));
this.okButton.addEventListener('click',
this.exitEmergencyCbMode.bind(this));
this.cancelButton.addEventListener('click', this.cancelPrompt.bind(this));
},
get timerString() {
var totalSec = this.timer / 1000;
var min = Math.floor(totalSec / 60);
var secString = (totalSec % 60).toString();
var sec = secString.length > 1 ? secString : '0' + secString;
return (min + ':' + sec);
},
exitEmergencyCbMode: function ecm_exitEmergencyCbMode() {
this.warningDialog.classList.remove('visible');
this.warningDialog.hidden = true;
this._conn.exitEmergencyCbMode();
},
notificationClicked: function ecm_notificationClicker() {
this.showEmergencyCbPrompt();
UtilityTray.hide();
},
toasterClicked: function ecm_toasterClicker() {
this.showEmergencyCbPrompt();
this.toaster.classList.remove('displayed');
this.toaster.hidden = true;
},
showEmergencyCbPrompt: function ecm_showEmergencyCbPrompt() {
// Add home key event handler to dismiss the prompt if home is pressed
// while prompt displayed.
var self = this;
window.addEventListener('home', function hidePrompt() {
window.removeEventListener('home', hidePrompt);
self.cancelPrompt();
});
this.warningDialog.classList.add('visible');
this.warningDialog.hidden = false;
},
cancelPrompt: function ecm_cancelPrompt() {
this.warningDialog.classList.remove('visible');
this.warningDialog.hidden = true;
},
displayNotificationAndToaster: function ecm_displayNotificationAndToaster() {
this.displayNotificationIfHidden();
this.toasterTimer.textContent = this.timerString;
this.toaster.classList.add('displayed');
this.toaster.hidden = false;
setTimeout(function waitToHide() {
this.toaster.classList.remove('displayed');
this.toaster.hidden = true;
}.bind(this), this.TOASTER_TIMEOUT);
},
hideNotificationIfDisplayed: function ecm_hideNotificationIfDisplayed() {
if (this.notification.classList.contains('displayed')) {
this.notification.classList.remove('displayed');
this.notification.hidden = true;
StatusBar.updateEmergencyCbNotification();
}
},
displayNotificationIfHidden: function ecm_displayNotificationIfHidden() {
if (!this.notification.classList.contains('displayed')) {
this.notification.classList.add('displayed');
this.notification.hidden = false;
StatusBar.updateEmergencyCbNotification(true);
}
},
updateTimer: function ecm_updateTimer() {
this.timer -= 1000;
if (this.timer < 0) {
window.clearInterval(this.timeoutController);
this.timeoutController = null;
return;
}
this.notificationTimer.textContent = this.dialogTimer.textContent =
this.timerString;
},
onDataError: function ecm_onDataError(evt) {
// TODO: We shold be able to confirm the error is caused by
// emergency callback mode before showing prompt.
this.showEmergencyCbPrompt();
},
onEmergencyCbModeChange: function ecm_onEmergencyCbModeChange(evt) {
var active = evt.active;
if (active) {
if (this.timeoutController) {
window.clearInterval(this.timeoutController);
}
this.timer = evt.timeoutMs;
this.displayNotificationAndToaster();
this.timeoutController =
window.setInterval(this.updateTimer.bind(this), 1000);
this._conn.ondataerror = this.onDataError.bind(this);
} else {
this.hideNotificationIfDisplayed();
this.warningDialog.classList.remove('visible');
this.warningDialog.hidden = true;
window.clearInterval(this.timeoutController);
this.timeoutController = null;
this._conn.ondataerror = null;
}
}
};
// unit tests call init() manually
if (navigator.mozL10n) {
// Always initialize EmergencyCbManager when start up because only CDMA
// connection will fire emergencycbmodechange event and it's harmless to
// add listener for other type of network.
navigator.mozL10n.once(EmergencyCbManager.init.bind(EmergencyCbManager));
}