Back

Writing DS Plugins

Hello World

DroidScript can't do everything imaginable. But you can help it to come closer to that by writing plugins.

There are basically two types of plugins: JavaScript plugins which will behave like an extra included JS source script and
Java plugins, which have a second compiled part written in java.

JavaScript Plugins

A basic JS plugin consists of 2 parts:
- a MyPlugin.inc file containing the main JS source code
- a MyPlugin.html file containing the main page of your plugin documentation.

However you can add as many resources to it as you want.

There is a generator available which can create, debug and install plugins on the fly. you can even debug your plugin directly from that app.
Download it from the dspk store

Java Plugins

Writing java plugins is a bit tricky but once you get used to it you can get almost everything working.

Structure

There are two types of Java plugin packages:
1. The standard zip file:
- contains a MyPlugin.inc and a MyPlugin.html file as JS plugins
- a MyPlugin.jar file which contains the compiled classes.dex file.

2. An APK file:
- an assets/ folder containing the MyPlugin.inc and the MyPlugin.html file
- a compiled classes.dex file


APK files are typically exported by AndroidStudio, AIDE and others.
That leads us to the ways how you can write and more importantly compile java plugins.

Compile and Install

1. The android app 'AIDE'
- Download AIDE from PlayStore
- Create a new AIDE project using the DroidScript SDK in the DS menu
- Modify, Build, Install and Run the plugin app. A popup will appear once the plugin is ready to be installed on the next DroidScript restart


2. AndroidStudio
- There is a UserPlugin template available on DroidScript's GitHub page
- Follow the instructions in this BlogSpot guide


It may be possible with other IDE's as well (ie. IntelliJ even has a AndroidSDK Plugin), but this has not been tested before or just couldn't be made to work.

Communicate with your Java code

In order to use the defined Java functions you have to call them from the JS part somehow.
For this purpose DroidScript provides a _CreatePlugin component:

var plg = _CreatePlugin( classpath, options )
plg.GetType()string: “Plugin”
plg.Send( name, p1, p2, p3, p4, p5, p6, p7, p8 )string
plg.SendObj( name, obj, p1, p2, p3, p4, p5, p6, p7, p8 )string
plg.CreateObj( type, p1, p2, p3, p4, p5, p6, p7, p8 )string: “#id”
plg.SendImg( name, img, width, height )string
plg.SendCam( name, cam )string

On the Java side following functions in public class MyPlugin { } will get called by DroidScript:

public MyPlugin()
public void Init( Context ctx, Object parent )
public void Release()
public String CallPlugin( Bundle b )
public String CallPlugin( Bundle b, Object obj )
public Object CreateObject( Bundle b )

app object
string
number: pixel
string: comma “,” separated
string: “com.mycompany.plugins.user”
string
number
boolean
object: { cmd, img, p1, p2, p3, p4, p5, p6, p7, p8 }
object: { cmd, p1, p2, p3, p4, p5, p6, p7, p8 }
object: { type, p1, p2, p3, p4, p5, p6, p7, p8 }
app object: Image
app object: CameraView
Calls Release()
Returns the component class name
Calls CallPlugin(Bundle)
Calls CallPlugin(Bundle, Object)
Calls CreateObject(Bundle) with a String on “type”
Calls CallPlugin(Bundle) with a byte[] on “img”
Calls CallPlugin(Bundle) with a byte[] on “cam”
Called when a new plugin instance was created
Called by DroidScript passing the app context/activity and the parent object
Called when java gets rid of your plugin
Called by plg.Send and plg.SendImg
Called by plg.SendObj
Called by plg.CreateObj