stylus_battery_delegate.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/palette/stylus_battery_delegate.h"
  5. #include <string>
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/resources/vector_icons/vector_icons.h"
  8. #include "ash/shell.h"
  9. #include "ash/strings/grit/ash_strings.h"
  10. #include "ash/style/ash_color_id.h"
  11. #include "ash/style/ash_color_provider.h"
  12. #include "ash/system/power/peripheral_battery_listener.h"
  13. #include "ash/system/power/power_status.h"
  14. #include "ash/system/tray/tray_constants.h"
  15. #include "base/time/time.h"
  16. #include "ui/base/l10n/l10n_util.h"
  17. #include "ui/color/color_provider.h"
  18. #include "ui/gfx/color_palette.h"
  19. #include "ui/gfx/paint_vector_icon.h"
  20. namespace ash {
  21. namespace {
  22. // Battery percentage threshold used to label the battery level as Low.
  23. constexpr int kStylusLowBatteryThreshold = 24;
  24. constexpr base::TimeDelta kStylusBatteryStatusStaleThreshold = base::Days(14);
  25. } // namespace
  26. StylusBatteryDelegate::StylusBatteryDelegate() {
  27. if (Shell::Get()->peripheral_battery_listener())
  28. battery_observation_.Observe(Shell::Get()->peripheral_battery_listener());
  29. }
  30. StylusBatteryDelegate::~StylusBatteryDelegate() = default;
  31. SkColor StylusBatteryDelegate::GetColorForBatteryLevel() const {
  32. if (!battery_level_.has_value()) {
  33. return AshColorProvider::Get()->GetContentLayerColor(
  34. AshColorProvider::ContentLayerType::kIconColorWarning);
  35. }
  36. if (battery_level_ <= kStylusLowBatteryThreshold && !IsBatteryCharging()) {
  37. return AshColorProvider::Get()->GetContentLayerColor(
  38. AshColorProvider::ContentLayerType::kIconColorAlert);
  39. }
  40. return AshColorProvider::Get()->GetContentLayerColor(
  41. AshColorProvider::ContentLayerType::kIconColorPrimary);
  42. }
  43. gfx::ImageSkia StylusBatteryDelegate::GetBatteryImage(
  44. ui::ColorProvider* color_provider) const {
  45. PowerStatus::BatteryImageInfo info;
  46. info.charge_percent = battery_level_.value_or(0);
  47. if (IsBatteryCharging()) {
  48. info.icon_badge = &kUnifiedMenuBatteryBoltIcon;
  49. if (features::IsDarkLightModeEnabled()) {
  50. info.badge_outline = &kUnifiedMenuBatteryBoltOutlineMaskIcon;
  51. } else {
  52. info.badge_outline = &kUnifiedMenuBatteryBoltOutlineIcon;
  53. }
  54. }
  55. const SkColor icon_fg_color = GetColorForBatteryLevel();
  56. DCHECK(color_provider);
  57. const SkColor icon_bg_color =
  58. color_provider->GetColor(kColorAshShieldAndBaseOpaque);
  59. return PowerStatus::GetBatteryImage(info, kUnifiedTrayBatteryIconSize,
  60. icon_bg_color, icon_fg_color);
  61. }
  62. gfx::ImageSkia StylusBatteryDelegate::GetBatteryStatusUnknownImage() const {
  63. const SkColor icon_color = AshColorProvider::Get()->GetContentLayerColor(
  64. AshColorProvider::ContentLayerType::kIconColorPrimary);
  65. return gfx::CreateVectorIcon(kStylusBatteryStatusUnknownIcon, icon_color);
  66. }
  67. void StylusBatteryDelegate::SetBatteryUpdateCallback(
  68. Callback battery_update_callback) {
  69. battery_update_callback_ = std::move(battery_update_callback);
  70. }
  71. bool StylusBatteryDelegate::IsBatteryCharging() const {
  72. return battery_charge_status_ ==
  73. PeripheralBatteryListener::BatteryInfo::ChargeStatus::kCharging ||
  74. battery_charge_status_ ==
  75. PeripheralBatteryListener::BatteryInfo::ChargeStatus::kFull;
  76. }
  77. bool StylusBatteryDelegate::IsBatteryLevelLow() const {
  78. if (!battery_level_.has_value())
  79. return false;
  80. return battery_level_ <= kStylusLowBatteryThreshold;
  81. }
  82. bool StylusBatteryDelegate::ShouldShowBatteryStatus() const {
  83. return last_update_timestamp_.has_value() && last_update_eligible_;
  84. }
  85. bool StylusBatteryDelegate::IsBatteryStatusStale() const {
  86. if (!last_update_timestamp_.has_value())
  87. return false;
  88. return (base::TimeTicks::Now() - last_update_timestamp_.value()) >
  89. kStylusBatteryStatusStaleThreshold;
  90. }
  91. bool StylusBatteryDelegate::IsBatteryStatusEligible() const {
  92. return last_update_eligible_;
  93. }
  94. bool StylusBatteryDelegate::IsBatteryInfoValid(
  95. const PeripheralBatteryListener::BatteryInfo& battery) const {
  96. if (battery.type != PeripheralBatteryListener::BatteryInfo::PeripheralType::
  97. kStylusViaCharger &&
  98. battery.type != PeripheralBatteryListener::BatteryInfo::PeripheralType::
  99. kStylusViaScreen) {
  100. return false;
  101. }
  102. if (!battery.last_active_update_timestamp.has_value() ||
  103. !battery.level.has_value()) {
  104. return false;
  105. }
  106. if (last_update_timestamp_.has_value() &&
  107. battery.last_active_update_timestamp < last_update_timestamp_) {
  108. return false;
  109. }
  110. return true;
  111. }
  112. void StylusBatteryDelegate::OnAddingBattery(
  113. const PeripheralBatteryListener::BatteryInfo& battery) {}
  114. void StylusBatteryDelegate::OnRemovingBattery(
  115. const PeripheralBatteryListener::BatteryInfo& battery) {}
  116. void StylusBatteryDelegate::OnUpdatedBatteryLevel(
  117. const PeripheralBatteryListener::BatteryInfo& battery) {
  118. if (!IsBatteryInfoValid(battery))
  119. return;
  120. battery_level_ = battery.level;
  121. battery_charge_status_ = battery.charge_status;
  122. last_update_timestamp_ = battery.last_active_update_timestamp;
  123. last_update_eligible_ = battery.battery_report_eligible;
  124. if (battery_update_callback_)
  125. battery_update_callback_.Run();
  126. }
  127. } // namespace ash