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/network/tray_network.h" | 5 #include "ash/system/network/tray_network.h" |
6 | 6 |
7 #include "ash/shelf/wm_shelf_util.h" | 7 #include "ash/shelf/wm_shelf_util.h" |
8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "ash/shell_port.h" | 9 #include "ash/shell_port.h" |
10 #include "ash/strings/grit/ash_strings.h" | 10 #include "ash/strings/grit/ash_strings.h" |
11 #include "ash/system/network/network_icon.h" | 11 #include "ash/system/network/network_icon.h" |
12 #include "ash/system/network/network_icon_animation.h" | 12 #include "ash/system/network/network_icon_animation.h" |
13 #include "ash/system/network/network_icon_animation_observer.h" | 13 #include "ash/system/network/network_icon_animation_observer.h" |
14 #include "ash/system/network/network_state_list_detailed_view.h" | 14 #include "ash/system/network/network_state_list_detailed_view.h" |
15 #include "ash/system/network/tray_network_state_observer.h" | 15 #include "ash/system/network/tray_network_state_observer.h" |
| 16 #include "ash/system/system_notifier.h" |
16 #include "ash/system/tray/system_tray.h" | 17 #include "ash/system/tray/system_tray.h" |
17 #include "ash/system/tray/system_tray_delegate.h" | 18 #include "ash/system/tray/system_tray_delegate.h" |
18 #include "ash/system/tray/system_tray_notifier.h" | 19 #include "ash/system/tray/system_tray_notifier.h" |
19 #include "ash/system/tray/tray_constants.h" | 20 #include "ash/system/tray/tray_constants.h" |
20 #include "ash/system/tray/tray_item_more.h" | 21 #include "ash/system/tray/tray_item_more.h" |
21 #include "ash/system/tray/tray_item_view.h" | 22 #include "ash/system/tray/tray_item_view.h" |
22 #include "ash/system/tray/tray_popup_item_style.h" | 23 #include "ash/system/tray/tray_popup_item_style.h" |
23 #include "ash/system/tray/tray_utils.h" | 24 #include "ash/system/tray/tray_utils.h" |
24 #include "base/command_line.h" | 25 #include "base/command_line.h" |
25 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
26 #include "chromeos/network/network_state.h" | 27 #include "chromeos/network/network_state.h" |
27 #include "chromeos/network/network_state_handler.h" | 28 #include "chromeos/network/network_state_handler.h" |
28 #include "third_party/cros_system_api/dbus/service_constants.h" | 29 #include "third_party/cros_system_api/dbus/service_constants.h" |
29 #include "ui/accessibility/ax_node_data.h" | 30 #include "ui/accessibility/ax_node_data.h" |
30 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
31 #include "ui/gfx/image/image_skia_operations.h" | 32 #include "ui/gfx/image/image_skia_operations.h" |
| 33 #include "ui/message_center/message_center.h" |
| 34 #include "ui/message_center/notification.h" |
32 #include "ui/views/controls/image_view.h" | 35 #include "ui/views/controls/image_view.h" |
33 #include "ui/views/controls/link.h" | 36 #include "ui/views/controls/link.h" |
34 #include "ui/views/controls/link_listener.h" | 37 #include "ui/views/controls/link_listener.h" |
35 #include "ui/views/layout/box_layout.h" | |
36 #include "ui/views/widget/widget.h" | 38 #include "ui/views/widget/widget.h" |
37 | 39 |
38 using chromeos::NetworkHandler; | 40 using chromeos::NetworkHandler; |
39 using chromeos::NetworkState; | 41 using chromeos::NetworkState; |
40 using chromeos::NetworkStateHandler; | 42 using chromeos::NetworkStateHandler; |
41 using chromeos::NetworkTypePattern; | 43 using chromeos::NetworkTypePattern; |
| 44 using message_center::Notification; |
42 | 45 |
43 namespace ash { | 46 namespace ash { |
44 namespace tray { | 47 namespace tray { |
45 | 48 |
46 namespace { | 49 namespace { |
47 | 50 |
| 51 constexpr char kWifiToggleNotificationId[] = "wifi-toggle"; |
| 52 |
48 // Returns the connected, non-virtual (aka VPN), network. | 53 // Returns the connected, non-virtual (aka VPN), network. |
49 const NetworkState* GetConnectedNetwork() { | 54 const NetworkState* GetConnectedNetwork() { |
50 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | 55 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); |
51 return handler->ConnectedNetworkByType(NetworkTypePattern::NonVirtual()); | 56 return handler->ConnectedNetworkByType(NetworkTypePattern::NonVirtual()); |
52 } | 57 } |
53 | 58 |
| 59 std::unique_ptr<Notification> CreateNotification(bool wifi_enabled) { |
| 60 const int string_id = wifi_enabled |
| 61 ? IDS_ASH_STATUS_TRAY_NETWORK_WIFI_ENABLED |
| 62 : IDS_ASH_STATUS_TRAY_NETWORK_WIFI_DISABLED; |
| 63 std::unique_ptr<Notification> notification(new Notification( |
| 64 message_center::NOTIFICATION_TYPE_SIMPLE, kWifiToggleNotificationId, |
| 65 base::string16(), l10n_util::GetStringUTF16(string_id), |
| 66 gfx::Image(network_icon::GetImageForWiFiEnabledState(wifi_enabled)), |
| 67 base::string16() /* display_source */, GURL(), |
| 68 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
| 69 system_notifier::kNotifierWifiToggle), |
| 70 message_center::RichNotificationData(), nullptr)); |
| 71 return notification; |
| 72 } |
| 73 |
54 } // namespace | 74 } // namespace |
55 | 75 |
56 class NetworkTrayView : public TrayItemView, | 76 class NetworkTrayView : public TrayItemView, |
57 public network_icon::AnimationObserver { | 77 public network_icon::AnimationObserver { |
58 public: | 78 public: |
59 explicit NetworkTrayView(TrayNetwork* network_tray) | 79 explicit NetworkTrayView(TrayNetwork* network_tray) |
60 : TrayItemView(network_tray) { | 80 : TrayItemView(network_tray) { |
61 CreateImageView(); | 81 CreateImageView(); |
62 UpdateNetworkStateHandlerIcon(); | 82 UpdateNetworkStateHandlerIcon(); |
63 } | 83 } |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 return style; | 197 return style; |
178 } | 198 } |
179 | 199 |
180 // Determines whether to use the ACTIVE or INACTIVE text style. | 200 // Determines whether to use the ACTIVE or INACTIVE text style. |
181 bool IsActive() const { return GetConnectedNetwork() != nullptr; } | 201 bool IsActive() const { return GetConnectedNetwork() != nullptr; } |
182 | 202 |
183 private: | 203 private: |
184 DISALLOW_COPY_AND_ASSIGN(NetworkDefaultView); | 204 DISALLOW_COPY_AND_ASSIGN(NetworkDefaultView); |
185 }; | 205 }; |
186 | 206 |
187 class NetworkWifiDetailedView : public NetworkDetailedView { | |
188 public: | |
189 explicit NetworkWifiDetailedView(SystemTrayItem* owner) | |
190 : NetworkDetailedView(owner) {} | |
191 | |
192 ~NetworkWifiDetailedView() override {} | |
193 | |
194 // NetworkDetailedView: | |
195 void Init() override { | |
196 constexpr int kVerticalPadding = 10; | |
197 auto* box_layout = new views::BoxLayout( | |
198 views::BoxLayout::kHorizontal, kTrayPopupPaddingHorizontal, | |
199 kVerticalPadding, kTrayPopupPaddingBetweenItems); | |
200 SetLayoutManager(box_layout); | |
201 | |
202 image_view_ = new views::ImageView; | |
203 AddChildView(image_view_); | |
204 | |
205 label_view_ = new views::Label(); | |
206 label_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
207 AddChildView(label_view_); | |
208 box_layout->SetFlexForView(label_view_, 1); | |
209 | |
210 Update(); | |
211 } | |
212 | |
213 NetworkDetailedView::DetailedViewType GetViewType() const override { | |
214 return NetworkDetailedView::WIFI_VIEW; | |
215 } | |
216 | |
217 void Update() override { | |
218 const bool wifi_enabled = | |
219 NetworkHandler::Get()->network_state_handler()->IsTechnologyEnabled( | |
220 NetworkTypePattern::WiFi()); | |
221 image_view_->SetImage( | |
222 network_icon::GetImageForWiFiEnabledState(wifi_enabled)); | |
223 | |
224 const int string_id = wifi_enabled | |
225 ? IDS_ASH_STATUS_TRAY_NETWORK_WIFI_ENABLED | |
226 : IDS_ASH_STATUS_TRAY_NETWORK_WIFI_DISABLED; | |
227 label_view_->SetText(l10n_util::GetStringUTF16(string_id)); | |
228 } | |
229 | |
230 private: | |
231 views::ImageView* image_view_ = nullptr; | |
232 views::Label* label_view_ = nullptr; | |
233 | |
234 DISALLOW_COPY_AND_ASSIGN(NetworkWifiDetailedView); | |
235 }; | |
236 | |
237 } // namespace tray | 207 } // namespace tray |
238 | 208 |
239 TrayNetwork::TrayNetwork(SystemTray* system_tray) | 209 TrayNetwork::TrayNetwork(SystemTray* system_tray) |
240 : SystemTrayItem(system_tray, UMA_NETWORK), | 210 : SystemTrayItem(system_tray, UMA_NETWORK), |
241 tray_(NULL), | 211 tray_(NULL), |
242 default_(NULL), | 212 default_(NULL), |
243 detailed_(NULL), | 213 detailed_(NULL) { |
244 request_wifi_view_(false) { | |
245 network_state_observer_.reset(new TrayNetworkStateObserver(this)); | 214 network_state_observer_.reset(new TrayNetworkStateObserver(this)); |
246 SystemTrayNotifier* notifier = Shell::Get()->system_tray_notifier(); | 215 SystemTrayNotifier* notifier = Shell::Get()->system_tray_notifier(); |
247 notifier->AddNetworkObserver(this); | 216 notifier->AddNetworkObserver(this); |
248 notifier->AddNetworkPortalDetectorObserver(this); | 217 notifier->AddNetworkPortalDetectorObserver(this); |
249 } | 218 } |
250 | 219 |
251 TrayNetwork::~TrayNetwork() { | 220 TrayNetwork::~TrayNetwork() { |
252 SystemTrayNotifier* notifier = Shell::Get()->system_tray_notifier(); | 221 SystemTrayNotifier* notifier = Shell::Get()->system_tray_notifier(); |
253 notifier->RemoveNetworkObserver(this); | 222 notifier->RemoveNetworkObserver(this); |
254 notifier->RemoveNetworkPortalDetectorObserver(this); | 223 notifier->RemoveNetworkPortalDetectorObserver(this); |
(...skipping 16 matching lines...) Expand all Loading... |
271 default_->SetEnabled(status != LoginStatus::LOCKED); | 240 default_->SetEnabled(status != LoginStatus::LOCKED); |
272 return default_; | 241 return default_; |
273 } | 242 } |
274 | 243 |
275 views::View* TrayNetwork::CreateDetailedView(LoginStatus status) { | 244 views::View* TrayNetwork::CreateDetailedView(LoginStatus status) { |
276 CHECK(detailed_ == NULL); | 245 CHECK(detailed_ == NULL); |
277 ShellPort::Get()->RecordUserMetricsAction( | 246 ShellPort::Get()->RecordUserMetricsAction( |
278 UMA_STATUS_AREA_DETAILED_NETWORK_VIEW); | 247 UMA_STATUS_AREA_DETAILED_NETWORK_VIEW); |
279 if (!chromeos::NetworkHandler::IsInitialized()) | 248 if (!chromeos::NetworkHandler::IsInitialized()) |
280 return NULL; | 249 return NULL; |
281 if (request_wifi_view_) { | 250 detailed_ = new tray::NetworkStateListDetailedView( |
282 detailed_ = new tray::NetworkWifiDetailedView(this); | 251 this, tray::NetworkStateListDetailedView::LIST_TYPE_NETWORK, status); |
283 request_wifi_view_ = false; | |
284 } else { | |
285 detailed_ = new tray::NetworkStateListDetailedView( | |
286 this, tray::NetworkStateListDetailedView::LIST_TYPE_NETWORK, status); | |
287 } | |
288 detailed_->Init(); | 252 detailed_->Init(); |
289 return detailed_; | 253 return detailed_; |
290 } | 254 } |
291 | 255 |
292 void TrayNetwork::DestroyTrayView() { | 256 void TrayNetwork::DestroyTrayView() { |
293 tray_ = NULL; | 257 tray_ = NULL; |
294 } | 258 } |
295 | 259 |
296 void TrayNetwork::DestroyDefaultView() { | 260 void TrayNetwork::DestroyDefaultView() { |
297 default_ = NULL; | 261 default_ = NULL; |
298 } | 262 } |
299 | 263 |
300 void TrayNetwork::DestroyDetailedView() { | 264 void TrayNetwork::DestroyDetailedView() { |
301 detailed_ = NULL; | 265 detailed_ = NULL; |
302 } | 266 } |
303 | 267 |
304 void TrayNetwork::RequestToggleWifi() { | 268 void TrayNetwork::RequestToggleWifi() { |
305 // This will always be triggered by a user action (e.g. keyboard shortcut) | 269 // This will always be triggered by a user action (e.g. keyboard shortcut) |
306 if (!detailed_ || | |
307 detailed_->GetViewType() == tray::NetworkDetailedView::WIFI_VIEW) { | |
308 request_wifi_view_ = true; | |
309 ShowDetailedView(kTrayPopupAutoCloseDelayForTextInSeconds, false); | |
310 } | |
311 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | 270 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); |
312 bool enabled = handler->IsTechnologyEnabled(NetworkTypePattern::WiFi()); | 271 bool enabled = handler->IsTechnologyEnabled(NetworkTypePattern::WiFi()); |
313 ShellPort::Get()->RecordUserMetricsAction( | 272 ShellPort::Get()->RecordUserMetricsAction( |
314 enabled ? UMA_STATUS_AREA_DISABLE_WIFI : UMA_STATUS_AREA_ENABLE_WIFI); | 273 enabled ? UMA_STATUS_AREA_DISABLE_WIFI : UMA_STATUS_AREA_ENABLE_WIFI); |
315 handler->SetTechnologyEnabled(NetworkTypePattern::WiFi(), !enabled, | 274 handler->SetTechnologyEnabled(NetworkTypePattern::WiFi(), !enabled, |
316 chromeos::network_handler::ErrorCallback()); | 275 chromeos::network_handler::ErrorCallback()); |
| 276 message_center::MessageCenter* message_center = |
| 277 message_center::MessageCenter::Get(); |
| 278 if (message_center->FindVisibleNotificationById( |
| 279 tray::kWifiToggleNotificationId)) { |
| 280 message_center->RemoveNotification(tray::kWifiToggleNotificationId, false); |
| 281 } |
| 282 message_center->AddNotification(tray::CreateNotification(!enabled)); |
317 } | 283 } |
318 | 284 |
319 void TrayNetwork::OnCaptivePortalDetected(const std::string& /* guid */) { | 285 void TrayNetwork::OnCaptivePortalDetected(const std::string& /* guid */) { |
320 NetworkStateChanged(); | 286 NetworkStateChanged(); |
321 } | 287 } |
322 | 288 |
323 void TrayNetwork::NetworkStateChanged() { | 289 void TrayNetwork::NetworkStateChanged() { |
324 if (tray_) | 290 if (tray_) |
325 tray_->UpdateNetworkStateHandlerIcon(); | 291 tray_->UpdateNetworkStateHandlerIcon(); |
326 if (default_) | 292 if (default_) |
327 default_->Update(); | 293 default_->Update(); |
328 if (detailed_) | 294 if (detailed_) |
329 detailed_->Update(); | 295 detailed_->Update(); |
330 } | 296 } |
331 | 297 |
332 } // namespace ash | 298 } // namespace ash |
OLD | NEW |