Getting the native rotation is easy from the java side, but it's a bit more tricky for us who use the NDK. So I thought I'd share the code to do it, since it can be quite painful to figure out by yourself
Code: Select all
// Context->App is the android_app*
JNIEnv* jni = 0;
Context->App->activity->vm->AttachCurrentThread(&jni, NULL);
if (jni)
{
jclass classNativeActivity = jni->FindClass("android/app/NativeActivity");
jclass classWindowManager = jni->FindClass("android/view/WindowManager");
jclass classDisplay = jni->FindClass("android/view/Display");
if (classWindowManager)
{
jmethodID idNativeActivity_getWindowManager = jni->GetMethodID(classNativeActivity, "getWindowManager", "()Landroid/view/WindowManager;");
jmethodID idWindowManager_getDefaultDisplay = jni->GetMethodID(classWindowManager, "getDefaultDisplay", "()Landroid/view/Display;");
jmethodID idWindowManager_getRotation = jni->GetMethodID(classDisplay, "getRotation", "()I");
if (idWindowManager_getRotation)
{
jobject windowManager = jni->CallObjectMethod(Context->App->activity->clazz, idNativeActivity_getWindowManager);
if (windowManager)
{
jobject display = jni->CallObjectMethod(windowManager, idWindowManager_getDefaultDisplay);
if (display)
{
int rotation = jni->CallIntMethod(display, idWindowManager_getRotation);
if (rotation == 0 || rotation == 2)
{
Context->ScreenRotation = ESR_PORTRAIT;
debugLog("default screen rotation is PORTRAIT");
}
else
{
Context->ScreenRotation = ESR_LANDSCAPE;
debugLog("default screen rotation is LANDSCAPE");
}
}
}
}
}
Context->App->activity->vm->DetachCurrentThread();
}