adaptive_charging_notification_controller.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2022 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/power/adaptive_charging_notification_controller.h"
  5. #include <string>
  6. #include "ash/constants/ash_pref_names.h"
  7. #include "ash/public/cpp/notification_utils.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/style/dark_light_mode_controller_impl.h"
  13. #include "base/i18n/time_formatting.h"
  14. #include "base/notreached.h"
  15. #include "chromeos/dbus/power/power_manager_client.h"
  16. #include "components/prefs/pref_service.h"
  17. #include "third_party/abseil-cpp/absl/types/optional.h"
  18. #include "ui/base/l10n/l10n_util.h"
  19. #include "ui/message_center/message_center.h"
  20. namespace ash {
  21. namespace {
  22. constexpr char kNotifierId[] = "adaptive-charging-notify";
  23. constexpr char kInfoNotificationId[] = "adaptive-charging-notify-info";
  24. constexpr base::TimeDelta kTimeDeltaRoundingInterval = base::Minutes(30);
  25. } // namespace
  26. AdaptiveChargingNotificationController::
  27. AdaptiveChargingNotificationController() = default;
  28. AdaptiveChargingNotificationController::
  29. ~AdaptiveChargingNotificationController() = default;
  30. void AdaptiveChargingNotificationController::ShowAdaptiveChargingNotification(
  31. absl::optional<int> hours_to_full) {
  32. if (!ShouldShowNotification())
  33. return;
  34. std::u16string notification_message;
  35. if (hours_to_full.has_value()) {
  36. DCHECK_GE(hours_to_full.value(), 0);
  37. notification_message = l10n_util::GetStringFUTF16(
  38. IDS_ASH_ADAPTIVE_CHARGING_NOTIFICATION_MESSAGE_TEMPORARY,
  39. base::TimeFormatTimeOfDayWithHourClockType(
  40. base::Time::FromDeltaSinceWindowsEpoch(
  41. base::Time::Now().ToDeltaSinceWindowsEpoch().RoundToMultiple(
  42. kTimeDeltaRoundingInterval)) +
  43. base::Hours(hours_to_full.value()),
  44. base::GetHourClockType(), base::kKeepAmPm));
  45. } else {
  46. notification_message = l10n_util::GetStringUTF16(
  47. IDS_ASH_ADAPTIVE_CHARGING_NOTIFICATION_MESSAGE_INDEFINITE);
  48. }
  49. message_center::RichNotificationData notification_data;
  50. notification_data.buttons.push_back(
  51. message_center::ButtonInfo(l10n_util::GetStringUTF16(
  52. IDS_ASH_ADAPTIVE_CHARGING_NOTIFICATION_FULLY_CHARGE_NOW_BUTTON_TEXT)));
  53. auto notification = CreateSystemNotification(
  54. message_center::NOTIFICATION_TYPE_SIMPLE, kInfoNotificationId,
  55. l10n_util::GetStringUTF16(IDS_ASH_ADAPTIVE_CHARGING_NOTIFICATION_TITLE),
  56. notification_message,
  57. /*display_source=*/std::u16string(), /*origin_url=*/GURL(),
  58. message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
  59. kNotifierId,
  60. NotificationCatalogName::kAdaptiveCharging),
  61. notification_data,
  62. base::MakeRefCounted<message_center::ThunkNotificationDelegate>(
  63. weak_ptr_factory_.GetWeakPtr()),
  64. kAdaptiveChargingBatteryIcon,
  65. message_center::SystemNotificationWarningLevel::NORMAL);
  66. if (hours_to_full.has_value())
  67. notification->set_priority(message_center::SYSTEM_PRIORITY);
  68. notification->set_accent_color(
  69. DarkLightModeControllerImpl::Get()->IsDarkModeEnabled()
  70. ? gfx::kGoogleGreen300
  71. : gfx::kGoogleGreen600);
  72. message_center::MessageCenter::Get()->AddNotification(
  73. std::move(notification));
  74. }
  75. void AdaptiveChargingNotificationController::CloseAdaptiveChargingNotification(
  76. bool by_user) {
  77. message_center::MessageCenter::Get()->RemoveNotification(kInfoNotificationId,
  78. by_user);
  79. }
  80. bool AdaptiveChargingNotificationController::ShouldShowNotification() {
  81. PrefService* pref_service =
  82. Shell::Get()->session_controller()->GetActivePrefService();
  83. return pref_service &&
  84. pref_service->GetBoolean(ash::prefs::kPowerAdaptiveChargingEnabled);
  85. }
  86. void AdaptiveChargingNotificationController::Click(
  87. const absl::optional<int>& button_index,
  88. const absl::optional<std::u16string>& reply) {
  89. if (!button_index.has_value())
  90. return;
  91. if (button_index.value() == 0) {
  92. PowerManagerClient::Get()->ChargeNowForAdaptiveCharging();
  93. CloseAdaptiveChargingNotification(/*by_user=*/true);
  94. } else {
  95. NOTREACHED() << "Unknown button index value";
  96. }
  97. }
  98. } // namespace ash