This repository has been archived by the owner on Nov 23, 2019. It is now read-only.
forked from mozilla-b2g/gaia
/
homescreen.js
1629 lines (1362 loc) · 45.8 KB
/
homescreen.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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
var _ = document.mozL10n.get;
var bookmarks = null;
// The appscreen is the main part of the homescreen: the part that
// displays icons that launch all of the installed apps.
var appscreen;
function startup() {
// set the 'lang' and 'dir' attributes to <html> when the page is translated
document.documentElement.lang = document.mozL10n.language.code;
document.documentElement.dir = document.mozL10n.language.direction;
document.dir = document.mozL10n.language.direction;
if (!appscreen) { // first start: init
appscreen = new AppScreen();
var touchables = [
document.getElementById('notifications-screen'),
document.getElementById('statusbar')
];
NotificationScreen.init(touchables);
LockScreen.init();
LockScreen.update(function fireHomescreenReady() {
ScreenManager.turnScreenOn();
new TelephonyListener();
window.parent.postMessage('homescreenready', '*');
});
} else { // locale has been changed: rebuild app grid
appscreen.build(true);
}
updateClock();
updateBattery();
updateConnection();
// <body> children are hidden until the UI is translated
document.body.classList.remove('hidden');
}
var LockScreen = {
get overlay() {
delete this.overlay;
return this.overlay = document.getElementById('lockscreen');
},
locked: true,
init: function lockscreen_init() {
var events = ['touchstart', 'touchmove', 'touchend', 'keydown', 'keyup'];
AddEventHandlers(LockScreen.overlay, this, events);
// TODO We don't really want to unlock the homescreen here
var telephony = navigator.mozTelephony;
if (telephony) {
telephony.addEventListener('incoming', (function incoming(evt) {
this.unlock(true);
}).bind(this));
}
},
update: function lockscreen_update(callback) {
var settings = window.navigator.mozSettings;
if (!settings) {
this.lock(true, callback);
return;
}
var request = settings.getLock().get('lockscreen.enabled');
request.addEventListener('success', (function onsuccess() {
var value = request.result['lockscreen.enabled'];
var enabled = typeof(value) == 'boolean' ? value : true;
if (enabled) {
this.lock(true, callback);
} else {
this.unlock(true, callback);
}
}).bind(this));
request.addEventListener('error', (function onerror() {
this.lock(true, callback);
}).bind(this));
},
lock: function lockscreen_lock(instant, callback) {
var style = this.overlay.style;
if (this.locked) {
if (instant) {
style.MozTransition = style.MozTransform = '';
} else {
style.MozTransition = '-moz-transform 0.2s linear';
}
style.MozTransform = 'translateY(0)';
if (callback)
setTimeout(callback, 0, true);
return;
}
this.locked = true;
if (instant) {
style.MozTransition = style.MozTransform = '';
} else {
style.MozTransition = '-moz-transform 0.2s linear';
style.MozTransform = 'translateY(0)';
}
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent('locked', true, true, null);
window.dispatchEvent(evt);
// Wait for paint before firing callback
if (!callback)
return;
var afterPaintCallback = function afterPaint() {
window.removeEventListener('MozAfterPaint', afterPaintCallback);
callback(true);
};
window.addEventListener('MozAfterPaint', afterPaintCallback);
},
// Unlock the lockscreen, doing an animation for the specified time.
// Call the callback, if there is one, without waiting for the
// animation to complete.
// If no time is specified use a default of 0.2 seconds.
// For backward compatibility, you can pass true as the first argument
// for a time of 0.
unlock: function lockscreen_unlock(time, callback) {
var style = this.overlay.style;
var wasAlreadyUnlocked = !this.locked;
if (time == undefined)
time = 0.2;
else if (time === true)
time = 0;
this.locked = false;
if (time === 0)
style.MozTransition = style.MozTransform = '';
else
style.MozTransition = '-moz-transform ' + time + 's linear';
style.MozTransform = 'translateY(-100%)';
if (!wasAlreadyUnlocked) {
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent('unlocked', true, true, null);
window.dispatchEvent(evt);
}
if (callback)
setTimeout(callback, 0, false);
},
onTouchStart: function lockscreen_touchStart(e) {
this.startY = this.lastY = e.pageY;
this.lastTime = e.timeStamp;
this.velocity = 0;
this.moving = true;
},
onTouchMove: function lockscreen_touchMove(e) {
if (this.moving) {
this.velocity = (this.lastY - e.pageY) / (e.timeStamp - this.lastTime);
this.lastY = e.pageY;
this.lastTime = e.timeStamp;
var dy = Math.max(0, this.startY - e.pageY);
var style = this.overlay.style;
style.MozTransition = '';
style.MozTransform = 'translateY(' + (-dy) + 'px)';
}
},
// Tune these parameters to make the lockscreen unlock feel right
FRICTION: 0.01, // pixels/ms/ms
UNLOCK_THRESHOLD: 0.25, // fraction of screen height
onTouchEnd: function lockscreen_touchend(e) {
if (this.moving) {
this.moving = false;
// Physics:
// An object moving with a velocity v against a frictional
// deceleration a will travel a distance of 0.5*v*v/a before
// coming to rest (I think).
//
// If the user's gesture plus the distance due to the gesture velocity
// is more than the unlock threshold times screen height,
// unlock the phone. Otherwise move the lockscreen back down again.
//
var dy = Math.max(0, this.startY - e.pageY);
var distance = dy +
0.5 * this.velocity * this.velocity / LockScreen.FRICTION;
if (distance > window.innerHeight * LockScreen.UNLOCK_THRESHOLD) {
// Perform the animation at the gesture velocity
var distanceRemaining = window.innerHeight - dy;
var timeRemaining = distanceRemaining / this.velocity / 1000;
// But don't take longer than 1/2 second to complete it.
timeRemaining = Math.min(timeRemaining, .5);
this.unlock(timeRemaining);
} else {
this.lock();
}
}
},
handleEvent: function lockscreen_handleEvent(e) {
switch (e.type) {
case 'touchstart':
this.onTouchStart(e.touches[0]);
this.overlay.setCapture(false);
break;
case 'touchmove':
this.onTouchMove(e.touches[0]);
break;
case 'touchend':
this.onTouchEnd(e.changedTouches[0]);
document.releaseCapture();
break;
case 'keydown':
if (e.keyCode != e.DOM_VK_SLEEP || !navigator.mozPower.screenEnabled)
return;
this._timeout = window.setTimeout(function() {
SleepMenu.show();
}, 1500);
break;
case 'keyup':
if (e.keyCode != e.DOM_VK_SLEEP || SleepMenu.visible)
return;
window.clearTimeout(this._timeout);
if (navigator.mozPower.screenEnabled) {
this.update(function lockScreenCallback() {
ScreenManager.turnScreenOff();
});
} else {
// XXX: screen could be turned off by idle service instead of us.
// Update the lockscreen again when turning the screen on.
// (home screen would still flash when USB is plugged in)
this.update(function lockScreenCallback() {
ScreenManager.turnScreenOn();
});
//ScreenManager.turnScreenOn();
}
e.preventDefault();
e.stopPropagation();
break;
default:
return;
}
e.preventDefault();
}
};
var NotificationScreen = {
get touchable() {
return this.touchables[this.locked ? 0 : 1];
},
get screenHeight() {
var screenHeight = this._screenHeight;
if (!screenHeight) {
screenHeight = this.touchables[0].getBoundingClientRect().height;
this._screenHeight = screenHeight;
}
return screenHeight;
},
get container() {
delete this.container;
return this.container = document.getElementById('notifications-container');
},
init: function ns_init(touchables) {
this.touchables = touchables;
this.attachEvents(touchables);
window.addEventListener('mozChromeEvent', function notificationListener(e) {
var detail = e.detail;
switch (detail.type) {
case 'desktop-notification':
NotificationScreen.addNotification('desktop-notification',
detail.title, detail.text,
detail.id);
var hasNotifications = document.getElementById('state-notifications');
hasNotifications.dataset.visible = 'true';
break;
default:
// XXX Needs to implements more UI but for now let's allow stuffs
var event = document.createEvent('CustomEvent');
event.initCustomEvent('mozContentEvent', true, true, {
type: 'permission-allow',
id: detail.id
});
window.dispatchEvent(event);
break;
break;
}
});
var self = this;
var notifications = this.container;
notifications.addEventListener('click', function notificationClick(evt) {
var target = evt.target;
var closing = false;
// Handling the close button
if (target.classList.contains('close')) {
closing = true;
target = target.parentNode;
}
self.removeNotification(target);
var type = target.dataset.type;
if (type != 'desktop-notification')
return;
var event = document.createEvent('CustomEvent');
event.initCustomEvent('mozContentEvent', true, true, {
type: closing ?
'desktop-notification-close' : 'desktop-notification-click',
id: target.dataset.notificationID
});
window.dispatchEvent(event);
// And hide the notification tray
if (!closing)
self.unlock();
});
},
onTouchStart: function ns_onTouchStart(e) {
this.startX = e.pageX;
this.startY = e.pageY;
this.onTouchMove({ pageY: e.pageY + 32 });
},
onTouchMove: function ns_onTouchMove(e) {
var dy = -(this.startY - e.pageY);
if (this.locked)
dy += this.screenHeight;
dy = Math.min(this.screenHeight, dy);
var style = this.touchables[0].style;
style.MozTransition = '';
style.MozTransform = 'translateY(' + dy + 'px)';
},
onTouchEnd: function ns_onTouchEnd(e) {
var dy = -(this.startY - e.pageY);
var offset = Math.abs(dy);
if ((!this.locked && offset > this.screenHeight / 4) ||
(this.locked && offset < 10))
this.lock();
else
this.unlock();
},
unlock: function ns_unlock(instant) {
var style = this.touchables[0].style;
style.MozTransition = instant ? '' : '-moz-transform 0.2s linear';
style.MozTransform = 'translateY(0)';
this.locked = false;
},
lock: function ns_lock(dy) {
var style = this.touchables[0].style;
style.MozTransition = '-moz-transform 0.2s linear';
style.MozTransform = 'translateY(100%)';
this.locked = true;
},
attachEvents: function ns_attachEvents(view) {
AddEventHandlers(window, this, ['touchstart', 'touchmove', 'touchend']);
},
detachEvents: function ns_detachEvents() {
RemoveEventHandlers(window, this, ['touchstart', 'touchmove', 'touchend']);
},
handleEvent: function(evt) {
var target = evt.target;
switch (evt.type) {
case 'touchstart':
if (target != this.touchable)
return;
this.active = true;
target.setCapture(this);
this.onTouchStart(evt.touches[0]);
break;
case 'touchmove':
if (!this.active)
return;
this.onTouchMove(evt.touches[0]);
break;
case 'touchend':
if (!this.active)
return;
this.active = false;
document.releaseCapture();
this.onTouchEnd(evt.changedTouches[0]);
break;
default:
return;
}
evt.preventDefault();
},
addNotification: function ns_addNotification(type, nTitle, body, nID) {
var notifications = this.container;
var notification = document.createElement('div');
notification.className = 'notification';
notification.dataset.type = type;
if (type == 'desktop-notification') {
notification.dataset.notificationID = nID;
}
var title = document.createElement('div');
title.textContent = nTitle;
notification.appendChild(title);
var message = document.createElement('div');
message.classList.add('detail');
message.textContent = body;
notification.appendChild(message);
var close = document.createElement('a');
close.className = 'close';
notification.appendChild(close);
notifications.appendChild(notification);
},
removeNotification: function ns_removeNotification(notification) {
notification.parentNode.removeChild(notification);
// Hiding the notification indicator in the status bar
// if this is the last desktop notification
var notifSelector = 'div[data-type="desktop-notification"]';
var desktopNotifications = this.container.querySelectorAll(notifSelector);
if (desktopNotifications.length == 0) {
var hasNotifications = document.getElementById('state-notifications');
delete hasNotifications.dataset.visible;
}
},
removeNotifications: function ns_removeNotifications(type) {
var notifications = this.container;
var typeSelector = 'div[data-type="' + type + '"]';
var children = notifications.querySelectorAll(typeSelector);
for (var i = children.length - 1; i >= 0; i--) {
var notification = children[i];
notification.parentNode.removeChild(notification);
}
}
};
// Update the clock and schedule a new update if appropriate
function updateClock() {
if (!navigator.mozPower.screenEnabled)
return;
var now = new Date();
var match = document.getElementsByClassName('time');
for (var n = 0; n < match.length; ++n) {
var element = match[n];
element.textContent = now.toLocaleFormat(element.dataset.format);
}
// Schedule another clock update when a new minute rolls around
var now = new Date();
var sec = now.getSeconds();
setTimeout(updateClock, (59 - sec) * 1000);
}
function updateBattery() {
var battery = window.navigator.mozBattery;
if (!battery)
return;
// If the display is off, there is nothing to do here
if (!navigator.mozPower.screenEnabled) {
battery.removeEventListener('chargingchange', updateBattery);
battery.removeEventListener('levelchange', updateBattery);
battery.removeEventListener('statuschange', updateBattery);
return;
}
var elements = document.getElementsByClassName('battery');
for (var n = 0; n < elements.length; ++n) {
var element = elements[n];
var fuel = element.children[0];
var level = battery.level * 100;
var charging = element.children[1];
if (battery.charging) {
charging.hidden = false;
fuel.className = 'charging';
fuel.style.minWidth = (level / 5.88) + 'px';
} else {
charging.hidden = true;
fuel.style.minWidth = fuel.style.width = (level / 5.88) + 'px';
if (level <= 10)
fuel.className = 'critical';
else if (level <= 30)
fuel.className = 'low';
else
fuel.className = '';
}
}
// Make sure we will be called for any changes to the battery status
battery.addEventListener('chargingchange', updateBattery);
battery.addEventListener('levelchange', updateBattery);
battery.addEventListener('statuschange', updateBattery);
}
function updateConnection() {
var conn = window.navigator.mozMobileConnection;
if (!conn) {
return;
}
if (!navigator.mozPower.screenEnabled) {
conn.removeEventListener('cardstatechange', updateConnection);
conn.removeEventListener('connectionchange', updateConnection);
return;
}
// Update the operator name / SIM status.
var title = '';
if (conn.cardState == 'absent') {
title = _('noSimCard');
} else if (!conn.connected) {
title = _('connecting');
} else {
if (conn.roaming) {
title = _('roaming', { operator: (conn.operator || '') });
} else {
title = conn.operator || '';
}
}
document.getElementById('titlebar').textContent = title;
// Update the signal strength bars.
var signalElements = document.querySelectorAll('#signal > span');
for (var i = 0; i < 4; i++) {
var haveSignal = (i < conn.bars);
var el = signalElements[i];
if (haveSignal) {
el.classList.add('haveSignal');
} else {
el.classList.remove('haveSignal');
}
}
conn.addEventListener('cardstatechange', updateConnection);
conn.addEventListener('connectionchange', updateConnection);
}
var SoundManager = {
currentVolume: 5,
changeVolume: function soundManager_changeVolume(delta) {
activePhoneSound = true;
var volume = this.currentVolume + delta;
this.currentVolume = volume = Math.max(0, Math.min(10, volume));
var notification = document.getElementById('volume');
if (volume == 0) {
notification.classList.add('vibration');
} else {
notification.classList.remove('vibration');
}
var steps = notification.children;
for (var i = 0; i < steps.length; i++) {
var step = steps[i];
if (i < volume)
step.classList.add('active');
else
step.classList.remove('active');
}
notification.classList.add('visible');
if (this._timeout)
window.clearTimeout(this._timeout);
this._timeout = window.setTimeout(function hideSound() {
notification.classList.remove('visible');
}, 3000);
}
};
var SleepMenu = {
get element() {
delete this.element;
return this.element = document.getElementById('sleep');
},
get visible() {
return this.element.classList.contains('visible');
},
show: function sleepmenu_show() {
this.element.classList.add('visible');
},
hide: function sleepmenu_hide() {
this.element.classList.remove('visible');
},
handleEvent: function sleepmenu_handleEvent(evt) {
if (!this.visible)
return;
switch (evt.type) {
case 'click':
var action = evt.target.dataset.value;
switch (action) {
case 'airplane':
// XXX There is no API for that yet
break;
case 'silent':
activePhoneSound = false;
var settings = window.navigator.mozSettings;
if (settings)
settings.getLock().set({ 'phone.ring.incoming': false});
document.getElementById('silent').hidden = true;
document.getElementById('normal').hidden = false;
break;
case 'normal':
activePhoneSound = true;
var settings = window.navigator.mozSettings;
if (settings)
settings.getLock().set({'phone.ring.incoming': true});
document.getElementById('silent').hidden = false;
document.getElementById('normal').hidden = true;
break;
case 'restart':
navigator.mozPower.reboot();
break;
case 'power':
navigator.mozPower.powerOff();
break;
}
this.hide();
break;
case 'keyup':
if (evt.keyCode == evt.DOM_VK_ESCAPE ||
evt.keyCode == evt.DOM_VK_HOME) {
this.hide();
evt.preventDefault();
evt.stopPropagation();
}
break;
}
}
};
window.addEventListener('click', SleepMenu, true);
window.addEventListener('keyup', SleepMenu, true);
var SettingsListener = {
_callbacks: {},
init: function sl_init() {
if ('mozSettings' in navigator && navigator.mozSettings)
navigator.mozSettings.onsettingchange = this.onchange.bind(this);
},
onchange: function sl_onchange(evt) {
var callback = this._callbacks[evt.settingName];
if (callback) {
callback(evt.settingValue);
}
},
observe: function sl_observe(name, defaultValue, callback) {
var settings = window.navigator.mozSettings;
if (!settings) {
setTimeout(function() { callback(defaultValue); });
return;
}
var req = settings.getLock().get(name);
req.addEventListener('success', (function onsuccess() {
callback(typeof(req.result[name]) != 'undefined' ?
req.result[name] : defaultValue);
}));
this._callbacks[name] = callback;
}
};
SettingsListener.init();
/* === Source View === */
var SourceView = {
get viewer() {
return document.getElementById('appViewsource');
},
get active() {
return !this.viewer ? false : this.viewer.style.visibility === 'visible';
},
show: function sv_show() {
var viewsource = this.viewer;
if (!viewsource) {
var style = '#appViewsource { ' +
' position: absolute;' +
' top: -moz-calc(10%);' +
' left: -moz-calc(10%);' +
' width: -moz-calc(80% - 2 * 15px);' +
' height: -moz-calc(80% - 2 * 15px);' +
' visibility: hidden;' +
' margin: 15px;' +
' background-color: white;' +
' opacity: 0.92;' +
' color: black;' +
' z-index: 9999;' +
'}';
document.styleSheets[0].insertRule(style, 0);
viewsource = document.createElement('iframe');
viewsource.id = 'appViewsource';
document.body.appendChild(viewsource);
window.addEventListener('locked', this);
}
var url = WindowManager.getDisplayedApp();
if (!url)
// Assume the home screen is the visible app.
url = window.location.toString();
viewsource.src = 'view-source: ' + url;
viewsource.style.visibility = 'visible';
},
hide: function sv_hide() {
var viewsource = this.viewer;
if (viewsource) {
viewsource.style.visibility = 'hidden';
viewsource.src = 'about:blank';
}
},
toggle: function sv_toggle() {
this.active ? this.hide() : this.show();
},
handleEvent: function sv_handleEvent(evt) {
switch (evt.type) {
case 'locked':
this.hide();
break;
}
}
};
/* === Debug GridView === */
var GridView = {
get grid() {
return document.getElementById('debug-grid');
},
get visible() {
return this.grid && this.grid.style.display === 'block';
},
hide: function gv_hide() {
if (this.grid)
this.grid.style.display = 'none';
},
show: function gv_show() {
var grid = this.grid;
if (!grid) {
var style = '#debug-grid {' +
' position: absolute;' +
' top: 0;' +
' left: 0;' +
' display: none;' +
' width: 480px;' +
' height: 800px;' +
' background: url(images/grid.png);' +
' z-index: 20002;' +
' opacity: 0.2;' +
' pointer-events: none;' +
'}';
document.styleSheets[0].insertRule(style, 0);
grid = document.createElement('div');
grid.id = 'debug-grid';
document.body.appendChild(grid);
}
grid.style.display = 'block';
},
toggle: function gv_toggle() {
this.visible ? this.hide() : this.show();
}
};
SettingsListener.observe('debug.grid.enabled', false, function(value) {
!!value ? GridView.show() : GridView.hide();
});
/* === Language === */
SettingsListener.observe('language.current', 'en-US', function(value) {
// change language -- this triggers startup() and a rebuild
document.mozL10n.language.code = value;
});
/* === Wallpapers === */
SettingsListener.observe('homescreen.wallpaper', 'default.png',
function(value) {
var home = document.getElementById('home');
home.style.backgroundImage =
'url(style/themes/default/backgrounds/' + value + ')';
}
);
/* === Ring Tone === */
var selectedPhoneSound = '';
SettingsListener.observe('homescreen.ring', 'classic.wav', function(value) {
selectedPhoneSound = 'style/ringtones/' + value;
});
var activePhoneSound = true;
SettingsListener.observe('phone.ring.incoming', true, function(value) {
activePhoneSound = !!value;
});
/* === Vibration === */
var activateVibration = false;
SettingsListener.observe('phone.vibration.incoming', false, function(value) {
activateVibration = !!value;
});
/* === Invert Display === */
SettingsListener.observe('accessibility.invert', false, function(value) {
var screen = document.getElementById('screen');
if (value)
screen.classList.add('accessibility-invert');
else
screen.classList.remove('accessibility-invert');
});
/* === KeyHandler === */
var KeyHandler = {
kRepeatTimeout: 700,
kRepeatRate: 100,
repeatKey: function kh_repeatKey(actionCallback) {
actionCallback();
clearTimeout(this._timer);
this._timer = setTimeout((function volumeTimeout() {
actionCallback();
this._timer = setInterval(function volumeInterval() {
actionCallback();
}, this.kRepeatRate);
}).bind(this), this.kRepeatTimeout);
},
handleEvent: function kh_handleEvent(evt) {
if (!navigator.mozPower.screenEnabled)
return;
switch (evt.type) {
case 'keydown':
switch (evt.keyCode) {
case evt.DOM_VK_PAGE_UP:
this.repeatKey((function repeatKeyCallback() {
if (SoundManager.currentVolume == 10) {
clearTimeout(this._timer);
return;
}
SoundManager.changeVolume(1);
}).bind(this));
break;
case evt.DOM_VK_PAGE_DOWN:
this.repeatKey((function repeatKeyCallback() {
if (SoundManager.currentVolume == 0) {
clearTimeout(this._timer);
return;
}
SoundManager.changeVolume(-1);
}).bind(this));
break;
}
break;
case 'keyup':
switch (evt.keyCode) {
case evt.DOM_VK_PAGE_UP:
case evt.DOM_VK_PAGE_DOWN:
clearTimeout(this._timer);
break;
case evt.DOM_VK_CONTEXT_MENU:
SourceView.toggle();
break;
case evt.DOM_VK_F6:
document.location.reload();
break;
}
break;
}
}
};
window.addEventListener('keydown', KeyHandler);
window.addEventListener('keyup', KeyHandler);
/* === Screen Manager === */
var ScreenManager = {
preferredBrightness: 0.5,
toggleScreen: function lockscreen_toggleScreen() {
if (navigator.mozPower.screenEnabled)
this.turnScreenOff();
else
this.turnScreenOn();
},
turnScreenOff: function lockscreen_turnScreenOff() {
if (!navigator.mozPower.screenEnabled)
return false;
navigator.mozPower.screenEnabled = false;
this.preferredBrightness = navigator.mozPower.screenBrightness;
navigator.mozPower.screenBrightness = 0.0;
updateClock();
updateBattery();
updateConnection();
return true;
},
turnScreenOn: function lockscreen_turnScreenOn() {
if (navigator.mozPower.screenEnabled)
return false;
navigator.mozPower.screenEnabled = true;
navigator.mozPower.screenBrightness = this.preferredBrightness;
updateClock();
updateBattery();
updateConnection();
return true;
}
};
SettingsListener.observe('screen.brightness', 0.5, function(value) {