mirror of
https://github.com/apache/cordova-plugin-screen-orientation.git
synced 2025-02-23 08:32:51 +08:00
New screenOrientation api, working minus checks and event firing
This commit is contained in:
parent
69996bc692
commit
2934bea321
@ -41,21 +41,13 @@ public class YoikScreenOrientation extends CordovaPlugin {
|
||||
* Screen Orientation Constants
|
||||
*/
|
||||
|
||||
// Refer to
|
||||
// http://developer.android.com/reference/android/R.attr.html#screenOrientation
|
||||
|
||||
private static final String UNSPECIFIED = "unspecified";
|
||||
private static final String LANDSCAPE = "landscape";
|
||||
private static final String UNLOCKED = "unlocked";
|
||||
private static final String PORTRAIT_PRIMARY = "portrait-primary";
|
||||
private static final String PORTRAIT_SECONDARY = "portrait-secondary";
|
||||
private static final String LANDSCAPE_PRIMARY = "landscape-primary";
|
||||
private static final String LANDSCAPE_SECONDARY = "landscape-secondary";
|
||||
private static final String PORTRAIT = "portrait";
|
||||
private static final String USER = "user";
|
||||
private static final String BEHIND = "behind";
|
||||
private static final String SENSOR = "sensor";
|
||||
private static final String NOSENSOR = "nosensor";
|
||||
private static final String SENSOR_LANDSCAPE = "sensorLandscape";
|
||||
private static final String SENSOR_PORTRAIT = "sensorPortrait";
|
||||
private static final String REVERSE_LANDSCAPE = "reverseLandscape";
|
||||
private static final String REVERSE_PORTRAIT = "reversePortrait";
|
||||
private static final String FULL_SENSOR = "fullSensor";
|
||||
private static final String LANDSCAPE = "landscape";
|
||||
|
||||
@Override
|
||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
|
||||
@ -84,40 +76,28 @@ public class YoikScreenOrientation extends CordovaPlugin {
|
||||
|
||||
Activity activity = cordova.getActivity();
|
||||
|
||||
if (orientation.equals(UNSPECIFIED)) {
|
||||
if (orientation.equals(UNLOCKED)) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
|
||||
} else if (orientation.equals(LANDSCAPE)) {
|
||||
} else if (orientation.equals(LANDSCAPE_PRIMARY)) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
|
||||
} else if (orientation.equals(PORTRAIT)) {
|
||||
} else if (orientation.equals(PORTRAIT_PRIMARY)) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
||||
} else if (orientation.equals(USER)) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
|
||||
} else if (orientation.equals(BEHIND)) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_BEHIND);
|
||||
} else if (orientation.equals(SENSOR)) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
|
||||
} else if (orientation.equals(NOSENSOR)) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
|
||||
} else if (orientation.equals(SENSOR_LANDSCAPE)) {
|
||||
} else if (orientation.equals(LANDSCAPE)) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
|
||||
} else if (orientation.equals(SENSOR_PORTRAIT)) {
|
||||
} else if (orientation.equals(PORTRAIT)) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
|
||||
} else if (orientation.equals(REVERSE_LANDSCAPE)) {
|
||||
} else if (orientation.equals(LANDSCAPE_SECONDARY)) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
|
||||
} else if (orientation.equals(REVERSE_PORTRAIT)) {
|
||||
} else if (orientation.equals(PORTRAIT_SECONDARY)) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
|
||||
} else if (orientation.equals(FULL_SENSOR)) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
|
||||
}
|
||||
|
||||
callbackContext.success();
|
||||
return true;
|
||||
|
||||
} else {
|
||||
|
||||
callbackContext.error("ScreenOrientation not recognised");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -23,23 +23,25 @@ SOFTWARE.
|
||||
*/
|
||||
var argscheck = require('cordova/argscheck'),
|
||||
exec = require('cordova/exec'),
|
||||
|
||||
Constants = {
|
||||
Orientation: {
|
||||
UNSPECIFIED: "unspecified",
|
||||
LANDSCAPE: "landscape",
|
||||
PORTRAIT: "portrait",
|
||||
USER: "user",
|
||||
BEHIND: "behind",
|
||||
SENSOR: "sensor",
|
||||
NOSENSOR: "nosensor",
|
||||
SENSOR_LANDSCAPE: "sensorLandscape",
|
||||
SENSOR_PORTRAIT: "sensorPortrait",
|
||||
REVERSE_LANDSCAPE: "reverseLandscape",
|
||||
REVERSE_PORTRAIT: "reversePortrait",
|
||||
FULL_SENSOR: "fullSensor"
|
||||
PORTRAIT_PRIMARY: 'portrait-primary',
|
||||
// The orientation is in the primary portrait mode.
|
||||
PORTRAIT_SECONDARY: 'portrait-secondary',
|
||||
// The orientation is in the secondary portrait mode.
|
||||
LANDSCAPE_PRIMARY: 'landscape-primary',
|
||||
// The orientation is in the primary landscape mode.
|
||||
LANDSCAPE_SECONDARY: 'landscape-secondary',
|
||||
// The orientation is in the secondary landscape mode.
|
||||
PORAIT: 'portrait',
|
||||
// The orientation is either portrait-primary or portrait-secondary.
|
||||
LANDSCAPE: 'landscape',
|
||||
// The orientation is either landscape-primary or landscape-secondary.
|
||||
UNLOCKED: 'unlocked'
|
||||
}
|
||||
},
|
||||
currOrientation = Constants.Orientation.UNSPECIFIED;
|
||||
currOrientation = Constants.Orientation.UNLOCKED;
|
||||
|
||||
var orientationExports = {};
|
||||
|
||||
@ -48,46 +50,70 @@ for (var key in Constants) {
|
||||
orientationExports[key] = Constants[key];
|
||||
}
|
||||
|
||||
orientationExports.setOrientation = function(successCallback, errorCallback, orientation) {
|
||||
if (typeof successCallback == "string") {
|
||||
orientation = successCallback;
|
||||
successCallback = function(){};
|
||||
errorCallback = function(){};
|
||||
}
|
||||
|
||||
function setOrientation(successCallback, errorCallback, orientation) {
|
||||
currOrientation = orientation ? orientation : Constants.Orientation.UNSPECIFIED;
|
||||
|
||||
exec(successCallback, errorCallback, "YoikScreenOrientation", "screenOrientation", ['set', currOrientation]);
|
||||
};
|
||||
}
|
||||
|
||||
function fireEvent(obj) {
|
||||
var event = document.createEvent('HTMLEvents');
|
||||
|
||||
event.initEvent('orientationchange', true, true);
|
||||
event.eventName = 'orientationchange';
|
||||
// screen.dispatchEvent(event);
|
||||
}
|
||||
|
||||
function addScreenOrientationApi(obj) {
|
||||
if (obj.unlockOrientation || obj.lockOrientation) {
|
||||
return;
|
||||
}
|
||||
|
||||
var successCallback = function() {
|
||||
fireEvent(obj);
|
||||
},
|
||||
errorCallback = function(){};
|
||||
|
||||
obj.lockOrientation = function(orientation) {
|
||||
// if (Object.keys(Constants.Orientation).indexOf(orientation) == -1) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
setOrientation(successCallback, errorCallback, orientation);
|
||||
};
|
||||
|
||||
obj.unlockOrientation = function() {
|
||||
setOrientation(successCallback, errorCallback, Constants.Orientation.UNLOCKED);
|
||||
};
|
||||
}
|
||||
|
||||
addScreenOrientationApi(screen);
|
||||
|
||||
// ios orientation callback/hook
|
||||
window.shouldRotateToOrientation = function(orientation) {
|
||||
var o = Constants.Orientation;
|
||||
switch (currOrientation) {
|
||||
case o.PORTRAIT:
|
||||
case o.SENSOR_PORTRAIT:
|
||||
case o.PORTRAIT_PRIMARY:
|
||||
if (orientation === 0) return true;
|
||||
break;
|
||||
case o.LANDSCAPE:
|
||||
case o.SENSOR_LANDSCAPE:
|
||||
case o.LANDSCAPE_PRIMARY:
|
||||
if (orientation === -90) return true;
|
||||
break;
|
||||
case o.REVERSE_LANDSCAPE:
|
||||
case o.LANDSCAPE_SECONDARY:
|
||||
case o.LANDSCAPE:
|
||||
if (orientation === 90) return true;
|
||||
break;
|
||||
case o.REVERSE_PORTRAIT:
|
||||
case o.PORTRAIT:
|
||||
case o.PORTRAIT_SECONDARY:
|
||||
if (orientation === 180) return true;
|
||||
break;
|
||||
case o.FULL_SENSOR:
|
||||
return true;
|
||||
break;
|
||||
case o.SENSOR:
|
||||
case o.UNSPECIFIED:
|
||||
case o.UNLOCKED:
|
||||
if (orientation === -90 || orientation === 90 || orientation === 0) return true;
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = orientationExports;
|
Loading…
Reference in New Issue
Block a user