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