JavaScript Modules

The Android Agent JavaScript engine supports loading third-party or in-house modules which conform to the CommonJS standard.

Third-Party Modules

The following example demonstrates how to use fraction.js:

var fraction = require('fraction/fraction.js');
var result = fraction(1).div(98).mul(98); // = 1
mobicontrol.log.info('Result of operation: ' + result);

The best place to search for third-party JavaScript libraries is the NPM central repository.
In order to extract module files from that repository:

  1. Install NPM
  2. Install the corresponding NPM package with npm i <package>
  3. Fetch the JavaScript files from the node_modules folder

It should be noted that only pure JavaScript modules are supported by the JavaScript engine. Modules dependent on Node.js are not supported.

In-House Modules

The following example demonstrates how to extract Zebra MX handling into a separate module named zebraMx.js:

exports.version = function getMxVersion() {
   var versionQuery = '<wap-provisioningdoc>' +
                      '    <characteristic type="MX">' +
                      '        <parm-query name="Version"/>' +
                      '    </characteristic>' +
                      '</wap-provisioningdoc>';
   var response = new XML(mobicontrol.mdm.configure(versionQuery));
   return response..parm.@value;
}

exports.disableTetheringControl = function disableTetheringControl() {
   setTetheringControl("2")
}

exports.enableTetheringControl = function enableTetheringControl() {
   setTetheringControl("1")
}

function setTetheringControl(value) {
   var command = '<wap-provisioningdoc>' +
                 '    <characteristic type="SettingsMgr">' +
                 '        <parm name="TetheringandPortableHotspot" value="' + value + '"/>' +
                 '    </characteristic>' +
                 '</wap-provisioningdoc>';
   var response = new XML(mobicontrol.mdm.configure(command));
   if (response.hasOwnProperty("characteristic-error")) {
       throw response["characteristic-error"].@desc;
   }
}

This module defines three functions: version(), enableTetheringControl() and disableTetheringControl().
version() returns the current MX version. enableTetheringControl() and disableTetheringControl() manage access to tethering and Wi-Fi hotspot settings through the Android Settings panel or Quick Settings control.
The two tethering control functions rely on Zebra SettingsManager functionality, which was introduced in MX 8.1. Note how the module finds and reports the error by throwing.

Here is code which uses the zebraMx.js module:

var mx = require('zebraMx/zebraMx.js');

mobicontrol.log.info('Mx version is ' + mx.version());

mx.disableTetheringControl();

If the script is executed on a device with MX version older than 8.1, the script will fail with a log similar to The DSD version is higher than supported DSD 4.4 (zebraMx.js#17).

Module Installation

Before a module can be used, it needs to be downloaded to the %scripts% folder of the agent using a file sync rule. It is recommended to create a sub-folder for each module.

These are the steps to install fraction.js:

  1. On the server, create a folder for syncing scripts, for example C:\scripts. This step can be skipped if the script syncing folder already exists.
  2. Create a fraction subfolder and copy fraction.js into it.
  3. Create a file sync rule to download files from the server's C:\scripts folder to the device's %scripts% folder (including sub-folders) and assign the rule.

System Modules

System modules are JavaScript modules embedded in the agent. Script writers still need to use the 'require' keyword to load the module, but they do not need to deploy the module to the device - it's already there. Currently, the agent only has one system module - INI. Deployed modules (user modules) override system modules. That is, if the 'ini' module is deployed to the device's %scripts% folder, then it will be loaded when required by a script, instead of the system 'ini' module.