get screen orientation

Monkey Targets Forums/Android/get screen orientation

bruZard(Posted 2011) [#1]
maybe someone find it useful:
import android.view.*;

class androidHelper{
	static int getOrientation(){
		android.view.Display display = ((WindowManager)MonkeyGame.activity.getSystemService(android.content.Context.WINDOW_SERVICE)).getDefaultDisplay();
		return display.getOrientation();
	}
	
	static int displayWidth(){
		android.view.Display display = ((WindowManager)MonkeyGame.activity.getSystemService(android.content.Context.WINDOW_SERVICE)).getDefaultDisplay();
		return display.getWidth();
	}
	
	static int displayHeight(){
		android.view.Display display = ((WindowManager)MonkeyGame.activity.getSystemService(android.content.Context.WINDOW_SERVICE)).getDefaultDisplay();
		return display.getHeight();
	}
}

implement in monkey:
#if TARGET = "android"
	Function getOrientation:Int() = "androidHelper.getOrientation"
	Function displayWidth:Int() = "androidHelper.displayWidth"
	Function displayHeight:Int() = "androidHelper.displayHeight"
#EndIf

"getOrientation()" can be 0 for Landscape or 1 for Portrait. Remind that you edit your android manifest from "{SCREEN_ORIENTATION}" to "sensor".


anawiki(Posted 2011) [#2]
Is there a way to detect if the screen is upside down?


therevills(Posted 2011) [#3]
@anawiki - Yes, with getRotation()

@bruZard - Nice work, but can I suggest you use getRotation() as getOrientation() is deprecated.

http://developer.android.com/reference/android/view/Display.html#getRotation%28%29


bruZard(Posted 2011) [#4]
oh, ok ... i've a look at this


bruZard(Posted 2011) [#5]
I've renamed "getOrientation()" to "getRotation()". Now these function returns a Value between 0 - 3
my module defines the following values:
Const ROTATION_0:Int	= 0
Const ROTATION_90:Int	= 1
Const ROTATION_180:Int	= 2
const ROTATION_270:Int	= 3

remind that the code i've posted here not includes these constants.

Android:
import android.view.*;

class androidHelper{
	static int getRotation(){
		android.view.Display display = ((WindowManager)MonkeyGame.activity.getSystemService(android.content.Context.WINDOW_SERVICE)).getDefaultDisplay();
		return display.getRotation();
	}
	
	static int displayWidth(){
		android.view.Display display = ((WindowManager)MonkeyGame.activity.getSystemService(android.content.Context.WINDOW_SERVICE)).getDefaultDisplay();
		return display.getWidth();
	}
	
	static int displayHeight(){
		android.view.Display display = ((WindowManager)MonkeyGame.activity.getSystemService(android.content.Context.WINDOW_SERVICE)).getDefaultDisplay();
		return display.getHeight();
	}
}


Monkey:
#if TARGET = "android"
	Function getRotation:Int() = "androidHelper.getRotation"
	Function displayWidth:Int() = "androidHelper.displayWidth"
	Function displayHeight:Int() = "androidHelper.displayHeight"
#EndIf



bruZard(Posted 2011) [#6]
i forgot to explain the results:

0 = 0
1 = 90
2 = 180
3 = 270


Pudsy(Posted 2011) [#7]
Thanks guys, this is fantastic! :)

Just what I needed to determine whether a device has a default landscape/portrait orientation.

Without this, if you set eg. "landscape" in CONFIG.TXT, Monkey doesn't provide any feedback as to whether the display
has actually rotated or not, and thus whether you may need to rotate any accelerometer-based inputs to match.
And DeviceWidth/Height just return the dimensions *after* any rotation has taken place, so they're no help.

An Android tablet for example doesn't rotate the display if specifying "landscape" (and AccelX() relates to the "longer" axis).
However, a typical phone with portrait display would rotate it (but AccelX() seems to still relate to the "shorter" axis).

Probably the wrong place to ask, but as it's directly related...
I don't suppose anyone knows whether a similar thing would be needed for iPhone/iOS?
Or do they automatically rotate accelerometer readings to match device orientation?
(I don't have access to the hardware at the moment)

This would actually be a useful feature/command to build into Monkey.
Maybe it could even auto-rotate the accelerometer readings accordingly?

As therevills says, getOrientation() is deprecated.
But getRotation() is only available since Android 2.2 (SDK 8).
So I've made a little amendment to check the version first & use getOrientation() if needed...
import android.view.*;
import android.os.*;

class LIBandroid{
	static int getRotation(){
		android.view.Display display = ((WindowManager)MonkeyGame.activity.getSystemService(android.content.Context.WINDOW_SERVICE)).getDefaultDisplay();
		if (android.os.Build.VERSION.SDK_INT < 8)
			return display.getOrientation(); // deprecated, but getRotation() is not available until Android v2.2 (SDK_INT=8)
		else
			return display.getRotation();
	}
	
	static int displayWidth(){
		android.view.Display display = ((WindowManager)MonkeyGame.activity.getSystemService(android.content.Context.WINDOW_SERVICE)).getDefaultDisplay();
		return display.getWidth();
	}
	
	static int displayHeight(){
		android.view.Display display = ((WindowManager)MonkeyGame.activity.getSystemService(android.content.Context.WINDOW_SERVICE)).getDefaultDisplay();
		return display.getHeight();
	}
}

EDIT: text-flow for easier reading due to super-long code!


Samah(Posted 2011) [#8]
If you're targeting an API < 2.2, it won't be able to find the getRotation method and will fail compilation. This is why Mark had to do the dodgy reflection hack for multitouch since those methods didn't exist in older versions of Android.

You might have to do something hackish like this:
if (android.os.Build.VERSION.SDK_INT < 8)
	return display.getOrientation(); // deprecated, but getRotation() is not available until Android v2.2 (SDK_INT=8)
else {
	try {
		Method getRot = display.getClass().getMethod("getRotation");
		return (Integer)getRot.invoke(display);
	} catch(NoSuchMethodException e) {
	} catch(IllegalAccessException e) {
	} catch(InvocationTargetException e) {
	}
	return 0;
}

Disclaimer: I wrote this code in Firefox so it might not work or even compile. :)


Pudsy(Posted 2011) [#9]
Interesting... dunno if there's some weirdness going on here then...

I originally noticed that getRotation() crashed with a runtime error on 2.1 - and subsequently discovered it didn't exist till 2.2

So I found out how to check the API version & made the amendment.

It seems to compile & run ok on a physical Android 2.1 device (as well as an emulated 2.1 device). I guess because it doesn't call getRotation() unless the device is 2.2 or greater.

Unless there's some setting I'm missing that I need to change to target 2.1 (is that the default)? But then I don't suppose it would be working on my 2.1 device if that was the case.

Anyway, I'll give your version a try if I run into any problems.

Thanks!