summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/low_battery_observer.cc
blob: 400c0248bed62f7a19c0f8bdd9ee599aab803beb (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
// Copyright (c) 2010 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 "chrome/browser/chromeos/low_battery_observer.h"

#include "app/l10n_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/time_format.h"
#include "grit/generated_resources.h"

namespace chromeos {

LowBatteryObserver::LowBatteryObserver(Profile* profile)
  : notification_(profile, "battery.chromeos",
                  l10n_util::GetStringUTF16(IDS_LOW_BATTERY_TITLE)),
    remaining_(0) {}

LowBatteryObserver::~LowBatteryObserver() {
  Hide();
}

void LowBatteryObserver::PowerChanged(PowerLibrary* object) {
  const int limit = 15;  // Notification will show when remaining number
                         // of minutes is <= limit

  base::TimeDelta remaining = object->battery_time_to_empty();
  bool line_power = object->line_power_on();

  // remaining time of zero means still calculating, this is denoted by
  // TimeDelta().

  // This is a simple state machine with two states and three edges:
  // States: visible_, !visible_
  // Edges: hide: is visible_ to !visible_ triggered if we transition
  //          to line_power, we're calculating our time remaining,
  //          or our time remaining has climbed higher than
  //          limit (either by reduced useor an undected transition to/from
  //          line_power).
  //        update: is visible_ to _visible triggered when we didn't hide
  //          and the minutes remaining changed from what's shown.
  //        show: is !visible_ to visible_ triggered when we're on battery,
  //          we know the remaining time, and that time is less than limit.

  if (notification_.visible()) {
    if (line_power || remaining == base::TimeDelta()
        || remaining.InMinutes() > limit) {
      Hide();
    } else if (remaining.InMinutes() != remaining_) {
      Show(remaining);
    }
  } else {
    if (!line_power && remaining != base::TimeDelta()
        && remaining.InMinutes() <= limit) {
      Show(remaining);
    }
  }
}

void LowBatteryObserver::Show(base::TimeDelta remaining) {
  notification_.Show(l10n_util::GetStringFUTF16(IDS_LOW_BATTERY_MESSAGE,
      WideToUTF16(TimeFormat::TimeRemaining(remaining))));
  remaining_ = remaining.InMinutes();
}

void LowBatteryObserver::Hide() {
  notification_.Hide();
}

}  // namespace chromeos