I've noticed that there are apps that let you control the orientation of the device, even though they are not the foreground apps, such as these:
https://play.google.com/store/apps/details?id=nl.fameit.rotate&hl=en https://play.google.com/store/apps/details?id=com.googlecode.eyesfree.setorientation&hl=en
I know it's possible to change orientation of the current app, and I know you can handle orientation using adb commands, but how do you do it for any app, using the official API ?