Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(415)

Side by Side Diff: chrome/browser/chromeos/low_battery_observer.cc

Issue 8347016: chromeos: Simplify power supply info in PowerLibrary (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: support unit testing, added power supply status struct Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "chrome/common/time_format.h" 8 #include "chrome/common/time_format.h"
9 #include "grit/generated_resources.h" 9 #include "grit/generated_resources.h"
10 #include "grit/theme_resources.h" 10 #include "grit/theme_resources.h"
11 #include "third_party/cros/chromeos_power.h"
satorux1 2011/10/20 04:32:40 I thought we don't need this?
Simon Que 2011/10/20 21:18:53 Done.
11 #include "ui/base/l10n/l10n_util.h" 12 #include "ui/base/l10n/l10n_util.h"
12 13
13 namespace chromeos { 14 namespace chromeos {
14 15
15 LowBatteryObserver::LowBatteryObserver(Profile* profile) 16 LowBatteryObserver::LowBatteryObserver(Profile* profile)
16 : notification_(profile, "battery.chromeos", 17 : notification_(profile, "battery.chromeos",
17 IDR_NOTIFICATION_LOW_BATTERY, 18 IDR_NOTIFICATION_LOW_BATTERY,
18 l10n_util::GetStringUTF16(IDS_LOW_BATTERY_TITLE)), 19 l10n_util::GetStringUTF16(IDS_LOW_BATTERY_TITLE)),
19 remaining_(0) {} 20 remaining_(0) {}
20 21
21 LowBatteryObserver::~LowBatteryObserver() { 22 LowBatteryObserver::~LowBatteryObserver() {
22 Hide(); 23 Hide();
23 } 24 }
24 25
25 void LowBatteryObserver::PowerChanged(PowerLibrary* power_lib) { 26 void LowBatteryObserver::PowerChanged(const PowerSupplyStatus& power_status) {
26 const int limit_min = 15; // Notification will show when remaining number 27 const int limit_min = 15; // Notification will show when remaining number
27 // of minutes is <= limit. 28 // of minutes is <= limit.
28 const int limit_max = 30; // Notification will hid when remaining number 29 const int limit_max = 30; // Notification will hid when remaining number
29 // of minutes is > limit_max. 30 // of minutes is > limit_max.
30 const int critical = 5; // Notification will be forced visible if hidden 31 const int critical = 5; // Notification will be forced visible if hidden
31 // by user when time remaining <= critical. 32 // by user when time remaining <= critical.
32 33
33 base::TimeDelta remaining = power_lib->GetBatteryTimeToEmpty(); 34 int remaining = power_status.battery_time_to_empty;
satorux1 2011/10/20 04:32:40 What happend to |remaining_minutes| in the orignal
Simon Que 2011/10/20 21:18:53 Fixed. Everything is a TimeDelta now.
34 int remaining_minutes = remaining.InMinutes();
35 35
36 // To simplify the logic - we handle the case of calculating the remaining 36 // To simplify the logic - we handle the case of calculating the remaining
37 // time as if we were on line power. 37 // time as if we were on line power.
38 // remaining time of zero means still calculating, this is denoted by 38 // remaining time of zero means still calculating, this is denoted by
39 // base::TimeDelta(). 39 // base::TimeDelta().
40 bool line_power = power_lib->IsLinePowerOn() || 40 bool line_power = power_status.line_power_on || remaining == 0;
41 remaining == base::TimeDelta();
42 41
43 // The urgent flag is used to re-notify the user if the power level 42 // The urgent flag is used to re-notify the user if the power level
44 // goes critical. We only want to do this once even if the time remaining 43 // goes critical. We only want to do this once even if the time remaining
45 // goes back up (so long as it doesn't go above limit_max. 44 // goes back up (so long as it doesn't go above limit_max.
46 bool urgent = !line_power && 45 bool urgent = !line_power &&
47 (notification_.urgent() || remaining_minutes <= critical); 46 (notification_.urgent() || remaining <= critical);
satorux1 2011/10/20 04:32:40 This looks wrong. The orignal code was using remai
Simon Que 2011/10/20 21:18:53 Done.
48 47
49 // This is a simple state machine with two states and three edges: 48 // This is a simple state machine with two states and three edges:
50 // States: visible_, !visible_ 49 // States: visible_, !visible_
51 // Edges: hide: is visible_ to !visible_ triggered if we transition 50 // Edges: hide: is visible_ to !visible_ triggered if we transition
52 // to line_power, we're calculating our time remaining, 51 // to line_power, we're calculating our time remaining,
53 // or our time remaining has climbed higher than 52 // or our time remaining has climbed higher than
54 // limit_max (either by reduced user an undetected transition 53 // limit_max (either by reduced user an undetected transition
55 // to/from line_power). 54 // to/from line_power).
56 // update: is visible_ to _visible triggered when we didn't hide 55 // update: is visible_ to _visible triggered when we didn't hide
57 // and the minutes remaining changed from what's shown. 56 // and the minutes remaining changed from what's shown.
58 // show: is !visible_ to visible_ triggered when we're on battery, 57 // show: is !visible_ to visible_ triggered when we're on battery,
59 // we know the remaining time, and that time is less than limit. 58 // we know the remaining time, and that time is less than limit.
60 59
61 if (notification_.visible()) { 60 if (notification_.visible()) {
62 if (line_power || remaining_minutes > limit_max) { 61 if (line_power || remaining > limit_max) {
63 Hide(); 62 Hide();
64 } else if (remaining_minutes != remaining_) { 63 } else if (remaining != remaining_) {
65 Show(remaining, urgent); 64 Show(base::TimeDelta::FromSeconds(remaining), urgent);
66 } 65 }
67 } else { 66 } else {
68 if (!line_power && remaining_minutes <= limit_min) { 67 if (!line_power && remaining <= limit_min) {
69 Show(remaining, urgent); 68 Show(base::TimeDelta::FromSeconds(remaining), urgent);
70 } 69 }
71 } 70 }
72 } 71 }
73 72
74 void LowBatteryObserver::Show(base::TimeDelta remaining, bool urgent) { 73 void LowBatteryObserver::Show(base::TimeDelta remaining, bool urgent) {
75 notification_.Show(l10n_util::GetStringFUTF16(IDS_LOW_BATTERY_MESSAGE, 74 notification_.Show(l10n_util::GetStringFUTF16(IDS_LOW_BATTERY_MESSAGE,
76 TimeFormat::TimeRemaining(remaining)), urgent, true); 75 TimeFormat::TimeRemaining(remaining)), urgent, true);
77 remaining_ = remaining.InMinutes(); 76 remaining_ = remaining.InMinutes();
78 } 77 }
79 78
80 void LowBatteryObserver::Hide() { 79 void LowBatteryObserver::Hide() {
81 notification_.Hide(); 80 notification_.Hide();
82 } 81 }
83 82
84 } // namespace chromeos 83 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698