Skip to content

Commit b97551f

Browse files
Arne de BreeJesse MacFadyen
authored andcommitted
Phonegap plugin for Android to enable Torch functionality
Initial commit of Phonegap plugin for Android to enable Torch functionality
1 parent 006689c commit b97551f

File tree

3 files changed

+306
-0
lines changed

3 files changed

+306
-0
lines changed

Android/Torch/README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Torch plugin for Phonegap (Android) #
2+
By Arne de Bree
3+
4+
## Adding the Plugin to your project ##
5+
1. To install the plugin, move `Torch.js` to your project's www folder and include a reference to it
6+
in your html files.
7+
8+
<script src="Torch.js"></script>
9+
10+
2. Create a folder called 'nl/debree/phonegap/plugin/torch' within your project's src folder.
11+
3. And copy the java file into that new folder.
12+
13+
<pre>
14+
mkdir -p <your_project>/src/nl/debree/phonegap/plugin/torch/
15+
cp ./TorchPlugin.java <your_project>/src/nl/debree/phonegap/plugin/torch/
16+
</pre>
17+
18+
4. Add a plugin line to `res/xml/plugins.xml`
19+
20+
&lt;plugin name="Torch" value="nl.debree.phonegap.plugin.torch.TorchPlugin" /&gt;
21+
22+
## Using the plugin ##
23+
The plugin creates the object `window.plugins.Torch` within your DOM. This object
24+
exposes the following functions:
25+
26+
- isOn
27+
- isCapable
28+
- toggle
29+
- turnOn
30+
- turnOff
31+
32+
<pre>
33+
window.plugins.Torch.isOn(
34+
function( result ) { console.log( "isOn: " + result.on ) } // success
35+
, function() { console.log( "error" ) } // error
36+
);
37+
38+
window.plugins.Torch.isCapable(
39+
function( result ) { console.log( "isCapable: " + result.capable ) } // success
40+
, function() { console.log( "error" ) } // error
41+
);
42+
43+
window.plugins.Torch.toggle(
44+
function() { console.log( "toggle" ) } // success
45+
, function() { console.log( "error" ) } // error
46+
);
47+
48+
window.plugins.Torch.turnOn(
49+
function() { console.log( "turnOn" ) } // success
50+
, function() { console.log( "error" ) } // error
51+
);
52+
53+
window.plugins.Torch.turnOff(
54+
function() { console.log( "turnOff" ) } // success
55+
, function() { console.log( "error" ) } // error
56+
);
57+
</pre>
58+
59+
60+
## BUGS AND CONTRIBUTIONS ##
61+
The latest bleeding-edge version is available [on GitHub](http://github.com/adebrees/phonegap-plugins/tree/master/Android/)
62+
If you have a patch, fork my repo baby and send me a pull request. Submit bug reports on GitHub, please.
63+
64+
## Licence ##
65+
66+
The MIT License
67+
68+
Copyright (c) 2011 Arne de Bree
69+
70+
Permission is hereby granted, free of charge, to any person obtaining a copy
71+
of this software and associated documentation files (the "Software"), to deal
72+
in the Software without restriction, including without limitation the rights
73+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
74+
copies of the Software, and to permit persons to whom the Software is
75+
furnished to do so, subject to the following conditions:
76+
77+
The above copyright notice and this permission notice shall be included in
78+
all copies or substantial portions of the Software.
79+
80+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
81+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
82+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
83+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
84+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
85+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
86+
THE SOFTWARE.
87+
88+
89+
90+
91+

Android/Torch/Torch.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Phonegap Torch plugin
3+
* Copyright (c) Arne de Bree 2011
4+
*
5+
*/
6+
7+
/**
8+
*
9+
* @return Object literal singleton instance of Torch
10+
*/
11+
var Torch = function() {};
12+
13+
/**
14+
* @param success The callback for success
15+
* @param error The callback for error
16+
*/
17+
Torch.prototype.isCapable = function( success, error )
18+
{
19+
return PhoneGap.exec( success, error, "Torch", "isCapable", [] );
20+
};
21+
22+
/**
23+
* @param success The callback for success
24+
* @param error The callback for error
25+
*/
26+
Torch.prototype.isOn = function( success, error )
27+
{
28+
return PhoneGap.exec( success, error, "Torch", "isOn", [] );
29+
};
30+
31+
/**
32+
* @param success The callback for success
33+
* @param error The callback for error
34+
*/
35+
Torch.prototype.turnOn = function( success, error )
36+
{
37+
return PhoneGap.exec( success, error, "Torch", "turnOn", [] );
38+
};
39+
40+
/**
41+
* @param success The callback for success
42+
* @param error The callback for error
43+
*/
44+
Torch.prototype.turnOff = function( success, error )
45+
{
46+
return PhoneGap.exec( success, error, "Torch", "turnOff", [] );
47+
};
48+
49+
/**
50+
* @param success The callback for success
51+
* @param error The callback for error
52+
*/
53+
Torch.prototype.toggle = function( success, error )
54+
{
55+
return PhoneGap.exec( success, error, "Torch", "toggle", [] );
56+
};
57+
58+
PhoneGap.addConstructor( function()
59+
{
60+
PhoneGap.addPlugin( "Torch", new Torch() );
61+
} );

Android/Torch/TorchPlugin.java

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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

Comments
 (0)