Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: athena/system/power_button_controller.cc

Issue 585623003: [Athena] Make a quick power button tap suspend the device (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 views::Label* label = new views::Label(
54 base::UTF8ToUTF16("Keep holding power button to shutdown."));
oshima 2014/09/22 17:20:19 TODO to use IDS
55 label->SetBackgroundColor(SK_ColorWHITE);
56 label->SetFontList(gfx::FontList().DeriveWithStyle(gfx::Font::BOLD));
57
58 views::View* container = new views::View;
59 container->AddChildView(label);
60
61 const int kBorderSpacing = 50;
62 container->SetLayoutManager(new views::BoxLayout(
63 views::BoxLayout::kHorizontal, kBorderSpacing, kBorderSpacing, 0));
64 container->set_background(
65 views::Background::CreateSolidBackground(SK_ColorWHITE));
66 container->SetBorder(views::Border::CreateSolidBorder(1, SK_ColorBLACK));
67
68 shutdown_warning_message_->SetContentsView(container);
69 shutdown_warning_message_->CenterWindow(container->GetPreferredSize());
70 shutdown_warning_message_->Show();
71
72 timer_.Start(FROM_HERE,
73 base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs -
74 kShowShutdownWarningTimeoutMs),
75 this,
76 &PowerButtonController::Shutdown);
77 }
78
79 void PowerButtonController::Shutdown() {
80 state_ = STATE_SHUTDOWN_REQUESTED;
81 chromeos::DBusThreadManager::Get()
82 ->GetPowerManagerClient()
83 ->RequestShutdown();
50 } 84 }
51 85
52 void PowerButtonController::BrightnessChanged(int level, bool user_initiated) { 86 void PowerButtonController::BrightnessChanged(int level, bool user_initiated) {
53 if (brightness_is_zero_) 87 if (brightness_is_zero_)
54 zero_brightness_end_time_ = base::TimeTicks::Now(); 88 zero_brightness_end_time_ = base::TimeTicks::Now();
55 brightness_is_zero_ = (level == 0); 89 brightness_is_zero_ = (level == 0);
56 } 90 }
57 91
58 void PowerButtonController::PowerButtonEventReceived( 92 void PowerButtonController::PowerButtonEventReceived(
59 bool down, 93 bool down,
60 const base::TimeTicks& timestamp) { 94 const base::TimeTicks& timestamp) {
61 // Avoid requesting a shutdown if the power button is pressed while the screen 95 // Avoid requesting suspend or shutdown if the power button is pressed while
62 // is off (http://crbug.com/128451) 96 // the screen is off (http://crbug.com/128451).
63 base::TimeDelta time_since_zero_brightness = brightness_is_zero_ ? 97 base::TimeDelta time_since_zero_brightness = brightness_is_zero_ ?
64 base::TimeDelta() : (base::TimeTicks::Now() - zero_brightness_end_time_); 98 base::TimeDelta() : (base::TimeTicks::Now() - zero_brightness_end_time_);
65 const int kShortTimeMs = 10; 99 const int kShortTimeMs = 10;
66 if (time_since_zero_brightness.InMilliseconds() <= kShortTimeMs) 100 if (time_since_zero_brightness.InMilliseconds() <= kShortTimeMs)
67 return; 101 return;
68 102
69 if (state_ == STATE_SHUTDOWN_REQUESTED) 103 if (state_ == STATE_SHUTDOWN_REQUESTED)
70 return; 104 return;
oshima 2014/09/22 17:20:18 can you move this to the top of the method?
71 105
72 StopObservingImplicitAnimations();
73 if (down) { 106 if (down) {
74 state_ = STATE_PRE_SHUTDOWN_ANIMATION; 107 state_ = STATE_SUSPEND_ON_RELEASE;
75 StartGrayscaleAndBrightnessAnimation( 108 timer_.Start(
76 1.0f, kShutdownDurationMs, gfx::Tween::EASE_IN); 109 FROM_HERE,
110 base::TimeDelta::FromMilliseconds(kShowShutdownWarningTimeoutMs),
111 this,
112 &PowerButtonController::ShowShutdownWarningDialog);
77 } else { 113 } else {
114 if (state_ == STATE_SUSPEND_ON_RELEASE) {
115 chromeos::DBusThreadManager::Get()
116 ->GetPowerManagerClient()
117 ->RequestSuspend();
118 }
78 state_ = STATE_OTHER; 119 state_ = STATE_OTHER;
79 StartGrayscaleAndBrightnessAnimation( 120 timer_.Stop();
80 0.0f, kCancelShutdownDurationMs, gfx::Tween::EASE_IN_OUT); 121 shutdown_warning_message_.reset();
81 } 122 }
82 } 123 }
83 124
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 125 } // namespace athena
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698