[Events] Added common code on cordova side.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 30 Nov 2015 14:46:11 +0000 (15:46 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Tue, 1 Dec 2015 06:48:35 +0000 (07:48 +0100)
Native implementation still needs to be added.

Change-Id: I23bab24d55306e6f3bbb349a197ba4d7cb6a80e6
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/cordova-api.gyp
src/events/cordova_events.gyp [new file with mode: 0644]
src/events/cordova_events_api.js [new file with mode: 0755]
src/events/cordova_events_extension.cc [new file with mode: 0755]
src/events/cordova_events_extension.h [new file with mode: 0755]
src/lib/cordova_plugins.js
src/lib/plugins/cordova-plugin-events/www/register.js [new file with mode: 0644]

index 24ed756..1b6354e 100644 (file)
@@ -11,6 +11,7 @@
         'cordova/cordova.gyp:*',
         'device/cordova_device.gyp:*',
         'dialog/cordova_dialog.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
new file mode 100644 (file)
index 0000000..a269cd9
--- /dev/null
@@ -0,0 +1,25 @@
+{
+  '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
new file mode 100755 (executable)
index 0000000..d0e14c0
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * 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
+
+exports = {
+  startListener: function(successCallback, errorCallback, args) {
+    var e = args[0];
+  },
+  stopListener: function(successCallback, errorCallback, args) {
+    var e = args[0];
+  }
+};
+
+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
new file mode 100755 (executable)
index 0000000..0f59ffe
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * 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
new file mode 100755 (executable)
index 0000000..6d85a7a
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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 <common/extension.h>
+
+namespace extension {
+namespace cordova {
+namespace events {
+
+class CordovaEventsExtension : public common::Extension {
+ public:
+  CordovaEventsExtension();
+  virtual ~CordovaEventsExtension();
+};
+
+}  // events
+}  // cordova
+}  // extension
+
+#endif // EVENTS_CORDOVA_EVENTS_EXTENSION_H_
index e3baec6..797e7f8 100644 (file)
@@ -29,6 +29,11 @@ module.exports = [
         ]
     },
     {
+      "file": "plugins/cordova-plugin-events/www/register.js",
+      "id": "cordova-plugin-events.register",
+      "runs": true
+    },
+    {
         "file": "plugins/cordova-plugin-file/www/DirectoryEntry.js",
         "id": "cordova-plugin-file.DirectoryEntry",
         "clobbers": [
@@ -230,6 +235,7 @@ module.exports.metadata =
     "cordova-plugin-console": "1.0.1",
     "cordova-plugin-device": "1.0.1",
     "cordova-plugin-dialogs": "1.1.1",
+    "cordova-plugin-events": "0.0.1",
     "cordova-plugin-file": "3.0.0",
     "cordova-plugin-file-transfer": "1.3.0",
     "cordova-plugin-network-information": "1.0.1",
diff --git a/src/lib/plugins/cordova-plugin-events/www/register.js b/src/lib/plugins/cordova-plugin-events/www/register.js
new file mode 100644 (file)
index 0000000..c8c25a6
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * 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.
+ */
+
+cordova.define("cordova-plugin-events.register", function(require, exports, module) {
+
+var cordova = require('cordova');
+var channel = require('cordova/channel');
+var exec = require('cordova/exec');
+
+var PLUGIN_NAME = 'Events';
+
+var handled_events = {};
+
+function fireEventListener(e) {
+  if (handled_events[e]) {
+    cordova.fireDocumentEvent(e);
+  } else {
+    console.error('Unknown event: ' + e);
+  }
+}
+
+function addChannelListener(ch, event_name) {
+  ch.onHasSubscribersChange = function() {
+    var start = this.numHandlers > 0;
+    var method = start ? 'startListener' : 'stopListener';
+    var callback = start ? fireEventListener : null;
+    exec(callback, null, PLUGIN_NAME, method, [event_name]);
+  };
+  handled_events[event_name] = event_name;
+}
+
+function createChannelForButton(button) {
+  var ch = cordova.addDocumentEventHandler(button);
+  addChannelListener(ch, button);
+}
+
+var buttons = [
+  'backbutton',
+  'menubutton',
+  'searchbutton',
+  'startcallbutton',
+  'endcallbutton',
+  'volumedownbutton',
+  'volumeupbutton'
+];
+
+for (var i = 0; i < buttons.length; ++i) {
+  createChannelForButton(buttons[i]);
+}
+
+var events = {
+  pause: channel.onPause,
+  resume: channel.onResume
+};
+
+for (var e in events) {
+  if (events.hasOwnProperty(e)) {
+    addChannelListener(events[e], e);
+  }
+}
+
+});