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() { |
| 33 DCHECK(!accelerometer_reader_.get()); |
32 } | 34 } |
33 | 35 |
34 void OrientationController::Shutdown() { | 36 void OrientationController::Shutdown() { |
| 37 accelerometer_reader_->RemoveObserver(this); |
35 accelerometer_reader_.reset(); | 38 accelerometer_reader_.reset(); |
36 } | 39 } |
37 | 40 |
38 void OrientationController::HandleAccelerometerUpdate( | 41 void OrientationController::OnAccelerometerUpdated( |
39 const ui::AccelerometerUpdate& update) { | 42 const ui::AccelerometerUpdate& update) { |
40 if (!update.has(ui::ACCELEROMETER_SOURCE_SCREEN)) | 43 if (!update.has(ui::ACCELEROMETER_SOURCE_SCREEN)) |
41 return; | 44 return; |
42 | 45 |
43 float gravity_x = update.get(ui::ACCELEROMETER_SOURCE_SCREEN).x(); | 46 float gravity_x = update.get(ui::ACCELEROMETER_SOURCE_SCREEN).x(); |
44 float gravity_y = update.get(ui::ACCELEROMETER_SOURCE_SCREEN).y(); | 47 float gravity_y = update.get(ui::ACCELEROMETER_SOURCE_SCREEN).y(); |
45 gfx::Display::Rotation rotation; | 48 gfx::Display::Rotation rotation; |
46 if (gravity_x < -kGravityThreshold) { | 49 if (gravity_x < -kGravityThreshold) { |
47 rotation = gfx::Display::ROTATE_270; | 50 rotation = gfx::Display::ROTATE_270; |
48 } else if (gravity_x > kGravityThreshold) { | 51 } else if (gravity_x > kGravityThreshold) { |
49 rotation = gfx::Display::ROTATE_90; | 52 rotation = gfx::Display::ROTATE_90; |
50 } else if (gravity_y < -kGravityThreshold) { | 53 } else if (gravity_y < -kGravityThreshold) { |
51 rotation = gfx::Display::ROTATE_180; | 54 rotation = gfx::Display::ROTATE_180; |
52 } else if (gravity_y > kGravityThreshold) { | 55 } else if (gravity_y > kGravityThreshold) { |
53 rotation = gfx::Display::ROTATE_0; | 56 rotation = gfx::Display::ROTATE_0; |
54 } else { | 57 } else { |
55 // No rotation as gravity threshold was not hit. | 58 // No rotation as gravity threshold was not hit. |
56 return; | 59 return; |
57 } | 60 } |
58 | 61 |
59 if (rotation == current_rotation_) | 62 if (rotation == current_rotation_) |
60 return; | 63 return; |
61 | 64 |
62 current_rotation_ = rotation; | 65 current_rotation_ = rotation; |
63 ScreenManager::Get()->SetRotation(rotation); | 66 ScreenManager::Get()->SetRotation(rotation); |
64 } | 67 } |
65 | 68 |
66 } // namespace athena | 69 } // namespace athena |
OLD | NEW |