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 "base/strings/utf_string_conversions.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "ui/gfx/font_list.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"
20 // The amount of time that the power button must be held to show the shutdown
22 const int kShowShutdownWarningTimeoutMs
= 1000;
24 // The amount of time that the power button must be held to shut down the
26 const int kShutdownTimeoutMs
= 5000;
30 PowerButtonController::PowerButtonController(aura::Window
* dialog_container
)
31 : warning_message_container_(dialog_container
),
32 brightness_is_zero_(false),
34 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
38 PowerButtonController::~PowerButtonController() {
39 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(
43 void PowerButtonController::ShowShutdownWarningDialog() {
44 state_
= STATE_SHUTDOWN_WARNING_VISIBLE
;
46 shutdown_warning_message_
.reset(new views::Widget
);
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
);
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
));
59 views::View
* container
= new views::View
;
60 container
->AddChildView(label
);
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
));
69 shutdown_warning_message_
->SetContentsView(container
);
70 shutdown_warning_message_
->CenterWindow(container
->GetPreferredSize());
71 shutdown_warning_message_
->Show();
73 timer_
.Start(FROM_HERE
,
74 base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs
-
75 kShowShutdownWarningTimeoutMs
),
77 &PowerButtonController::Shutdown
);
80 void PowerButtonController::Shutdown() {
81 state_
= STATE_SHUTDOWN_REQUESTED
;
82 chromeos::DBusThreadManager::Get()
83 ->GetPowerManagerClient()
87 void PowerButtonController::BrightnessChanged(int level
, bool user_initiated
) {
88 if (brightness_is_zero_
)
89 zero_brightness_end_time_
= base::TimeTicks::Now();
90 brightness_is_zero_
= (level
== 0);
93 void PowerButtonController::PowerButtonEventReceived(
95 const base::TimeTicks
& timestamp
) {
96 if (state_
== STATE_SHUTDOWN_REQUESTED
)
99 // Avoid requesting suspend or shutdown if the power button is pressed while
100 // the screen is off (http://crbug.com/128451).
101 base::TimeDelta time_since_zero_brightness
= brightness_is_zero_
?
102 base::TimeDelta() : (base::TimeTicks::Now() - zero_brightness_end_time_
);
103 const int kShortTimeMs
= 10;
104 if (time_since_zero_brightness
.InMilliseconds() <= kShortTimeMs
)
108 state_
= STATE_SUSPEND_ON_RELEASE
;
111 base::TimeDelta::FromMilliseconds(kShowShutdownWarningTimeoutMs
),
113 &PowerButtonController::ShowShutdownWarningDialog
);
115 if (state_
== STATE_SUSPEND_ON_RELEASE
) {
116 chromeos::DBusThreadManager::Get()
117 ->GetPowerManagerClient()
120 state_
= STATE_OTHER
;
122 shutdown_warning_message_
.reset();
126 } // namespace athena