OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "athena/main/debug/debug_window.h" | 5 #include "athena/system/status_icon_container_view.h" |
6 | 6 |
7 #include "athena/common/container_priorities.h" | |
8 #include "athena/main/debug/network_selector.h" | |
9 #include "athena/resources/athena_resources.h" | 7 #include "athena/resources/athena_resources.h" |
10 #include "athena/screen/public/screen_manager.h" | 8 #include "athena/system/network_selector.h" |
11 #include "base/bind.h" | 9 #include "base/bind.h" |
12 #include "base/macros.h" | 10 #include "base/macros.h" |
13 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
15 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
16 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" | 14 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" |
17 #include "chromeos/dbus/power_manager_client.h" | 15 #include "chromeos/dbus/power_manager_client.h" |
18 #include "chromeos/dbus/update_engine_client.h" | 16 #include "chromeos/dbus/update_engine_client.h" |
19 #include "chromeos/network/network_state.h" | 17 #include "chromeos/network/network_state.h" |
20 #include "chromeos/network/network_state_handler.h" | 18 #include "chromeos/network/network_state_handler.h" |
21 #include "chromeos/network/network_state_handler_observer.h" | 19 #include "chromeos/network/network_state_handler_observer.h" |
22 #include "chromeos/network/network_type_pattern.h" | 20 #include "chromeos/network/network_type_pattern.h" |
21 #include "extensions/shell/common/version.h" | |
23 #include "ui/aura/window.h" | 22 #include "ui/aura/window.h" |
24 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
25 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
26 #include "ui/gfx/image/image_skia.h" | 25 #include "ui/gfx/image/image_skia.h" |
27 #include "ui/gfx/image/image_skia_operations.h" | 26 #include "ui/gfx/image/image_skia_operations.h" |
28 #include "ui/views/background.h" | |
29 #include "ui/views/border.h" | |
30 #include "ui/views/controls/image_view.h" | 27 #include "ui/views/controls/image_view.h" |
31 #include "ui/views/controls/label.h" | 28 #include "ui/views/controls/label.h" |
32 #include "ui/views/layout/box_layout.h" | 29 #include "ui/views/layout/box_layout.h" |
33 #include "ui/views/view.h" | |
34 #include "ui/views/widget/widget.h" | |
35 | 30 |
31 namespace athena { | |
36 namespace { | 32 namespace { |
37 | 33 |
38 views::Label* CreateDebugLabel(const std::string& text) { | 34 views::Label* CreateLabel(const std::string& text) { |
39 views::Label* label = new views::Label(base::UTF8ToUTF16(text)); | 35 views::Label* label = new views::Label(base::UTF8ToUTF16(text)); |
40 label->SetEnabledColor(SK_ColorWHITE); | 36 label->SetEnabledColor(SK_ColorWHITE); |
41 label->SetBackgroundColor(SK_ColorTRANSPARENT); | 37 label->SetAutoColorReadabilityEnabled(false); |
42 label->SetFontList(gfx::FontList().Derive(-2, gfx::Font::BOLD)); | 38 label->SetFontList(gfx::FontList().DeriveWithStyle(gfx::Font::BOLD)); |
43 return label; | 39 return label; |
44 } | 40 } |
45 | 41 |
46 class PowerStatus : public chromeos::PowerManagerClient::Observer { | 42 } // namespace |
43 | |
44 class StatusIconContainerView::PowerStatus | |
45 : public chromeos::PowerManagerClient::Observer { | |
47 public: | 46 public: |
48 PowerStatus(views::ImageView* icon, const base::Closure& closure) | 47 explicit PowerStatus(views::ImageView* icon) : icon_(icon) { |
49 : icon_(icon), closure_(closure) { | |
50 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( | 48 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( |
51 this); | 49 this); |
52 chromeos::DBusThreadManager::Get() | 50 chromeos::DBusThreadManager::Get() |
53 ->GetPowerManagerClient() | 51 ->GetPowerManagerClient() |
54 ->RequestStatusUpdate(); | 52 ->RequestStatusUpdate(); |
55 } | 53 } |
56 | 54 |
57 virtual ~PowerStatus() { | 55 virtual ~PowerStatus() { |
58 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | 56 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
59 this); | 57 this); |
(...skipping 29 matching lines...) Expand all Loading... | |
89 | 87 |
90 bool IsCharging(const power_manager::PowerSupplyProperties& proto) const { | 88 bool IsCharging(const power_manager::PowerSupplyProperties& proto) const { |
91 return proto.external_power() != | 89 return proto.external_power() != |
92 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED; | 90 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED; |
93 } | 91 } |
94 | 92 |
95 // chromeos::PowerManagerClient::Observer: | 93 // chromeos::PowerManagerClient::Observer: |
96 virtual void PowerChanged( | 94 virtual void PowerChanged( |
97 const power_manager::PowerSupplyProperties& proto) OVERRIDE { | 95 const power_manager::PowerSupplyProperties& proto) OVERRIDE { |
98 icon_->SetImage(GetPowerIcon(proto)); | 96 icon_->SetImage(GetPowerIcon(proto)); |
99 if (!closure_.is_null()) | |
100 closure_.Run(); | |
101 } | 97 } |
102 | 98 |
103 views::ImageView* icon_; | 99 views::ImageView* icon_; |
104 base::Closure closure_; | |
105 | 100 |
106 DISALLOW_COPY_AND_ASSIGN(PowerStatus); | 101 DISALLOW_COPY_AND_ASSIGN(PowerStatus); |
107 }; | 102 }; |
108 | 103 |
109 class NetworkStatus : public chromeos::NetworkStateHandlerObserver { | 104 class StatusIconContainerView::NetworkStatus |
105 : public chromeos::NetworkStateHandlerObserver { | |
110 public: | 106 public: |
111 NetworkStatus(views::Label* label, const base::Closure& closure) | 107 explicit NetworkStatus(views::Label* label) : label_(label) { |
112 : label_(label), closure_(closure) { | |
113 chromeos::NetworkStateHandler* handler = | 108 chromeos::NetworkStateHandler* handler = |
114 chromeos::NetworkHandler::Get()->network_state_handler(); | 109 chromeos::NetworkHandler::Get()->network_state_handler(); |
115 handler->AddObserver(this, FROM_HERE); | 110 handler->AddObserver(this, FROM_HERE); |
116 } | 111 } |
117 | 112 |
118 virtual ~NetworkStatus() { | 113 virtual ~NetworkStatus() { |
119 chromeos::NetworkStateHandler* handler = | 114 chromeos::NetworkStateHandler* handler = |
120 chromeos::NetworkHandler::Get()->network_state_handler(); | 115 chromeos::NetworkHandler::Get()->network_state_handler(); |
121 handler->RemoveObserver(this, FROM_HERE); | 116 handler->RemoveObserver(this, FROM_HERE); |
122 } | 117 } |
123 | 118 |
124 private: | 119 private: |
125 void Update() { | 120 void Update() { |
126 std::string status = "<unknown>"; | 121 std::string status = "<unknown>"; |
127 chromeos::NetworkStateHandler* handler = | 122 chromeos::NetworkStateHandler* handler = |
128 chromeos::NetworkHandler::Get()->network_state_handler(); | 123 chromeos::NetworkHandler::Get()->network_state_handler(); |
129 const chromeos::NetworkState* network = handler->DefaultNetwork(); | 124 const chromeos::NetworkState* network = handler->DefaultNetwork(); |
130 if (!network) { | 125 if (!network) { |
131 network = handler->ConnectedNetworkByType( | 126 network = handler->ConnectedNetworkByType( |
132 chromeos::NetworkTypePattern::NonVirtual()); | 127 chromeos::NetworkTypePattern::NonVirtual()); |
133 } | 128 } |
134 if (network) { | 129 if (network) { |
135 status = base::StringPrintf( | 130 status = base::StringPrintf( |
136 "%s (%s)", network->ip_address().c_str(), network->name().c_str()); | 131 "%s (%s)", network->ip_address().c_str(), network->name().c_str()); |
137 } | 132 } |
138 label_->SetText(base::UTF8ToUTF16(status)); | 133 label_->SetText(base::UTF8ToUTF16(status)); |
139 if (!closure_.is_null()) | |
140 closure_.Run(); | |
sadrul
2014/08/21 18:44:30
This was necessary to make sure the widget and the
pkotwicz
2014/08/21 21:57:16
Yes I checked
| |
141 } | 134 } |
142 | 135 |
143 // chromeos::NetworkStateHandlerObserver: | 136 // chromeos::NetworkStateHandlerObserver: |
144 virtual void DefaultNetworkChanged( | 137 virtual void DefaultNetworkChanged( |
145 const chromeos::NetworkState* network) OVERRIDE { | 138 const chromeos::NetworkState* network) OVERRIDE { |
146 Update(); | 139 Update(); |
147 } | 140 } |
148 | 141 |
149 virtual void NetworkConnectionStateChanged( | 142 virtual void NetworkConnectionStateChanged( |
150 const chromeos::NetworkState* network) OVERRIDE { | 143 const chromeos::NetworkState* network) OVERRIDE { |
151 Update(); | 144 Update(); |
152 } | 145 } |
153 | 146 |
154 virtual void NetworkPropertiesUpdated( | 147 virtual void NetworkPropertiesUpdated( |
155 const chromeos::NetworkState* network) OVERRIDE { | 148 const chromeos::NetworkState* network) OVERRIDE { |
156 Update(); | 149 Update(); |
157 } | 150 } |
158 | 151 |
159 views::Label* label_; | 152 views::Label* label_; |
160 base::Closure closure_; | 153 |
154 DISALLOW_COPY_AND_ASSIGN(NetworkStatus); | |
161 }; | 155 }; |
162 | 156 |
163 void StartUpdateCallback( | 157 void StartUpdateCallback( |
164 chromeos::UpdateEngineClient::UpdateCheckResult result) { | 158 chromeos::UpdateEngineClient::UpdateCheckResult result) { |
165 VLOG(1) << "Callback from RequestUpdateCheck, result " << result; | 159 VLOG(1) << "Callback from RequestUpdateCheck, result " << result; |
166 } | 160 } |
167 | 161 |
168 class UpdateStatus : public chromeos::UpdateEngineClient::Observer { | 162 class StatusIconContainerView::UpdateStatus |
163 : public chromeos::UpdateEngineClient::Observer { | |
169 public: | 164 public: |
170 UpdateStatus(views::ImageView* icon, const base::Closure& closure) | 165 explicit UpdateStatus(views::ImageView* icon) : icon_(icon) { |
171 : icon_(icon), closure_(closure) { | |
172 chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()->AddObserver( | 166 chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()->AddObserver( |
173 this); | 167 this); |
174 chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()-> | 168 chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()-> |
175 RequestUpdateCheck(base::Bind(StartUpdateCallback)); | 169 RequestUpdateCheck(base::Bind(StartUpdateCallback)); |
176 } | 170 } |
177 | 171 |
178 virtual ~UpdateStatus() { | 172 virtual ~UpdateStatus() { |
179 chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()->RemoveObserver( | 173 chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()->RemoveObserver( |
180 this); | 174 this); |
181 } | 175 } |
182 | 176 |
183 // chromeos::UpdateEngineClient::Observer: | 177 // chromeos::UpdateEngineClient::Observer: |
184 virtual void UpdateStatusChanged( | 178 virtual void UpdateStatusChanged( |
185 const chromeos::UpdateEngineClient::Status& status) OVERRIDE { | 179 const chromeos::UpdateEngineClient::Status& status) OVERRIDE { |
186 if (status.status != | 180 if (status.status != |
187 chromeos::UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT) { | 181 chromeos::UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
188 return; | 182 return; |
189 } | 183 } |
190 icon_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 184 icon_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
191 IDR_AURA_UBER_TRAY_UPDATE)); | 185 IDR_AURA_UBER_TRAY_UPDATE)); |
192 if (!closure_.is_null()) | |
193 closure_.Run(); | |
194 } | 186 } |
195 | 187 |
196 private: | 188 private: |
197 views::ImageView* icon_; | 189 views::ImageView* icon_; |
198 base::Closure closure_; | |
199 | 190 |
200 DISALLOW_COPY_AND_ASSIGN(UpdateStatus); | 191 DISALLOW_COPY_AND_ASSIGN(UpdateStatus); |
201 }; | 192 }; |
202 | 193 |
203 // Processes user input to show the detailed network-list. | 194 StatusIconContainerView::StatusIconContainerView(aura::Window* popup_container) |
204 class DetailViewHandler : public ui::EventHandler { | 195 : popup_container_(popup_container) { |
205 public: | 196 const int kHorizontalSpacing = 10; |
206 explicit DetailViewHandler(aura::Window* container) : container_(container) {} | 197 const int kVerticalSpacing = 3; |
207 virtual ~DetailViewHandler() {} | 198 const int kBetweenChildSpacing = 10; |
199 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, | |
200 kHorizontalSpacing, | |
201 kVerticalSpacing, | |
202 kBetweenChildSpacing)); | |
208 | 203 |
209 private: | 204 std::string version_text = |
210 // ui::EventHandler: | 205 base::StringPrintf("%s (Build %s)", PRODUCT_VERSION, LAST_CHANGE); |
211 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE { | 206 AddChildView(CreateLabel(version_text)); |
212 if (event->type() == ui::ET_MOUSE_PRESSED) { | 207 |
213 debug::CreateNetworkSelector(container_); | 208 AddChildView(CreateLabel("Network:")); |
214 event->SetHandled(); | 209 views::Label* network_label = CreateLabel(std::string()); |
215 } | 210 AddChildView(network_label); |
211 network_status_.reset(new NetworkStatus(network_label)); | |
212 | |
213 views::ImageView* battery_view = new views::ImageView(); | |
214 AddChildView(battery_view); | |
215 power_status_.reset(new PowerStatus(battery_view)); | |
216 | |
217 views::ImageView* update_view = new views::ImageView(); | |
218 AddChildView(update_view); | |
219 update_status_.reset(new UpdateStatus(update_view)); | |
220 } | |
221 | |
222 StatusIconContainerView::~StatusIconContainerView() { | |
223 } | |
224 | |
225 bool StatusIconContainerView::OnMousePressed(const ui::MouseEvent& event) { | |
226 CreateNetworkSelector(popup_container_); | |
227 return true; | |
228 } | |
229 | |
230 void StatusIconContainerView::OnGestureEvent(ui::GestureEvent* event) { | |
231 if (event->type() == ui::ET_GESTURE_TAP) { | |
232 CreateNetworkSelector(popup_container_); | |
233 event->SetHandled(); | |
216 } | 234 } |
235 } | |
217 | 236 |
218 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE { | 237 void StatusIconContainerView::ChildPreferredSizeChanged(views::View* child) { |
219 if (event->type() == ui::ET_GESTURE_TAP) { | 238 PreferredSizeChanged(); |
220 debug::CreateNetworkSelector(container_); | 239 } |
221 event->SetHandled(); | |
222 } | |
223 } | |
224 | 240 |
225 aura::Window* container_; | 241 } // namespace athena |
226 | |
227 DISALLOW_COPY_AND_ASSIGN(DetailViewHandler); | |
228 }; | |
229 | |
230 class DebugWidget { | |
231 public: | |
232 DebugWidget() : container_(NULL), widget_(NULL) { | |
233 CreateContainer(); | |
234 CreateWidget(); | |
235 | |
236 CreateBatteryView(); | |
237 CreateUpdateView(); | |
238 CreateNetworkView(); | |
239 | |
240 UpdateSize(); | |
241 } | |
242 | |
243 virtual ~DebugWidget() {} | |
244 | |
245 private: | |
246 void CreateContainer() { | |
247 athena::ScreenManager::ContainerParams params("DebugContainer", | |
248 athena::CP_DEBUG); | |
249 params.can_activate_children = true; | |
250 container_ = athena::ScreenManager::Get()->CreateContainer(params); | |
251 } | |
252 | |
253 void CreateWidget() { | |
254 views::Widget::InitParams params; | |
255 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; | |
256 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
257 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; | |
258 params.accept_events = true; | |
259 params.bounds = gfx::Rect(200, 0, 100, 105); | |
260 params.parent = container_; | |
261 widget_ = new views::Widget(); | |
262 widget_->Init(params); | |
263 | |
264 event_handler_.reset(new DetailViewHandler(container_)); | |
265 | |
266 const int kHorizontalSpacing = 10; | |
267 const int kBorderVerticalSpacing = 3; | |
268 const int kBetweenChildSpacing = 10; | |
269 const int kBackgroundColor = SkColorSetARGB(0x7f, 0, 0, 0); | |
270 views::View* container = new views::View; | |
271 container->SetLayoutManager( | |
272 new views::BoxLayout(views::BoxLayout::kHorizontal, | |
273 kHorizontalSpacing, | |
274 kBorderVerticalSpacing, | |
275 kBetweenChildSpacing)); | |
276 container->set_background( | |
277 views::Background::CreateSolidBackground(kBackgroundColor)); | |
278 container->SetBorder(views::Border::CreateSolidBorder(1, kBackgroundColor)); | |
279 container->set_target_handler(event_handler_.get()); | |
280 widget_->SetContentsView(container); | |
281 widget_->StackAtTop(); | |
282 widget_->Show(); | |
283 | |
284 widget_->SetBounds(gfx::Rect(600, 0, 300, 25)); | |
285 } | |
286 | |
287 void CreateBatteryView() { | |
288 views::View* container = widget_->GetContentsView(); | |
289 views::ImageView* icon = new views::ImageView(); | |
290 container->AddChildView(icon); | |
291 container->Layout(); | |
292 | |
293 power_status_.reset(new PowerStatus( | |
294 icon, base::Bind(&DebugWidget::UpdateSize, base::Unretained(this)))); | |
295 } | |
296 | |
297 void CreateNetworkView() { | |
298 views::View* container = widget_->GetContentsView(); | |
299 container->AddChildView(CreateDebugLabel("Network:")); | |
300 | |
301 views::Label* label = CreateDebugLabel(std::string()); | |
302 container->AddChildView(label); | |
303 container->Layout(); | |
304 | |
305 network_status_.reset(new NetworkStatus( | |
306 label, base::Bind(&DebugWidget::UpdateSize, base::Unretained(this)))); | |
307 } | |
308 | |
309 void CreateUpdateView() { | |
310 views::View* container = widget_->GetContentsView(); | |
311 views::ImageView* icon = new views::ImageView(); | |
312 container->AddChildView(icon); | |
313 container->Layout(); | |
314 | |
315 update_status_.reset(new UpdateStatus( | |
316 icon, base::Bind(&DebugWidget::UpdateSize, base::Unretained(this)))); | |
317 } | |
318 | |
319 const gfx::Rect GetPositionForSize(const gfx::Size& size) { | |
320 int right = container_->bounds().right(); | |
321 int x = right - size.width(); | |
322 return gfx::Rect(x, 0, size.width(), size.height()); | |
323 } | |
324 | |
325 void UpdateSize() { | |
326 views::View* container = widget_->GetContentsView(); | |
327 container->Layout(); | |
328 gfx::Size size = container->GetPreferredSize(); | |
329 widget_->SetBounds(GetPositionForSize(size)); | |
330 } | |
331 | |
332 aura::Window* container_; | |
333 views::Widget* widget_; | |
334 scoped_ptr<PowerStatus> power_status_; | |
335 scoped_ptr<NetworkStatus> network_status_; | |
336 scoped_ptr<UpdateStatus> update_status_; | |
337 scoped_ptr<ui::EventHandler> event_handler_; | |
338 | |
339 DISALLOW_COPY_AND_ASSIGN(DebugWidget); | |
340 }; | |
341 | |
342 } // namespace | |
343 | |
344 void CreateDebugWindow() { | |
345 new DebugWidget(); | |
346 } | |
OLD | NEW |