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