User Tools

Site Tools


script_battery_charging_state

====== Differences ====== This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
script_battery_charging_state [2015/01/23 09:28]
pierrox page moved from battery_charging_state
script_battery_charging_state [2015/03/15 13:23] (current)
jappie Script-name and tag-name
Line 8: Line 8:
 This script will NOT showing anything when the power cable unplugged, just change the batstat value (in variable declaration or onReceive function) to change this. :D This script will NOT showing anything when the power cable unplugged, just change the batstat value (in variable declaration or onReceive function) to change this. :D
  
-Create shortcut to run this script, it'll be dynamic label where the script ​show battery state. Give it name "​batv"​.+  * Create shortcut ​"​Lightning Action - Do Nothing" ​to run this script
 +  * Disable Icon 
 +  * Set '​BattStatus-resume' script ​to event resume 
 +  * Set '​BattStatus-pause'​ script to event pause
  
-Also assign script ​to running when displaying the page where "​batv"​ placed (follow this step if user want to see battery state updated/​checked when access spesific page instad of checking the state everytime). +This version edited by Pierre ​to minimize resource usage + updated without ​user intervention,​ 
-How to assignLightning Setting >> Current desktop >> Event & Actions >> Resumed >> Run a script [choose this script]. +The old version can be found herehttps://www.dropbox.com/​s/​c0hews7f01b7l01/​_AJS_script_oldBATSTATE.txt?​dl=0 
- +:)
-Dictionary:​ +
- +
-var: +
- +
-item (get LLX item), batcon (get LLX context), batstat ( String value to edit the "​batv"​ label), curd (get current page), curi (get "​batv"​);​ +
-ifilter (filtering android intent), batterystatus (intent where activity to change batstat'​value executed), text (temporary place used as bridge between batstat value to "​batv"​ label); +
-etc (status, isCharging, chargePlug, usbCharge, acCharge they'​re variable used to change batstat'​s value) +
- +
-function: +
- +
-onReceive(context,​ intent= function to change batstat'​s value; +
-manref () = function to manual update "​batv"​ label+
  
 ====== Script code ====== ====== Script code ======
 +======= BattStatus-resume =======
 <​code>​ <​code>​
 LL.bindClass("​android.os.BatteryManager"​);​ LL.bindClass("​android.os.BatteryManager"​);​
 LL.bindClass("​android.content.BroadcastReceiver"​);​ LL.bindClass("​android.content.BroadcastReceiver"​);​
 LL.bindClass("​android.content.Intent"​);​ LL.bindClass("​android.content.Intent"​);​
-LL.bindClass("​android.content.Context"​);​ 
 LL.bindClass("​android.content.IntentFilter"​);​ LL.bindClass("​android.content.IntentFilter"​);​
 +
 +// Update every 10s, this is a cyclic update. Improvement:​ use a receiver instead
 +var PERIOD = 10000;
  
 var item = LL.getEvent().getItem();​ var item = LL.getEvent().getItem();​
-var batstat = "";​ 
-var batcon = LL.getContext();​ 
-var curd = LL.getCurrentDesktop();​ 
-var curi = curd.getItemByName("​batv"​);​ 
- 
  
 +function refresh() {
  var ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);​  var ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);​
- var batteryStatus = batcon.registerReceiver(null,​ ifilter);+ var batteryStatus = LL.getContext().registerReceiver(null,​ ifilter);
  
- function onReceive(context,​ intent) {  +    ​var status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS,​ -1); 
-        ​var status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,​ -1); +    var isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;​ 
-        var isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || + 
-                            ​status == BatteryManager.BATTERY_STATUS_FULL;​ +    var chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED,​ -1); 
-  +    var usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;​ 
-        var chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,​ -1); +    var acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;​ 
-        var usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;​ + 
-        var acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;​+    var batstat = "";​ 
 + 
 +    if (isCharging) 
 +    { 
 +        if (usbCharge) 
 +        { 
 +            batstat = "usb charging . . ."; 
 +        }else 
 +        { 
 +            batstat = "AC charging . . ."; 
 +        } 
 +    }
   
- if (isCharging) +    item.setLabel(batstat);
-+
- if (usbCharge) +
-+
- batstat = "usb charging . . ."; +
- }else +
-+
- batstat = "AC charging . . ."; +
-+
- }else +
-+
- batstat = ""​; +
-+
-  +
-  +
-    }+
  
-function manref() { +    item.setTag("​battSt",​ setTimeout(refresh,​ PERIOD));
-  var text = batstat; +
-  curi.setLabel(text);+
 } }
  
-onReceive(LL.getContext(), batteryStatus); +refresh(); 
-manref();+</​code>​ 
 + 
 +======= BattStatus-pause ======= 
 +<​code>​ 
 +// important: preserve the battery life and clear timeouts when the item is paused to avoid useless background activity 
 +var id = LL.getEvent().getItem().getTag("​battSt"​); 
 +if(id != null) { 
 +    clearTimeout(parseInt(id)); 
 +}
 </​code>​ </​code>​
script_battery_charging_state.1422005319.txt.gz · Last modified: 2015/01/23 09:28 by pierrox