| OLD | NEW |
| 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 "ui/base/l10n/l10n_util.h" | 11 #include "ui/base/l10n/l10n_util.h" |
| 12 | 12 |
| 13 namespace chromeos { | 13 namespace chromeos { |
| 14 | 14 |
| 15 LowBatteryObserver::LowBatteryObserver(Profile* profile) | 15 LowBatteryObserver::LowBatteryObserver(Profile* profile) |
| 16 : notification_(profile, "battery.chromeos", | 16 : notification_(profile, "battery.chromeos", |
| 17 IDR_NOTIFICATION_LOW_BATTERY, | 17 IDR_NOTIFICATION_LOW_BATTERY, |
| 18 l10n_util::GetStringUTF16(IDS_LOW_BATTERY_TITLE)), | 18 l10n_util::GetStringUTF16(IDS_LOW_BATTERY_TITLE)), |
| 19 remaining_(0) {} | 19 remaining_(0) {} |
| 20 | 20 |
| 21 LowBatteryObserver::~LowBatteryObserver() { | 21 LowBatteryObserver::~LowBatteryObserver() { |
| 22 Hide(); | 22 Hide(); |
| 23 } | 23 } |
| 24 | 24 |
| 25 void LowBatteryObserver::PowerChanged(PowerLibrary* power_lib) { | 25 void LowBatteryObserver::PowerChanged(const PowerSupplyStatus& power_status) { |
| 26 const int limit_min = 15; // Notification will show when remaining number | 26 // Notification will show when remaining number of minutes is <= limit_min. |
| 27 // of minutes is <= limit. | 27 const base::TimeDelta limit_min = base::TimeDelta::FromMinutes(15); |
| 28 const int limit_max = 30; // Notification will hid when remaining number | 28 // Notification will hide when remaining number of minutes is > limit_max. |
| 29 // of minutes is > limit_max. | 29 const base::TimeDelta limit_max = base::TimeDelta::FromMinutes(30); |
| 30 const int critical = 5; // Notification will be forced visible if hidden | 30 // Notification will be forced visible if hidden by user when time remaining |
| 31 // by user when time remaining <= critical. | 31 // <= critical. |
| 32 const base::TimeDelta critical = base::TimeDelta::FromMinutes(5); |
| 32 | 33 |
| 33 base::TimeDelta remaining = power_lib->GetBatteryTimeToEmpty(); | 34 base::TimeDelta remaining = |
| 34 int remaining_minutes = remaining.InMinutes(); | 35 base::TimeDelta::FromSeconds(power_status.battery_seconds_to_empty); |
| 35 | 36 |
| 36 // To simplify the logic - we handle the case of calculating the remaining | 37 // To simplify the logic - we handle the case of calculating the remaining |
| 37 // time as if we were on line power. | 38 // time as if we were on line power. |
| 38 // remaining time of zero means still calculating, this is denoted by | 39 // remaining time of zero means still calculating, this is denoted by |
| 39 // base::TimeDelta(). | 40 // base::TimeDelta(). |
| 40 bool line_power = power_lib->IsLinePowerOn() || | 41 bool line_power = power_status.line_power_on || |
| 41 remaining == base::TimeDelta(); | 42 remaining == base::TimeDelta(); |
| 42 | 43 |
| 43 // The urgent flag is used to re-notify the user if the power level | 44 // 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 | 45 // 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. | 46 // goes back up (so long as it doesn't go above limit_max. |
| 46 bool urgent = !line_power && | 47 bool urgent = !line_power && |
| 47 (notification_.urgent() || remaining_minutes <= critical); | 48 (notification_.urgent() || remaining <= critical); |
| 48 | 49 |
| 49 // This is a simple state machine with two states and three edges: | 50 // This is a simple state machine with two states and three edges: |
| 50 // States: visible_, !visible_ | 51 // States: visible_, !visible_ |
| 51 // Edges: hide: is visible_ to !visible_ triggered if we transition | 52 // Edges: hide: is visible_ to !visible_ triggered if we transition |
| 52 // to line_power, we're calculating our time remaining, | 53 // to line_power, we're calculating our time remaining, |
| 53 // or our time remaining has climbed higher than | 54 // or our time remaining has climbed higher than |
| 54 // limit_max (either by reduced user an undetected transition | 55 // limit_max (either by reduced user an undetected transition |
| 55 // to/from line_power). | 56 // to/from line_power). |
| 56 // update: is visible_ to _visible triggered when we didn't hide | 57 // update: is visible_ to _visible triggered when we didn't hide |
| 57 // and the minutes remaining changed from what's shown. | 58 // and the minutes remaining changed from what's shown. |
| 58 // show: is !visible_ to visible_ triggered when we're on battery, | 59 // show: is !visible_ to visible_ triggered when we're on battery, |
| 59 // we know the remaining time, and that time is less than limit. | 60 // we know the remaining time, and that time is less than limit. |
| 60 | 61 |
| 61 if (notification_.visible()) { | 62 if (notification_.visible()) { |
| 62 if (line_power || remaining_minutes > limit_max) { | 63 if (line_power || remaining > limit_max) { |
| 63 Hide(); | 64 Hide(); |
| 64 } else if (remaining_minutes != remaining_) { | 65 } else if (remaining.InMinutes() != remaining_) { |
| 65 Show(remaining, urgent); | 66 Show(remaining, urgent); |
| 66 } | 67 } |
| 67 } else { | 68 } else { |
| 68 if (!line_power && remaining_minutes <= limit_min) { | 69 if (!line_power && remaining <= limit_min) { |
| 69 Show(remaining, urgent); | 70 Show(remaining, urgent); |
| 70 } | 71 } |
| 71 } | 72 } |
| 72 } | 73 } |
| 73 | 74 |
| 74 void LowBatteryObserver::Show(base::TimeDelta remaining, bool urgent) { | 75 void LowBatteryObserver::Show(base::TimeDelta remaining, bool urgent) { |
| 75 notification_.Show(l10n_util::GetStringFUTF16(IDS_LOW_BATTERY_MESSAGE, | 76 notification_.Show(l10n_util::GetStringFUTF16(IDS_LOW_BATTERY_MESSAGE, |
| 76 TimeFormat::TimeRemaining(remaining)), urgent, true); | 77 TimeFormat::TimeRemaining(remaining)), urgent, true); |
| 77 remaining_ = remaining.InMinutes(); | 78 remaining_ = remaining.InMinutes(); |
| 78 } | 79 } |
| 79 | 80 |
| 80 void LowBatteryObserver::Hide() { | 81 void LowBatteryObserver::Hide() { |
| 81 notification_.Hide(); | 82 notification_.Hide(); |
| 82 } | 83 } |
| 83 | 84 |
| 84 } // namespace chromeos | 85 } // namespace chromeos |
| OLD | NEW |