nearby_share_feature_pod_controller.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/nearby_share/nearby_share_feature_pod_controller.h"
  5. #include "ash/public/cpp/nearby_share_delegate.h"
  6. #include "ash/resources/vector_icons/vector_icons.h"
  7. #include "ash/session/session_controller_impl.h"
  8. #include "ash/shell.h"
  9. #include "ash/strings/grit/ash_strings.h"
  10. #include "ash/system/model/system_tray_model.h"
  11. #include "ash/system/unified/feature_pod_button.h"
  12. #include "ash/system/unified/unified_system_tray_controller.h"
  13. #include "base/bind.h"
  14. #include "base/strings/string_number_conversions.h"
  15. #include "base/strings/utf_string_conversions.h"
  16. #include "base/time/time.h"
  17. #include "ui/base/l10n/l10n_util.h"
  18. namespace ash {
  19. namespace {
  20. constexpr base::TimeDelta kOneMinute = base::Minutes(1);
  21. constexpr base::TimeDelta kOneSecond = base::Seconds(1);
  22. std::u16string RemainingTimeString(base::TimeDelta remaining_time) {
  23. if (remaining_time > kOneMinute) {
  24. return l10n_util::GetStringFUTF16Int(
  25. IDS_ASH_STATUS_TRAY_NEARBY_SHARE_REMAINING_MINUTES,
  26. remaining_time.InMinutes() + 1);
  27. }
  28. return l10n_util::GetStringFUTF16Int(
  29. IDS_ASH_STATUS_TRAY_NEARBY_SHARE_REMAINING_SECONDS,
  30. static_cast<int>(remaining_time.InSeconds()) + 1);
  31. }
  32. } // namespace
  33. NearbyShareFeaturePodController::NearbyShareFeaturePodController(
  34. UnifiedSystemTrayController* tray_controller)
  35. : countdown_timer_(
  36. FROM_HERE,
  37. kOneSecond,
  38. base::BindRepeating(&NearbyShareFeaturePodController::UpdateButton,
  39. base::Unretained(this),
  40. /*enabled=*/true)),
  41. tray_controller_(tray_controller),
  42. nearby_share_delegate_(Shell::Get()->nearby_share_delegate()),
  43. nearby_share_controller_(Shell::Get()->nearby_share_controller()) {
  44. DCHECK(tray_controller_);
  45. DCHECK(nearby_share_delegate_);
  46. DCHECK(nearby_share_controller_);
  47. nearby_share_controller_->AddObserver(this);
  48. }
  49. NearbyShareFeaturePodController::~NearbyShareFeaturePodController() {
  50. nearby_share_controller_->RemoveObserver(this);
  51. }
  52. FeaturePodButton* NearbyShareFeaturePodController::CreateButton() {
  53. DCHECK(!button_);
  54. button_ = new FeaturePodButton(this);
  55. SessionControllerImpl* session_controller =
  56. Shell::Get()->session_controller();
  57. button_->SetVisible(nearby_share_delegate_->IsPodButtonVisible() &&
  58. session_controller->IsActiveUserSessionStarted() &&
  59. session_controller->IsUserPrimary() &&
  60. !session_controller->IsUserSessionBlocked());
  61. button_->SetLabel(
  62. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NEARBY_SHARE_BUTTON_LABEL));
  63. button_->SetLabelTooltip(l10n_util::GetStringUTF16(
  64. IDS_ASH_STATUS_TRAY_NEARBY_SHARE_SETTINGS_TOOLTIP));
  65. button_->SetIconTooltip(l10n_util::GetStringUTF16(
  66. IDS_ASH_STATUS_TRAY_NEARBY_SHARE_TOGGLE_TOOLTIP));
  67. bool enabled = nearby_share_delegate_->IsHighVisibilityOn();
  68. OnHighVisibilityEnabledChanged(enabled);
  69. return button_;
  70. }
  71. void NearbyShareFeaturePodController::OnIconPressed() {
  72. if (nearby_share_delegate_->IsHighVisibilityOn()) {
  73. nearby_share_delegate_->DisableHighVisibility();
  74. } else {
  75. nearby_share_delegate_->EnableHighVisibility();
  76. }
  77. }
  78. void NearbyShareFeaturePodController::OnLabelPressed() {
  79. nearby_share_delegate_->ShowNearbyShareSettings();
  80. }
  81. SystemTrayItemUmaType NearbyShareFeaturePodController::GetUmaType() const {
  82. return SystemTrayItemUmaType::UMA_NEARBY_SHARE;
  83. }
  84. void NearbyShareFeaturePodController::OnHighVisibilityEnabledChanged(
  85. bool enabled) {
  86. if (enabled) {
  87. shutoff_time_ = nearby_share_delegate_->HighVisibilityShutoffTime();
  88. countdown_timer_.Reset();
  89. } else {
  90. countdown_timer_.Stop();
  91. }
  92. UpdateButton(enabled);
  93. }
  94. void NearbyShareFeaturePodController::UpdateButton(bool enabled) {
  95. button_->SetToggled(enabled);
  96. button_->SetVectorIcon(enabled ? kUnifiedMenuNearbyShareVisibleIcon
  97. : kUnifiedMenuNearbyShareNotVisibleIcon);
  98. if (enabled) {
  99. button_->SetSubLabel(l10n_util::GetStringFUTF16(
  100. IDS_ASH_STATUS_TRAY_NEARBY_SHARE_ON_STATE,
  101. RemainingTimeString(RemainingHighVisibilityTime())));
  102. } else {
  103. button_->SetSubLabel(
  104. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NEARBY_SHARE_OFF_STATE));
  105. }
  106. }
  107. base::TimeDelta NearbyShareFeaturePodController::RemainingHighVisibilityTime()
  108. const {
  109. base::TimeTicks now = base::TimeTicks::Now();
  110. return shutoff_time_ > now ? shutoff_time_ - now : base::Seconds(0);
  111. }
  112. } // namespace ash