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
/
dialer_agent.js
237 lines (190 loc) · 6.44 KB
/
dialer_agent.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
'use strict';
/* global SettingsListener, SettingsURL */
/* global Service, LazyLoader, toneUpgrader */
/* r=? dialer+system peers for changes in this file. */
(function(exports) {
/**
*
* The delay between an incoming phone call and the first ring or vibration
* needs to be as short as possible.
*
* This simple module keeps the ringtone (blob) around and starts alerting the
* user as soon as a new incoming call is detected via the mozTelephony API.
*
* We also listen for the sleep and volumedown hardware buttons to provide
* the user with an easy way to stop the ringing.
*
* @example
* var dialerAgent = new DialerAgent();
* dialerAgent.start(); // Attach the event listeners.
* dialerAgent.stop(); // Deattach the event listeners.
*
* @class DialerAgent
* @requires SettingsListener
* @requires SettingsURL
* @requires Service
* @requires LazyLoader
*
**/
var DialerAgent = function DialerAgent() {
var telephony = navigator.mozTelephony;
if (!telephony) {
return;
}
this._telephony = telephony;
this._started = false;
this._powerHangsUp = false;
this._shouldVibrate = true;
this._alerting = false;
this._vibrateInterval = null;
var player = new Audio();
this._player = player;
// XXX: This will need to be updated for bug 961967
// (audio competing in system app)
player.mozAudioChannelType = 'ringer';
player.preload = 'metadata';
player.loop = true;
};
DialerAgent.prototype.start = function da_start() {
if (!this._telephony) {
return;
}
if (this._started) {
throw 'Instance should not be start()\'ed twice.';
}
this._started = true;
SettingsListener.observe('audio.volume.notification', 7, function(value) {
this._playRing();
}.bind(this));
SettingsListener.observe('dialer.ringtone', '', function(value) {
LazyLoader.load(['../shared/js/settings_url.js']).then(function() {
var phoneSoundURL = new SettingsURL();
this._player.pause();
this._player.src = phoneSoundURL.set(value);
this._playRing();
}.bind(this)).catch((err) => {
console.error(err);
});
}.bind(this));
// We have new default ringtones in 2.0, so check if the version is upgraded
// then execute the necessary migration.
if (Service.query('justUpgraded')) {
LazyLoader.load('js/tone_upgrader.js').then(() => {
toneUpgrader.perform('ringtone');
});
}
SettingsListener.observe('dialer.power_hangsup', false, function(value) {
this._powerHangsUp = !!value;
}.bind(this));
SettingsListener.observe('vibration.enabled', true, function(value) {
this._shouldVibrate = !!value;
}.bind(this));
this._telephony.addEventListener('callschanged', this);
window.addEventListener('sleep', this);
window.addEventListener('wake', this);
window.addEventListener('volumedown', this);
Service.registerState('onCall', this);
return this;
};
DialerAgent.prototype.stop = function da_stop() {
if (!this._started) {
return;
}
this._started = false;
this._telephony.removeEventListener('callschanged', this);
window.removeEventListener('sleep', this);
window.removeEventListener('wake', this);
window.removeEventListener('volumedown', this);
Service.unregisterState('onCall', this);
// TODO: should remove the settings listener once the helper
// allows it.
// See bug 981373.
};
DialerAgent.prototype.handleEvent = function da_handleEvent(evt) {
if (evt.type === 'volumedown') {
this._stopAlerting();
return;
}
if ((evt.type === 'sleep') && this._alerting) {
this._stopAlerting();
return;
}
if ((evt.type === 'sleep') && this._powerHangsUp && this.onCall()) {
// Hangup all calls
this._telephony.calls.forEach(call => call.hangUp());
if (this._telephony.conferenceGroup.calls.length > 0) {
this._telephony.conferenceGroup.hangUp();
}
return;
}
if ((evt.type === 'wake') && this.onCall()) {
Service.request('turnScreenOn');
return;
}
if (evt.type !== 'callschanged') {
return;
}
var calls = this._telephony.calls;
if (calls.length === 0) {
Service.request('turnScreenOn');
return;
}
var calling = calls.some(function(call) {
if (call.state === 'incoming' || call.state === 'dialing') {
return true;
}
});
if (calling) {
// Show the callscreen window if it already exists.
Service.request('AttentionWindowManager:showCallscreenWindow');
}
var incomingCall = calls[calls.length - 1];
if (this._alerting || incomingCall.state !== 'incoming') {
return;
}
var self = this;
// Silence the ringtone if more than one call is present
this._startAlerting(/* silent */ (this.numOpenLines() > 1));
incomingCall.addEventListener('statechange', function callStateChange() {
incomingCall.removeEventListener('statechange', callStateChange);
self._stopAlerting();
});
};
DialerAgent.prototype._playRing = function da_playRing() {
// Notice that, even we are in the vibration mode, we would still play the
// silence ringer. That is because when the incoming call is coming, other
// playing sound should be paused. Therefore, we need to a silence ringer
// to compete for the audio output.
if (this._alerting) {
this._player.play();
}
};
DialerAgent.prototype._startAlerting = function da_startAlerting(silent) {
this._alerting = true;
if ('vibrate' in navigator && this._shouldVibrate) {
this._vibrateInterval = window.setInterval(function vibrate() {
navigator.vibrate([200]);
}, 600);
navigator.vibrate([200]);
}
this._player.volume = silent ? 0.0 : 1.0;
this._playRing();
};
DialerAgent.prototype._stopAlerting = function da_stopAlerting() {
var player = this._player;
this._alerting = false;
if (player && player.readyState > player.HAVE_NOTHING) {
player.pause();
player.currentTime = 0;
}
window.clearInterval(this._vibrateInterval);
};
DialerAgent.prototype.numOpenLines = function() {
return this._telephony.calls.length +
(this._telephony.conferenceGroup.calls.length ? 1 : 0);
};
DialerAgent.prototype.onCall = function() {
return (this.numOpenLines() > 0);
};
exports.DialerAgent = DialerAgent;
}(window));