network_feature_pod_button_legacy.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2018 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_feature_pod_button_legacy.h"
  5. #include "ash/shell.h"
  6. #include "ash/strings/grit/ash_strings.h"
  7. #include "ash/system/model/system_tray_model.h"
  8. #include "ash/system/network/active_network_icon.h"
  9. #include "ash/system/network/network_icon.h"
  10. #include "ash/system/network/network_icon_animation.h"
  11. #include "ash/system/network/tray_network_state_model.h"
  12. #include "ash/system/tray/system_tray_notifier.h"
  13. #include "base/strings/utf_string_conversions.h"
  14. #include "chromeos/services/network_config/public/cpp/cros_network_config_util.h"
  15. #include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
  16. #include "components/onc/onc_constants.h"
  17. #include "third_party/cros_system_api/dbus/service_constants.h"
  18. #include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
  19. #include "ui/base/l10n/l10n_util.h"
  20. using chromeos::network_config::mojom::ActivationStateType;
  21. using chromeos::network_config::mojom::CellularStateProperties;
  22. using chromeos::network_config::mojom::ConnectionStateType;
  23. using chromeos::network_config::mojom::DeviceStateType;
  24. using chromeos::network_config::mojom::NetworkStateProperties;
  25. using chromeos::network_config::mojom::NetworkType;
  26. namespace ash {
  27. namespace {
  28. std::u16string GetSubLabelForConnectedNetwork(
  29. const NetworkStateProperties* network) {
  30. DCHECK(network &&
  31. chromeos::network_config::StateIsConnected(network->connection_state));
  32. if (!chromeos::network_config::NetworkStateMatchesType(
  33. network, NetworkType::kWireless)) {
  34. return l10n_util::GetStringUTF16(
  35. IDS_ASH_STATUS_TRAY_NETWORK_STATUS_CONNECTED);
  36. }
  37. if (network->type == NetworkType::kCellular) {
  38. CellularStateProperties* cellular =
  39. network->type_state->get_cellular().get();
  40. if (cellular->network_technology == onc::cellular::kTechnologyCdma1Xrtt) {
  41. return l10n_util::GetStringUTF16(
  42. IDS_ASH_STATUS_TRAY_NETWORK_CELLULAR_TYPE_ONE_X);
  43. }
  44. if (cellular->network_technology == onc::cellular::kTechnologyGsm) {
  45. return l10n_util::GetStringUTF16(
  46. IDS_ASH_STATUS_TRAY_NETWORK_CELLULAR_TYPE_GSM);
  47. }
  48. if (cellular->network_technology == onc::cellular::kTechnologyGprs) {
  49. return l10n_util::GetStringUTF16(
  50. IDS_ASH_STATUS_TRAY_NETWORK_CELLULAR_TYPE_GPRS);
  51. }
  52. if (cellular->network_technology == onc::cellular::kTechnologyEdge) {
  53. return l10n_util::GetStringUTF16(
  54. IDS_ASH_STATUS_TRAY_NETWORK_CELLULAR_TYPE_EDGE);
  55. }
  56. if (cellular->network_technology == onc::cellular::kTechnologyEvdo ||
  57. cellular->network_technology == onc::cellular::kTechnologyUmts) {
  58. return l10n_util::GetStringUTF16(
  59. IDS_ASH_STATUS_TRAY_NETWORK_CELLULAR_TYPE_THREE_G);
  60. }
  61. if (cellular->network_technology == onc::cellular::kTechnologyHspa) {
  62. return l10n_util::GetStringUTF16(
  63. IDS_ASH_STATUS_TRAY_NETWORK_CELLULAR_TYPE_HSPA);
  64. }
  65. if (cellular->network_technology == onc::cellular::kTechnologyHspaPlus) {
  66. return l10n_util::GetStringUTF16(
  67. IDS_ASH_STATUS_TRAY_NETWORK_CELLULAR_TYPE_HSPA_PLUS);
  68. }
  69. if (cellular->network_technology == onc::cellular::kTechnologyLte) {
  70. return l10n_util::GetStringUTF16(
  71. IDS_ASH_STATUS_TRAY_NETWORK_CELLULAR_TYPE_LTE);
  72. }
  73. if (cellular->network_technology == onc::cellular::kTechnologyLteAdvanced) {
  74. return l10n_util::GetStringUTF16(
  75. IDS_ASH_STATUS_TRAY_NETWORK_CELLULAR_TYPE_LTE_PLUS);
  76. }
  77. // All connectivity types exposed by Shill should be covered above. However,
  78. // as a fail-safe, return the default "Connected" string here to protect
  79. // against Shill providing an unexpected value.
  80. NOTREACHED();
  81. return l10n_util::GetStringUTF16(
  82. IDS_ASH_STATUS_TRAY_NETWORK_STATUS_CONNECTED);
  83. }
  84. int signal_strength =
  85. chromeos::network_config::GetWirelessSignalStrength(network);
  86. switch (network_icon::GetSignalStrength(signal_strength)) {
  87. case network_icon::SignalStrength::NONE:
  88. return l10n_util::GetStringUTF16(
  89. IDS_ASH_STATUS_TRAY_NETWORK_STATUS_CONNECTED);
  90. case network_icon::SignalStrength::WEAK:
  91. return l10n_util::GetStringUTF16(
  92. IDS_ASH_STATUS_TRAY_NETWORK_SIGNAL_WEAK_SUBLABEL);
  93. case network_icon::SignalStrength::MEDIUM:
  94. return l10n_util::GetStringUTF16(
  95. IDS_ASH_STATUS_TRAY_NETWORK_SIGNAL_MEDIUM_SUBLABEL);
  96. case network_icon::SignalStrength::STRONG:
  97. return l10n_util::GetStringUTF16(
  98. IDS_ASH_STATUS_TRAY_NETWORK_SIGNAL_STRONG_SUBLABEL);
  99. }
  100. NOTREACHED();
  101. return l10n_util::GetStringUTF16(
  102. IDS_ASH_STATUS_TRAY_NETWORK_STATUS_CONNECTED);
  103. }
  104. } // namespace
  105. NetworkFeaturePodButtonLegacy::NetworkFeaturePodButtonLegacy(
  106. FeaturePodControllerBase* controller)
  107. : FeaturePodButton(controller) {
  108. Shell::Get()->system_tray_model()->network_state_model()->AddObserver(this);
  109. ShowDetailedViewArrow();
  110. Update();
  111. }
  112. NetworkFeaturePodButtonLegacy::~NetworkFeaturePodButtonLegacy() {
  113. network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
  114. Shell::Get()->system_tray_model()->network_state_model()->RemoveObserver(
  115. this);
  116. }
  117. void NetworkFeaturePodButtonLegacy::NetworkIconChanged() {
  118. Update();
  119. }
  120. void NetworkFeaturePodButtonLegacy::ActiveNetworkStateChanged() {
  121. Update();
  122. }
  123. void NetworkFeaturePodButtonLegacy::OnThemeChanged() {
  124. FeaturePodButton::OnThemeChanged();
  125. // Need to redraw all network icons with new colors.
  126. Shell::Get()
  127. ->system_tray_model()
  128. ->active_network_icon()
  129. ->PurgeNetworkIconCache();
  130. NetworkIconChanged();
  131. }
  132. const char* NetworkFeaturePodButtonLegacy::GetClassName() const {
  133. // The name returned is intentionally missing the "Legacy" suffix to avoid
  134. // updating tests that search for this node, both now and when this class
  135. // is removed and is replaced by a new NetworkFeaturePodButton.
  136. return "NetworkFeaturePodButton";
  137. }
  138. void NetworkFeaturePodButtonLegacy::Update() {
  139. TrayNetworkStateModel* model =
  140. Shell::Get()->system_tray_model()->network_state_model();
  141. const NetworkStateProperties* network = model->default_network();
  142. const bool toggled = network || model->GetDeviceState(NetworkType::kWiFi) ==
  143. DeviceStateType::kEnabled;
  144. network_icon::IconType icon_type =
  145. toggled ? network_icon::ICON_TYPE_FEATURE_POD_TOGGLED
  146. : network_icon::ICON_TYPE_FEATURE_POD;
  147. bool image_animating = false;
  148. gfx::ImageSkia image =
  149. Shell::Get()->system_tray_model()->active_network_icon()->GetImage(
  150. ActiveNetworkIcon::Type::kSingle, icon_type, &image_animating);
  151. gfx::ImageSkia image_disabled =
  152. Shell::Get()->system_tray_model()->active_network_icon()->GetImage(
  153. ActiveNetworkIcon::Type::kSingle,
  154. network_icon::ICON_TYPE_FEATURE_POD_DISABLED, &image_animating);
  155. if (image_animating)
  156. network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
  157. else
  158. network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
  159. icon_button()->SetImage(views::Button::STATE_NORMAL, image);
  160. icon_button()->SetImage(views::Button::STATE_DISABLED, image_disabled);
  161. SetToggled(toggled);
  162. std::u16string network_name;
  163. if (network) {
  164. network_name = network->type == NetworkType::kEthernet
  165. ? l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ETHERNET)
  166. : base::UTF8ToUTF16(network->name);
  167. }
  168. // Check for Activating first since activating networks may be connected.
  169. if (network && network->type == NetworkType::kCellular &&
  170. network->type_state->get_cellular()->activation_state ==
  171. ActivationStateType::kActivating) {
  172. SetLabel(network_name);
  173. SetSubLabel(l10n_util::GetStringUTF16(
  174. IDS_ASH_STATUS_TRAY_NETWORK_ACTIVATING_SUBLABEL));
  175. } else if (network && chromeos::network_config::StateIsConnected(
  176. network->connection_state)) {
  177. SetLabel(network_name);
  178. SetSubLabel(GetSubLabelForConnectedNetwork(network));
  179. } else if (network &&
  180. network->connection_state == ConnectionStateType::kConnecting) {
  181. SetLabel(network_name);
  182. SetSubLabel(l10n_util::GetStringUTF16(
  183. IDS_ASH_STATUS_TRAY_NETWORK_CONNECTING_SUBLABEL));
  184. } else {
  185. SetLabel(l10n_util::GetStringUTF16(
  186. IDS_ASH_STATUS_TRAY_NETWORK_DISCONNECTED_LABEL));
  187. SetSubLabel(l10n_util::GetStringUTF16(
  188. IDS_ASH_STATUS_TRAY_NETWORK_DISCONNECTED_SUBLABEL));
  189. }
  190. std::u16string tooltip;
  191. Shell::Get()
  192. ->system_tray_model()
  193. ->active_network_icon()
  194. ->GetConnectionStatusStrings(ActiveNetworkIcon::Type::kSingle,
  195. /*a11y_name=*/nullptr,
  196. /*a11y_desc=*/nullptr, &tooltip);
  197. UpdateTooltip(tooltip);
  198. }
  199. void NetworkFeaturePodButtonLegacy::UpdateTooltip(
  200. const std::u16string& connection_state_message) {
  201. // When the button is enabled, use tooltips to alert the user of the actions
  202. // that will be taken when interacting with the button/toggle. However, if the
  203. // button is disabled, those actions cannot be taken, so simply display the
  204. // state of the connection as a tooltip
  205. if (!GetEnabled()) {
  206. SetIconTooltip(connection_state_message);
  207. SetLabelTooltip(connection_state_message);
  208. return;
  209. }
  210. SetIconTooltip(l10n_util::GetStringFUTF16(
  211. IDS_ASH_STATUS_TRAY_NETWORK_TOGGLE_TOOLTIP, connection_state_message));
  212. SetLabelTooltip(l10n_util::GetStringFUTF16(
  213. IDS_ASH_STATUS_TRAY_NETWORK_SETTINGS_TOOLTIP, connection_state_message));
  214. }
  215. } // namespace ash