Commit 349541e5 authored by derat@chromium.org's avatar derat@chromium.org

chromeos: Refactor system tray code into PowerStatus.

This attempts to clean up shared Ash system tray code
related to displaying the battery status by moving it into
the PowerStatus class. Other classes no longer use the
PowerSupplyStatus struct, and in a following change, I plan
to remove that struct entirely.

BUG=254173

Review URL: https://chromiumcodereview.appspot.com/18176013

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@209342 0039d316-1c4b-4281-b951-d872f2087c98
parent a877bc13
// Copyright 2013 The Chromium Authors. All rights reserved. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -7,17 +7,56 @@ ...@@ -7,17 +7,56 @@
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/dbus/dbus_thread_manager.h" #include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/power_manager_client.h" #include "chromeos/dbus/power_manager_client.h"
#include "grit/ash_resources.h"
#include "grit/ash_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/rect.h"
namespace ash { namespace ash {
namespace internal { namespace internal {
namespace { namespace {
base::string16 GetBatteryTimeAccessibilityString(int hour, int min) {
DCHECK(hour || min);
if (hour && !min) {
return Shell::GetInstance()->delegate()->GetTimeDurationLongString(
base::TimeDelta::FromHours(hour));
}
if (min && !hour) {
return Shell::GetInstance()->delegate()->GetTimeDurationLongString(
base::TimeDelta::FromMinutes(min));
}
return l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_TRAY_BATTERY_TIME_ACCESSIBLE,
Shell::GetInstance()->delegate()->GetTimeDurationLongString(
base::TimeDelta::FromHours(hour)),
Shell::GetInstance()->delegate()->GetTimeDurationLongString(
base::TimeDelta::FromMinutes(min)));
}
static PowerStatus* g_power_status = NULL; static PowerStatus* g_power_status = NULL;
// Minimum battery percentage rendered in UI.
const int kMinBatteryPercent = 1;
// Width and height of battery images.
const int kBatteryImageHeight = 25;
const int kBatteryImageWidth = 25;
// Number of different power states.
const int kNumPowerImages = 15;
} // namespace } // namespace
// static // static
...@@ -54,6 +93,129 @@ void PowerStatus::RemoveObserver(Observer* observer) { ...@@ -54,6 +93,129 @@ void PowerStatus::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer); observers_.RemoveObserver(observer);
} }
void PowerStatus::RequestStatusUpdate() {
chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
RequestStatusUpdate();
}
bool PowerStatus::IsBatteryPresent() const {
return status_.battery_is_present;
}
bool PowerStatus::IsBatteryFull() const {
return status_.battery_is_full;
}
double PowerStatus::GetBatteryPercent() const {
return status_.battery_percentage;
}
int PowerStatus::GetRoundedBatteryPercent() const {
return std::max(kMinBatteryPercent,
static_cast<int>(status_.battery_percentage + 0.5));
}
bool PowerStatus::IsBatteryTimeBeingCalculated() const {
return status_.is_calculating_battery_time;
}
base::TimeDelta PowerStatus::GetBatteryTimeToEmpty() const {
return base::TimeDelta::FromSeconds(status_.battery_seconds_to_empty);
}
base::TimeDelta PowerStatus::GetBatteryTimeToFull() const {
return base::TimeDelta::FromSeconds(status_.battery_seconds_to_full);
}
bool PowerStatus::IsLinePowerConnected() const {
return status_.line_power_on;
}
bool PowerStatus::IsMainsChargerConnected() const {
return status_.battery_state == chromeos::PowerSupplyStatus::CHARGING;
}
bool PowerStatus::IsUsbChargerConnected() const {
return status_.battery_state == chromeos::PowerSupplyStatus::CONNECTED_TO_USB;
}
gfx::ImageSkia PowerStatus::GetBatteryImage(IconSet icon_set) const {
gfx::Image all;
if (IsUsbChargerConnected()) {
all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
icon_set == ICON_DARK ?
IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE_DARK :
IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE);
} else {
all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
icon_set == ICON_DARK ?
IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL);
}
// Get the horizontal offset in the battery icon array image.
int offset = (IsUsbChargerConnected() || !status_.line_power_on) ? 0 : 1;
// Get the icon index in the battery icon array image.
int index = -1;
if (status_.battery_percentage >= 100) {
index = kNumPowerImages - 1;
} else if (!status_.battery_is_present) {
index = kNumPowerImages;
} else {
index = static_cast<int>(
status_.battery_percentage / 100.0 * (kNumPowerImages - 1));
index = std::max(std::min(index, kNumPowerImages - 2), 0);
}
gfx::Rect region(
offset * kBatteryImageWidth, index * kBatteryImageHeight,
kBatteryImageWidth, kBatteryImageHeight);
return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region);
}
base::string16 PowerStatus::GetAccessibleNameString() const {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
if (status_.line_power_on && status_.battery_is_full) {
return rb.GetLocalizedString(
IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE);
}
base::string16 battery_percentage_accessible = l10n_util::GetStringFUTF16(
IsLinePowerConnected() ?
IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_CHARGING_ACCESSIBLE :
IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ACCESSIBLE,
base::IntToString16(GetRoundedBatteryPercent()));
base::string16 battery_time_accessible = base::string16();
if (IsUsbChargerConnected()) {
battery_time_accessible = rb.GetLocalizedString(
IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE_ACCESSIBLE);
} else {
if (IsBatteryTimeBeingCalculated()) {
battery_time_accessible = rb.GetLocalizedString(
IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING_ACCESSIBLE);
} else {
base::TimeDelta time = IsLinePowerConnected() ? GetBatteryTimeToFull() :
GetBatteryTimeToEmpty();
int hour = time.InHours();
int min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
if (hour || min) {
base::string16 minute = min < 10 ?
ASCIIToUTF16("0") + base::IntToString16(min) :
base::IntToString16(min);
battery_time_accessible =
l10n_util::GetStringFUTF16(
IsLinePowerConnected() ?
IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_ACCESSIBLE :
IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_ACCESSIBLE,
GetBatteryTimeAccessibilityString(hour, min));
}
}
}
return battery_time_accessible.empty() ?
battery_percentage_accessible :
battery_percentage_accessible + ASCIIToUTF16(". ") +
battery_time_accessible;
}
PowerStatus::PowerStatus() { PowerStatus::PowerStatus() {
chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
AddObserver(this); AddObserver(this);
...@@ -66,19 +228,12 @@ PowerStatus::~PowerStatus() { ...@@ -66,19 +228,12 @@ PowerStatus::~PowerStatus() {
RemoveObserver(this); RemoveObserver(this);
} }
void PowerStatus::RequestStatusUpdate() { void PowerStatus::PowerChanged(const chromeos::PowerSupplyStatus& status) {
chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> status_ = status;
RequestStatusUpdate(); if (status_.battery_is_full)
} status_.battery_percentage = 100.0;
chromeos::PowerSupplyStatus PowerStatus::GetPowerSupplyStatus() const {
return power_supply_status_;
}
void PowerStatus::PowerChanged( FOR_EACH_OBSERVER(Observer, observers_, OnPowerStatusChanged());
const chromeos::PowerSupplyStatus& power_status) {
power_supply_status_ = power_status;
FOR_EACH_OBSERVER(Observer, observers_, OnPowerStatusChanged(power_status));
} }
} // namespace internal } // namespace internal
......
// Copyright 2013 The Chromium Authors. All rights reserved. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -8,19 +8,32 @@ ...@@ -8,19 +8,32 @@
#include "ash/ash_export.h" #include "ash/ash_export.h"
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "base/strings/string16.h"
#include "base/time/time.h"
#include "chromeos/dbus/power_manager_client.h" #include "chromeos/dbus/power_manager_client.h"
#include "chromeos/dbus/power_supply_status.h" #include "chromeos/dbus/power_supply_status.h"
#include "ui/gfx/image/image_skia.h"
namespace ash { namespace ash {
namespace internal { namespace internal {
// PowerStatus is a singleton that receives updates about the system's
// power status from chromeos::PowerManagerClient and makes the information
// available to interested classes within Ash.
class ASH_EXPORT PowerStatus : public chromeos::PowerManagerClient::Observer { class ASH_EXPORT PowerStatus : public chromeos::PowerManagerClient::Observer {
public: public:
// Different styles of battery icons.
enum IconSet {
ICON_LIGHT,
ICON_DARK
};
// Interface for classes that wish to be notified when the power status
// has changed.
class Observer { class Observer {
public: public:
// Called when the power status changes. // Called when the power status changes.
virtual void OnPowerStatusChanged( virtual void OnPowerStatusChanged() = 0;
const chromeos::PowerSupplyStatus& power_status) = 0;
protected: protected:
virtual ~Observer() {} virtual ~Observer() {}
...@@ -40,30 +53,69 @@ class ASH_EXPORT PowerStatus : public chromeos::PowerManagerClient::Observer { ...@@ -40,30 +53,69 @@ class ASH_EXPORT PowerStatus : public chromeos::PowerManagerClient::Observer {
// Gets the global instance. Initialize must be called first. // Gets the global instance. Initialize must be called first.
static PowerStatus* Get(); static PowerStatus* Get();
// Adds an observer. void set_status_for_testing(const chromeos::PowerSupplyStatus& status) {
virtual void AddObserver(Observer* observer); status_ = status;
}
// Removes an observer. // Adds or removes an observer.
virtual void RemoveObserver(Observer* observer); void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
// Requests power status update. // Requests updated status from the power manager.
void RequestStatusUpdate(); void RequestStatusUpdate();
// Gets the current power supply status. // Returns true if a battery is present.
chromeos::PowerSupplyStatus GetPowerSupplyStatus() const; bool IsBatteryPresent() const;
// Returns true if the battery is full.
bool IsBatteryFull() const;
// Returns the battery's remaining charge as a value in the range [0.0,
// 100.0].
double GetBatteryPercent() const;
// Returns the battery's remaining charge, rounded to an integer with a
// maximum value of 100.
int GetRoundedBatteryPercent() const;
// Returns true if the battery's time-to-full and time-to-empty estimates
// should not be displayed because the power manager is still calculating
// them.
bool IsBatteryTimeBeingCalculated() const;
// Returns the estimated time until the battery is empty (if line power
// is disconnected) or full (if line power is connected). These estimates
// should only be used if IsBatteryTimeBeingCalculated() returns false.
base::TimeDelta GetBatteryTimeToEmpty() const;
base::TimeDelta GetBatteryTimeToFull() const;
// Returns true if line power (including a charger of any type) is connected.
bool IsLinePowerConnected() const;
// Returns true if an official, non-USB charger is connected.
bool IsMainsChargerConnected() const;
// Returns true if a USB charger (which is likely to only support a low
// charging rate) is connected.
bool IsUsbChargerConnected() const;
// Returns the image that should be shown for the battery's current state.
gfx::ImageSkia GetBatteryImage(IconSet icon_set) const;
// Returns an string describing the current state for accessibility.
base::string16 GetAccessibleNameString() const;
protected: protected:
PowerStatus(); PowerStatus();
private: private:
// Overriden from PowerManagerClient::Observer. // Overriden from PowerManagerClient::Observer.
virtual void PowerChanged( virtual void PowerChanged(const chromeos::PowerSupplyStatus& status) OVERRIDE;
const chromeos::PowerSupplyStatus& power_status) OVERRIDE;
ObserverList<Observer> observers_; ObserverList<Observer> observers_;
// PowerSupplyStatus state. // Current state.
chromeos::PowerSupplyStatus power_supply_status_; chromeos::PowerSupplyStatus status_;
DISALLOW_COPY_AND_ASSIGN(PowerStatus); DISALLOW_COPY_AND_ASSIGN(PowerStatus);
}; };
......
// Copyright 2013 The Chromium Authors. All rights reserved. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -20,32 +20,16 @@ namespace internal { ...@@ -20,32 +20,16 @@ namespace internal {
namespace { namespace {
void ValidatePowerSupplyStatus(const chromeos::PowerSupplyStatus& status) {
EXPECT_TRUE(status.battery_is_present);
EXPECT_GT(status.battery_percentage, 0.0);
if (status.battery_state == chromeos::PowerSupplyStatus::DISCHARGING) {
EXPECT_GT(status.battery_seconds_to_empty, 0);
EXPECT_EQ(status.battery_seconds_to_full, 0);
} else {
EXPECT_GT(status.battery_seconds_to_full, 0);
EXPECT_EQ(status.battery_seconds_to_empty, 0);
}
}
class TestObserver : public PowerStatus::Observer { class TestObserver : public PowerStatus::Observer {
public: public:
TestObserver() : power_changed_count_(0) { TestObserver() : power_changed_count_(0) {}
}
virtual ~TestObserver() {} virtual ~TestObserver() {}
virtual void OnPowerStatusChanged(
const chromeos::PowerSupplyStatus& power_status) OVERRIDE {
++power_changed_count_;
ValidatePowerSupplyStatus(power_status);
}
int power_changed_count() const { return power_changed_count_; } int power_changed_count() const { return power_changed_count_; }
// PowerStatus::Observer overrides:
virtual void OnPowerStatusChanged() OVERRIDE { ++power_changed_count_; }
private: private:
int power_changed_count_; int power_changed_count_;
...@@ -91,9 +75,6 @@ TEST_F(PowerStatusTest, PowerStatusInitializeAndUpdate) { ...@@ -91,9 +75,6 @@ TEST_F(PowerStatusTest, PowerStatusInitializeAndUpdate) {
// power supply status data. // power supply status data.
message_loop_.RunUntilIdle(); message_loop_.RunUntilIdle();
EXPECT_EQ(1, test_observer_->power_changed_count()); EXPECT_EQ(1, test_observer_->power_changed_count());
chromeos::PowerSupplyStatus init_status =
power_status_->GetPowerSupplyStatus();
ValidatePowerSupplyStatus(init_status);
// Test RequestUpdate, test_obsever_ should be notified for power suuply // Test RequestUpdate, test_obsever_ should be notified for power suuply
// status change. // status change.
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "ash/shell.h" #include "ash/shell.h"
#include "ash/shell_delegate.h" #include "ash/shell_delegate.h"
#include "ash/system/chromeos/power/power_status.h"
#include "ash/system/chromeos/power/tray_power.h" #include "ash/system/chromeos/power/tray_power.h"
#include "ash/system/tray/fixed_sized_image_view.h" #include "ash/system/tray/fixed_sized_image_view.h"
#include "ash/system/tray/tray_constants.h" #include "ash/system/tray/tray_constants.h"
...@@ -30,8 +31,6 @@ const int kPaddingVertical = 10; ...@@ -30,8 +31,6 @@ const int kPaddingVertical = 10;
const int kLabelMinWidth = 120; const int kLabelMinWidth = 120;
// Padding between battery status text and battery icon on default view. // Padding between battery status text and battery icon on default view.
const int kPaddingBetweenBatteryStatusAndIcon = 3; const int kPaddingBetweenBatteryStatusAndIcon = 3;
// Minimum battery percentage rendered in UI.
const int kMinBatteryPercent = 1;
} // namespace } // namespace
PowerStatusView::PowerStatusView(ViewType view_type, PowerStatusView::PowerStatusView(ViewType view_type,
...@@ -42,10 +41,8 @@ PowerStatusView::PowerStatusView(ViewType view_type, ...@@ -42,10 +41,8 @@ PowerStatusView::PowerStatusView(ViewType view_type,
time_status_label_(NULL), time_status_label_(NULL),
percentage_label_(NULL), percentage_label_(NULL),
icon_(NULL), icon_(NULL),
icon_image_index_(-1),
icon_image_offset_(0),
battery_charging_unreliable_(false),
view_type_(view_type) { view_type_(view_type) {
PowerStatus::Get()->AddObserver(this);
if (view_type == VIEW_DEFAULT) { if (view_type == VIEW_DEFAULT) {
time_status_label_ = new views::Label; time_status_label_ = new views::Label;
percentage_label_ = new views::Label; percentage_label_ = new views::Label;
...@@ -56,17 +53,22 @@ PowerStatusView::PowerStatusView(ViewType view_type, ...@@ -56,17 +53,22 @@ PowerStatusView::PowerStatusView(ViewType view_type,
time_label_ = new views::Label; time_label_ = new views::Label;
LayoutNotificationView(); LayoutNotificationView();
} }
Update(); OnPowerStatusChanged();
} }
void PowerStatusView::UpdatePowerStatus( PowerStatusView::~PowerStatusView() {
const chromeos::PowerSupplyStatus& status) { PowerStatus::Get()->RemoveObserver(this);
supply_status_ = status; }
// Sanitize.
if (supply_status_.battery_is_full) void PowerStatusView::OnPowerStatusChanged() {
supply_status_.battery_percentage = 100.0; view_type_ == VIEW_DEFAULT ?
UpdateTextForDefaultView() : UpdateTextForNotificationView();
Update(); if (icon_) {
icon_->SetImage(
PowerStatus::Get()->GetBatteryImage(PowerStatus::ICON_DARK));
icon_->SetVisible(true);
}
} }
void PowerStatusView::LayoutDefaultView() { void PowerStatusView::LayoutDefaultView() {
...@@ -107,43 +109,29 @@ void PowerStatusView::LayoutNotificationView() { ...@@ -107,43 +109,29 @@ void PowerStatusView::LayoutNotificationView() {
AddChildView(time_label_); AddChildView(time_label_);
} }
void PowerStatusView::UpdateText() {
view_type_ == VIEW_DEFAULT ?
UpdateTextForDefaultView() : UpdateTextForNotificationView();
accessible_name_ = TrayPower::GetAccessibleNameString(supply_status_);
}
void PowerStatusView::UpdateTextForDefaultView() { void PowerStatusView::UpdateTextForDefaultView() {
const PowerStatus& status = *PowerStatus::Get();
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
base::string16 battery_percentage; base::string16 battery_percentage;
base::string16 battery_time_status; base::string16 battery_time_status;
bool is_charging_unreliable =
TrayPower::IsBatteryChargingUnreliable(supply_status_); if (status.IsLinePowerConnected() && status.IsBatteryFull()) {
if (supply_status_.line_power_on && supply_status_.battery_is_full) {
battery_time_status = battery_time_status =
rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_BATTERY_FULL); rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_BATTERY_FULL);
} else if (supply_status_.battery_percentage < 0.0f) {
battery_time_status =
is_charging_unreliable ?
rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE) :
rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING);
} else { } else {
battery_percentage = l10n_util::GetStringFUTF16( battery_percentage = l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ONLY, IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ONLY,
base::IntToString16(TrayPower::GetRoundedBatteryPercentage( base::IntToString16(status.GetRoundedBatteryPercent()));
supply_status_.battery_percentage))); if (status.IsUsbChargerConnected()) {
if (is_charging_unreliable) {
battery_time_status = rb.GetLocalizedString( battery_time_status = rb.GetLocalizedString(
IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE); IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE);
} else { } else {
if (supply_status_.is_calculating_battery_time) { if (status.IsBatteryTimeBeingCalculated()) {
battery_time_status = battery_time_status =
rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING); rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING);
} else { } else {
base::TimeDelta time = base::TimeDelta::FromSeconds( base::TimeDelta time = status.IsLinePowerConnected() ?
supply_status_.line_power_on ? status.GetBatteryTimeToFull() : status.GetBatteryTimeToEmpty();
supply_status_.battery_seconds_to_full :
supply_status_.battery_seconds_to_empty);
int hour = time.InHours(); int hour = time.InHours();
int min = (time - base::TimeDelta::FromHours(hour)).InMinutes(); int min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
if (hour || min) { if (hour || min) {
...@@ -152,7 +140,7 @@ void PowerStatusView::UpdateTextForDefaultView() { ...@@ -152,7 +140,7 @@ void PowerStatusView::UpdateTextForDefaultView() {
base::IntToString16(min); base::IntToString16(min);
battery_time_status = battery_time_status =
l10n_util::GetStringFUTF16( l10n_util::GetStringFUTF16(
supply_status_.line_power_on ? status.IsLinePowerConnected() ?
IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_SHORT : IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_SHORT :
IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_SHORT, IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_SHORT,
base::IntToString16(hour), base::IntToString16(hour),
...@@ -170,60 +158,44 @@ void PowerStatusView::UpdateTextForDefaultView() { ...@@ -170,60 +158,44 @@ void PowerStatusView::UpdateTextForDefaultView() {
} }
void PowerStatusView::UpdateTextForNotificationView() { void PowerStatusView::UpdateTextForNotificationView() {
int hour = 0; const PowerStatus& status = *PowerStatus::Get();
int min = 0; if (status.IsLinePowerConnected() && status.IsBatteryFull()) {
if (!supply_status_.is_calculating_battery_time) {
base::TimeDelta time = base::TimeDelta::FromSeconds(
supply_status_.line_power_on ?
supply_status_.battery_seconds_to_full :
supply_status_.battery_seconds_to_empty);
hour = time.InHours();
min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
}
bool is_charging_unreliable =
TrayPower::IsBatteryChargingUnreliable(supply_status_);
if (supply_status_.line_power_on && supply_status_.battery_is_full) {
status_label_->SetText( status_label_->SetText(
ui::ResourceBundle::GetSharedInstance().GetLocalizedString( ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
IDS_ASH_STATUS_TRAY_BATTERY_FULL)); IDS_ASH_STATUS_TRAY_BATTERY_FULL));
} else { } else {
if (supply_status_.battery_percentage < 0.0f) { status_label_->SetText(
// If charging is unreliable and no percentage available, we l10n_util::GetStringFUTF16(
// leave the top field, |staus_label|, blank. We do not want to IDS_ASH_STATUS_TRAY_BATTERY_PERCENT,
// show "Calculating". The user is informed in the bottom field, base::IntToString16(status.GetRoundedBatteryPercent())));
// |time_label|, that "Charging not reliable".
status_label_->SetText(
is_charging_unreliable ?
base::string16() :
ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING));
} else {
status_label_->SetText(
l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_TRAY_BATTERY_PERCENT,
base::IntToString16(TrayPower::GetRoundedBatteryPercentage(
supply_status_.battery_percentage))));
}
} }
if (is_charging_unreliable) { int hour = 0;
int min = 0;
if (!status.IsBatteryTimeBeingCalculated()) {
base::TimeDelta time = status.IsLinePowerConnected() ?
status.GetBatteryTimeToFull() : status.GetBatteryTimeToEmpty();
hour = time.InHours();
min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
}
if (status.IsUsbChargerConnected()) {
time_label_->SetText( time_label_->SetText(
ui::ResourceBundle::GetSharedInstance().GetLocalizedString( ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE)); IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE));
} else if (supply_status_.is_calculating_battery_time) { } else if (status.IsBatteryTimeBeingCalculated()) {
time_label_->SetText( time_label_->SetText(
ui::ResourceBundle::GetSharedInstance().GetLocalizedString( ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING)); IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING));
} else if (hour || min) { } else if (hour || min) {
if (supply_status_.line_power_on) { if (status.IsLinePowerConnected()) {
time_label_->SetText( time_label_->SetText(
l10n_util::GetStringFUTF16( l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL, IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL,
base::IntToString16(hour), base::IntToString16(hour),
base::IntToString16(min))); base::IntToString16(min)));
} else { } else {
// This is a low battery warning, which prompts user when battery // This is a low battery warning prompting the user in minutes.
// time left is not much (ie in minutes).
min = hour * 60 + min; min = hour * 60 + min;
ShellDelegate* delegate = Shell::GetInstance()->delegate(); ShellDelegate* delegate = Shell::GetInstance()->delegate();
if (delegate) { if (delegate) {
...@@ -238,54 +210,6 @@ void PowerStatusView::UpdateTextForNotificationView() { ...@@ -238,54 +210,6 @@ void PowerStatusView::UpdateTextForNotificationView() {
} }
} }
base::string16 PowerStatusView::GetBatteryTimeAccessibilityString(
int hour, int min) {
DCHECK(hour || min);
if (hour && !min) {
return Shell::GetInstance()->delegate()->GetTimeDurationLongString(
base::TimeDelta::FromHours(hour));
} else if (min && !hour) {
return Shell::GetInstance()->delegate()->GetTimeDurationLongString(
base::TimeDelta::FromMinutes(min));
} else {
return l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_TRAY_BATTERY_TIME_ACCESSIBLE,
Shell::GetInstance()->delegate()->GetTimeDurationLongString(
base::TimeDelta::FromHours(hour)),
Shell::GetInstance()->delegate()->GetTimeDurationLongString(
base::TimeDelta::FromMinutes(min)));
}
}
void PowerStatusView::UpdateIcon() {
if (icon_) {
int index = TrayPower::GetBatteryImageIndex(supply_status_);
int offset = TrayPower::GetBatteryImageOffset(supply_status_);
bool charging_unreliable =
TrayPower::IsBatteryChargingUnreliable(supply_status_);
if (icon_image_index_ != index ||
icon_image_offset_ != offset ||
battery_charging_unreliable_ != charging_unreliable) {
icon_image_index_ = index;
icon_image_offset_ = offset;
battery_charging_unreliable_ = charging_unreliable;
if (icon_image_index_ != -1) {
icon_->SetImage(
TrayPower::GetBatteryImage(icon_image_index_,
icon_image_offset_,
battery_charging_unreliable_,
ICON_DARK));
}
}
icon_->SetVisible(true);
}
}
void PowerStatusView::Update() {
UpdateText();
UpdateIcon();
}
void PowerStatusView::ChildPreferredSizeChanged(views::View* child) { void PowerStatusView::ChildPreferredSizeChanged(views::View* child) {
PreferredSizeChanged(); PreferredSizeChanged();
} }
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#ifndef ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_VIEW_H_ #ifndef ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_VIEW_H_
#define ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_VIEW_H_ #define ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_VIEW_H_
#include "chromeos/dbus/power_supply_status.h" #include "ash/system/chromeos/power/power_status.h"
#include "ui/views/view.h" #include "ui/views/view.h"
namespace views { namespace views {
...@@ -16,7 +16,7 @@ class Label; ...@@ -16,7 +16,7 @@ class Label;
namespace ash { namespace ash {
namespace internal { namespace internal {
class PowerStatusView : public views::View { class PowerStatusView : public views::View, public PowerStatus::Observer {
public: public:
enum ViewType { enum ViewType {
VIEW_DEFAULT, VIEW_DEFAULT,
...@@ -24,25 +24,21 @@ class PowerStatusView : public views::View { ...@@ -24,25 +24,21 @@ class PowerStatusView : public views::View {
}; };
PowerStatusView(ViewType view_type, bool default_view_right_align); PowerStatusView(ViewType view_type, bool default_view_right_align);
virtual ~PowerStatusView() {} virtual ~PowerStatusView();
void UpdatePowerStatus(const chromeos::PowerSupplyStatus& status);
const base::string16& accessible_name() const { return accessible_name_; }
// Overridden from views::View. // Overridden from views::View.
virtual gfx::Size GetPreferredSize() OVERRIDE; virtual gfx::Size GetPreferredSize() OVERRIDE;
virtual int GetHeightForWidth(int width) OVERRIDE; virtual int GetHeightForWidth(int width) OVERRIDE;
virtual void Layout() OVERRIDE; virtual void Layout() OVERRIDE;
// Overridden from PowerStatus::Observer.
virtual void OnPowerStatusChanged() OVERRIDE;
private: private:
void LayoutDefaultView(); void LayoutDefaultView();
void LayoutNotificationView(); void LayoutNotificationView();
void UpdateText();
void UpdateIcon();
void Update();
void UpdateTextForDefaultView(); void UpdateTextForDefaultView();
void UpdateTextForNotificationView(); void UpdateTextForNotificationView();
base::string16 GetBatteryTimeAccessibilityString(int hour, int min);
// Overridden from views::View. // Overridden from views::View.
virtual void ChildPreferredSizeChanged(views::View* child) OVERRIDE; virtual void ChildPreferredSizeChanged(views::View* child) OVERRIDE;
...@@ -62,24 +58,8 @@ class PowerStatusView : public views::View { ...@@ -62,24 +58,8 @@ class PowerStatusView : public views::View {
// Battery status indicator icon. // Battery status indicator icon.
views::ImageView* icon_; views::ImageView* icon_;
// Index of the current icon in the icon array image, or -1 if unknown.
int icon_image_index_;
// Horizontal offset of the current icon in the icon array image.
int icon_image_offset_;
// Battery charging may be unreliable for non-standard power supplies.
// It may change from charging to discharging frequently depending on
// charger power and current power consumption. We show different UIs
// when in this state. See TrayPower::IsBatteryChargingUnreliable.
bool battery_charging_unreliable_;
ViewType view_type_; ViewType view_type_;
chromeos::PowerSupplyStatus supply_status_;
base::string16 accessible_name_;
DISALLOW_COPY_AND_ASSIGN(PowerStatusView); DISALLOW_COPY_AND_ASSIGN(PowerStatusView);
}; };
......
This diff is collapsed.
...@@ -27,11 +27,6 @@ class PowerNotificationView; ...@@ -27,11 +27,6 @@ class PowerNotificationView;
class PowerTrayView; class PowerTrayView;
} }
enum IconSet {
ICON_LIGHT,
ICON_DARK
};
class ASH_EXPORT TrayPower : public SystemTrayItem, class ASH_EXPORT TrayPower : public SystemTrayItem,
public PowerStatus::Observer { public PowerStatus::Observer {
public: public:
...@@ -50,38 +45,6 @@ class ASH_EXPORT TrayPower : public SystemTrayItem, ...@@ -50,38 +45,6 @@ class ASH_EXPORT TrayPower : public SystemTrayItem,
message_center::MessageCenter* message_center); message_center::MessageCenter* message_center);
virtual ~TrayPower(); virtual ~TrayPower();
// Gets whether battery charging is unreliable for |supply_status|.
// When a non-standard power supply is connected, the battery may
// change from being charged to discharged frequently depending on the
// charger power and power consumption, i.e usage. In this case we
// do not want to show either a charging or discharging state.
static bool IsBatteryChargingUnreliable(
const chromeos::PowerSupplyStatus& supply_status);
// Gets the icon index in the battery icon array image based on
// |supply_status|. If |supply_status| is uncertain about the power state,
// returns -1.
static int GetBatteryImageIndex(
const chromeos::PowerSupplyStatus& supply_status);
// Gets the horizontal offset in the battery icon array image based on
// |supply_status|.
static int GetBatteryImageOffset(
const chromeos::PowerSupplyStatus& supply_status);
// Looks up the actual icon in the icon array image for |image_index|.
static gfx::ImageSkia GetBatteryImage(int image_index,
int image_offset,
bool charging_unreliable,
IconSet icon_set);
// Gets the battery accessible string for |supply_status|.
static base::string16 GetAccessibleNameString(
const chromeos::PowerSupplyStatus& supply_status);
// Gets rounded battery percentage for |battery_percentage|.
static int GetRoundedBatteryPercentage(double battery_percentage);
private: private:
friend class TrayPowerTest; friend class TrayPowerTest;
...@@ -98,31 +61,25 @@ class ASH_EXPORT TrayPower : public SystemTrayItem, ...@@ -98,31 +61,25 @@ class ASH_EXPORT TrayPower : public SystemTrayItem,
ShelfAlignment alignment) OVERRIDE; ShelfAlignment alignment) OVERRIDE;
// Overridden from PowerStatus::Observer. // Overridden from PowerStatus::Observer.
virtual void OnPowerStatusChanged( virtual void OnPowerStatusChanged() OVERRIDE;
const chromeos::PowerSupplyStatus& status) OVERRIDE;
// Requests a power status update.
void RequestStatusUpdate() const;
// Show a notification that a low-power USB charger has been connected. // Show a notification that a low-power USB charger has been connected.
// Returns true if a notification was shown or explicitly hidden. // Returns true if a notification was shown or explicitly hidden.
bool MaybeShowUsbChargerNotification( bool MaybeShowUsbChargerNotification();
const chromeos::PowerSupplyStatus& old_status,
const chromeos::PowerSupplyStatus& new_status);
// Sets |notification_state_|. Returns true if a notification should be shown. // Sets |notification_state_|. Returns true if a notification should be shown.
bool UpdateNotificationState(const chromeos::PowerSupplyStatus& status); bool UpdateNotificationState();
bool UpdateNotificationStateForRemainingTime(int remaining_seconds); bool UpdateNotificationStateForRemainingTime();
bool UpdateNotificationStateForRemainingPercentage( bool UpdateNotificationStateForRemainingPercentage();
double remaining_percentage);
message_center::MessageCenter* message_center_; // Not owned. message_center::MessageCenter* message_center_; // Not owned.
tray::PowerTrayView* power_tray_; tray::PowerTrayView* power_tray_;
tray::PowerNotificationView* notification_view_; tray::PowerNotificationView* notification_view_;
NotificationState notification_state_; NotificationState notification_state_;
// Power supply status at the last update. // Was a USB charger connected the last time OnPowerStatusChanged() was
chromeos::PowerSupplyStatus last_power_supply_status_; // called?
bool usb_charger_was_connected_;
DISALLOW_COPY_AND_ASSIGN(TrayPower); DISALLOW_COPY_AND_ASSIGN(TrayPower);
}; };
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "ash/ash_switches.h" #include "ash/ash_switches.h"
#include "ash/test/ash_test_base.h" #include "ash/test/ash_test_base.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "chromeos/dbus/power_supply_status.h"
#include "ui/message_center/fake_message_center.h" #include "ui/message_center/fake_message_center.h"
using chromeos::PowerSupplyStatus; using chromeos::PowerSupplyStatus;
...@@ -68,18 +69,19 @@ class TrayPowerTest : public test::AshTestBase { ...@@ -68,18 +69,19 @@ class TrayPowerTest : public test::AshTestBase {
return tray_power_->notification_state_; return tray_power_->notification_state_;
} }
bool MaybeShowUsbChargerNotification(const PowerSupplyStatus& old_status, bool MaybeShowUsbChargerNotification(const PowerSupplyStatus& status) {
const PowerSupplyStatus& new_status) { PowerStatus::Get()->set_status_for_testing(status);
return tray_power_->MaybeShowUsbChargerNotification(old_status, new_status); return tray_power_->MaybeShowUsbChargerNotification();
} }
bool UpdateNotificationState(const PowerSupplyStatus& status) { bool UpdateNotificationState(const PowerSupplyStatus& status) {
return tray_power_->UpdateNotificationState(status); PowerStatus::Get()->set_status_for_testing(status);
return tray_power_->UpdateNotificationState();
} }
void SetLastPowerStatus(const PowerSupplyStatus& status) { void SetUsbChargerConnected(bool connected) {
tray_power_->last_power_supply_status_ = status; tray_power_->usb_charger_was_connected_ = connected;
} }
// Returns a discharging PowerSupplyStatus more appropriate for testing. // Returns a discharging PowerSupplyStatus more appropriate for testing.
static PowerSupplyStatus DefaultPowerSupplyStatus() { static PowerSupplyStatus DefaultPowerSupplyStatus() {
...@@ -103,13 +105,18 @@ class TrayPowerTest : public test::AshTestBase { ...@@ -103,13 +105,18 @@ class TrayPowerTest : public test::AshTestBase {
}; };
TEST_F(TrayPowerTest, MaybeShowUsbChargerNotification) { TEST_F(TrayPowerTest, MaybeShowUsbChargerNotification) {
// Notification shows when connecting a USB charger.
PowerSupplyStatus discharging = DefaultPowerSupplyStatus(); PowerSupplyStatus discharging = DefaultPowerSupplyStatus();
EXPECT_FALSE(MaybeShowUsbChargerNotification(discharging));
EXPECT_EQ(0, message_center()->add_count());
EXPECT_EQ(0, message_center()->remove_count());
// Notification shows when connecting a USB charger.
PowerSupplyStatus usb_connected = DefaultPowerSupplyStatus(); PowerSupplyStatus usb_connected = DefaultPowerSupplyStatus();
usb_connected.line_power_on = true; usb_connected.line_power_on = true;
usb_connected.battery_state = PowerSupplyStatus::CONNECTED_TO_USB; usb_connected.battery_state = PowerSupplyStatus::CONNECTED_TO_USB;
EXPECT_TRUE(MaybeShowUsbChargerNotification(discharging, usb_connected)); EXPECT_TRUE(MaybeShowUsbChargerNotification(usb_connected));
EXPECT_EQ(1, message_center()->add_count()); EXPECT_EQ(1, message_center()->add_count());
EXPECT_EQ(0, message_center()->remove_count());
// Change in charge does not trigger the notification again. // Change in charge does not trigger the notification again.
PowerSupplyStatus more_charge = DefaultPowerSupplyStatus(); PowerSupplyStatus more_charge = DefaultPowerSupplyStatus();
...@@ -117,13 +124,15 @@ TEST_F(TrayPowerTest, MaybeShowUsbChargerNotification) { ...@@ -117,13 +124,15 @@ TEST_F(TrayPowerTest, MaybeShowUsbChargerNotification) {
more_charge.battery_seconds_to_full = 60 * 60; more_charge.battery_seconds_to_full = 60 * 60;
more_charge.battery_percentage = 75.0; more_charge.battery_percentage = 75.0;
more_charge.battery_state = PowerSupplyStatus::CONNECTED_TO_USB; more_charge.battery_state = PowerSupplyStatus::CONNECTED_TO_USB;
EXPECT_FALSE(MaybeShowUsbChargerNotification(usb_connected, more_charge)); SetUsbChargerConnected(true);
EXPECT_FALSE(MaybeShowUsbChargerNotification(more_charge));
EXPECT_EQ(1, message_center()->add_count()); EXPECT_EQ(1, message_center()->add_count());
EXPECT_EQ(0, message_center()->remove_count()); EXPECT_EQ(0, message_center()->remove_count());
// Disconnecting a USB charger with the notification showing should close // Disconnecting a USB charger with the notification showing should close
// the notification. // the notification.
EXPECT_TRUE(MaybeShowUsbChargerNotification(usb_connected, discharging)); EXPECT_TRUE(MaybeShowUsbChargerNotification(discharging));
EXPECT_EQ(1, message_center()->add_count());
EXPECT_EQ(1, message_center()->remove_count()); EXPECT_EQ(1, message_center()->remove_count());
} }
......
...@@ -30,12 +30,14 @@ namespace internal { ...@@ -30,12 +30,14 @@ namespace internal {
namespace tray { namespace tray {
class SettingsDefaultView : public ActionableView { class SettingsDefaultView : public ActionableView,
public PowerStatus::Observer {
public: public:
explicit SettingsDefaultView(user::LoginStatus status) explicit SettingsDefaultView(user::LoginStatus status)
: login_status_(status), : login_status_(status),
label_(NULL), label_(NULL),
power_status_view_(NULL) { power_status_view_(NULL) {
PowerStatus::Get()->AddObserver(this);
SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
ash::kTrayPopupPaddingHorizontal, 0, ash::kTrayPopupPaddingHorizontal, 0,
ash::kTrayPopupPaddingBetweenItems)); ash::kTrayPopupPaddingBetweenItems));
...@@ -58,27 +60,16 @@ class SettingsDefaultView : public ActionableView { ...@@ -58,27 +60,16 @@ class SettingsDefaultView : public ActionableView {
power_view_right_align = true; power_view_right_align = true;
} }
chromeos::PowerSupplyStatus power_status = if (PowerStatus::Get()->IsBatteryPresent()) {
PowerStatus::Get()->GetPowerSupplyStatus();
if (power_status.battery_is_present) {
power_status_view_ = new ash::internal::PowerStatusView( power_status_view_ = new ash::internal::PowerStatusView(
ash::internal::PowerStatusView::VIEW_DEFAULT, power_view_right_align); ash::internal::PowerStatusView::VIEW_DEFAULT, power_view_right_align);
AddChildView(power_status_view_); AddChildView(power_status_view_);
UpdatePowerStatus(power_status); OnPowerStatusChanged();
} }
} }
virtual ~SettingsDefaultView() {} virtual ~SettingsDefaultView() {
PowerStatus::Get()->RemoveObserver(this);
void UpdatePowerStatus(const chromeos::PowerSupplyStatus& status) {
if (!power_status_view_)
return;
power_status_view_->UpdatePowerStatus(status);
base::string16 accessible_name = label_ ?
label_->text() + ASCIIToUTF16(", ") +
power_status_view_->accessible_name() :
power_status_view_->accessible_name();
SetAccessibleName(accessible_name);
} }
// Overridden from ash::internal::ActionableView. // Overridden from ash::internal::ActionableView.
...@@ -112,6 +103,18 @@ class SettingsDefaultView : public ActionableView { ...@@ -112,6 +103,18 @@ class SettingsDefaultView : public ActionableView {
Layout(); Layout();
} }
// Overridden from PowerStatus::Observer.
virtual void OnPowerStatusChanged() OVERRIDE {
if (!PowerStatus::Get()->IsBatteryPresent())
return;
base::string16 accessible_name = label_ ?
label_->text() + ASCIIToUTF16(", ") +
PowerStatus::Get()->GetAccessibleNameString() :
PowerStatus::Get()->GetAccessibleNameString();
SetAccessibleName(accessible_name);
}
private: private:
user::LoginStatus login_status_; user::LoginStatus login_status_;
views::Label* label_; views::Label* label_;
...@@ -125,11 +128,9 @@ class SettingsDefaultView : public ActionableView { ...@@ -125,11 +128,9 @@ class SettingsDefaultView : public ActionableView {
TraySettings::TraySettings(SystemTray* system_tray) TraySettings::TraySettings(SystemTray* system_tray)
: SystemTrayItem(system_tray), : SystemTrayItem(system_tray),
default_view_(NULL) { default_view_(NULL) {
PowerStatus::Get()->AddObserver(this);
} }
TraySettings::~TraySettings() { TraySettings::~TraySettings() {
PowerStatus::Get()->RemoveObserver(this);
} }
views::View* TraySettings::CreateTrayView(user::LoginStatus status) { views::View* TraySettings::CreateTrayView(user::LoginStatus status) {
...@@ -138,7 +139,7 @@ views::View* TraySettings::CreateTrayView(user::LoginStatus status) { ...@@ -138,7 +139,7 @@ views::View* TraySettings::CreateTrayView(user::LoginStatus status) {
views::View* TraySettings::CreateDefaultView(user::LoginStatus status) { views::View* TraySettings::CreateDefaultView(user::LoginStatus status) {
if ((status == user::LOGGED_IN_NONE || status == user::LOGGED_IN_LOCKED) && if ((status == user::LOGGED_IN_NONE || status == user::LOGGED_IN_LOCKED) &&
!PowerStatus::Get()->GetPowerSupplyStatus().battery_is_present) !PowerStatus::Get()->IsBatteryPresent())
return NULL; return NULL;
CHECK(default_view_ == NULL); CHECK(default_view_ == NULL);
...@@ -164,11 +165,5 @@ void TraySettings::DestroyDetailedView() { ...@@ -164,11 +165,5 @@ void TraySettings::DestroyDetailedView() {
void TraySettings::UpdateAfterLoginStatusChange(user::LoginStatus status) { void TraySettings::UpdateAfterLoginStatusChange(user::LoginStatus status) {
} }
void TraySettings::OnPowerStatusChanged(
const chromeos::PowerSupplyStatus& status) {
if (default_view_)
default_view_->UpdatePowerStatus(status);
}
} // namespace internal } // namespace internal
} // namespace ash } // namespace ash
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#ifndef ASH_SYSTEM_CHROMEOS_SETTINGS_TRAY_SETTINGS_H_ #ifndef ASH_SYSTEM_CHROMEOS_SETTINGS_TRAY_SETTINGS_H_
#define ASH_SYSTEM_CHROMEOS_SETTINGS_TRAY_SETTINGS_H_ #define ASH_SYSTEM_CHROMEOS_SETTINGS_TRAY_SETTINGS_H_
#include "ash/system/chromeos/power/power_status.h"
#include "ash/system/tray/system_tray_item.h" #include "ash/system/tray/system_tray_item.h"
namespace ash { namespace ash {
...@@ -15,7 +14,7 @@ namespace tray { ...@@ -15,7 +14,7 @@ namespace tray {
class SettingsDefaultView; class SettingsDefaultView;
} }
class TraySettings : public SystemTrayItem, public PowerStatus::Observer { class TraySettings : public SystemTrayItem {
public: public:
explicit TraySettings(SystemTray* system_tray); explicit TraySettings(SystemTray* system_tray);
virtual ~TraySettings(); virtual ~TraySettings();
...@@ -30,10 +29,6 @@ class TraySettings : public SystemTrayItem, public PowerStatus::Observer { ...@@ -30,10 +29,6 @@ class TraySettings : public SystemTrayItem, public PowerStatus::Observer {
virtual void DestroyDetailedView() OVERRIDE; virtual void DestroyDetailedView() OVERRIDE;
virtual void UpdateAfterLoginStatusChange(user::LoginStatus status) OVERRIDE; virtual void UpdateAfterLoginStatusChange(user::LoginStatus status) OVERRIDE;
// Overridden from PowerStatus::Observer.
virtual void OnPowerStatusChanged(
const chromeos::PowerSupplyStatus& status) OVERRIDE;
tray::SettingsDefaultView* default_view_; tray::SettingsDefaultView* default_view_;
DISALLOW_COPY_AND_ASSIGN(TraySettings); DISALLOW_COPY_AND_ASSIGN(TraySettings);
......
...@@ -804,24 +804,28 @@ class PowerManagerClientStubImpl : public PowerManagerClient { ...@@ -804,24 +804,28 @@ class PowerManagerClientStubImpl : public PowerManagerClient {
void UpdateStatus() { void UpdateStatus() {
if (pause_count_ > 0) { if (pause_count_ > 0) {
pause_count_--; pause_count_--;
if (pause_count_ == 2)
discharging_ = !discharging_;
} else { } else {
int discharge_amt = battery_percentage_ <= 10 ? 1 : 10; if (discharging_)
battery_percentage_ += (discharging_ ? -discharge_amt : discharge_amt); battery_percentage_ -= (battery_percentage_ <= 10 ? 1 : 10);
else
battery_percentage_ += (battery_percentage_ >= 10 ? 10 : 1);
battery_percentage_ = std::min(std::max(battery_percentage_, 0), 100); battery_percentage_ = std::min(std::max(battery_percentage_, 0), 100);
// We pause at 0 and 100% so that it's easier to check those conditions. // We pause at 0 and 100% so that it's easier to check those conditions.
if (battery_percentage_ == 0 || battery_percentage_ == 100) { if (battery_percentage_ == 0 || battery_percentage_ == 100) {
discharging_ = !discharging_;
pause_count_ = 4; pause_count_ = 4;
if (battery_percentage_ == 100) if (battery_percentage_ == 100)
cycle_count_ = (cycle_count_ + 1) % 3; cycle_count_ = (cycle_count_ + 1) % 3;
} }
} }
const int kSecondsToEmptyFullBattery(3 * 60 * 60); // 3 hours. const int kSecondsToEmptyFullBattery = 3 * 60 * 60; // 3 hours.
status_.is_calculating_battery_time = (pause_count_ > 1); status_.is_calculating_battery_time = (pause_count_ > 1);
status_.line_power_on = !discharging_; status_.line_power_on = !discharging_;
status_.battery_is_present = true; status_.battery_is_present = true;
status_.battery_percentage = battery_percentage_; status_.battery_percentage = battery_percentage_;
status_.battery_is_full = battery_percentage_ == 100 && !discharging_;
if (cycle_count_ != 2) { if (cycle_count_ != 2) {
status_.battery_state = discharging_ ? status_.battery_state = discharging_ ?
PowerSupplyStatus::DISCHARGING : PowerSupplyStatus::CHARGING; PowerSupplyStatus::DISCHARGING : PowerSupplyStatus::CHARGING;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment