lock_screen_note_display_state_handler.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2017 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/lock_screen_action/lock_screen_note_display_state_handler.h"
  5. #include <utility>
  6. #include "ash/lock_screen_action/lock_screen_note_launcher.h"
  7. #include "ash/public/mojom/tray_action.mojom.h"
  8. #include "ash/shell.h"
  9. #include "ash/system/power/scoped_backlights_forced_off.h"
  10. #include "ash/tray_action/tray_action.h"
  11. #include "base/bind.h"
  12. #include "base/location.h"
  13. #include "base/time/time.h"
  14. namespace ash {
  15. namespace {
  16. // The max amount of time display state change handling can be delayed due to a
  17. // lock screen note action launch. The time starts running when the app launch
  18. // is requested.
  19. constexpr base::TimeDelta kNoteLaunchTimeout = base::Milliseconds(1500);
  20. } // namespace
  21. LockScreenNoteDisplayStateHandler::LockScreenNoteDisplayStateHandler(
  22. BacklightsForcedOffSetter* backlights_forced_off_setter)
  23. : backlights_forced_off_setter_(backlights_forced_off_setter),
  24. backlights_forced_off_observation_(this) {
  25. backlights_forced_off_observation_.Observe(backlights_forced_off_setter_);
  26. }
  27. LockScreenNoteDisplayStateHandler::~LockScreenNoteDisplayStateHandler() =
  28. default;
  29. void LockScreenNoteDisplayStateHandler::OnBacklightsForcedOffChanged(
  30. bool backlights_forced_off) {
  31. // Close lock screen note when backlights are forced off - unless the
  32. // backlights are forced off by this as part of note app launch.
  33. if (backlights_forced_off && !backlights_forced_off_) {
  34. Shell::Get()->tray_action()->CloseLockScreenNote(
  35. mojom::CloseLockScreenNoteReason::kScreenDimmed);
  36. }
  37. }
  38. void LockScreenNoteDisplayStateHandler::OnScreenBacklightStateChanged(
  39. ScreenBacklightState screen_backlight_state) {
  40. if (screen_backlight_state != ScreenBacklightState::ON &&
  41. note_launch_delayed_until_screen_off_) {
  42. RunLockScreenNoteLauncher();
  43. }
  44. }
  45. void LockScreenNoteDisplayStateHandler::AttemptNoteLaunchForStylusEject() {
  46. if (!LockScreenNoteLauncher::CanAttemptLaunch() ||
  47. NoteLaunchInProgressOrDelayed()) {
  48. return;
  49. }
  50. if (!backlights_forced_off_ && ShouldForceBacklightsOffForNoteLaunch()) {
  51. backlights_forced_off_ =
  52. backlights_forced_off_setter_->ForceBacklightsOff();
  53. }
  54. DCHECK(!launch_timer_.IsRunning());
  55. launch_timer_.Start(
  56. FROM_HERE, kNoteLaunchTimeout,
  57. base::BindOnce(&LockScreenNoteDisplayStateHandler::NoteLaunchDone,
  58. weak_ptr_factory_.GetWeakPtr(), false));
  59. // Delay note launch if backlights are forced off, but the screen hasn't
  60. // been turned off yet - the note should be launched when the pending
  61. // backlights state is finished (i.e. the screen is turned off).
  62. if (backlights_forced_off_setter_->backlights_forced_off() &&
  63. backlights_forced_off_setter_->GetScreenBacklightState() ==
  64. ScreenBacklightState::ON) {
  65. note_launch_delayed_until_screen_off_ = true;
  66. return;
  67. }
  68. RunLockScreenNoteLauncher();
  69. }
  70. void LockScreenNoteDisplayStateHandler::Reset() {
  71. note_launch_delayed_until_screen_off_ = false;
  72. backlights_forced_off_.reset();
  73. lock_screen_note_launcher_.reset();
  74. launch_timer_.Stop();
  75. }
  76. void LockScreenNoteDisplayStateHandler::RunLockScreenNoteLauncher() {
  77. DCHECK(!lock_screen_note_launcher_);
  78. if (!LockScreenNoteLauncher::CanAttemptLaunch()) {
  79. Reset();
  80. return;
  81. }
  82. note_launch_delayed_until_screen_off_ = false;
  83. lock_screen_note_launcher_ = std::make_unique<LockScreenNoteLauncher>();
  84. lock_screen_note_launcher_->Run(
  85. mojom::LockScreenNoteOrigin::kStylusEject,
  86. base::BindOnce(&LockScreenNoteDisplayStateHandler::NoteLaunchDone,
  87. weak_ptr_factory_.GetWeakPtr()));
  88. }
  89. bool LockScreenNoteDisplayStateHandler::ShouldForceBacklightsOffForNoteLaunch()
  90. const {
  91. // Backlights should be kept off during app launch if the display has been
  92. // turned off without user interaction (e.g. due to user inactivity), or if
  93. // the backlights are currently being forced off - the goal is to avoid flash
  94. // of lock screen UI if the display gets turned on before lock screen app
  95. // window is shown.
  96. // There is no need to force the backlight off if the display has been turned
  97. // off due to user action - in this case display brightness will not change
  98. // when backlights stop being forced off (due to stylus eject) - the
  99. // brightness will remain at user selected level, so the lock screen UI will
  100. // not actually become visible.
  101. //
  102. // Note that backlights_forced_off_setter_ check is required as there is a
  103. // delay between request to force backlights off and screen state getting
  104. // updated due to that request.
  105. return backlights_forced_off_setter_->backlights_forced_off() ||
  106. backlights_forced_off_setter_->GetScreenBacklightState() ==
  107. ScreenBacklightState::OFF_AUTO;
  108. }
  109. bool LockScreenNoteDisplayStateHandler::NoteLaunchInProgressOrDelayed() const {
  110. return note_launch_delayed_until_screen_off_ || lock_screen_note_launcher_;
  111. }
  112. void LockScreenNoteDisplayStateHandler::NoteLaunchDone(bool success) {
  113. Reset();
  114. }
  115. } // namespace ash