| Index: ash/wm/tablet_power_button_controller.cc
|
| diff --git a/ash/wm/tablet_power_button_controller.cc b/ash/wm/tablet_power_button_controller.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..40a821bd3da0c7ba44468435f9e71ddc294bb2f7
|
| --- /dev/null
|
| +++ b/ash/wm/tablet_power_button_controller.cc
|
| @@ -0,0 +1,149 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "ash/wm/tablet_power_button_controller.h"
|
| +
|
| +#include "ash/common/session/session_state_delegate.h"
|
| +#include "ash/common/wm/maximize_mode/maximize_mode_controller.h"
|
| +#include "ash/common/wm_shell.h"
|
| +#include "ash/shell.h"
|
| +#include "ash/wm/lock_state_controller.h"
|
| +#include "chromeos/dbus/dbus_thread_manager.h"
|
| +#include "chromeos/dbus/power_manager_client.h"
|
| +
|
| +namespace ash {
|
| +
|
| +namespace {
|
| +
|
| +// Amount of time the power button must be held to start the pre-shutdown
|
| +// animation when in tablet mode.
|
| +constexpr int kTabletShutdownTimeoutMs = 1000;
|
| +
|
| +} // namespace
|
| +
|
| +TabletPowerButtonController::TestApi::TestApi(
|
| + TabletPowerButtonController* controller)
|
| + : controller_(controller) {}
|
| +
|
| +TabletPowerButtonController::TestApi::~TestApi() {}
|
| +
|
| +bool TabletPowerButtonController::TestApi::TabletPowerButtonTimerIsRunning()
|
| + const {
|
| + return controller_->tablet_power_button_timer_.IsRunning();
|
| +}
|
| +
|
| +void TabletPowerButtonController::TestApi::TriggerTabletPowerButtonTimeout() {
|
| + controller_->OnTabletPowerButtonTimeout();
|
| + controller_->tablet_power_button_timer_.Stop();
|
| +}
|
| +
|
| +TabletPowerButtonController::TabletPowerButtonController(
|
| + LockStateController* controller) {
|
| + controller_ = controller;
|
| + Shell::GetInstance()->PrependPreTargetHandler(this);
|
| + GetInitialBacklightsForcedOff();
|
| +}
|
| +
|
| +TabletPowerButtonController::~TabletPowerButtonController() {
|
| + Shell::GetInstance()->RemovePreTargetHandler(this);
|
| +}
|
| +
|
| +bool TabletPowerButtonController::ShouldHandlePowerButtonEvents() const {
|
| + return IsTabletModeSupported();
|
| +}
|
| +
|
| +void TabletPowerButtonController::OnPowerButtonEvent(
|
| + bool down,
|
| + const base::TimeTicks& timestamp,
|
| + bool brightness_is_zero) {
|
| + if (down) {
|
| + power_button_down_while_screen_off_ = brightness_is_zero;
|
| + SetBacklightsForcedOff(false);
|
| + StartTabletPowerButtonTimer();
|
| + } else {
|
| + if (tablet_power_button_timer_.IsRunning()) {
|
| + tablet_power_button_timer_.Stop();
|
| + if (!power_button_down_while_screen_off_) {
|
| + SetBacklightsForcedOff(true);
|
| + LockScreenIfRequired();
|
| + }
|
| + }
|
| + power_button_down_while_screen_off_ = false;
|
| +
|
| + // When power button is released, Cancel Shutdown animation whenever it is
|
| + // still cancellable.
|
| + if (controller_->CanCancelShutdownAnimation())
|
| + controller_->CancelShutdownAnimation();
|
| + }
|
| +}
|
| +
|
| +void TabletPowerButtonController::OnKeyEvent(ui::KeyEvent* event) {
|
| + if (!IsTabletModeActive() && backlights_forced_off_)
|
| + SetBacklightsForcedOff(false);
|
| +}
|
| +
|
| +void TabletPowerButtonController::OnMouseEvent(ui::MouseEvent* event) {
|
| + if (!IsTabletModeActive() && backlights_forced_off_)
|
| + SetBacklightsForcedOff(false);
|
| +}
|
| +
|
| +void TabletPowerButtonController::SetBacklightsForcedOff(bool forced_off) {
|
| + if (backlights_forced_off_ == forced_off)
|
| + return;
|
| +
|
| + chromeos::DBusThreadManager::Get()
|
| + ->GetPowerManagerClient()
|
| + ->SetBacklightsForcedOff(forced_off);
|
| + backlights_forced_off_ = forced_off;
|
| +}
|
| +
|
| +void TabletPowerButtonController::GetInitialBacklightsForcedOff() {
|
| + chromeos::DBusThreadManager::Get()
|
| + ->GetPowerManagerClient()
|
| + ->GetBacklightsForcedOff(base::Bind(
|
| + &TabletPowerButtonController::OnGotInitialBacklightsForcedOff,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void TabletPowerButtonController::OnGotInitialBacklightsForcedOff(
|
| + bool is_forced_off) {
|
| + backlights_forced_off_ = is_forced_off;
|
| +}
|
| +
|
| +void TabletPowerButtonController::StartTabletPowerButtonTimer() {
|
| + tablet_power_button_timer_.Start(
|
| + FROM_HERE, base::TimeDelta::FromMilliseconds(kTabletShutdownTimeoutMs),
|
| + this, &TabletPowerButtonController::OnTabletPowerButtonTimeout);
|
| +}
|
| +
|
| +void TabletPowerButtonController::OnTabletPowerButtonTimeout() {
|
| + controller_->StartShutdownAnimation();
|
| +}
|
| +
|
| +void TabletPowerButtonController::LockScreenIfRequired() {
|
| + SessionStateDelegate* session_state_delegate =
|
| + WmShell::Get()->GetSessionStateDelegate();
|
| + if (session_state_delegate->ShouldLockScreenAutomatically() &&
|
| + session_state_delegate->CanLockScreen() &&
|
| + !session_state_delegate->IsUserSessionBlocked() &&
|
| + !controller_->LockRequested()) {
|
| + session_state_delegate->LockScreen();
|
| + }
|
| +}
|
| +
|
| +bool TabletPowerButtonController::IsTabletModeSupported() const {
|
| + MaximizeModeController* maximize_mode_controller =
|
| + WmShell::Get()->maximize_mode_controller();
|
| + return maximize_mode_controller &&
|
| + maximize_mode_controller->CanEnterMaximizeMode();
|
| +}
|
| +
|
| +bool TabletPowerButtonController::IsTabletModeActive() const {
|
| + MaximizeModeController* maximize_mode_controller =
|
| + WmShell::Get()->maximize_mode_controller();
|
| + return maximize_mode_controller &&
|
| + maximize_mode_controller->IsMaximizeModeWindowManagerEnabled();
|
| +}
|
| +
|
| +} // namespace ash
|
|
|