OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ash/common/system/chromeos/network/network_list_md.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "ash/common/system/chromeos/network/network_icon.h" | |
10 #include "ash/common/system/chromeos/network/network_icon_animation.h" | |
11 #include "ash/common/system/chromeos/network/network_list_delegate.h" | |
12 #include "ash/common/system/tray/system_menu_button.h" | |
13 #include "ash/common/system/tray/tray_constants.h" | |
14 #include "ash/common/system/tray/tray_popup_item_style.h" | |
15 #include "ash/common/system/tray/tray_popup_utils.h" | |
16 #include "ash/strings/grit/ash_strings.h" | |
17 #include "base/memory/ptr_util.h" | |
18 #include "chromeos/dbus/dbus_thread_manager.h" | |
19 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" | |
20 #include "chromeos/dbus/power_manager_client.h" | |
21 #include "chromeos/login/login_state.h" | |
22 #include "chromeos/network/managed_network_configuration_handler.h" | |
23 #include "chromeos/network/network_state.h" | |
24 #include "chromeos/network/network_state_handler.h" | |
25 #include "chromeos/network/network_state_handler_observer.h" | |
26 #include "components/device_event_log/device_event_log.h" | |
27 #include "ui/base/l10n/l10n_util.h" | |
28 #include "ui/base/resource/resource_bundle.h" | |
29 #include "ui/gfx/color_palette.h" | |
30 #include "ui/gfx/font.h" | |
31 #include "ui/gfx/paint_vector_icon.h" | |
32 #include "ui/views/controls/button/toggle_button.h" | |
33 #include "ui/views/controls/label.h" | |
34 #include "ui/views/controls/separator.h" | |
35 #include "ui/views/layout/box_layout.h" | |
36 #include "ui/views/layout/fill_layout.h" | |
37 #include "ui/views/painter.h" | |
38 #include "ui/views/view.h" | |
39 | |
40 using chromeos::LoginState; | |
41 using chromeos::NetworkHandler; | |
42 using chromeos::NetworkStateHandler; | |
43 using chromeos::ManagedNetworkConfigurationHandler; | |
44 using chromeos::NetworkTypePattern; | |
45 | |
46 namespace ash { | |
47 | |
48 namespace { | |
49 | |
50 bool IsProhibitedByPolicy(const chromeos::NetworkState* network) { | |
51 if (!NetworkTypePattern::WiFi().MatchesType(network->type())) | |
52 return false; | |
53 if (!LoginState::IsInitialized() || !LoginState::Get()->IsUserLoggedIn()) | |
54 return false; | |
55 ManagedNetworkConfigurationHandler* managed_configuration_handler = | |
56 NetworkHandler::Get()->managed_network_configuration_handler(); | |
57 const base::DictionaryValue* global_network_config = | |
58 managed_configuration_handler->GetGlobalConfigFromPolicy( | |
59 std::string() /* no username hash, device policy */); | |
60 bool policy_prohibites_unmanaged = false; | |
61 if (global_network_config) { | |
62 global_network_config->GetBooleanWithoutPathExpansion( | |
63 ::onc::global_network_config::kAllowOnlyPolicyNetworksToConnect, | |
64 &policy_prohibites_unmanaged); | |
65 } | |
66 if (!policy_prohibites_unmanaged) | |
67 return false; | |
68 return !managed_configuration_handler->FindPolicyByGuidAndProfile( | |
69 network->guid(), network->profile_path()); | |
70 } | |
71 | |
72 } // namespace | |
73 | |
74 // A header row for sections in network detailed view which contains a title and | |
75 // a toggle button to turn on/off the section. Subclasses are given the | |
76 // opportunity to add extra buttons before the toggle button is added. | |
77 class NetworkListView::SectionHeaderRowView : public views::View, | |
78 public views::ButtonListener { | |
79 public: | |
80 explicit SectionHeaderRowView(int title_id) | |
81 : title_id_(title_id), | |
82 container_(nullptr), | |
83 toggle_(nullptr), | |
84 style_( | |
85 new TrayPopupItemStyle(TrayPopupItemStyle::FontStyle::SUB_HEADER)) { | |
86 } | |
87 | |
88 ~SectionHeaderRowView() override {} | |
89 | |
90 void Init(bool enabled) { | |
91 InitializeLayout(); | |
92 AddExtraButtons(enabled); | |
93 AddToggleButton(enabled); | |
94 } | |
95 | |
96 virtual void SetIsOn(bool enabled) { | |
97 toggle_->SetEnabled(true); | |
98 toggle_->SetIsOn(enabled, true); | |
99 } | |
100 | |
101 protected: | |
102 // This is called before the toggle button is added to give subclasses an | |
103 // opportunity to add more buttons before the toggle button. Subclasses can | |
104 // add buttons to container() using AddChildView(). | |
105 virtual void AddExtraButtons(bool enabled) {} | |
106 | |
107 // Called when |toggle_| is clicked and toggled. Subclasses can override to | |
108 // enabled/disable their respective technology, for example. | |
109 virtual void OnToggleToggled(bool is_on) = 0; | |
110 | |
111 TriView* container() const { return container_; } | |
112 TrayPopupItemStyle* style() const { return style_.get(); } | |
113 | |
114 int GetHeightForWidth(int w) const override { | |
115 // Make row height fixed avoiding layout manager adjustments. | |
116 return GetPreferredSize().height(); | |
117 } | |
118 | |
119 // views::ButtonListener: | |
120 void ButtonPressed(views::Button* sender, const ui::Event& event) override { | |
121 DCHECK_EQ(toggle_, sender); | |
122 // In the event of frequent clicks, helps to prevent a toggle button state | |
123 // from becoming inconsistent with the async operation of enabling / | |
124 // disabling of mobile radio. The toggle will get re-enabled in the next | |
125 // call to NetworkListView::Update(). | |
126 toggle_->SetEnabled(false); | |
127 OnToggleToggled(toggle_->is_on()); | |
128 } | |
129 | |
130 private: | |
131 void InitializeLayout() { | |
132 TrayPopupUtils::ConfigureAsStickyHeader(this); | |
133 SetLayoutManager(new views::FillLayout); | |
134 container_ = TrayPopupUtils::CreateSubHeaderRowView(); | |
135 AddChildView(container_); | |
136 | |
137 views::Label* label = TrayPopupUtils::CreateDefaultLabel(); | |
138 style()->SetupLabel(label); | |
139 label->SetText(l10n_util::GetStringUTF16(title_id_)); | |
140 container_->AddView(TriView::Container::CENTER, label); | |
141 } | |
142 | |
143 void AddToggleButton(bool enabled) { | |
144 toggle_ = TrayPopupUtils::CreateToggleButton(this, title_id_); | |
145 toggle_->SetIsOn(enabled, false); | |
146 container_->AddView(TriView::Container::END, toggle_); | |
147 } | |
148 | |
149 // Resource ID for the string to use as the title of the section and for the | |
150 // accessible text on the section header toggle button. | |
151 const int title_id_; | |
152 | |
153 // View containing header row views, including title, toggle, and extra | |
154 // buttons. | |
155 TriView* container_; | |
156 | |
157 // ToggleButton to toggle section on or off. | |
158 views::ToggleButton* toggle_; | |
159 | |
160 // TrayPopupItemStyle used to configure labels and buttons. | |
161 std::unique_ptr<TrayPopupItemStyle> style_; | |
162 | |
163 DISALLOW_COPY_AND_ASSIGN(SectionHeaderRowView); | |
164 }; | |
165 | |
166 namespace { | |
167 | |
168 class CellularHeaderRowView : public NetworkListView::SectionHeaderRowView { | |
169 public: | |
170 CellularHeaderRowView() | |
171 : SectionHeaderRowView(IDS_ASH_STATUS_TRAY_NETWORK_MOBILE) {} | |
172 | |
173 ~CellularHeaderRowView() override {} | |
174 | |
175 const char* GetClassName() const override { return "CellularHeaderRowView"; } | |
176 | |
177 protected: | |
178 void OnToggleToggled(bool is_on) override { | |
179 NetworkStateHandler* handler = | |
180 NetworkHandler::Get()->network_state_handler(); | |
181 handler->SetTechnologyEnabled(NetworkTypePattern::Cellular(), is_on, | |
182 chromeos::network_handler::ErrorCallback()); | |
183 } | |
184 | |
185 private: | |
186 DISALLOW_COPY_AND_ASSIGN(CellularHeaderRowView); | |
187 }; | |
188 | |
189 class TetherHeaderRowView : public NetworkListView::SectionHeaderRowView { | |
190 public: | |
191 TetherHeaderRowView() | |
192 : SectionHeaderRowView(IDS_ASH_STATUS_TRAY_NETWORK_TETHER) {} | |
193 | |
194 ~TetherHeaderRowView() override {} | |
195 | |
196 const char* GetClassName() const override { return "TetherHeaderRowView"; } | |
197 | |
198 protected: | |
199 void OnToggleToggled(bool is_on) override { | |
200 // TODO (hansberry): Persist toggle to settings/preferences. | |
201 } | |
202 | |
203 private: | |
204 DISALLOW_COPY_AND_ASSIGN(TetherHeaderRowView); | |
205 }; | |
206 | |
207 class WifiHeaderRowView : public NetworkListView::SectionHeaderRowView { | |
208 public: | |
209 explicit WifiHeaderRowView(NetworkListDelegate* network_list_delegate) | |
210 : SectionHeaderRowView(IDS_ASH_STATUS_TRAY_NETWORK_WIFI), | |
211 network_list_delegate_(network_list_delegate), | |
212 join_(nullptr) {} | |
213 | |
214 ~WifiHeaderRowView() override {} | |
215 | |
216 void SetIsOn(bool enabled) override { | |
217 join_->SetEnabled(enabled); | |
218 SectionHeaderRowView::SetIsOn(enabled); | |
219 } | |
220 | |
221 const char* GetClassName() const override { return "WifiHeaderRowView"; } | |
222 | |
223 protected: | |
224 // SectionHeaderRowView: | |
225 void OnToggleToggled(bool is_on) override { | |
226 NetworkStateHandler* handler = | |
227 NetworkHandler::Get()->network_state_handler(); | |
228 handler->SetTechnologyEnabled(NetworkTypePattern::WiFi(), is_on, | |
229 chromeos::network_handler::ErrorCallback()); | |
230 } | |
231 | |
232 void AddExtraButtons(bool enabled) override { | |
233 const SkColor prominent_color = GetNativeTheme()->GetSystemColor( | |
234 ui::NativeTheme::kColorId_ProminentButtonColor); | |
235 gfx::ImageSkia normal_image = network_icon::GetImageForNewWifiNetwork( | |
236 SkColorSetA(prominent_color, kJoinIconAlpha), | |
237 SkColorSetA(prominent_color, kJoinBadgeAlpha)); | |
238 gfx::ImageSkia disabled_image = network_icon::GetImageForNewWifiNetwork( | |
239 SkColorSetA(prominent_color, kDisabledJoinIconAlpha), | |
240 SkColorSetA(prominent_color, kDisabledJoinBadgeAlpha)); | |
241 join_ = new SystemMenuButton(this, TrayPopupInkDropStyle::HOST_CENTERED, | |
242 normal_image, disabled_image, | |
243 IDS_ASH_STATUS_TRAY_OTHER_WIFI); | |
244 join_->SetInkDropColor(prominent_color); | |
245 join_->SetEnabled(enabled); | |
246 container()->AddView(TriView::Container::END, join_); | |
247 } | |
248 | |
249 void ButtonPressed(views::Button* sender, const ui::Event& event) override { | |
250 if (sender == join_) { | |
251 network_list_delegate_->OnOtherWifiClicked(); | |
252 return; | |
253 } | |
254 SectionHeaderRowView::ButtonPressed(sender, event); | |
255 } | |
256 | |
257 private: | |
258 // Full opacity for badge. | |
259 static constexpr int kJoinBadgeAlpha = 0xFF; | |
260 | |
261 // .30 opacity for icon. | |
262 static constexpr int kJoinIconAlpha = 0x4D; | |
263 | |
264 // .38 opacity for disabled badge. | |
265 static constexpr int kDisabledJoinBadgeAlpha = 0x61; | |
266 | |
267 // .30 * .38 opacity for disabled icon. | |
268 static constexpr int kDisabledJoinIconAlpha = 0x1D; | |
269 | |
270 NetworkListDelegate* network_list_delegate_; | |
271 | |
272 // A button to invoke "Join Wi-Fi network" dialog. | |
273 SystemMenuButton* join_; | |
274 | |
275 DISALLOW_COPY_AND_ASSIGN(WifiHeaderRowView); | |
276 }; | |
277 | |
278 } // namespace | |
279 | |
280 // NetworkListView: | |
281 | |
282 NetworkListView::NetworkListView(NetworkListDelegate* delegate) | |
283 : needs_relayout_(false), | |
284 delegate_(delegate), | |
285 no_wifi_networks_view_(nullptr), | |
286 no_cellular_networks_view_(nullptr), | |
287 cellular_header_view_(nullptr), | |
288 tether_header_view_(nullptr), | |
289 wifi_header_view_(nullptr), | |
290 cellular_separator_view_(nullptr), | |
291 tether_separator_view_(nullptr), | |
292 wifi_separator_view_(nullptr) { | |
293 CHECK(delegate_); | |
294 } | |
295 | |
296 NetworkListView::~NetworkListView() { | |
297 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this); | |
298 } | |
299 | |
300 void NetworkListView::Update() { | |
301 CHECK(container()); | |
302 | |
303 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | |
304 | |
305 NetworkStateHandler::NetworkStateList network_list; | |
306 handler->GetVisibleNetworkList(&network_list); | |
307 UpdateNetworks(network_list); | |
308 | |
309 // Add Tether networks. | |
310 NetworkStateHandler::NetworkStateList tether_network_list; | |
311 handler->GetTetherNetworkList(0 /* no limit */, &tether_network_list); | |
312 for (const auto* tether_network : tether_network_list) { | |
313 network_list_.push_back( | |
314 base::MakeUnique<NetworkInfo>(tether_network->guid())); | |
315 } | |
316 | |
317 UpdateNetworkIcons(); | |
318 OrderNetworks(); | |
319 UpdateNetworkListInternal(); | |
320 } | |
321 | |
322 bool NetworkListView::IsNetworkEntry(views::View* view, | |
323 std::string* guid) const { | |
324 std::map<views::View*, std::string>::const_iterator found = | |
325 network_map_.find(view); | |
326 if (found == network_map_.end()) | |
327 return false; | |
328 *guid = found->second; | |
329 return true; | |
330 } | |
331 | |
332 void NetworkListView::UpdateNetworks( | |
333 const NetworkStateHandler::NetworkStateList& networks) { | |
334 SCOPED_NET_LOG_IF_SLOW(); | |
335 // |network_list_| contains all the info and is going to be cleared and | |
336 // recreated. Save them to |last_network_info_map_|. | |
337 last_network_info_map_.clear(); | |
338 for (auto& info : network_list_) | |
339 last_network_info_map_[info->guid] = std::move(info); | |
340 | |
341 network_list_.clear(); | |
342 const NetworkTypePattern pattern = delegate_->GetNetworkTypePattern(); | |
343 for (const auto* network : networks) { | |
344 if (!pattern.MatchesType(network->type())) | |
345 continue; | |
346 | |
347 // Do not add Wi-Fi networks that are associated with a Tether network. | |
348 if (NetworkTypePattern::WiFi().MatchesType(network->type()) && | |
349 !network->tether_guid().empty()) | |
350 continue; | |
351 | |
352 network_list_.push_back(base::MakeUnique<NetworkInfo>(network->guid())); | |
353 } | |
354 } | |
355 | |
356 void NetworkListView::OrderNetworks() { | |
357 struct CompareNetwork { | |
358 explicit CompareNetwork(NetworkStateHandler* handler) : handler_(handler) {} | |
359 | |
360 // Returns true if |network1| is less than (i.e. is ordered before) | |
361 // |network2|. | |
362 bool operator()(const std::unique_ptr<NetworkInfo>& network1, | |
363 const std::unique_ptr<NetworkInfo>& network2) { | |
364 const int order1 = | |
365 GetOrder(handler_->GetNetworkStateFromGuid(network1->guid)); | |
366 const int order2 = | |
367 GetOrder(handler_->GetNetworkStateFromGuid(network2->guid)); | |
368 if (order1 != order2) | |
369 return order1 < order2; | |
370 if (network1->connected != network2->connected) | |
371 return network1->connected; | |
372 if (network1->connecting != network2->connecting) | |
373 return network1->connecting; | |
374 if (network1->highlight != network2->highlight) | |
375 return network1->highlight; | |
376 return network1->guid.compare(network2->guid) < 0; | |
377 } | |
378 | |
379 private: | |
380 static int GetOrder(const chromeos::NetworkState* network) { | |
381 if (!network) | |
382 return 999; | |
383 if (network->Matches(NetworkTypePattern::Ethernet())) | |
384 return 0; | |
385 if (network->Matches(NetworkTypePattern::Cellular())) | |
386 return 1; | |
387 if (network->Matches(NetworkTypePattern::Mobile())) | |
388 return 2; | |
389 if (network->Matches(NetworkTypePattern::WiFi())) | |
390 return 3; | |
391 return 4; | |
392 } | |
393 | |
394 NetworkStateHandler* handler_; | |
395 }; | |
396 std::sort(network_list_.begin(), network_list_.end(), | |
397 CompareNetwork(NetworkHandler::Get()->network_state_handler())); | |
398 } | |
399 | |
400 void NetworkListView::UpdateNetworkIcons() { | |
401 SCOPED_NET_LOG_IF_SLOW(); | |
402 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | |
403 | |
404 // First, update state for all networks. | |
405 bool animating = false; | |
406 | |
407 for (auto& info : network_list_) { | |
408 const chromeos::NetworkState* network = | |
409 handler->GetNetworkStateFromGuid(info->guid); | |
410 if (!network) | |
411 continue; | |
412 bool prohibited_by_policy = IsProhibitedByPolicy(network); | |
413 info->label = network_icon::GetLabelForNetwork( | |
414 network, network_icon::ICON_TYPE_MENU_LIST); | |
415 info->image = | |
416 network_icon::GetImageForNetwork(network, network_icon::ICON_TYPE_LIST); | |
417 info->disable = | |
418 (network->activation_state() == shill::kActivationStateActivating) || | |
419 prohibited_by_policy; | |
420 info->connected = network->IsConnectedState(); | |
421 info->connecting = network->IsConnectingState(); | |
422 info->highlight = info->connected || info->connecting; | |
423 if (network->Matches(NetworkTypePattern::WiFi())) | |
424 info->type = NetworkInfo::Type::WIFI; | |
425 else if (network->Matches(NetworkTypePattern::Cellular())) | |
426 info->type = NetworkInfo::Type::CELLULAR; | |
427 else if (network->Matches(NetworkTypePattern::Tether())) | |
428 info->type = NetworkInfo::Type::TETHER; | |
429 if (prohibited_by_policy) { | |
430 info->tooltip = | |
431 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NETWORK_PROHIBITED); | |
432 } | |
433 if (!animating && network->IsConnectingState()) | |
434 animating = true; | |
435 } | |
436 if (animating) | |
437 network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this); | |
438 else | |
439 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this); | |
440 } | |
441 | |
442 void NetworkListView::UpdateNetworkListInternal() { | |
443 SCOPED_NET_LOG_IF_SLOW(); | |
444 // Get the updated list entries. | |
445 needs_relayout_ = false; | |
446 network_map_.clear(); | |
447 std::unique_ptr<std::set<std::string>> new_guids = UpdateNetworkListEntries(); | |
448 | |
449 // Remove old children. | |
450 std::set<std::string> remove_guids; | |
451 for (const auto& iter : network_guid_map_) { | |
452 if (new_guids->find(iter.first) == new_guids->end()) { | |
453 remove_guids.insert(iter.first); | |
454 network_map_.erase(iter.second); | |
455 delete iter.second; | |
456 needs_relayout_ = true; | |
457 } | |
458 } | |
459 | |
460 for (const auto& remove_iter : remove_guids) | |
461 network_guid_map_.erase(remove_iter); | |
462 | |
463 if (!needs_relayout_) | |
464 return; | |
465 | |
466 views::View* selected_view = nullptr; | |
467 for (const auto& iter : network_guid_map_) { | |
468 if (iter.second->IsMouseHovered()) { | |
469 selected_view = iter.second; | |
470 break; | |
471 } | |
472 } | |
473 container()->SizeToPreferredSize(); | |
474 delegate_->RelayoutScrollList(); | |
475 if (selected_view) | |
476 container()->ScrollRectToVisible(selected_view->bounds()); | |
477 } | |
478 | |
479 std::unique_ptr<std::set<std::string>> | |
480 NetworkListView::UpdateNetworkListEntries() { | |
481 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | |
482 | |
483 // First add high-priority networks (not Wi-Fi nor cellular). | |
484 std::unique_ptr<std::set<std::string>> new_guids = | |
485 UpdateNetworkChildren(NetworkInfo::Type::UNKNOWN, 0); | |
486 | |
487 // Keep an index where the next child should be inserted. | |
488 int index = new_guids->size(); | |
489 | |
490 const NetworkTypePattern pattern = delegate_->GetNetworkTypePattern(); | |
491 if (pattern.MatchesPattern(NetworkTypePattern::Cellular())) { | |
492 if (handler->IsTechnologyAvailable(NetworkTypePattern::Cellular())) { | |
493 index = UpdateSectionHeaderRow( | |
494 NetworkTypePattern::Cellular(), | |
495 handler->IsTechnologyEnabled(NetworkTypePattern::Cellular()), index, | |
496 &cellular_header_view_, &cellular_separator_view_); | |
497 } | |
498 | |
499 // Cellular initializing. | |
500 int message_id = network_icon::GetCellularUninitializedMsg(); | |
501 if (!message_id && | |
502 handler->IsTechnologyEnabled(NetworkTypePattern::Mobile()) && | |
503 !handler->FirstNetworkByType(NetworkTypePattern::Mobile())) { | |
504 message_id = IDS_ASH_STATUS_TRAY_NO_MOBILE_NETWORKS; | |
505 } | |
506 UpdateInfoLabel(message_id, index, &no_cellular_networks_view_); | |
507 if (message_id) | |
508 ++index; | |
509 | |
510 // Add cellular networks. | |
511 std::unique_ptr<std::set<std::string>> new_cellular_guids = | |
512 UpdateNetworkChildren(NetworkInfo::Type::CELLULAR, index); | |
513 index += new_cellular_guids->size(); | |
514 new_guids->insert(new_cellular_guids->begin(), new_cellular_guids->end()); | |
515 } | |
516 | |
517 // TODO (hansberry): Audit existing usage of NonVirtual and consider changing | |
518 // it to include Tether. See crbug.com/693647. | |
519 if (handler->IsTechnologyAvailable(NetworkTypePattern::Tether())) { | |
520 index = UpdateSectionHeaderRow( | |
521 NetworkTypePattern::Tether(), | |
522 handler->IsTechnologyEnabled(NetworkTypePattern::Tether()), index, | |
523 &tether_header_view_, &tether_separator_view_); | |
524 | |
525 // TODO (hansberry): Should a message similar to | |
526 // IDS_ASH_STATUS_TRAY_NO_CELLULAR_NETWORKS be shown if Tether technology | |
527 // is enabled but no networks are around? | |
528 | |
529 // Add Tether networks. | |
530 std::unique_ptr<std::set<std::string>> new_tether_guids = | |
531 UpdateNetworkChildren(NetworkInfo::Type::TETHER, index); | |
532 index += new_tether_guids->size(); | |
533 new_guids->insert(new_tether_guids->begin(), new_tether_guids->end()); | |
534 } | |
535 | |
536 if (pattern.MatchesPattern(NetworkTypePattern::WiFi())) { | |
537 index = UpdateSectionHeaderRow( | |
538 NetworkTypePattern::WiFi(), | |
539 handler->IsTechnologyEnabled(NetworkTypePattern::WiFi()), index, | |
540 &wifi_header_view_, &wifi_separator_view_); | |
541 | |
542 // "Wifi Enabled / Disabled". | |
543 int message_id = 0; | |
544 if (network_list_.empty()) { | |
545 message_id = handler->IsTechnologyEnabled(NetworkTypePattern::WiFi()) | |
546 ? IDS_ASH_STATUS_TRAY_NETWORK_WIFI_ENABLED | |
547 : IDS_ASH_STATUS_TRAY_NETWORK_WIFI_DISABLED; | |
548 } | |
549 UpdateInfoLabel(message_id, index, &no_wifi_networks_view_); | |
550 if (message_id) | |
551 ++index; | |
552 | |
553 // Add Wi-Fi networks. | |
554 std::unique_ptr<std::set<std::string>> new_wifi_guids = | |
555 UpdateNetworkChildren(NetworkInfo::Type::WIFI, index); | |
556 index += new_wifi_guids->size(); | |
557 new_guids->insert(new_wifi_guids->begin(), new_wifi_guids->end()); | |
558 } | |
559 | |
560 // No networks or other messages (fallback). | |
561 if (index == 0) { | |
562 UpdateInfoLabel(IDS_ASH_STATUS_TRAY_NO_NETWORKS, index, | |
563 &no_wifi_networks_view_); | |
564 } | |
565 | |
566 return new_guids; | |
567 } | |
568 | |
569 std::unique_ptr<std::set<std::string>> NetworkListView::UpdateNetworkChildren( | |
570 NetworkInfo::Type type, | |
571 int index) { | |
572 std::unique_ptr<std::set<std::string>> new_guids(new std::set<std::string>); | |
573 for (const auto& info : network_list_) { | |
574 if (info->type != type) | |
575 continue; | |
576 UpdateNetworkChild(index++, info.get()); | |
577 new_guids->insert(info->guid); | |
578 } | |
579 return new_guids; | |
580 } | |
581 | |
582 void NetworkListView::UpdateNetworkChild(int index, const NetworkInfo* info) { | |
583 views::View* network_view = nullptr; | |
584 NetworkGuidMap::const_iterator found = network_guid_map_.find(info->guid); | |
585 if (found == network_guid_map_.end()) { | |
586 network_view = delegate_->CreateViewForNetwork(*info); | |
587 } else { | |
588 network_view = found->second; | |
589 if (NeedUpdateViewForNetwork(*info)) { | |
590 network_view->RemoveAllChildViews(true); | |
591 delegate_->UpdateViewForNetwork(network_view, *info); | |
592 network_view->Layout(); | |
593 network_view->SchedulePaint(); | |
594 } | |
595 } | |
596 PlaceViewAtIndex(network_view, index); | |
597 if (info->disable) | |
598 network_view->SetEnabled(false); | |
599 network_map_[network_view] = info->guid; | |
600 network_guid_map_[info->guid] = network_view; | |
601 } | |
602 | |
603 void NetworkListView::PlaceViewAtIndex(views::View* view, int index) { | |
604 if (view->parent() != container()) { | |
605 container()->AddChildViewAt(view, index); | |
606 } else { | |
607 if (container()->child_at(index) == view) | |
608 return; | |
609 container()->ReorderChildView(view, index); | |
610 } | |
611 needs_relayout_ = true; | |
612 } | |
613 | |
614 void NetworkListView::UpdateInfoLabel(int message_id, | |
615 int insertion_index, | |
616 views::Label** label_ptr) { | |
617 views::Label* label = *label_ptr; | |
618 if (!message_id) { | |
619 if (label) { | |
620 needs_relayout_ = true; | |
621 delete label; | |
622 *label_ptr = nullptr; | |
623 } | |
624 return; | |
625 } | |
626 base::string16 text = | |
627 ui::ResourceBundle::GetSharedInstance().GetLocalizedString(message_id); | |
628 if (!label) | |
629 label = delegate_->CreateInfoLabel(); | |
630 label->SetText(text); | |
631 PlaceViewAtIndex(label, insertion_index); | |
632 *label_ptr = label; | |
633 } | |
634 | |
635 int NetworkListView::UpdateSectionHeaderRow(NetworkTypePattern pattern, | |
636 bool enabled, | |
637 int child_index, | |
638 SectionHeaderRowView** view, | |
639 views::Separator** separator_view) { | |
640 if (!*view) { | |
641 if (pattern.Equals(NetworkTypePattern::Cellular())) | |
642 *view = new CellularHeaderRowView(); | |
643 else if (pattern.Equals(NetworkTypePattern::Tether())) | |
644 *view = new TetherHeaderRowView(); | |
645 else if (pattern.Equals(NetworkTypePattern::WiFi())) | |
646 *view = new WifiHeaderRowView(delegate_); | |
647 else | |
648 NOTREACHED(); | |
649 (*view)->Init(enabled); | |
650 } | |
651 // Show or hide a separator above the header. The separator should only be | |
652 // visible when the header row is not at the top of the list. | |
653 if (child_index > 0) { | |
654 if (!*separator_view) | |
655 *separator_view = TrayPopupUtils::CreateListSubHeaderSeparator(); | |
656 PlaceViewAtIndex(*separator_view, child_index++); | |
657 } else { | |
658 if (*separator_view) | |
659 delete *separator_view; | |
660 *separator_view = nullptr; | |
661 } | |
662 | |
663 (*view)->SetIsOn(enabled); | |
664 PlaceViewAtIndex(*view, child_index++); | |
665 return child_index; | |
666 } | |
667 | |
668 void NetworkListView::NetworkIconChanged() { | |
669 Update(); | |
670 } | |
671 | |
672 bool NetworkListView::NeedUpdateViewForNetwork(const NetworkInfo& info) const { | |
673 NetworkInfoMap::const_iterator found = last_network_info_map_.find(info.guid); | |
674 if (found == last_network_info_map_.end()) { | |
675 // If we cannot find |info| in |last_network_info_map_|, just return true | |
676 // since this is a new network so we have nothing to compare. | |
677 return true; | |
678 } else { | |
679 return *found->second != info; | |
680 } | |
681 } | |
682 | |
683 } // namespace ash | |
OLD | NEW |