OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/network/tray_network.h" | 5 #include "ash/system/chromeos/network/tray_network.h" |
6 | 6 |
7 #include "ash/ash_switches.h" | 7 #include "ash/ash_switches.h" |
8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "ash/system/chromeos/network/network_icon_animation.h" | 9 #include "ash/system/chromeos/network/network_icon_animation.h" |
10 #include "ash/system/chromeos/network/network_state_list_detailed_view.h" | 10 #include "ash/system/chromeos/network/network_state_list_detailed_view.h" |
11 #include "ash/system/chromeos/network/network_tray_delegate.h" | |
12 #include "ash/system/chromeos/network/tray_network_state_observer.h" | 11 #include "ash/system/chromeos/network/tray_network_state_observer.h" |
13 #include "ash/system/tray/system_tray.h" | 12 #include "ash/system/tray/system_tray.h" |
14 #include "ash/system/tray/system_tray_delegate.h" | 13 #include "ash/system/tray/system_tray_delegate.h" |
15 #include "ash/system/tray/system_tray_notifier.h" | 14 #include "ash/system/tray/system_tray_notifier.h" |
16 #include "ash/system/tray/tray_constants.h" | 15 #include "ash/system/tray/tray_constants.h" |
17 #include "ash/system/tray/tray_item_more.h" | 16 #include "ash/system/tray/tray_item_more.h" |
18 #include "ash/system/tray/tray_item_view.h" | 17 #include "ash/system/tray/tray_item_view.h" |
19 #include "ash/system/tray/tray_notification_view.h" | |
20 #include "ash/system/tray/tray_utils.h" | 18 #include "ash/system/tray/tray_utils.h" |
21 #include "base/command_line.h" | 19 #include "base/command_line.h" |
22 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
23 #include "chromeos/network/network_state.h" | 21 #include "chromeos/network/network_state.h" |
24 #include "chromeos/network/network_state_handler.h" | 22 #include "chromeos/network/network_state_handler.h" |
25 #include "grit/ash_resources.h" | 23 #include "grit/ash_resources.h" |
26 #include "grit/ash_strings.h" | 24 #include "grit/ash_strings.h" |
27 #include "third_party/cros_system_api/dbus/service_constants.h" | 25 #include "third_party/cros_system_api/dbus/service_constants.h" |
28 #include "ui/base/accessibility/accessible_view_state.h" | 26 #include "ui/base/accessibility/accessible_view_state.h" |
29 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
30 #include "ui/base/resource/resource_bundle.h" | 28 #include "ui/base/resource/resource_bundle.h" |
31 #include "ui/views/controls/image_view.h" | 29 #include "ui/views/controls/image_view.h" |
32 #include "ui/views/controls/link.h" | 30 #include "ui/views/controls/link.h" |
33 #include "ui/views/controls/link_listener.h" | 31 #include "ui/views/controls/link_listener.h" |
34 #include "ui/views/layout/box_layout.h" | 32 #include "ui/views/layout/box_layout.h" |
35 #include "ui/views/widget/widget.h" | 33 #include "ui/views/widget/widget.h" |
36 | 34 |
37 using chromeos::NetworkHandler; | 35 using chromeos::NetworkHandler; |
38 using chromeos::NetworkState; | 36 using chromeos::NetworkState; |
39 using chromeos::NetworkStateHandler; | 37 using chromeos::NetworkStateHandler; |
40 | 38 |
41 namespace ash { | 39 namespace ash { |
42 namespace internal { | 40 namespace internal { |
43 | 41 |
44 namespace { | |
45 | |
46 int GetMessageIcon(NetworkObserver::MessageType message_type, | |
47 NetworkObserver::NetworkType network_type) { | |
48 switch(message_type) { | |
49 case NetworkObserver::ERROR_CONNECT_FAILED: | |
50 if (NetworkObserver::NETWORK_CELLULAR == network_type) | |
51 return IDR_AURA_UBER_TRAY_CELLULAR_NETWORK_FAILED; | |
52 else | |
53 return IDR_AURA_UBER_TRAY_NETWORK_FAILED; | |
54 case NetworkObserver::ERROR_OUT_OF_CREDITS: | |
55 case NetworkObserver::MESSAGE_DATA_PROMO: | |
56 if (network_type == TrayNetwork::NETWORK_CELLULAR_LTE) | |
57 return IDR_AURA_UBER_TRAY_NOTIFICATION_LTE; | |
58 else | |
59 return IDR_AURA_UBER_TRAY_NOTIFICATION_3G; | |
60 } | |
61 NOTREACHED(); | |
62 return 0; | |
63 } | |
64 | |
65 } // namespace | |
66 | |
67 namespace tray { | 42 namespace tray { |
68 | 43 |
69 class NetworkMessages { | |
70 public: | |
71 struct Message { | |
72 Message() : delegate(NULL) {} | |
73 Message(NetworkTrayDelegate* in_delegate, | |
74 NetworkObserver::NetworkType network_type, | |
75 const base::string16& in_title, | |
76 const base::string16& in_message, | |
77 const std::vector<base::string16>& in_links) : | |
78 delegate(in_delegate), | |
79 network_type_(network_type), | |
80 title(in_title), | |
81 message(in_message), | |
82 links(in_links) {} | |
83 NetworkTrayDelegate* delegate; | |
84 NetworkObserver::NetworkType network_type_; | |
85 base::string16 title; | |
86 base::string16 message; | |
87 std::vector<base::string16> links; | |
88 }; | |
89 typedef std::map<NetworkObserver::MessageType, Message> MessageMap; | |
90 | |
91 MessageMap& messages() { return messages_; } | |
92 const MessageMap& messages() const { return messages_; } | |
93 | |
94 private: | |
95 MessageMap messages_; | |
96 }; | |
97 | |
98 class NetworkTrayView : public TrayItemView, | 44 class NetworkTrayView : public TrayItemView, |
99 public network_icon::AnimationObserver { | 45 public network_icon::AnimationObserver { |
100 public: | 46 public: |
101 explicit NetworkTrayView(TrayNetwork* network_tray) | 47 explicit NetworkTrayView(TrayNetwork* network_tray) |
102 : TrayItemView(network_tray), | 48 : TrayItemView(network_tray), |
103 network_tray_(network_tray) { | 49 network_tray_(network_tray) { |
104 SetLayoutManager( | 50 SetLayoutManager( |
105 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); | 51 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); |
106 | 52 |
107 image_view_ = new views::ImageView; | 53 image_view_ = new views::ImageView; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 kTrayPopupPaddingHorizontal * 2 - kTrayPopupPaddingBetweenItems - | 235 kTrayPopupPaddingHorizontal * 2 - kTrayPopupPaddingBetweenItems - |
290 kTrayPopupDetailsIconWidth); | 236 kTrayPopupDetailsIconWidth); |
291 } | 237 } |
292 | 238 |
293 views::ImageView* image_view_; | 239 views::ImageView* image_view_; |
294 views::Label* label_view_; | 240 views::Label* label_view_; |
295 | 241 |
296 DISALLOW_COPY_AND_ASSIGN(NetworkWifiDetailedView); | 242 DISALLOW_COPY_AND_ASSIGN(NetworkWifiDetailedView); |
297 }; | 243 }; |
298 | 244 |
299 class NetworkMessageView : public views::View, | |
300 public views::LinkListener { | |
301 public: | |
302 NetworkMessageView(TrayNetwork* tray_network, | |
303 NetworkObserver::MessageType message_type, | |
304 const NetworkMessages::Message& network_msg) | |
305 : tray_network_(tray_network), | |
306 message_type_(message_type), | |
307 network_type_(network_msg.network_type_) { | |
308 SetLayoutManager( | |
309 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1)); | |
310 | |
311 if (!network_msg.title.empty()) { | |
312 views::Label* title = new views::Label(network_msg.title); | |
313 title->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
314 title->SetFont(title->font().DeriveFont(0, gfx::Font::BOLD)); | |
315 AddChildView(title); | |
316 } | |
317 | |
318 if (!network_msg.message.empty()) { | |
319 views::Label* message = new views::Label(network_msg.message); | |
320 message->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
321 message->SetMultiLine(true); | |
322 message->SizeToFit(kTrayNotificationContentsWidth); | |
323 AddChildView(message); | |
324 } | |
325 | |
326 if (!network_msg.links.empty()) { | |
327 for (size_t i = 0; i < network_msg.links.size(); ++i) { | |
328 views::Link* link = new views::Link(network_msg.links[i]); | |
329 link->set_id(i); | |
330 link->set_listener(this); | |
331 link->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
332 link->SetMultiLine(true); | |
333 link->SizeToFit(kTrayNotificationContentsWidth); | |
334 AddChildView(link); | |
335 } | |
336 } | |
337 } | |
338 | |
339 virtual ~NetworkMessageView() { | |
340 } | |
341 | |
342 // Overridden from views::LinkListener. | |
343 virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE { | |
344 tray_network_->LinkClicked(message_type_, source->id()); | |
345 } | |
346 | |
347 NetworkObserver::MessageType message_type() const { return message_type_; } | |
348 NetworkObserver::NetworkType network_type() const { return network_type_; } | |
349 | |
350 private: | |
351 TrayNetwork* tray_network_; | |
352 NetworkObserver::MessageType message_type_; | |
353 NetworkObserver::NetworkType network_type_; | |
354 | |
355 DISALLOW_COPY_AND_ASSIGN(NetworkMessageView); | |
356 }; | |
357 | |
358 class NetworkNotificationView : public TrayNotificationView { | |
359 public: | |
360 explicit NetworkNotificationView(TrayNetwork* tray_network) | |
361 : TrayNotificationView(tray_network, 0), | |
362 tray_network_(tray_network) { | |
363 CreateMessageView(); | |
364 InitView(network_message_view_); | |
365 SetIconImage(*ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
366 GetMessageIcon(network_message_view_->message_type(), | |
367 network_message_view_->network_type()))); | |
368 } | |
369 | |
370 // Overridden from TrayNotificationView. | |
371 virtual void OnClose() OVERRIDE { | |
372 tray_network_->ClearNetworkMessage(network_message_view_->message_type()); | |
373 } | |
374 | |
375 virtual void OnClickAction() OVERRIDE { | |
376 if (network_message_view_->message_type() != | |
377 TrayNetwork::MESSAGE_DATA_PROMO) | |
378 tray_network_->PopupDetailedView(0, true); | |
379 } | |
380 | |
381 void Update() { | |
382 CreateMessageView(); | |
383 UpdateViewAndImage(network_message_view_, | |
384 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
385 GetMessageIcon(network_message_view_->message_type(), | |
386 network_message_view_->network_type()))); | |
387 } | |
388 | |
389 private: | |
390 void CreateMessageView() { | |
391 // Display the first (highest priority) message. | |
392 CHECK(!tray_network_->messages()->messages().empty()); | |
393 NetworkMessages::MessageMap::const_iterator iter = | |
394 tray_network_->messages()->messages().begin(); | |
395 network_message_view_ = | |
396 new NetworkMessageView(tray_network_, iter->first, iter->second); | |
397 } | |
398 | |
399 TrayNetwork* tray_network_; | |
400 tray::NetworkMessageView* network_message_view_; | |
401 | |
402 DISALLOW_COPY_AND_ASSIGN(NetworkNotificationView); | |
403 }; | |
404 | |
405 } // namespace tray | 245 } // namespace tray |
406 | 246 |
407 TrayNetwork::TrayNetwork(SystemTray* system_tray) | 247 TrayNetwork::TrayNetwork(SystemTray* system_tray) |
408 : SystemTrayItem(system_tray), | 248 : SystemTrayItem(system_tray), |
409 tray_(NULL), | 249 tray_(NULL), |
410 default_(NULL), | 250 default_(NULL), |
411 detailed_(NULL), | 251 detailed_(NULL), |
412 notification_(NULL), | |
413 messages_(new tray::NetworkMessages()), | |
414 request_wifi_view_(false) { | 252 request_wifi_view_(false) { |
415 network_state_observer_.reset(new TrayNetworkStateObserver(this)); | 253 network_state_observer_.reset(new TrayNetworkStateObserver(this)); |
416 Shell::GetInstance()->system_tray_notifier()->AddNetworkObserver(this); | 254 Shell::GetInstance()->system_tray_notifier()->AddNetworkObserver(this); |
417 } | 255 } |
418 | 256 |
419 TrayNetwork::~TrayNetwork() { | 257 TrayNetwork::~TrayNetwork() { |
420 Shell::GetInstance()->system_tray_notifier()->RemoveNetworkObserver(this); | 258 Shell::GetInstance()->system_tray_notifier()->RemoveNetworkObserver(this); |
421 } | 259 } |
422 | 260 |
423 views::View* TrayNetwork::CreateTrayView(user::LoginStatus status) { | 261 views::View* TrayNetwork::CreateTrayView(user::LoginStatus status) { |
(...skipping 11 matching lines...) Expand all Loading... |
435 CHECK(tray_ != NULL); | 273 CHECK(tray_ != NULL); |
436 default_ = new tray::NetworkDefaultView( | 274 default_ = new tray::NetworkDefaultView( |
437 this, status != user::LOGGED_IN_LOCKED); | 275 this, status != user::LOGGED_IN_LOCKED); |
438 return default_; | 276 return default_; |
439 } | 277 } |
440 | 278 |
441 views::View* TrayNetwork::CreateDetailedView(user::LoginStatus status) { | 279 views::View* TrayNetwork::CreateDetailedView(user::LoginStatus status) { |
442 CHECK(detailed_ == NULL); | 280 CHECK(detailed_ == NULL); |
443 if (!chromeos::NetworkHandler::IsInitialized()) | 281 if (!chromeos::NetworkHandler::IsInitialized()) |
444 return NULL; | 282 return NULL; |
445 // Clear any notifications when showing the detailed view. | |
446 messages_->messages().clear(); | |
447 HideNotificationView(); | |
448 if (request_wifi_view_) { | 283 if (request_wifi_view_) { |
449 detailed_ = new tray::NetworkWifiDetailedView(this); | 284 detailed_ = new tray::NetworkWifiDetailedView(this); |
450 request_wifi_view_ = false; | 285 request_wifi_view_ = false; |
451 } else { | 286 } else { |
452 detailed_ = new tray::NetworkStateListDetailedView( | 287 detailed_ = new tray::NetworkStateListDetailedView( |
453 this, tray::NetworkStateListDetailedView::LIST_TYPE_NETWORK, status); | 288 this, tray::NetworkStateListDetailedView::LIST_TYPE_NETWORK, status); |
454 detailed_->Init(); | 289 detailed_->Init(); |
455 } | 290 } |
456 return detailed_; | 291 return detailed_; |
457 } | 292 } |
458 | 293 |
459 views::View* TrayNetwork::CreateNotificationView(user::LoginStatus status) { | |
460 CHECK(notification_ == NULL); | |
461 if (messages_->messages().empty()) | |
462 return NULL; // Message has already been cleared. | |
463 notification_ = new tray::NetworkNotificationView(this); | |
464 return notification_; | |
465 } | |
466 | |
467 void TrayNetwork::DestroyTrayView() { | 294 void TrayNetwork::DestroyTrayView() { |
468 tray_ = NULL; | 295 tray_ = NULL; |
469 } | 296 } |
470 | 297 |
471 void TrayNetwork::DestroyDefaultView() { | 298 void TrayNetwork::DestroyDefaultView() { |
472 default_ = NULL; | 299 default_ = NULL; |
473 } | 300 } |
474 | 301 |
475 void TrayNetwork::DestroyDetailedView() { | 302 void TrayNetwork::DestroyDetailedView() { |
476 detailed_ = NULL; | 303 detailed_ = NULL; |
477 } | 304 } |
478 | 305 |
479 void TrayNetwork::DestroyNotificationView() { | |
480 notification_ = NULL; | |
481 } | |
482 | |
483 void TrayNetwork::UpdateAfterLoginStatusChange(user::LoginStatus status) { | 306 void TrayNetwork::UpdateAfterLoginStatusChange(user::LoginStatus status) { |
484 } | 307 } |
485 | 308 |
486 void TrayNetwork::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) { | 309 void TrayNetwork::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) { |
487 if (tray_) | 310 if (tray_) |
488 SetTrayImageItemBorder(tray_, alignment); | 311 SetTrayImageItemBorder(tray_, alignment); |
489 } | 312 } |
490 | 313 |
491 void TrayNetwork::SetNetworkMessage(NetworkTrayDelegate* delegate, | |
492 MessageType message_type, | |
493 NetworkType network_type, | |
494 const base::string16& title, | |
495 const base::string16& message, | |
496 const std::vector<base::string16>& links) { | |
497 messages_->messages()[message_type] = tray::NetworkMessages::Message( | |
498 delegate, network_type, title, message, links); | |
499 if (!Shell::GetInstance()->system_tray_delegate()->IsOobeCompleted()) | |
500 return; | |
501 if (notification_) | |
502 notification_->Update(); | |
503 else | |
504 ShowNotificationView(); | |
505 } | |
506 | |
507 void TrayNetwork::ClearNetworkMessage(MessageType message_type) { | |
508 messages_->messages().erase(message_type); | |
509 if (messages_->messages().empty()) { | |
510 HideNotificationView(); | |
511 return; | |
512 } | |
513 if (notification_) | |
514 notification_->Update(); | |
515 else | |
516 ShowNotificationView(); | |
517 } | |
518 | |
519 void TrayNetwork::RequestToggleWifi() { | 314 void TrayNetwork::RequestToggleWifi() { |
520 // This will always be triggered by a user action (e.g. keyboard shortcut) | 315 // This will always be triggered by a user action (e.g. keyboard shortcut) |
521 if (!detailed_ || | 316 if (!detailed_ || |
522 detailed_->GetViewType() == tray::NetworkDetailedView::WIFI_VIEW) { | 317 detailed_->GetViewType() == tray::NetworkDetailedView::WIFI_VIEW) { |
523 request_wifi_view_ = true; | 318 request_wifi_view_ = true; |
524 PopupDetailedView(kTrayPopupAutoCloseDelayForTextInSeconds, false); | 319 PopupDetailedView(kTrayPopupAutoCloseDelayForTextInSeconds, false); |
525 } | 320 } |
526 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | 321 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); |
527 bool enabled = handler->IsTechnologyEnabled(flimflam::kTypeWifi); | 322 bool enabled = handler->IsTechnologyEnabled(flimflam::kTypeWifi); |
528 handler->SetTechnologyEnabled( | 323 handler->SetTechnologyEnabled( |
(...skipping 12 matching lines...) Expand all Loading... |
541 else | 336 else |
542 detailed_->ManagerChanged(); | 337 detailed_->ManagerChanged(); |
543 } | 338 } |
544 } | 339 } |
545 | 340 |
546 void TrayNetwork::NetworkServiceChanged(const chromeos::NetworkState* network) { | 341 void TrayNetwork::NetworkServiceChanged(const chromeos::NetworkState* network) { |
547 if (detailed_) | 342 if (detailed_) |
548 detailed_->NetworkServiceChanged(network); | 343 detailed_->NetworkServiceChanged(network); |
549 } | 344 } |
550 | 345 |
551 void TrayNetwork::LinkClicked(MessageType message_type, int link_id) { | |
552 tray::NetworkMessages::MessageMap::const_iterator iter = | |
553 messages()->messages().find(message_type); | |
554 if (iter != messages()->messages().end() && iter->second.delegate) | |
555 iter->second.delegate->NotificationLinkClicked(message_type, link_id); | |
556 } | |
557 | |
558 } // namespace internal | 346 } // namespace internal |
559 } // namespace ash | 347 } // namespace ash |
OLD | NEW |