1
+ /**
2
+ * Phonegap Torch Plugin
3
+ * Copyright (c) Arne de Bree 2011
4
+ *
5
+ */
6
+ package nl .debree .phonegap .plugin .torch ;
7
+
8
+ import java .util .List ;
9
+
10
+ import org .json .JSONArray ;
11
+ import org .json .JSONException ;
12
+ import org .json .JSONObject ;
13
+ import com .phonegap .api .Plugin ;
14
+ import com .phonegap .api .PluginResult ;
15
+ import com .phonegap .api .PluginResult .Status ;
16
+
17
+ import android .hardware .Camera ;
18
+ import android .util .Log ;
19
+
20
+ /**
21
+ * Plugin to turn on or off the Camera Flashlight of an Android device
22
+ * after the capability is tested
23
+ */
24
+ public class TorchPlugin extends Plugin {
25
+
26
+ public static final String CMD_ON = "turnOn" ;
27
+ public static final String CMD_OFF = "turnOff" ;
28
+ public static final String CMD_TOGGLE = "toggle" ;
29
+ public static final String CMD_IS_ON = "isOn" ;
30
+ public static final String CMD_HAS_TORCH = "isCapable" ;
31
+
32
+ // Create camera and parameter objects
33
+ private Camera mCamera ;
34
+ private Camera .Parameters mParameters ;
35
+ private boolean mbTorchEnabled = false ;
36
+
37
+ /**
38
+ * Constructor
39
+ */
40
+ public TorchPlugin () {
41
+ Log .d ( "TorchPlugin" , "Plugin created" );
42
+
43
+ mCamera = Camera .open ();
44
+ }
45
+
46
+ /*
47
+ * Executes the request and returns PluginResult.
48
+ *
49
+ * @param action action to perform. Allowed values: turnOn, turnOff, toggle, isOn, isCapable
50
+ * @param data input data, currently not in use
51
+ * @param callbackId The callback id used when calling back into JavaScript.
52
+ * @return A PluginResult object with a status and message.
53
+ *
54
+ * @see com.phonegap.api.Plugin#execute(java.lang.String,
55
+ * org.json.JSONArray, java.lang.String)
56
+ */
57
+ @ Override
58
+ public PluginResult execute (String action , JSONArray data , String callbackId ) {
59
+ Log .d ( "TorchPlugin" , "Plugin Called " + action );
60
+
61
+ PluginResult result = null ;
62
+ JSONObject response = new JSONObject ();
63
+
64
+ if (action .equals (CMD_ON )) {
65
+
66
+ this .toggleTorch ( true );
67
+ result = new PluginResult ( Status .OK );
68
+
69
+ } else if (action .equals (CMD_OFF )) {
70
+
71
+ this .toggleTorch ( false );
72
+ result = new PluginResult ( Status .OK );
73
+
74
+ } else if (action .equals (CMD_TOGGLE )) {
75
+
76
+ this .toggleTorch ();
77
+ result = new PluginResult ( Status .OK );
78
+
79
+ } else if (action .equals (CMD_IS_ON )) {
80
+ try {
81
+ response .put ( "on" , mbTorchEnabled );
82
+
83
+ result = new PluginResult ( Status .OK , response );
84
+ } catch ( JSONException jsonEx ) {
85
+ result = new PluginResult (Status .JSON_EXCEPTION );
86
+ }
87
+ } else if (action .equals (CMD_HAS_TORCH )) {
88
+ try {
89
+ response .put ( "capable" , this .isCapable () );
90
+
91
+ result = new PluginResult ( Status .OK , response );
92
+ } catch ( JSONException jsonEx ) {
93
+ result = new PluginResult (Status .JSON_EXCEPTION );
94
+ }
95
+
96
+ } else {
97
+ result = new PluginResult (Status .INVALID_ACTION );
98
+ Log .d ( "TorchPlugin" , "Invalid action : " + action + " passed" );
99
+ }
100
+
101
+ return result ;
102
+ }
103
+
104
+ /**
105
+ * Test if this device has a Flashlight we can use and put in Torch mode
106
+ *
107
+ * @return boolean
108
+ */
109
+ protected boolean isCapable () {
110
+ boolean result = false ;
111
+
112
+ List <String > flashModes = mParameters .getSupportedFlashModes ();
113
+
114
+ if (flashModes != null && flashModes .contains (Camera .Parameters .FLASH_MODE_TORCH )) {
115
+ result = true ;
116
+ }
117
+
118
+ return result ;
119
+ }
120
+
121
+ /**
122
+ * True toggle function, turns the torch on when off and vise versa
123
+ *
124
+ */
125
+ protected void toggleTorch () {
126
+ toggleTorch ( !mbTorchEnabled );
127
+ }
128
+
129
+ /**
130
+ * Toggle the torch in the requested state
131
+ *
132
+ * @param state The requested state
133
+ *
134
+ */
135
+ protected void toggleTorch (boolean state ) {
136
+ mParameters = mCamera .getParameters ();
137
+
138
+ // Make sure that torch mode is supported
139
+ //
140
+ if ( this .isCapable () ) {
141
+ if (state ) {
142
+ mParameters .setFlashMode (Camera .Parameters .FLASH_MODE_TORCH );
143
+ } else {
144
+ mParameters .setFlashMode (Camera .Parameters .FLASH_MODE_ON );
145
+ }
146
+
147
+ // Commit the camera parameters
148
+ //
149
+ mCamera .setParameters (mParameters );
150
+
151
+ mbTorchEnabled = state ;
152
+ }
153
+ }
154
+ }
0 commit comments