MyPlugin
Back
MyPlugin
Todo: Documentation for MyPlugin
In order to use MyPlugin, you must first load the plugin at the top of your script
using the LoadPlugin method like this:
app.LoadPlugin( "MyPlugin" );
Then you can create an instance of the plugin object when you need it like this:
plg = app.CreateMyPlugin();
Examples:
Example - Get Version
app.LoadPlugin( "MyPlugin" );
function OnStart()
{
lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
btn = app.CreateButton( "Press Me" );
btn.SetOnTouch( CallPlugin );
lay.AddChild( btn );
plg = app.CreateMyPlugin();
app.AddLayout( lay );
}
function CallPlugin()
{
alert( plg.GetVersion() );
}
Example - Test Callback
app.LoadPlugin( "MyPlugin" );
function OnStart()
{
lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
btn = app.CreateButton( "Press Me" );
btn.SetOnTouch( CallPlugin );
lay.AddChild( btn );
plg = app.CreateMyPlugin();
plg.SetOnMyReply( OnMyReply );
app.AddLayout( lay );
}
function CallPlugin()
{
plg.MyFunc( "hello", 21, true );
}
function OnMyReply( txt, num, bool )
{
alert( "txt=" + txt + " num=" + num + " bool=" + bool );
}
* **File 2: The inc-file with the Plugin-code (Javascript)**
app.CreateMyPlugin = function()
{
return new MyPlugin();
}
function MyPlugin()
{
this.callback = null;
this.GetVersion = function( num, txt )
{
return 1.0;
}
this.MyFunc = function( txt, num, bool )
{
this.callback( txt + " World!!", num+20, !bool );
}
this.SetOnMyReply = function( cb )
{
this.callback = cb;
}
}
* **File 3: The text-file with the Versionnumber**
1.00
* **It is no longer necessary to add an empty jar-file and we recommend you do not so**
----
====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:
MyPlugin.zip
MyPlugin.html
MyPlugin.inc
MyPlugin.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 systempath
* 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
=====Notes=====
* If your Plugin had another name like "TestPlugin" you have to rename all the "MyPlugin"-words in all files!
* When you come to renaming and making your own plugins, please make sure you use the same format and case for file names as those shown in this sample!
* You can make sub folders in the zip file for your documentation images etc
=====How can you uninstall your plugin?=====
There are two ways at the moment:
You can download and run the following script if you want uninstall a plugin or you uninstall and reinstall Droidscript completely. Using the second option causes all your plugins to be uninstalled so you have to reinstall all plugins.
//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 );
lvw.Show();
}
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 );
yesNo.Show();
}
}
function yesNo_OnTouch( yesNoresult )
{
if( yesNoresult == "Yes" )
{
app.DeleteFolder(plugDir);
app.Alert("Plugin " + txt.GetText() + " uninstalled!");
txt.SetText("");
}
else
{
app.ShowPopup("No changings!");
}
}
----
===== Zip file with all 4 files =====
You can find the Zip-file with all 4 files here: {{:wiki:myplugin.zip|MyPlugin.zip}}