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 pressed 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 pressed 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() { |
flackr
2014/09/19 14:45:29
I think when this shows we should no longer suspen
| |
36 float target, | 44 shutdown_warning_message_.reset(new views::Widget); |
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 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); |
49 animator->SetGrayscale(target); | 47 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
48 params.parent = warning_message_container_; | |
49 shutdown_warning_message_->Init(params); | |
50 | |
51 views::Label* label = new views::Label( | |
52 base::UTF8ToUTF16("Keep pressing power button to shutdown.")); | |
flackr
2014/09/19 14:45:29
nit s/pressing/holding
| |
53 label->SetBackgroundColor(SK_ColorWHITE); | |
54 label->SetFontList(gfx::FontList().DeriveWithStyle(gfx::Font::BOLD)); | |
55 | |
56 views::View* container = new views::View; | |
57 container->AddChildView(label); | |
58 | |
59 const int kBorderSpacing = 50; | |
60 container->SetLayoutManager(new views::BoxLayout( | |
61 views::BoxLayout::kHorizontal, kBorderSpacing, kBorderSpacing, 0)); | |
62 container->set_background( | |
63 views::Background::CreateSolidBackground(SK_ColorWHITE)); | |
64 container->SetBorder(views::Border::CreateSolidBorder(1, SK_ColorBLACK)); | |
65 | |
66 shutdown_warning_message_->SetContentsView(container); | |
67 shutdown_warning_message_->CenterWindow(container->GetPreferredSize()); | |
68 shutdown_warning_message_->Show(); | |
69 | |
70 timer_.Start(FROM_HERE, | |
71 base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs - | |
72 kShowShutdownWarningTimeoutMs), | |
73 this, | |
74 &PowerButtonController::Shutdown); | |
75 } | |
76 | |
77 void PowerButtonController::Shutdown() { | |
78 state_ = STATE_SHUTDOWN_REQUESTED; | |
79 chromeos::DBusThreadManager::Get() | |
80 ->GetPowerManagerClient() | |
81 ->RequestShutdown(); | |
50 } | 82 } |
51 | 83 |
52 void PowerButtonController::BrightnessChanged(int level, bool user_initiated) { | 84 void PowerButtonController::BrightnessChanged(int level, bool user_initiated) { |
53 if (brightness_is_zero_) | 85 if (brightness_is_zero_) |
54 zero_brightness_end_time_ = base::TimeTicks::Now(); | 86 zero_brightness_end_time_ = base::TimeTicks::Now(); |
55 brightness_is_zero_ = (level == 0); | 87 brightness_is_zero_ = (level == 0); |
56 } | 88 } |
57 | 89 |
58 void PowerButtonController::PowerButtonEventReceived( | 90 void PowerButtonController::PowerButtonEventReceived( |
59 bool down, | 91 bool down, |
60 const base::TimeTicks& timestamp) { | 92 const base::TimeTicks& timestamp) { |
61 // Avoid requesting a shutdown if the power button is pressed while the screen | 93 // Avoid requesting suspend or shutdown if the power button is pressed while |
62 // is off (http://crbug.com/128451) | 94 // the screen is off (http://crbug.com/128451). |
63 base::TimeDelta time_since_zero_brightness = brightness_is_zero_ ? | 95 base::TimeDelta time_since_zero_brightness = brightness_is_zero_ ? |
64 base::TimeDelta() : (base::TimeTicks::Now() - zero_brightness_end_time_); | 96 base::TimeDelta() : (base::TimeTicks::Now() - zero_brightness_end_time_); |
65 const int kShortTimeMs = 10; | 97 const int kShortTimeMs = 10; |
66 if (time_since_zero_brightness.InMilliseconds() <= kShortTimeMs) | 98 if (time_since_zero_brightness.InMilliseconds() <= kShortTimeMs) |
67 return; | 99 return; |
68 | 100 |
69 if (state_ == STATE_SHUTDOWN_REQUESTED) | 101 if (state_ == STATE_SHUTDOWN_REQUESTED) |
70 return; | 102 return; |
71 | 103 |
72 StopObservingImplicitAnimations(); | |
73 if (down) { | 104 if (down) { |
74 state_ = STATE_PRE_SHUTDOWN_ANIMATION; | 105 state_ = STATE_SUSPEND_ON_RELEASE; |
75 StartGrayscaleAndBrightnessAnimation( | 106 timer_.Start( |
76 1.0f, kShutdownDurationMs, gfx::Tween::EASE_IN); | 107 FROM_HERE, |
108 base::TimeDelta::FromMilliseconds(kShowShutdownWarningTimeoutMs), | |
109 this, | |
110 &PowerButtonController::ShowShutdownWarningDialog); | |
77 } else { | 111 } else { |
112 if (state_ == STATE_SUSPEND_ON_RELEASE) { | |
113 chromeos::DBusThreadManager::Get() | |
114 ->GetPowerManagerClient() | |
115 ->RequestSuspend(); | |
116 } | |
78 state_ = STATE_OTHER; | 117 state_ = STATE_OTHER; |
79 StartGrayscaleAndBrightnessAnimation( | 118 timer_.Stop(); |
80 0.0f, kCancelShutdownDurationMs, gfx::Tween::EASE_IN_OUT); | 119 shutdown_warning_message_.reset(); |
81 } | 120 } |
82 } | 121 } |
83 | 122 |
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 | 123 } // namespace athena |
OLD | NEW |