summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/options/chromeos/system_options.js
blob: 055f7bfb628bfbd4a10b615951a43c90f8b0be0b (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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('options', function() {

  var OptionsPage = options.OptionsPage;
  var RepeatingButton = cr.ui.RepeatingButton;

  /////////////////////////////////////////////////////////////////////////////
  // SystemOptions class:

  /**
   * Encapsulated handling of ChromeOS system options page.
   * @constructor
   */
  function SystemOptions() {
    OptionsPage.call(this, 'system', templateData.systemPageTabTitle,
                     'systemPage');
  }

  cr.addSingletonGetter(SystemOptions);

  // Inherit SystemOptions from OptionsPage.
  SystemOptions.prototype = {
    __proto__: options.OptionsPage.prototype,

    /**
     * Flag indicating if currently scanning for Bluetooth devices.
     * @type {boolean}
     */
    isScanning_: false,

    /**
     * Initializes SystemOptions page.
     * Calls base class implementation to starts preference initialization.
     */
    initializePage: function() {
      OptionsPage.prototype.initializePage.call(this);

      // Disable time-related settings if we're not logged in as a real user.
      if (AccountsOptions.loggedInAsGuest()) {
        var timezone = $('timezone-select');
        if (timezone)
          timezone.disabled = true;
        var use_24hour_clock = $('use-24hour-clock');
        if (use_24hour_clock)
          use_24hour_clock.disabled = true;
      }

      options.system.bluetooth.BluetoothDeviceList.decorate(
          $('bluetooth-paired-devices-list'));

      $('bluetooth-add-device').onclick = function(event) {
        if (! this.isScanning_)
          findBluetoothDevices_(true);
        OptionsPage.navigateToPage('bluetooth');
      };

      $('enable-bluetooth').onchange = function(event) {
        var state = $('enable-bluetooth').checked;
        chrome.send('bluetoothEnableChange', [Boolean(state)]);
      };

      $('bluetooth-reconnect-device').onclick = function(event) {
        var device = $('bluetooth-paired-devices-list').selectedItem;
        var address = device.address;
        chrome.send('updateBluetoothDevice', [address, 'connect']);
        OptionsPage.closeOverlay();
      };

      $('bluetooth-reconnect-device').onmousedown = function(event) {
        // Prevent 'blur' event, which would reset the list selection,
        // thereby disabling the apply button.
        event.preventDefault();
      };

      $('bluetooth-paired-devices-list').addEventListener('change', function() {
        var item = $('bluetooth-paired-devices-list').selectedItem;
        var disabled = !item || !item.paired || item.connected;
        $('bluetooth-reconnect-device').disabled = disabled;
      });

      $('language-button').onclick = function(event) {
        OptionsPage.navigateToPage('language');
      };
      $('modifier-keys-button').onclick = function(event) {
        OptionsPage.navigateToPage('languageCustomizeModifierKeysOverlay');
      };
      $('accessibility-spoken-feedback-check').onchange = function(event) {
        chrome.send('spokenFeedbackChange',
                    [$('accessibility-spoken-feedback-check').checked]);
      };
      $('accessibility-high-contrast-check').onchange = function(event) {
        chrome.send('highContrastChange',
                    [$('accessibility-high-contrast-check').checked]);
      };
      $('accessibility-screen-magnifier-check').onchange = function(event) {
        chrome.send('screenMagnifierChange',
                    [$('accessibility-screen-magnifier-check').checked]);
      };
      $('accessibility-virtual-keyboard-check').onchange = function(event) {
        chrome.send('virtualKeyboardChange',
                    [$('accessibility-virtual-keyboard-check').checked]);
      };
      initializeBrightnessButton_('brightness-decrease-button',
          'decreaseScreenBrightness');
      initializeBrightnessButton_('brightness-increase-button',
          'increaseScreenBrightness');
    }
  };

  /**
   * Initializes a button for controlling screen brightness.
   * @param {string} id Button ID.
   * @param {string} callback Name of the callback function.
   */
  function initializeBrightnessButton_(id, callback) {
    var button = $(id);
    cr.ui.decorate(button, RepeatingButton);
    button.repeatInterval = 300;
    button.addEventListener(RepeatingButton.Event.BUTTON_HELD, function(e) {
      chrome.send(callback);
    });
  }

  /**
   * Scan for bluetooth devices.
   * @param {boolean} reset Indicates if the list of unpaired devices should be
   *     cleared.
   * @private
   */
  function findBluetoothDevices_(reset) {
    this.isScanning_ = true;
    if (reset)
      $('bluetooth-unpaired-devices-list').clear();
    chrome.send('findBluetoothDevices');
  }

  //
  // Chrome callbacks
  //

  /**
   * Set the initial state of the spoken feedback checkbox.
   */
  SystemOptions.setSpokenFeedbackCheckboxState = function(checked) {
    $('accessibility-spoken-feedback-check').checked = checked;
  };

  /**
   * Set the initial state of the high contrast checkbox.
   */
  SystemOptions.setHighContrastCheckboxState = function(checked) {
    $('accessibility-high-contrast-check').checked = checked;
  };

  /**
   * Set the initial state of the screen magnifier checkbox.
   */
  SystemOptions.setScreenMagnifierCheckboxState = function(checked) {
    $('accessibility-screen-magnifier-check').checked = checked;
  };

  /**
   * Set the initial state of the virtual keyboard checkbox.
   */
  SystemOptions.setVirtualKeyboardCheckboxState = function(checked) {
    $('accessibility-virtual-keyboard-check').checked = checked;
  };

  /**
   * Activate the bluetooth settings section on the System settings page.
   */
  SystemOptions.showBluetoothSettings = function() {
    $('bluetooth-devices').hidden = false;
  };

  /**
   * Sets the state of the checkbox indicating if bluetooth is turned on. The
   * state of the "Find devices" button and the list of discovered devices may
   * also be affected by a change to the state.
   * @param {boolean} checked Flag Indicating if Bluetooth is turned on.
   */
  SystemOptions.setBluetoothState = function(checked) {
    $('enable-bluetooth').checked = checked;
    $('bluetooth-paired-devices-list').parentNode.hidden = !checked;
    $('bluetooth-add-device').hidden = !checked;
    $('bluetooth-reconnect-device').hidden = !checked;
    // Flush list of previously discovered devices if bluetooth is turned off.
    if (!checked) {
      $('bluetooth-paired-devices-list').clear();
      $('bluetooth-unpaired-devices-list').clear();
    }
    // TODO(kevers): Replace following with a call to fetch the list of
    // previously discovered devices rather than searching for all available
    // devices.
    if (checked && ! this.isScanning_)
      findBluetoothDevices_(true);
  }

  /**
   * Adds an element to the list of available bluetooth devices. If an element
   * with a matching address is found, the existing element is updated.
   * @param {{name: string,
   *          address: string,
   *          icon: string,
   *          paired: boolean,
   *          connected: boolean}} device
   *     Decription of the bluetooth device.
   */
  SystemOptions.addBluetoothDevice = function(device) {
    var list = $('bluetooth-unpaired-devices-list');
    if (device.paired) {
      // Test to see if the device is currently in the unpaired list, in which
      // case it should be removed from that list.
      var index = $('bluetooth-unpaired-devices-list').find(device.address);
      if (index != undefined)
        $('bluetooth-unpaired-devices-list').deleteItemAtIndex(index);
      list = $('bluetooth-paired-devices-list');
    }
    list.appendDevice(device);

    // One device can be in the process of pairing.  If found, display
    // the Bluetooth pairing overlay.
    if (device.pairing)
      BluetoothPairing.showDialog(device);
  };

  /**
   * Notification that a single pass of device discovery has completed.
   */
  SystemOptions.notifyBluetoothSearchComplete = function() {
    // TODO(kevers): Determine the fate of this method once continuous
    // scanning is implemented in the Bluetooth code.
    this.isScanning_ = false;
  };

  /**
   * Displays the touchpad controls section when we detect a touchpad, hides it
   * otherwise.
   */
  SystemOptions.showTouchpadControls = function(show) {
    $('touchpad-controls').hidden = !show;
  };

  /**
   * Displays the mouse controls section when we detect a mouse, hides it
   * otherwise.
   */
  SystemOptions.showMouseControls = function(show) {
    $('mouse-controls').hidden = !show;
  };

  // Export
  return {
    SystemOptions: SystemOptions
  };

});