DroidScript wiki

(was AndroidScript) unofficial documentation by the community

User Tools

Site Tools


plugins:stopwatch

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
plugins:stopwatch [2016/08/08 12:48]
202.8.246.29
plugins:stopwatch [2016/08/08 13:18] (current)
202.8.246.29
Line 1: Line 1:
 ====== Stopwatch Plugin ====== ====== 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 ​a *{{:​wiki:​Stopwatch.zip}}* plugin ​zip file to this post. You simply need to drop this zip file into a folder called **/​sdcard/​DroidScript/​Plugins** on your device and DroidScript will import it into the plugins list along with the documentation.+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.//​ //After DroidScript is executed, the plugin will appear in the plugin menu and it's documentation will be accessible.//​
Line 9: Line 9:
 Nelson Tengkiat (dated: Aug 8, 2016) 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**
 +
 +<code html Stopwatch.html>​
 +<​!DOCTYPE html> ​
 +<​html>​
 +
 +<​head>​
 +    <​title>​Stopwatch</​title>​
 + <meta name="​viewport"​ content="​width=device-width,​ initial-scale=1">​
 +</​head> ​
 +<!-- /use &emsp; as tabs &nbsp; as space -->
 +
 +<​body> ​
 +
 +<div data-role="​page"​ data-theme="​a">​
 +
 + <div data-role="​header"​ data-position="​fixed">​
 + <a href='#'​ class='​ui-btn-left'​ data-icon='​arrow-l'​ data-theme="​c"​ onclick="​history.back();​ return false">​Back</​a>​
 + <​h1>​Stopwatch Plugin</​h1>​
 + </​div><​!-- /header -->
 +
 + <div data-role="​content">​
 + <​p>​In order to use Stopwatch functions plugin, you must first load 
 + the plugin at the top of your script using the <​b>​LoadPlugin</​b>​ method ​
 + like this:</​p>​
 +     
 + <div class="​samp">​
 + <div id="​plug1">​
 + // Load the Stopwatch plugin<​br>​
 + app.LoadPlugin( "​Stopwatch"​ );<​br>​
 + ​ </​div>​
 + <div name="​divCopy"​ align="​right">​
 + <a href="#"​ data-role="​button"​ data-mini="​true"​ data-inline="​true"​ onclick="​copy(plug1)">&​nbsp;&​nbsp;​Copy&​nbsp;&​nbsp;</​a>​
 + </​div>​
 + </​div>​
 +
 + <​p>​Then you can create an instance of the plugin object when you need it like this:</​p>​
 + 
 + <div class="​samp">​
 + <div id="​plug2">​
 + &​emsp;​plg = app.CreateStopwatch( );</​div>​
 + <div name="​divCopy"​ align="​right">​
 + <a href="#"​ data-role="​button"​ data-mini="​true"​ data-inline="​true"​ onclick="​copy(plug2)">&​emsp;​Copy&​emsp;</​a>​
 + </​div>​
 + </​div> ​       ​
 +
 + <​br>​
 + <​p><​span style="​color:#​0099CC"><​large><​b>​Version Information</​b></​large></​span><​br>​
 + You can get the plugin version by calling <​b>​GetVersion( )</b> for program use or 
 + toast display the version by calling <​b>​ShowVersion( )</​b><​br>​
 + <​br>​
 + <​large><​b>​Methods:</​b></​large>​
 + <​br>​
 + <div data-role="​collapsible"​ data-collapsed="​true" ​ data-mini="​true"​ data-theme="​a"​ data-content-theme="​b">​
 + <​h3>​GetVersion( ) - returns string</​h3>​
 + <​style="​font-size:​100%">​
 + &​emsp;​- returns version string
 + </​div>​
 + <div data-role="​collapsible"​ data-collapsed="​true" ​ data-mini="​true"​ data-theme="​a"​ data-content-theme="​b">​
 + <​h3>​ShowVersion( )</​h3>​
 + <​style="​font-size:​100%">​
 + &​emsp;​- toast display the version number
 + </​div>​
 + <​br>​
 + <​large><​b>​Example:</​b></​large>​
 + </​p>​
 + <div data-role="​collapsible"​ data-collapsed="​true" ​ data-mini="​true"​ data-theme="​a"​ data-content-theme="​b">​
 + <​h3>​Example - Get / Show Version</​h3>​
 + <div id="​sam_ver"​ style="​font-size:​80%">​
 + app.LoadPlugin("​Stopwatch"​);<​br>​
 + <​br>​
 + function OnStart( ) {<br>
 + &​emsp;​lay=app.CreateLayout("​Linear","​VCenter,​FillXY"​);<​br>​
 + <​br>​
 + &​emsp;//​ make buttons on screen<​br>​
 + &​emsp;​btn = app.CreateButton("​Get Version",​0.3,​-1,'​gray'​);<​br>​
 + &​emsp;​btn.SetTextSize(18);<​br>​
 + &​emsp;​btn.SetOnTouch(btnGetVer);<​br>​
 + &​emsp;​lay.AddChild(btn);<​br>​
 + <​br>​
 + &​emsp;​btn = app.CreateButton("​Show Version",​0.3,​-1,'​gray'​);<​br>​
 + &​emsp;​btn.SetTextSize(18);<​br>​
 + &​emsp;​btn.SetOnTouch(btnShowVer);<​br>​
 + &​emsp;​lay.AddChild(btn);<​br>​
 + <​br>​
 + &​emsp;​btn = app.CreateButton("​Exit",​0.3,​-1,'​gray'​);<​br>​
 + &​emsp;​btn.SetTextSize(18);<​br>​
 + &​emsp;​btn.SetOnTouch(btnExit);<​br>​
 + &​emsp;​lay.AddChild(btn);<​br>​
 + <​br>​
 + &​emsp;​app.AddLayout(lay);<​br>​
 + }<​br>​
 + <​br>​
 + function btnGetVer( ) {<br>
 + <b id="​sam_snip" ​ style="​font-size:​100%">​
 + &​emsp;​plg = app.CreateStopwatch( );<​br>​
 + &​emsp;​app.ShowPopup( '​Stopwatch version '​+plg.GetVersion( ), '​bottom,​short'​ );</​b><​br>​
 + }<​br>​
 + <​br>​
 + function btnShowVer( ) {<br>
 + &​emsp;​plg = app.CreateStopwatch( );<​br>​
 + &​emsp;​plg.ShowVersion();<​br>​
 + }<​br>​
 + <​br>​
 + function btnExit( ) { app.Exit( ); }<br>
 + </​div>​
 + <div name="​divCopy"​ align="​right">​
 + <a href="#"​ data-role="​button"​ data-mini="​true"​ data-inline="​true"​ onclick="​copy(sam_snip)">&​emsp;&​emsp;​Copy&​emsp;&​emsp;</​a>​
 + <a href="#"​ data-role="​button"​ data-mini="​true"​ data-inline="​true"​ onclick="​copy(sam_ver)">​Copy All</​a>​
 + <a href="#"​ data-role="​button"​ data-mini="​true"​ data-inline="​true"​ onclick="​demo(sam_ver)">&​nbsp;&​nbsp;&​nbsp;​Run&​nbsp;&​nbsp;&​nbsp;</​a>​
 + </​div>​
 + </​div>​
 +
 + <​p>​You must issue a <​b>​Start( )</b> function to start the timer
 + when an action is complete a <​b>​Stop( )</b> function should be declared.</​p>​
 + <​br>​
 + To compare the time passed between two points of time.
 + The <​b>​Time( option )</b> function takes an optional <​b>"​milli"</​b>​ option
 + that provides time accuracy to the milliseconds.<​br>​
 + <​br>​
 + <​large><​b>​Example:</​b></​large>​
 + </​p>​
 + <div data-role="​collapsible"​ data-collapsed="​true" ​ data-mini="​true"​ data-theme="​a"​ data-content-theme="​b">​
 + <​h3>​Example - Stopwatch</​h3>​
 + <div id="​examp2"​ style="​font-size:​80%">​
 + app.LoadPlugin("​Stopwatch"​);<​br>​
 + <​br>​
 + //​called when the application is started.<​br>​
 + function OnStart( ) {<br>
 + &​emsp;​lay = app.CreateLayout("​Linear","​VCenter,​FillXY"​);<​br>​
 + <​br>​
 + &​emsp;//​ create screen buttons<​br>​
 + &​emsp;​btn1 = app.CreateButton("​Start",​0.5,​-1,'​gray'​);<​br>​
 + &​emsp;​btn1.SetTextSize( 18 );<​br>​
 + &​emsp;​btn1.SetOnTouch( StartClock );<​br>​
 + <​br>​
 + &​emsp;​btn2 = app.CreateButton("​Elapse Time",​0.5,​-1,'​gray'​);<​br>​
 + &​emsp;​btn2.SetTextSize( 18 );<​br>​
 + &​emsp;​btn2.SetOnTouch( StopClock );<​br>​
 + <​br>​
 + &​emsp;​btn3 = app.CreateButton("​Exit",​0.5,​-1,'​gray'​);<​br>​
 + &​emsp;​btn3.SetTextSize( 18 );<​br>​
 + &​emsp;​btn3.SetOnTouch( Exit );<​br>​
 + <​br>​
 + &​emsp;//​ layout the screen<​br>​
 + &​emsp;​lay.AddChild( btn1 );<​br>​
 + &​emsp;​lay.AddChild( btn2 );<​br>​
 + &​emsp;​lay.AddChild( btn3 );<​br>​
 + &​emsp;​app.AddLayout( lay );<​br>​
 + <​br>​
 + &​emsp;​plg_sw = app.CreateStopwatch( );<​br>​
 + }<​br>​
 + <​br>​
 + // start the clock<​br>​
 + function StartClock( ) {<br>
 + &​emsp;​plg_sw.Start( );<​br>​
 + &​emsp;​app.ShowPopup('​Starting the clock','​bottom'​);<​br>​
 + }<​br>​
 + <​br>​
 + // stop the clock<​br>​
 + function StopClock( ) {<br>
 + &​emsp;​plg_sw.Stop( );<​br>​
 + &​emsp;​app.ShowPopup('​Time Elapsed: '​+plg_sw.Time('​milli'​),'​bottom'​);<​br>​
 + }<​br>​
 + <​br>​
 + // exit the app<​br>​
 + function Exit( ) {<br>
 + &​emsp;​app.Exit();<​br>​
 + }<​br>​
 + <​br>​
 + </​div>​
 + <div name="​divCopy"​ align="​right">​
 + <a href="#"​ data-role="​button"​ data-mini="​true"​ data-inline="​true"​ onclick="​copy(examp2)">​Copy All</​a>​
 + <a href="#"​ data-role="​button"​ data-mini="​true"​ data-inline="​true"​ onclick="​demo(examp2)">&​emsp;​Run&​emsp;</​a>​
 + </​div>​
 + </​div>​
 +
 + <​br>​
 + <​p>​The following methods are available on the Stopwatch object:</​p>​
 + <div data-role="​collapsible"​ data-collapsed="​true" ​ data-mini="​true"​ data-theme="​a"​ data-content-theme="​b">​
 + <​h3>​Start( )</​h3>​
 + <​style="​font-size:​100%">​
 + &​emsp;​- Start the timer
 + </​div>​
 + <div data-role="​collapsible"​ data-collapsed="​true" ​ data-mini="​true"​ data-theme="​a"​ data-content-theme="​b">​
 + <​h3>​Stop( )</​h3>​
 + <​style="​font-size:​100%">​
 + &​emsp;​- Stop the timer
 + </​div>​
 + <div data-role="​collapsible"​ data-collapsed="​true" ​ data-mini="​true"​ data-theme="​a"​ data-content-theme="​b">​
 + <​h3>​Time( option ) - returns string</​h3>​
 + <​style="​font-size:​100%">​
 + &​emsp;<​b>​option</​b>​ (optional) - option <​b>​milli</​b>​ for accuracy to the milliseconds<​br>​
 + <​br>​
 + &​emsp;​- handles milliseconds,​ seconds, minutes, hours, days of time since started<​br>​
 + <​br>​
 + &​emsp;​- returns a time formatted string of days, hours, mins, secs, milliseconds
 + </​div>​
 +
 + <​br>​
 + <​p>​This DS Code plugin was written by Nelson Tengkiat, it is free to 
 + use in your projects. <b> A credit line if used would be nice :​)</​b>​
 + </​p>​
 +
 + </​div><​!-- /content -->
 +
 +</​div><​!-- /page -->
 +
 +</​body>​
 +</​html>​
 +</​code>​
 +
 +  * **File 2: The inc-file with the Plugin-code (Javascript)**
 +
 +<code Javascript Stopwatch.inc>​
 +/*
 +
 +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()
 +</​code>​
 +
 +  * **File 3: The text-file with the Version number**
 +
 +<code text Version.txt>​
 +1.12
 +</​code>​
 +
 +  * **File 4: A blank jar-file without any text**
 +
 +<code Java Stopwatch.jar>​
 +
 +</​code>​
 +
 +====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.
 +
 +<code javascript UninstallPlugin.js>​
 +
 +//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!"​);​
 +    }
 +}
 +
 +</​code>​
 +
 +
 +
 +----
  
plugins/stopwatch.txt ยท Last modified: 2016/08/08 13:18 by 202.8.246.29