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/network_icon.h" | 5 #include "ash/system/chromeos/network/network_icon.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/system/chromeos/network/network_icon_animation.h" | 8 #include "ash/system/chromeos/network/network_icon_animation.h" |
9 #include "ash/system/chromeos/network/network_icon_animation_observer.h" | 9 #include "ash/system/chromeos/network/network_icon_animation_observer.h" |
10 #include "base/utf_string_conversions.h" | |
11 #include "chromeos/network/device_state.h" | 10 #include "chromeos/network/device_state.h" |
12 #include "chromeos/network/network_state.h" | 11 #include "chromeos/network/network_state.h" |
13 #include "chromeos/network/network_state_handler.h" | 12 #include "chromeos/network/network_state_handler.h" |
14 #include "grit/ash_resources.h" | 13 #include "grit/ash_resources.h" |
15 #include "grit/ash_strings.h" | |
16 #include "third_party/cros_system_api/dbus/service_constants.h" | 14 #include "third_party/cros_system_api/dbus/service_constants.h" |
17 #include "ui/base/l10n/l10n_util.h" | |
18 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
19 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
20 #include "ui/gfx/image/image_skia_operations.h" | 17 #include "ui/gfx/image/image_skia_operations.h" |
21 #include "ui/gfx/image/image_skia_source.h" | 18 #include "ui/gfx/image/image_skia_source.h" |
22 #include "ui/gfx/rect.h" | 19 #include "ui/gfx/rect.h" |
23 #include "ui/gfx/size_conversions.h" | 20 #include "ui/gfx/size_conversions.h" |
24 | 21 |
25 using chromeos::DeviceState; | 22 using chromeos::DeviceState; |
26 using chromeos::NetworkStateHandler; | 23 using chromeos::NetworkStateHandler; |
27 using chromeos::NetworkState; | 24 using chromeos::NetworkState; |
28 | 25 |
29 namespace ash { | 26 namespace ash { |
30 namespace network_icon { | 27 namespace network_icon { |
31 | 28 |
32 namespace { | 29 namespace { |
33 | 30 |
34 //------------------------------------------------------------------------------ | 31 //------------------------------------------------------------------------------ |
35 // Struct to pass icon badges to NetworkIconImageSource. | 32 // NetworkIconImpl class used for maintaining a map of network state and images. |
36 struct Badges { | |
37 Badges() | |
38 : top_left(NULL), | |
39 top_right(NULL), | |
40 bottom_left(NULL), | |
41 bottom_right(NULL) { | |
42 } | |
43 const gfx::ImageSkia* top_left; | |
44 const gfx::ImageSkia* top_right; | |
45 const gfx::ImageSkia* bottom_left; | |
46 const gfx::ImageSkia* bottom_right; | |
47 }; | |
48 | 33 |
49 //------------------------------------------------------------------------------ | |
50 // class used for maintaining a map of network state and images. | |
51 class NetworkIconImpl { | 34 class NetworkIconImpl { |
52 public: | 35 public: |
53 explicit NetworkIconImpl(IconType icon_type); | 36 NetworkIconImpl(const std::string& service_path, ResourceColorTheme color); |
54 | 37 |
55 // Determines whether or not the associated network might be dirty and if so | 38 // Determines whether or not the associated network might be dirty and if so |
56 // updates and generates the icon. Does nothing if network no longer exists. | 39 // updates and generates the icon. Does nothing if network no longer exists. |
57 void Update(const chromeos::NetworkState* network); | 40 void Update(const chromeos::NetworkState* network); |
58 | 41 |
59 const gfx::ImageSkia& image() const { return image_; } | 42 const gfx::ImageSkia& image() const { return image_; } |
60 | 43 |
61 private: | 44 private: |
62 // Updates |strength_index_| for wireless networks. Returns true if changed. | 45 // Updates |strength_index_| for wireless networks. Returns true if changed. |
63 bool UpdateWirelessStrengthIndex(const chromeos::NetworkState* network); | 46 bool UpdateWirelessStrengthIndex(const chromeos::NetworkState* network); |
64 | 47 |
65 // Updates the local state for cellular networks. Returns true if changed. | 48 // Updates the local state for cellular networks. Returns true if changed. |
66 bool UpdateCellularState(const chromeos::NetworkState* network); | 49 bool UpdateCellularState(const chromeos::NetworkState* network); |
67 | 50 |
68 // Updates the VPN badge. Returns true if changed. | |
69 bool UpdateVPNBadge(); | |
70 | |
71 // Gets |badges| based on |network| and the current state. | |
72 void GetBadges(const NetworkState* network, Badges* badges); | |
73 | |
74 // Gets the appropriate icon and badges and composites the image. | 51 // Gets the appropriate icon and badges and composites the image. |
75 void GenerateImage(const chromeos::NetworkState* network); | 52 void GenerateImage(const chromeos::NetworkState* network); |
76 | 53 |
77 // Defines color theme and VPN badging | 54 // Service path for the network this icon is associated with. |
78 const IconType icon_type_; | 55 std::string service_path_; |
| 56 |
| 57 // Color theme for the icon. |
| 58 ResourceColorTheme color_; |
79 | 59 |
80 // Cached state of the network when the icon was last generated. | 60 // Cached state of the network when the icon was last generated. |
81 std::string state_; | 61 std::string state_; |
82 | 62 |
83 // Cached strength index of the network when the icon was last generated. | 63 // Cached strength index of the network when the icon was last generated. |
84 int strength_index_; | 64 int strength_index_; |
85 | 65 |
86 // Cached technology badge for the network when the icon was last generated. | 66 // Cached technology badge for the network when the icon was last generated. |
87 const gfx::ImageSkia* technology_badge_; | 67 const gfx::ImageSkia* technology_badge_; |
88 | 68 |
89 // Cached vpn badge for the network when the icon was last generated. | |
90 const gfx::ImageSkia* vpn_badge_; | |
91 | |
92 // Cached roaming state of the network when the icon was last generated. | 69 // Cached roaming state of the network when the icon was last generated. |
93 std::string roaming_state_; | 70 std::string roaming_state_; |
94 | 71 |
95 // Generated icon image. | 72 // Generated icon image. |
96 gfx::ImageSkia image_; | 73 gfx::ImageSkia image_; |
97 | 74 |
98 DISALLOW_COPY_AND_ASSIGN(NetworkIconImpl); | 75 DISALLOW_COPY_AND_ASSIGN(NetworkIconImpl); |
99 }; | 76 }; |
100 | 77 |
101 //------------------------------------------------------------------------------ | 78 //------------------------------------------------------------------------------ |
102 // Maintain a static (global) icon map. Note: Icons are never destroyed; | 79 // Maintain a static (global) icon map. Note: Icons are never destroyed; |
103 // it is assumed that a finite and reasonable number of network icons will be | 80 // it is assumed that a finite and reasonable number of network icons will be |
104 // created during a session. | 81 // created during a session. |
105 | 82 |
106 typedef std::map<std::string, NetworkIconImpl*> NetworkIconMap; | 83 typedef std::map<std::string, NetworkIconImpl*> NetworkIconMap; |
107 | 84 |
108 NetworkIconMap* GetIconMap(IconType icon_type) { | 85 NetworkIconMap* GetIconMap(ResourceColorTheme color) { |
109 typedef std::map<IconType, NetworkIconMap*> IconTypeMap; | 86 if (color == COLOR_DARK) { |
110 static IconTypeMap* s_icon_map = NULL; | 87 static NetworkIconMap* s_icon_map_dark = NULL; |
111 if (s_icon_map == NULL) | 88 if (s_icon_map_dark == NULL) |
112 s_icon_map = new IconTypeMap; | 89 s_icon_map_dark = new NetworkIconMap; |
113 if (s_icon_map->count(icon_type) == 0) | 90 return s_icon_map_dark; |
114 (*s_icon_map)[icon_type] = new NetworkIconMap; | 91 } else { |
115 return (*s_icon_map)[icon_type]; | 92 static NetworkIconMap* s_icon_map_light = NULL; |
| 93 if (s_icon_map_light == NULL) |
| 94 s_icon_map_light = new NetworkIconMap; |
| 95 return s_icon_map_light; |
| 96 } |
116 } | 97 } |
117 | 98 |
118 //------------------------------------------------------------------------------ | 99 //------------------------------------------------------------------------------ |
119 // Utilities for generating icon images. | 100 // Utilities for generating icon images. |
120 | 101 |
121 // 'NONE' will default to ARCS behavior where appropriate (e.g. no network or | |
122 // if a new type gets added). | |
123 enum ImageType { | 102 enum ImageType { |
124 ARCS, | 103 ARCS = 0, |
125 BARS, | 104 BARS |
126 NONE | 105 }; |
| 106 |
| 107 struct Badges { |
| 108 Badges() |
| 109 : top_left(NULL), |
| 110 top_right(NULL), |
| 111 bottom_left(NULL), |
| 112 bottom_right(NULL) { |
| 113 } |
| 114 const gfx::ImageSkia* top_left; |
| 115 const gfx::ImageSkia* top_right; |
| 116 const gfx::ImageSkia* bottom_left; |
| 117 const gfx::ImageSkia* bottom_right; |
127 }; | 118 }; |
128 | 119 |
129 // Amount to fade icons while connecting. | 120 // Amount to fade icons while connecting. |
130 const double kConnectingImageAlpha = 0.5; | 121 const double kConnectingImageAlpha = 0.5; |
131 | 122 |
132 // Images for strength bars for wired networks. | 123 // Images for strength bars for wired networks. |
133 const int kNumBarsImages = 5; | 124 const int kNumBarsImages = 5; |
134 gfx::ImageSkia* kBarsImagesAnimatingDark[kNumBarsImages - 1]; | 125 gfx::ImageSkia* kBarsImagesAnimatingDark[kNumBarsImages - 1]; |
135 gfx::ImageSkia* kBarsImagesAnimatingLight[kNumBarsImages - 1]; | 126 gfx::ImageSkia* kBarsImagesAnimatingLight[kNumBarsImages - 1]; |
136 | 127 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 private: | 208 private: |
218 const gfx::ImageSkia icon_; | 209 const gfx::ImageSkia icon_; |
219 const Badges badges_; | 210 const Badges badges_; |
220 | 211 |
221 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource); | 212 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource); |
222 }; | 213 }; |
223 | 214 |
224 //------------------------------------------------------------------------------ | 215 //------------------------------------------------------------------------------ |
225 // Utilities for extracting icon images. | 216 // Utilities for extracting icon images. |
226 | 217 |
227 bool IconTypeIsDark(IconType icon_type) { | 218 int NumImagesForType(ImageType type) { |
228 return (icon_type != ICON_TYPE_TRAY); | 219 return (type == ARCS) ? kNumArcsImages : kNumBarsImages; |
229 } | 220 } |
230 | 221 |
231 bool IconTypeHasVPNBadge(IconType icon_type) { | 222 gfx::ImageSkia** ImageListForType(ImageType type, |
232 return (icon_type != ICON_TYPE_LIST); | 223 ResourceColorTheme color) { |
233 } | |
234 | |
235 int NumImagesForType(ImageType type) { | |
236 return (type == BARS) ? kNumBarsImages : kNumArcsImages; | |
237 } | |
238 | |
239 gfx::ImageSkia** ImageListForType(ImageType image_type, IconType icon_type) { | |
240 gfx::ImageSkia** images; | 224 gfx::ImageSkia** images; |
241 if (image_type == BARS) { | 225 if (type == ARCS) { |
242 images = IconTypeIsDark(icon_type) ? | 226 images = (color == COLOR_DARK) ? |
| 227 kArcsImagesAnimatingDark : kArcsImagesAnimatingLight; |
| 228 } else { |
| 229 images = (color == COLOR_DARK) ? |
243 kBarsImagesAnimatingDark : kBarsImagesAnimatingLight; | 230 kBarsImagesAnimatingDark : kBarsImagesAnimatingLight; |
244 } else { | |
245 images = IconTypeIsDark(icon_type) ? | |
246 kArcsImagesAnimatingDark : kArcsImagesAnimatingLight; | |
247 } | 231 } |
248 return images; | 232 return images; |
249 } | 233 } |
250 | 234 |
251 gfx::ImageSkia* BaseImageForType(ImageType image_type, IconType icon_type) { | 235 gfx::ImageSkia* BaseImageForType(ImageType type, |
| 236 ResourceColorTheme color) { |
252 gfx::ImageSkia* image; | 237 gfx::ImageSkia* image; |
253 if (image_type == BARS) { | 238 if (type == ARCS) { |
254 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 239 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
255 IconTypeIsDark(icon_type) ? | 240 color == COLOR_DARK ? |
| 241 IDR_AURA_UBER_TRAY_NETWORK_ARCS_DARK : |
| 242 IDR_AURA_UBER_TRAY_NETWORK_ARCS_LIGHT); |
| 243 } else { |
| 244 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 245 color == COLOR_DARK ? |
256 IDR_AURA_UBER_TRAY_NETWORK_BARS_DARK : | 246 IDR_AURA_UBER_TRAY_NETWORK_BARS_DARK : |
257 IDR_AURA_UBER_TRAY_NETWORK_BARS_LIGHT); | 247 IDR_AURA_UBER_TRAY_NETWORK_BARS_LIGHT); |
258 } else { | |
259 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
260 IconTypeIsDark(icon_type) ? | |
261 IDR_AURA_UBER_TRAY_NETWORK_ARCS_DARK : | |
262 IDR_AURA_UBER_TRAY_NETWORK_ARCS_LIGHT); | |
263 } | 248 } |
264 return image; | 249 return image; |
265 } | 250 } |
266 | 251 |
267 ImageType ImageTypeForNetworkType(const std::string& type) { | 252 gfx::ImageSkia GetImageForIndex(ImageType type, |
268 if (type == flimflam::kTypeWifi || type == flimflam::kTypeWimax) | 253 ResourceColorTheme color, |
269 return ARCS; | |
270 else if (type == flimflam::kTypeCellular) | |
271 return BARS; | |
272 return NONE; | |
273 } | |
274 | |
275 gfx::ImageSkia GetImageForIndex(ImageType image_type, | |
276 IconType icon_type, | |
277 int index) { | 254 int index) { |
278 int num_images = NumImagesForType(image_type); | 255 int num_images = NumImagesForType(type); |
279 if (index < 0 || index >= num_images) | 256 if (index < 0 || index >= num_images) |
280 return gfx::ImageSkia(); | 257 return gfx::ImageSkia(); |
281 gfx::ImageSkia* images = BaseImageForType(image_type, icon_type); | 258 gfx::ImageSkia* images = BaseImageForType(type, color); |
282 int width = images->width(); | 259 int width = images->width(); |
283 int height = images->height() / num_images; | 260 int height = images->height() / num_images; |
284 return gfx::ImageSkiaOperations::ExtractSubset(*images, | 261 return gfx::ImageSkiaOperations::ExtractSubset(*images, |
285 gfx::Rect(0, index * height, width, height)); | 262 gfx::Rect(0, index * height, width, height)); |
286 } | 263 } |
287 | 264 |
288 const gfx::ImageSkia GetDisconnectedImage(const std::string& type, | 265 const gfx::ImageSkia GetDisconnectedImage(ResourceColorTheme color) { |
289 IconType icon_type) { | 266 return GetImageForIndex(ARCS, color, 0); |
290 ImageType image_type = ImageTypeForNetworkType(type); | |
291 const int disconnected_index = 0; | |
292 return GetImageForIndex(image_type, icon_type, disconnected_index); | |
293 } | 267 } |
294 | 268 |
295 int StrengthIndex(int strength, int count) { | 269 int StrengthIndex(int strength, int count) { |
296 // Return an index in the range [1, count-1]. | 270 // Return an index in the range [1, count-1]. |
297 const float findex = (static_cast<float>(strength) / 100.0f) * | 271 const float findex = (static_cast<float>(strength) / 100.0f) * |
298 nextafter(static_cast<float>(count - 1), 0); | 272 nextafter(static_cast<float>(count - 1), 0); |
299 int index = 1 + static_cast<int>(findex); | 273 int index = 1 + static_cast<int>(findex); |
300 index = std::max(std::min(index, count - 1), 1); | 274 index = std::max(std::min(index, count - 1), 1); |
301 return index; | 275 return index; |
302 } | 276 } |
303 | 277 |
304 int GetStrengthIndex(const NetworkState* network) { | 278 int GetStrengthIndex(const NetworkState* network) { |
305 ImageType image_type = ImageTypeForNetworkType(network->type()); | 279 if (network->type() == flimflam::kTypeWifi) { |
306 if (image_type == ARCS) | |
307 return StrengthIndex(network->signal_strength(), kNumArcsImages); | 280 return StrengthIndex(network->signal_strength(), kNumArcsImages); |
308 else if (image_type == BARS) | 281 } else if (network->type() == flimflam::kTypeWimax) { |
309 return StrengthIndex(network->signal_strength(), kNumBarsImages); | 282 return StrengthIndex(network->signal_strength(), kNumBarsImages); |
| 283 } else if (network->type() == flimflam::kTypeCellular) { |
| 284 return StrengthIndex(network->signal_strength(), kNumBarsImages); |
| 285 } |
310 return 0; | 286 return 0; |
311 } | 287 } |
312 | 288 |
313 const gfx::ImageSkia* BadgeForNetworkTechnology(const NetworkState* network, | 289 const gfx::ImageSkia* BadgeForNetworkTechnology(const NetworkState* network, |
314 IconType icon_type) { | 290 ResourceColorTheme color) { |
315 const int kUnknownBadgeType = -1; | 291 const int kUnknownBadgeType = -1; |
316 int id = kUnknownBadgeType; | 292 int id = kUnknownBadgeType; |
317 if (network->technology() == flimflam::kNetworkTechnologyEvdo) { | 293 if (network->technology() == flimflam::kNetworkTechnologyEvdo) { |
318 id = IconTypeIsDark(icon_type) ? | 294 id = (color == COLOR_DARK) ? |
319 IDR_AURA_UBER_TRAY_NETWORK_3G_DARK : | 295 IDR_AURA_UBER_TRAY_NETWORK_3G_DARK : |
320 IDR_AURA_UBER_TRAY_NETWORK_3G_LIGHT; | 296 IDR_AURA_UBER_TRAY_NETWORK_3G_LIGHT; |
321 } else if (network->technology() == flimflam::kNetworkTechnology1Xrtt) { | 297 } else if (network->technology() == flimflam::kNetworkTechnology1Xrtt) { |
322 id = IDR_AURA_UBER_TRAY_NETWORK_1X; | 298 id = IDR_AURA_UBER_TRAY_NETWORK_1X; |
323 } else if (network->technology() == flimflam::kNetworkTechnologyGprs) { | 299 } else if (network->technology() == flimflam::kNetworkTechnologyGprs) { |
324 id = IconTypeIsDark(icon_type) ? | 300 id = (color == COLOR_DARK) ? |
325 IDR_AURA_UBER_TRAY_NETWORK_GPRS_DARK : | 301 IDR_AURA_UBER_TRAY_NETWORK_GPRS_DARK : |
326 IDR_AURA_UBER_TRAY_NETWORK_GPRS_LIGHT; | 302 IDR_AURA_UBER_TRAY_NETWORK_GPRS_LIGHT; |
327 } else if (network->technology() == flimflam::kNetworkTechnologyEdge) { | 303 } else if (network->technology() == flimflam::kNetworkTechnologyEdge) { |
328 id = IconTypeIsDark(icon_type) ? | 304 id = (color == COLOR_DARK) ? |
329 IDR_AURA_UBER_TRAY_NETWORK_EDGE_DARK : | 305 IDR_AURA_UBER_TRAY_NETWORK_EDGE_DARK : |
330 IDR_AURA_UBER_TRAY_NETWORK_EDGE_LIGHT; | 306 IDR_AURA_UBER_TRAY_NETWORK_EDGE_LIGHT; |
331 } else if (network->technology() == flimflam::kNetworkTechnologyUmts) { | 307 } else if (network->technology() == flimflam::kNetworkTechnologyUmts) { |
332 id = IconTypeIsDark(icon_type) ? | 308 id = (color == COLOR_DARK) ? |
333 IDR_AURA_UBER_TRAY_NETWORK_3G_DARK : | 309 IDR_AURA_UBER_TRAY_NETWORK_3G_DARK : |
334 IDR_AURA_UBER_TRAY_NETWORK_3G_LIGHT; | 310 IDR_AURA_UBER_TRAY_NETWORK_3G_LIGHT; |
335 } else if (network->technology() == flimflam::kNetworkTechnologyHspa) { | 311 } else if (network->technology() == flimflam::kNetworkTechnologyHspa) { |
336 id = IconTypeIsDark(icon_type) ? | 312 id = (color == COLOR_DARK) ? |
337 IDR_AURA_UBER_TRAY_NETWORK_HSPA_DARK : | 313 IDR_AURA_UBER_TRAY_NETWORK_HSPA_DARK : |
338 IDR_AURA_UBER_TRAY_NETWORK_HSPA_LIGHT; | 314 IDR_AURA_UBER_TRAY_NETWORK_HSPA_LIGHT; |
339 } else if (network->technology() == flimflam::kNetworkTechnologyHspaPlus) { | 315 } else if (network->technology() == flimflam::kNetworkTechnologyHspaPlus) { |
340 id = IconTypeIsDark(icon_type) ? | 316 id = (color == COLOR_DARK) ? |
341 IDR_AURA_UBER_TRAY_NETWORK_HSPA_PLUS_DARK : | 317 IDR_AURA_UBER_TRAY_NETWORK_HSPA_PLUS_DARK : |
342 IDR_AURA_UBER_TRAY_NETWORK_HSPA_PLUS_LIGHT; | 318 IDR_AURA_UBER_TRAY_NETWORK_HSPA_PLUS_LIGHT; |
343 } else if (network->technology() == flimflam::kNetworkTechnologyLte) { | 319 } else if (network->technology() == flimflam::kNetworkTechnologyLte) { |
344 id = IconTypeIsDark(icon_type) ? | 320 id = (color == COLOR_DARK) ? |
345 IDR_AURA_UBER_TRAY_NETWORK_LTE_DARK : | 321 IDR_AURA_UBER_TRAY_NETWORK_LTE_DARK : |
346 IDR_AURA_UBER_TRAY_NETWORK_LTE_LIGHT; | 322 IDR_AURA_UBER_TRAY_NETWORK_LTE_LIGHT; |
347 } else if (network->technology() == flimflam::kNetworkTechnologyLteAdvanced) { | 323 } else if (network->technology() == flimflam::kNetworkTechnologyLteAdvanced) { |
348 id = IconTypeIsDark(icon_type) ? | 324 id = (color == COLOR_DARK) ? |
349 IDR_AURA_UBER_TRAY_NETWORK_LTE_ADVANCED_DARK : | 325 IDR_AURA_UBER_TRAY_NETWORK_LTE_ADVANCED_DARK : |
350 IDR_AURA_UBER_TRAY_NETWORK_LTE_ADVANCED_LIGHT; | 326 IDR_AURA_UBER_TRAY_NETWORK_LTE_ADVANCED_LIGHT; |
351 } else if (network->technology() == flimflam::kNetworkTechnologyGsm) { | 327 } else if (network->technology() == flimflam::kNetworkTechnologyGsm) { |
352 id = IconTypeIsDark(icon_type) ? | 328 id = (color == COLOR_DARK) ? |
353 IDR_AURA_UBER_TRAY_NETWORK_GPRS_DARK : | 329 IDR_AURA_UBER_TRAY_NETWORK_GPRS_DARK : |
354 IDR_AURA_UBER_TRAY_NETWORK_GPRS_LIGHT; | 330 IDR_AURA_UBER_TRAY_NETWORK_GPRS_LIGHT; |
355 } | 331 } |
356 if (id == kUnknownBadgeType) | 332 if (id == kUnknownBadgeType) |
357 return NULL; | 333 return NULL; |
358 else | 334 else |
359 return ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(id); | 335 return ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(id); |
360 } | 336 } |
361 | 337 |
362 const gfx::ImageSkia* BadgeForVPN(IconType icon_type) { | |
363 return ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
364 IDR_AURA_UBER_TRAY_NETWORK_VPN_BADGE); | |
365 } | |
366 | |
367 gfx::ImageSkia GetIcon(const NetworkState* network, | 338 gfx::ImageSkia GetIcon(const NetworkState* network, |
368 IconType icon_type, | 339 ResourceColorTheme color, |
369 int strength_index) { | 340 int strength_index) { |
370 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 341 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
371 const std::string& type = network->type(); | 342 const std::string& type = network->type(); |
372 if (type == flimflam::kTypeEthernet) { | 343 if (type == flimflam::kTypeEthernet) { |
373 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); | 344 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); |
374 } else if (type == flimflam::kTypeWifi || | 345 } else if (type == flimflam::kTypeWifi) { |
375 type == flimflam::kTypeWimax || | |
376 type == flimflam::kTypeCellular) { | |
377 DCHECK(strength_index > 0); | 346 DCHECK(strength_index > 0); |
378 return GetImageForIndex( | 347 return GetImageForIndex(ARCS, color, strength_index); |
379 ImageTypeForNetworkType(type), icon_type, strength_index); | 348 } else if (type == flimflam::kTypeWimax) { |
| 349 DCHECK(strength_index > 0); |
| 350 return GetImageForIndex(BARS, color, strength_index); |
| 351 } else if (type == flimflam::kTypeCellular) { |
| 352 DCHECK(strength_index > 0); |
| 353 return GetImageForIndex(BARS, color, strength_index); |
380 } else if (type == flimflam::kTypeVPN) { | 354 } else if (type == flimflam::kTypeVPN) { |
381 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN); | 355 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN); |
382 } else { | 356 } else { |
383 LOG(WARNING) << "Request for icon for unsupported type: " << type; | 357 LOG(WARNING) << "Request for icon for unsupported type: " << type; |
384 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); | 358 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); |
385 } | 359 } |
386 } | 360 } |
387 | 361 |
| 362 void GetBadges(const NetworkState* network, |
| 363 ResourceColorTheme color, |
| 364 Badges* badges) { |
| 365 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 366 chromeos::NetworkStateHandler* handler = chromeos::NetworkStateHandler::Get(); |
| 367 |
| 368 bool use_dark_icons = color == COLOR_DARK; |
| 369 const std::string& type = network->type(); |
| 370 if (type == flimflam::kTypeWifi) { |
| 371 if (network->security() != flimflam::kSecurityNone && use_dark_icons) { |
| 372 badges->bottom_right = rb.GetImageSkiaNamed( |
| 373 IDR_AURA_UBER_TRAY_NETWORK_SECURE_DARK); |
| 374 } |
| 375 } else if (type == flimflam::kTypeWimax) { |
| 376 badges->top_left = rb.GetImageSkiaNamed( |
| 377 use_dark_icons ? |
| 378 IDR_AURA_UBER_TRAY_NETWORK_4G_DARK : |
| 379 IDR_AURA_UBER_TRAY_NETWORK_4G_LIGHT); |
| 380 } else if (type == flimflam::kTypeCellular) { |
| 381 if (network->roaming() == flimflam::kRoamingStateRoaming) { |
| 382 // For networks that are always in roaming don't show roaming badge. |
| 383 const DeviceState* device = |
| 384 handler->GetDeviceState(network->device_path()); |
| 385 if (!device->provider_requires_roaming()) { |
| 386 badges->bottom_right = rb.GetImageSkiaNamed( |
| 387 use_dark_icons ? |
| 388 IDR_AURA_UBER_TRAY_NETWORK_ROAMING_DARK : |
| 389 IDR_AURA_UBER_TRAY_NETWORK_ROAMING_LIGHT); |
| 390 } |
| 391 } |
| 392 if (!network->IsConnectingState()) { |
| 393 badges->top_left = BadgeForNetworkTechnology(network, color); |
| 394 } |
| 395 } |
| 396 } |
| 397 |
388 //------------------------------------------------------------------------------ | 398 //------------------------------------------------------------------------------ |
389 // Get connecting images | 399 // Get connecting images |
390 | 400 |
391 gfx::ImageSkia GetConnectingImage(const std::string& type, IconType icon_type) { | 401 gfx::ImageSkia GetConnectingImage(const std::string& type, |
392 ImageType image_type = ImageTypeForNetworkType(type); | 402 ResourceColorTheme color) { |
| 403 ImageType image_type = (type == flimflam::kTypeWifi) ? ARCS : BARS; |
393 int image_count = NumImagesForType(image_type) - 1; | 404 int image_count = NumImagesForType(image_type) - 1; |
394 gfx::ImageSkia** images = ImageListForType(image_type, icon_type); | 405 gfx::ImageSkia** images = ImageListForType(image_type, color); |
395 double animation = NetworkIconAnimation::GetInstance()->GetAnimation(); | 406 double animation = NetworkIconAnimation::GetInstance()->GetAnimation(); |
396 int index = animation * nextafter(static_cast<float>(image_count), 0); | 407 int index = animation * nextafter(static_cast<float>(image_count), 0); |
397 index = std::max(std::min(index, image_count - 1), 0); | 408 index = std::max(std::min(index, image_count - 1), 0); |
398 | 409 |
399 // Lazily cache images. | 410 // Lazily cache images. |
400 if (!images[index]) { | 411 if (!images[index]) { |
401 gfx::ImageSkia source = GetImageForIndex(image_type, icon_type, index + 1); | 412 gfx::ImageSkia source = GetImageForIndex(image_type, color, index + 1); |
402 images[index] = new gfx::ImageSkia( | 413 images[index] = new gfx::ImageSkia( |
403 gfx::ImageSkiaOperations::CreateBlendedImage( | 414 gfx::ImageSkiaOperations::CreateBlendedImage( |
404 gfx::ImageSkia(new EmptyImageSource(source.size()), source.size()), | 415 gfx::ImageSkia(new EmptyImageSource(source.size()), source.size()), |
405 source, | 416 source, |
406 kConnectingImageAlpha)); | 417 kConnectingImageAlpha)); |
407 } | 418 } |
408 gfx::ImageSkia& icon = *images[index]; | 419 gfx::ImageSkia& icon = *images[index]; |
409 return gfx::ImageSkia( | 420 return gfx::ImageSkia( |
410 new NetworkIconImageSource(icon, Badges()), icon.size()); | 421 new NetworkIconImageSource(icon, Badges()), icon.size()); |
411 } | 422 } |
412 | 423 |
413 } // namespace | 424 } // namespace |
414 | 425 |
415 //------------------------------------------------------------------------------ | 426 //------------------------------------------------------------------------------ |
416 // NetworkIconImpl | 427 // NetworkIconImpl |
417 | 428 |
418 NetworkIconImpl::NetworkIconImpl(IconType icon_type) | 429 NetworkIconImpl::NetworkIconImpl(const std::string& service_path, |
419 : icon_type_(icon_type), | 430 ResourceColorTheme color) |
| 431 : service_path_(service_path), |
| 432 color_(color), |
420 strength_index_(-1), | 433 strength_index_(-1), |
421 technology_badge_(NULL), | 434 technology_badge_(NULL) { |
422 vpn_badge_(NULL) { | |
423 // Default image | 435 // Default image |
424 image_ = GetDisconnectedImage(flimflam::kTypeWifi, icon_type); | 436 image_ = GetDisconnectedImage(color); |
425 } | 437 } |
426 | 438 |
427 void NetworkIconImpl::Update(const NetworkState* network) { | 439 void NetworkIconImpl::Update(const NetworkState* network) { |
428 DCHECK(network); | |
429 // Determine whether or not we need to update the icon. | 440 // Determine whether or not we need to update the icon. |
430 bool dirty = image_.isNull(); | 441 bool dirty = image_.isNull(); |
431 | 442 |
432 // If the network state has changed, the icon needs updating. | 443 // If the network state has changed, the icon needs updating. |
433 if (state_ != network->connection_state()) { | 444 if (state_ != network->connection_state()) { |
434 state_ = network->connection_state(); | 445 state_ = network->connection_state(); |
435 dirty = true; | 446 dirty = true; |
436 } | 447 } |
437 | 448 |
438 const std::string& type = network->type(); | 449 const std::string& type = network->type(); |
439 if (type != flimflam::kTypeEthernet) | 450 if (type != flimflam::kTypeEthernet) |
440 dirty |= UpdateWirelessStrengthIndex(network); | 451 dirty |= UpdateWirelessStrengthIndex(network); |
441 | 452 |
442 if (type == flimflam::kTypeCellular) | 453 if (type == flimflam::kTypeCellular) |
443 dirty |= UpdateCellularState(network); | 454 dirty |= UpdateCellularState(network); |
444 | 455 |
445 if (IconTypeHasVPNBadge(icon_type_) && type != flimflam::kTypeVPN) | |
446 dirty |= UpdateVPNBadge(); | |
447 | |
448 if (dirty) { | 456 if (dirty) { |
449 // Set the icon and badges based on the network and generate the image. | 457 // Set the icon and badges based on the network and generate the image. |
450 GenerateImage(network); | 458 GenerateImage(network); |
451 } | 459 } |
452 } | 460 } |
453 | 461 |
454 bool NetworkIconImpl::UpdateWirelessStrengthIndex(const NetworkState* network) { | 462 bool NetworkIconImpl::UpdateWirelessStrengthIndex(const NetworkState* network) { |
455 int index = GetStrengthIndex(network); | 463 int index = GetStrengthIndex(network); |
456 if (index != strength_index_) { | 464 if (index != strength_index_) { |
457 strength_index_ = index; | 465 strength_index_ = index; |
458 return true; | 466 return true; |
459 } | 467 } |
460 return false; | 468 return false; |
461 } | 469 } |
462 | 470 |
463 bool NetworkIconImpl::UpdateCellularState(const NetworkState* network) { | 471 bool NetworkIconImpl::UpdateCellularState(const NetworkState* network) { |
464 bool dirty = false; | 472 bool dirty = false; |
465 const gfx::ImageSkia* technology_badge = | 473 const gfx::ImageSkia* technology_badge = |
466 BadgeForNetworkTechnology(network, icon_type_); | 474 BadgeForNetworkTechnology(network, color_); |
467 if (technology_badge != technology_badge_) { | 475 if (technology_badge != technology_badge_) { |
468 technology_badge_ = technology_badge; | 476 technology_badge_ = technology_badge; |
469 dirty = true; | 477 dirty = true; |
470 } | 478 } |
471 std::string roaming_state = network->roaming(); | 479 std::string roaming_state = network->roaming(); |
472 if (roaming_state != roaming_state_) { | 480 if (roaming_state != roaming_state_) { |
473 roaming_state_ = roaming_state; | 481 roaming_state_ = roaming_state; |
474 dirty = true; | 482 dirty = true; |
475 } | 483 } |
476 return dirty; | 484 return dirty; |
477 } | 485 } |
478 | 486 |
479 bool NetworkIconImpl::UpdateVPNBadge() { | |
480 const NetworkState* vpn = | |
481 chromeos::NetworkStateHandler::Get()->ConnectedNetworkByType( | |
482 flimflam::kTypeVPN); | |
483 if (vpn && vpn_badge_ == NULL) { | |
484 vpn_badge_ = BadgeForVPN(icon_type_); | |
485 return true; | |
486 } else if (!vpn && vpn_badge_ != NULL) { | |
487 vpn_badge_ = NULL; | |
488 return true; | |
489 } | |
490 return false; | |
491 } | |
492 | |
493 void NetworkIconImpl::GetBadges(const NetworkState* network, Badges* badges) { | |
494 DCHECK(network); | |
495 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
496 chromeos::NetworkStateHandler* handler = chromeos::NetworkStateHandler::Get(); | |
497 | |
498 const std::string& type = network->type(); | |
499 if (type == flimflam::kTypeWifi) { | |
500 if (network->security() != flimflam::kSecurityNone && | |
501 IconTypeIsDark(icon_type_)) { | |
502 badges->bottom_right = rb.GetImageSkiaNamed( | |
503 IDR_AURA_UBER_TRAY_NETWORK_SECURE_DARK); | |
504 } | |
505 } else if (type == flimflam::kTypeWimax) { | |
506 badges->top_left = rb.GetImageSkiaNamed( | |
507 IconTypeIsDark(icon_type_) ? | |
508 IDR_AURA_UBER_TRAY_NETWORK_4G_DARK : | |
509 IDR_AURA_UBER_TRAY_NETWORK_4G_LIGHT); | |
510 } else if (type == flimflam::kTypeCellular) { | |
511 if (network->roaming() == flimflam::kRoamingStateRoaming) { | |
512 // For networks that are always in roaming don't show roaming badge. | |
513 const DeviceState* device = | |
514 handler->GetDeviceState(network->device_path()); | |
515 DCHECK(device); | |
516 if (!device->provider_requires_roaming()) { | |
517 badges->bottom_right = rb.GetImageSkiaNamed( | |
518 IconTypeIsDark(icon_type_) ? | |
519 IDR_AURA_UBER_TRAY_NETWORK_ROAMING_DARK : | |
520 IDR_AURA_UBER_TRAY_NETWORK_ROAMING_LIGHT); | |
521 } | |
522 } | |
523 } | |
524 if (!network->IsConnectingState()) { | |
525 badges->top_left = technology_badge_; | |
526 badges->bottom_left = vpn_badge_; | |
527 } | |
528 } | |
529 | |
530 void NetworkIconImpl::GenerateImage(const NetworkState* network) { | 487 void NetworkIconImpl::GenerateImage(const NetworkState* network) { |
531 DCHECK(network); | 488 gfx::ImageSkia icon = GetIcon(network, color_, strength_index_); |
532 gfx::ImageSkia icon = GetIcon(network, icon_type_, strength_index_); | |
533 Badges badges; | 489 Badges badges; |
534 GetBadges(network, &badges); | 490 GetBadges(network, color_, &badges); |
535 image_ = gfx::ImageSkia( | 491 image_ = gfx::ImageSkia( |
536 new NetworkIconImageSource(icon, badges), icon.size()); | 492 new NetworkIconImageSource(icon, badges), icon.size()); |
537 } | 493 } |
538 | 494 |
539 //------------------------------------------------------------------------------ | 495 //------------------------------------------------------------------------------ |
540 // Public interface | 496 // Public interface |
541 | 497 |
542 gfx::ImageSkia GetImageForNetwork(const NetworkState* network, | 498 gfx::ImageSkia GetImageForNetwork(const NetworkState* network, |
543 IconType icon_type) { | 499 ResourceColorTheme color, |
544 DCHECK(network); | 500 AnimationObserver* observer) { |
545 // Handle connecting icons. | |
546 if (network->IsConnectingState()) { | 501 if (network->IsConnectingState()) { |
547 NetworkIconAnimation::GetInstance()->AddNetwork(network->path()); | 502 if (observer) |
548 return GetConnectingImage(network->type(), icon_type); | 503 NetworkIconAnimation::GetInstance()->AddObserver(observer); |
| 504 return GetConnectingImage(network->type(), color); |
549 } | 505 } |
550 NetworkIconAnimation::GetInstance()->RemoveNetwork(network->path()); | 506 // Not connecting, remove observer. |
| 507 if (observer) |
| 508 NetworkIconAnimation::GetInstance()->RemoveObserver(observer); |
| 509 |
| 510 NetworkIconMap* icon_map = GetIconMap(color); |
551 | 511 |
552 // Find or add the icon. | 512 // Find or add the icon. |
553 NetworkIconMap* icon_map = GetIconMap(icon_type); | |
554 NetworkIconImpl* icon; | 513 NetworkIconImpl* icon; |
555 NetworkIconMap::iterator iter = icon_map->find(network->path()); | 514 NetworkIconMap::iterator iter = icon_map->find(network->path()); |
556 if (iter == icon_map->end()) { | 515 if (iter == icon_map->end()) { |
557 icon = new NetworkIconImpl(icon_type); | 516 icon = new NetworkIconImpl(network->path(), color); |
558 icon_map->insert(std::make_pair(network->path(), icon)); | 517 icon_map->insert(std::make_pair(network->path(), icon)); |
559 } else { | 518 } else { |
560 icon = iter->second; | 519 icon = iter->second; |
561 } | 520 } |
562 | 521 |
563 // Update and return the icon's image. | 522 // Update and return the icon's image. |
564 icon->Update(network); | 523 icon->Update(network); |
565 return icon->image(); | 524 return icon->image(); |
566 } | 525 } |
567 | 526 |
568 gfx::ImageSkia GetImageForConnectingNetwork(IconType icon_type, | |
569 const std::string& network_type) { | |
570 return GetConnectingImage(network_type, icon_type); | |
571 } | |
572 | |
573 gfx::ImageSkia GetImageForDisconnectedNetwork(IconType icon_type, | |
574 const std::string& network_type) { | |
575 return GetDisconnectedImage(network_type, icon_type); | |
576 } | |
577 | |
578 string16 GetLabelForNetwork(const chromeos::NetworkState* network, | |
579 IconType icon_type) { | |
580 DCHECK(network); | |
581 if (icon_type == ICON_TYPE_LIST) { | |
582 if (network->IsConnectingState()) { | |
583 return l10n_util::GetStringFUTF16( | |
584 IDS_ASH_STATUS_TRAY_NETWORK_LIST_CONNECTING, | |
585 UTF8ToUTF16(network->name())); | |
586 } | |
587 } else { | |
588 if (network->IsConnectedState()) { | |
589 return l10n_util::GetStringFUTF16( | |
590 IDS_ASH_STATUS_TRAY_NETWORK_CONNECTED, UTF8ToUTF16(network->name())); | |
591 } | |
592 if (network->IsConnectingState()) { | |
593 return l10n_util::GetStringFUTF16( | |
594 IDS_ASH_STATUS_TRAY_NETWORK_CONNECTING, UTF8ToUTF16(network->name())); | |
595 } | |
596 } | |
597 return UTF8ToUTF16(network->name()); | |
598 } | |
599 | |
600 } // namespace network_icon | 527 } // namespace network_icon |
601 } // namespace ash | 528 } // namespace ash |
OLD | NEW |