Bug 740190 - Screen Orientation API: implement locking in Android. r=dougt

This commit is contained in:
Mounir Lamouri 2012-03-29 23:31:12 -07:00
parent aaec9372e9
commit 15a94acd89
7 changed files with 132 additions and 3 deletions

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -220,12 +220,24 @@ GetCurrentScreenOrientation(dom::ScreenOrientation* aScreenOrientation)
bool
LockScreenOrientation(const dom::ScreenOrientation& aOrientation)
{
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

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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)

View File

@ -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;