summaryrefslogtreecommitdiffstats
path: root/ash/system/power/tray_power_date.cc
blob: 1e97865b7b32ac80e40ebeff5ae90cf67b18bd0d (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
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
// 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.

#include "ash/system/power/tray_power_date.h"

#include "ash/shell.h"
#include "ash/system/power/power_supply_status.h"
#include "ash/system/tray/system_tray_delegate.h"
#include "ash/system/tray/tray_constants.h"
#include "base/i18n/time_formatting.h"
#include "base/stringprintf.h"
#include "base/time.h"
#include "base/timer.h"
#include "base/utf_string_conversions.h"
#include "grit/ui_resources.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkRect.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/size.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/text_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view.h"
#include "unicode/datefmt.h"
#include "unicode/fieldpos.h"
#include "unicode/fmtable.h"

namespace ash {
namespace internal {

namespace {
// Width and height of battery images.
const int kBatteryImageHeight = 25;
const int kBatteryImageWidth = 25;
// Number of different power states.
const int kNumPowerImages = 16;
// Amount of slop to add into the timer to make sure we're into the next minute
// when the timer goes off.
const int kTimerSlopSeconds = 1;

string16 FormatNicely(const base::Time& time) {
  icu::UnicodeString date_string;

  scoped_ptr<icu::DateFormat> formatter(
      icu::DateFormat::createDateInstance(icu::DateFormat::kFull));
  icu::FieldPosition position;
  position.setField(UDAT_DAY_OF_WEEK_FIELD);
  formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000),
                    date_string,
                    position);
  icu::UnicodeString day = date_string.retainBetween(position.getBeginIndex(),
                                                     position.getEndIndex());

  date_string.remove();
  formatter.reset(
      icu::DateFormat::createDateInstance(icu::DateFormat::kMedium));
  formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), date_string);

  date_string += "\n";
  date_string += day;

  return string16(date_string.getBuffer(),
                  static_cast<size_t>(date_string.length()));
}

}

namespace tray {

// This view is used for both the tray and the popup.
class DateView : public views::View {
 public:
  enum TimeType {
    TIME,
    DATE
  };

  explicit DateView(TimeType type)
      : hour_type_(ash::Shell::GetInstance()->tray_delegate()->
            GetHourClockType()),
        type_(type),
        actionable_(false) {
    SetLayoutManager(new views::FillLayout());
    label_ = new views::Label;
    label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
    label_->SetBackgroundColor(SkColorSetARGB(0, 255, 255, 255));
    UpdateText();
    AddChildView(label_);
  }

  virtual ~DateView() {
    timer_.Stop();
  }

  void UpdateTimeFormat() {
    hour_type_ = ash::Shell::GetInstance()->tray_delegate()->GetHourClockType();
    UpdateText();
  }

  views::Label* label() const { return label_; }

  void set_actionable(bool actionable) { actionable_ = actionable; }

  void UpdateText() {
    base::Time now = base::Time::Now();
    if (type_ == DATE) {
      label_->SetText(FormatNicely(now));
    } else {
      label_->SetText(base::TimeFormatTimeOfDayWithHourClockType(
          now, hour_type_, base::kDropAmPm));
    }

    label_->SetTooltipText(base::TimeFormatFriendlyDate(now));
    SchedulePaint();

    // Try to set the timer to go off at the next change of the minute. We don't
    // want to have the timer go off more than necessary since that will cause
    // the CPU to wake up and consume power.
    base::Time::Exploded exploded;
    now.LocalExplode(&exploded);

    // Often this will be called at minute boundaries, and we'll actually want
    // 60 seconds from now.
    int seconds_left = 60 - exploded.second;
    if (seconds_left == 0)
      seconds_left = 60;

    // Make sure that the timer fires on the next minute. Without this, if it is
    // called just a teeny bit early, then it will skip the next minute.
    seconds_left += kTimerSlopSeconds;

    timer_.Stop();
    timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), this,
        &DateView::UpdateText);
  }

 private:
  // Overridden from views::View.
  virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE {
    if (!actionable_)
      return false;

    ash::Shell::GetInstance()->tray_delegate()->ShowDateSettings();
    return true;
  }

  virtual void OnMouseEntered(const views::MouseEvent& event) OVERRIDE {
    if (!actionable_)
      return;
    gfx::Font font = label_->font();
    label_->SetFont(font.DeriveFont(0,
          font.GetStyle() | gfx::Font::UNDERLINED));
    SchedulePaint();
  }

  virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE {
    if (!actionable_)
      return;
    gfx::Font font = label_->font();
    label_->SetFont(font.DeriveFont(0,
          font.GetStyle() & ~gfx::Font::UNDERLINED));
    SchedulePaint();
  }

  virtual void OnLocaleChanged() OVERRIDE {
    UpdateText();
  }

  base::OneShotTimer<DateView> timer_;
  base::HourClockType hour_type_;
  TimeType type_;
  bool actionable_;
  views::Label* label_;

  DISALLOW_COPY_AND_ASSIGN(DateView);
};

// This view is used only for the tray.
class PowerTrayView : public views::ImageView {
 public:
  PowerTrayView() {
    UpdateImage();
  }

  virtual ~PowerTrayView() {
  }

  void UpdatePowerStatus(const PowerSupplyStatus& status) {
    supply_status_ = status;
    // Sanitize.
    if (supply_status_.battery_is_full)
      supply_status_.battery_percentage = 100.0;

    UpdateImage();
  }

 private:
  void UpdateImage() {
    SkBitmap image;
    gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
        IDR_AURA_UBER_TRAY_POWER_SMALL);

    int image_index = 0;
    if (supply_status_.battery_percentage >= 100) {
      image_index = kNumPowerImages - 1;
    } else if (!supply_status_.battery_is_present) {
      image_index = kNumPowerImages;
    } else {
      image_index = static_cast<int> (
          supply_status_.battery_percentage / 100.0 *
          (kNumPowerImages - 1));
      image_index =
        std::max(std::min(image_index, kNumPowerImages - 2), 0);
    }

    SkIRect region = SkIRect::MakeXYWH(
        supply_status_.line_power_on ? kBatteryImageWidth : 0,
        image_index * kBatteryImageHeight,
        kBatteryImageWidth, kBatteryImageHeight);
    all.ToSkBitmap()->extractSubset(&image, region);

    SetImage(image);
  }

  PowerSupplyStatus supply_status_;

  DISALLOW_COPY_AND_ASSIGN(PowerTrayView);
};

// This view is used only for the popup.
class PowerPopupView : public views::Label {
 public:
  PowerPopupView() {
    SetHorizontalAlignment(ALIGN_RIGHT);
    UpdateText();
  }

  virtual ~PowerPopupView() {
  }

  void UpdatePowerStatus(const PowerSupplyStatus& status) {
    supply_status_ = status;
    // Sanitize.
    if (supply_status_.battery_is_full)
      supply_status_.battery_percentage = 100.0;

    UpdateText();
  }

 private:
  void UpdateText() {
    base::TimeDelta time = base::TimeDelta::FromSeconds(
        supply_status_.line_power_on ?
        supply_status_.battery_seconds_to_full :
        supply_status_.battery_seconds_to_empty);
    int hour = time.InHours();
    int min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
    // TODO: Translation
    SetText(ASCIIToUTF16(base::StringPrintf("Battery: %.0lf%%\n%dh%02dm",
          supply_status_.battery_percentage,
          hour, min)));
  }

  PowerSupplyStatus supply_status_;

  DISALLOW_COPY_AND_ASSIGN(PowerPopupView);
};

}  // namespace tray

TrayPowerDate::TrayPowerDate()
    : power_(NULL),
      power_tray_(NULL) {
}

TrayPowerDate::~TrayPowerDate() {
}

views::View* TrayPowerDate::CreateTrayView(user::LoginStatus status) {
  views::View* container = new views::View;
  container->SetLayoutManager(new views::BoxLayout(
        views::BoxLayout::kHorizontal, 0, 0, kTrayPaddingBetweenItems));

  PowerSupplyStatus power_status =
      ash::Shell::GetInstance()->tray_delegate()->GetPowerSupplyStatus();
  if (power_status.battery_is_present) {
    power_tray_.reset(new tray::PowerTrayView());
    power_tray_->UpdatePowerStatus(power_status);
    container->AddChildView(power_tray_.get());
  }

  date_tray_.reset(new tray::DateView(tray::DateView::TIME));
  date_tray_->label()->SetFont(
      date_tray_->label()->font().DeriveFont(2, gfx::Font::BOLD));
  date_tray_->label()->SetAutoColorReadabilityEnabled(false);
  date_tray_->label()->SetEnabledColor(SK_ColorWHITE);
  container->AddChildView(date_tray_.get());

  return container;
}

views::View* TrayPowerDate::CreateDefaultView(user::LoginStatus status) {
  date_.reset(new tray::DateView(tray::DateView::DATE));
  if (status != user::LOGGED_IN_NONE)
    date_->set_actionable(true);

  views::View* container = new views::View;
  views::BoxLayout* layout = new views::BoxLayout(views::BoxLayout::kHorizontal,
      kTrayPopupPaddingHorizontal, 10, 0);
  layout->set_spread_blank_space(true);
  container->SetLayoutManager(layout);
  container->set_background(views::Background::CreateSolidBackground(
      SkColorSetRGB(245, 245, 245)));
  container->AddChildView(date_.get());

  PowerSupplyStatus power_status =
      ash::Shell::GetInstance()->tray_delegate()->GetPowerSupplyStatus();
  if (power_status.battery_is_present) {
    power_.reset(new tray::PowerPopupView());
    power_->UpdatePowerStatus(power_status);
    container->AddChildView(power_.get());
  }
  return container;
}

views::View* TrayPowerDate::CreateDetailedView(user::LoginStatus status) {
  return NULL;
}

void TrayPowerDate::DestroyTrayView() {
  date_tray_.reset();
  power_tray_.reset();
}

void TrayPowerDate::DestroyDefaultView() {
  date_.reset();
  power_.reset();
}

void TrayPowerDate::DestroyDetailedView() {
}

void TrayPowerDate::OnPowerStatusChanged(const PowerSupplyStatus& status) {
  if (power_tray_.get())
    power_tray_->UpdatePowerStatus(status);
  if (power_.get())
    power_->UpdatePowerStatus(status);
}

void TrayPowerDate::OnDateFormatChanged() {
  date_tray_->UpdateTimeFormat();
}

void TrayPowerDate::Refresh() {
  date_tray_->UpdateText();
}

}  // namespace internal
}  // namespace ash