Add UMA reporting to CdmPromise.
[chromium-blink-merge.git] / athena / system / power_button_controller.cc
blobb4195a3061a080c7ea01b60c68061beec61dfa74
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"
12 namespace athena {
13 namespace {
15 // Duration of the shutdown animation.
16 const int kShutdownDurationMs = 1000;
18 // Duration of the cancel shutdown animation.
19 const int kCancelShutdownDurationMs = 500;
21 } // namespace
23 PowerButtonController::PowerButtonController()
24 : brightness_is_zero_(false),
25 state_(STATE_OTHER) {
26 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
27 this);
30 PowerButtonController::~PowerButtonController() {
31 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(
32 this);
35 void PowerButtonController::StartGrayscaleAndBrightnessAnimation(
36 float target,
37 int duration_ms,
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(
59 bool down,
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)
67 return;
69 if (state_ == STATE_SHUTDOWN_REQUESTED)
70 return;
72 StopObservingImplicitAnimations();
73 if (down) {
74 state_ = STATE_PRE_SHUTDOWN_ANIMATION;
75 StartGrayscaleAndBrightnessAnimation(
76 1.0f, kShutdownDurationMs, gfx::Tween::EASE_IN);
77 } else {
78 state_ = STATE_OTHER;
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()
89 ->RequestShutdown();
93 } // namespace athena