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/screen/public/screen_manager.h"
6 #include "athena/system/orientation_controller.h"
8 #include "base/files/file_path_watcher.h"
9 #include "base/files/file_util.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/task_runner.h"
12 #include "chromeos/accelerometer/accelerometer_reader.h"
18 // Threshold after which to rotate in a given direction.
19 const int kGravityThreshold
= 6.0f
;
23 OrientationController::OrientationController() {
26 void OrientationController::InitWith(
27 scoped_refptr
<base::TaskRunner
> blocking_task_runner
) {
28 chromeos::AccelerometerReader::GetInstance()->Initialize(
29 blocking_task_runner
);
30 chromeos::AccelerometerReader::GetInstance()->AddObserver(this);
33 OrientationController::~OrientationController() {
36 void OrientationController::Shutdown() {
37 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this);
40 void OrientationController::OnAccelerometerUpdated(
41 const ui::AccelerometerUpdate
& update
) {
42 if (!update
.has(ui::ACCELEROMETER_SOURCE_SCREEN
))
45 float gravity_x
= update
.get(ui::ACCELEROMETER_SOURCE_SCREEN
).x();
46 float gravity_y
= update
.get(ui::ACCELEROMETER_SOURCE_SCREEN
).y();
47 gfx::Display::Rotation rotation
;
48 if (gravity_x
< -kGravityThreshold
) {
49 rotation
= gfx::Display::ROTATE_270
;
50 } else if (gravity_x
> kGravityThreshold
) {
51 rotation
= gfx::Display::ROTATE_90
;
52 } else if (gravity_y
< -kGravityThreshold
) {
53 rotation
= gfx::Display::ROTATE_180
;
54 } else if (gravity_y
> kGravityThreshold
) {
55 rotation
= gfx::Display::ROTATE_0
;
57 // No rotation as gravity threshold was not hit.
61 if (rotation
== current_rotation_
)
64 current_rotation_
= rotation
;
65 ScreenManager::Get()->SetRotation(rotation
);