From: Pawel Andruszkiewicz Date: Thu, 7 Apr 2016 12:11:14 +0000 (+0200) Subject: [Events] Code structure changed to match cordova architecture. X-Git-Tag: submit/tizen/20160411.115901^2~7 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fcfe9c350c8256524fa539e4603f909718cdda07;p=platform%2Fcore%2Fapi%2Fcordova-plugins.git [Events] Code structure changed to match cordova architecture. Change-Id: I148576302885da489c84490ddcc0ac0fb6743e48 Signed-off-by: Pawel Andruszkiewicz --- diff --git a/src/cordova-api.gyp b/src/cordova-api.gyp index 21eb766..970edd7 100644 --- a/src/cordova-api.gyp +++ b/src/cordova-api.gyp @@ -9,7 +9,6 @@ 'type': 'none', 'dependencies': [ 'cordova/cordova.gyp:*', - 'events/cordova_events.gyp:*', 'file/cordova_file.gyp:*', 'globalization/cordova_globalization.gyp:*', 'networkinformation/cordova_networkinformation.gyp:*', diff --git a/src/events/cordova_events.gyp b/src/events/cordova_events.gyp deleted file mode 100644 index a269cd9..0000000 --- a/src/events/cordova_events.gyp +++ /dev/null @@ -1,25 +0,0 @@ -{ - 'includes':[ - '/usr/include/webapi-plugins/src/common/common.gypi', - ], - 'targets': [ - { - 'target_name': 'tizen_cordova_events', - 'type': 'loadable_module', - 'sources': [ - 'cordova_events_api.js', - 'cordova_events_extension.cc', - 'cordova_events_extension.h', - ], - 'include_dirs': [ - '../', - '<(SHARED_INTERMEDIATE_DIR)', - ], - 'variables': { - 'packages': [ - 'webapi-plugins', - ], - }, - }, - ], -} diff --git a/src/events/cordova_events_api.js b/src/events/cordova_events_api.js deleted file mode 100755 index a407271..0000000 --- a/src/events/cordova_events_api.js +++ /dev/null @@ -1,195 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// TODO: remove when added to public cordova repository -> begin -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, target) { - EventHandler.call(this, name); - this.event_type = event_type; - this.callback = callback; - this.target = target || window; -} - -WindowEventHandler.prototype = Object.create(EventHandler.prototype); -WindowEventHandler.prototype.constructor = WindowEventHandler; - -WindowEventHandler.prototype.startListener = function(l) { - if (this.callback) { - this.listener = l; - this.target.addEventListener(this.event_type, this.callback); - } else { - Object.getPrototypeOf(WindowEventHandler.prototype).startListener.call(this, l); - } -}; - -WindowEventHandler.prototype.stopListener = function() { - if (this.callback) { - this.target.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, document); -} - -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); - } - } -}; - -require('cordova/exec/proxy').add('Events', exports); - -console.log('Loaded cordova.events API'); - -// TODO: remove when added to public cordova repository -> begin -}); - -exports = function(require) { - require('cordova-tizen').addPlugin('cordova-plugin-events.register', plugin_name, 'runs'); -}; -// TODO: remove -> end diff --git a/src/events/cordova_events_extension.cc b/src/events/cordova_events_extension.cc deleted file mode 100755 index 0f59ffe..0000000 --- a/src/events/cordova_events_extension.cc +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "events/cordova_events_extension.h" - -// This will be generated from cordova_events_api.js -extern const char kSource_cordova_events_api[]; - -common::Extension* CreateExtension() { - return new extension::cordova::events::CordovaEventsExtension(); -} - -namespace extension { -namespace cordova { -namespace events { - -CordovaEventsExtension::CordovaEventsExtension() { - SetExtensionName("tizen.cordova.events"); - SetJavaScriptAPI(kSource_cordova_events_api); -} - -CordovaEventsExtension::~CordovaEventsExtension() {} - -} // events -} // cordova -} // extension diff --git a/src/events/cordova_events_extension.h b/src/events/cordova_events_extension.h deleted file mode 100755 index 6d85a7a..0000000 --- a/src/events/cordova_events_extension.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef EVENTS_CORDOVA_EVENTS_EXTENSION_H_ -#define EVENTS_CORDOVA_EVENTS_EXTENSION_H_ - -#include - -namespace extension { -namespace cordova { -namespace events { - -class CordovaEventsExtension : public common::Extension { - public: - CordovaEventsExtension(); - virtual ~CordovaEventsExtension(); -}; - -} // events -} // cordova -} // extension - -#endif // EVENTS_CORDOVA_EVENTS_EXTENSION_H_ diff --git a/src/lib/cordova_plugins.js b/src/lib/cordova_plugins.js index c2ef0cf..699af6e 100644 --- a/src/lib/cordova_plugins.js +++ b/src/lib/cordova_plugins.js @@ -62,6 +62,11 @@ module.exports = [ "id": "cordova-plugin-events.register", "runs": true }, + { + "file": "plugins/cordova-plugin-events/tizen/Events.js", + "id": "cordova-plugin-events.tizen.Events", + "runs": true + }, { "file": "plugins/cordova-plugin-file/www/DirectoryEntry.js", "id": "cordova-plugin-file.DirectoryEntry", diff --git a/src/lib/plugins/cordova-plugin-events/tizen/Events.js b/src/lib/plugins/cordova-plugin-events/tizen/Events.js new file mode 100755 index 0000000..6c4f048 --- /dev/null +++ b/src/lib/plugins/cordova-plugin-events/tizen/Events.js @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// TODO: remove when added to public cordova repository -> begin +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, target) { + EventHandler.call(this, name); + this.event_type = event_type; + this.callback = callback; + this.target = target || window; +} + +WindowEventHandler.prototype = Object.create(EventHandler.prototype); +WindowEventHandler.prototype.constructor = WindowEventHandler; + +WindowEventHandler.prototype.startListener = function(l) { + if (this.callback) { + this.listener = l; + this.target.addEventListener(this.event_type, this.callback); + } else { + Object.getPrototypeOf(WindowEventHandler.prototype).startListener.call(this, l); + } +}; + +WindowEventHandler.prototype.stopListener = function() { + if (this.callback) { + this.target.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, document); +} + +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); + } + } +}; + +require('cordova/exec/proxy').add('Events', exports); + +console.log('Loaded cordova.events API'); + +// TODO: remove when added to public cordova repository -> begin +}); +// TODO: remove -> end