User Tools

Site Tools


script_battery_charging_state

**This is an old revision of the document!** ----

A PCRE internal error occured. This might be caused by a faulty plugin

====== About the script ====== * Purpose : Show battery "charging" or "discharging" state * Author : Abednego JS ( https://plus.google.com/u/0/118095355037125433304 ) * Link: https://plus.google.com/u/0/118095355037125433304/posts/UXfaG9YQyqX ====== How to use the script ====== 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 "Lightning Action - Do Nothing" to run this script, it'll be dynamic label where the script show battery state. Give it name "batv". Also assign the two scripts below to item resume and pause events. How to assign: Long tap on shortcut >> Customize Item >> More... >> "+" tab >> Resumed >> Run a script [choose the resume script]. Repeat these steps for "Paused". 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's 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 ====== ======= Item resume script ======= <code> LL.bindClass("android.os.BatteryManager"); LL.bindClass("android.content.BroadcastReceiver"); LL.bindClass("android.content.Intent"); 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(); function refresh() { var ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); var batteryStatus = LL.getContext().registerReceiver(null, ifilter); var status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); 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 acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC; var batstat = ""; if (isCharging) { if (usbCharge) { batstat = "usb charging . . ."; }else { batstat = "AC charging . . ."; } } item.setLabel(batstat); item.setTag(setTimeout(refresh, PERIOD)); } refresh(); </code> ======= Item pause script ======= <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(); if(id != null) { clearTimeout(parseInt(id)); } </code>

script_battery_charging_state.1422006540.txt.gz · Last modified: 2015/01/23 09:49 by pierrox