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