OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/chromeos/network/network_list.h" | |
6 | |
7 #include "chromeos/dbus/dbus_thread_manager.h" | |
8 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" | |
9 #include "chromeos/dbus/power_manager_client.h" | |
10 #include "chromeos/network/network_state.h" | |
11 #include "chromeos/network/network_state_handler.h" | |
12 #include "chromeos/network/network_state_handler_observer.h" | |
13 #include "grit/ui_chromeos_strings.h" | |
14 #include "ui/base/resource/resource_bundle.h" | |
15 #include "ui/chromeos/network/network_icon.h" | |
16 #include "ui/chromeos/network/network_icon_animation.h" | |
17 #include "ui/chromeos/network/network_info.h" | |
18 #include "ui/chromeos/network/network_list_delegate.h" | |
19 #include "ui/gfx/font.h" | |
20 #include "ui/views/controls/label.h" | |
21 #include "ui/views/view.h" | |
22 | |
23 using chromeos::NetworkHandler; | |
24 using chromeos::NetworkStateHandler; | |
25 using chromeos::NetworkTypePattern; | |
26 | |
27 namespace ui { | |
28 | |
29 // NetworkListView: | |
30 | |
31 NetworkListView::NetworkListView(NetworkListDelegate* delegate) | |
32 : delegate_(delegate), | |
33 content_(NULL), | |
34 scanning_view_(NULL), | |
35 no_wifi_networks_view_(NULL), | |
36 no_cellular_networks_view_(NULL) { | |
37 CHECK(delegate); | |
38 } | |
39 | |
40 NetworkListView::~NetworkListView() { | |
41 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this); | |
42 } | |
43 | |
44 void NetworkListView::UpdateNetworkList() { | |
45 CHECK(content_); | |
46 NetworkStateHandler::NetworkStateList network_list; | |
47 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | |
48 handler->GetVisibleNetworkList(&network_list); | |
49 UpdateNetworks(network_list); | |
50 UpdateNetworkListInternal(); | |
51 } | |
52 | |
53 bool NetworkListView::IsViewInList(views::View* view, | |
54 std::string* service_path) const { | |
55 std::map<views::View*, std::string>::const_iterator found = | |
56 network_map_.find(view); | |
57 if (found == network_map_.end()) | |
58 return false; | |
59 *service_path = found->second; | |
60 return true; | |
61 } | |
62 | |
63 void NetworkListView::UpdateNetworks( | |
64 const NetworkStateHandler::NetworkStateList& networks) { | |
65 network_list_.clear(); | |
66 for (NetworkStateHandler::NetworkStateList::const_iterator iter = | |
67 networks.begin(); | |
68 iter != networks.end(); | |
69 ++iter) { | |
70 const chromeos::NetworkState* network = *iter; | |
71 if (delegate_->ShouldSkipNetwork(*network)) | |
72 continue; | |
73 NetworkInfo* info = new NetworkInfo(network->path()); | |
74 network_list_.push_back(info); | |
75 } | |
76 } | |
77 | |
78 void NetworkListView::UpdateNetworkListInternal() { | |
79 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | |
80 | |
81 // First, update state for all networks | |
82 bool animating = false; | |
83 for (size_t i = 0; i < network_list_.size(); ++i) { | |
84 NetworkInfo* info = network_list_[i]; | |
85 const chromeos::NetworkState* network = | |
86 handler->GetNetworkState(info->service_path); | |
87 if (!network) | |
88 continue; | |
89 info->image = | |
90 network_icon::GetImageForNetwork(network, network_icon::ICON_TYPE_LIST); | |
91 info->label = | |
92 network_icon::GetLabelForNetwork(network, network_icon::ICON_TYPE_LIST); | |
93 info->highlight = | |
94 network->IsConnectedState() || network->IsConnectingState(); | |
95 info->disable = | |
96 network->activation_state() == shill::kActivationStateActivating; | |
97 if (!animating && network->IsConnectingState()) | |
98 animating = true; | |
99 } | |
100 | |
101 if (animating) | |
102 network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this); | |
103 else | |
104 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this); | |
105 | |
106 // Get the updated list entries | |
107 network_map_.clear(); | |
108 std::set<std::string> new_service_paths; | |
109 bool needs_relayout = UpdateNetworkListEntries(&new_service_paths); | |
110 | |
111 // Remove old children | |
112 std::set<std::string> remove_service_paths; | |
113 for (ServicePathMap::const_iterator it = service_path_map_.begin(); | |
114 it != service_path_map_.end(); | |
115 ++it) { | |
116 if (new_service_paths.find(it->first) == new_service_paths.end()) { | |
117 remove_service_paths.insert(it->first); | |
118 network_map_.erase(it->second); | |
119 content_->RemoveChildView(it->second); | |
120 needs_relayout = true; | |
121 } | |
122 } | |
123 | |
124 for (std::set<std::string>::const_iterator remove_it = | |
125 remove_service_paths.begin(); | |
126 remove_it != remove_service_paths.end(); | |
127 ++remove_it) { | |
128 service_path_map_.erase(*remove_it); | |
129 } | |
130 | |
131 if (needs_relayout) { | |
132 views::View* selected_view = NULL; | |
133 for (ServicePathMap::const_iterator iter = service_path_map_.begin(); | |
134 iter != service_path_map_.end(); | |
135 ++iter) { | |
136 if (delegate_->IsViewHovered(iter->second)) { | |
137 selected_view = iter->second; | |
138 break; | |
139 } | |
140 } | |
141 content_->SizeToPreferredSize(); | |
142 delegate_->RelayoutScrollList(); | |
143 if (selected_view) | |
144 content_->ScrollRectToVisible(selected_view->bounds()); | |
145 } | |
146 } | |
147 | |
148 bool NetworkListView::UpdateNetworkListEntries( | |
149 std::set<std::string>* new_service_paths) { | |
150 bool needs_relayout = false; | |
151 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | |
152 | |
153 // Insert child views | |
154 int index = 0; | |
155 | |
156 // Highlighted networks | |
157 for (size_t i = 0; i < network_list_.size(); ++i) { | |
158 const NetworkInfo* info = network_list_[i]; | |
159 if (info->highlight) { | |
160 if (UpdateNetworkChild(index++, info)) | |
161 needs_relayout = true; | |
162 new_service_paths->insert(info->service_path); | |
163 } | |
164 } | |
165 | |
166 if (!delegate_->ShouldListOnlyVPN()) { | |
stevenjb
2014/07/24 19:13:54
I don't like embedding "OnlyVPN" in this class, it
sadrul
2014/07/24 20:38:45
Done.
| |
167 // Cellular initializing | |
168 int message_id = network_icon::GetCellularUninitializedMsg(); | |
169 if (!message_id && | |
170 handler->IsTechnologyEnabled(NetworkTypePattern::Mobile()) && | |
171 !handler->FirstNetworkByType(NetworkTypePattern::Mobile())) { | |
172 message_id = IDS_ASH_STATUS_TRAY_NO_CELLULAR_NETWORKS; | |
173 } | |
174 if (UpdateInfoLabel(message_id, index, &no_cellular_networks_view_)) | |
175 needs_relayout = true; | |
176 if (message_id) | |
177 ++index; | |
178 | |
179 // "Wifi Enabled / Disabled" | |
180 message_id = 0; | |
181 if (network_list_.empty()) { | |
182 message_id = handler->IsTechnologyEnabled(NetworkTypePattern::WiFi()) | |
183 ? IDS_ASH_STATUS_TRAY_NETWORK_WIFI_ENABLED | |
184 : IDS_ASH_STATUS_TRAY_NETWORK_WIFI_DISABLED; | |
185 } | |
186 if (UpdateInfoLabel(message_id, index, &no_wifi_networks_view_)) | |
187 needs_relayout = true; | |
188 if (message_id) | |
189 ++index; | |
190 | |
191 // "Wifi Scanning" | |
192 message_id = 0; | |
193 if (handler->GetScanningByType(NetworkTypePattern::WiFi())) | |
194 message_id = IDS_ASH_STATUS_TRAY_WIFI_SCANNING_MESSAGE; | |
195 if (UpdateInfoLabel(message_id, index, &scanning_view_)) | |
196 needs_relayout = true; | |
197 if (message_id) | |
198 ++index; | |
199 } | |
200 | |
201 // Un-highlighted networks | |
202 for (size_t i = 0; i < network_list_.size(); ++i) { | |
203 const NetworkInfo* info = network_list_[i]; | |
204 if (!info->highlight) { | |
205 if (UpdateNetworkChild(index++, info)) | |
206 needs_relayout = true; | |
207 new_service_paths->insert(info->service_path); | |
208 } | |
209 } | |
210 | |
211 // No networks or other messages (fallback) | |
212 if (index == 0) { | |
213 int message_id = 0; | |
214 if (delegate_->ShouldListOnlyVPN()) | |
stevenjb
2014/07/24 19:13:54
So, I think what we really want is delegate_->GetN
sadrul
2014/07/24 20:38:45
Very nice! Done. I like this, this is much cleaner
| |
215 message_id = IDS_ASH_STATUS_TRAY_NETWORK_NO_VPN; | |
216 else | |
217 message_id = IDS_ASH_STATUS_TRAY_NO_NETWORKS; | |
218 if (UpdateInfoLabel(message_id, index, &scanning_view_)) | |
219 needs_relayout = true; | |
220 } | |
221 | |
222 return needs_relayout; | |
223 } | |
224 | |
225 bool NetworkListView::UpdateNetworkChild(int index, const NetworkInfo* info) { | |
226 bool needs_relayout = false; | |
227 views::View* container = NULL; | |
228 ServicePathMap::const_iterator found = | |
229 service_path_map_.find(info->service_path); | |
230 if (found == service_path_map_.end()) { | |
231 container = delegate_->CreateViewForNetwork(*info); | |
232 content_->AddChildViewAt(container, index); | |
233 needs_relayout = true; | |
234 } else { | |
235 container = found->second; | |
236 container->RemoveAllChildViews(true); | |
237 delegate_->UpdateViewForNetwork(container, *info); | |
238 container->Layout(); | |
239 container->SchedulePaint(); | |
240 needs_relayout = PlaceViewAtIndex(container, index); | |
241 } | |
242 if (info->disable) | |
243 container->SetEnabled(false); | |
244 network_map_[container] = info->service_path; | |
245 service_path_map_[info->service_path] = container; | |
246 return needs_relayout; | |
247 } | |
248 | |
249 bool NetworkListView::PlaceViewAtIndex(views::View* view, int index) { | |
250 if (content_->child_at(index) == view) | |
251 return false; | |
252 content_->ReorderChildView(view, index); | |
253 return true; | |
254 } | |
255 | |
256 bool NetworkListView::UpdateInfoLabel(int message_id, | |
257 int index, | |
258 views::Label** label) { | |
259 CHECK(label); | |
260 bool needs_relayout = false; | |
261 if (message_id) { | |
262 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
263 base::string16 text = rb.GetLocalizedString(message_id); | |
264 if (!*label) { | |
265 *label = delegate_->CreateInfoLabel(); | |
266 (*label)->SetText(text); | |
267 content_->AddChildViewAt(*label, index); | |
268 needs_relayout = true; | |
269 } else { | |
270 (*label)->SetText(text); | |
271 needs_relayout = PlaceViewAtIndex(*label, index); | |
272 } | |
273 } else if (*label) { | |
274 content_->RemoveChildView(*label); | |
275 delete *label; | |
276 *label = NULL; | |
277 needs_relayout = true; | |
278 } | |
279 return needs_relayout; | |
280 } | |
281 | |
282 void NetworkListView::NetworkIconChanged() { | |
283 UpdateNetworkList(); | |
284 } | |
285 | |
286 } // namespace ui | |
OLD | NEW |