• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/chromeos/low_battery_observer.h"
6 
7 #include "base/utf_string_conversions.h"
8 #include "chrome/common/time_format.h"
9 #include "grit/generated_resources.h"
10 #include "grit/theme_resources.h"
11 #include "ui/base/l10n/l10n_util.h"
12 
13 namespace chromeos {
14 
LowBatteryObserver(Profile * profile)15 LowBatteryObserver::LowBatteryObserver(Profile* profile)
16   : notification_(profile, "battery.chromeos",
17                   IDR_NOTIFICATION_LOW_BATTERY,
18                   l10n_util::GetStringUTF16(IDS_LOW_BATTERY_TITLE)),
19     remaining_(0) {}
20 
~LowBatteryObserver()21 LowBatteryObserver::~LowBatteryObserver() {
22   Hide();
23 }
24 
PowerChanged(PowerLibrary * object)25 void LowBatteryObserver::PowerChanged(PowerLibrary* object) {
26   const int limit_min = 15;   // Notification will show when remaining number
27                               // of minutes is <= limit.
28   const int limit_max = 30;   // Notification will hid when remaining number
29                               // of minutes is > limit_max.
30   const int critical = 5;  // Notification will be forced visible if hidden
31                            // by user when time remaining <= critical.
32 
33   base::TimeDelta remaining = object->battery_time_to_empty();
34   int remaining_minutes = remaining.InMinutes();
35 
36   // To simplify the logic - we handle the case of calculating the remaining
37   // time as if we were on line power.
38   // remaining time of zero means still calculating, this is denoted by
39   // base::TimeDelta().
40   bool line_power = object->line_power_on() || remaining == base::TimeDelta();
41 
42   // The urgent flag is used to re-notify the user if the power level
43   // goes critical. We only want to do this once even if the time remaining
44   // goes back up (so long as it doesn't go above limit_max.
45   bool urgent = !line_power &&
46       (notification_.urgent() || remaining_minutes <= critical);
47 
48   // This is a simple state machine with two states and three edges:
49   // States: visible_, !visible_
50   // Edges: hide: is visible_ to !visible_ triggered if we transition
51   //          to line_power, we're calculating our time remaining,
52   //          or our time remaining has climbed higher than
53   //          limit_max (either by reduced user an undetected transition
54   //          to/from line_power).
55   //        update: is visible_ to _visible triggered when we didn't hide
56   //          and the minutes remaining changed from what's shown.
57   //        show: is !visible_ to visible_ triggered when we're on battery,
58   //          we know the remaining time, and that time is less than limit.
59 
60   if (notification_.visible()) {
61     if (line_power || remaining_minutes > limit_max) {
62       Hide();
63     } else if (remaining_minutes != remaining_) {
64       Show(remaining, urgent);
65     }
66   } else {
67     if (!line_power && remaining_minutes <= limit_min) {
68       Show(remaining, urgent);
69     }
70   }
71 }
72 
Show(base::TimeDelta remaining,bool urgent)73 void LowBatteryObserver::Show(base::TimeDelta remaining, bool urgent) {
74   notification_.Show(l10n_util::GetStringFUTF16(IDS_LOW_BATTERY_MESSAGE,
75       TimeFormat::TimeRemaining(remaining)), urgent, true);
76   remaining_ = remaining.InMinutes();
77 }
78 
Hide()79 void LowBatteryObserver::Hide() {
80   notification_.Hide();
81 }
82 
83 }  // namespace chromeos
84