1 // Copyright 2014 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.
5 #include "athena/system/power_button_controller.h"
7 #include "athena/screen/public/screen_manager.h"
8 #include "chromeos/dbus/dbus_thread_manager.h"
9 #include "ui/compositor/layer_animator.h"
10 #include "ui/compositor/scoped_layer_animation_settings.h"
15 // Duration of the shutdown animation.
16 const int kShutdownDurationMs
= 1000;
18 // Duration of the cancel shutdown animation.
19 const int kCancelShutdownDurationMs
= 500;
23 PowerButtonController::PowerButtonController()
24 : brightness_is_zero_(false),
26 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
30 PowerButtonController::~PowerButtonController() {
31 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(
35 void PowerButtonController::StartGrayscaleAndBrightnessAnimation(
38 gfx::Tween::Type tween_type
) {
39 ui::LayerAnimator
* animator
= ScreenManager::Get()->GetScreenAnimator();
40 ui::ScopedLayerAnimationSettings
settings(animator
);
41 settings
.SetTransitionDuration(
42 base::TimeDelta::FromMilliseconds(duration_ms
));
43 settings
.SetTweenType(tween_type
);
44 settings
.SetPreemptionStrategy(
45 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
46 settings
.AddObserver(this);
48 animator
->SetBrightness(target
);
49 animator
->SetGrayscale(target
);
52 void PowerButtonController::BrightnessChanged(int level
, bool user_initiated
) {
53 if (brightness_is_zero_
)
54 zero_brightness_end_time_
= base::TimeTicks::Now();
55 brightness_is_zero_
= (level
== 0);
58 void PowerButtonController::PowerButtonEventReceived(
60 const base::TimeTicks
& timestamp
) {
61 // Avoid requesting a shutdown if the power button is pressed while the screen
62 // is off (http://crbug.com/128451)
63 base::TimeDelta time_since_zero_brightness
= brightness_is_zero_
?
64 base::TimeDelta() : (base::TimeTicks::Now() - zero_brightness_end_time_
);
65 const int kShortTimeMs
= 10;
66 if (time_since_zero_brightness
.InMilliseconds() <= kShortTimeMs
)
69 if (state_
== STATE_SHUTDOWN_REQUESTED
)
72 StopObservingImplicitAnimations();
74 state_
= STATE_PRE_SHUTDOWN_ANIMATION
;
75 StartGrayscaleAndBrightnessAnimation(
76 1.0f
, kShutdownDurationMs
, gfx::Tween::EASE_IN
);
79 StartGrayscaleAndBrightnessAnimation(
80 0.0f
, kCancelShutdownDurationMs
, gfx::Tween::EASE_IN_OUT
);
84 void PowerButtonController::OnImplicitAnimationsCompleted() {
85 if (state_
== STATE_PRE_SHUTDOWN_ANIMATION
) {
86 state_
= STATE_SHUTDOWN_REQUESTED
;
87 chromeos::DBusThreadManager::Get()
88 ->GetPowerManagerClient()