| 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/screen/public/screen_manager.h" | 5 #include "athena/screen/public/screen_manager.h" |
| 6 #include "athena/system/orientation_controller.h" | 6 #include "athena/system/orientation_controller.h" |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path_watcher.h" | 8 #include "base/files/file_path_watcher.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/task_runner.h" | 11 #include "base/task_runner.h" |
| 12 | 12 |
| 13 namespace athena { | 13 namespace athena { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Threshold after which to rotate in a given direction. | 17 // Threshold after which to rotate in a given direction. |
| 18 const int kGravityThreshold = 6.0f; | 18 const int kGravityThreshold = 6.0f; |
| 19 | 19 |
| 20 } // namespace | 20 } // namespace |
| 21 | 21 |
| 22 OrientationController::OrientationController() { | 22 OrientationController::OrientationController() { |
| 23 } | 23 } |
| 24 | 24 |
| 25 void OrientationController::InitWith( | 25 void OrientationController::InitWith( |
| 26 scoped_refptr<base::TaskRunner> blocking_task_runner) { | 26 scoped_refptr<base::TaskRunner> blocking_task_runner) { |
| 27 accelerometer_reader_.reset( | 27 accelerometer_reader_.reset(new chromeos::AccelerometerReader()); |
| 28 new chromeos::AccelerometerReader(blocking_task_runner, this)); | 28 accelerometer_reader_->Initialize(blocking_task_runner); |
| 29 accelerometer_reader_->AddObserver(this); |
| 29 } | 30 } |
| 30 | 31 |
| 31 OrientationController::~OrientationController() { | 32 OrientationController::~OrientationController() { |
| 32 } | 33 } |
| 33 | 34 |
| 34 void OrientationController::Shutdown() { | 35 void OrientationController::Shutdown() { |
| 36 accelerometer_reader_->RemoveObserver(this); |
| 35 accelerometer_reader_.reset(); | 37 accelerometer_reader_.reset(); |
| 36 } | 38 } |
| 37 | 39 |
| 38 void OrientationController::HandleAccelerometerUpdate( | 40 void OrientationController::OnAccelerometerUpdated( |
| 39 const ui::AccelerometerUpdate& update) { | 41 const ui::AccelerometerUpdate& update) { |
| 40 if (!update.has(ui::ACCELEROMETER_SOURCE_SCREEN)) | 42 if (!update.has(ui::ACCELEROMETER_SOURCE_SCREEN)) |
| 41 return; | 43 return; |
| 42 | 44 |
| 43 float gravity_x = update.get(ui::ACCELEROMETER_SOURCE_SCREEN).x(); | 45 float gravity_x = update.get(ui::ACCELEROMETER_SOURCE_SCREEN).x(); |
| 44 float gravity_y = update.get(ui::ACCELEROMETER_SOURCE_SCREEN).y(); | 46 float gravity_y = update.get(ui::ACCELEROMETER_SOURCE_SCREEN).y(); |
| 45 gfx::Display::Rotation rotation; | 47 gfx::Display::Rotation rotation; |
| 46 if (gravity_x < -kGravityThreshold) { | 48 if (gravity_x < -kGravityThreshold) { |
| 47 rotation = gfx::Display::ROTATE_270; | 49 rotation = gfx::Display::ROTATE_270; |
| 48 } else if (gravity_x > kGravityThreshold) { | 50 } else if (gravity_x > kGravityThreshold) { |
| 49 rotation = gfx::Display::ROTATE_90; | 51 rotation = gfx::Display::ROTATE_90; |
| 50 } else if (gravity_y < -kGravityThreshold) { | 52 } else if (gravity_y < -kGravityThreshold) { |
| 51 rotation = gfx::Display::ROTATE_180; | 53 rotation = gfx::Display::ROTATE_180; |
| 52 } else if (gravity_y > kGravityThreshold) { | 54 } else if (gravity_y > kGravityThreshold) { |
| 53 rotation = gfx::Display::ROTATE_0; | 55 rotation = gfx::Display::ROTATE_0; |
| 54 } else { | 56 } else { |
| 55 // No rotation as gravity threshold was not hit. | 57 // No rotation as gravity threshold was not hit. |
| 56 return; | 58 return; |
| 57 } | 59 } |
| 58 | 60 |
| 59 if (rotation == current_rotation_) | 61 if (rotation == current_rotation_) |
| 60 return; | 62 return; |
| 61 | 63 |
| 62 current_rotation_ = rotation; | 64 current_rotation_ = rotation; |
| 63 ScreenManager::Get()->SetRotation(rotation); | 65 ScreenManager::Get()->SetRotation(rotation); |
| 64 } | 66 } |
| 65 | 67 |
| 66 } // namespace athena | 68 } // namespace athena |
| OLD | NEW |