adaptive_charging_controller.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_controller.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "base/scoped_observation.h"
  7. #include "chromeos/dbus/power/power_manager_client.h"
  8. #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
  9. namespace ash {
  10. namespace {
  11. #if DCHECK_IS_ON()
  12. // Fake input for notification testing.
  13. constexpr int kFakeNotificationInputForTesting = 8;
  14. #endif // DCHECK_IS_ON()
  15. } // namespace
  16. AdaptiveChargingController::AdaptiveChargingController()
  17. : nudge_controller_(std::make_unique<AdaptiveChargingNudgeController>()),
  18. notification_controller_(
  19. std::make_unique<AdaptiveChargingNotificationController>()) {
  20. power_manager_observation_.Observe(chromeos::PowerManagerClient::Get());
  21. }
  22. AdaptiveChargingController::~AdaptiveChargingController() = default;
  23. bool AdaptiveChargingController::IsAdaptiveChargingSupported() {
  24. const absl::optional<power_manager::PowerSupplyProperties>&
  25. power_supply_proto = chromeos::PowerManagerClient::Get()->GetLastStatus();
  26. return power_supply_proto.has_value() &&
  27. power_supply_proto->adaptive_charging_supported();
  28. }
  29. void AdaptiveChargingController::PowerChanged(
  30. const power_manager::PowerSupplyProperties& proto) {
  31. bool is_on_charger_now = false;
  32. if (proto.has_external_power()) {
  33. is_on_charger_now =
  34. proto.external_power() == power_manager::PowerSupplyProperties::AC;
  35. }
  36. #if DCHECK_IS_ON()
  37. if (features::IsAdaptiveChargingForTestingEnabled()) {
  38. if (!is_on_charger_ && is_on_charger_now) {
  39. nudge_controller_->ShowNudgeForTesting(); // IN-TEST
  40. notification_controller_->ShowAdaptiveChargingNotification(
  41. kFakeNotificationInputForTesting);
  42. }
  43. is_on_charger_ = is_on_charger_now;
  44. return;
  45. }
  46. #endif // DCHECK_IS_ON()
  47. // Nudge and notification should be shown only if heuristic is enabled for
  48. // this user.
  49. if (proto.has_adaptive_charging_heuristic_enabled() &&
  50. !proto.adaptive_charging_heuristic_enabled()) {
  51. // |is_adaptive_delaying_charge_| is set to false when there is no
  52. // heuristic enabled. This is to make sure quick settings gets correct
  53. // information.
  54. is_adaptive_delaying_charge_ = false;
  55. notification_controller_->CloseAdaptiveChargingNotification();
  56. return;
  57. }
  58. // Showing educational nudge.
  59. if (proto.has_adaptive_charging_heuristic_enabled() &&
  60. proto.adaptive_charging_heuristic_enabled() && !is_on_charger_ &&
  61. is_on_charger_now)
  62. nudge_controller_->ShowNudge();
  63. is_on_charger_ = is_on_charger_now;
  64. // Return if this change does not contain any adaptive_delaying_charge info.
  65. if (!proto.has_adaptive_delaying_charge())
  66. return;
  67. // We only care about the change in this field.
  68. if (is_adaptive_delaying_charge_ == proto.adaptive_delaying_charge())
  69. return;
  70. is_adaptive_delaying_charge_ = proto.adaptive_delaying_charge();
  71. // Notification should be shown only if the adaptive charging is actually
  72. // active.
  73. if (!is_adaptive_delaying_charge_) {
  74. notification_controller_->CloseAdaptiveChargingNotification();
  75. return;
  76. }
  77. if (proto.has_battery_time_to_full_sec() &&
  78. proto.battery_time_to_full_sec() > 0) {
  79. // Converts time to full from second to hours.
  80. notification_controller_->ShowAdaptiveChargingNotification(
  81. static_cast<int>(proto.battery_time_to_full_sec() / 3600));
  82. } else {
  83. notification_controller_->ShowAdaptiveChargingNotification();
  84. }
  85. }
  86. } // namespace ash