Chromium Code Reviews| Index: ash/content/display/screen_orientation_controller_chromeos.cc |
| diff --git a/ash/content/display/screen_orientation_delegate_chromeos.cc b/ash/content/display/screen_orientation_controller_chromeos.cc |
| similarity index 32% |
| rename from ash/content/display/screen_orientation_delegate_chromeos.cc |
| rename to ash/content/display/screen_orientation_controller_chromeos.cc |
| index 10d2b9bcf3ca662cda0831a3d7e073ea6074b870..1ed994e36ffb9464d4b418f90c0879be4fd68d93 100644 |
| --- a/ash/content/display/screen_orientation_delegate_chromeos.cc |
| +++ b/ash/content/display/screen_orientation_controller_chromeos.cc |
| @@ -2,13 +2,15 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "ash/content/display/screen_orientation_delegate_chromeos.h" |
| +#include "ash/content/display/screen_orientation_controller_chromeos.h" |
| +#include "ash/accelerometer/accelerometer_controller.h" |
| #include "ash/ash_switches.h" |
| #include "ash/display/display_info.h" |
| #include "ash/display/display_manager.h" |
| #include "ash/shell.h" |
| #include "ash/wm/maximize_mode/maximize_mode_controller.h" |
| +#include "base/auto_reset.h" |
| #include "base/command_line.h" |
| #include "content/public/browser/screen_orientation_provider.h" |
| #include "content/public/browser/web_contents.h" |
| @@ -18,6 +20,26 @@ |
| namespace { |
| +// The maximum deviation from the acceleration expected due to gravity under |
| +// which to detect hinge angle and screen rotation in m/s^2 |
| +const float kDeviationFromGravityThreshold = 1.0f; |
| + |
| +// The angle which the screen has to be rotated past before the display will |
| +// rotate to match it (i.e. 45.0f is no stickiness). |
| +const float kDisplayRotationStickyAngleDegrees = 60.0f; |
| + |
| +// The mean acceleration due to gravity on Earth in m/s^2. |
| +const float kMeanGravity = 9.80665f; |
| + |
| +// The minimum acceleration in m/s^2 in a direction required to trigger screen |
| +// rotation. This prevents rapid toggling of rotation when the device is near |
| +// flat and there is very little screen aligned force on it. The value is |
| +// effectively the sine of the rise angle required times the acceleration due |
| +// to gravity, with the current value requiring at least a 25 degree rise. |
| +const float kMinimumAccelerationScreenRotation = 4.2f; |
| + |
| +const float kRadiansToDegrees = 180.0f / 3.14159265f; |
| + |
| blink::WebScreenOrientationLockType GetDisplayNaturalOrientation() { |
| ash::DisplayManager* display_manager = |
| ash::Shell::GetInstance()->display_manager(); |
| @@ -43,36 +65,111 @@ blink::WebScreenOrientationLockType GetDisplayNaturalOrientation() { |
| return blink::WebScreenOrientationLockLandscape; |
| } |
| +// Returns the angle between |base| and |other| in degrees. |
| +float AngleBetweenVectorsInDegrees(const gfx::Vector3dF& base, |
| + const gfx::Vector3dF& other) { |
| + return acos(gfx::DotProduct(base, other) / base.Length() / other.Length()) * |
| + kRadiansToDegrees; |
| +} |
| + |
| +// Returns the clockwise angle between |base| and |other| where |normal| is the |
| +// normal of the virtual surface to measure clockwise according to. |
| +float ClockwiseAngleBetweenVectorsInDegrees(const gfx::Vector3dF& base, |
|
flackr
2014/12/12 20:46:06
These are still used in maximize_mode_controller a
jonross
2015/01/06 19:56:10
Done.
|
| + const gfx::Vector3dF& other, |
| + const gfx::Vector3dF& normal) { |
| + float angle = AngleBetweenVectorsInDegrees(base, other); |
| + gfx::Vector3dF cross(base); |
| + cross.Cross(other); |
| + |
| + // If the dot product of this cross product is normal, it means that the |
| + // shortest angle between |base| and |other| was counterclockwise with respect |
| + // to the surface represented by |normal| and this angle must be reversed. |
| + if (gfx::DotProduct(cross, normal) > 0.0f) |
| + angle = 360.0f - angle; |
| + return angle; |
| +} |
| + |
| } // namespace |
| namespace ash { |
| -ScreenOrientationDelegate::ScreenOrientationDelegate() |
| +ScreenOrientationController::ScreenOrientationController() |
| : locking_window_(NULL), |
| - natural_orientation_(GetDisplayNaturalOrientation()) { |
| + natural_orientation_(GetDisplayNaturalOrientation()), |
| + ignore_display_configuration_updates_(false), |
| + rotation_locked_(false), |
| + user_rotation_(gfx::Display::ROTATE_0), |
| + current_rotation_(gfx::Display::ROTATE_0) { |
| content::ScreenOrientationProvider::SetDelegate(this); |
| + Shell::GetInstance()->AddShellObserver(this); |
| } |
| -ScreenOrientationDelegate::~ScreenOrientationDelegate() { |
| +ScreenOrientationController::~ScreenOrientationController() { |
| content::ScreenOrientationProvider::SetDelegate(NULL); |
| + Shell::GetInstance()->RemoveShellObserver(this); |
| + Shell::GetInstance()->accelerometer_controller()->RemoveObserver(this); |
| + Shell::GetInstance()->display_controller()->RemoveObserver(this); |
| +} |
| + |
| +void ScreenOrientationController::AddObserver(Observer* observer) { |
| + observers_.AddObserver(observer); |
| +} |
| + |
| +void ScreenOrientationController::RemoveObserver(Observer* observer) { |
| + observers_.RemoveObserver(observer); |
| } |
| -bool ScreenOrientationDelegate::FullScreenRequired( |
| +void ScreenOrientationController::SetRotationLocked(bool rotation_locked) { |
| + if (rotation_locked_ == rotation_locked) |
| + return; |
| + rotation_locked_ = rotation_locked; |
| + FOR_EACH_OBSERVER(Observer, observers_, |
| + OnRotationLockChanged(rotation_locked_)); |
| + DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| + if (!display_manager->HasInternalDisplay()) |
| + return; |
| + base::AutoReset<bool> auto_ignore_display_configuration_updates( |
| + &ignore_display_configuration_updates_, true); |
| + display_manager->RegisterDisplayRotationProperties(rotation_locked_, |
| + current_rotation_); |
| +} |
| + |
| +void ScreenOrientationController::SetDisplayRotation( |
| + gfx::Display::Rotation rotation) { |
| + DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| + if (!display_manager->HasInternalDisplay()) |
| + return; |
| + current_rotation_ = rotation; |
| + base::AutoReset<bool> auto_ignore_display_configuration_updates( |
| + &ignore_display_configuration_updates_, true); |
| + display_manager->SetDisplayRotation(gfx::Display::InternalDisplayId(), |
| + rotation); |
| +} |
| + |
| +void ScreenOrientationController::OnAccelerometerUpdated( |
| + const ui::AccelerometerUpdate& update) { |
| + if (rotation_locked_) |
| + return; |
| + if (!update.has(ui::ACCELEROMETER_SOURCE_SCREEN)) |
| + return; |
| + // Ignore the reading if it appears unstable. The reading is considered |
| + // unstable if it deviates too much from gravity |
| + if (!(std::abs(update.get(ui::ACCELEROMETER_SOURCE_SCREEN).Length() - |
| + kMeanGravity) <= kDeviationFromGravityThreshold)) { |
|
oshima
2014/12/12 19:59:53
this one too?
jonross
2015/01/06 19:56:10
Done.
|
| + return; |
| + } |
| + HandleScreenRotation(update.get(ui::ACCELEROMETER_SOURCE_SCREEN)); |
| +} |
| + |
| +bool ScreenOrientationController::FullScreenRequired( |
| content::WebContents* web_contents) { |
| return true; |
| } |
| -void ScreenOrientationDelegate::Lock( |
| +void ScreenOrientationController::Lock( |
| content::WebContents* web_contents, |
| blink::WebScreenOrientationLockType lock_orientation) { |
| aura::Window* requesting_window = web_contents->GetNativeView(); |
| - |
| - // TODO(jonross): Make ScreenOrientationDelegate responsible for rotation |
| - // lock. Have MaximizeModeController, and TrayRotationLock both use it |
| - // instead. |
| - MaximizeModeController* controller = |
| - Shell::GetInstance()->maximize_mode_controller(); |
| - |
| // TODO(jonross): Track one rotation lock per window. When the active window |
| // changes apply any corresponding rotation lock. |
| if (!locking_window_) |
| @@ -82,7 +179,7 @@ void ScreenOrientationDelegate::Lock( |
| switch (lock_orientation) { |
| case blink::WebScreenOrientationLockAny: |
| - controller->SetRotationLocked(false); |
| + SetRotationLocked(false); |
| locking_window_ = NULL; |
| break; |
| case blink::WebScreenOrientationLockDefault: |
| @@ -108,7 +205,7 @@ void ScreenOrientationDelegate::Lock( |
| blink::WebScreenOrientationLockLandscape); |
| break; |
| case blink::WebScreenOrientationLockNatural: |
| - controller->LockRotation(gfx::Display::ROTATE_0); |
| + LockRotation(gfx::Display::ROTATE_0); |
| break; |
| default: |
| NOTREACHED(); |
| @@ -116,7 +213,7 @@ void ScreenOrientationDelegate::Lock( |
| } |
| } |
| -bool ScreenOrientationDelegate::ScreenOrientationProviderSupported() { |
| +bool ScreenOrientationController::ScreenOrientationProviderSupported() { |
| return Shell::GetInstance() |
| ->maximize_mode_controller() |
| ->IsMaximizeModeWindowManagerEnabled() && |
| @@ -124,29 +221,73 @@ bool ScreenOrientationDelegate::ScreenOrientationProviderSupported() { |
| switches::kAshEnableTouchViewTesting); |
| } |
| -void ScreenOrientationDelegate::Unlock(content::WebContents* web_contents) { |
| +void ScreenOrientationController::Unlock(content::WebContents* web_contents) { |
| aura::Window* requesting_window = web_contents->GetNativeView(); |
| if (requesting_window != locking_window_) |
| return; |
| locking_window_ = NULL; |
| - Shell::GetInstance()->maximize_mode_controller()->SetRotationLocked(false); |
| + SetRotationLocked(false); |
| } |
| -void ScreenOrientationDelegate::LockRotationToPrimaryOrientation( |
| +void ScreenOrientationController::OnDisplayConfigurationChanged() { |
| + if (ignore_display_configuration_updates_) |
| + return; |
| + gfx::Display::Rotation user_rotation = |
| + Shell::GetInstance() |
| + ->display_manager() |
| + ->GetDisplayInfo(gfx::Display::InternalDisplayId()) |
| + .rotation(); |
| + if (user_rotation != current_rotation_) { |
| + // A user may change other display configuration settings. When the user |
| + // does change the rotation setting, then lock rotation to prevent the |
| + // accelerometer from erasing their change. |
| + SetRotationLocked(true); |
| + user_rotation_ = current_rotation_ = user_rotation; |
| + } |
| +} |
| + |
| +void ScreenOrientationController::OnMaximizeModeStarted() { |
| + DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| + if (!display_manager->HasInternalDisplay()) |
| + return; |
| + current_rotation_ = user_rotation_ = |
| + display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()) |
| + .rotation(); |
| + LoadDisplayRotationProperties(); |
| + Shell::GetInstance()->accelerometer_controller()->AddObserver(this); |
|
flackr
2014/12/12 20:46:06
Hmm, this means the rotation update will be delaye
jonross
2015/01/06 19:56:10
Done.
|
| + Shell::GetInstance()->display_controller()->AddObserver(this); |
| +} |
| + |
| +void ScreenOrientationController::OnMaximizeModeEnded() { |
| + if (!Shell::GetInstance()->display_manager()->HasInternalDisplay()) |
| + return; |
| + Shell::GetInstance()->accelerometer_controller()->RemoveObserver(this); |
| + Shell::GetInstance()->display_controller()->RemoveObserver(this); |
| + if (current_rotation_ != user_rotation_) |
| + SetDisplayRotation(user_rotation_); |
| +} |
| + |
| +void ScreenOrientationController::LockRotation( |
| + gfx::Display::Rotation rotation) { |
| + SetRotationLocked(true); |
| + SetDisplayRotation(rotation); |
| +} |
| + |
| +void ScreenOrientationController::LockRotationToPrimaryOrientation( |
| blink::WebScreenOrientationLockType lock_orientation) { |
| - Shell::GetInstance()->maximize_mode_controller()->LockRotation( |
| - natural_orientation_ == lock_orientation ? gfx::Display::ROTATE_0 |
| - : gfx::Display::ROTATE_90); |
| + LockRotation(natural_orientation_ == lock_orientation |
| + ? gfx::Display::ROTATE_0 |
| + : gfx::Display::ROTATE_90); |
| } |
| -void ScreenOrientationDelegate::LockRotationToSecondaryOrientation( |
| +void ScreenOrientationController::LockRotationToSecondaryOrientation( |
| blink::WebScreenOrientationLockType lock_orientation) { |
| - Shell::GetInstance()->maximize_mode_controller()->LockRotation( |
| - natural_orientation_ == lock_orientation ? gfx::Display::ROTATE_180 |
| - : gfx::Display::ROTATE_270); |
| + LockRotation(natural_orientation_ == lock_orientation |
| + ? gfx::Display::ROTATE_180 |
| + : gfx::Display::ROTATE_270); |
| } |
| -void ScreenOrientationDelegate::LockToRotationMatchingOrientation( |
| +void ScreenOrientationController::LockToRotationMatchingOrientation( |
| blink::WebScreenOrientationLockType lock_orientation) { |
| // TODO(jonross): Update MaximizeModeController to allow rotation between |
| // two angles of an orientation (e.g. from ROTATE_0 to ROTATE_180, and from |
| @@ -158,23 +299,78 @@ void ScreenOrientationDelegate::LockToRotationMatchingOrientation( |
| gfx::Display::Rotation rotation = |
| display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()) |
| .rotation(); |
| - MaximizeModeController* controller = |
| - Shell::GetInstance()->maximize_mode_controller(); |
| if (natural_orientation_ == lock_orientation) { |
| if (rotation == gfx::Display::ROTATE_0 || |
| rotation == gfx::Display::ROTATE_180) { |
| - controller->SetRotationLocked(true); |
| + SetRotationLocked(true); |
| } else { |
| - controller->LockRotation(gfx::Display::ROTATE_0); |
| + LockRotation(gfx::Display::ROTATE_0); |
| } |
| } else { |
| if (rotation == gfx::Display::ROTATE_90 || |
| rotation == gfx::Display::ROTATE_270) { |
| - controller->SetRotationLocked(true); |
| + SetRotationLocked(true); |
| } else { |
| - controller->LockRotation(gfx::Display::ROTATE_90); |
| + LockRotation(gfx::Display::ROTATE_90); |
| } |
| } |
| } |
| +void ScreenOrientationController::HandleScreenRotation( |
| + const gfx::Vector3dF& lid) { |
| + gfx::Vector3dF lid_flattened(lid.x(), lid.y(), 0.0f); |
| + float lid_flattened_length = lid_flattened.Length(); |
| + // When the lid is close to being flat, don't change rotation as it is too |
| + // sensitive to slight movements. |
| + if (lid_flattened_length < kMinimumAccelerationScreenRotation) |
| + return; |
| + |
| + // The reference vector is the angle of gravity when the device is rotated |
| + // clockwise by 45 degrees. Computing the angle between this vector and |
| + // gravity we can easily determine the expected display rotation. |
| + static const gfx::Vector3dF rotation_reference(-1.0f, -1.0f, 0.0f); |
| + |
| + // Set the down vector to match the expected direction of gravity given the |
| + // last configured rotation. This is used to enforce a stickiness that the |
| + // user must overcome to rotate the display and prevents frequent rotations |
| + // when holding the device near 45 degrees. |
| + gfx::Vector3dF down(0.0f, 0.0f, 0.0f); |
| + if (current_rotation_ == gfx::Display::ROTATE_0) |
| + down.set_y(-1.0f); |
| + else if (current_rotation_ == gfx::Display::ROTATE_90) |
| + down.set_x(-1.0f); |
| + else if (current_rotation_ == gfx::Display::ROTATE_180) |
| + down.set_y(1.0f); |
| + else |
| + down.set_x(1.0f); |
| + |
| + // Don't rotate if the screen has not passed the threshold. |
| + if (AngleBetweenVectorsInDegrees(down, lid_flattened) < |
| + kDisplayRotationStickyAngleDegrees) { |
| + return; |
| + } |
| + |
| + float angle = ClockwiseAngleBetweenVectorsInDegrees( |
| + rotation_reference, lid_flattened, gfx::Vector3dF(0.0f, 0.0f, -1.0f)); |
| + |
| + gfx::Display::Rotation new_rotation = gfx::Display::ROTATE_90; |
| + if (angle < 90.0f) |
| + new_rotation = gfx::Display::ROTATE_0; |
| + else if (angle < 180.0f) |
| + new_rotation = gfx::Display::ROTATE_270; |
| + else if (angle < 270.0f) |
| + new_rotation = gfx::Display::ROTATE_180; |
| + |
| + if (new_rotation != current_rotation_) |
| + SetDisplayRotation(new_rotation); |
| +} |
| + |
| +void ScreenOrientationController::LoadDisplayRotationProperties() { |
| + DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| + if (!display_manager->registered_internal_display_rotation_lock()) |
| + return; |
| + SetDisplayRotation(display_manager->registered_internal_display_rotation()); |
| + SetRotationLocked(true); |
| +} |
| + |
| } // namespace ash |