diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6df9abc --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +#If ignorance is bliss, then somebody knock the smile off my face + +*.csproj.user +*.suo +*.cache +Thumbs.db +*.DS_Store + +*.bak +*.cache +*.log +*.swp +*.user + +demo/hooks/ +demo/platforms/ +demo/plugins/ + +.vscode/ + + + + + + + + + + diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..62e8c49 --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +demo/ +tests/ diff --git a/README.md b/README.md index 58d2918..fe022b0 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ # Cordova Screen Orientation Plugin -Cordova plugin to set/lock the screen orientation in a common way for iOS, Android, WP8 and Blackberry 10. This plugin is based on an early version of [Screen Orientation API](http://www.w3.org/TR/screen-orientation/) so the api does not currently match the current spec. +Cordova plugin to set/lock the screen orientation in a common way for iOS, Android, WP8 and Blackberry 10. This plugin is based on [Screen Orientation API](http://www.w3.org/TR/screen-orientation/) so the api matches the current spec. The plugin adds the following to the screen object (`window.screen`): @@ -36,12 +36,6 @@ The plugin adds the following to the screen object (`window.screen`): ## Install -_cordova < 4_ - -```bash -cordova plugin add net.yoik.cordova.plugins.screenorientation -``` -_cordova > 4_ ```bash cordova plugin add cordova-plugin-screen-orientation @@ -67,6 +61,9 @@ cordova plugin add cordova-plugin-screen-orientation #### landscape > The orientation is either landscape-primary or landscape-secondary (sensor). +#### any +> orientation is unlocked - all orientations are supported. + ## Usage ```js @@ -85,9 +82,6 @@ console.log('Orientation is ' + screen.orientation); 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. -For this plugin to follow the full API events should be fired on the screen object. -iOS and BB10 do not currently support events on the _screen_ object so custom event -handling will need to be added (Suggestions welcome!). ### Example usage @@ -101,28 +95,11 @@ window.addEventListener("orientationchange", function(){ The __screen.orientation__ property will not update when the phone is [rotated 180 degrees](http://www.quirksmode.org/dom/events/orientationchange.html). -## 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 implementation of the _window.shouldRotateToOrientation_ it will have to be removed for the plugin to function as expected.__ - -#### iOS6 - -There has been a few cases where the rotation does not change the width of the viewport - -Issue [#1](https://github.com/gbenvenuti/cordova-plugin-screen-orientation/issues/1) @dokterbob - ->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. - -#### iOS8 - -Versions prior to 1.2.0 will cause an application crash in iOS8 due to a change in presentViewController timing. - ## BB10 Notes Wraps the com.blackberry.app plugin functions, auto installed as a dependancy. +<<<<<<< HEAD ## WP8 Notes Windows phone does not support specification or primary and secondary orientations. If called with a specific orientation the plugin will just apply the landscape or portait orientation. @@ -134,6 +111,15 @@ Windows 8.1 Applicaitons (runtime/metro applications) will only display orientat # Legacy Changelog See [RELEASENOTES.md](RELEASENOTES.md) for the automated changelog. +======= +# Changelog + +## 2.0.0 +* Common javascript for iOS, Android and Windows. + +## 1.4.2 +* [#101](https://github.com/gbenvenuti/cordova-plugin-screen-orientation/pull/101) make iOS rotate as needed when lockOrientation is called +>>>>>>> f2f2dbde729b9d3ac5884de6ee80ae354a3bf5ef ## 1.4.1 * [#89](https://github.com/gbenvenuti/cordova-plugin-screen-orientation/pull/89) Fix for cordova >= 3.6.3 diff --git a/demo/config.xml b/demo/config.xml new file mode 100644 index 0000000..97595db --- /dev/null +++ b/demo/config.xml @@ -0,0 +1,26 @@ + + + ScreenOrientationDemo + + A sample Apache Cordova application that showcases the Screenorientation plugin. + + + Apache Cordova Team + + + + + + + + + + + + + + + + + + diff --git a/demo/www/css/index.css b/demo/www/css/index.css new file mode 100644 index 0000000..51daa79 --- /dev/null +++ b/demo/www/css/index.css @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +* { + -webkit-tap-highlight-color: rgba(0,0,0,0); /* make transparent link selection, adjust last value opacity 0 to 1.0 */ +} + +body { + -webkit-touch-callout: none; /* prevent callout to copy image, etc when tap to hold */ + -webkit-text-size-adjust: none; /* prevent webkit from resizing text to fit */ + -webkit-user-select: none; /* prevent copy paste, to allow, change 'none' to 'text' */ + background-color:#E4E4E4; + background-image:linear-gradient(top, #A7A7A7 0%, #E4E4E4 51%); + background-image:-webkit-linear-gradient(top, #A7A7A7 0%, #E4E4E4 51%); + background-image:-ms-linear-gradient(top, #A7A7A7 0%, #E4E4E4 51%); + background-image:-webkit-gradient( + linear, + left top, + left bottom, + color-stop(0, #A7A7A7), + color-stop(0.51, #E4E4E4) + ); + background-attachment:fixed; + font-family:'HelveticaNeue-Light', 'HelveticaNeue', Helvetica, Arial, sans-serif; + font-size:12px; + height:100%; + margin:0px; + padding:0px; + text-transform:uppercase; + width:100%; +} + +/* Portrait layout (default) */ +.app { + background:url(../img/logo.png) no-repeat center top; /* 170px x 200px */ + position:absolute; /* position in the center of the screen */ + left:50%; + top:50%; + height:50px; /* text area height */ + width:225px; /* text area width */ + text-align:center; + padding:180px 0px 0px 0px; /* image height is 200px (bottom 20px are overlapped with text) */ + margin:-115px 0px 0px -112px; /* offset vertical: half of image height and text area height */ + /* offset horizontal: half of text area width */ +} + +/* Landscape layout (with min-width) */ +@media screen and (min-aspect-ratio: 1/1) and (min-width:400px) { + .app { + background-position:left center; + padding:75px 0px 75px 170px; /* padding-top + padding-bottom + text area = image height */ + margin:-90px 0px 0px -198px; /* offset vertical: half of image height */ + /* offset horizontal: half of image width and text area width */ + } +} + +h1 { + font-size:24px; + font-weight:normal; + margin:0px; + overflow:visible; + padding:0px; + text-align:center; +} + +.event { + border-radius:4px; + -webkit-border-radius:4px; + color:#FFFFFF; + font-size:12px; + margin:0px 30px; + padding:2px 0px; +} + +.event.listening { + background-color:#333333; + display:block; +} + +.event.received { + background-color:#4B946A; + display:none; +} + +@keyframes fade { + from { opacity: 1.0; } + 50% { opacity: 0.4; } + to { opacity: 1.0; } +} + +@-webkit-keyframes fade { + from { opacity: 1.0; } + 50% { opacity: 0.4; } + to { opacity: 1.0; } +} + +.blink { + animation:fade 3000ms infinite; + -webkit-animation:fade 3000ms infinite; +} diff --git a/demo/www/img/logo.png b/demo/www/img/logo.png new file mode 100644 index 0000000..9519e7d Binary files /dev/null and b/demo/www/img/logo.png differ diff --git a/demo/www/index.html b/demo/www/index.html new file mode 100644 index 0000000..36f0596 --- /dev/null +++ b/demo/www/index.html @@ -0,0 +1,58 @@ + + + + + + + + + + + Hello World + + +
+

Apache Cordova

+ +
+ + +
+
+ + +
+ +
+ + + + diff --git a/demo/www/js/index.js b/demo/www/js/index.js new file mode 100644 index 0000000..3df971c --- /dev/null +++ b/demo/www/js/index.js @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +var app = { + // Application Constructor + initialize: function() { + this.bindEvents(); + }, + // Bind Event Listeners + // + // Bind any events that are required on startup. Common events are: + // 'load', 'deviceready', 'offline', and 'online'. + bindEvents: function() { + document.addEventListener('deviceready', this.onDeviceReady, false); + + }, + // deviceready Event Handler + // + // The scope of 'this' is the event. In order to call the 'receivedEvent' + // function, we must explicitly call 'app.receivedEvent(...);' + onDeviceReady: function() { + app.receivedEvent('deviceready'); + btnPortrait.addEventListener("click", function() { + // alert('Orientation is ' + screen.orientation); + screen.lockOrientation('portrait').then(function(obj) { + console.log(obj); + }).catch(function(obj) { + console.log(obj); + }); + }); + btnLandscape.addEventListener("click", function() { + // alert('Orientation is ' + screen.orientation); + screen.lockOrientation('landscape').then(function(obj) { + console.log(obj); + }).catch(function(obj) { + console.log(obj); + }); + }); + btnPortPrimary.addEventListener("click", function() { + // alert('Orientation is ' + screen.orientation); + screen.lockOrientation('portrait-primary').then(function(obj) { + console.log(obj); + }).catch(function(obj) { + console.log(obj); + }); + }); + btnLandPrimary.addEventListener("click", function() { + // alert('Orientation is ' + screen.orientation); + screen.lockOrientation('landscape-primary').then(function(obj) { + console.log(obj); + }).catch(function(obj) { + console.log(obj); + }); + }); + btnAny.addEventListener("click", function() { + // alert('Orientation is ' + screen.orientation); + screen.lockOrientation('any').then(function(obj) { + console.log(obj); + }).catch(function(obj) { + console.log(obj); + }); + }); + + + + + }, + // Update DOM on a Received Event + receivedEvent: function(id) { + var parentElement = document.getElementById(id); + var listeningElement = parentElement.querySelector('.listening'); + var receivedElement = parentElement.querySelector('.received'); + + listeningElement.setAttribute('style', 'display:none;'); + receivedElement.setAttribute('style', 'display:block;'); + + console.log('Received Event: ' + id); + } +}; + +app.initialize(); \ No newline at end of file diff --git a/package.json b/package.json index cb42665..b440fa4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-screen-orientation", - "version": "1.4.3-dev", + "version": "2.0.0-dev", "description": "Adds Screen Orientation API lock and unlock functions to the global screen object in android, iOS, WP8, W8.1, and BB10.", "repository": { "type": "git", diff --git a/plugin.xml b/plugin.xml index c4aa0f9..9ecc897 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,25 +1,24 @@ + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + --> - + version="2.0.0-dev"> Screen Orientation Adds Screen Orientation API lock and unlock functions to the global screen object in android, iOS, WP8, W8.1, and BB10. MIT @@ -34,30 +33,25 @@ - - + + - - - - - - - + + + + - + - - + + - - - + @@ -70,21 +64,11 @@ - - - - - - - - - - - + - - + + diff --git a/src/android/YoikScreenOrientation.java b/src/android/CDVOrientation.java similarity index 54% rename from src/android/YoikScreenOrientation.java rename to src/android/CDVOrientation.java index 41674cf..c7dc207 100644 --- a/src/android/YoikScreenOrientation.java +++ b/src/android/CDVOrientation.java @@ -17,9 +17,9 @@ * specific language governing permissions and limitations * under the License. * -*/ + */ -package net.yoik.cordova.plugins.screenorientation; +package cordova.plugins.screenorientation; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; @@ -31,71 +31,67 @@ import android.app.Activity; import android.content.pm.ActivityInfo; import android.util.Log; -public class YoikScreenOrientation extends CordovaPlugin { - - private static final String TAG = "YoikScreenOrientation"; - +public class CDVOrientation extends CordovaPlugin { + + /** * Screen Orientation Constants */ - - private static final String UNLOCKED = "unlocked"; + + private static final String ANY = "any"; 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 LANDSCAPE = "landscape"; - + @Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { - + Log.d(TAG, "execute action: " + action); - + // Route the Action if (action.equals("screenOrientation")) { return routeScreenOrientation(args, callbackContext); } - + // Action not found callbackContext.error("action not recognised"); return false; } - + private boolean routeScreenOrientation(JSONArray args, CallbackContext callbackContext) { - + String action = args.optString(0); - - if (action.equals("set")) { - - String orientation = args.optString(1); - - Log.d(TAG, "Requested ScreenOrientation: " + orientation); - - Activity activity = cordova.getActivity(); - - if (orientation.equals(UNLOCKED)) { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); - } else if (orientation.equals(LANDSCAPE_PRIMARY)) { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); - } else if (orientation.equals(PORTRAIT_PRIMARY)) { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - } else if (orientation.equals(LANDSCAPE)) { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); - } else if (orientation.equals(PORTRAIT)) { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); - } else if (orientation.equals(LANDSCAPE_SECONDARY)) { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); - } else if (orientation.equals(PORTRAIT_SECONDARY)) { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); - } - - callbackContext.success(); - return true; - - } else { - callbackContext.error("ScreenOrientation not recognised"); - return false; + + + + String orientation = args.optString(1); + + Log.d(TAG, "Requested ScreenOrientation: " + orientation); + + Activity activity = cordova.getActivity(); + + if (orientation.equals(ANY)) { + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); + } else if (orientation.equals(LANDSCAPE_PRIMARY)) { + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + } else if (orientation.equals(PORTRAIT_PRIMARY)) { + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } else if (orientation.equals(LANDSCAPE)) { + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); + } else if (orientation.equals(PORTRAIT)) { + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); + } else if (orientation.equals(LANDSCAPE_SECONDARY)) { + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); + } else if (orientation.equals(PORTRAIT_SECONDARY)) { + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); } + + callbackContext.success(); + return true; + + } } \ No newline at end of file diff --git a/src/ios/CDVViewController+UpdateSupportedOrientations.h b/src/ios/CDVOrientation.h similarity index 84% rename from src/ios/CDVViewController+UpdateSupportedOrientations.h rename to src/ios/CDVOrientation.h index 6cff917..48166a3 100644 --- a/src/ios/CDVViewController+UpdateSupportedOrientations.h +++ b/src/ios/CDVOrientation.h @@ -17,12 +17,15 @@ * specific language governing permissions and limitations * under the License. * -*/ + */ +#import +#import #import -@interface CDVViewController (UpdateSupportedOrientations) +@interface CDVOrientation : CDVPlugin +{} -- (void)updateSupportedOrientations:(NSArray *)orientations; +- (void)screenOrientation:(CDVInvokedUrlCommand *)command; -@end +@end \ No newline at end of file diff --git a/src/ios/CDVOrientation.m b/src/ios/CDVOrientation.m new file mode 100644 index 0000000..b38a55d --- /dev/null +++ b/src/ios/CDVOrientation.m @@ -0,0 +1,65 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#import "CDVOrientation.h" +#import +#import + +@interface CDVOrientation () {} +@end + +@implementation CDVOrientation + +-(void)screenOrientation:(CDVInvokedUrlCommand *)command +{ + CDVPluginResult* pluginResult; + NSInteger orientationMask = [[command argumentAtIndex:0] integerValue]; + CDVViewController* vc = (CDVViewController*)self.viewController; + NSMutableArray* result = [[NSMutableArray alloc] init]; + + if(orientationMask & 1) { + [result addObject:[NSNumber numberWithInt:UIInterfaceOrientationPortrait]]; + } + if(orientationMask & 2) { + [result addObject:[NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown]]; + } + if(orientationMask & 4) { + [result addObject:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]]; + } + if(orientationMask & 8) { + [result addObject:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight]]; + } + + SEL selector = NSSelectorFromString(@"setSupportedOrientations:"); + + if([vc respondsToSelector:selector]) { + ((void (*)(CDVViewController*, SEL, NSMutableArray*))objc_msgSend)(vc,selector,result); + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + } + else { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_INVALID_ACTION messageAsString:@"Error calling to set supported orientations"]; + } + + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + +} + +@end \ No newline at end of file diff --git a/src/ios/CDVViewController+UpdateSupportedOrientations.m b/src/ios/CDVViewController+UpdateSupportedOrientations.m deleted file mode 100644 index b930923..0000000 --- a/src/ios/CDVViewController+UpdateSupportedOrientations.m +++ /dev/null @@ -1,32 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -#import "CDVViewController+UpdateSupportedOrientations.h" - -@implementation CDVViewController (UpdateSupportedOrientations) - -- (void)updateSupportedOrientations:(NSArray *)orientations { - - [self setValue:orientations forKey:@"supportedOrientations"]; - -} - -@end diff --git a/src/ios/YoikScreenOrientation.h b/src/ios/YoikScreenOrientation.h deleted file mode 100644 index f2d4c67..0000000 --- a/src/ios/YoikScreenOrientation.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -#import -#import - -@interface YoikScreenOrientation : CDVPlugin - -- (void)screenOrientation:(CDVInvokedUrlCommand *)command; -@property (strong, nonatomic) NSArray *originalSupportedOrientations; - -@end - -@interface ForcedViewController : UIViewController - -@property (strong, nonatomic) NSString *calledWith; - -@end \ No newline at end of file diff --git a/src/ios/YoikScreenOrientation.m b/src/ios/YoikScreenOrientation.m deleted file mode 100644 index 3008b0c..0000000 --- a/src/ios/YoikScreenOrientation.m +++ /dev/null @@ -1,120 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -#import "YoikScreenOrientation.h" -#import "CDVViewController+UpdateSupportedOrientations.h" - -@implementation YoikScreenOrientation - --(void)screenOrientation:(CDVInvokedUrlCommand *)command -{ - [self.commandDelegate runInBackground:^{ - - if(self.originalSupportedOrientations == nil) { - self.originalSupportedOrientations = [self.viewController valueForKey:@"supportedOrientations"]; - } - - NSArray* arguments = command.arguments; - NSString* orientationIn = [arguments objectAtIndex:1]; - - if ([orientationIn isEqual: @"unlocked"]) { - [(CDVViewController*)self.viewController updateSupportedOrientations:self.originalSupportedOrientations]; - self.originalSupportedOrientations = nil; - return; - } - - // grab the device orientation so we can pass it back to the js side. - NSString *orientation; - switch ([[UIDevice currentDevice] orientation]) { - case UIDeviceOrientationLandscapeLeft: - orientation = @"landscape-secondary"; - break; - case UIDeviceOrientationLandscapeRight: - orientation = @"landscape-primary"; - break; - case UIDeviceOrientationPortrait: - orientation = @"portrait-primary"; - break; - case UIDeviceOrientationPortraitUpsideDown: - orientation = @"portrait-secondary"; - break; - default: - orientation = @"portait"; - break; - } - - // we send the result prior to the view controller presentation so that the JS side - // is ready for the unlock call. - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK - messageAsDictionary:@{@"device":orientation}]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - - // SEE https://github.com/Adlotto/cordova-plugin-recheck-screen-orientation - // HACK: Force rotate by changing the view hierarchy. - ForcedViewController *vc = [[ForcedViewController alloc] init]; - vc.calledWith = orientationIn; - - // backgound should be transparent as it is briefly visible - // prior to closing. - vc.view.backgroundColor = [UIColor clearColor]; - // vc.view.alpha = 0.0; - vc.view.opaque = YES; - -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 - // This stops us getting the black application background flash, iOS8 - vc.modalPresentationStyle = UIModalPresentationOverFullScreen; -#endif - - dispatch_async(dispatch_get_main_queue(), ^{ - [self.viewController presentViewController:vc animated:NO completion:nil]; - }); - - }]; -} - -@end - -@implementation ForcedViewController - --(void) viewDidAppear:(BOOL)animated { - CDVViewController *presenter = (CDVViewController*)self.presentingViewController; - - if ([self.calledWith rangeOfString:@"portrait"].location != NSNotFound) { - [presenter updateSupportedOrientations:@[[NSNumber numberWithInt:UIInterfaceOrientationPortrait]]]; - - } else if([self.calledWith rangeOfString:@"landscape"].location != NSNotFound) { - [presenter updateSupportedOrientations:@[[NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft], [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight]]]; - } else { - [presenter updateSupportedOrientations:@[[NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft], [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight], [NSNumber numberWithInt:UIInterfaceOrientationPortrait]]]; - } - [presenter dismissViewControllerAnimated:NO completion:nil]; -} - -- (UIInterfaceOrientationMask) supportedInterfaceOrientations -{ - if ([self.calledWith rangeOfString:@"portrait"].location != NSNotFound) { - return UIInterfaceOrientationMaskPortrait; - } else if([self.calledWith rangeOfString:@"landscape"].location != NSNotFound) { - return UIInterfaceOrientationMaskLandscape; - } - return UIInterfaceOrientationMaskAll; -} -@end \ No newline at end of file diff --git a/src/windows/CDVOrientation.cs b/src/windows/CDVOrientation.cs new file mode 100644 index 0000000..76a6b46 --- /dev/null +++ b/src/windows/CDVOrientation.cs @@ -0,0 +1,58 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +var DisplayInfo = Windows.Graphics.Display.DisplayInformation; + var Orientations = Windows.Graphics.Display.DisplayOrientations; + +module.exports = { + setAllowedOrientations: function (win, fail, args) { + //console.log("setAllowedOrientations proxy called with " + args); + + try { + var prefOrients = args[0]; + var winPrefs = 0; + + if (prefOrients & 1) { // UIInterfaceOrientationPortrait + winPrefs = winPrefs | Orientations.portrait; + } + if (prefOrients & 2) { // UIInterfaceOrientationPortraitUpsideDown + winPrefs = winPrefs | Orientations.portraitFlipped; + } + if(prefOrients & 4) { // UIInterfaceOrientationLandscapeLeft + winPrefs = winPrefs | Orientations.landscape; + } + if (prefOrients & 8) { // UIInterfaceOrientationLandscapeRight + winPrefs = winPrefs | Orientations.landscapeFlipped; + } + setTimeout(function () { + DisplayInfo.autoRotationPreferences = winPrefs; + win(); + }, 0); + } + catch (err) { + console.log("error :: " + err); + fail(); + } + + } +} + +require("cordova/exec/proxy").add("CDVOrientation", module.exports); diff --git a/src/wp/YoikScreenOrientation.cs b/src/wp/YoikScreenOrientation.cs deleted file mode 100644 index 2108c54..0000000 --- a/src/wp/YoikScreenOrientation.cs +++ /dev/null @@ -1,140 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -using System; -using System.Net; -using System.IO; -using Microsoft.Phone.Controls; -using Microsoft.Phone.Notification; -using Microsoft.Phone.Shell; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Documents; -using System.Windows.Ink; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Animation; -using System.Windows.Shapes; -using System.Windows.Threading; -using System.Runtime.Serialization; -using System.Collections.Generic; -using System.Diagnostics; -using System.Globalization; -using System.Threading; - -using WPCordovaClassLib.Cordova; -using WPCordovaClassLib.Cordova.Commands; -using WPCordovaClassLib.Cordova.JSON; - - -namespace WPCordovaClassLib.Cordova.Commands -{ - public class YoikScreenOrientation : BaseCommand - { - #region Static members - private const string UNLOCKED = "unlocked"; - - private const string PORTRAIT = "portrait"; - private const string PORTRAIT_PRIMARY = "portrait-primary"; - private const string PORTRAIT_SECONDARY = "portrait-secondary"; - - private const string LANDSCAPE = "landscape"; - private const string LANDSCAPE_PRIMARY = "landscape-primary"; - private const string LANDSCAPE_SECONDARY = "landscape-secondary"; - - #endregion - - /// - /// Current orientation - /// - private string currentOrientation; - - public YoikScreenOrientation() - { - - } - - /// - /// Changes the orientation - /// - - public void screenOrientation(string options) - { - string orientation = null; - try - { - orientation = JSON.JsonHelper.Deserialize(options)[0]; - } - catch (Exception ex) - { - this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, ex.Message)); - return; - } - - if (string.IsNullOrEmpty(orientation)) - { - this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); - return; - } - - if (this.currentOrientation != orientation) // Might prevent flickering - { - - Deployment.Current.Dispatcher.BeginInvoke(() => - { - PhoneApplicationFrame frame; - PhoneApplicationPage page; - if (TryCast(Application.Current.RootVisual, out frame) && - TryCast(frame.Content, out page)) - { - if (orientation == PORTRAIT || orientation == PORTRAIT_PRIMARY || orientation == PORTRAIT_SECONDARY) - { - page.SupportedOrientations = SupportedPageOrientation.Portrait; - } - - else if (orientation == LANDSCAPE || orientation == LANDSCAPE_PRIMARY || orientation == LANDSCAPE_SECONDARY) - { - page.SupportedOrientations = SupportedPageOrientation.Landscape; - } - else if (orientation == UNLOCKED) - { - page.SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape; - } - else - { - this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Screen orientation not detected.")); - return; - } - this.currentOrientation = orientation; - } - }); - - this.DispatchCommandResult(); - } - } - - static bool TryCast(object obj, out T result) where T : class - { - result = obj as T; - return result != null; - } - } -} diff --git a/tests/plugin.xml b/tests/plugin.xml new file mode 100644 index 0000000..5d01bad --- /dev/null +++ b/tests/plugin.xml @@ -0,0 +1,31 @@ + + + + + Cordova Screen Orientation Plugin Tests + Apache 2.0 + + + + diff --git a/www/screenorientation.android.js b/tests/tests.js similarity index 64% rename from www/screenorientation.android.js rename to tests/tests.js index 1b8857e..7a28ff5 100644 --- a/www/screenorientation.android.js +++ b/tests/tests.js @@ -19,11 +19,18 @@ * */ -var exec = require('cordova/exec'), - screenOrientation = {}; +/* jshint jasmine: true */ -screenOrientation.setOrientation = function(orientation) { - exec(null, null, "YoikScreenOrientation", "screenOrientation", ['set', orientation]); +exports.defineAutoTests = function() { + describe('Orientation Information (window.orientation)', function () { + it("should exist", function() { + expect(window.orientation).toBeDefined(); + }); + + it("should contain a platform specification that is a string", function() { + expect(window.orientation).toBeDefined(); + expect((String(window.orientation.type)).length > 0).toBe(true); + }); + + }); }; - -module.exports = screenOrientation; \ No newline at end of file diff --git a/www/screenorientation.ios.js b/www/screenorientation.ios.js deleted file mode 100644 index c6882f4..0000000 --- a/www/screenorientation.ios.js +++ /dev/null @@ -1,56 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -var exec = require('cordova/exec'), - screenOrientation = {}, - iosOrientation = 'unlocked', - orientationMap = { - 'portrait': [0,180], - 'portrait-primary': [0], - 'portrait-secondary': [180], - 'landscape': [-90,90], - 'landscape-primary': [-90], - 'landscape-secondary': [90], - 'default': [-90,90,0,180] - }; - -screenOrientation.setOrientation = function(orientation) { - iosOrientation = orientation; - - var success = function(res) { - if (orientation === 'unlocked' && res.device) { - iosOrientation = res.device; - setTimeout(function() { - iosOrientation = 'unlocked'; - },300); - } - }; - - exec(success, null, "YoikScreenOrientation", "screenOrientation", ['set', orientation]); -}; - -module.exports = screenOrientation; - -// ios orientation callback/hook -window.shouldRotateToOrientation = function(orientation) { - var map = orientationMap[iosOrientation] || orientationMap['default']; - return map.indexOf(orientation) >= 0; -}; diff --git a/www/screenorientation.js b/www/screenorientation.js index dd0a596..d574faa 100644 --- a/www/screenorientation.js +++ b/www/screenorientation.js @@ -1,3 +1,4 @@ +cordova.define("cordova-plugin-screen-orientation.screenorientation", function(require, exports, module) { /* * * Licensed to the Apache Software Foundation (ASF) under one @@ -17,78 +18,117 @@ * specific language governing permissions and limitations * under the License. * -*/ + */ -var screenOrientation = {}, - 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' - // The orientation is either landscape-primary or landscape-secondary. - ]; + var screenOrientation = {}, + 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', + // The orientation is either landscape-primary or landscape-secondary. + 'any' + // All orientations are supported (unlocked orientation) + ]; -screenOrientation.Orientations = Orientations; -screenOrientation.currOrientation = 'unlocked'; + screenOrientation.Orientations = Orientations; + screenOrientation.currOrientation = 'any'; + var orientationMask = 0; + screenOrientation.setOrientation = function(orientation) { + if(orientation == Orientations[0]){ + orientationMask = 1; + } + else if(orientation == Orientations[1]){ + orientationMask = 2; + } + else if(orientation == Orientations[2]){ + orientationMask = 4; + } + else if(orientation == Orientations[3]){ + orientationMask = 8; + } + else if(orientation == Orientations[4]){ + orientationMask = 3; + } + else if(orientation == Orientations[5]){ + orientationMask = 12; + } + else if(orientation == Orientations[6]){ + orientationMask = 15; + } -screenOrientation.setOrientation = function(orientation) { - //platform specific files override this function - console.log('setOrientation not supported on device'); -}; -function addScreenOrientationApi(screenObject) { - if (screenObject.unlockOrientation || screenObject.lockOrientation) { - return; - } + cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]); + //console.log('setOrientation not supported on device'); + }; - screenObject.lockOrientation = function(orientation) { - if (Orientations.indexOf(orientation) == -1) { - console.log('INVALID ORIENTATION', orientation); - return; - } - screenOrientation.currOrientation = screenObject.orientation = orientation; - screenOrientation.setOrientation(orientation); - }; + function addScreenOrientationApi(screenObject) { + if (screenObject.unlockOrientation || screenObject.lockOrientation) { + return; + } - screenObject.unlockOrientation = function() { - screenOrientation.currOrientation = screenObject.orientation = 'unlocked'; - screenOrientation.setOrientation('unlocked'); - }; -} + screenObject.lockOrientation = function(orientation) { + + var p = new Promise(function(resolve,reject){ + if (Orientations.indexOf(orientation) == -1) { + var err = new Error(); + err.name = "NotSupportedError"; + + reject(err);//"cannot change orientation"); + + } + else { + screenOrientation.currOrientation = screenObject.orientation = orientation; + screenOrientation.setOrientation(orientation); + resolve("Orientation set"); // orientation change successful + } + }); + return p; + + + }; -addScreenOrientationApi(screen); -orientationChange(); + screenObject.unlockOrientation = function() { + screenOrientation.currOrientation = screenObject.orientation = 'any'; + screenOrientation.setOrientation('any'); + }; + } -function orientationChange() { - var orientation; + addScreenOrientationApi(screen); + orientationChange(); - switch (window.orientation) { - case 0: - orientation = 'portrait-primary'; - break; - case 90: - orientation = 'landscape-primary'; - break; - case 180: - orientation = 'portrait-secondary'; - break; - case -90: - orientation = 'landscape-secondary'; - break; - default: - orientation = 'unknown'; - } + function orientationChange() { + var orientation; - screen.orientation = orientation; -} + switch (window.orientation) { + case 0: + orientation = 'portrait-primary'; + break; + case 90: + orientation = 'landscape-primary'; + break; + case 180: + orientation = 'portrait-secondary'; + break; + case -90: + orientation = 'landscape-secondary'; + break; + default: + orientation = 'any'; + } -window.addEventListener("orientationchange", orientationChange, true); + screen.orientation = orientation; + } -module.exports = screenOrientation; \ No newline at end of file + window.addEventListener("orientationchange", orientationChange, true); + + module.exports = screenOrientation; + +}); diff --git a/www/screenorientation.windows.js b/www/screenorientation.windows.js deleted file mode 100644 index a1b3af0..0000000 --- a/www/screenorientation.windows.js +++ /dev/null @@ -1,54 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -var screenOrientation = {}; - -screenOrientation.setOrientation = function (orientation) { - var orientationNumber; - switch (orientation) { - case 'landscape': - orientationNumber = 5; - break; - case 'portrait': - orientationNumber = 10; - break; - case 'landscape-primary': - orientationNumber = 1; - break; - case 'landscape-secondary': - orientationNumber = 4; - break; - case 'portrait-primary': - orientationNumber = 2; - break; - case 'portrait-secondary': - orientationNumber = 8; - break; - case 'unlocked': - orientationNumber = 0; - break; - default: - break; - } - Windows.Graphics.Display.DisplayInformation.autoRotationPreferences = orientationNumber; -}; - -module.exports = screenOrientation; diff --git a/www/screenorientation.wp8.js b/www/screenorientation.wp8.js deleted file mode 100644 index 8712c26..0000000 --- a/www/screenorientation.wp8.js +++ /dev/null @@ -1,29 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -var exec = require('cordova/exec'), - screenOrientation = {}; - -screenOrientation.setOrientation = function(orientation) { - exec(null, null, "YoikScreenOrientation", "screenOrientation", [orientation]); -}; - -module.exports = screenOrientation; \ No newline at end of file