Skip to content

Commit

Permalink
Torch intensity control on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Sep 17, 2016
1 parent 2175f33 commit 08ddc47
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ Just add the following xml to your `config.xml` to always use the latest version
```

## 3. Usage

Since version 3.2.0 of this plugin you can pass in an `intensity` property
which needs to be anywhere between 0.0 and 1.0. __Only__ on iOS this will affect the
brightness of the torch.

```javascript
window.plugins.flashlight.available(function(isAvailable) {
if (isAvailable) {

// switch on
window.plugins.flashlight.switchOn(); // success/error callbacks may be passed
window.plugins.flashlight.switchOn(
function() {}, // optional success callback
function() {}, // optional error callback
{intensity: 0.3} // optional as well
);

// switch off after 3 seconds
setTimeout(function() {
Expand All @@ -63,7 +72,11 @@ window.plugins.flashlight.available(function(isAvailable) {

As an alternative to `switchOn` and `switchOff`, you can use the `toggle` function
```javascript
window.plugins.flashlight.toggle(); // success/error callbacks may be passed
window.plugins.flashlight.toggle(
function() {}, // optional success callback
function() {}, // optional error callback
{intensity: 0.3} // optional as well, used on iOS when switching on
);
```

To know if the flashlight is on or off you can call `isSwitchedOn`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-flashlight",
"version": "3.1.0",
"version": "3.2.0",
"description": "This plugin allows you switch the Flashlight / Torch of your device on or off.",
"cordova": {
"id": "cordova-plugin-flashlight",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-flashlight"
version="3.1.0">
version="3.2.0">

<name>Flashlight</name>

Expand Down
14 changes: 13 additions & 1 deletion src/ios/Flashlight.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ - (void)switchOn:(CDVInvokedUrlCommand*)command {
if ([self deviceHasFlashlight]) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
NSError *error = nil;
float value = AVCaptureMaxAvailableTorchLevel;
if (command.arguments.count > 0) {
NSDictionary* options = command.arguments[0];
NSNumber *intensity = options[@"intensity"];
if (intensity != nil) {
float requestedValue = [intensity floatValue];
if (requestedValue > 0.0 && requestedValue < 1.0) {
value = requestedValue;
}
}
}
[device setTorchModeOnWithLevel:value error:&error];
[device setFlashMode:AVCaptureFlashModeOn];
[device unlockForConfiguration];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
Expand Down
9 changes: 5 additions & 4 deletions www/Flashlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ Flashlight.prototype = {
}, function() { callback(false); }, "Flashlight", "available", []);
},

switchOn: function (successCallback, errorCallback) {
switchOn: function (successCallback, errorCallback, options) {
var opts = options || {};
this._isSwitchedOn = true;
cordova.exec(successCallback, errorCallback, "Flashlight", "switchOn", []);
cordova.exec(successCallback, errorCallback, "Flashlight", "switchOn", [opts]);
},

switchOff: function (successCallback, errorCallback) {
this._isSwitchedOn = false;
cordova.exec(successCallback, errorCallback, "Flashlight", "switchOff", []);
},

toggle: function (successCallback, errorCallback) {
toggle: function (successCallback, errorCallback, options) {
if (this._isSwitchedOn) {
this.switchOff(successCallback, errorCallback);
} else {
this.switchOn(successCallback, errorCallback);
this.switchOn(successCallback, errorCallback, options);
}
},

Expand Down

0 comments on commit 08ddc47

Please sign in to comment.