kiot-cordova-plugin-home-console
Version:
This plugin allows your Android app to download and install compressed updates without the Google Play Store.
114 lines (105 loc) • 3.26 kB
JavaScript
// plugin.js
var exec = require('cordova/exec');
var RockChipInterface = {
// Event listeners
eventListeners: {
rotation: [],
button: []
},
/**
* Start the interface
*
* @returns {Promise} Promise that resolves when started
*/
start: function() {
var self = this;
return new Promise(function(resolve, reject) {
exec(
function(result) {
if (typeof result === 'string') {
// This is the initial success response
resolve(result);
} else if (typeof result === 'object') {
// This is an event
self._handleEvent(result);
}
},
reject,
'RockChipInterface',
'start',
[]
);
});
},
/**
* Stop the interface
*
* @returns {Promise} Promise that resolves when stopped
*/
stop: function() {
return new Promise(function(resolve, reject) {
exec(resolve, reject, 'RockChipInterface', 'stop', []);
});
},
/**
* Get the current rotary encoder position
*
* @returns {Promise<number>} Promise that resolves with the current position
*/
getPosition: function() {
return new Promise(function(resolve, reject) {
exec(resolve, reject, 'RockChipInterface', 'getPosition', []);
});
},
/**
* Set the rotary encoder position
*
* @param {number} position - The position to set
* @returns {Promise} Promise that resolves when the position is set
*/
setPosition: function(position) {
return new Promise(function(resolve, reject) {
exec(resolve, reject, 'RockChipInterface', 'setPosition', [position]);
});
},
/**
* Add an event listener
*
* @param {string} eventType - The event type (rotation, button)
* @param {Function} listener - The event listener
*/
addEventListener: function(eventType, listener) {
if (this.eventListeners[eventType]) {
this.eventListeners[eventType].push(listener);
}
},
/**
* Remove an event listener
*
* @param {string} eventType - The event type (rotation, button)
* @param {Function} listener - The event listener to remove
*/
removeEventListener: function(eventType, listener) {
if (this.eventListeners[eventType]) {
var index = this.eventListeners[eventType].indexOf(listener);
if (index !== -1) {
this.eventListeners[eventType].splice(index, 1);
}
}
},
/**
* Handle an event from the native side
*
* @private
* @param {Object} event - The event object
*/
_handleEvent: function(event) {
if (event.type && this.eventListeners[event.type]) {
var listeners = this.eventListeners[event.type];
for (var i = 0; i < listeners.length; i++) {
listeners[i](event);
}
}
}
};
module.exports = RockChipInterface;