mirror of
https://github.com/apache/cordova-plugin-screen-orientation.git
synced 2025-05-05 01:32:59 +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
|
* Screen Orientation Constants
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Refer to
|
private static final String UNLOCKED = "unlocked";
|
||||||
// http://developer.android.com/reference/android/R.attr.html#screenOrientation
|
private static final String PORTRAIT_PRIMARY = "portrait-primary";
|
||||||
|
private static final String PORTRAIT_SECONDARY = "portrait-secondary";
|
||||||
private static final String UNSPECIFIED = "unspecified";
|
private static final String LANDSCAPE_PRIMARY = "landscape-primary";
|
||||||
private static final String LANDSCAPE = "landscape";
|
private static final String LANDSCAPE_SECONDARY = "landscape-secondary";
|
||||||
private static final String PORTRAIT = "portrait";
|
private static final String PORTRAIT = "portrait";
|
||||||
private static final String USER = "user";
|
private static final String LANDSCAPE = "landscape";
|
||||||
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";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
|
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
|
||||||
@ -84,40 +76,28 @@ public class YoikScreenOrientation extends CordovaPlugin {
|
|||||||
|
|
||||||
Activity activity = cordova.getActivity();
|
Activity activity = cordova.getActivity();
|
||||||
|
|
||||||
if (orientation.equals(UNSPECIFIED)) {
|
if (orientation.equals(UNLOCKED)) {
|
||||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
|
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
|
||||||
} else if (orientation.equals(LANDSCAPE)) {
|
} else if (orientation.equals(LANDSCAPE_PRIMARY)) {
|
||||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
|
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
|
||||||
} else if (orientation.equals(PORTRAIT)) {
|
} else if (orientation.equals(PORTRAIT_PRIMARY)) {
|
||||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
||||||
} else if (orientation.equals(USER)) {
|
} else if (orientation.equals(LANDSCAPE)) {
|
||||||
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)) {
|
|
||||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_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);
|
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);
|
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);
|
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
|
||||||
} else if (orientation.equals(FULL_SENSOR)) {
|
|
||||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
callbackContext.success();
|
callbackContext.success();
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
callbackContext.error("ScreenOrientation not recognised");
|
callbackContext.error("ScreenOrientation not recognised");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -23,23 +23,25 @@ SOFTWARE.
|
|||||||
*/
|
*/
|
||||||
var argscheck = require('cordova/argscheck'),
|
var argscheck = require('cordova/argscheck'),
|
||||||
exec = require('cordova/exec'),
|
exec = require('cordova/exec'),
|
||||||
|
|
||||||
Constants = {
|
Constants = {
|
||||||
Orientation: {
|
Orientation: {
|
||||||
UNSPECIFIED: "unspecified",
|
PORTRAIT_PRIMARY: 'portrait-primary',
|
||||||
LANDSCAPE: "landscape",
|
// The orientation is in the primary portrait mode.
|
||||||
PORTRAIT: "portrait",
|
PORTRAIT_SECONDARY: 'portrait-secondary',
|
||||||
USER: "user",
|
// The orientation is in the secondary portrait mode.
|
||||||
BEHIND: "behind",
|
LANDSCAPE_PRIMARY: 'landscape-primary',
|
||||||
SENSOR: "sensor",
|
// The orientation is in the primary landscape mode.
|
||||||
NOSENSOR: "nosensor",
|
LANDSCAPE_SECONDARY: 'landscape-secondary',
|
||||||
SENSOR_LANDSCAPE: "sensorLandscape",
|
// The orientation is in the secondary landscape mode.
|
||||||
SENSOR_PORTRAIT: "sensorPortrait",
|
PORAIT: 'portrait',
|
||||||
REVERSE_LANDSCAPE: "reverseLandscape",
|
// The orientation is either portrait-primary or portrait-secondary.
|
||||||
REVERSE_PORTRAIT: "reversePortrait",
|
LANDSCAPE: 'landscape',
|
||||||
FULL_SENSOR: "fullSensor"
|
// The orientation is either landscape-primary or landscape-secondary.
|
||||||
|
UNLOCKED: 'unlocked'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
currOrientation = Constants.Orientation.UNSPECIFIED;
|
currOrientation = Constants.Orientation.UNLOCKED;
|
||||||
|
|
||||||
var orientationExports = {};
|
var orientationExports = {};
|
||||||
|
|
||||||
@ -48,46 +50,70 @@ for (var key in Constants) {
|
|||||||
orientationExports[key] = Constants[key];
|
orientationExports[key] = Constants[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
orientationExports.setOrientation = function(successCallback, errorCallback, orientation) {
|
function setOrientation(successCallback, errorCallback, orientation) {
|
||||||
if (typeof successCallback == "string") {
|
|
||||||
orientation = successCallback;
|
|
||||||
successCallback = function(){};
|
|
||||||
errorCallback = function(){};
|
|
||||||
}
|
|
||||||
|
|
||||||
currOrientation = orientation ? orientation : Constants.Orientation.UNSPECIFIED;
|
currOrientation = orientation ? orientation : Constants.Orientation.UNSPECIFIED;
|
||||||
|
|
||||||
exec(successCallback, errorCallback, "YoikScreenOrientation", "screenOrientation", ['set', currOrientation]);
|
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
|
// ios orientation callback/hook
|
||||||
window.shouldRotateToOrientation = function(orientation) {
|
window.shouldRotateToOrientation = function(orientation) {
|
||||||
var o = Constants.Orientation;
|
var o = Constants.Orientation;
|
||||||
switch (currOrientation) {
|
switch (currOrientation) {
|
||||||
case o.PORTRAIT:
|
case o.PORTRAIT:
|
||||||
case o.SENSOR_PORTRAIT:
|
case o.PORTRAIT_PRIMARY:
|
||||||
if (orientation === 0) return true;
|
if (orientation === 0) return true;
|
||||||
break;
|
break;
|
||||||
case o.LANDSCAPE:
|
case o.LANDSCAPE:
|
||||||
case o.SENSOR_LANDSCAPE:
|
case o.LANDSCAPE_PRIMARY:
|
||||||
if (orientation === -90) return true;
|
if (orientation === -90) return true;
|
||||||
break;
|
break;
|
||||||
case o.REVERSE_LANDSCAPE:
|
case o.LANDSCAPE_SECONDARY:
|
||||||
|
case o.LANDSCAPE:
|
||||||
if (orientation === 90) return true;
|
if (orientation === 90) return true;
|
||||||
break;
|
break;
|
||||||
case o.REVERSE_PORTRAIT:
|
case o.PORTRAIT:
|
||||||
|
case o.PORTRAIT_SECONDARY:
|
||||||
if (orientation === 180) return true;
|
if (orientation === 180) return true;
|
||||||
break;
|
break;
|
||||||
case o.FULL_SENSOR:
|
case o.UNLOCKED:
|
||||||
return true;
|
|
||||||
break;
|
|
||||||
case o.SENSOR:
|
|
||||||
case o.UNSPECIFIED:
|
|
||||||
if (orientation === -90 || orientation === 90 || orientation === 0) return true;
|
if (orientation === -90 || orientation === 90 || orientation === 0) return true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports = orientationExports;
|
module.exports = orientationExports;
|
Loading…
x
Reference in New Issue
Block a user