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