Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / athena / system / orientation_controller.cc
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.
4
5 #include "athena/screen/public/screen_manager.h"
6 #include "athena/system/orientation_controller.h"
7 #include "base/bind.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
13 namespace athena {
14
15 namespace {
16
17 // Threshold after which to rotate in a given direction.
18 const int kGravityThreshold = 6.0f;
19
20 }  // namespace
21
22 OrientationController::OrientationController() {
23 }
24
25 void OrientationController::InitWith(
26     scoped_refptr<base::TaskRunner> blocking_task_runner) {
27   accelerometer_reader_.reset(
28       new chromeos::AccelerometerReader(blocking_task_runner, this));
29 }
30
31 OrientationController::~OrientationController() {
32 }
33
34 void OrientationController::Shutdown() {
35   accelerometer_reader_.reset();
36 }
37
38 void OrientationController::HandleAccelerometerUpdate(
39     const ui::AccelerometerUpdate& update) {
40   if (!update.has(ui::ACCELEROMETER_SOURCE_SCREEN))
41     return;
42
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;
54   } else {
55     // No rotation as gravity threshold was not hit.
56     return;
57   }
58
59   if (rotation == current_rotation_)
60     return;
61
62   current_rotation_ = rotation;
63   ScreenManager::Get()->SetRotation(rotation);
64 }
65
66 }  // namespace athena