'use strict'; var exec = require('cordova/exec'); var SERVICE = 'BluememeUtils'; var eventHandlers = {}; function noop() {} function asWifiOptions(input) { if (typeof input === 'string') { return { ssid: input }; } return input || {}; } function call(action, args, success, error) { exec(success || noop, error || noop, SERVICE, action, args || []); } function createCustomEvent(name, detail) { var eventName = 'bluememeutils.' + name; if (typeof window.CustomEvent === 'function') { return new window.CustomEvent(eventName, { detail: detail }); } var event = document.createEvent('CustomEvent'); event.initCustomEvent(eventName, false, false, detail); return event; } function dispatchLocalEvent(event) { if (!event || !event.type) { return; } var handlers = eventHandlers[event.type] || []; handlers.slice().forEach(function (handler) { handler(event); }); if (typeof window !== 'undefined' && typeof window.dispatchEvent === 'function') { window.dispatchEvent(createCustomEvent(event.type, event)); } } var api = { getDeviceInfo: function (success, error) { call('getDeviceInfo', [], success, error); }, saveWifi: function (options, success, error) { call('saveWifi', [asWifiOptions(options)], success, error); }, connectWifi: function (options, success, error) { call('connectWifi', [asWifiOptions(options)], success, error); }, disconnectWifi: function (success, error) { call('disconnectWifi', [], success, error); }, removeWifi: function (options, success, error) { call('removeWifi', [asWifiOptions(options)], success, error); }, requestWifiPermission: function (success, error) { call('requestWifiPermission', [], success, error); }, isBiometricAvailable: function (success, error) { call('isBiometricAvailable', [], success, error); }, unlockWithBiometrics: function (options, success, error) { call('unlockWithBiometrics', [options || {}], success, error); }, requestPushPermission: function (success, error) { call('requestPushPermission', [], success, error); }, getFirebaseToken: function (success, error) { call('getFirebaseToken', [], success, error); }, subscribeToTopic: function (topic, success, error) { call('subscribeToTopic', [topic], success, error); }, unsubscribeFromTopic: function (topic, success, error) { call('unsubscribeFromTopic', [topic], success, error); }, getInitialNotification: function (success, error) { call('getInitialNotification', [], success, error); }, startNotificationListener: function (success, error) { call('startNotificationListener', [], function (event) { dispatchLocalEvent(event); if (success) { success(event); } }, error); }, stopNotificationListener: function (success, error) { call('stopNotificationListener', [], success, error); }, on: function (type, handler) { if (!eventHandlers[type]) { eventHandlers[type] = []; } eventHandlers[type].push(handler); }, off: function (type, handler) { var handlers = eventHandlers[type]; if (!handlers) { return; } var index = handlers.indexOf(handler); if (index >= 0) { handlers.splice(index, 1); } } }; module.exports = api;