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" | 7 #include "athena/screen/public/screen_manager.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
8 #include "chromeos/dbus/dbus_thread_manager.h" | 9 #include "chromeos/dbus/dbus_thread_manager.h" |
9 #include "ui/compositor/layer_animator.h" | 10 #include "ui/gfx/font_list.h" |
10 #include "ui/compositor/scoped_layer_animation_settings.h" | 11 #include "ui/views/background.h" |
| 12 #include "ui/views/border.h" |
| 13 #include "ui/views/controls/label.h" |
| 14 #include "ui/views/layout/box_layout.h" |
| 15 #include "ui/views/widget/widget.h" |
11 | 16 |
12 namespace athena { | 17 namespace athena { |
13 namespace { | 18 namespace { |
14 | 19 |
15 // Duration of the shutdown animation. | 20 // The amount of time that the power button must be held to show the shutdown |
16 const int kShutdownDurationMs = 1000; | 21 // warning dialog. |
| 22 const int kShowShutdownWarningTimeoutMs = 1000; |
17 | 23 |
18 // Duration of the cancel shutdown animation. | 24 // The amount of time that the power button must be held to shut down the |
19 const int kCancelShutdownDurationMs = 500; | 25 // device. |
| 26 const int kShutdownTimeoutMs = 5000; |
20 | 27 |
21 } // namespace | 28 } // namespace |
22 | 29 |
23 PowerButtonController::PowerButtonController() | 30 PowerButtonController::PowerButtonController(aura::Window* dialog_container) |
24 : brightness_is_zero_(false), | 31 : warning_message_container_(dialog_container), |
| 32 brightness_is_zero_(false), |
25 state_(STATE_OTHER) { | 33 state_(STATE_OTHER) { |
26 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( | 34 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( |
27 this); | 35 this); |
28 } | 36 } |
29 | 37 |
30 PowerButtonController::~PowerButtonController() { | 38 PowerButtonController::~PowerButtonController() { |
31 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | 39 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
32 this); | 40 this); |
33 } | 41 } |
34 | 42 |
35 void PowerButtonController::StartGrayscaleAndBrightnessAnimation( | 43 void PowerButtonController::ShowShutdownWarningDialog() { |
36 float target, | 44 state_ = STATE_SHUTDOWN_WARNING_VISIBLE; |
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); | |
47 | 45 |
48 animator->SetBrightness(target); | 46 shutdown_warning_message_.reset(new views::Widget); |
49 animator->SetGrayscale(target); | 47 |
| 48 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); |
| 49 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 50 params.parent = warning_message_container_; |
| 51 shutdown_warning_message_->Init(params); |
| 52 |
| 53 // TODO(pkotwicz): Get text from the resource. |
| 54 views::Label* label = new views::Label( |
| 55 base::UTF8ToUTF16("Keep holding power button to shutdown.")); |
| 56 label->SetBackgroundColor(SK_ColorWHITE); |
| 57 label->SetFontList(gfx::FontList().DeriveWithStyle(gfx::Font::BOLD)); |
| 58 |
| 59 views::View* container = new views::View; |
| 60 container->AddChildView(label); |
| 61 |
| 62 const int kBorderSpacing = 50; |
| 63 container->SetLayoutManager(new views::BoxLayout( |
| 64 views::BoxLayout::kHorizontal, kBorderSpacing, kBorderSpacing, 0)); |
| 65 container->set_background( |
| 66 views::Background::CreateSolidBackground(SK_ColorWHITE)); |
| 67 container->SetBorder(views::Border::CreateSolidBorder(1, SK_ColorBLACK)); |
| 68 |
| 69 shutdown_warning_message_->SetContentsView(container); |
| 70 shutdown_warning_message_->CenterWindow(container->GetPreferredSize()); |
| 71 shutdown_warning_message_->Show(); |
| 72 |
| 73 timer_.Start(FROM_HERE, |
| 74 base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs - |
| 75 kShowShutdownWarningTimeoutMs), |
| 76 this, |
| 77 &PowerButtonController::Shutdown); |
| 78 } |
| 79 |
| 80 void PowerButtonController::Shutdown() { |
| 81 state_ = STATE_SHUTDOWN_REQUESTED; |
| 82 chromeos::DBusThreadManager::Get() |
| 83 ->GetPowerManagerClient() |
| 84 ->RequestShutdown(); |
50 } | 85 } |
51 | 86 |
52 void PowerButtonController::BrightnessChanged(int level, bool user_initiated) { | 87 void PowerButtonController::BrightnessChanged(int level, bool user_initiated) { |
53 if (brightness_is_zero_) | 88 if (brightness_is_zero_) |
54 zero_brightness_end_time_ = base::TimeTicks::Now(); | 89 zero_brightness_end_time_ = base::TimeTicks::Now(); |
55 brightness_is_zero_ = (level == 0); | 90 brightness_is_zero_ = (level == 0); |
56 } | 91 } |
57 | 92 |
58 void PowerButtonController::PowerButtonEventReceived( | 93 void PowerButtonController::PowerButtonEventReceived( |
59 bool down, | 94 bool down, |
60 const base::TimeTicks& timestamp) { | 95 const base::TimeTicks& timestamp) { |
61 // Avoid requesting a shutdown if the power button is pressed while the screen | 96 if (state_ == STATE_SHUTDOWN_REQUESTED) |
62 // is off (http://crbug.com/128451) | 97 return; |
| 98 |
| 99 // Avoid requesting suspend or shutdown if the power button is pressed while |
| 100 // the screen is off (http://crbug.com/128451). |
63 base::TimeDelta time_since_zero_brightness = brightness_is_zero_ ? | 101 base::TimeDelta time_since_zero_brightness = brightness_is_zero_ ? |
64 base::TimeDelta() : (base::TimeTicks::Now() - zero_brightness_end_time_); | 102 base::TimeDelta() : (base::TimeTicks::Now() - zero_brightness_end_time_); |
65 const int kShortTimeMs = 10; | 103 const int kShortTimeMs = 10; |
66 if (time_since_zero_brightness.InMilliseconds() <= kShortTimeMs) | 104 if (time_since_zero_brightness.InMilliseconds() <= kShortTimeMs) |
67 return; | 105 return; |
68 | 106 |
69 if (state_ == STATE_SHUTDOWN_REQUESTED) | |
70 return; | |
71 | |
72 StopObservingImplicitAnimations(); | |
73 if (down) { | 107 if (down) { |
74 state_ = STATE_PRE_SHUTDOWN_ANIMATION; | 108 state_ = STATE_SUSPEND_ON_RELEASE; |
75 StartGrayscaleAndBrightnessAnimation( | 109 timer_.Start( |
76 1.0f, kShutdownDurationMs, gfx::Tween::EASE_IN); | 110 FROM_HERE, |
| 111 base::TimeDelta::FromMilliseconds(kShowShutdownWarningTimeoutMs), |
| 112 this, |
| 113 &PowerButtonController::ShowShutdownWarningDialog); |
77 } else { | 114 } else { |
| 115 if (state_ == STATE_SUSPEND_ON_RELEASE) { |
| 116 chromeos::DBusThreadManager::Get() |
| 117 ->GetPowerManagerClient() |
| 118 ->RequestSuspend(); |
| 119 } |
78 state_ = STATE_OTHER; | 120 state_ = STATE_OTHER; |
79 StartGrayscaleAndBrightnessAnimation( | 121 timer_.Stop(); |
80 0.0f, kCancelShutdownDurationMs, gfx::Tween::EASE_IN_OUT); | 122 shutdown_warning_message_.reset(); |
81 } | 123 } |
82 } | 124 } |
83 | 125 |
84 void PowerButtonController::OnImplicitAnimationsCompleted() { | |
85 if (state_ == STATE_PRE_SHUTDOWN_ANIMATION) { | |
86 state_ = STATE_SHUTDOWN_REQUESTED; | |
87 chromeos::DBusThreadManager::Get() | |
88 ->GetPowerManagerClient() | |
89 ->RequestShutdown(); | |
90 } | |
91 } | |
92 | |
93 } // namespace athena | 126 } // namespace athena |
OLD | NEW |