From 15a94acd89ee7b2d33adb32a98b641608d2dacc8 Mon Sep 17 00:00:00 2001 From: Mounir Lamouri Date: Thu, 29 Mar 2012 23:31:12 -0700 Subject: [PATCH] Bug 740190 - Screen Orientation API: implement locking in Android. r=dougt --- embedding/android/GeckoAppShell.java | 8 ++++ .../GeckoScreenOrientationListener.java | 43 ++++++++++++++++++- hal/android/AndroidHal.cpp | 14 +++++- mobile/android/base/GeckoAppShell.java | 8 ++++ .../base/GeckoScreenOrientationListener.java | 43 ++++++++++++++++++- widget/android/AndroidBridge.cpp | 15 +++++++ widget/android/AndroidBridge.h | 4 ++ 7 files changed, 132 insertions(+), 3 deletions(-) diff --git a/embedding/android/GeckoAppShell.java b/embedding/android/GeckoAppShell.java index 237d2f263cf..02404028618 100644 --- a/embedding/android/GeckoAppShell.java +++ b/embedding/android/GeckoAppShell.java @@ -1855,4 +1855,12 @@ public class GeckoAppShell public static void disableScreenOrientationNotifications() { GeckoScreenOrientationListener.getInstance().disableNotifications(); } + + public static void lockScreenOrientation(int aOrientation) { + GeckoScreenOrientationListener.getInstance().lockScreenOrientation(aOrientation); + } + + public static void unlockScreenOrientation() { + GeckoScreenOrientationListener.getInstance().unlockScreenOrientation(); + } } diff --git a/embedding/android/GeckoScreenOrientationListener.java b/embedding/android/GeckoScreenOrientationListener.java index 440833db7d7..0bcbb96127f 100644 --- a/embedding/android/GeckoScreenOrientationListener.java +++ b/embedding/android/GeckoScreenOrientationListener.java @@ -8,9 +8,12 @@ import android.content.Context; import android.util.Log; import android.view.OrientationEventListener; import android.view.Surface; +import android.content.pm.ActivityInfo; public class GeckoScreenOrientationListener { + private static final String LOGTAG = "GeckoScreenOrientationListener"; + static class OrientationEventListenerImpl extends OrientationEventListener { public OrientationEventListenerImpl(Context c) { super(c); @@ -25,10 +28,13 @@ public class GeckoScreenOrientationListener static private GeckoScreenOrientationListener sInstance = null; // Make sure that any change in dom/base/ScreenOrientation.h happens here too. + static public final short eScreenOrientation_None = 0; static public final short eScreenOrientation_PortraitPrimary = 1; static public final short eScreenOrientation_PortraitSecondary = 2; + static public final short eScreenOrientation_Portrait = 3; static public final short eScreenOrientation_LandscapePrimary = 4; static public final short eScreenOrientation_LandscapeSecondary = 8; + static public final short eScreenOrientation_Landscape = 12; private short mOrientation; private OrientationEventListenerImpl mListener = null; @@ -107,7 +113,7 @@ public class GeckoScreenOrientationListener } else if (rotation == Surface.ROTATION_90) { mOrientation = eScreenOrientation_LandscapePrimary; } else { - Log.e("GeckoScreenOrientationListener", "Unexpected value received! (" + rotation + ")"); + Log.e(LOGTAG, "Unexpected value received! (" + rotation + ")"); return; } @@ -119,4 +125,39 @@ public class GeckoScreenOrientationListener public short getScreenOrientation() { return mOrientation; } + + public void lockScreenOrientation(int aOrientation) { + int orientation = 0; + + switch (aOrientation) { + case eScreenOrientation_PortraitPrimary: + orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; + break; + case eScreenOrientation_PortraitSecondary: + orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; + break; + case eScreenOrientation_Portrait: + orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT; + break; + case eScreenOrientation_LandscapePrimary: + orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; + break; + case eScreenOrientation_LandscapeSecondary: + orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; + break; + case eScreenOrientation_Landscape: + orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE; + break; + default: + Log.e(LOGTAG, "Unexpected value received! (" + aOrientation + ")"); + } + + GeckoApp.mAppContext.setRequestedOrientation(orientation); + updateScreenOrientation(); + } + + public void unlockScreenOrientation() { + GeckoApp.mAppContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); + updateScreenOrientation(); + } } diff --git a/hal/android/AndroidHal.cpp b/hal/android/AndroidHal.cpp index 22b75dbe2cb..942bae2d827 100644 --- a/hal/android/AndroidHal.cpp +++ b/hal/android/AndroidHal.cpp @@ -220,12 +220,24 @@ GetCurrentScreenOrientation(dom::ScreenOrientation* aScreenOrientation) bool LockScreenOrientation(const dom::ScreenOrientation& aOrientation) { - return false; + AndroidBridge* bridge = AndroidBridge::Bridge(); + if (!bridge) { + return false; + } + + bridge->LockScreenOrientation(dom::ScreenOrientationWrapper(aOrientation)); + return true; } void UnlockScreenOrientation() { + AndroidBridge* bridge = AndroidBridge::Bridge(); + if (!bridge) { + return; + } + + bridge->UnlockScreenOrientation(); } } // hal_impl diff --git a/mobile/android/base/GeckoAppShell.java b/mobile/android/base/GeckoAppShell.java index a007c07a99b..3172e36310a 100644 --- a/mobile/android/base/GeckoAppShell.java +++ b/mobile/android/base/GeckoAppShell.java @@ -2052,4 +2052,12 @@ public class GeckoAppShell public static void disableScreenOrientationNotifications() { GeckoScreenOrientationListener.getInstance().disableNotifications(); } + + public static void lockScreenOrientation(int aOrientation) { + GeckoScreenOrientationListener.getInstance().lockScreenOrientation(aOrientation); + } + + public static void unlockScreenOrientation() { + GeckoScreenOrientationListener.getInstance().unlockScreenOrientation(); + } } diff --git a/mobile/android/base/GeckoScreenOrientationListener.java b/mobile/android/base/GeckoScreenOrientationListener.java index 64738fb861a..09dc55a29e4 100644 --- a/mobile/android/base/GeckoScreenOrientationListener.java +++ b/mobile/android/base/GeckoScreenOrientationListener.java @@ -8,9 +8,12 @@ import android.content.Context; import android.util.Log; import android.view.OrientationEventListener; import android.view.Surface; +import android.content.pm.ActivityInfo; public class GeckoScreenOrientationListener { + private static final String LOGTAG = "GeckoScreenOrientationListener"; + static class OrientationEventListenerImpl extends OrientationEventListener { public OrientationEventListenerImpl(Context c) { super(c); @@ -25,10 +28,13 @@ public class GeckoScreenOrientationListener static private GeckoScreenOrientationListener sInstance = null; // Make sure that any change in dom/base/ScreenOrientation.h happens here too. + static public final short eScreenOrientation_None = 0; static public final short eScreenOrientation_PortraitPrimary = 1; static public final short eScreenOrientation_PortraitSecondary = 2; + static public final short eScreenOrientation_Portrait = 3; static public final short eScreenOrientation_LandscapePrimary = 4; static public final short eScreenOrientation_LandscapeSecondary = 8; + static public final short eScreenOrientation_Landscape = 12; private short mOrientation; private OrientationEventListenerImpl mListener = null; @@ -107,7 +113,7 @@ public class GeckoScreenOrientationListener } else if (rotation == Surface.ROTATION_90) { mOrientation = eScreenOrientation_LandscapePrimary; } else { - Log.e("GeckoScreenOrientationListener", "Unexpected value received! (" + rotation + ")"); + Log.e(LOGTAG, "Unexpected value received! (" + rotation + ")"); return; } @@ -119,4 +125,39 @@ public class GeckoScreenOrientationListener public short getScreenOrientation() { return mOrientation; } + + public void lockScreenOrientation(int aOrientation) { + int orientation = 0; + + switch (aOrientation) { + case eScreenOrientation_PortraitPrimary: + orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; + break; + case eScreenOrientation_PortraitSecondary: + orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; + break; + case eScreenOrientation_Portrait: + orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT; + break; + case eScreenOrientation_LandscapePrimary: + orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; + break; + case eScreenOrientation_LandscapeSecondary: + orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; + break; + case eScreenOrientation_Landscape: + orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE; + break; + default: + Log.e(LOGTAG, "Unexpected value received! (" + aOrientation + ")"); + } + + GeckoApp.mAppContext.setRequestedOrientation(orientation); + updateScreenOrientation(); + } + + public void unlockScreenOrientation() { + GeckoApp.mAppContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); + updateScreenOrientation(); + } } diff --git a/widget/android/AndroidBridge.cpp b/widget/android/AndroidBridge.cpp index ea5d06ea610..4c73390ed0a 100644 --- a/widget/android/AndroidBridge.cpp +++ b/widget/android/AndroidBridge.cpp @@ -184,6 +184,8 @@ AndroidBridge::Init(JNIEnv *jEnv, jGetScreenOrientation = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "getScreenOrientation", "()S"); jEnableScreenOrientationNotifications = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "enableScreenOrientationNotifications", "()V"); jDisableScreenOrientationNotifications = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "disableScreenOrientationNotifications", "()V"); + jLockScreenOrientation = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "lockScreenOrientation", "(I)V"); + jUnlockScreenOrientation = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "unlockScreenOrientation", "()V"); jEGLContextClass = (jclass) jEnv->NewGlobalRef(jEnv->FindClass("javax/microedition/khronos/egl/EGLContext")); jEGL10Class = (jclass) jEnv->NewGlobalRef(jEnv->FindClass("javax/microedition/khronos/egl/EGL10")); @@ -2095,6 +2097,19 @@ AndroidBridge::DisableScreenOrientationNotifications() mJNIEnv->CallStaticVoidMethod(mGeckoAppShellClass, jDisableScreenOrientationNotifications); } +void +AndroidBridge::LockScreenOrientation(const dom::ScreenOrientationWrapper& aOrientation) +{ + ALOG_BRIDGE("AndroidBridge::LockScreenOrientation"); + mJNIEnv->CallStaticVoidMethod(mGeckoAppShellClass, jLockScreenOrientation, aOrientation.orientation); +} + +void +AndroidBridge::UnlockScreenOrientation() +{ + ALOG_BRIDGE("AndroidBridge::UnlockScreenOrientation"); + mJNIEnv->CallStaticVoidMethod(mGeckoAppShellClass, jUnlockScreenOrientation); +} /* attribute nsIAndroidBrowserApp browserApp; */ NS_IMETHODIMP nsAndroidBridge::GetBrowserApp(nsIAndroidBrowserApp * *aBrowserApp) diff --git a/widget/android/AndroidBridge.h b/widget/android/AndroidBridge.h index 6f60ebf5985..bb227068c7b 100644 --- a/widget/android/AndroidBridge.h +++ b/widget/android/AndroidBridge.h @@ -422,6 +422,8 @@ public: void GetScreenOrientation(dom::ScreenOrientationWrapper& aOrientation); void EnableScreenOrientationNotifications(); void DisableScreenOrientationNotifications(); + void LockScreenOrientation(const dom::ScreenOrientationWrapper& aOrientation); + void UnlockScreenOrientation(); protected: static AndroidBridge *sBridge; @@ -530,6 +532,8 @@ protected: jmethodID jGetScreenOrientation; jmethodID jEnableScreenOrientationNotifications; jmethodID jDisableScreenOrientationNotifications; + jmethodID jLockScreenOrientation; + jmethodID jUnlockScreenOrientation; // stuff we need for CallEglCreateWindowSurface jclass jEGLSurfaceImplClass;