network_list_mobile_header_view_impl.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2022 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_list_mobile_header_view_impl.h"
  5. #include "ash/ash_export.h"
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/public/cpp/system_tray_client.h"
  8. #include "ash/resources/vector_icons/vector_icons.h"
  9. #include "ash/shell.h"
  10. #include "ash/strings/grit/ash_strings.h"
  11. #include "ash/style/icon_button.h"
  12. #include "ash/system/model/system_tray_model.h"
  13. #include "ash/system/network/network_list_mobile_header_view.h"
  14. #include "ash/system/network/network_list_network_header_view.h"
  15. #include "ash/system/network/tray_network_state_model.h"
  16. #include "ash/system/tray/tray_popup_utils.h"
  17. #include "ash/system/tray/tri_view.h"
  18. #include "base/memory/weak_ptr.h"
  19. #include "chromeos/services/network_config/public/cpp/cros_network_config_util.h"
  20. #include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
  21. #include "components/onc/onc_constants.h"
  22. #include "ui/base/l10n/l10n_util.h"
  23. #include "ui/views/controls/image_view.h"
  24. #include "ui/views/view.h"
  25. namespace ash {
  26. namespace {
  27. using chromeos::network_config::mojom::DeviceStateProperties;
  28. using chromeos::network_config::mojom::DeviceStateType;
  29. using chromeos::network_config::mojom::NetworkType;
  30. int GetAddESimTooltipMessageId() {
  31. const DeviceStateProperties* cellular_device =
  32. Shell::Get()->system_tray_model()->network_state_model()->GetDevice(
  33. NetworkType::kCellular);
  34. DCHECK(cellular_device);
  35. switch (cellular_device->inhibit_reason) {
  36. case chromeos::network_config::mojom::InhibitReason::kInstallingProfile:
  37. return IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_INSTALLING_PROFILE;
  38. case chromeos::network_config::mojom::InhibitReason::kRenamingProfile:
  39. return IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_RENAMING_PROFILE;
  40. case chromeos::network_config::mojom::InhibitReason::kRemovingProfile:
  41. return IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_REMOVING_PROFILE;
  42. case chromeos::network_config::mojom::InhibitReason::kConnectingToProfile:
  43. return IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_CONNECTING_TO_PROFILE;
  44. case chromeos::network_config::mojom::InhibitReason::kRefreshingProfileList:
  45. return IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_REFRESHING_PROFILE_LIST;
  46. case chromeos::network_config::mojom::InhibitReason::kNotInhibited:
  47. return IDS_ASH_STATUS_TRAY_ADD_CELLULAR_LABEL;
  48. case chromeos::network_config::mojom::InhibitReason::kResettingEuiccMemory:
  49. return IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_RESETTING_ESIM;
  50. case chromeos::network_config::mojom::InhibitReason::kDisablingProfile:
  51. return IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_DISABLING_PROFILE;
  52. }
  53. }
  54. } // namespace
  55. NetworkListMobileHeaderViewImpl::NetworkListMobileHeaderViewImpl(
  56. NetworkListNetworkHeaderView::Delegate* delegate)
  57. : NetworkListMobileHeaderView(delegate) {
  58. AddExtraButtons();
  59. }
  60. NetworkListMobileHeaderViewImpl::~NetworkListMobileHeaderViewImpl() = default;
  61. void NetworkListMobileHeaderViewImpl::AddExtraButtons() {
  62. // The button navigates to Settings, only add it if this can occur.
  63. if (!TrayPopupUtils::CanOpenWebUISettings())
  64. return;
  65. const gfx::VectorIcon& icon = base::i18n::IsRTL() ? kAddCellularNetworkRtlIcon
  66. : kAddCellularNetworkIcon;
  67. std::unique_ptr<IconButton> add_esim_button = std::make_unique<IconButton>(
  68. base::BindRepeating(
  69. &NetworkListMobileHeaderViewImpl::AddESimButtonPressed,
  70. weak_factory_.GetWeakPtr()),
  71. IconButton::Type::kSmall, &icon, /*is_togglable=*/false,
  72. /*has_border=*/false);
  73. add_esim_button.get()->SetID(kAddESimButtonId);
  74. add_esim_button_ = add_esim_button.get();
  75. container()->AddViewAt(TriView::Container::END, add_esim_button.release(),
  76. /*index=*/0);
  77. };
  78. void NetworkListMobileHeaderViewImpl::OnToggleToggled(bool is_on) {
  79. delegate()->OnMobileToggleClicked(is_on);
  80. }
  81. void NetworkListMobileHeaderViewImpl::AddESimButtonPressed() {
  82. Shell::Get()->system_tray_model()->client()->ShowNetworkCreate(
  83. ::onc::network_type::kCellular);
  84. }
  85. void NetworkListMobileHeaderViewImpl::SetAddESimButtonState(bool enabled,
  86. bool visible) {
  87. if (!add_esim_button_)
  88. return;
  89. add_esim_button_->SetVisible(visible);
  90. add_esim_button_->SetEnabled(enabled);
  91. // We do not bother updating the tooltip when the "add eSIM" button is
  92. // not visible to avoid the case where no Cellular device is available
  93. // since we do not have a tooltip for this situation.
  94. if (!visible)
  95. return;
  96. add_esim_button_->SetTooltipText(
  97. l10n_util::GetStringUTF16(GetAddESimTooltipMessageId()));
  98. }
  99. } // namespace ash