OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include "ash/wm/tablet_power_button_controller.h" |
| 6 |
| 7 #include "ash/common/session/session_state_delegate.h" |
| 8 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h" |
| 9 #include "ash/common/wm_shell.h" |
| 10 #include "ash/shell.h" |
| 11 #include "ash/wm/lock_state_controller.h" |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" |
| 13 #include "chromeos/dbus/power_manager_client.h" |
| 14 |
| 15 namespace ash { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Amount of time the power button must be held to start the pre-shutdown |
| 20 // animation when in tablet mode. |
| 21 constexpr int kTabletShutdownTimeoutMs = 1000; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 TabletPowerButtonController::TestApi::TestApi( |
| 26 TabletPowerButtonController* controller) |
| 27 : controller_(controller) {} |
| 28 |
| 29 TabletPowerButtonController::TestApi::~TestApi() {} |
| 30 |
| 31 bool TabletPowerButtonController::TestApi::TabletPowerButtonTimerIsRunning() |
| 32 const { |
| 33 return controller_->tablet_power_button_timer_.IsRunning(); |
| 34 } |
| 35 |
| 36 void TabletPowerButtonController::TestApi::TriggerTabletPowerButtonTimeout() { |
| 37 controller_->OnTabletPowerButtonTimeout(); |
| 38 controller_->tablet_power_button_timer_.Stop(); |
| 39 } |
| 40 |
| 41 TabletPowerButtonController::TabletPowerButtonController( |
| 42 LockStateController* controller) { |
| 43 controller_ = controller; |
| 44 Shell::GetInstance()->PrependPreTargetHandler(this); |
| 45 GetInitialBacklightsForcedOff(); |
| 46 } |
| 47 |
| 48 TabletPowerButtonController::~TabletPowerButtonController() { |
| 49 Shell::GetInstance()->RemovePreTargetHandler(this); |
| 50 } |
| 51 |
| 52 bool TabletPowerButtonController::ShouldHandlePowerButtonEvents() const { |
| 53 return IsTabletModeSupported(); |
| 54 } |
| 55 |
| 56 void TabletPowerButtonController::OnPowerButtonEvent( |
| 57 bool down, |
| 58 const base::TimeTicks& timestamp, |
| 59 bool brightness_is_zero) { |
| 60 if (down) { |
| 61 power_button_down_while_screen_off_ = brightness_is_zero; |
| 62 SetBacklightsForcedOff(false); |
| 63 StartTabletPowerButtonTimer(); |
| 64 } else { |
| 65 if (tablet_power_button_timer_.IsRunning()) { |
| 66 tablet_power_button_timer_.Stop(); |
| 67 if (!power_button_down_while_screen_off_) { |
| 68 SetBacklightsForcedOff(true); |
| 69 LockScreenIfRequired(); |
| 70 } |
| 71 } |
| 72 power_button_down_while_screen_off_ = false; |
| 73 |
| 74 // When power button is released, Cancel Shutdown animation whenever it is |
| 75 // still cancellable. |
| 76 if (controller_->CanCancelShutdownAnimation()) |
| 77 controller_->CancelShutdownAnimation(); |
| 78 } |
| 79 } |
| 80 |
| 81 void TabletPowerButtonController::OnKeyEvent(ui::KeyEvent* event) { |
| 82 if (!IsTabletModeActive() && backlights_forced_off_) |
| 83 SetBacklightsForcedOff(false); |
| 84 } |
| 85 |
| 86 void TabletPowerButtonController::OnMouseEvent(ui::MouseEvent* event) { |
| 87 if (!IsTabletModeActive() && backlights_forced_off_) |
| 88 SetBacklightsForcedOff(false); |
| 89 } |
| 90 |
| 91 void TabletPowerButtonController::SetBacklightsForcedOff(bool forced_off) { |
| 92 if (backlights_forced_off_ == forced_off) |
| 93 return; |
| 94 |
| 95 chromeos::DBusThreadManager::Get() |
| 96 ->GetPowerManagerClient() |
| 97 ->SetBacklightsForcedOff(forced_off); |
| 98 backlights_forced_off_ = forced_off; |
| 99 } |
| 100 |
| 101 void TabletPowerButtonController::GetInitialBacklightsForcedOff() { |
| 102 chromeos::DBusThreadManager::Get() |
| 103 ->GetPowerManagerClient() |
| 104 ->GetBacklightsForcedOff(base::Bind( |
| 105 &TabletPowerButtonController::OnGotInitialBacklightsForcedOff, |
| 106 base::Unretained(this))); |
| 107 } |
| 108 |
| 109 void TabletPowerButtonController::OnGotInitialBacklightsForcedOff( |
| 110 bool is_forced_off) { |
| 111 backlights_forced_off_ = is_forced_off; |
| 112 } |
| 113 |
| 114 void TabletPowerButtonController::StartTabletPowerButtonTimer() { |
| 115 tablet_power_button_timer_.Start( |
| 116 FROM_HERE, base::TimeDelta::FromMilliseconds(kTabletShutdownTimeoutMs), |
| 117 this, &TabletPowerButtonController::OnTabletPowerButtonTimeout); |
| 118 } |
| 119 |
| 120 void TabletPowerButtonController::OnTabletPowerButtonTimeout() { |
| 121 controller_->StartShutdownAnimation(); |
| 122 } |
| 123 |
| 124 void TabletPowerButtonController::LockScreenIfRequired() { |
| 125 SessionStateDelegate* session_state_delegate = |
| 126 WmShell::Get()->GetSessionStateDelegate(); |
| 127 if (session_state_delegate->ShouldLockScreenAutomatically() && |
| 128 session_state_delegate->CanLockScreen() && |
| 129 !session_state_delegate->IsUserSessionBlocked() && |
| 130 !controller_->LockRequested()) { |
| 131 session_state_delegate->LockScreen(); |
| 132 } |
| 133 } |
| 134 |
| 135 bool TabletPowerButtonController::IsTabletModeSupported() const { |
| 136 MaximizeModeController* maximize_mode_controller = |
| 137 WmShell::Get()->maximize_mode_controller(); |
| 138 return maximize_mode_controller && |
| 139 maximize_mode_controller->CanEnterMaximizeMode(); |
| 140 } |
| 141 |
| 142 bool TabletPowerButtonController::IsTabletModeActive() const { |
| 143 MaximizeModeController* maximize_mode_controller = |
| 144 WmShell::Get()->maximize_mode_controller(); |
| 145 return maximize_mode_controller && |
| 146 maximize_mode_controller->IsMaximizeModeWindowManagerEnabled(); |
| 147 } |
| 148 |
| 149 } // namespace ash |
OLD | NEW |