From 2934bea321ccd5bf4d49e64560a2cb8020a1977e Mon Sep 17 00:00:00 2001 From: Grant Benvenuti Date: Sat, 12 Jul 2014 16:05:30 +1000 Subject: [PATCH 1/3] New screenOrientation api, working minus checks and event firing --- .../YoikScreenOrientation.java | 46 +++------- www/screenorientation.js | 90 ++++++++++++------- 2 files changed, 71 insertions(+), 65 deletions(-) diff --git a/src/android/net/yoik/cordova/plugins/screenorientation/YoikScreenOrientation.java b/src/android/net/yoik/cordova/plugins/screenorientation/YoikScreenOrientation.java index efe36f1..bce750b 100644 --- a/src/android/net/yoik/cordova/plugins/screenorientation/YoikScreenOrientation.java +++ b/src/android/net/yoik/cordova/plugins/screenorientation/YoikScreenOrientation.java @@ -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; } } - } \ No newline at end of file diff --git a/www/screenorientation.js b/www/screenorientation.js index 1614453..0b498c8 100644 --- a/www/screenorientation.js +++ b/www/screenorientation.js @@ -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; \ No newline at end of file From 1e4a7b18c440370f353c0a6cc2425ec8394dc5eb Mon Sep 17 00:00:00 2001 From: Grant Benvenuti Date: Sun, 13 Jul 2014 09:26:44 +1000 Subject: [PATCH 2/3] Updated to attach functions directly to screen object and constant cleanup --- www/screenorientation.js | 91 +++++++++++++++------------------------- 1 file changed, 33 insertions(+), 58 deletions(-) diff --git a/www/screenorientation.js b/www/screenorientation.js index 0b498c8..a53af30 100644 --- a/www/screenorientation.js +++ b/www/screenorientation.js @@ -23,45 +23,26 @@ SOFTWARE. */ var argscheck = require('cordova/argscheck'), exec = require('cordova/exec'), - - Constants = { - Orientation: { - 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.UNLOCKED; + Orientations = [ + 'portrait-primary', + // The orientation is in the primary portrait mode. + 'portrait-secondary', + // The orientation is in the secondary portrait mode. + 'landscape-primary', + // The orientation is in the primary landscape mode. + 'landscape-secondary', + // The orientation is in the secondary landscape mode. + 'portrait', + // The orientation is either portrait-primary or portrait-secondary. + 'landscape' + ], + currOrientation = 'unlocked'; var orientationExports = {}; -// Tack on the orientation Constants to the base plugin. -for (var key in Constants) { - orientationExports[key] = Constants[key]; -} - -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 setOrientation(orientation) { + currOrientation = orientation ? orientation : 'unlocked'; + exec(null, null, "YoikScreenOrientation", "screenOrientation", ['set', currOrientation]); } function addScreenOrientationApi(obj) { @@ -69,21 +50,16 @@ function addScreenOrientationApi(obj) { 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); + if (Orientations.indexOf(orientation) == -1) { + console.log('INVALID ORIENTATION', orientation); + return; + } + setOrientation(orientation); }; obj.unlockOrientation = function() { - setOrientation(successCallback, errorCallback, Constants.Orientation.UNLOCKED); + setOrientation('unlocked'); }; } @@ -91,29 +67,28 @@ addScreenOrientationApi(screen); // ios orientation callback/hook window.shouldRotateToOrientation = function(orientation) { - var o = Constants.Orientation; switch (currOrientation) { - case o.PORTRAIT: - case o.PORTRAIT_PRIMARY: + case 'portrait': + case 'portrait-primary': if (orientation === 0) return true; break; - case o.LANDSCAPE: - case o.LANDSCAPE_PRIMARY: + case 'landscape': + case 'landscape-primary': if (orientation === -90) return true; break; - case o.LANDSCAPE_SECONDARY: - case o.LANDSCAPE: + case 'landscape': + case 'landscape-secondary': if (orientation === 90) return true; break; - case o.PORTRAIT: - case o.PORTRAIT_SECONDARY: + case 'portrait': + case 'portrait-secondary': if (orientation === 180) return true; break; - case o.UNLOCKED: + default: if (orientation === -90 || orientation === 90 || orientation === 0) return true; break; } return false; }; -module.exports = orientationExports; \ No newline at end of file +module.exports = {}; \ No newline at end of file From 82394e15e75542f3fe8c02978fa6f72d1f5eeabf Mon Sep 17 00:00:00 2001 From: Grant Benvenuti Date: Sun, 13 Jul 2014 09:34:21 +1000 Subject: [PATCH 3/3] Readme and plugin.xml update --- README.md | 81 +++++++++++++++++++++++++++++++++--------------------- plugin.xml | 4 +-- 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index c4dd024..c49334c 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,62 @@ #cordova-yoik-screenorientation -Cordova plugin to set/lock the screen orientation in a common way for both iOS and Android. +Cordova plugin to set/lock the screen orientation in a common way for both iOS and Android. From version 1.0.0 the +interface is based on the [Screen Orientation API](http://www.w3.org/TR/screen-orientation/). ##Install -cordova plugin add https://github.com/yoik/cordova-yoik-screenorientation +cordova plugin add net.yoik.cordova.plugins.screenorientation -###Android +###Source +https://github.com/yoik/cordova-yoik-screenorientation -The android version is implemented via the standard _activity.setRequestedOrientation_ as used in other screen orientation plugins -###iOS +##Orientations + +__portrait-primary__ +The orientation is in the primary portrait mode. + +__portrait-secondary__ +The orientation is in the secondary portrait mode. + +__landscape-primary__ +The orientation is in the primary landscape mode. + +__landscape-secondary__ +The orientation is in the secondary landscape mode. + +##Usage + + screen.lockOrientation('landscape'); + + screen.unlockOrientation(); + +##Events + +Both android and iOS will fire the orientationchange event on the window object. +For this version of the plugin use the window object if you require notification. + +i.e. + + function init() { + window.addEventListener("orientationchange", orientationChange, true); + } + + function orientationChange(e) { + var orientation="portrait"; + if(window.orientation == -90 || window.orientation == 90) orientation = "landscape"; + document.getElementById("status").innerHTML+=orientation+"
"; + } + +For this plugin to follow the API events should be fired on the screen object. +iOS does not currently support events on the _screen_ object so custom event +handling will need to be added (Suggestions welcome!). + +##iOS Notes The iOS version is a combination of the cordova JS callback _window.shouldRotateToOrientation_ and the workaround to recheck the orientation as implemented in https://github.com/Adlotto/cordova-plugin-recheck-screen-orientation. -If you have a custom impelemntation of the _window.shouldRotateToOrientation_ it will have to be removed for the plugin to function as expected. +__If you have a custom implementation of the _window.shouldRotateToOrientation_ it will have to be removed for the plugin to function as expected.__ ####iOS6 @@ -24,30 +66,5 @@ Issue [#1](https://github.com/yoik/cordova-yoik-screenorientation/issues/1) @dok >It seems to be related to having width=device-width, height=device-height in the meta viewport (which is part of the boilerplate phonegap/cordova app). It can be solved by updating the viewport with width=device-height, height=device-width or simply removing width and height altogether. -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" - } -Usage -==== - - var so = cordova.plugins.screenorientation; - - // with callbacks - so.setOrientation(successCallback, errorCallback, so.Orientation.PORTRAIT); - - // no callbacks - so.setOrientation(so.Orientation.SENSOR_LANDSCAPE); +Pull requests welcome. diff --git a/plugin.xml b/plugin.xml index af5c5ad..47d4c04 100644 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="1.0.0"> YoikScreenOrientation Yoik Screen Orientation Plugin @@ -36,4 +36,4 @@ - \ No newline at end of file +