mirror of
https://github.com/apache/cordova-plugin-screen-orientation.git
synced 2025-02-23 16:42:50 +08:00
initial commit
This commit is contained in:
parent
41e6ca887f
commit
9602b540d9
42
plugin.xml
Normal file
42
plugin.xml
Normal file
@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
id="net.yoik.cordova.plugins.screenorientation"
|
||||
version="0.0.1">
|
||||
|
||||
<name>YoikScreenOrientation</name>
|
||||
<description>Yoik Screen Orientation Plugin</description>
|
||||
<license>MIT</license>
|
||||
|
||||
<engines>
|
||||
<engine name="cordova" version=">=3.0.0" />
|
||||
</engines>
|
||||
|
||||
<js-module src="www/screenorientation.js" name="screenorientation">
|
||||
<clobbers target="cordova.plugins.screenorientation" />
|
||||
</js-module>
|
||||
|
||||
<platform name="ios">
|
||||
<config-file target="config.xml" parent="/*">
|
||||
<feature name="YoikScreenOrientation">
|
||||
<param name="ios-package" value="YoikScreenOrientation" />
|
||||
</feature>
|
||||
</config-file>
|
||||
<header-file src="src/ios/YoikScreenOrientation.h" />
|
||||
<source-file src="src/ios/YoikScreenOrientation.m" />
|
||||
|
||||
</platform>
|
||||
|
||||
<platform name="android">
|
||||
|
||||
<source-file src="src/android/net/yoik/cordova/plugins/screenorientation/YoikScreenOrientation.java" target-dir="src/net/yoik/cordova/plugins/screenorientation/" />
|
||||
|
||||
<config-file target="res/xml/config.xml" parent="/*">
|
||||
<feature name="YoikScreenOrientation">
|
||||
<param name="android-package" value="net.yoik.cordova.plugins.screenorientation.YoikScreenOrientation" />
|
||||
</feature>
|
||||
</config-file>
|
||||
|
||||
</platform>
|
||||
|
||||
</plugin>
|
@ -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 <r.diaz@pni.com.au>
|
||||
* @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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
33
src/ios/YoikScreenOrientation.h
Normal file
33
src/ios/YoikScreenOrientation.h
Normal file
@ -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 <Cordova/CDVPlugin.h>
|
||||
#import <Cordova/CDVShared.h>
|
||||
|
||||
@interface YoikScreenOrientation : CDVPlugin
|
||||
|
||||
|
||||
- (void)screenOrientation:(CDVInvokedUrlCommand *)command;
|
||||
|
||||
@end
|
51
src/ios/YoikScreenOrientation.m
Normal file
51
src/ios/YoikScreenOrientation.m
Normal file
@ -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
|
86
www/screenorientation.js
Normal file
86
www/screenorientation.js
Normal file
@ -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;
|
Loading…
Reference in New Issue
Block a user