[Android NDK] Get default screen rotation

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

[Android NDK] Get default screen rotation

Post by ent1ty »

An Android device can consider it's "default" rotation to be either portrait or landscape, depending on whether it's a phone or a tablet (then again my xperia mini with a 480x320 screen considers it's native rotation to be landscape... ). This has an effect on how the device interprets the coordinate system in which accelerometer, gyroscope etc. input are expressed. You can read more on this in a nifty paper here: http://developer.download.nvidia.com/te ... er_v5f.pdf

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 :P

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();
            }
 
Thanks to CuteAlien and his android_tools in the Android hello world example, I basically only added one more function call.
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Post Reply