├── .gitignore ├── src ├── ios │ ├── Brightness.h │ └── Brightness.m └── android │ └── BrightnessPlugin.java ├── www └── brightness.js ├── package.json ├── plugin.xml └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /src/ios/Brightness.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Evgeniy Lukovsky 3 | * 4 | */ 5 | 6 | 7 | #import 8 | 9 | @interface Brightness : CDVPlugin 10 | 11 | - (void)getBrightness:(CDVInvokedUrlCommand*)command; 12 | - (void)setBrightness:(CDVInvokedUrlCommand*)command; 13 | - (void)setKeepScreenOn:(CDVInvokedUrlCommand*)command; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /www/brightness.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var exec = require('cordova/exec'); 4 | 5 | exports.getBrightness = function( success, error) { 6 | exec(success, error, 'Brightness', 'getBrightness', []); 7 | }; 8 | 9 | exports.setBrightness = function(value, success, error) { 10 | exec(success, error, 'Brightness', 'setBrightness', [value]); 11 | }; 12 | 13 | exports.setKeepScreenOn = function(value, success, error) { 14 | exec(success, error, 'Brightness', 'setKeepScreenOn', [value]); 15 | }; 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cordova-plugin-brightness", 3 | "version": "0.2.0", 4 | "description": "Cordova Brightness Control Plugin", 5 | "cordova": { 6 | "id": "cordova-plugin-brightness", 7 | "platforms": [ 8 | "ios" 9 | ] 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/mgcrea/cordova-plugin-brightness" 14 | }, 15 | "keywords": [ 16 | "cordova", 17 | "unity", 18 | "ecosystem:cordova", 19 | "cordova-ios" 20 | ], 21 | "engines": [ 22 | { 23 | "name": "cordova-ios", 24 | "version": ">=3.6.0" 25 | } 26 | ], 27 | "author": "Evgeniy Lukovsky", 28 | "license": "MIT" 29 | } 30 | -------------------------------------------------------------------------------- /src/ios/Brightness.m: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Evgeniy Lukovsky 3 | * 4 | */ 5 | 6 | #import 7 | #import "Brightness.h" 8 | 9 | @implementation Brightness : CDVPlugin 10 | 11 | - (void)getBrightness:(CDVInvokedUrlCommand *)command 12 | { 13 | CDVPluginResult * pluginResult = nil; 14 | float brightness = [UIScreen mainScreen].brightness; 15 | NSString *result = [NSString stringWithFormat:@"%f", brightness]; 16 | pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:result]; 17 | [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 18 | } 19 | 20 | - (void)setBrightness:(CDVInvokedUrlCommand *)command 21 | { 22 | CDVPluginResult * pluginResult = nil; 23 | NSString *value = [command.arguments objectAtIndex:0]; 24 | float brightness = [value floatValue]; 25 | [UIScreen mainScreen].brightness = brightness; 26 | pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:YES]; 27 | [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 28 | } 29 | 30 | -(void)setKeepScreenOn:(CDVInvokedUrlCommand *)command 31 | { 32 | CDVPluginResult* pluginResult = nil; 33 | BOOL value = [[command.arguments objectAtIndex:0] boolValue]; 34 | [UIApplication sharedApplication].idleTimerDisabled = value; 35 | pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:YES]; 36 | [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | Brightness 8 | 9 | Evgeniy Lukovsky 10 | 11 | MIT 12 | 13 | 14 | This plugin gives you the ability to get and set the screen brightness 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | This plugin gives you the ability to get and set the screen brightness. Also recently I have added the function for keep screen on. 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Cordova Brightness](https://github.com/mgcrea/cordova-plugin-brightness) [![Release](https://img.shields.io/npm/v/cordova-plugin-brightness.svg?style=flat)](https://github.com/mgcrea/cordova-plugin-brightness/releases) 2 | 3 | This plugin provides a simple way to interact with the brightness of your device. 4 | 5 | * This plugin is built for `cordova@^3.6`. 6 | 7 | * This plugin currently supports both iOS and Android. 8 | 9 | 10 | ## Plugin setup 11 | 12 | Using this plugin requires [Cordova iOS](https://github.com/apache/cordova-ios). 13 | 14 | 1. `cordova plugin add cordova-plugin-brightness` 15 | 16 | 17 | ## Javascript interface 18 | 19 | // After device ready, create a local alias 20 | var brightness = cordova.plugins.brightness; 21 | 22 | // value should be float in range from 0 to 1. 23 | brightness.setBrightness(value, success, error); 24 | // success([-1,0-1]) float 0-1 is a brightness level, -1 represents a system default 25 | brightness.getBrightness(success, error); 26 | // prevents sleep 27 | brightness.setKeepScreenOn(true); 28 | 29 | * Check [source](https://github.com/mgcrea/cordova-plugin-brightness/tree/master/www/brightness.js) for additional configuration. 30 | 31 | 32 | ## Communication 33 | 34 | - If you **need help**, use [Stack Overflow](http://stackoverflow.com/questions/tagged/cordova). (Tag `cordova`) 35 | - If you'd like to **ask a general question**, use [Stack Overflow](http://stackoverflow.com/questions/tagged/cordova). 36 | - If you **found a bug**, open an issue. 37 | - If you **have a feature request**, open an issue. 38 | - If you **want to contribute**, submit a pull request. 39 | 40 | 41 | ## Contributing 42 | 43 | Patches welcome! Send a pull request. Since this is not a part of Cordova Core (which requires a CLA), this should be easier. 44 | 45 | Please submit all pull requests the against master branch. If your pull request contains JavaScript patches or features, you should include relevant unit tests. Thanks! 46 | 47 | 48 | ## Authors 49 | 50 | **Evgeniy Lukovsky** 51 | 52 | + http://github.com/fiscal-cliff 53 | 54 | 55 | ## Copyright and license 56 | 57 | The MIT License (MIT) 58 | 59 | Copyright (c) 2015 Evgeniy Lukovsky 60 | 61 | Permission is hereby granted, free of charge, to any person obtaining a copy 62 | of this software and associated documentation files (the "Software"), to deal 63 | in the Software without restriction, including without limitation the rights 64 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 65 | copies of the Software, and to permit persons to whom the Software is 66 | furnished to do so, subject to the following conditions: 67 | 68 | The above copyright notice and this permission notice shall be included in all 69 | copies or substantial portions of the Software. 70 | 71 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 72 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 73 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 74 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 75 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 76 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 77 | SOFTWARE. 78 | -------------------------------------------------------------------------------- /src/android/BrightnessPlugin.java: -------------------------------------------------------------------------------- 1 | package org.apache.cordova.plugin.Brightness; 2 | 3 | import org.apache.cordova.CallbackContext; 4 | import org.apache.cordova.CordovaPlugin; 5 | import org.json.JSONArray; 6 | import org.json.JSONException; 7 | 8 | import android.app.Activity; 9 | import android.view.WindowManager.LayoutParams; 10 | import android.view.WindowManager; 11 | import android.view.Window; 12 | 13 | /** 14 | * @author Evgeniy Lukovsky 15 | * 16 | */ 17 | public class BrightnessPlugin extends CordovaPlugin { 18 | public enum Action{ 19 | setBrightness, 20 | getBrightness, 21 | setKeepScreenOn 22 | } 23 | 24 | private class SetTask implements Runnable{ 25 | private Activity target = null; 26 | private LayoutParams lp = null; 27 | @Override 28 | public void run() { 29 | target.getWindow().setAttributes(lp); 30 | } 31 | public void setParams(Activity act, LayoutParams params){ 32 | this.target = act; 33 | this.lp = params; 34 | } 35 | } 36 | 37 | private class KeepOnTask implements Runnable{ 38 | private Window win = null; 39 | private boolean state = false; 40 | @Override 41 | public void run() { 42 | if(state){ 43 | win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 44 | } else { 45 | win.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 46 | } 47 | } 48 | public void setParams(Window win, boolean state){ 49 | this.win = win; 50 | this.state = state; 51 | } 52 | } 53 | 54 | 55 | /* (non-Javadoc) 56 | * @see org.apache.cordova.CordovaPlugin#execute(java.lang.String, org.json.JSONArray, org.apache.cordova.CallbackContext) 57 | */ 58 | @Override 59 | public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 60 | System.out.println("plugin has been started"); 61 | boolean result = false; 62 | 63 | switch(Action.valueOf(action)){ 64 | case setBrightness: result = true; 65 | setBrightness(args, callbackContext); 66 | break; 67 | case getBrightness: result = true; 68 | getBrightness(args, callbackContext); 69 | break; 70 | case setKeepScreenOn: result = true; 71 | setKeepScreenOn(args, callbackContext); 72 | break; 73 | } 74 | return result; 75 | } 76 | 77 | /** 78 | * @param args 79 | * @param callbackContext 80 | * @return 81 | */ 82 | private boolean setBrightness(JSONArray args, CallbackContext callbackContext) { 83 | try { 84 | Activity activity = cordova.getActivity(); 85 | WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes(); 86 | String value = args.getString(0); 87 | double brightness = Double.parseDouble(value); 88 | layoutParams.screenBrightness = (float) brightness; 89 | SetTask task = new SetTask(); 90 | task.setParams(activity, layoutParams); 91 | activity.runOnUiThread(task); 92 | callbackContext.success("OK"); 93 | 94 | } catch (NullPointerException e) { 95 | System.out.println("Null pointer exception"); 96 | System.out.println(e.getMessage()); 97 | callbackContext.error(e.getMessage()); 98 | return false; 99 | } catch (JSONException e) { 100 | System.out.println("JSONException exception"); 101 | System.out.println(e.getMessage()); 102 | callbackContext.error(e.getMessage()); 103 | return false; 104 | } 105 | System.out.println("All went fine."); 106 | return true; 107 | } 108 | 109 | /** 110 | * @param args 111 | * @param callbackContext 112 | * @return 113 | */ 114 | private boolean getBrightness(JSONArray args, CallbackContext callbackContext) { 115 | try { 116 | Activity activity = cordova.getActivity(); 117 | WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes(); 118 | Double brightness = (double) layoutParams.screenBrightness; 119 | callbackContext.success(brightness.toString()); 120 | 121 | } catch (NullPointerException e) { 122 | System.out.println("Null pointer exception"); 123 | System.out.println(e.getMessage()); 124 | callbackContext.error(e.getMessage()); 125 | return false; 126 | } 127 | System.out.println("All went fine."); 128 | return true; 129 | } 130 | /** 131 | * @param args 132 | * @param callbackContext 133 | * @return 134 | */ 135 | private boolean setKeepScreenOn(JSONArray args, CallbackContext callbackContext){ 136 | try { 137 | boolean value = args.getBoolean(0); 138 | Activity activity = cordova.getActivity(); 139 | KeepOnTask task = new KeepOnTask(); 140 | task.setParams(activity.getWindow(), value); 141 | activity.runOnUiThread(task); 142 | callbackContext.success("OK"); 143 | 144 | } catch (NullPointerException e) { 145 | System.out.println("Null pointer exception"); 146 | System.out.println(e.getMessage()); 147 | callbackContext.error(e.getMessage()); 148 | return false; 149 | } catch (JSONException e) { 150 | System.out.println("JSONException"); 151 | System.out.println(e.getMessage()); 152 | callbackContext.error(e.getMessage()); 153 | return false; 154 | } 155 | System.out.println("All went fine."); 156 | return true; 157 | } 158 | } 159 | --------------------------------------------------------------------------------