[Cordova] NetworkInformation plugin implemented.
authorLukasz Bardeli <l.bardeli@samsung.com>
Wed, 21 Oct 2015 11:06:39 +0000 (13:06 +0200)
committerLukasz Bardeli <l.bardeli@samsung.com>
Wed, 21 Oct 2015 11:06:39 +0000 (13:06 +0200)
[Verification] Test pass rate: 100% (4/4/0/0/0)

Change-Id: Ic566c97fa34f299b8b91d9309089867601cd6aac
Signed-off-by: Lukasz Bardeli <l.bardeli@samsung.com>
src/cordova-api.gyp
src/networkinformation/cordova_networkinformation.gyp [new file with mode: 0644]
src/networkinformation/cordova_networkinformation_api.js [new file with mode: 0644]
src/networkinformation/cordova_networkinformation_extension.cc [new file with mode: 0644]
src/networkinformation/cordova_networkinformation_extension.h [new file with mode: 0644]

index bb61934..e0f29c2 100644 (file)
@@ -11,6 +11,7 @@
         'cordova/cordova.gyp:*',
         'device/cordova_device.gyp:*',
         'file/cordova_file.gyp:*',
+        'networkinformation/cordova_networkinformation.gyp:*',
       ],
     },
   ], # end targets
diff --git a/src/networkinformation/cordova_networkinformation.gyp b/src/networkinformation/cordova_networkinformation.gyp
new file mode 100644 (file)
index 0000000..e22a8d1
--- /dev/null
@@ -0,0 +1,25 @@
+{
+  'includes':[
+    '/usr/include/webapi-plugins/src/common/common.gypi',
+  ],
+  'targets': [
+    {
+      'target_name': 'tizen_cordova_networkinformation',
+      'type': 'loadable_module',
+      'sources': [
+        'cordova_networkinformation_api.js',
+        'cordova_networkinformation_extension.cc',
+        'cordova_networkinformation_extension.h',
+      ],
+      'include_dirs': [
+        '../',
+        '<(SHARED_INTERMEDIATE_DIR)',
+      ],
+      'variables': {
+        'packages': [
+          'webapi-plugins',
+        ],
+      },
+    },
+  ],
+}
diff --git a/src/networkinformation/cordova_networkinformation_api.js b/src/networkinformation/cordova_networkinformation_api.js
new file mode 100644 (file)
index 0000000..4504096
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * 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.
+ */
+
+var _global = window || global || {};
+
+var Connection = {
+  UNKNOWN: "unknown",
+  ETHERNET: "ethernet",
+  WIFI: "wifi",
+  CELL_2G: "2g",
+  CELL_3G: "3g",
+  CELL_4G: "4g",
+  CELL:"cellular",
+  NONE: "none"
+};
+
+var type = Connection.UNKNOWN;
+
+var connection = {};
+
+Object.defineProperty(connection, 'type', {
+  get: function() { return type; },
+  set: function() {},
+  enumerable: true
+});
+
+var network = {
+  connection: connection
+};
+
+function onSuccessCallback(info) {
+  switch (info.networkType) {
+    case "NONE":
+      type = Connection.NONE;
+      break;
+    case "2G":
+      type = Connection.CELL_2G;
+      break;
+    case "2.5G":
+      // TODO consider. In cordova documentation there is no information about 2.5G
+      // so instead 2G is returned
+      type = Connection.CELL_2G;
+      break;
+    case "3G":
+      type = Connection.CELL_3G;
+      break;
+    case "4G":
+      type = Connection.CELL_4G;
+      break;
+    case "WIFI":
+      type = Connection.WIFI;
+      break;
+    case "ETHERNET":
+      type = Connection.ETHERNET;
+      break;
+    default:
+      type = Connection.UNKNOWN;
+      break;
+  }
+
+  if (Connection.NONE === type || Connection.UNKNOWN === type) {
+    document.dispatchEvent(new Event("offline"));
+  } else {
+    document.dispatchEvent(new Event("online"));
+  }
+}
+
+function onErrorCallback(e) {
+  type = Connection.UNKNOWN;
+  console.error("Error appeared " + e.name + ", message "+ e.message);
+}
+
+tizen.systeminfo.getPropertyValue("NETWORK", onSuccessCallback, onErrorCallback);
+// TODO currently method addPropertyValueChangeListener is registered only to SIM1 and
+// ignore changes on SIM2 (if device has dual SIM standby mode). Consider to use
+// method addPropertyValueChangeListenerArray to get information from both SIM, but
+// which type should be returned then?
+tizen.systeminfo.addPropertyValueChangeListener("NETWORK", onSuccessCallback, onErrorCallback);
+
+_global.Connection = Connection;
+_global.navigator.connection = connection;
+_global.navigator.network = network;
+
+console.log('Loaded cordova.networkinformation API');
diff --git a/src/networkinformation/cordova_networkinformation_extension.cc b/src/networkinformation/cordova_networkinformation_extension.cc
new file mode 100644 (file)
index 0000000..fdcec2e
--- /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 "networkinformation/cordova_networkinformation_extension.h"
+
+// This will be generated from cordova_networkinformation_api.js
+extern const char kSource_cordova_networkinformation_api[];
+
+common::Extension* CreateExtension() {
+  return new extension::cordova::networkinformation::CordovaNetworkInformationExtension();
+}
+
+namespace extension {
+namespace cordova {
+namespace networkinformation {
+
+CordovaNetworkInformationExtension::CordovaNetworkInformationExtension() {
+  SetExtensionName("tizen.cordova.networkinformation");
+  SetJavaScriptAPI(kSource_cordova_networkinformation_api);
+}
+
+CordovaNetworkInformationExtension::~CordovaNetworkInformationExtension() {}
+
+}  // networkinformation
+}  // cordova
+}  // extension
diff --git a/src/networkinformation/cordova_networkinformation_extension.h b/src/networkinformation/cordova_networkinformation_extension.h
new file mode 100644 (file)
index 0000000..3168b57
--- /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 NETWORKINFORMATION_CORDOVA_NETWORKINFORMATION_EXTENSION_H_
+#define NETWORKINFORMATION_CORDOVA_NETWORKINFORMATION_EXTENSION_H_
+
+#include <common/extension.h>
+
+namespace extension {
+namespace cordova {
+namespace networkinformation {
+
+class CordovaNetworkInformationExtension : public common::Extension {
+ public:
+  CordovaNetworkInformationExtension();
+  virtual ~CordovaNetworkInformationExtension();
+};
+
+}  // networkinformation
+}  // cordova
+}  // extension
+
+#endif // NETWORKINFORMATION_CORDOVA_NETWORKINFORMATION_EXTENSION_H_