network_icon.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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
  3. // found in the LICENSE file.
  4. #include "ash/system/network/network_icon.h"
  5. #include <tuple>
  6. #include <utility>
  7. #include "ash/constants/ash_features.h"
  8. #include "ash/public/cpp/network_icon_image_source.h"
  9. #include "ash/resources/vector_icons/vector_icons.h"
  10. #include "ash/strings/grit/ash_strings.h"
  11. #include "ash/style/ash_color_provider.h"
  12. #include "ash/style/dark_light_mode_controller_impl.h"
  13. #include "ash/system/network/network_icon_animation.h"
  14. #include "ash/system/network/network_icon_animation_observer.h"
  15. #include "ash/system/tray/tray_constants.h"
  16. #include "base/containers/flat_map.h"
  17. #include "base/cxx17_backports.h"
  18. #include "base/memory/ptr_util.h"
  19. #include "base/strings/utf_string_conversions.h"
  20. #include "chromeos/services/network_config/public/cpp/cros_network_config_util.h"
  21. #include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
  22. #include "components/onc/onc_constants.h"
  23. #include "components/vector_icons/vector_icons.h"
  24. #include "ui/base/l10n/l10n_util.h"
  25. #include "ui/gfx/geometry/skia_conversions.h"
  26. #include "ui/gfx/image/image_skia.h"
  27. #include "ui/gfx/image/image_skia_operations.h"
  28. #include "ui/gfx/image/image_skia_source.h"
  29. #include "ui/gfx/paint_vector_icon.h"
  30. #include "ui/gfx/vector_icon_types.h"
  31. using chromeos::network_config::mojom::ActivationStateType;
  32. using chromeos::network_config::mojom::ConnectionStateType;
  33. using chromeos::network_config::mojom::NetworkStateProperties;
  34. using chromeos::network_config::mojom::NetworkType;
  35. using chromeos::network_config::mojom::SecurityType;
  36. namespace ash {
  37. namespace network_icon {
  38. namespace {
  39. // class used for maintaining a map of network state and images.
  40. class NetworkIconImpl {
  41. public:
  42. NetworkIconImpl(const std::string& guid,
  43. IconType icon_type,
  44. NetworkType network_type);
  45. NetworkIconImpl(const NetworkIconImpl&) = delete;
  46. NetworkIconImpl& operator=(const NetworkIconImpl&) = delete;
  47. // Determines whether or not the associated network might be dirty and if so
  48. // updates and generates the icon. Does nothing if network no longer exists.
  49. void Update(const NetworkStateProperties* network, bool show_vpn_badge);
  50. const gfx::ImageSkia& image() const { return image_; }
  51. private:
  52. // Updates |strength_index_| for wireless networks. Returns true if changed.
  53. bool UpdateWirelessStrengthIndex(const NetworkStateProperties* network);
  54. // Updates the local state for cellular networks. Returns true if changed.
  55. bool UpdateCellularState(const NetworkStateProperties* network);
  56. // Gets |badges| based on |network| and the current state.
  57. void GetBadges(const NetworkStateProperties* network, Badges* badges);
  58. // Gets the appropriate icon and badges and composites the image.
  59. void GenerateImage(const NetworkStateProperties* network);
  60. // Defines color theme and VPN badging
  61. const IconType icon_type_;
  62. // Cached state of the network when the icon was last generated.
  63. ConnectionStateType connection_state_ = ConnectionStateType::kNotConnected;
  64. int strength_index_ = -1;
  65. Badge technology_badge_ = {};
  66. bool show_vpn_badge_ = false;
  67. bool is_roaming_ = false;
  68. bool is_dark_themed_ = false;
  69. // Generated icon image.
  70. gfx::ImageSkia image_;
  71. };
  72. //------------------------------------------------------------------------------
  73. // Maintain a static (global) icon map. Note: Icons are never destroyed;
  74. // it is assumed that a finite and reasonable number of network icons will be
  75. // created during a session.
  76. typedef std::map<std::string, NetworkIconImpl*> NetworkIconMap;
  77. NetworkIconMap* GetIconMapInstance(IconType icon_type, bool create) {
  78. typedef std::map<IconType, NetworkIconMap*> IconTypeMap;
  79. static IconTypeMap* s_icon_map = nullptr;
  80. if (s_icon_map == nullptr) {
  81. if (!create)
  82. return nullptr;
  83. s_icon_map = new IconTypeMap;
  84. }
  85. if (s_icon_map->count(icon_type) == 0) {
  86. if (!create)
  87. return nullptr;
  88. (*s_icon_map)[icon_type] = new NetworkIconMap;
  89. }
  90. return (*s_icon_map)[icon_type];
  91. }
  92. NetworkIconMap* GetIconMap(IconType icon_type) {
  93. return GetIconMapInstance(icon_type, true);
  94. }
  95. void PurgeIconMap(IconType icon_type,
  96. const std::set<std::string>& network_guids) {
  97. NetworkIconMap* icon_map = GetIconMapInstance(icon_type, false);
  98. if (!icon_map)
  99. return;
  100. for (NetworkIconMap::iterator loop_iter = icon_map->begin();
  101. loop_iter != icon_map->end();) {
  102. NetworkIconMap::iterator cur_iter = loop_iter++;
  103. if (network_guids.count(cur_iter->first) == 0) {
  104. delete cur_iter->second;
  105. icon_map->erase(cur_iter);
  106. }
  107. }
  108. }
  109. //------------------------------------------------------------------------------
  110. // Utilities for generating icon images.
  111. // Amount to fade icons while connecting.
  112. const double kConnectingImageAlpha = 0.5;
  113. // Number of discrete images to use for alpha fade animation
  114. const int kNumFadeImages = 10;
  115. bool IsTrayIcon(IconType icon_type) {
  116. return icon_type == ICON_TYPE_TRAY_REGULAR ||
  117. icon_type == ICON_TYPE_TRAY_OOBE;
  118. }
  119. bool IconTypeHasVPNBadge(IconType icon_type) {
  120. return (icon_type != ICON_TYPE_LIST && icon_type != ICON_TYPE_MENU_LIST);
  121. }
  122. gfx::ImageSkia CreateNetworkIconImage(const gfx::ImageSkia& icon,
  123. const Badges& badges) {
  124. return gfx::CanvasImageSource::MakeImageSkia<NetworkIconImageSource>(
  125. icon.size(), icon, badges);
  126. }
  127. //------------------------------------------------------------------------------
  128. // Utilities for extracting icon images.
  129. ImageType ImageTypeForNetworkType(NetworkType network_type) {
  130. if (network_type == NetworkType::kWiFi)
  131. return ARCS;
  132. if (network_type == NetworkType::kCellular ||
  133. network_type == NetworkType::kTether) {
  134. return BARS;
  135. }
  136. return NONE;
  137. }
  138. gfx::ImageSkia GetImageForIndex(ImageType image_type,
  139. IconType icon_type,
  140. int index) {
  141. return gfx::CanvasImageSource::MakeImageSkia<SignalStrengthImageSource>(
  142. image_type, GetDefaultColorForIconType(icon_type),
  143. gfx::Size(kUnifiedTrayIconSize, kUnifiedTrayIconSize), index,
  144. kUnifiedTrayNetworkIconPadding);
  145. }
  146. gfx::ImageSkia& ConnectingWirelessImage(ImageType image_type,
  147. IconType icon_type,
  148. double animation) {
  149. // Connecting icons animate by adjusting their signal strength up and down,
  150. // but the empty (no signal) image is skipped for aesthetic reasons.
  151. static const int kNumConnectingImages = kNumNetworkImages - 1;
  152. // Cache of images used to avoid redrawing the icon during every animation;
  153. // the key is a tuple including a bool representing whether the icon displays
  154. // bars (as oppose to arcs), the IconType, and an int representing the index
  155. // of the image (with respect to GetImageForIndex()).
  156. static base::flat_map<std::tuple<bool, IconType, int>, gfx::ImageSkia>
  157. s_image_cache;
  158. // Note that if |image_type| is NONE, arcs are displayed by default.
  159. bool is_bars_image = image_type == BARS;
  160. int index =
  161. animation * nextafter(static_cast<float>(kNumConnectingImages), 0);
  162. index = base::clamp(index, 0, kNumConnectingImages - 1);
  163. auto map_key = std::make_tuple(is_bars_image, icon_type, index);
  164. if (!s_image_cache.contains(map_key)) {
  165. // Lazily cache images.
  166. // TODO(estade): should the alpha be applied in SignalStrengthImageSource?
  167. gfx::ImageSkia source = GetImageForIndex(image_type, icon_type, index + 1);
  168. s_image_cache[map_key] =
  169. gfx::ImageSkia(gfx::ImageSkiaOperations::CreateTransparentImage(
  170. source, kConnectingImageAlpha));
  171. }
  172. return s_image_cache[map_key];
  173. }
  174. gfx::ImageSkia ConnectingVpnImage(double animation) {
  175. float floored_animation_value =
  176. std::floor(animation * kNumFadeImages) / kNumFadeImages;
  177. const SkColor icon_color = AshColorProvider::Get()->GetContentLayerColor(
  178. AshColorProvider::ContentLayerType::kIconColorPrimary);
  179. return gfx::CreateVectorIcon(
  180. kNetworkVpnIcon,
  181. gfx::Tween::ColorValueBetween(
  182. floored_animation_value,
  183. SkColorSetA(icon_color, kConnectingImageAlpha), icon_color));
  184. }
  185. int StrengthIndex(int strength) {
  186. if (strength <= 0)
  187. return 0;
  188. if (strength >= 100)
  189. return kNumNetworkImages - 1;
  190. // Return an index in the range [1, kNumNetworkImages - 1].
  191. // This logic is equivalent to network_icon.js:strengthToIndex_().
  192. int zero_based_index = (strength - 1) * (kNumNetworkImages - 1) / 100;
  193. return zero_based_index + 1;
  194. }
  195. Badge BadgeForNetworkTechnology(const NetworkStateProperties* network,
  196. IconType icon_type) {
  197. DCHECK(network->type == NetworkType::kCellular);
  198. Badge badge = {nullptr, GetDefaultColorForIconType(icon_type)};
  199. const std::string& technology =
  200. network->type_state->get_cellular()->network_technology;
  201. if (technology == onc::cellular::kTechnologyEvdo) {
  202. badge.icon = &kNetworkBadgeTechnologyEvdoIcon;
  203. } else if (technology == onc::cellular::kTechnologyCdma1Xrtt) {
  204. badge.icon = &kNetworkBadgeTechnology1xIcon;
  205. } else if (technology == onc::cellular::kTechnologyGprs ||
  206. technology == onc::cellular::kTechnologyGsm) {
  207. badge.icon = &kNetworkBadgeTechnologyGprsIcon;
  208. } else if (technology == onc::cellular::kTechnologyEdge) {
  209. badge.icon = &kNetworkBadgeTechnologyEdgeIcon;
  210. } else if (technology == onc::cellular::kTechnologyUmts) {
  211. badge.icon = &kNetworkBadgeTechnology3gIcon;
  212. } else if (technology == onc::cellular::kTechnologyHspa) {
  213. badge.icon = &kNetworkBadgeTechnologyHspaIcon;
  214. } else if (technology == onc::cellular::kTechnologyHspaPlus) {
  215. badge.icon = &kNetworkBadgeTechnologyHspaPlusIcon;
  216. } else if (technology == onc::cellular::kTechnologyLte) {
  217. badge.icon = &kNetworkBadgeTechnologyLteIcon;
  218. } else if (technology == onc::cellular::kTechnologyLteAdvanced) {
  219. badge.icon = &kNetworkBadgeTechnologyLteAdvancedIcon;
  220. } else if (technology == onc::cellular::kTechnology5gNr) {
  221. badge.icon = &kNetworkBadgeTechnology5gIcon;
  222. } else {
  223. return {};
  224. }
  225. return badge;
  226. }
  227. gfx::ImageSkia GetIcon(const NetworkStateProperties* network,
  228. IconType icon_type,
  229. int strength_index) {
  230. if (network->type == NetworkType::kEthernet) {
  231. return gfx::CreateVectorIcon(vector_icons::kEthernetIcon,
  232. GetDefaultColorForIconType(icon_type));
  233. }
  234. if (network->type == NetworkType::kVPN) {
  235. DCHECK(!IsTrayIcon(icon_type));
  236. return gfx::CreateVectorIcon(kNetworkVpnIcon,
  237. GetDefaultColorForIconType(ICON_TYPE_LIST));
  238. }
  239. DCHECK_GE(strength_index, 0)
  240. << "Strength not set for type: " << network->type;
  241. DCHECK_LT(strength_index, kNumNetworkImages);
  242. return GetImageForIndex(ImageTypeForNetworkType(network->type), icon_type,
  243. strength_index);
  244. }
  245. gfx::ImageSkia GetConnectingVpnImage(IconType icon_type) {
  246. double animation = NetworkIconAnimation::GetInstance()->GetAnimation();
  247. gfx::ImageSkia icon = ConnectingVpnImage(animation);
  248. return CreateNetworkIconImage(icon, Badges());
  249. }
  250. } // namespace
  251. //------------------------------------------------------------------------------
  252. // NetworkIconImpl
  253. NetworkIconImpl::NetworkIconImpl(const std::string& guid,
  254. IconType icon_type,
  255. NetworkType network_type)
  256. : icon_type_(icon_type),
  257. is_dark_themed_(DarkLightModeControllerImpl::Get()->IsDarkModeEnabled()) {
  258. // Default image is null.
  259. }
  260. void NetworkIconImpl::Update(const NetworkStateProperties* network,
  261. bool show_vpn_badge) {
  262. // Determine whether or not we need to update the icon.
  263. bool dirty = image_.isNull();
  264. if (network->connection_state != connection_state_) {
  265. VLOG(2) << "Update connection state: "
  266. << static_cast<int>(network->connection_state);
  267. connection_state_ = network->connection_state;
  268. dirty = true;
  269. }
  270. NetworkType type = network->type;
  271. if (chromeos::network_config::NetworkTypeMatchesType(
  272. type, NetworkType::kWireless)) {
  273. dirty |= UpdateWirelessStrengthIndex(network);
  274. }
  275. if (type == NetworkType::kCellular)
  276. dirty |= UpdateCellularState(network);
  277. bool new_show_vpn_badge = show_vpn_badge && IconTypeHasVPNBadge(icon_type_);
  278. if (new_show_vpn_badge != show_vpn_badge_) {
  279. VLOG(2) << "Update VPN badge: " << new_show_vpn_badge;
  280. show_vpn_badge_ = new_show_vpn_badge;
  281. dirty = true;
  282. }
  283. const bool is_dark_themed =
  284. DarkLightModeControllerImpl::Get()->IsDarkModeEnabled();
  285. if (is_dark_themed_ != is_dark_themed) {
  286. is_dark_themed_ = is_dark_themed;
  287. dirty = true;
  288. }
  289. if (dirty) {
  290. // Set the icon and badges based on the network and generate the image.
  291. GenerateImage(network);
  292. }
  293. }
  294. bool NetworkIconImpl::UpdateWirelessStrengthIndex(
  295. const NetworkStateProperties* network) {
  296. int index = StrengthIndex(
  297. chromeos::network_config::GetWirelessSignalStrength(network));
  298. if (index != strength_index_) {
  299. VLOG(2) << "New strength index: " << index;
  300. strength_index_ = index;
  301. return true;
  302. }
  303. return false;
  304. }
  305. bool NetworkIconImpl::UpdateCellularState(
  306. const NetworkStateProperties* network) {
  307. bool dirty = false;
  308. if (!features::IsSeparateNetworkIconsEnabled()) {
  309. const Badge technology_badge =
  310. BadgeForNetworkTechnology(network, icon_type_);
  311. if (technology_badge != technology_badge_) {
  312. VLOG(2) << "New technology badge.";
  313. technology_badge_ = technology_badge;
  314. dirty = true;
  315. }
  316. }
  317. bool roaming = network->type_state->get_cellular()->roaming;
  318. if (roaming != is_roaming_) {
  319. VLOG(2) << "New is_roaming: " << roaming;
  320. is_roaming_ = roaming;
  321. dirty = true;
  322. }
  323. return dirty;
  324. }
  325. void NetworkIconImpl::GetBadges(const NetworkStateProperties* network,
  326. Badges* badges) {
  327. const NetworkType type = network->type;
  328. const SkColor icon_color = GetDefaultColorForIconType(icon_type_);
  329. bool is_connected =
  330. chromeos::network_config::StateIsConnected(network->connection_state);
  331. if (type == NetworkType::kWiFi) {
  332. if (network->type_state->get_wifi()->security != SecurityType::kNone &&
  333. !IsTrayIcon(icon_type_)) {
  334. badges->bottom_right = {&kUnifiedNetworkBadgeSecureIcon, icon_color};
  335. }
  336. } else if (type == NetworkType::kCellular) {
  337. // technology_badge_ is set in UpdateCellularState.
  338. if (is_connected && network->type_state->get_cellular()->roaming) {
  339. badges->top_left = {&kNetworkBadgeRoamingIcon, icon_color};
  340. } else if (is_connected && !features::IsSeparateNetworkIconsEnabled()) {
  341. // Only show technology badge when connected and roaming is not active.
  342. badges->top_left = technology_badge_;
  343. }
  344. }
  345. if (show_vpn_badge_)
  346. badges->bottom_left = {&kUnifiedNetworkBadgeVpnIcon, icon_color};
  347. if (connection_state_ == ConnectionStateType::kPortal)
  348. badges->bottom_right = {&kUnifiedNetworkBadgeCaptivePortalIcon, icon_color};
  349. }
  350. void NetworkIconImpl::GenerateImage(const NetworkStateProperties* network) {
  351. gfx::ImageSkia icon = GetIcon(network, icon_type_, strength_index_);
  352. Badges badges;
  353. GetBadges(network, &badges);
  354. image_ = CreateNetworkIconImage(icon, badges);
  355. }
  356. namespace {
  357. NetworkIconImpl* FindAndUpdateImageImpl(const NetworkStateProperties* network,
  358. IconType icon_type,
  359. bool show_vpn_badge) {
  360. // Find or add the icon.
  361. NetworkIconMap* icon_map = GetIconMap(icon_type);
  362. NetworkIconImpl* icon;
  363. NetworkIconMap::iterator iter = icon_map->find(network->guid);
  364. if (iter == icon_map->end()) {
  365. VLOG(1) << "new NetworkIconImpl: " << network->name;
  366. icon = new NetworkIconImpl(network->guid, icon_type, network->type);
  367. icon_map->insert(std::make_pair(network->guid, icon));
  368. } else {
  369. VLOG(1) << "found NetworkIconImpl: " << network->name;
  370. icon = iter->second;
  371. }
  372. // Update and return the icon's image.
  373. icon->Update(network, show_vpn_badge);
  374. return icon;
  375. }
  376. } // namespace
  377. //------------------------------------------------------------------------------
  378. // Public interface
  379. SkColor GetDefaultColorForIconType(IconType icon_type) {
  380. auto* ash_color_provider = AshColorProvider::Get();
  381. switch (icon_type) {
  382. case ICON_TYPE_TRAY_OOBE:
  383. return kIconColorInOobe;
  384. case ICON_TYPE_FEATURE_POD:
  385. return ash_color_provider->GetContentLayerColor(
  386. AshColorProvider::ContentLayerType::kButtonIconColor);
  387. case ICON_TYPE_FEATURE_POD_TOGGLED:
  388. return ash_color_provider->GetContentLayerColor(
  389. AshColorProvider::ContentLayerType::kButtonIconColorPrimary);
  390. case ICON_TYPE_FEATURE_POD_DISABLED:
  391. return color_utils::GetResultingPaintColor(
  392. AshColorProvider::GetDisabledColor(
  393. GetDefaultColorForIconType(ICON_TYPE_FEATURE_POD)),
  394. ash_color_provider->GetBackgroundColor());
  395. default:
  396. return ash_color_provider->GetContentLayerColor(
  397. AshColorProvider::ContentLayerType::kIconColorPrimary);
  398. }
  399. }
  400. const gfx::ImageSkia GetBasicImage(IconType icon_type,
  401. NetworkType network_type,
  402. bool connected) {
  403. DCHECK_NE(NetworkType::kVPN, network_type);
  404. return GetImageForIndex(ImageTypeForNetworkType(network_type), icon_type,
  405. connected ? kNumNetworkImages - 1 : 0);
  406. }
  407. gfx::ImageSkia GetImageForNonVirtualNetwork(
  408. const NetworkStateProperties* network,
  409. IconType icon_type,
  410. bool show_vpn_badge,
  411. bool* animating) {
  412. DCHECK_NE(NetworkType::kVPN, network->type);
  413. NetworkType network_type = network->type;
  414. if (network->connection_state == ConnectionStateType::kConnecting) {
  415. if (animating)
  416. *animating = true;
  417. return GetConnectingImageForNetworkType(network_type, icon_type);
  418. }
  419. NetworkIconImpl* icon =
  420. FindAndUpdateImageImpl(network, icon_type, show_vpn_badge);
  421. if (animating)
  422. *animating = false;
  423. return icon->image();
  424. }
  425. gfx::ImageSkia GetImageForVPN(const NetworkStateProperties* vpn,
  426. IconType icon_type,
  427. bool* animating) {
  428. DCHECK_EQ(NetworkType::kVPN, vpn->type);
  429. if (vpn->connection_state == ConnectionStateType::kConnecting) {
  430. if (animating)
  431. *animating = true;
  432. return GetConnectingVpnImage(icon_type);
  433. }
  434. NetworkIconImpl* icon =
  435. FindAndUpdateImageImpl(vpn, icon_type, false /* show_vpn_badge */);
  436. if (animating)
  437. *animating = false;
  438. return icon->image();
  439. }
  440. gfx::ImageSkia GetImageForWiFiNoConnections(IconType icon_type) {
  441. return gfx::CreateVectorIcon(kUnifiedMenuWifiNoConnectionIcon,
  442. GetDefaultColorForIconType(icon_type));
  443. }
  444. gfx::ImageSkia GetImageForWiFiEnabledState(bool enabled, IconType icon_type) {
  445. if (!enabled) {
  446. return gfx::CreateVectorIcon(kUnifiedMenuWifiOffIcon, kUnifiedTrayIconSize,
  447. GetDefaultColorForIconType(icon_type));
  448. }
  449. gfx::ImageSkia image =
  450. GetBasicImage(icon_type, NetworkType::kWiFi, true /* connected */);
  451. Badges badges;
  452. if (!enabled) {
  453. badges.center = {&kNetworkBadgeOffIcon,
  454. GetDefaultColorForIconType(icon_type)};
  455. }
  456. return CreateNetworkIconImage(image, badges);
  457. }
  458. gfx::ImageSkia GetConnectingImageForNetworkType(NetworkType network_type,
  459. IconType icon_type) {
  460. DCHECK_NE(NetworkType::kVPN, network_type);
  461. ImageType image_type = ImageTypeForNetworkType(network_type);
  462. double animation = NetworkIconAnimation::GetInstance()->GetAnimation();
  463. return CreateNetworkIconImage(
  464. ConnectingWirelessImage(image_type, icon_type, animation), Badges());
  465. }
  466. gfx::ImageSkia GetConnectedNetworkWithConnectingVpnImage(
  467. const NetworkStateProperties* connected_network,
  468. IconType icon_type) {
  469. gfx::ImageSkia icon = GetImageForNonVirtualNetwork(
  470. connected_network, icon_type, false /* show_vpn_badge */);
  471. double animation = NetworkIconAnimation::GetInstance()->GetAnimation();
  472. Badges badges;
  473. badges.bottom_left = {
  474. &kUnifiedNetworkBadgeVpnIcon,
  475. SkColorSetA(GetDefaultColorForIconType(icon_type), 0xFF * animation)};
  476. return CreateNetworkIconImage(icon, badges);
  477. }
  478. gfx::ImageSkia GetDisconnectedImageForNetworkType(NetworkType network_type) {
  479. return GetBasicImage(ICON_TYPE_LIST, network_type, false /* connected */);
  480. }
  481. std::u16string GetLabelForNetworkList(const NetworkStateProperties* network) {
  482. if (network->type == NetworkType::kCellular) {
  483. ActivationStateType activation_state =
  484. network->type_state->get_cellular()->activation_state;
  485. if (activation_state == ActivationStateType::kActivating) {
  486. return l10n_util::GetStringFUTF16(
  487. IDS_ASH_STATUS_TRAY_NETWORK_LIST_ACTIVATING,
  488. base::UTF8ToUTF16(network->name));
  489. }
  490. if (activation_state == ActivationStateType::kNotActivated ||
  491. activation_state == ActivationStateType::kPartiallyActivated) {
  492. return base::UTF8ToUTF16(network->name);
  493. }
  494. }
  495. // Otherwise just show the network name or 'Ethernet'.
  496. if (network->type == NetworkType::kEthernet)
  497. return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ETHERNET);
  498. return base::UTF8ToUTF16(network->name);
  499. }
  500. void PurgeNetworkIconCache(const std::set<std::string>& network_guids) {
  501. PurgeIconMap(ICON_TYPE_TRAY_OOBE, network_guids);
  502. PurgeIconMap(ICON_TYPE_TRAY_REGULAR, network_guids);
  503. PurgeIconMap(ICON_TYPE_DEFAULT_VIEW, network_guids);
  504. PurgeIconMap(ICON_TYPE_LIST, network_guids);
  505. PurgeIconMap(ICON_TYPE_MENU_LIST, network_guids);
  506. }
  507. SignalStrength GetSignalStrength(int strength) {
  508. // Decide whether the signal is considered weak, medium or strong based on the
  509. // strength index. Each signal strength corresponds to a bucket which
  510. // attempted to be split evenly from |kNumNetworkImages| - 1. Remainders go
  511. // first to the lowest bucket and then the second lowest bucket.
  512. const int index = StrengthIndex(strength);
  513. if (index == 0)
  514. return SignalStrength::NONE;
  515. const int seperations = kNumNetworkImages - 1;
  516. const int bucket_size = seperations / 3;
  517. const int weak_max = bucket_size + static_cast<int>(seperations % 3 != 0);
  518. const int medium_max =
  519. weak_max + bucket_size + static_cast<int>(seperations % 3 == 2);
  520. if (index <= weak_max)
  521. return SignalStrength::WEAK;
  522. else if (index <= medium_max)
  523. return SignalStrength::MEDIUM;
  524. return SignalStrength::STRONG;
  525. }
  526. } // namespace network_icon
  527. } // namespace ash