From 9b9cf255de9ff83fc7c7ad916437e43abdad2859 Mon Sep 17 00:00:00 2001 From: Pawel Andruszkiewicz Date: Tue, 1 Dec 2015 11:22:08 +0100 Subject: [PATCH] [Events] Added native implementation. Change-Id: Ifb1605e3404485bc5947657ca72151c47ce42b84 Signed-off-by: Pawel Andruszkiewicz --- src/events/cordova_events_api.js | 152 +++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/src/events/cordova_events_api.js b/src/events/cordova_events_api.js index d0e14c0..c99afc9 100755 --- a/src/events/cordova_events_api.js +++ b/src/events/cordova_events_api.js @@ -20,12 +20,164 @@ var plugin_name = 'cordova-plugin-events.tizen.Events'; cordova.define(plugin_name, function(require, exports, module) { // TODO: remove -> end +//////////////////////////// EventHandler +function EventHandler(name) { + this.name = name; +} + +EventHandler.prototype.startListener = function(l) { + console.error('Event \"' + this.name + '\" is not suported.'); +}; + +EventHandler.prototype.stopListener = function() { + console.error('Event \"' + this.name + '\" is not suported.'); +}; + +//////////////////////////// WindowEventHandler +function WindowEventHandler(name, event_type, callback) { + EventHandler.call(this, name); + this.event_type = event_type; + this.callback = callback; +} + +WindowEventHandler.prototype = Object.create(EventHandler.prototype); +WindowEventHandler.prototype.constructor = WindowEventHandler; + +WindowEventHandler.prototype.startListener = function(l) { + if (this.callback) { + this.listener = l; + window.addEventListener(this.event_type, this.callback); + } else { + Object.getPrototypeOf(WindowEventHandler.prototype).startListener.call(this, l); + } +}; + +WindowEventHandler.prototype.stopListener = function() { + if (this.callback) { + window.removeEventListener(this.event_type, this.callback); + this.listener = undefined; + } else { + Object.getPrototypeOf(WindowEventHandler.prototype).stopListener.call(this); + } +}; + +//////////////////////////// HwKeyEventHandler +function HwKeyEventHandler(name, type) { + var that = this; + var callback = function(e) { + if (type === e.keyName && that.listener) { + that.listener(that.name); + } + }; + WindowEventHandler.call(this, name, 'tizenhwkey', callback); +} + +HwKeyEventHandler.prototype = Object.create(WindowEventHandler.prototype); +HwKeyEventHandler.prototype.constructor = HwKeyEventHandler; + +//////////////////////////// VisibilityEventHandler +function VisibilityEventHandler(name, hidden) { + var prop, visibility_event, callback; + + if (typeof document.hidden !== 'undefined') { + prop = 'hidden'; + visibility_event = 'visibilitychange'; + } else if (typeof document.webkitHidden !== 'undefined') { + prop = 'webkitHidden'; + visibility_event = 'webkitvisibilitychange'; + } + + if (prop) { + var that = this; + callback = function() { + if (hidden === document[prop] && that.listener) { + that.listener(that.name); + } + }; + } + + WindowEventHandler.call(this, name, visibility_event, callback); +} + +VisibilityEventHandler.prototype = Object.create(WindowEventHandler.prototype); +VisibilityEventHandler.prototype.constructor = VisibilityEventHandler; + +//////////////////////////// InputDeviceEventHandler +function InputDeviceEventHandler(name, type) { + var callback; + + try { + this.key = tizen.inputdevice.getKey(type); + + var that = this; + callback = function(e) { + if (that.key.code === e.keyCode && that.listener) { + that.listener(that.name); + } + }; + } catch (e) { + console.error('Exception: ' + e.message); + } + + WindowEventHandler.call(this, name, 'keydown', callback); +} + +InputDeviceEventHandler.prototype = Object.create(WindowEventHandler.prototype); +InputDeviceEventHandler.prototype.constructor = InputDeviceEventHandler; + +InputDeviceEventHandler.prototype.startListener = function(l) { + if (this.key) { + try { + tizen.inputdevice.registerKey(this.key.name); + } catch (e) { + console.error('Exception: ' + e.message); + } + } + + Object.getPrototypeOf(InputDeviceEventHandler.prototype).startListener.call(this, l); +}; + +InputDeviceEventHandler.prototype.stopListener = function() { + if (this.key) { + try { + tizen.inputdevice.unregisterKey(this.key.name); + } catch (e) { + console.error('Exception: ' + e.message); + } + } + + Object.getPrototypeOf(InputDeviceEventHandler.prototype).stopListener.call(this); +}; + +//////////////////////////// all handlers +var handlers = { + backbutton: new HwKeyEventHandler('backbutton', 'back'), + menubutton: new HwKeyEventHandler('menubutton', 'menu'), + searchbutton: new EventHandler('searchbutton'), + startcallbutton: new EventHandler('startcallbutton'), + endcallbutton: new EventHandler('endcallbutton'), + volumedownbutton: new InputDeviceEventHandler('volumedownbutton', 'VolumeDown'), + volumeupbutton: new InputDeviceEventHandler('volumeupbutton', 'VolumeUp'), + pause: new VisibilityEventHandler('pause', true), + resume: new VisibilityEventHandler('resume', false) +}; + exports = { startListener: function(successCallback, errorCallback, args) { var e = args[0]; + if (handlers[e]) { + handlers[e].startListener(successCallback); + } else { + console.error('Unknown event: ' + e); + } }, stopListener: function(successCallback, errorCallback, args) { var e = args[0]; + if (handlers[e]) { + handlers[e].stopListener(); + } else { + console.error('Unknown event: ' + e); + } } }; -- 2.34.1