Bug 1286925 - Add orientation change method in GeckoScreenOrientation; r=snorp
authorJim Chen <nchen@mozilla.com>
Wed, 20 Jul 2016 21:44:48 -0400
changeset 305921 97f65ce2ed0e6b988c413dfe9a2726f2bc15fdfe
parent 305920 f4981006cbcf5fd8f7dce47d47c7fe71b54b8e54
child 305922 8f94a06c6c7f1e43b85d6beafea1245517acae5e
push id79711
push usernchen@mozilla.com
push dateThu, 21 Jul 2016 01:44:59 +0000
treeherdermozilla-inbound@f689b7b832af [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1286925
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1286925 - Add orientation change method in GeckoScreenOrientation; r=snorp Add and use the onOrientationChanged native method in GeckoScreenOrientation in place of the screen orientation event in GeckoEvent.
mobile/android/base/java/org/mozilla/gecko/GeckoScreenOrientation.java
widget/android/GeckoScreenOrientation.h
widget/android/nsAppShell.cpp
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoScreenOrientation.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoScreenOrientation.java
@@ -1,15 +1,17 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
+import org.mozilla.gecko.annotation.WrapForJNI;
+
 import android.content.pm.ActivityInfo;
 import android.content.res.Configuration;
 import android.util.Log;
 import android.view.Surface;
 import android.app.Activity;
 
 import java.util.Arrays;
 import java.util.List;
@@ -125,16 +127,19 @@ public class GeckoScreenOrientation {
      *        Android screen orientation from Configuration.orientation.
      *
      * @return Whether the screen orientation has changed.
      */
     public boolean update(int aAndroidOrientation) {
         return update(getScreenOrientation(aAndroidOrientation, getRotation()));
     }
 
+    @WrapForJNI
+    private static native void onOrientationChange(short screenOrientation, short angle);
+
     /*
      * Update screen orientation given the screen orientation.
      *
      * @param aScreenOrientation
      *        Gecko screen orientation based on android orientation and rotation.
      *
      * @return Whether the screen orientation has changed.
      */
@@ -147,19 +152,23 @@ public class GeckoScreenOrientation {
         if (mShouldNotify) {
             // Gecko expects a definite screen orientation, so we default to the
             // primary orientations.
             if (aScreenOrientation == ScreenOrientation.PORTRAIT) {
                 aScreenOrientation = ScreenOrientation.PORTRAIT_PRIMARY;
             } else if (aScreenOrientation == ScreenOrientation.LANDSCAPE) {
                 aScreenOrientation = ScreenOrientation.LANDSCAPE_PRIMARY;
             }
-            GeckoAppShell.sendEventToGecko(
-                GeckoEvent.createScreenOrientationEvent(aScreenOrientation.value,
-                                                        getAngle()));
+
+            if (GeckoThread.isRunning()) {
+                onOrientationChange(aScreenOrientation.value, getAngle());
+            } else {
+                GeckoThread.queueNativeCall(GeckoScreenOrientation.class, "onOrientationChange",
+                                            aScreenOrientation.value, getAngle());
+            }
         }
         GeckoAppShell.resetScreenSize();
         return true;
     }
 
     /*
      * @return The Android orientation (Configuration.orientation).
      */
new file mode 100644
--- /dev/null
+++ b/widget/android/GeckoScreenOrientation.h
@@ -0,0 +1,56 @@
+/* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef GeckoScreenOrientation_h
+#define GeckoScreenOrientation_h
+
+#include "GeneratedJNINatives.h"
+#include "nsAppShell.h"
+#include "nsCOMPtr.h"
+#include "nsIScreenManager.h"
+
+#include "mozilla/Hal.h"
+#include "mozilla/dom/ScreenOrientation.h"
+
+namespace mozilla {
+
+class GeckoScreenOrientation final
+    : public widget::GeckoScreenOrientation::Natives<GeckoScreenOrientation>
+    , public UsesGeckoThreadProxy
+{
+    GeckoScreenOrientation() = delete;
+
+public:
+    static void
+    OnOrientationChange(int16_t aOrientation, int16_t aAngle)
+    {
+        nsCOMPtr<nsIScreenManager> screenMgr =
+                do_GetService("@mozilla.org/gfx/screenmanager;1");
+        nsCOMPtr<nsIScreen> screen;
+
+        if (!screenMgr || NS_FAILED(screenMgr->GetPrimaryScreen(
+                getter_AddRefs(screen))) || !screen) {
+            return;
+        }
+
+        nsIntRect rect;
+        int32_t colorDepth, pixelDepth;
+
+        if (NS_FAILED(screen->GetRect(&rect.x, &rect.y,
+                                      &rect.width, &rect.height)) ||
+                NS_FAILED(screen->GetColorDepth(&colorDepth)) ||
+                NS_FAILED(screen->GetPixelDepth(&pixelDepth))) {
+            return;
+        }
+
+        hal::NotifyScreenConfigurationChange(hal::ScreenConfiguration(
+                rect, static_cast<dom::ScreenOrientationInternal>(aOrientation),
+                aAngle, colorDepth, pixelDepth));
+    }
+};
+
+} // namespace mozilla
+
+#endif // GeckoScreenOrientation_h
--- a/widget/android/nsAppShell.cpp
+++ b/widget/android/nsAppShell.cpp
@@ -56,16 +56,17 @@
 #endif
 
 #ifdef MOZ_LOGGING
 #include "mozilla/Logging.h"
 #endif
 
 #include "ANRReporter.h"
 #include "GeckoNetworkManager.h"
+#include "GeckoScreenOrientation.h"
 #include "PrefsHelper.h"
 
 #ifdef DEBUG_ANDROID_EVENTS
 #define EVLOG(args...)  ALOG(args)
 #else
 #define EVLOG(args...) do { } while (0)
 #endif
 
@@ -381,16 +382,17 @@ nsAppShell::nsAppShell()
 
     if (jni::IsAvailable()) {
         // Initialize JNI and Set the corresponding state in GeckoThread.
         AndroidBridge::ConstructBridge();
         GeckoAppShellSupport::Init();
         GeckoThreadSupport::Init();
         mozilla::ANRReporter::Init();
         mozilla::GeckoNetworkManager::Init();
+        mozilla::GeckoScreenOrientation::Init();
         mozilla::PrefsHelper::Init();
         nsWindow::InitNatives();
 
         widget::GeckoThread::SetState(widget::GeckoThread::State::JNI_READY());
     }
 
     sPowerManagerService = do_GetService(POWERMANAGERSERVICE_CONTRACTID);