OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "athena/system/power_button_controller.h" | 5 #include "athena/system/power_button_controller.h" |
6 | 6 |
7 #include "athena/screen/public/screen_manager.h" | |
8 #include "base/bind.h" | |
9 #include "base/message_loop/message_loop.h" | |
7 #include "chromeos/dbus/dbus_thread_manager.h" | 10 #include "chromeos/dbus/dbus_thread_manager.h" |
11 #include "ui/aura/window.h" | |
12 #include "ui/compositor/layer.h" | |
13 #include "ui/compositor/scoped_layer_animation_settings.h" | |
14 #include "ui/gfx/animation/tween.h" | |
8 | 15 |
9 namespace athena { | 16 namespace athena { |
10 | 17 |
18 namespace { | |
19 | |
20 // Duration of the animation prior to shutdown. | |
21 const int kPreShutdownDurationMs = 1000; | |
22 | |
23 // Duration of the animation when shutdown is canceled. | |
24 const int kCancelShutdownDurationMs = 500; | |
25 | |
26 // Additional time (beyond kPreShutdownDurationMs) to wait after starting | |
27 // the pre-shutdown animation before actually shutting down to give time for the | |
28 // last frame of the animation to get painted. | |
29 const int kShutdownRequestDelayMs = 50; | |
30 | |
31 } // namespace | |
32 | |
11 PowerButtonController::PowerButtonController() | 33 PowerButtonController::PowerButtonController() |
12 : brightness_is_zero_(false), | 34 : brightness_is_zero_(false), |
13 shutdown_requested_(false) { | 35 shutdown_requested_(false), |
36 can_cancel_shutdown_(true) { | |
14 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( | 37 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( |
15 this); | 38 this); |
16 } | 39 } |
17 | 40 |
18 PowerButtonController::~PowerButtonController() { | 41 PowerButtonController::~PowerButtonController() { |
19 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | 42 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
20 this); | 43 this); |
21 } | 44 } |
22 | 45 |
46 void PowerButtonController::StartGrayscaleAndBrightnessAnimation( | |
47 float target, | |
48 int duration_ms, | |
49 gfx::Tween::Type tween_type) { | |
50 ui::Layer* layer = | |
51 ScreenManager::Get()->GetContext()->GetRootWindow()->layer(); | |
52 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); | |
53 settings.SetTransitionDuration( | |
54 base::TimeDelta::FromMilliseconds(duration_ms)); | |
55 settings.SetTweenType(tween_type); | |
56 settings.SetPreemptionStrategy( | |
57 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
58 settings.AddObserver(this); | |
59 layer->SetLayerBrightness(target); | |
60 layer->SetLayerGrayscale(target); | |
flackr
2014/08/07 18:23:31
Defer to oshima, but I suspect we'll want these an
oshima
2014/08/07 19:53:38
I'm actually fine to implement the animation itsel
| |
61 } | |
62 | |
63 void PowerButtonController::Shutdown() { | |
64 chromeos::DBusThreadManager::Get() | |
65 ->GetPowerManagerClient() | |
66 ->RequestShutdown(); | |
67 } | |
68 | |
23 void PowerButtonController::BrightnessChanged(int level, bool user_initiated) { | 69 void PowerButtonController::BrightnessChanged(int level, bool user_initiated) { |
24 if (brightness_is_zero_) | 70 if (brightness_is_zero_) |
25 zero_brightness_end_time_ = base::TimeTicks::Now(); | 71 zero_brightness_end_time_ = base::TimeTicks::Now(); |
26 brightness_is_zero_ = (level == 0); | 72 brightness_is_zero_ = (level == 0); |
27 } | 73 } |
28 | 74 |
29 void PowerButtonController::PowerButtonEventReceived( | 75 void PowerButtonController::PowerButtonEventReceived( |
30 bool down, | 76 bool down, |
31 const base::TimeTicks& details) { | 77 const base::TimeTicks& timestamp) { |
32 // Avoid requesting a shutdown if the power button is pressed while the screen | 78 // Avoid requesting a shutdown if the power button is pressed while the screen |
33 // is off (http://crbug.com/128451) | 79 // is off (http://crbug.com/128451) |
34 base::TimeDelta time_since_zero_brightness = brightness_is_zero_ ? | 80 base::TimeDelta time_since_zero_brightness = brightness_is_zero_ ? |
35 base::TimeDelta() : (base::TimeTicks::Now() - zero_brightness_end_time_); | 81 base::TimeDelta() : (base::TimeTicks::Now() - zero_brightness_end_time_); |
36 const int kShortTimeMs = 10; | 82 const int kShortTimeMs = 10; |
37 if (time_since_zero_brightness.InMilliseconds() <= kShortTimeMs) | 83 if (time_since_zero_brightness.InMilliseconds() <= kShortTimeMs) |
38 return; | 84 return; |
39 | 85 |
40 if (down && !shutdown_requested_) { | 86 if (!can_cancel_shutdown_) |
flackr
2014/08/07 18:23:31
Seems a bit arbitrary to no longer allow canceling
oshima
2014/08/07 19:53:38
can we use state enum rather than multiple boolean
| |
41 shutdown_requested_ = true; | 87 return; |
42 chromeos::DBusThreadManager::Get() | 88 |
43 ->GetPowerManagerClient() | 89 StopObservingImplicitAnimations(); |
44 ->RequestShutdown(); | 90 shutdown_requested_ = down; |
91 if (shutdown_requested_) { | |
92 StartGrayscaleAndBrightnessAnimation( | |
93 1.0f, kPreShutdownDurationMs, gfx::Tween::EASE_IN); | |
94 } else { | |
95 StartGrayscaleAndBrightnessAnimation( | |
96 0.0f, kCancelShutdownDurationMs, gfx::Tween::EASE_IN_OUT); | |
45 } | 97 } |
46 } | 98 } |
47 | 99 |
100 void PowerButtonController::OnImplicitAnimationsCompleted() { | |
101 if (shutdown_requested_) { | |
102 can_cancel_shutdown_ = false; | |
103 base::MessageLoopForUI::current()->PostDelayedTask( | |
104 FROM_HERE, | |
105 base::Bind(&PowerButtonController::Shutdown, base::Unretained(this)), | |
106 base::TimeDelta::FromMilliseconds(kShutdownRequestDelayMs)); | |
107 } | |
108 } | |
109 | |
48 } // namespace athena | 110 } // namespace athena |
OLD | NEW |