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

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

Issue 654343002: Intorduce PowerButtonObserver API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 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 "athena/strings/grit/athena_strings.h" 8 #include "athena/strings/grit/athena_strings.h"
9 #include "chromeos/dbus/dbus_thread_manager.h" 9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/power_manager_client.h"
10 #include "ui/base/l10n/l10n_util.h" 11 #include "ui/base/l10n/l10n_util.h"
11 #include "ui/gfx/font_list.h" 12 #include "ui/gfx/font_list.h"
12 #include "ui/views/background.h" 13 #include "ui/views/background.h"
13 #include "ui/views/border.h" 14 #include "ui/views/border.h"
14 #include "ui/views/controls/label.h" 15 #include "ui/views/controls/label.h"
15 #include "ui/views/layout/box_layout.h" 16 #include "ui/views/layout/box_layout.h"
16 #include "ui/views/widget/widget.h" 17 #include "ui/views/widget/widget.h"
17 18
18 namespace athena { 19 namespace athena {
19 namespace { 20 namespace {
20 21
21 // The amount of time that the power button must be held to show the shutdown
22 // warning dialog.
23 const int kShowShutdownWarningTimeoutMs = 1000;
24
25 // The amount of time that the power button must be held to shut down the 22 // The amount of time that the power button must be held to shut down the
26 // device. 23 // device after shutdown dialog is shown.
27 const int kShutdownTimeoutMs = 5000; 24 const int kShutdownTimeoutMs = 4000;
28 25
29 } // namespace 26 } // namespace
30 27
31 PowerButtonController::PowerButtonController(aura::Window* dialog_container) 28 PowerButtonController::PowerButtonController(aura::Window* dialog_container)
32 : warning_message_container_(dialog_container), 29 : warning_message_container_(dialog_container),
33 brightness_is_zero_(false),
34 state_(STATE_OTHER) { 30 state_(STATE_OTHER) {
35 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( 31 InputManager::Get()->AddPowerButtonObserver(this);
36 this);
37 } 32 }
38 33
39 PowerButtonController::~PowerButtonController() { 34 PowerButtonController::~PowerButtonController() {
40 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( 35 InputManager::Get()->RemovePowerButtonObserver(this);
41 this);
42 } 36 }
43 37
44 void PowerButtonController::ShowShutdownWarningDialog() { 38 void PowerButtonController::ShowShutdownWarningDialog() {
45 state_ = STATE_SHUTDOWN_WARNING_VISIBLE; 39 state_ = STATE_SHUTDOWN_WARNING_VISIBLE;
46 40
47 shutdown_warning_message_.reset(new views::Widget); 41 shutdown_warning_message_.reset(new views::Widget);
48 42
49 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); 43 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
50 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 44 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
51 params.parent = warning_message_container_; 45 params.parent = warning_message_container_;
(...skipping 10 matching lines...) Expand all
62 const int kBorderSpacing = 50; 56 const int kBorderSpacing = 50;
63 container->SetLayoutManager(new views::BoxLayout( 57 container->SetLayoutManager(new views::BoxLayout(
64 views::BoxLayout::kHorizontal, kBorderSpacing, kBorderSpacing, 0)); 58 views::BoxLayout::kHorizontal, kBorderSpacing, kBorderSpacing, 0));
65 container->set_background( 59 container->set_background(
66 views::Background::CreateSolidBackground(SK_ColorWHITE)); 60 views::Background::CreateSolidBackground(SK_ColorWHITE));
67 container->SetBorder(views::Border::CreateSolidBorder(1, SK_ColorBLACK)); 61 container->SetBorder(views::Border::CreateSolidBorder(1, SK_ColorBLACK));
68 62
69 shutdown_warning_message_->SetContentsView(container); 63 shutdown_warning_message_->SetContentsView(container);
70 shutdown_warning_message_->CenterWindow(container->GetPreferredSize()); 64 shutdown_warning_message_->CenterWindow(container->GetPreferredSize());
71 shutdown_warning_message_->Show(); 65 shutdown_warning_message_->Show();
72
73 timer_.Start(FROM_HERE, 66 timer_.Start(FROM_HERE,
74 base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs - 67 base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs),
75 kShowShutdownWarningTimeoutMs),
76 this, 68 this,
77 &PowerButtonController::Shutdown); 69 &PowerButtonController::Shutdown);
78 } 70 }
79 71
80 void PowerButtonController::Shutdown() { 72 void PowerButtonController::Shutdown() {
81 state_ = STATE_SHUTDOWN_REQUESTED; 73 state_ = STATE_SHUTDOWN_REQUESTED;
82 chromeos::DBusThreadManager::Get() 74 chromeos::DBusThreadManager::Get()
83 ->GetPowerManagerClient() 75 ->GetPowerManagerClient()
84 ->RequestShutdown(); 76 ->RequestShutdown();
85 } 77 }
86 78
87 void PowerButtonController::BrightnessChanged(int level, bool user_initiated) { 79 void PowerButtonController::OnPowerButtonStateChanged(
88 if (brightness_is_zero_) 80 PowerButtonObserver::State state) {
89 zero_brightness_end_time_ = base::TimeTicks::Now();
90 brightness_is_zero_ = (level == 0);
91 }
92
93 void PowerButtonController::PowerButtonEventReceived(
94 bool down,
95 const base::TimeTicks& timestamp) {
96 if (state_ == STATE_SHUTDOWN_REQUESTED) 81 if (state_ == STATE_SHUTDOWN_REQUESTED)
97 return; 82 return;
98 83
99 // Avoid requesting suspend or shutdown if the power button is pressed while 84 switch (state) {
100 // the screen is off (http://crbug.com/128451). 85 case PRESSED:
101 base::TimeDelta time_since_zero_brightness = brightness_is_zero_ ? 86 state_ = STATE_SUSPEND_ON_RELEASE;
102 base::TimeDelta() : (base::TimeTicks::Now() - zero_brightness_end_time_); 87 break;
103 const int kShortTimeMs = 10; 88 case LONG_PRESSED:
104 if (time_since_zero_brightness.InMilliseconds() <= kShortTimeMs) 89 ShowShutdownWarningDialog();
105 return; 90 break;
106 91 case RELEASED:
107 if (down) { 92 if (state_ == STATE_SUSPEND_ON_RELEASE) {
108 state_ = STATE_SUSPEND_ON_RELEASE; 93 chromeos::DBusThreadManager::Get()
109 timer_.Start( 94 ->GetPowerManagerClient()
110 FROM_HERE, 95 ->RequestSuspend();
111 base::TimeDelta::FromMilliseconds(kShowShutdownWarningTimeoutMs), 96 }
112 this, 97 state_ = STATE_OTHER;
113 &PowerButtonController::ShowShutdownWarningDialog); 98 timer_.Stop();
114 } else { 99 shutdown_warning_message_.reset();
115 if (state_ == STATE_SUSPEND_ON_RELEASE) { 100 break;
116 chromeos::DBusThreadManager::Get()
117 ->GetPowerManagerClient()
118 ->RequestSuspend();
119 }
120 state_ = STATE_OTHER;
121 timer_.Stop();
122 shutdown_warning_message_.reset();
123 } 101 }
124 } 102 }
125 103
126 } // namespace athena 104 } // namespace athena
OLDNEW
« athena/system/power_button_controller.h ('K') | « athena/system/power_button_controller.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698