gesture_education_notification_controller.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/gesture_education/gesture_education_notification_controller.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/constants/ash_pref_names.h"
  7. #include "ash/constants/notifier_catalogs.h"
  8. #include "ash/public/cpp/notification_utils.h"
  9. #include "ash/public/cpp/shelf_config.h"
  10. #include "ash/public/cpp/system_tray_client.h"
  11. #include "ash/session/session_controller_impl.h"
  12. #include "ash/shell.h"
  13. #include "ash/strings/grit/ash_strings.h"
  14. #include "ash/system/model/system_tray_model.h"
  15. #include "base/bind.h"
  16. #include "components/prefs/pref_registry_simple.h"
  17. #include "components/prefs/pref_service.h"
  18. #include "components/vector_icons/vector_icons.h"
  19. #include "ui/base/l10n/l10n_util.h"
  20. #include "ui/message_center/message_center.h"
  21. #include "ui/message_center/public/cpp/notification.h"
  22. namespace ash {
  23. // static
  24. const char GestureEducationNotificationController::kNotificationId[] =
  25. "chrome://gesture_education";
  26. GestureEducationNotificationController::
  27. GestureEducationNotificationController() {
  28. Shell::Get()->session_controller()->AddObserver(this);
  29. tablet_mode_observation_.Observe(Shell::Get()->tablet_mode_controller());
  30. }
  31. GestureEducationNotificationController::
  32. ~GestureEducationNotificationController() {
  33. Shell::Get()->session_controller()->RemoveObserver(this);
  34. tablet_mode_observation_.Reset();
  35. }
  36. void GestureEducationNotificationController::MaybeShowNotification() {
  37. bool is_user_session_blocked =
  38. Shell::Get()->session_controller()->IsUserSessionBlocked();
  39. if (features::IsHideShelfControlsInTabletModeEnabled() &&
  40. TabletModeController::Get()->InTabletMode() && !is_user_session_blocked &&
  41. (active_user_prefs_ && !active_user_prefs_->GetBoolean(
  42. prefs::kGestureEducationNotificationShown)) &&
  43. !ShelfConfig::Get()->ShelfControlsForcedShownForAccessibility()) {
  44. GenerateGestureEducationNotification();
  45. active_user_prefs_->SetBoolean(prefs::kGestureEducationNotificationShown,
  46. true);
  47. }
  48. }
  49. void GestureEducationNotificationController::OnActiveUserPrefServiceChanged(
  50. PrefService* prefs) {
  51. active_user_prefs_ = prefs;
  52. MaybeShowNotification();
  53. }
  54. void GestureEducationNotificationController::OnSessionStateChanged(
  55. session_manager::SessionState state) {
  56. MaybeShowNotification();
  57. }
  58. void GestureEducationNotificationController::OnTabletModeStarted() {
  59. MaybeShowNotification();
  60. }
  61. void GestureEducationNotificationController::OnTabletControllerDestroyed() {
  62. tablet_mode_observation_.Reset();
  63. }
  64. void GestureEducationNotificationController::RegisterProfilePrefs(
  65. PrefRegistrySimple* registry,
  66. bool for_test) {
  67. // Some tests fail when the notification shows up. Use |for_test|
  68. // as the default value so that it is true in test to suppress the
  69. // notification there.
  70. registry->RegisterBooleanPref(prefs::kGestureEducationNotificationShown,
  71. for_test);
  72. }
  73. void GestureEducationNotificationController::
  74. GenerateGestureEducationNotification() {
  75. std::unique_ptr<message_center::Notification> notification =
  76. CreateSystemNotification(
  77. message_center::NOTIFICATION_TYPE_SIMPLE, kNotificationId,
  78. GetNotificationTitle(), GetNotificationMessage(),
  79. std::u16string() /* display_source */, GURL(),
  80. message_center::NotifierId(
  81. message_center::NotifierType::SYSTEM_COMPONENT, kNotificationId,
  82. NotificationCatalogName::kGestureEducation),
  83. message_center::RichNotificationData(),
  84. base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
  85. base::BindRepeating(&GestureEducationNotificationController::
  86. HandleNotificationClick,
  87. weak_ptr_factory_.GetWeakPtr())),
  88. vector_icons::kSettingsIcon,
  89. message_center::SystemNotificationWarningLevel::NORMAL);
  90. message_center::MessageCenter::Get()->AddNotification(
  91. std::move(notification));
  92. }
  93. void GestureEducationNotificationController::HandleNotificationClick() {
  94. Shell::Get()->system_tray_model()->client()->ShowGestureEducationHelp();
  95. }
  96. void GestureEducationNotificationController::ResetPrefForTest() {
  97. if (active_user_prefs_) {
  98. active_user_prefs_->SetBoolean(prefs::kGestureEducationNotificationShown,
  99. false);
  100. }
  101. }
  102. std::u16string GestureEducationNotificationController::GetNotificationMessage()
  103. const {
  104. return l10n_util::GetStringUTF16(IDS_GESTURE_NOTIFICATION_MESSAGE_LEARN_MORE);
  105. }
  106. std::u16string GestureEducationNotificationController::GetNotificationTitle()
  107. const {
  108. return l10n_util::GetStringUTF16(IDS_GESTURE_NOTIFICATION_TITLE);
  109. }
  110. } // namespace ash