[Android Apps and Games] dapaua:
Now Widgets - script. (NWS)

Now widgets is a collection of widgets based in the Now aesthetics.

This first widget is fully configurable and runs based on shell scripts. The script language used is mksh, that is included in every android 4.0+ device. The documentation is here:mksh man page

Some examples are included:

Time: A simple clock

isSunday: Tells you if it is or not Sunday. Shows the use of icons.

pingtest: Tests if it can connect to the google servers

battery:Shows remaining battery

counter:A simple counter. Shows button and storage use

launcher (4 types): A more elaborate example

The basics is that you create a shell script that prints the widget configuration, depending on whatever you need. The output should be something like this:

SETLAYOUT ICON_TEXT
SETTEXT Connection OK
SETICON OK
UPDATE
REFRESH 120

An example program (pingtest) follows

#Lets you know if you have internet connection
echo SETLAYOUT ICON_TEXT

ping -c 1 [url]www.google.com[/url] >/dev/null
OUT=$?
if [[ $OUT -eq 0 ]]; then
echo SETTEXT Connection OK
echo SETICON OK
else
echo SETTEXT "Can't connect"
echo SETICON CROSS
fi
echo UPDATE
echo REFRESH 120

Pre-defined variables at the start of the script:

$NW_BATTERY ; Will contain the remaining battery, from 0 to 100.

$NW_DATA_PATH ; Will contain a folder where you can write and read files. It is different for each widget and its contents are not cleared after finishing the script. Can be used as temporary folder or as long-term storage.

$1 ; Will contain "REFRESH" if the script was called because a widget update is needed. Will contain "ICON1","ICON2","ICON3",...,"ICON12" if the script was called because an icon was tapped. Will contain "TEXT" if the script was called because the widget text was tapped.

There are two types of commands: pre-processed commands and run commands. Pre-processed commands are needed so NWS knows what info the script needs before running. They always start with #

List of pre -processed commands:

#GETVAR variable default_value

This command sets a variable for the script from long-term storage. NWS stores this value even after restarts, so you can store settings. Example:

#GETVAR MYCOUNTER 0

This will get the $MYCOUNTER variable from storage. If it wasn't previously stored, will set it to "0"

#INFO info_text

This will set the info shown in the widget chooser.Example

#INFO A simple clock.

The list of run commands is as follows. Note that run commands must be prepended by echo to be executed (ex: echo SETLAYOUT ICON)

SETLAYOUT layout

Sets a layout. Possible layouts are: ICON, TEXT, 2ICON, 3ICON, 4ICON, 5ICON, 10ICON, 12ICON, ICON_TEXT, 2ICON_TEXT, ICON_TEXT_ICON. Example:

SETLAYOUT ICON

SETICON icon position

Sets an icon. Included icons are: OK, CROSS ,LEFT ,RIGHT ,BLANK ,REFRESH

Possible positions are 1,2,3,4,5,6,7,8,9,10,11,12. The chosen layout must have the icon slots for them to show.

Example:

SETICON OK 1

SETAPPICON app position

Sets an icon to the launcher icon for the given app. Positions are as in SETICON. Example

SETAPPICON com.google.android.gm 1

This sets the icon 1 to the gmail app icon

RUNAPP app

Runs an app. Example:

RUNAPP com.google.android.gm

This would start gmail.

STOREVAR variable value

Stores a variable in permanent storage. Can be retrieved with #GETVAR. Examples:

STOREVAR NAME dapaua

This would store "dapaua" as NAME.

STOREVAR MYCOUNTER $MYCOUNTER

This would store the value of $MYCOUNTER to MYCOUNTER.

SETTEXT text

Sets the text for the widget.

ADDTEXT text

Adds text to the existing text for the widget.

REFRESH time_in_seconds

Programs the next widget refresh after a number of seconds. The script will then be reeexecuted. Example:

REFRESH 300

This would re-run the script in 5 minutes.

UPDATE

This updates the widget. It is needed if you do any change to the widget, it is not needed if you just execute something. Can be used more than once in a script, but it is usually done at the end.

Don't forget to share your creations!