From 9602b540d9d6dfe6ee249505d58b6a5f346e572d Mon Sep 17 00:00:00 2001 From: Grant Benvenuti Date: Tue, 6 May 2014 16:53:23 +1000 Subject: [PATCH] initial commit --- plugin.xml | 42 +++++++ .../YoikScreenOrientation.java | 119 ++++++++++++++++++ src/ios/YoikScreenOrientation.h | 33 +++++ src/ios/YoikScreenOrientation.m | 51 ++++++++ www/screenorientation.js | 86 +++++++++++++ 5 files changed, 331 insertions(+) create mode 100644 plugin.xml create mode 100644 src/android/net/yoik/cordova/plugins/screenorientation/YoikScreenOrientation.java create mode 100644 src/ios/YoikScreenOrientation.h create mode 100644 src/ios/YoikScreenOrientation.m create mode 100644 www/screenorientation.js diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..546d6c8 --- /dev/null +++ b/plugin.xml @@ -0,0 +1,42 @@ + + + + YoikScreenOrientation + Yoik Screen Orientation Plugin + MIT + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/android/net/yoik/cordova/plugins/screenorientation/YoikScreenOrientation.java b/src/android/net/yoik/cordova/plugins/screenorientation/YoikScreenOrientation.java new file mode 100644 index 0000000..5729e76 --- /dev/null +++ b/src/android/net/yoik/cordova/plugins/screenorientation/YoikScreenOrientation.java @@ -0,0 +1,119 @@ +package net.yoik.cordova.plugins.screenorientation; + +import org.apache.cordova.CallbackContext; + +import org.apache.cordova.CordovaPlugin; +import org.json.JSONArray; +import org.json.JSONException; + +import android.app.Activity; +import android.content.pm.ActivityInfo; +import android.util.Log; +import android.webkit.CookieSyncManager; +import android.widget.Toast; + + +public class YoikScreenOrientation extends CordovaPlugin { + + private static final String TAG = "YoikScreenOrientation"; + + /** + * 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 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"; + + // an index for the toast message + private static final int TOAST_MESSAGE_INDEX = 0; + + + @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; + } + + /** + * Screen Orientation Methods + * + * @author Ronald Diaz + * @return boolean + */ + 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(); + + Log.d(TAG, "ROUTING SET"); + + if (orientation.equals(UNSPECIFIED)) { + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); + } else if (orientation.equals(LANDSCAPE)) { + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + } else if (orientation.equals(PORTRAIT)) { + 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)) { + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); + } else if (orientation.equals(SENSOR_PORTRAIT)) { + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); + } else if (orientation.equals(REVERSE_LANDSCAPE)) { + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); + } else if (orientation.equals(REVERSE_PORTRAIT)) { + 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/src/ios/YoikScreenOrientation.h b/src/ios/YoikScreenOrientation.h new file mode 100644 index 0000000..0d6d5b4 --- /dev/null +++ b/src/ios/YoikScreenOrientation.h @@ -0,0 +1,33 @@ +/* +The MIT License (MIT) + +Copyright (c) 2014 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ + +#import +#import + +@interface YoikScreenOrientation : CDVPlugin + + +- (void)screenOrientation:(CDVInvokedUrlCommand *)command; + +@end \ No newline at end of file diff --git a/src/ios/YoikScreenOrientation.m b/src/ios/YoikScreenOrientation.m new file mode 100644 index 0000000..94b6731 --- /dev/null +++ b/src/ios/YoikScreenOrientation.m @@ -0,0 +1,51 @@ +// +// Utility.m +// Yoik +// +// Created by Nick Luo on 14/06/13. +// +// + +#import "YoikScreenOrientation.h" + +// #import "AppDelegate.h" +// #import "MainViewController.h" +// #import "TempViewController.h" + +@implementation YoikScreenOrientation + + +-(void)screenOrientation:(CDVInvokedUrlCommand *)command +{ + // NSString *action = [command.arguments objectAtIndex:0]; + + // AppDelegate *delegate = [UIApplication sharedApplication].delegate; + + // MainViewController *viewController =(MainViewController *)delegate.viewController; + + // if ([action isEqual:@"portrait"]) { + // [viewController forcePortrait]; + // } else { + // [viewController forceLandscape]; + // } + + // TempViewController *temp = [[[TempViewController alloc] init] autorelease]; + + // [delegate.viewController presentViewController:temp animated:NO completion:^{ + // [temp dismissModalViewControllerAnimated:NO]; + + // CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + // [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + + // }]; + + // HACK: Force rotate by changing the view hierarchy. Present modal view then dismiss it immediately. + [self.viewController presentViewController:[UIViewController new] animated:NO completion:^{ [self.viewController dismissViewControllerAnimated:NO completion:nil]; }]; + + // Assume everything went ok + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + +} + +@end diff --git a/www/screenorientation.js b/www/screenorientation.js new file mode 100644 index 0000000..73c13cb --- /dev/null +++ b/www/screenorientation.js @@ -0,0 +1,86 @@ +/* + * + * 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 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" + } + }, + currOrientation = Constants.Orientation.UNSPECIFIED; + +var orientationExports = {}; + +// Tack on the orientation Constants to the base plugin. +for (var key in Constants) { + orientationExports[key] = Constants[key]; +} + +orientationExports.setOrientation = function(successCallback, errorCallback, orientation) { + argscheck.checkArgs('fFs', 'screenorientation.setOrientation', arguments); + + currOrientation = orientation ? orientation : Constants.Orientation.UNSPECIFIED; + + exec(successCallback, errorCallback, "YoikScreenOrientation", "screenOrientation", ['set', currOrientation]); +}; + +// ios orientation callback/hook +window.shouldRotateToOrientation = function(orientation) { + var o = Constants.Orientation; + switch (currOrientation) { + case o.PORTRAIT: + case o.SENSOR_PORTRAIT: + if (orientation === 0) return true; + break; + case o.LANDSCAPE: + case o.SENSOR_LANDSCAPE: + if (orientation === -90) return true; + break; + case o.REVERSE_LANDSCAPE: + if (orientation === 90) return true; + break; + case o.REVERSE_PORTRAIT: + if (orientation === 180) return true; + break; + case o.FULL_SENSOR: + return true; + break; + case o.UNSPECIFIED: + if (orientation === -90 || orientation === 90 || orientation === 0) return true; + break; + } + + return false; +} + +module.exports = orientationExports; \ No newline at end of file