summaryrefslogtreecommitdiff
path: root/plugins/com.phonegap.plugins.OrientationLock/src/com/plugin/phonegap/OrientationLock.java
blob: 063fb90074187497d002218ccc94128301fdfa90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.plugin.phonegap;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.pm.ActivityInfo;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;

/**
 *
 * Android Phonegap Plugin for locking/unlocking the orientation from JS code
 *
 */
public class OrientationLock extends CordovaPlugin {

	private static final String LANSCAPE = "landscape";
	private static final String PORTRAIT = "portrait";

	public OrientationLock() {
	}

	public void unlock() {
		this.cordova.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
	}

	public void lock(String orientation) {
		if (orientation.equals(PORTRAIT))
			this.cordova.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		else
			this.cordova.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

	@Override
	public boolean execute(String action, JSONArray arguments, CallbackContext callbackContext) {
		if (action.equals("lock")) {

			try {
				String orientation = arguments.getString(0);

				if (orientation!=null && (orientation.equals(LANSCAPE) ||  orientation.equals(PORTRAIT))) {
					this.lock(orientation);
					callbackContext.success();
					return true;
				} else {
					return false;
				}
			} catch (JSONException e) {
				callbackContext.error("JSON_EXCEPTION");
				return true;
			}

		} else if (action.equals("unlock")) {
			this.unlock();
			callbackContext.success();
			return true;

		} else {
			return false;
		}
	}
}