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"
17 // Threshold after which to rotate in a given direction.
18 const int kGravityThreshold = 6.0f;
22 OrientationController::OrientationController() {
25 void OrientationController::InitWith(
26 scoped_refptr<base::TaskRunner> blocking_task_runner) {
27 accelerometer_reader_.reset(
28 new chromeos::AccelerometerReader(blocking_task_runner, this));
31 OrientationController::~OrientationController() {
34 void OrientationController::Shutdown() {
35 accelerometer_reader_.reset();
38 void OrientationController::HandleAccelerometerUpdate(
39 const ui::AccelerometerUpdate& update) {
40 if (!update.has(ui::ACCELEROMETER_SOURCE_SCREEN))
43 float gravity_x = update.get(ui::ACCELEROMETER_SOURCE_SCREEN).x();
44 float gravity_y = update.get(ui::ACCELEROMETER_SOURCE_SCREEN).y();
45 gfx::Display::Rotation rotation;
46 if (gravity_x < -kGravityThreshold) {
47 rotation = gfx::Display::ROTATE_270;
48 } else if (gravity_x > kGravityThreshold) {
49 rotation = gfx::Display::ROTATE_90;
50 } else if (gravity_y < -kGravityThreshold) {
51 rotation = gfx::Display::ROTATE_180;
52 } else if (gravity_y > kGravityThreshold) {
53 rotation = gfx::Display::ROTATE_0;
55 // No rotation as gravity threshold was not hit.
59 if (rotation == current_rotation_)
62 current_rotation_ = rotation;
63 ScreenManager::Get()->SetRotation(rotation);