====== Stopwatch Plugin ====== This is a my private plugin i used for checking code execution time between functions/methods. But it can also be used for other purposes. You can make an interface with buttons for start and stop time and display difference between them. Currently it does not have a LAP method, but i have ideas already for this... will update this plugin on a later date. I have attached 4 files to build the zip-plugin file. You simply need to build these into a zip file and put it into a folder called **/sdcard/DroidScript/Plugins** on your device and DroidScript will import it into the plugins list along with the documentation. //After DroidScript is executed, the plugin will appear in the plugin menu and it's documentation will be accessible.// Regards, Nelson Tengkiat (dated: Aug 8, 2016) =====Usage of the Example===== ====First Step:==== Since i don't have enough rights to post the ZIP file. You simply need to download all the files need to build this plugin yourself by zipping the file. Download the following 4 Files: * **File 1: The html-page for the Documentation** Stopwatch
Back

Stopwatch Plugin

In order to use Stopwatch functions plugin, you must first load the plugin at the top of your script using the LoadPlugin method like this:

// Load the Stopwatch plugin
app.LoadPlugin( "Stopwatch" );
  Copy  

Then you can create an instance of the plugin object when you need it like this:

 plg = app.CreateStopwatch( );
 Copy 

Version Information
You can get the plugin version by calling GetVersion( ) for program use or toast display the version by calling ShowVersion( )

Methods:

GetVersion( ) - returns string

 - returns version string

ShowVersion( )

 - toast display the version number

Example:

Example - Get / Show Version

app.LoadPlugin("Stopwatch");

function OnStart( ) {
 lay=app.CreateLayout("Linear","VCenter,FillXY");

 // make buttons on screen
 btn = app.CreateButton("Get Version",0.3,-1,'gray');
 btn.SetTextSize(18);
 btn.SetOnTouch(btnGetVer);
 lay.AddChild(btn);

 btn = app.CreateButton("Show Version",0.3,-1,'gray');
 btn.SetTextSize(18);
 btn.SetOnTouch(btnShowVer);
 lay.AddChild(btn);

 btn = app.CreateButton("Exit",0.3,-1,'gray');
 btn.SetTextSize(18);
 btn.SetOnTouch(btnExit);
 lay.AddChild(btn);

 app.AddLayout(lay);
}

function btnGetVer( ) {
 plg = app.CreateStopwatch( );
 app.ShowPopup( 'Stopwatch version '+plg.GetVersion( ), 'bottom,short' );

}

function btnShowVer( ) {
 plg = app.CreateStopwatch( );
 plg.ShowVersion();
}

function btnExit( ) { app.Exit( ); }
  Copy   Copy All    Run   

You must issue a Start( ) function to start the timer when an action is complete a Stop( ) function should be declared.


To compare the time passed between two points of time. The Time( option ) function takes an optional "milli" option that provides time accuracy to the milliseconds.

Example:

Example - Stopwatch

app.LoadPlugin("Stopwatch");

//called when the application is started.
function OnStart( ) {
 lay = app.CreateLayout("Linear","VCenter,FillXY");

 // create screen buttons
 btn1 = app.CreateButton("Start",0.5,-1,'gray');
 btn1.SetTextSize( 18 );
 btn1.SetOnTouch( StartClock );

 btn2 = app.CreateButton("Elapse Time",0.5,-1,'gray');
 btn2.SetTextSize( 18 );
 btn2.SetOnTouch( StopClock );

 btn3 = app.CreateButton("Exit",0.5,-1,'gray');
 btn3.SetTextSize( 18 );
 btn3.SetOnTouch( Exit );

 // layout the screen
 lay.AddChild( btn1 );
 lay.AddChild( btn2 );
 lay.AddChild( btn3 );
 app.AddLayout( lay );

 plg_sw = app.CreateStopwatch( );
}

// start the clock
function StartClock( ) {
 plg_sw.Start( );
 app.ShowPopup('Starting the clock','bottom');
}

// stop the clock
function StopClock( ) {
 plg_sw.Stop( );
 app.ShowPopup('Time Elapsed: '+plg_sw.Time('milli'),'bottom');
}

// exit the app
function Exit( ) {
 app.Exit();
}

Copy All  Run 

The following methods are available on the Stopwatch object:

Start( )

 - Start the timer

Stop( )

 - Stop the timer

Time( option ) - returns string

option (optional) - option milli for accuracy to the milliseconds

 - handles milliseconds, seconds, minutes, hours, days of time since started

 - returns a time formatted string of days, hours, mins, secs, milliseconds

This DS Code plugin was written by Nelson Tengkiat, it is free to use in your projects. A credit line if used would be nice :)

* **File 2: The inc-file with the Plugin-code (Javascript)** /* Stopwatch Library Code Author by: Nelson Tengkiat Stopwatch version history 1.00 dated: pre 2016 - initial code not yet plugin 1.10 dated: jan 25, 2016 - initial creation of plugin - not release to public - added GetVersion(), Help(); - added int addZero(i); - added pub Help(), Start(), Stop(), Time(option) 1.11 dated: jan 30, 2016 - added help function - edited stopwatch example program 1.12 dated: jan 31, 2016 - edited stopwatch example program - initialize stopwatch value to 0 todo: add laps */ // ****************************** // * Library Creation // ****************************** // stopwatch script app.CreateStopwatch = function() { return new sWatch();} // ****************************** // * Stopwatch functions // ****************************** // show time difference on the stopwatch // option - milli (for accuracy) function sWatch() { //app.SetDebugEnabled(false); // disable debug // ****************************** // * global vars // ****************************** // stopwatch global var var o_sw = { // code version "ver": '1.12', "start": 0, "stop": 0, "time": 0 } var shelp= 'function StopWatch()\n\n'+ 'methods\n'+ ' Start() - start the watch\n'+ ' Stop() - stop the watch\n'+ ' Time() - time diffrence\n'; // ****************************** // * Get / Show version // ****************************** // return the version number this.GetVersion = function () { return o_sw.ver } // show version on screen this.ShowVersion = function () { ss='sWatch ver '+this.GetVersion(); app.ShowPopup(ss,'bottom'); } // ****************************** // * Internal functions // ****************************** // pad zero in front if less than 2 char addZero = function (i) { if (i < 10) { i = "0" + i; } return i } // function addZero(i) // ****************************** // * Public functions // ****************************** // give the help screen this.Help = function () { alert(shelp); } // start the stopwatch. save ticks this.Start = function () { app.SetDebugEnabled(false); // disable debug o_sw.start=new Date().getTime(); app.SetDebugEnabled(true); // re-enable debug } // stop the stopwatch this.Stop = function () { app.SetDebugEnabled(false); // disable debug o_sw.stop=new Date().getTime(); o_sw.time=o_sw.stop-o_sw.start; app.SetDebugEnabled(true); // re-enable debug } // show time difference on the stopwatch // option - milli (for accuracy) this.Time = function (option) { app.SetDebugEnabled(false); // disable debug var days=hrs=mins=secs=milli=0; var str=""; unt=tmp=0; var lap=o_sw.time; // days tmp=lap/86400000; days=Math.floor(tmp); lap-=days*86400000; if (days!="00") unt++; // hours tmp=lap/3600000; hrs=addZero(Math.floor(tmp)); lap-=hrs*3600000; if (hrs!="00") unt++; // minutes tmp=lap/60000; mins=addZero(Math.floor(tmp)); lap-=mins*60000; if (mins!="00") unt++; // seconds tmp=lap/1000; secs=addZero(Math.floor(tmp)); lap-=secs*1000; if (secs!="00") unt++; // make sure millisecs is 3 digits milli=lap.toString(); while (milli.length<3) {milli="0"+milli} // add string of units switch (unt) { case 0: str=milli+" millisecs"; break; case 1: if (option=="milli") str=secs+"."+milli+" secs"; else str=secs+" secs"; break; case 2: if (option=="milli") str=mins+":"+secs+"."+milli+" mins"; else str=mins+":"+secs+" mins"; break; case 3: if (option=="milli") str=hrs+"hrs "+mins+":"+secs+"."+milli+" mins"; else str=hrs+"hrs "+mins+":"+secs+"mins"; break; case 4: if (option=="milli") str=days+" days "+hrs+":"+mins+" hrs "+secs+"."+milli+" secs"; else str=days+" days "+hrs+":"+mins+"hrs "+secs+'.'+milli+'secs'; break; } //app.SetDebugEnabled(true); // re-enable debug return str; } // Time (option) //app.SetDebugEnabled(true); // re-enable debug } // sWatch() * **File 3: The text-file with the Version number** 1.12 * **File 4: A blank jar-file without any text** ====Second Step:==== Create a Zip-folder called "MyPlugin.zip" and copy this folder to the right path on your android-device: * Select the 4 Files and use a tool on a Windows Desktop-PC like [[http://www.7-zip.org/|7-Zip]] to create the Zip-File. If you Use Android you can use [[https://play.google.com/store/apps/details?id=com.estrongs.android.pop&hl=en|ES File Explorer]] to create the file. * after this process you should have a file with the following structure: Stopwatch.zip Stopwatch.html Stopwatch.inc Stopwatch.jar Version.txt * Now you go on your Android Device to the folder **/sdcard/DroidScript** and create a new folder called "**Plugins**" within the folder **Droidscript** * Copy the zip-file in this folder and start Droidscript * DroidScript imports the Plugin now in the Droidscript-Pluginfolder in the system path * Now you can see your plugin if you press the left "Docs"-button and than the "Plugins"-button * The folder **/sdcard/DroidScript/Plugins** is blank after this process =====How can you uninstall your plugin?===== This is a copy of the uninstall program from a different section of this wiki site. //Called when application is started. function OnStart() { //Create a layout with objects vertically centered. lay = app.CreateLayout( "linear", "VCenter,FillXY" ); //Create a text label and add it to layout. txt = app.CreateTextEdit( "" ); txt.SetHint("Plugin to delete") lay.AddChild( txt ); btn=app.CreateButton("Delete Plugin"); btn.SetOnTouch(DeleteUserPlugin); lay.AddChild(btn); privFldr = app.GetPrivateFolder( "Plugins" ); plgins = app.ListFolder(privFldr); lvw = app.CreateListView( plgins, "Select a Plugin for uninstalling or press Back" ); lvw.SetOnTouch( lvw_OnTouch ); //Add layout to app. app.AddLayout( lay ); } function lvw_OnTouch( item ) { txt.SetText( item ); } function DeleteUserPlugin() { var plg = "" + txt.GetText() if (plg == "") return; plugDir = privFldr + "/" + plg.toLowerCase(); if (app.FolderExists(plugDir)) { var list = app.ListFolder(plugDir); var yesNo = app.CreateYesNoDialog( "Do you really want to uninstall the plugin " + txt.GetText() + "? \nThe following files or folders will be all deleted:\n\n" + list + "\n\nIt is no way for undo!"); yesNo.SetOnTouch( yesNo_OnTouch ); } } function yesNo_OnTouch( yesNoresult ) { if( yesNoresult == "Yes" ) { app.DeleteFolder(plugDir); app.Alert("Plugin " + txt.GetText() + " uninstalled!"); txt.SetText(""); } else { app.ShowPopup("No changings!"); } } ----