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

Side by Side Diff: ash/system/chromeos/power/power_status.cc

Issue 18176013: chromeos: Refactor system tray code into PowerStatus. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove virtual Created 7 years, 5 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "ash/system/chromeos/power/power_status.h" 5 #include "ash/system/chromeos/power/power_status.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 9
10 #include "ash/shell.h"
11 #include "ash/shell_delegate.h"
10 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/utf_string_conversions.h"
11 #include "chromeos/dbus/dbus_thread_manager.h" 15 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/dbus/power_manager_client.h" 16 #include "chromeos/dbus/power_manager_client.h"
17 #include "grit/ash_resources.h"
18 #include "grit/ash_strings.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/gfx/image/image.h"
22 #include "ui/gfx/image/image_skia_operations.h"
23 #include "ui/gfx/rect.h"
13 24
14 namespace ash { 25 namespace ash {
15 namespace internal { 26 namespace internal {
16 27
17 namespace { 28 namespace {
18 29
30 base::string16 GetBatteryTimeAccessibilityString(int hour, int min) {
31 DCHECK(hour || min);
32 if (hour && !min) {
33 return Shell::GetInstance()->delegate()->GetTimeDurationLongString(
34 base::TimeDelta::FromHours(hour));
35 }
36 if (min && !hour) {
37 return Shell::GetInstance()->delegate()->GetTimeDurationLongString(
38 base::TimeDelta::FromMinutes(min));
39 }
40 return l10n_util::GetStringFUTF16(
41 IDS_ASH_STATUS_TRAY_BATTERY_TIME_ACCESSIBLE,
42 Shell::GetInstance()->delegate()->GetTimeDurationLongString(
43 base::TimeDelta::FromHours(hour)),
44 Shell::GetInstance()->delegate()->GetTimeDurationLongString(
45 base::TimeDelta::FromMinutes(min)));
46 }
47
19 static PowerStatus* g_power_status = NULL; 48 static PowerStatus* g_power_status = NULL;
20 49
50 // Minimum battery percentage rendered in UI.
51 const int kMinBatteryPercent = 1;
52
53 // Width and height of battery images.
54 const int kBatteryImageHeight = 25;
55 const int kBatteryImageWidth = 25;
56
57 // Number of different power states.
58 const int kNumPowerImages = 15;
59
21 } // namespace 60 } // namespace
22 61
23 // static 62 // static
24 void PowerStatus::Initialize() { 63 void PowerStatus::Initialize() {
25 CHECK(!g_power_status); 64 CHECK(!g_power_status);
26 g_power_status = new PowerStatus(); 65 g_power_status = new PowerStatus();
27 } 66 }
28 67
29 // static 68 // static
30 void PowerStatus::Shutdown() { 69 void PowerStatus::Shutdown() {
(...skipping 16 matching lines...) Expand all
47 void PowerStatus::AddObserver(Observer* observer) { 86 void PowerStatus::AddObserver(Observer* observer) {
48 DCHECK(observer); 87 DCHECK(observer);
49 observers_.AddObserver(observer); 88 observers_.AddObserver(observer);
50 } 89 }
51 90
52 void PowerStatus::RemoveObserver(Observer* observer) { 91 void PowerStatus::RemoveObserver(Observer* observer) {
53 DCHECK(observer); 92 DCHECK(observer);
54 observers_.RemoveObserver(observer); 93 observers_.RemoveObserver(observer);
55 } 94 }
56 95
96 void PowerStatus::RequestStatusUpdate() {
97 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
98 RequestStatusUpdate();
99 }
100
101 bool PowerStatus::IsBatteryPresent() const {
102 return status_.battery_is_present;
103 }
104
105 bool PowerStatus::IsBatteryFull() const {
106 return status_.battery_is_full;
107 }
108
109 double PowerStatus::GetBatteryPercent() const {
110 return status_.battery_percentage;
111 }
112
113 int PowerStatus::GetRoundedBatteryPercent() const {
114 return std::max(kMinBatteryPercent,
115 static_cast<int>(status_.battery_percentage + 0.5));
116 }
117
118 bool PowerStatus::IsBatteryTimeBeingCalculated() const {
119 return status_.is_calculating_battery_time;
120 }
121
122 base::TimeDelta PowerStatus::GetBatteryTimeToEmpty() const {
123 return base::TimeDelta::FromSeconds(status_.battery_seconds_to_empty);
124 }
125
126 base::TimeDelta PowerStatus::GetBatteryTimeToFull() const {
127 return base::TimeDelta::FromSeconds(status_.battery_seconds_to_full);
128 }
129
130 bool PowerStatus::IsLinePowerConnected() const {
131 return status_.line_power_on;
132 }
133
134 bool PowerStatus::IsMainsChargerConnected() const {
135 return status_.battery_state == chromeos::PowerSupplyStatus::CHARGING;
136 }
137
138 bool PowerStatus::IsUsbChargerConnected() const {
139 return status_.battery_state == chromeos::PowerSupplyStatus::CONNECTED_TO_USB;
140 }
141
142 gfx::ImageSkia PowerStatus::GetBatteryImage(IconSet icon_set) const {
143 gfx::Image all;
144 if (IsUsbChargerConnected()) {
145 all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
146 icon_set == ICON_DARK ?
147 IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE_DARK :
148 IDR_AURA_UBER_TRAY_POWER_SMALL_CHARGING_UNRELIABLE);
149 } else {
150 all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
151 icon_set == ICON_DARK ?
152 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL);
153 }
154
155 // Get the horizontal offset in the battery icon array image.
156 int offset = (IsUsbChargerConnected() || !status_.line_power_on) ? 0 : 1;
157
158 // Get the icon index in the battery icon array image.
159 int index = -1;
160 if (status_.battery_percentage >= 100) {
161 index = kNumPowerImages - 1;
162 } else if (!status_.battery_is_present) {
163 index = kNumPowerImages;
164 } else {
165 index = static_cast<int>(
166 status_.battery_percentage / 100.0 * (kNumPowerImages - 1));
167 index = std::max(std::min(index, kNumPowerImages - 2), 0);
168 }
169
170 gfx::Rect region(
171 offset * kBatteryImageWidth, index * kBatteryImageHeight,
172 kBatteryImageWidth, kBatteryImageHeight);
173 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region);
174 }
175
176 base::string16 PowerStatus::GetAccessibleNameString() const {
177 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
178 if (status_.line_power_on && status_.battery_is_full) {
179 return rb.GetLocalizedString(
180 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE);
181 }
182 base::string16 battery_percentage_accessible = l10n_util::GetStringFUTF16(
183 IsLinePowerConnected() ?
184 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_CHARGING_ACCESSIBLE :
185 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ACCESSIBLE,
186 base::IntToString16(GetRoundedBatteryPercent()));
187 base::string16 battery_time_accessible = base::string16();
188 if (IsUsbChargerConnected()) {
189 battery_time_accessible = rb.GetLocalizedString(
190 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE_ACCESSIBLE);
191 } else {
192 if (IsBatteryTimeBeingCalculated()) {
193 battery_time_accessible = rb.GetLocalizedString(
194 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING_ACCESSIBLE);
195 } else {
196 base::TimeDelta time = IsLinePowerConnected() ? GetBatteryTimeToFull() :
197 GetBatteryTimeToEmpty();
198 int hour = time.InHours();
199 int min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
200 if (hour || min) {
201 base::string16 minute = min < 10 ?
202 ASCIIToUTF16("0") + base::IntToString16(min) :
203 base::IntToString16(min);
204 battery_time_accessible =
205 l10n_util::GetStringFUTF16(
206 IsLinePowerConnected() ?
207 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_ACCESSIBLE :
208 IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_ACCESSIBLE,
209 GetBatteryTimeAccessibilityString(hour, min));
210 }
211 }
212 }
213 return battery_time_accessible.empty() ?
214 battery_percentage_accessible :
215 battery_percentage_accessible + ASCIIToUTF16(". ") +
216 battery_time_accessible;
217 }
218
57 PowerStatus::PowerStatus() { 219 PowerStatus::PowerStatus() {
58 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> 220 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
59 AddObserver(this); 221 AddObserver(this);
60 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> 222 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
61 RequestStatusUpdate(); 223 RequestStatusUpdate();
62 } 224 }
63 225
64 PowerStatus::~PowerStatus() { 226 PowerStatus::~PowerStatus() {
65 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> 227 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
66 RemoveObserver(this); 228 RemoveObserver(this);
67 } 229 }
68 230
69 void PowerStatus::RequestStatusUpdate() { 231 void PowerStatus::PowerChanged(const chromeos::PowerSupplyStatus& status) {
70 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> 232 status_ = status;
71 RequestStatusUpdate(); 233 if (status_.battery_is_full)
72 } 234 status_.battery_percentage = 100.0;
73 235
74 chromeos::PowerSupplyStatus PowerStatus::GetPowerSupplyStatus() const { 236 FOR_EACH_OBSERVER(Observer, observers_, OnPowerStatusChanged());
75 return power_supply_status_;
76 }
77
78 void PowerStatus::PowerChanged(
79 const chromeos::PowerSupplyStatus& power_status) {
80 power_supply_status_ = power_status;
81 FOR_EACH_OBSERVER(Observer, observers_, OnPowerStatusChanged(power_status));
82 } 237 }
83 238
84 } // namespace internal 239 } // namespace internal
85 } // namespace ash 240 } // namespace ash
OLDNEW
« no previous file with comments | « ash/system/chromeos/power/power_status.h ('k') | ash/system/chromeos/power/power_status_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698