Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / ui / native_app_window_tizen.cc
index 5a9d39f..50ff11f 100644 (file)
 
 #include "base/logging.h"
 #include "base/memory/scoped_ptr.h"
-#include "ui/aura/root_window.h"
+#include "ui/aura/window.h"
 #include "ui/gfx/transform.h"
 #include "ui/gfx/rect.h"
 #include "ui/gfx/screen.h"
 #include "ui/views/view.h"
 #include "ui/views/widget/widget.h"
+#include "xwalk/runtime/browser/ui/splash_screen_tizen.h"
 #include "xwalk/runtime/browser/ui/top_view_layout_views.h"
 #include "xwalk/runtime/browser/xwalk_browser_main_parts_tizen.h"
 
+namespace {
+
+static gfx::Display::Rotation ToDisplayRotation(gfx::Display display,
+    blink::WebScreenOrientationType orientation) {
+  gfx::Display::Rotation rot = gfx::Display::ROTATE_0;
+  switch (orientation) {
+    case blink::WebScreenOrientationUndefined:
+    case blink::WebScreenOrientationPortraitPrimary:
+      rot = gfx::Display::ROTATE_0;
+      break;
+    case blink::WebScreenOrientationLandscapeSecondary:
+      rot = gfx::Display::ROTATE_90;
+      break;
+    case blink::WebScreenOrientationPortraitSecondary:
+      rot = gfx::Display::ROTATE_180;
+      break;
+    case blink::WebScreenOrientationLandscapePrimary:
+      rot = gfx::Display::ROTATE_270;
+      break;
+  default:
+      NOTREACHED();
+  }
+
+  if (display.bounds().width() > display.bounds().height()) {
+    // Landscape devices have landscape-primary as default.
+    rot = static_cast<gfx::Display::Rotation>((rot - 1) % 4);
+  }
+
+  return rot;
+}
+
+static void SetWindowRotation(aura::Window* window, gfx::Display display) {
+  // This methods assumes that window is fullscreen.
+
+#if defined(OS_TIZEN_MOBILE)
+  // Assumes portrait display; shows overlay indicator in landscape only.
+  bool useOverlay = display.rotation() == gfx::Display::ROTATE_90 ||
+      display.rotation() == gfx::Display::ROTATE_180;
+  top_view_layout()->SetUseOverlay(enableOverlay);
+  indicator_widget_->SetDisplay(display);
+#endif
+
+  // As everything is calculated from the fixed position we do
+  // not update the display bounds after rotation change.
+  gfx::Transform rotate;
+  float one_pixel = 1.0f / display.device_scale_factor();
+  switch (display.rotation()) {
+    case gfx::Display::ROTATE_0:
+      break;
+    case gfx::Display::ROTATE_90:
+      rotate.Translate(display.bounds().width() - one_pixel, 0);
+      rotate.Rotate(90);
+      break;
+    case gfx::Display::ROTATE_270:
+      rotate.Translate(0, display.bounds().height() - one_pixel);
+      rotate.Rotate(270);
+      break;
+    case gfx::Display::ROTATE_180:
+      rotate.Translate(display.bounds().width() - one_pixel,
+                       display.bounds().height() - one_pixel);
+      rotate.Rotate(180);
+      break;
+  }
+
+  window->SetTransform(rotate);
+}
+
+}  // namespace.
+
 namespace xwalk {
 
 NativeAppWindowTizen::NativeAppWindowTizen(
     const NativeAppWindow::CreateParams& create_params)
     : NativeAppWindowViews(create_params),
+#if defined(OS_TIZEN_MOBILE)
       indicator_widget_(new TizenSystemIndicatorWidget()),
-      allowed_orientations_(ANY) {
-  indicator_container_.reset(new WidgetContainerView(indicator_widget_));
-}
+      indicator_container_(new WidgetContainerView(indicator_widget_)),
+#endif
+      orientation_lock_(blink::WebScreenOrientationLockAny) {}
 
 void NativeAppWindowTizen::Initialize() {
   NativeAppWindowViews::Initialize();
 
+  const base::FilePath& splash_screen_path = create_params().splash_screen_path;
+  if (!splash_screen_path.empty()) {
+    splash_screen_.reset(new SplashScreenTizen(
+        GetWidget(), splash_screen_path, create_params().web_contents));
+    splash_screen_->Start();
+  }
+
   // Get display info such as device_scale_factor, and current
-  // rotation (orientation). NOTE: This is a local copy of the info.
+  // rotation (orientation).
+  // NOTE: This is a local copy of the info.
   display_ = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
 
   aura::Window* root_window = GetNativeWindow()->GetRootWindow();
   DCHECK(root_window);
   root_window->AddObserver(this);
 
-  OrientationMask ua_default =
-      XWalkBrowserMainPartsTizen::GetAllowedUAOrientations();
-  OnAllowedOrientationsChanged(ua_default);
+  SensorProvider::GetInstance()->AddObserver(this);
 
-  if (SensorProvider* sensor = SensorProvider::GetInstance()) {
-    gfx::Display::Rotation rotation
-      = GetClosestAllowedRotation(sensor->GetCurrentRotation());
-    display_.set_rotation(rotation);
-    ApplyDisplayRotation();
-    sensor->AddObserver(this);
+  if (SensorProvider::GetInstance()->connected()) {
+    OnScreenOrientationChanged(
+        SensorProvider::GetInstance()->GetScreenOrientation());
   }
 }
 
 NativeAppWindowTizen::~NativeAppWindowTizen() {
-  if (SensorProvider::GetInstance())
+  if (SensorProvider::GetInstance()->connected())
     SensorProvider::GetInstance()->RemoveObserver(this);
 }
 
+void NativeAppWindowTizen::LockOrientation(
+      blink::WebScreenOrientationLockType lock) {
+  orientation_lock_ = lock;
+  if (SensorProvider::GetInstance()->connected())
+    OnScreenOrientationChanged(
+        SensorProvider::GetInstance()->GetScreenOrientation());
+}
+
 void NativeAppWindowTizen::ViewHierarchyChanged(
     const ViewHierarchyChangedDetails& details) {
   if (details.is_add && details.child == this) {
     NativeAppWindowViews::ViewHierarchyChanged(details);
+
+#if defined(OS_TIZEN_MOBILE)
     indicator_widget_->Initialize(GetNativeWindow());
     top_view_layout()->set_top_view(indicator_container_.get());
     AddChildView(indicator_container_.get());
+#endif
   }
 }
 
@@ -86,145 +171,77 @@ void NativeAppWindowTizen::OnWindowDestroying(aura::Window* window) {
 
 void NativeAppWindowTizen::OnWindowVisibilityChanging(
     aura::Window* window, bool visible) {
-  if (visible)
-    ApplyDisplayRotation();
-}
-
-gfx::Transform NativeAppWindowTizen::GetRotationTransform() const {
-  // This method assumed a fixed portrait device. As everything
-  // is calculated from the fixed position we do not update the
-  // display bounds after rotation change.
-  gfx::Transform rotate;
-  float one_pixel = 1.0f / display_.device_scale_factor();
-  switch (display_.rotation()) {
-    case gfx::Display::ROTATE_0:
-      break;
-    case gfx::Display::ROTATE_90:
-      rotate.Translate(display_.bounds().width() - one_pixel, 0);
-      rotate.Rotate(90);
-      break;
-    case gfx::Display::ROTATE_270:
-      rotate.Translate(0, display_.bounds().height() - one_pixel);
-      rotate.Rotate(270);
+  if (!visible)
+    return;
+  SetDisplayRotation(display_);
+}
+
+blink::WebScreenOrientationType
+    NativeAppWindowTizen::FindNearestAllowedOrientation(
+        blink::WebScreenOrientationType orientation) const {
+  switch (orientation_lock_) {
+    case blink::WebScreenOrientationLockDefault:
+    case blink::WebScreenOrientationLockAny:
+      return orientation;
+    case blink::WebScreenOrientationLockLandscape: {
+      switch (orientation) {
+        case blink::WebScreenOrientationLandscapePrimary:
+        case blink::WebScreenOrientationLandscapeSecondary:
+          return orientation;
+        default:
+          return blink::WebScreenOrientationLandscapePrimary;
+      }
       break;
-    case gfx::Display::ROTATE_180:
-      rotate.Translate(display_.bounds().width() - one_pixel,
-                       display_.bounds().height() - one_pixel);
-      rotate.Rotate(180);
+    }
+    case blink::WebScreenOrientationLockPortrait: {
+      switch (orientation) {
+        case blink::WebScreenOrientationPortraitPrimary:
+        case blink::WebScreenOrientationPortraitSecondary:
+          return orientation;
+        default:
+          return blink::WebScreenOrientationPortraitPrimary;
+      }
       break;
-  }
-
-  return rotate;
-}
-
-namespace {
-
-// Rotates a binary mask of 4 positions to the left.
-unsigned rotl4(unsigned value, int shift) {
-  unsigned res = (value << shift);
-  if (res > (1 << 4) - 1)
-    res = res >> 4;
-  return res;
-}
-
-bool IsLandscapeOrientation(const gfx::Display::Rotation& rotation) {
-  return rotation == gfx::Display::ROTATE_90 ||
-         rotation == gfx::Display::ROTATE_270;
-}
-
-}  // namespace.
-
-gfx::Display::Rotation NativeAppWindowTizen::GetClosestAllowedRotation(
-    gfx::Display::Rotation rotation) const {
-
-  unsigned result = PORTRAIT_PRIMARY;
-  // gfx::Display::Rotation starts at portrait-primary and
-  // belongs to the set [0:3].
-  result = rotl4(result, rotation);
-
-  // Test current orientation
-  if (allowed_orientations_ & result)
-    return rotation;
-
-  // Test orientation right of current one.
-  if (allowed_orientations_ & rotl4(result, 1))
-    return static_cast<gfx::Display::Rotation>((rotation + 1) % 4);
-
-  // Test orientation left of current one.
-  if (allowed_orientations_ & rotl4(result, 3))
-    return static_cast<gfx::Display::Rotation>((rotation + 3) % 4);
-
-  // Test orientation opposite of current one.
-  if (allowed_orientations_ & rotl4(result, 2))
-    return static_cast<gfx::Display::Rotation>((rotation + 2) % 4);
-
-  NOTREACHED();
-  return rotation;
-}
-
-Orientation NativeAppWindowTizen::GetCurrentOrientation() const {
-  switch (display_.rotation()) {
-    case gfx::Display::ROTATE_0:
-      return PORTRAIT_PRIMARY;
-    case gfx::Display::ROTATE_90:
-      return LANDSCAPE_PRIMARY;
-    case gfx::Display::ROTATE_180:
-      return PORTRAIT_SECONDARY;
-    case gfx::Display::ROTATE_270:
-      return LANDSCAPE_SECONDARY;
-    default:
+    }
+    case blink::WebScreenOrientationLockPortraitPrimary:
+      return blink::WebScreenOrientationPortraitPrimary;
+    case blink::WebScreenOrientationLockPortraitSecondary:
+      return blink::WebScreenOrientationPortraitSecondary;
+    case blink::WebScreenOrientationLockLandscapePrimary:
+      return blink::WebScreenOrientationLandscapePrimary;
+    case blink::WebScreenOrientationLockLandscapeSecondary:
+      return blink::WebScreenOrientationLandscapeSecondary;
+  default:
       NOTREACHED();
-      return PORTRAIT_PRIMARY;
   }
+  return orientation;
 }
 
-void NativeAppWindowTizen::OnAllowedOrientationsChanged(
-    OrientationMask orientations) {
-  allowed_orientations_ = orientations;
+void NativeAppWindowTizen::OnScreenOrientationChanged(
+    blink::WebScreenOrientationType orientation) {
 
-  // As we might have been locked before our current orientation
-  // might not fit with the sensor orienation.
-  gfx::Display::Rotation rotation = display_.rotation();
-  if (SensorProvider* sensor = SensorProvider::GetInstance())
-    rotation = sensor->GetCurrentRotation();
-
-  rotation = GetClosestAllowedRotation(rotation);
-  if (display_.rotation() == rotation)
-    return;
-
-  display_.set_rotation(rotation);
-  ApplyDisplayRotation();
-}
-
-void NativeAppWindowTizen::OnRotationChanged(
-    gfx::Display::Rotation rotation) {
   // We always store the current sensor position, even if we do not
   // apply it in case the window is invisible.
-
-  rotation = GetClosestAllowedRotation(rotation);
-  if (display_.rotation() == rotation)
+  gfx::Display::Rotation rot = ToDisplayRotation(display_,
+      FindNearestAllowedOrientation(orientation));
+  if (display_.rotation() == rot)
     return;
 
-  display_.set_rotation(rotation);
-
-  ApplyDisplayRotation();
+  display_.set_rotation(rot);
+  SetDisplayRotation(display_);
 }
 
-void NativeAppWindowTizen::UpdateTopViewOverlay() {
-  top_view_layout()->SetUseOverlay(
-      IsLandscapeOrientation(display_.rotation()));
+void NativeAppWindowTizen::OnSensorConnected() {
+  OnScreenOrientationChanged(
+      SensorProvider::GetInstance()->GetScreenOrientation());
 }
 
-void NativeAppWindowTizen::ApplyDisplayRotation() {
-  if (observer())
-    observer()->OnOrientationChanged(GetCurrentOrientation());
-
-  aura::Window* root_window = GetNativeWindow()->GetRootWindow();
-  if (!root_window->IsVisible())
+void NativeAppWindowTizen::SetDisplayRotation(gfx::Display display) {
+  aura::Window* window = GetNativeWindow()->GetRootWindow();
+  if (!window->IsVisible())
     return;
-  UpdateTopViewOverlay();
-  indicator_widget_->SetDisplay(display_);
-  root_window->SetTransform(GetRotationTransform());
+
+  SetWindowRotation(window, display);
 }
 
 }  // namespace xwalk