Expected Behaviour:
If device auto-rotation is OFF, app screen should not rotate on device rotation.
Actual Behaviour seen on a default Iris App:
Even if device auto-rotation is OFF, app screen rotates from portrait to reverse-portrait (**Note: not all Android devices support reverse-portrait mode) or landscape to reverse-landscape.
By default, Volt MX Iris app enables sensory-rotation.
After android app is built, the generated Android project (<WORKSPACE>/temp/<APP_ID>/build/luaandroid/dist/<APP_ID>/ will reveal the below in the AndroidManifest.xml.
android:screenOrientation="sensor"
To overcome this, we have to make the following changes:
- overwrite the android:screenOrientation tag
- overwrite the "onCreate" function of the Main App Activity class. This can be found in the "src" folder inside the generated android project mentioned above.
Sample code mentioned here are for a portrait mode scenario.
1) Add an entry in Iris project settings, as shown below.
2) Overwrite the generated main activity class as shown below.
public class Test extends KonyMain {
private static Test context;
public boolean isAutoRotateEnabled() {
boolean isAutoRotate = false;
try {
isAutoRotate = android.provider.Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1) == 1;
} catch (Exception e) {
e.printStackTrace();
}
return isAutoRotate;
}
OrientationEventListener mOrientationListener;
public void onCreate(Bundle savedInstanceState) {
context = this;
super.onCreate(savedInstanceState);
if (!isAutoRotateEnabled()) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
mOrientationListener = new OrientationEventListener(Test.this, SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
if (!isAutoRotateEnabled()) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
}
};
mOrientationListener.enable();
}
// Leave the rest of code - not adding here to keep it simple for demo purpose
}
To avoid manually updating the generated Main activity class, we should use "androidprecompiletask.xml". You can find more details in the link below.
Android Pre-compile and Post-compile Ant Tasks Support
I have attached a sample (rename from .txt to .xml) of the same.
Hope this helps.
Contributors: Subhajit Saha, John Talluri
androidprecompiletask.txt