summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/emulator/battery_settings.js
blob: 9696161813cb61ca96e5fd6cb75ebe2d7a83ab3f (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
// Copyright 2015 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.

var BatterySettings = Polymer({
  is: 'battery-settings',

  properties: {
    /**
     * The system's battery percentage.
     */
    batteryPercent: {
      type: Number,
      observer: 'batteryPercentChanged',
    },

    /**
     * A string representing a value in the
     * PowerSupplyProperties_BatteryState enumeration.
     */
    batteryState: {
      type: String,
      observer: 'batteryStateChanged',
    },

    /**
     * An array representing the battery state options.
     * The names are ordered based on the
     * PowerSupplyProperties_BatteryState enumeration. These values must be
     * in sync.
     */
    batteryStateOptions: {
      type: Array,
      value: function() { return ['Full', 'Charging', 'Disharging',
                                  'Not Present']; },
    },

    /**
     * A string representing a value in the
     * PowerSupplyProperties_ExternalPower enumeration.
     */
    externalPower: {
      type: String,
      observer: 'externalPowerChanged',
    },

    /**
     * An array representing the external power options.
     * The names are ordered based on the
     * PowerSupplyProperties_ExternalPower enumeration. These values must be
     * in sync.
     */
    externalPowerOptions: {
      type: Array,
      value: function() { return ['AC', 'USB (Low Power)', 'Disconnected']; }
    },

    /**
     * A string representing the time left until the battery is discharged.
     */
    timeUntilEmpty: {
      type: String,
      observer: 'timeUntilEmptyChanged',
    },

    /**
     * A string representing the time left until the battery is at 100%.
     */
    timeUntilFull: {
      type: String,
      observer: 'timeUntilFullChanged',
    },

    /**
     * The title for the settings section.
     */
    title: {
      type: String,
    },
  },

  ready: function() {
    this.title = 'Power';
  },

  initialize: function() {
    if (!this.initialized) {
      chrome.send('requestPowerInfo');
      this.initialized = true;
    }
  },

  batteryPercentChanged: function(percent, oldPercent) {
    if (oldPercent != undefined)
      chrome.send('updateBatteryPercent', [parseInt(percent)]);
  },

  batteryStateChanged: function(state) {
    // Find the index of the selected battery state.
    var index = this.batteryStateOptions.indexOf(state);
    if (index >= 0)
      chrome.send('updateBatteryState', [index]);
  },

  externalPowerChanged: function(source) {
    // Find the index of the selected power source.
    var index = this.externalPowerOptions.indexOf(source);
    if (index >= 0)
      chrome.send('updateExternalPower', [index]);
  },

  timeUntilEmptyChanged: function(time, oldTime) {
    if (oldTime != undefined)
      chrome.send('updateTimeToEmpty', [parseInt(time)]);
  },

  timeUntilFullChanged: function(time, oldTime) {
    if (oldTime != undefined)
      chrome.send('updateTimeToFull', [parseInt(time)]);
  },

  updatePowerProperties: function(power_properties) {
    this.batteryPercent = power_properties.battery_percent;
    this.batteryState =
        this.batteryStateOptions[power_properties.battery_state];
    this.externalPower =
        this.externalPowerOptions[power_properties.external_power];
    this.timeUntilEmpty = power_properties.battery_time_to_empty_sec;
    this.timeUntilFull = power_properties.battery_time_to_full_sec;
  }
});