locate_phone_quick_action_controller.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/locate_phone_quick_action_controller.h"
  5. #include "ash/resources/vector_icons/vector_icons.h"
  6. #include "ash/strings/grit/ash_strings.h"
  7. #include "ash/system/phonehub/phone_hub_metrics.h"
  8. #include "ash/system/phonehub/quick_action_item.h"
  9. #include "base/bind.h"
  10. #include "base/timer/timer.h"
  11. #include "ui/base/l10n/l10n_util.h"
  12. namespace ash {
  13. using phone_hub_metrics::LogQuickActionClick;
  14. using phone_hub_metrics::QuickAction;
  15. namespace {
  16. // Time to wait until we check the state of the phone to prevent showing wrong
  17. // state
  18. constexpr base::TimeDelta kWaitForRequestTimeout = base::Seconds(5);
  19. } // namespace
  20. using Status = phonehub::FindMyDeviceController::Status;
  21. LocatePhoneQuickActionController::LocatePhoneQuickActionController(
  22. phonehub::FindMyDeviceController* find_my_device_controller)
  23. : find_my_device_controller_(find_my_device_controller) {
  24. DCHECK(find_my_device_controller_);
  25. find_my_device_controller_->AddObserver(this);
  26. }
  27. LocatePhoneQuickActionController::~LocatePhoneQuickActionController() {
  28. find_my_device_controller_->RemoveObserver(this);
  29. }
  30. QuickActionItem* LocatePhoneQuickActionController::CreateItem() {
  31. DCHECK(!item_);
  32. item_ = new QuickActionItem(this, IDS_ASH_PHONE_HUB_LOCATE_PHONE_TITLE,
  33. kPhoneHubLocatePhoneIcon);
  34. OnPhoneRingingStateChanged();
  35. return item_;
  36. }
  37. void LocatePhoneQuickActionController::OnButtonPressed(bool is_now_enabled) {
  38. LogQuickActionClick(is_now_enabled ? QuickAction::kToggleLocatePhoneOff
  39. : QuickAction::kToggleLocatePhoneOn);
  40. requested_state_ = is_now_enabled ? ActionState::kOff : ActionState::kOn;
  41. SetItemState(requested_state_.value());
  42. check_requested_state_timer_ = std::make_unique<base::OneShotTimer>();
  43. check_requested_state_timer_->Start(
  44. FROM_HERE, kWaitForRequestTimeout,
  45. base::BindOnce(&LocatePhoneQuickActionController::CheckRequestedState,
  46. base::Unretained(this)));
  47. find_my_device_controller_->RequestNewPhoneRingingState(!is_now_enabled);
  48. }
  49. void LocatePhoneQuickActionController::OnPhoneRingingStateChanged() {
  50. UpdateState();
  51. }
  52. void LocatePhoneQuickActionController::UpdateState() {
  53. // Disable Locate Phone if Silence Phone is on, otherwise change accordingly
  54. // based on status from FindMyDeviceController.
  55. switch (find_my_device_controller_->GetPhoneRingingStatus()) {
  56. case Status::kRingingOff:
  57. state_ = ActionState::kOff;
  58. break;
  59. case Status::kRingingOn:
  60. state_ = ActionState::kOn;
  61. break;
  62. case Status::kRingingNotAvailable:
  63. state_ = ActionState::kNotAvailable;
  64. break;
  65. }
  66. SetItemState(state_);
  67. // If |requested_state_| correctly resembles the current state, reset it and
  68. // the timer.
  69. if (state_ == requested_state_) {
  70. check_requested_state_timer_.reset();
  71. requested_state_.reset();
  72. }
  73. }
  74. void LocatePhoneQuickActionController::SetItemState(ActionState state) {
  75. bool icon_enabled;
  76. bool button_enabled;
  77. int state_text_id;
  78. int sub_label_text;
  79. switch (state) {
  80. case ActionState::kNotAvailable:
  81. icon_enabled = false;
  82. button_enabled = false;
  83. state_text_id = IDS_ASH_PHONE_HUB_LOCATE_BUTTON_NOT_AVAILABLE_TOOLTIP;
  84. sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_NOT_AVAILABLE_STATE;
  85. break;
  86. case ActionState::kOff:
  87. icon_enabled = false;
  88. button_enabled = true;
  89. state_text_id = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_DISABLED_STATE_TOOLTIP;
  90. sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_OFF_STATE;
  91. break;
  92. case ActionState::kOn:
  93. icon_enabled = true;
  94. button_enabled = true;
  95. state_text_id = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_ENABLED_STATE_TOOLTIP;
  96. sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_ON_STATE;
  97. break;
  98. }
  99. item_->SetEnabled(button_enabled);
  100. item_->SetToggled(icon_enabled);
  101. item_->SetSubLabel(l10n_util::GetStringUTF16(sub_label_text));
  102. if (state == ActionState::kNotAvailable) {
  103. item_->SetTooltip(l10n_util::GetStringUTF16(state_text_id));
  104. } else {
  105. std::u16string tooltip_state =
  106. l10n_util::GetStringFUTF16(state_text_id, item_->GetItemLabel());
  107. item_->SetTooltip(l10n_util::GetStringFUTF16(
  108. IDS_ASH_PHONE_HUB_QUICK_ACTIONS_TOGGLE_TOOLTIP, item_->GetItemLabel(),
  109. tooltip_state));
  110. }
  111. }
  112. void LocatePhoneQuickActionController::CheckRequestedState() {
  113. // If the current state is different from the requested state, it means that
  114. // we fail to change the state, so switch back to the original one.
  115. if (state_ != requested_state_)
  116. SetItemState(state_);
  117. check_requested_state_timer_.reset();
  118. requested_state_.reset();
  119. }
  120. } // namespace ash