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