quiet_mode_feature_pod_controller.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2018 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/unified/quiet_mode_feature_pod_controller.h"
  5. #include "ash/public/cpp/notifier_metadata.h"
  6. #include "ash/public/cpp/notifier_settings_controller.h"
  7. #include "ash/resources/vector_icons/vector_icons.h"
  8. #include "ash/session/session_controller_impl.h"
  9. #include "ash/shell.h"
  10. #include "ash/strings/grit/ash_strings.h"
  11. #include "ash/system/machine_learning/user_settings_event_logger.h"
  12. #include "ash/system/unified/feature_pod_button.h"
  13. #include "ash/system/unified/unified_system_tray_controller.h"
  14. #include "base/metrics/histogram_macros.h"
  15. #include "base/metrics/user_metrics.h"
  16. #include "ui/base/l10n/l10n_util.h"
  17. #include "ui/message_center/message_center.h"
  18. using message_center::MessageCenter;
  19. namespace ash {
  20. namespace {
  21. void LogUserQuietModeEvent(const bool enabled) {
  22. auto* logger = ml::UserSettingsEventLogger::Get();
  23. if (logger) {
  24. logger->LogQuietModeUkmEvent(enabled);
  25. }
  26. }
  27. } // namespace
  28. QuietModeFeaturePodController::QuietModeFeaturePodController(
  29. UnifiedSystemTrayController* tray_controller)
  30. : tray_controller_(tray_controller) {
  31. MessageCenter::Get()->AddObserver(this);
  32. }
  33. QuietModeFeaturePodController::~QuietModeFeaturePodController() {
  34. NotifierSettingsController::Get()->RemoveNotifierSettingsObserver(this);
  35. MessageCenter::Get()->RemoveObserver(this);
  36. }
  37. FeaturePodButton* QuietModeFeaturePodController::CreateButton() {
  38. DCHECK(!button_);
  39. button_ = new FeaturePodButton(this);
  40. button_->SetVectorIcon(kUnifiedMenuDoNotDisturbIcon);
  41. button_->SetVisible(
  42. Shell::Get()->session_controller()->ShouldShowNotificationTray() &&
  43. !Shell::Get()->session_controller()->IsScreenLocked());
  44. button_->SetLabel(
  45. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NOTIFICATIONS_LABEL));
  46. button_->SetIconTooltip(l10n_util::GetStringFUTF16(
  47. IDS_ASH_STATUS_TRAY_NOTIFICATIONS_TOGGLE_TOOLTIP,
  48. GetQuietModeStateTooltip()));
  49. button_->ShowDetailedViewArrow();
  50. NotifierSettingsController::Get()->AddNotifierSettingsObserver(this);
  51. OnQuietModeChanged(MessageCenter::Get()->IsQuietMode());
  52. return button_;
  53. }
  54. void QuietModeFeaturePodController::OnIconPressed() {
  55. MessageCenter* message_center = MessageCenter::Get();
  56. bool is_quiet_mode = message_center->IsQuietMode();
  57. LogUserQuietModeEvent(!is_quiet_mode);
  58. message_center->SetQuietMode(!is_quiet_mode);
  59. if (message_center->IsQuietMode()) {
  60. base::RecordAction(base::UserMetricsAction("StatusArea_QuietMode_Enabled"));
  61. } else {
  62. base::RecordAction(
  63. base::UserMetricsAction("StatusArea_QuietMode_Disabled"));
  64. }
  65. }
  66. void QuietModeFeaturePodController::OnLabelPressed() {
  67. tray_controller_->ShowNotifierSettingsView();
  68. }
  69. SystemTrayItemUmaType QuietModeFeaturePodController::GetUmaType() const {
  70. return SystemTrayItemUmaType::UMA_QUIET_MODE;
  71. }
  72. void QuietModeFeaturePodController::OnQuietModeChanged(bool in_quiet_mode) {
  73. button_->SetToggled(in_quiet_mode);
  74. button_->SetIconTooltip(l10n_util::GetStringFUTF16(
  75. IDS_ASH_STATUS_TRAY_NOTIFICATIONS_TOGGLE_TOOLTIP,
  76. GetQuietModeStateTooltip()));
  77. if (in_quiet_mode) {
  78. button_->SetSubLabel(l10n_util::GetStringUTF16(
  79. IDS_ASH_STATUS_TRAY_NOTIFICATIONS_DO_NOT_DISTURB_SUBLABEL));
  80. button_->SetLabelTooltip(l10n_util::GetStringUTF16(
  81. IDS_ASH_STATUS_TRAY_NOTIFICATIONS_SETTINGS_DO_NOT_DISTURB_TOOLTIP));
  82. } else if (button_->GetVisible()) {
  83. NotifierSettingsController::Get()->GetNotifiers();
  84. }
  85. }
  86. void QuietModeFeaturePodController::OnNotifiersUpdated(
  87. const std::vector<NotifierMetadata>& notifiers) {
  88. if (MessageCenter::Get()->IsQuietMode())
  89. return;
  90. int disabled_count = 0;
  91. for (const NotifierMetadata& notifier : notifiers) {
  92. if (!notifier.enabled)
  93. ++disabled_count;
  94. }
  95. RecordDisabledNotifierCount(disabled_count);
  96. if (disabled_count > 0) {
  97. button_->SetSubLabel(l10n_util::GetPluralStringFUTF16(
  98. IDS_ASH_STATUS_TRAY_NOTIFICATIONS_OFF_FOR_APPS_SUBLABEL,
  99. disabled_count));
  100. button_->SetLabelTooltip(l10n_util::GetPluralStringFUTF16(
  101. IDS_ASH_STATUS_TRAY_NOTIFICATIONS_SETTINGS_OFF_FOR_APPS_TOOLTIP,
  102. disabled_count));
  103. } else {
  104. button_->SetSubLabel(l10n_util::GetStringUTF16(
  105. IDS_ASH_STATUS_TRAY_NOTIFICATIONS_ON_SUBLABEL));
  106. button_->SetLabelTooltip(l10n_util::GetStringUTF16(
  107. IDS_ASH_STATUS_TRAY_NOTIFICATIONS_SETTINGS_ON_TOOLTIP));
  108. }
  109. }
  110. std::u16string QuietModeFeaturePodController::GetQuietModeStateTooltip() {
  111. return l10n_util::GetStringUTF16(
  112. MessageCenter::Get()->IsQuietMode()
  113. ? IDS_ASH_STATUS_TRAY_NOTIFICATIONS_DO_NOT_DISTURB_ON_STATE
  114. : IDS_ASH_STATUS_TRAY_NOTIFICATIONS_DO_NOT_DISTURB_OFF_STATE);
  115. }
  116. void QuietModeFeaturePodController::RecordDisabledNotifierCount(
  117. int disabled_count) {
  118. if (!last_disabled_count_.has_value()) {
  119. last_disabled_count_ = disabled_count;
  120. UMA_HISTOGRAM_COUNTS_100("ChromeOS.SystemTray.BlockedNotifiersOnOpen",
  121. disabled_count);
  122. return;
  123. }
  124. if (*last_disabled_count_ == disabled_count)
  125. return;
  126. last_disabled_count_ = disabled_count;
  127. UMA_HISTOGRAM_COUNTS_100("ChromeOS.SystemTray.BlockedNotifiersAfterUpdate",
  128. disabled_count);
  129. }
  130. } // namespace ash