Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/apps/system/js/sleep_menu.js
blob: a97b7728955a11cd7d198acaf84a9c3e39444c33 (plain)
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
/* -*- 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 SleepMenu = {
  // Indicate setting status of ril.radio.disabled
  isFlightModeEnabled: false,

  // Indicate setting status of volume
  isSilentModeEnabled: false,

  elements: {},

  get visible() {
    return this.elements.overlay.classList.contains('visible');
  },

  getAllElements: function sm_getAllElements() {
    this.elements.overlay = document.getElementById('sleep-menu');
    this.elements.container =
      document.querySelector('#sleep-menu-container ul');
    this.elements.cancel = document.querySelector('#sleep-menu button');
  },

  init: function sm_init() {
    this.getAllElements();
    window.addEventListener('holdsleep', this.show.bind(this));
    window.addEventListener('click', this, true);
    window.addEventListener('screenchange', this, true);
    window.addEventListener('home', this);
    this.elements.cancel.addEventListener('click', this);

    var self = this;
    SettingsListener.observe('ril.radio.disabled', false, function(value) {
      self.isFlightModeEnabled = value;
    });

    var settings = navigator.mozSettings;
    SettingsListener.observe('audio.volume.notification', 7, function(value) {
      settings.createLock().set({'ring.enabled': (value != 0)});
    });

    SettingsListener.observe('ring.enabled', true, function(value) {
      self.isSilentModeEnabled = !value;
    });
  },

  // Generate items
  generateItems: function sm_generateItems() {
    var items = [];
    var _ = navigator.mozL10n.get;
    var options = {
      airplane: {
        label: _('airplane'),
        value: 'airplane',
        icon: '/style/sleep_menu/images/airplane.png'
      },
      airplaneOff: {
        label: _('airplaneOff'),
        value: 'airplane'
      },
      silent: {
        label: _('silent'),
        value: 'silent',
        icon: '/style/sleep_menu/images/vibration.png'
      },
      silentOff: {
        label: _('normal'),
        value: 'silentOff'
      },
      restart: {
        label: _('restart'),
        value: 'restart',
        icon: '/style/sleep_menu/images/restart.png'
      },
      power: {
        label: _('power'),
        value: 'power',
        icon: '/style/sleep_menu/images/power-off.png'
      }
    };

    if (this.isFlightModeEnabled) {
      items.push(options.airplaneOff);
    } else {
      items.push(options.airplane);
    }

    if (!this.isSilentModeEnabled) {
      items.push(options.silent);
    } else {
      items.push(options.silentOff);
    }

    items.push(options.restart);
    items.push(options.power);

    return items;
  },

  show: function sm_show() {
    this.elements.container.innerHTML = '';
    this.buildMenu(this.generateItems());
    this.elements.overlay.classList.add('visible');
  },

  buildMenu: function sm_buildMenu(items) {
    items.forEach(function traveseItems(item) {
      var item_li = document.createElement('li');
      item_li.dataset.value = item.value;
      item_li.textContent = item.label;
      this.elements.container.appendChild(item_li);
    }, this);
  },

  hide: function lm_hide() {
    this.elements.overlay.classList.remove('visible');
  },

  handleEvent: function sm_handleEvent(evt) {
    switch (evt.type) {
      case 'screenchange':
        if (!evt.detail.screenEnabled)
          this.hide();
        break;

      case 'click':
        if (!this.visible)
          return;

        if (evt.currentTarget === this.elements.cancel) {
          this.hide();
          return;
        }

        var action = evt.target.dataset.value;
        if (!action) {
          return;
        }
        this.hide();
        this.handler(action);
        break;

      case 'home':
        if (this.visible) {
          this.hide();
        }
        break;
    }
  },

  handler: function sm_handler(action) {
    switch (action) {
      case 'airplane':
        // Airplane mode should turn off
        //
        // Radio ('ril.radio.disabled'`)
        // Data ('ril.data.enabled'`)
        // Wifi
        // Bluetooth
        // Geolocation
        //
        // It should also save the status of the latter 4 items
        // so when leaving the airplane mode we could know which one to turn on.

        if (!window.navigator.mozSettings)
          return;

        SettingsListener.getSettingsLock().set({
          'ril.radio.disabled': !this.isFlightModeEnabled
        });

        break;

      // About silent and silentOff
      // * Turn on silent mode will cause:
      //   * Turn off ringtone no matter if ring is on or off
      //   * for sms and incoming calls.
      // * Turn off silent mode will cause:
      //   * Turn on ringtone no matter if ring is on or off
      //   * for sms and incoming calls.
      case 'silent':
        if (!window.navigator.mozSettings)
          return;

        SettingsListener.getSettingsLock().set({
          'ring.enabled': false
        });
        this.isSilentModeEnabled = true;

        break;

      case 'silentOff':
        if (!window.navigator.mozSettings)
          return;

        SettingsListener.getSettingsLock().set({
          'ring.enabled': true
        });
        this.isSilentModeEnabled = false;

        break;

      case 'restart':
        this.startPowerOff(true);

        break;

      case 'power':
        this.startPowerOff(false);

        break;
    }
  },

  startPowerOff: function sm_startPowerOff(reboot) {
    var power = navigator.mozPower;
    if (!power)
      return;

    // Early return if we are already shutting down.
    if (document.getElementById('poweroff-splash'))
      return;

    // Show shutdown animation before actually performing shutdown.
    //  * step1: fade-in poweroff-splash.
    //  * step2: The 3-rings animation is performed on the screen.
    var div = document.createElement('div');
    div.dataset.zIndexLevel = 'poweroff-splash';
    div.id = 'poweroff-splash';

    // The overall animation ends when the inner span of the bottom ring
    // is animated, so we store it for detecting.
    var inner;

    for (var i = 1; i <= 3; i++) {
      var outer = document.createElement('span');
      outer.className = 'poweroff-ring';
      outer.id = 'poweroff-ring-' + i;
      div.appendChild(outer);

      inner = document.createElement('span');
      outer.appendChild(inner);
    }

    div.className = 'step1';

    var nextAnimation = function nextAnimation(e) {
      // Switch to next class
      if (e.target == div)
        div.className = 'step2';

      if (e.target != inner)
        return;

      // Actual poweroff/reboot
      setTimeout(function powerOffAnimated() {
        if (reboot) {
          power.reboot();
        } else {
          power.powerOff();
        }
      });

      // Paint screen to black before reboot/poweroff
      ScreenManager.turnScreenOff(true);
    };

    div.addEventListener('animationend', nextAnimation);

    document.getElementById('screen').appendChild(div);
  }
};

SleepMenu.init();