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
/
keypad.js
727 lines (617 loc) · 21.1 KB
/
keypad.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/* exported KeypadManager */
/* globals AddContactMenu, CallHandler, CallLogDBManager, CallsHandler,
CallScreen, CustomDialog, FontSizeManager, LazyLoader, LazyL10n,
MultiSimActionButton, SimPicker, SettingsListener, TonePlayer */
'use strict';
// Frequencies coming from http://en.wikipedia.org/wiki/Telephone_keypad
var gTonesFrequencies = {
'1': [697, 1209], '2': [697, 1336], '3': [697, 1477],
'4': [770, 1209], '5': [770, 1336], '6': [770, 1477],
'7': [852, 1209], '8': [852, 1336], '9': [852, 1477],
'*': [941, 1209], '0': [941, 1336], '#': [941, 1477]
};
/**
* DTMF tone constructor, providing the SIM on which this tone will be played
* is mandatory.
*
* @param {String} tone The tone to be played.
* @param {Boolean} short True if this will be a short tone, false otherwise.
* @param {Integer} serviceId The ID of the SIM card on which to play the tone.
*/
function DtmfTone(tone, short, serviceId) {
this.tone = tone;
this.short = short;
this.serviceId = serviceId;
this.timer = 0;
}
DtmfTone.prototype = {
/**
* Starts playing the tone, if this is a short tone it will stop automatically
* after kShortToneLength milliseconds, otherwise it will play until stopped.
*/
play: function dt_play() {
clearTimeout(this.timer);
// Stop previous tone before dispatching a new one
navigator.mozTelephony.stopTone(this.serviceId);
navigator.mozTelephony.startTone(this.tone, this.serviceId);
if (this.short) {
this.timer = window.setTimeout(function dt_stopTone(serviceId) {
navigator.mozTelephony.stopTone(serviceId);
}, DtmfTone.kShortToneLength, this.serviceId);
}
},
/**
* Stop the DTMF tone, this is safe to call even if the DTMF tone has already
* stopped.
*/
stop: function dt_stop() {
clearTimeout(this.timer);
navigator.mozTelephony.stopTone(this.serviceId);
}
};
/**
* Length of a short DTMF tone, currently 120ms.
*/
DtmfTone.kShortToneLength = 120;
var KeypadManager = {
kMaxDigits: 50,
_phoneNumber: '',
_onCall: false,
_keypadSoundIsEnabled: false,
_shortTone: false,
_vibrationEnabled: false,
// Keep in sync with Lockscreen and keyboard vibration
kVibrationDuration: 50, // ms
onValueChanged: null,
get phoneNumberView() {
delete this.phoneNumberView;
this.phoneNumberView = document.getElementById('phone-number-view');
return this.phoneNumberView;
},
get phoneNumberViewContainer() {
delete this.phoneNumberViewContainer;
this.phoneNumberViewContainer =
document.getElementById('phone-number-view-container');
return this.phoneNumberViewContainer;
},
get keypad() {
delete this.keypad;
this.keypad = document.getElementById('keypad');
return this.keypad;
},
get callBar() {
delete this.callBar;
this.callBar =
document.getElementById('keypad-callbar');
return this.callBar;
},
get hideBar() {
delete this.hideBar;
this.hideBar = document.getElementById('keypad-hidebar');
return this.hideBar;
},
get callBarAddContact() {
delete this.callBarAddContact;
this.callBarAddContact =
document.getElementById('keypad-callbar-add-contact');
return this.callBarAddContact;
},
get callBarCallAction() {
delete this.callBarCallAction;
this.callBarCallAction =
document.getElementById('keypad-callbar-call-action');
return this.callBarCallAction;
},
get callBarCancelAction() {
delete this.callBarCancelAction;
this.callBarCancelAction =
document.getElementById('keypad-callbar-cancel');
return this.callBarCancelAction;
},
get deleteButton() {
delete this.deleteButton;
this.deleteButton = document.getElementById('keypad-delete');
return this.deleteButton;
},
get hideBarHangUpAction() {
delete this.hideBarHangUpAction;
this.hideBarHangUpAction =
document.getElementById('keypad-hidebar-hang-up-action-wrapper');
return this.hideBarHangUpAction;
},
get hideBarHideAction() {
delete this.hideBarHideAction;
this.hideBarHideAction =
document.getElementById('keypad-hidebar-hide-keypad-action');
return this.hideBarHideAction;
},
multiSimActionButton: null,
init: function kh_init(oncall) {
this._onCall = !!oncall;
this.phoneNumberView.value = '';
this._phoneNumber = '';
var keyHandler = this.keyHandler.bind(this);
this.keypad.addEventListener('touchstart', keyHandler, true);
this.keypad.addEventListener('touchmove', keyHandler, true);
this.keypad.addEventListener('touchend', keyHandler, true);
this.keypad.addEventListener('touchcancel', keyHandler, true);
this.deleteButton.addEventListener('touchstart', keyHandler);
this.deleteButton.addEventListener('touchend', keyHandler);
// The keypad add contact bar is only included in the normal version of
// the keypad.
if (this.callBarAddContact) {
this.callBarAddContact.addEventListener('click',
this.addContact.bind(this));
}
// The keypad call bar is only included in the normal version and
// the emergency call version of the keypad.
if (this.callBarCallAction) {
if (typeof MultiSimActionButton !== 'undefined') {
if (navigator.mozMobileConnections &&
navigator.mozMobileConnections.length > 1) {
// Use LazyL10n to load l10n.js. Single SIM devices will be slowed
// down by the cost of loading l10n.js in the startup path if we don't
// do this.
var self = this;
LazyL10n.get(function localized(_) {
self.multiSimActionButton =
new MultiSimActionButton(self.callBarCallAction,
CallHandler.call,
'ril.telephony.defaultServiceId',
self.phoneNumber.bind(self));
});
} else {
this.multiSimActionButton =
new MultiSimActionButton(this.callBarCallAction,
CallHandler.call,
'ril.telephony.defaultServiceId',
this.phoneNumber.bind(this));
}
}
this.callBarCallAction.addEventListener('click',
this.fetchLastCalled.bind(this));
}
// The keypad cancel bar is only the emergency call version of the keypad.
if (this.callBarCancelAction) {
this.callBarCancelAction.addEventListener('click', function() {
window.parent.LockScreen.switchPanel();
});
}
// The keypad hide bar is only included in the on call version of the
// keypad.
if (this.hideBarHideAction) {
this.hideBarHideAction.addEventListener('click',
this.callbarBackAction);
}
if (this.hideBarHangUpAction) {
this.hideBarHangUpAction.addEventListener('click',
this.hangUpCallFromKeypad);
}
TonePlayer.init('normal');
var channel = this._onCall ? 'telephony' : 'normal';
window.addEventListener('visibilitychange', (function() {
var telephony = navigator.mozTelephony;
var callIsActive = telephony.calls.length ||
telephony.conferenceGroup.calls.length;
if (TonePlayer) {
// If app is hidden and we are not in the middle of a call, then switch
// to normal channel, no matter what channel this app should use
if (document.hidden && !callIsActive) {
TonePlayer.setChannel('normal');
} else {
// Otherwise switch to the channel this app is supposed to use
TonePlayer.setChannel(channel);
}
}
}).bind(this));
this.render();
LazyLoader.load(['/shared/style/action_menu.css',
'/dialer/js/suggestion_bar.js']);
this._observePreferences();
},
moveCaretToEnd: function hk_util_moveCaretToEnd(el) {
if (typeof el.selectionStart == 'number') {
el.selectionStart = el.selectionEnd = el.value.length;
} else if (typeof el.createTextRange != 'undefined') {
el.focus();
var range = el.createTextRange();
range.collapse(false);
range.select();
}
},
render: function hk_render(layoutType) {
if (layoutType == 'oncall') {
if (CallsHandler.activeCall) {
var activeCall = CallsHandler.activeCall.call;
this._phoneNumber = activeCall.id.number;
}
this._isKeypadClicked = false;
this.phoneNumberViewContainer.classList.add('keypad-visible');
if (this.callBar) {
this.callBar.classList.add('hide');
}
if (this.hideBar) {
this.hideBar.classList.remove('hide');
}
this.deleteButton.classList.add('hide');
} else {
this.phoneNumberViewContainer.classList.remove('keypad-visible');
if (this.callBar) {
this.callBar.classList.remove('hide');
}
if (this.hideBar) {
this.hideBar.classList.add('hide');
}
this.deleteButton.classList.remove('hide');
}
},
phoneNumber: function hk_phoneNumber() {
return this._phoneNumber;
},
fetchLastCalled: function hk_fetchLastCalled() {
if (this._phoneNumber !== '') {
return;
}
var self = this;
CallLogDBManager.getGroupAtPosition(1, 'lastEntryDate', true, 'dialing',
function hk_ggap_callback(result) {
if (result && (typeof result === 'object') && result.number) {
self.updatePhoneNumber(result.number);
}
}
);
},
addContact: function hk_addContact(event) {
var number = this._phoneNumber;
if (!number) {
return;
}
LazyLoader.load(['/dialer/js/add_contact_menu.js'], function() {
AddContactMenu.show(number);
});
},
callbarBackAction: function hk_callbarBackAction(event) {
CallScreen.hideKeypad();
},
hangUpCallFromKeypad: function hk_hangUpCallFromKeypad(event) {
CallScreen.body.classList.remove('showKeypad');
CallsHandler.end();
},
_lastPressedKey: null,
_dtmfTone: null,
_playDtmfTone: function kh_playDtmfTone(key) {
var serviceId = 0;
if (!this._onCall) {
return;
}
if (CallsHandler.activeCall) {
// Single call
serviceId = CallsHandler.activeCall.call.serviceId;
} else {
// Conference call
serviceId = navigator.mozTelephony.active.calls[0].serviceId;
}
if (this._dtmfTone) {
this._dtmfTone.stop();
this._dtmfTone = null;
}
this._dtmfTone = new DtmfTone(key, this._shortTone, serviceId);
this._dtmfTone.play();
},
_stopDtmfTone: function kh_stopDtmfTone() {
if (!this._dtmfTone) {
return;
}
this._dtmfTone.stop();
this._dtmfTone = null;
},
/**
* Function used to respond to touchstart events over the keypad. Reacts to
* the first key that has been pressed by playing the appropriate tone and
* sets up the necessary timers to react to long presses.
*
* @param {String} key The key that was hit by this touchstart event.
* @param {Boolean} [voicemail] If present and true indicates that the
* pressed key corresponded with the button used for voicemail.
*/
_touchStart: function kh_touchStart(key, voicemail) {
this._longPress = false;
this._lastPressedKey = key;
if (key != 'delete') {
if (this._keypadSoundIsEnabled) {
// We do not support long press if not on a call
TonePlayer.start(
gTonesFrequencies[key], !this._onCall || this._shortTone);
}
if (this._vibrationEnabled) {
navigator.vibrate(this.kVibrationDuration);
}
this._playDtmfTone(key);
}
// Manage long press
if ((key == '0' && !this._onCall) || key == 'delete') {
this._holdTimer = setTimeout(function(self) {
if (key == 'delete') {
self._phoneNumber = '';
} else {
var index = self._phoneNumber.length - 1;
//Remove last 0, this is a long press and we want to add the '+'
if (index >= 0 && self._phoneNumber[index] === '0') {
self._phoneNumber = self._phoneNumber.substr(0, index);
}
self._phoneNumber += '+';
}
self._longPress = true;
self._updatePhoneNumberView('begin', false);
}, 400, this);
}
// Voicemail long press (needs to be longer since it actually dials)
if (voicemail) {
this._holdTimer = setTimeout(function vm_call(self) {
self._longPress = true;
self._callVoicemail();
self._phoneNumber = self._phoneNumber.slice(0, -1);
self._updatePhoneNumberView('begin', false);
}, 1500, this);
}
if (key == 'delete') {
this._phoneNumber = this._phoneNumber.slice(0, -1);
} else if (this.phoneNumberViewContainer.classList.
contains('keypad-visible')) {
if (!this._isKeypadClicked) {
this._isKeypadClicked = true;
this._phoneNumber = key;
this.replaceAdditionalContactInfo('');
} else {
this._phoneNumber += key;
}
} else {
this._phoneNumber += key;
}
setTimeout(function(self) {
self._updatePhoneNumberView('begin', false);
}, 0, this);
},
/**
* Function used to respond to touchmove events over the keypad. Stops playing
* the tone associated with the last pressed key and resets it if the target
* goes outside its area.
*
* @param {Object} touch Touch position object for this move.
*/
_touchMove: function kh_touchMove(touch) {
var target = document.elementFromPoint(touch.pageX, touch.pageY);
var key = target.dataset ? target.dataset.value : null;
if (key !== this._lastPressedKey || key === 'delete') {
this._stopDtmfTone();
this._lastPressedKey = null;
}
},
/**
* Function used to respond to touchend events over the keypad. Stops playing
* tones and resets timers associated with the key press.
*
* @param {String} key The key over which the tap finished.
*/
_touchEnd: function kh_touchEnd(key) {
if (key !== 'delete' && key === this._lastPressedKey) {
this._stopDtmfTone();
this._lastPressedKey = null;
}
if (this._keypadSoundIsEnabled) {
TonePlayer.stop();
}
// If it was a long press our work is already done
if (this._longPress) {
this._longPress = false;
this._holdTimer = null;
return;
}
if (this._holdTimer) {
clearTimeout(this._holdTimer);
}
},
keyHandler: function kh_keyHandler(event) {
var key = event.target.dataset.value;
// We could receive this event from an element that
// doesn't have the dataset value. Got the last key
// pressed and assing this value to continue with the
// proccess.
if (!key) {
return;
}
// Per certification requirement, we need to send an MMI request to
// get the device's IMEI as soon as the user enters the last # key from
// the "*#06#" MMI string. See bug 857944.
if (key === '#' && this._phoneNumber === '*#06#') {
this.multiSimActionButton.performAction();
return;
}
// If user input number more 50 digits, app shouldn't accept.
// The limit only applies while not on a call - there is no
// limit while on a call (bug 917630).
if (key != 'delete' && this._phoneNumber.length >= this.kMaxDigits &&
!this._onCall) {
event.target.classList.remove('active');
return;
}
event.stopPropagation();
switch (event.type) {
case 'touchstart':
event.target.classList.add('active');
this._touchStart(key, event.target.dataset.voicemail);
break;
case 'touchmove':
this._touchMove(event.touches[0]);
break;
case 'touchend':
case 'touchcancel':
event.target.classList.remove('active');
this._touchEnd(key);
break;
}
},
sanitizePhoneNumber: function(number) {
return number.replace(/\s+/g, '');
},
updatePhoneNumber: function kh_updatePhoneNumber(number, ellipsisSide,
forceMaxFontSize) {
number = this.sanitizePhoneNumber(number);
this._phoneNumber = number;
this._updatePhoneNumberView(ellipsisSide, forceMaxFontSize);
},
press: function(value) {
this._playDtmfTone(value);
TonePlayer.start(gTonesFrequencies[value], true);
setTimeout((function nextTick() {
TonePlayer.stop();
this._stopDtmfTone();
}).bind(this));
},
_updatePhoneNumberView: function kh_updatePhoneNumberview(ellipsisSide,
forceMaxFontSize) {
var phoneNumber = this._phoneNumber;
// If there are digits in the phone number, show the delete button
// and enable the add contact button
if (this._onCall) {
this.replacePhoneNumber(phoneNumber, ellipsisSide, forceMaxFontSize);
} else {
var visibility;
if (phoneNumber.length > 0) {
visibility = 'visible';
this.callBarAddContact.removeAttribute('disabled');
} else {
visibility = 'hidden';
this.callBarAddContact.setAttribute('disabled', 'disabled');
}
this.deleteButton.style.visibility = visibility;
this.phoneNumberView.value = phoneNumber;
this.moveCaretToEnd(this.phoneNumberView);
FontSizeManager.adaptToSpace(
FontSizeManager.DIAL_PAD, this.phoneNumberView, forceMaxFontSize,
ellipsisSide);
}
if (this.onValueChanged) {
this.onValueChanged(this._phoneNumber);
}
},
replacePhoneNumber:
function kh_replacePhoneNumber(phoneNumber, ellipsisSide,
forceMaxFontSize) {
if (this._onCall && CallsHandler.activeCall) {
CallsHandler.activeCall.
replacePhoneNumber(phoneNumber, ellipsisSide, forceMaxFontSize);
}
},
restorePhoneNumber:
function kh_restorePhoneNumber() {
if (this._onCall && CallsHandler.activeCall) {
CallsHandler.activeCall.restorePhoneNumber();
}
},
replaceAdditionalContactInfo:
function kh_replaceAdditionalContactInfo(additionalContactInfo) {
var call = CallsHandler.activeCall;
if (this._onCall && call) {
call.replaceAdditionalContactInfo(additionalContactInfo);
}
},
restoreAdditionalContactInfo: function kh_restoreAdditionalContactInfo() {
if (this._onCall && CallsHandler.activeCall) {
CallsHandler.activeCall.restoreAdditionalContactInfo();
}
},
_callVoicemail: function() {
if (navigator.mozIccManager.iccIds.length <= 1) {
this._callVoicemailForSim(0);
return;
}
var self = this;
var key = 'ril.voicemail.defaultServiceId';
var req = navigator.mozSettings.createLock().get(key);
req.onsuccess = function() {
LazyLoader.load(['/shared/js/sim_picker.js'], function() {
LazyL10n.get(function(_) {
SimPicker.getOrPick(req.result[key], _('voiceMail'),
self._callVoicemailForSim);
});
});
};
},
_callVoicemailForSim: function(cardIndex) {
var settings = navigator.mozSettings;
if (!settings) {
return;
}
var transaction = settings.createLock();
var request = transaction.get('ril.iccInfo.mbdn');
request.onsuccess = (function() {
var numbers = request.result['ril.iccInfo.mbdn'];
var number;
if (typeof numbers == 'string') {
number = numbers;
} else {
number = numbers && numbers[cardIndex];
}
var voicemail = navigator.mozVoicemail;
if (!number && voicemail) {
number = voicemail.getNumber();
}
if (number) {
CallHandler.call(number, cardIndex);
} else {
this._showNoVoicemailDialog();
}
}).bind(this);
request.onerror = function() {};
},
_showNoVoicemailDialog: function hk_showNoVoicemailDialog() {
var voicemailDialog = {
title: 'voicemailNoNumberTitle',
text: 'voicemailNoNumberText',
confirm: {
title: 'voicemailNoNumberSettings',
recommend: true,
callback: this.showVoicemailSettings
},
cancel: {
title: 'voicemailNoNumberCancel',
callback: this._hideNoVoicemailDialog
}
};
LazyLoader.load(['/shared/js/custom_dialog.js'], function() {
CustomDialog.show(
voicemailDialog.title, voicemailDialog.text,
voicemailDialog.cancel, voicemailDialog.confirm);
});
},
_hideNoVoicemailDialog: function kh_hideNoVoicemailDialog() {
CustomDialog.hide();
},
showVoicemailSettings: function kh_showVoicemailSettings() {
var activity = new window.MozActivity({
name: 'configure',
data: {
target: 'device',
section: 'call'
}
});
activity.onerror = function() {
console.warn('Configure activity error:', activity.error.name);
};
},
_observePreferences: function kh_observePreferences() {
var self = this;
LazyLoader.load('/shared/js/settings_listener.js', function() {
SettingsListener.observe('phone.ring.keypad', false, function(value) {
self._keypadSoundIsEnabled = !!value;
});
SettingsListener.observe('phone.dtmf.type', false, function(value) {
self._shortTone = (value === 'short');
});
SettingsListener.observe('keyboard.vibration', false, function(value) {
self._vibrationEnabled = !!value;
});
});
}
};