enable_hotspot_quick_action_controller.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2020 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/phonehub/enable_hotspot_quick_action_controller.h"
  5. #include "ash/resources/vector_icons/vector_icons.h"
  6. #include "ash/strings/grit/ash_strings.h"
  7. #include "ash/style/ash_color_provider.h"
  8. #include "ash/system/phonehub/phone_hub_metrics.h"
  9. #include "ash/system/phonehub/quick_action_item.h"
  10. #include "ui/base/l10n/l10n_util.h"
  11. namespace ash {
  12. using Status = phonehub::TetherController::Status;
  13. using phone_hub_metrics::LogQuickActionClick;
  14. using phone_hub_metrics::QuickAction;
  15. EnableHotspotQuickActionController::EnableHotspotQuickActionController(
  16. phonehub::TetherController* tether_controller)
  17. : tether_controller_(tether_controller) {
  18. DCHECK(tether_controller_);
  19. tether_controller_->AddObserver(this);
  20. }
  21. EnableHotspotQuickActionController::~EnableHotspotQuickActionController() {
  22. tether_controller_->RemoveObserver(this);
  23. }
  24. QuickActionItem* EnableHotspotQuickActionController::CreateItem() {
  25. DCHECK(!item_);
  26. item_ = new QuickActionItem(this, IDS_ASH_PHONE_HUB_ENABLE_HOTSPOT_TITLE,
  27. kPhoneHubEnableHotspotIcon);
  28. OnTetherStatusChanged();
  29. return item_;
  30. }
  31. void EnableHotspotQuickActionController::OnButtonPressed(bool is_now_enabled) {
  32. LogQuickActionClick(is_now_enabled ? QuickAction::kToggleHotspotOff
  33. : QuickAction::kToggleHotspotOn);
  34. is_now_enabled ? tether_controller_->Disconnect()
  35. : tether_controller_->AttemptConnection();
  36. }
  37. void EnableHotspotQuickActionController::OnTetherStatusChanged() {
  38. switch (tether_controller_->GetStatus()) {
  39. case Status::kIneligibleForFeature:
  40. item_->SetVisible(false);
  41. return;
  42. case Status::kConnectionUnavailable:
  43. [[fallthrough]];
  44. case Status::kConnectionAvailable:
  45. SetState(ActionState::kOff);
  46. break;
  47. case Status::kConnecting:
  48. SetState(ActionState::kConnecting);
  49. break;
  50. case Status::kConnected:
  51. SetState(ActionState::kConnected);
  52. break;
  53. case Status::kNoReception:
  54. SetState(ActionState::kNoReception);
  55. break;
  56. }
  57. item_->SetVisible(true);
  58. }
  59. void EnableHotspotQuickActionController::SetState(ActionState state) {
  60. item_->SetEnabled(true);
  61. bool icon_enabled;
  62. bool button_enabled;
  63. int state_text_id;
  64. int sub_label_text;
  65. SkColor sub_label_color;
  66. switch (state) {
  67. case ActionState::kOff:
  68. icon_enabled = false;
  69. button_enabled = true;
  70. state_text_id = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_DISABLED_STATE_TOOLTIP;
  71. sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_OFF_STATE;
  72. sub_label_color = AshColorProvider::Get()->GetContentLayerColor(
  73. AshColorProvider::ContentLayerType::kTextColorSecondary);
  74. break;
  75. case ActionState::kConnecting:
  76. icon_enabled = true;
  77. button_enabled = true;
  78. state_text_id = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_CONNECTING_STATE_TOOLTIP;
  79. sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_CONNECTING_STATE;
  80. sub_label_color = AshColorProvider::Get()->GetContentLayerColor(
  81. AshColorProvider::ContentLayerType::kTextColorSecondary);
  82. break;
  83. case ActionState::kConnected:
  84. icon_enabled = true;
  85. button_enabled = true;
  86. state_text_id = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_CONNECTED_STATE_TOOLTIP;
  87. sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_CONNECTED_STATE;
  88. sub_label_color = AshColorProvider::Get()->GetContentLayerColor(
  89. AshColorProvider::ContentLayerType::kTextColorPositive);
  90. break;
  91. case ActionState::kNoReception:
  92. icon_enabled = false;
  93. button_enabled = false;
  94. state_text_id =
  95. IDS_ASH_PHONE_HUB_ENABLE_HOTSPOT_NO_RECEPTION_STATE_TOOLTIP;
  96. sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_NOT_AVAILABLE_STATE;
  97. sub_label_color = AshColorProvider::Get()->GetContentLayerColor(
  98. AshColorProvider::ContentLayerType::kTextColorSecondary);
  99. break;
  100. }
  101. item_->SetToggled(icon_enabled);
  102. item_->SetEnabled(button_enabled);
  103. item_->SetSubLabel(l10n_util::GetStringUTF16(sub_label_text));
  104. item_->SetSubLabelColor(sub_label_color);
  105. if (state == ActionState::kNoReception) {
  106. item_->SetTooltip(l10n_util::GetStringUTF16(state_text_id));
  107. } else {
  108. std::u16string tooltip_state =
  109. l10n_util::GetStringFUTF16(state_text_id, item_->GetItemLabel());
  110. item_->SetTooltip(l10n_util::GetStringFUTF16(
  111. IDS_ASH_PHONE_HUB_QUICK_ACTIONS_TOGGLE_TOOLTIP, item_->GetItemLabel(),
  112. tooltip_state));
  113. }
  114. }
  115. } // namespace ash