I wrote a cordova plugin, OrientationPlugin
for Android that allows to lock the app into portrait or landscape mode for certain screens.
orientationplugin
├── src
│ └── android
│ └── OrientationPlugin.java
├── www
│ └── orientation.js
└── plugin.xml
package net.jsloop.android.orientation;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import android.content.pm.ActivityInfo;
import android.util.Log;
public class OrientationPlugin extends CordovaPlugin {
private final String pluginName = "OrientationPlugin";
public boolean execute(final String action, final JSONArray data, final CallbackContext callBackContext) {
Log.d(pluginName, "OrientationPlugin called with options: " + data);
if (action.equals("portrait")) {
cordova.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (action.equals("landscape")) {
cordova.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else if (action.equals("both")) {
cordova.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
return true;
}
}
/* global cordova module */
function exec(success, error, className, methodName, argsArr) {
cordova.exec(success, error, className, methodName, argsArr);
}
module.exports = {
portrait: function (name, success, error) {
exec(success, error, "OrientationPlugin", "portrait", []);
},
landscape: function (success, error) {
exec(success, error, "OrientationPlugin", "landscape", []);
},
both: function (success, error) {
exec(success, error, "OrientationPlugin", "both", []);
}
};
<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
id="net.jsloop.android.orientation"
version="1.0.0">
<name>OrientationPlugin</name>
<engines>
<engine name="cordova" version=">=3.4.0"/>
</engines>
<asset src="www/orientation.js" target="orientation.js"/>
<js-module src="www/orientation.js" name="orientation">
<clobbers target="orientation" />
</js-module>
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="OrientationPlugin">
<param name="android-package" value="net.jsloop.android.orientation.OrientationPlugin"/>
</feature>
</config-file>
<source-file src="src/android/OrientationPlugin.java" target-dir="src/net/jsloop/android/orientation/"/>
</platform>
</plugin>