16e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
26e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
36e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// found in the LICENSE file.
46e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
56e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "athena/screen/public/screen_manager.h"
66e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "athena/system/orientation_controller.h"
76e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/bind.h"
86e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/files/file_path_watcher.h"
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/message_loop/message_loop.h"
116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/task_runner.h"
126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace athena {
146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace {
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Threshold after which to rotate in a given direction.
186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)const int kGravityThreshold = 6.0f;
196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}  // namespace
216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciOrientationController::OrientationController() {
2303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
2403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
2503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)void OrientationController::InitWith(
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    scoped_refptr<base::TaskRunner> blocking_task_runner) {
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  accelerometer_reader_.reset(
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      new chromeos::AccelerometerReader(blocking_task_runner, this));
296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)OrientationController::~OrientationController() {
326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
3403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)void OrientationController::Shutdown() {
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  accelerometer_reader_.reset();
3603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
3703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid OrientationController::HandleAccelerometerUpdate(
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const ui::AccelerometerUpdate& update) {
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!update.has(ui::ACCELEROMETER_SOURCE_SCREEN))
416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return;
426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  float gravity_x = update.get(ui::ACCELEROMETER_SOURCE_SCREEN).x();
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  float gravity_y = update.get(ui::ACCELEROMETER_SOURCE_SCREEN).y();
456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  gfx::Display::Rotation rotation;
466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (gravity_x < -kGravityThreshold) {
476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    rotation = gfx::Display::ROTATE_270;
486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  } else if (gravity_x > kGravityThreshold) {
496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    rotation = gfx::Display::ROTATE_90;
506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  } else if (gravity_y < -kGravityThreshold) {
516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    rotation = gfx::Display::ROTATE_180;
526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  } else if (gravity_y > kGravityThreshold) {
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    rotation = gfx::Display::ROTATE_0;
546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  } else {
556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // No rotation as gravity threshold was not hit.
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return;
576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (rotation == current_rotation_)
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return;
616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  current_rotation_ = rotation;
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ScreenManager::Get()->SetRotation(rotation);
646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}  // namespace athena
67