| 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/shutdown_dialog.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 ShutdownDialog::ShutdownDialog(aura::Window* dialog_container) |
| 32 : warning_message_container_(dialog_container), | 29 : warning_message_container_(dialog_container), state_(STATE_OTHER) { |
| 33 brightness_is_zero_(false), | 30 InputManager::Get()->AddPowerButtonObserver(this); |
| 34 state_(STATE_OTHER) { | |
| 35 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( | |
| 36 this); | |
| 37 } | 31 } |
| 38 | 32 |
| 39 PowerButtonController::~PowerButtonController() { | 33 ShutdownDialog::~ShutdownDialog() { |
| 40 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | 34 InputManager::Get()->RemovePowerButtonObserver(this); |
| 41 this); | |
| 42 } | 35 } |
| 43 | 36 |
| 44 void PowerButtonController::ShowShutdownWarningDialog() { | 37 void ShutdownDialog::ShowShutdownWarningDialog() { |
| 45 state_ = STATE_SHUTDOWN_WARNING_VISIBLE; | 38 state_ = STATE_SHUTDOWN_WARNING_VISIBLE; |
| 46 | 39 |
| 47 shutdown_warning_message_.reset(new views::Widget); | 40 shutdown_warning_message_.reset(new views::Widget); |
| 48 | 41 |
| 49 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); | 42 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); |
| 50 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 43 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 51 params.parent = warning_message_container_; | 44 params.parent = warning_message_container_; |
| 52 shutdown_warning_message_->Init(params); | 45 shutdown_warning_message_->Init(params); |
| 53 | 46 |
| 54 views::Label* label = | 47 views::Label* label = |
| 55 new views::Label(l10n_util::GetStringUTF16(IDS_ATHENA_SHUTDOWN_WARNING)); | 48 new views::Label(l10n_util::GetStringUTF16(IDS_ATHENA_SHUTDOWN_WARNING)); |
| 56 label->SetBackgroundColor(SK_ColorWHITE); | 49 label->SetBackgroundColor(SK_ColorWHITE); |
| 57 label->SetFontList(gfx::FontList().DeriveWithStyle(gfx::Font::BOLD)); | 50 label->SetFontList(gfx::FontList().DeriveWithStyle(gfx::Font::BOLD)); |
| 58 | 51 |
| 59 views::View* container = new views::View; | 52 views::View* container = new views::View; |
| 60 container->AddChildView(label); | 53 container->AddChildView(label); |
| 61 | 54 |
| 62 const int kBorderSpacing = 50; | 55 const int kBorderSpacing = 50; |
| 63 container->SetLayoutManager(new views::BoxLayout( | 56 container->SetLayoutManager(new views::BoxLayout( |
| 64 views::BoxLayout::kHorizontal, kBorderSpacing, kBorderSpacing, 0)); | 57 views::BoxLayout::kHorizontal, kBorderSpacing, kBorderSpacing, 0)); |
| 65 container->set_background( | 58 container->set_background( |
| 66 views::Background::CreateSolidBackground(SK_ColorWHITE)); | 59 views::Background::CreateSolidBackground(SK_ColorWHITE)); |
| 67 container->SetBorder(views::Border::CreateSolidBorder(1, SK_ColorBLACK)); | 60 container->SetBorder(views::Border::CreateSolidBorder(1, SK_ColorBLACK)); |
| 68 | 61 |
| 69 shutdown_warning_message_->SetContentsView(container); | 62 shutdown_warning_message_->SetContentsView(container); |
| 70 shutdown_warning_message_->CenterWindow(container->GetPreferredSize()); | 63 shutdown_warning_message_->CenterWindow(container->GetPreferredSize()); |
| 71 shutdown_warning_message_->Show(); | 64 shutdown_warning_message_->Show(); |
| 72 | |
| 73 timer_.Start(FROM_HERE, | 65 timer_.Start(FROM_HERE, |
| 74 base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs - | 66 base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs), |
| 75 kShowShutdownWarningTimeoutMs), | |
| 76 this, | 67 this, |
| 77 &PowerButtonController::Shutdown); | 68 &ShutdownDialog::Shutdown); |
| 78 } | 69 } |
| 79 | 70 |
| 80 void PowerButtonController::Shutdown() { | 71 void ShutdownDialog::Shutdown() { |
| 81 state_ = STATE_SHUTDOWN_REQUESTED; | 72 state_ = STATE_SHUTDOWN_REQUESTED; |
| 82 chromeos::DBusThreadManager::Get() | 73 chromeos::DBusThreadManager::Get() |
| 83 ->GetPowerManagerClient() | 74 ->GetPowerManagerClient() |
| 84 ->RequestShutdown(); | 75 ->RequestShutdown(); |
| 85 } | 76 } |
| 86 | 77 |
| 87 void PowerButtonController::BrightnessChanged(int level, bool user_initiated) { | 78 void ShutdownDialog::OnPowerButtonStateChanged( |
| 88 if (brightness_is_zero_) | 79 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) | 80 if (state_ == STATE_SHUTDOWN_REQUESTED) |
| 97 return; | 81 return; |
| 98 | 82 |
| 99 // Avoid requesting suspend or shutdown if the power button is pressed while | 83 switch (state) { |
| 100 // the screen is off (http://crbug.com/128451). | 84 case PRESSED: |
| 101 base::TimeDelta time_since_zero_brightness = brightness_is_zero_ ? | 85 state_ = STATE_SUSPEND_ON_RELEASE; |
| 102 base::TimeDelta() : (base::TimeTicks::Now() - zero_brightness_end_time_); | 86 break; |
| 103 const int kShortTimeMs = 10; | 87 case LONG_PRESSED: |
| 104 if (time_since_zero_brightness.InMilliseconds() <= kShortTimeMs) | 88 ShowShutdownWarningDialog(); |
| 105 return; | 89 break; |
| 106 | 90 case RELEASED: |
| 107 if (down) { | 91 if (state_ == STATE_SUSPEND_ON_RELEASE) { |
| 108 state_ = STATE_SUSPEND_ON_RELEASE; | 92 chromeos::DBusThreadManager::Get() |
| 109 timer_.Start( | 93 ->GetPowerManagerClient() |
| 110 FROM_HERE, | 94 ->RequestSuspend(); |
| 111 base::TimeDelta::FromMilliseconds(kShowShutdownWarningTimeoutMs), | 95 } |
| 112 this, | 96 state_ = STATE_OTHER; |
| 113 &PowerButtonController::ShowShutdownWarningDialog); | 97 timer_.Stop(); |
| 114 } else { | 98 shutdown_warning_message_.reset(); |
| 115 if (state_ == STATE_SUSPEND_ON_RELEASE) { | 99 break; |
| 116 chromeos::DBusThreadManager::Get() | |
| 117 ->GetPowerManagerClient() | |
| 118 ->RequestSuspend(); | |
| 119 } | |
| 120 state_ = STATE_OTHER; | |
| 121 timer_.Stop(); | |
| 122 shutdown_warning_message_.reset(); | |
| 123 } | 100 } |
| 124 } | 101 } |
| 125 | 102 |
| 126 } // namespace athena | 103 } // namespace athena |
| OLD | NEW |