[NetworkInformation] Adapted to cordova plugin model.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Fri, 30 Oct 2015 14:42:02 +0000 (15:42 +0100)
committerHyunJin Park <hj.na.park@samsung.com>
Tue, 3 Nov 2015 02:22:16 +0000 (11:22 +0900)
[Verification] All tests pass.

Change-Id: I07a1d51e585cc4808c1225d22c707e4892a19b91
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/lib/cordova_plugins.js
src/lib/plugins/cordova-plugin-network-information/www/Connection.js [new file with mode: 0644]
src/lib/plugins/cordova-plugin-network-information/www/network.js [new file with mode: 0644]
src/networkinformation/cordova_networkinformation_api.js

index 3192c61..1ed826b 100644 (file)
@@ -166,6 +166,21 @@ module.exports = [
             "window.FileTransfer"
         ]
     },
+    {
+        "file": "plugins/cordova-plugin-network-information/www/network.js",
+        "id": "cordova-plugin-network-information.network",
+        "clobbers": [
+            "navigator.connection",
+            "navigator.network.connection"
+        ]
+    },
+    {
+        "file": "plugins/cordova-plugin-network-information/www/Connection.js",
+        "id": "cordova-plugin-network-information.Connection",
+        "clobbers": [
+            "Connection"
+        ]
+    },
 ];
 module.exports.metadata =
 // TOP OF METADATA
@@ -174,6 +189,7 @@ module.exports.metadata =
     "cordova-plugin-dialogs": "1.1.1",
     "cordova-plugin-file": "3.0.0",
     "cordova-plugin-file-transfer": "1.3.0",
+    "cordova-plugin-network-information": "1.0.1",
 }
 // BOTTOM OF METADATA
 });
diff --git a/src/lib/plugins/cordova-plugin-network-information/www/Connection.js b/src/lib/plugins/cordova-plugin-network-information/www/Connection.js
new file mode 100644 (file)
index 0000000..1450e95
--- /dev/null
@@ -0,0 +1,36 @@
+cordova.define("cordova-plugin-network-information.Connection", function(require, exports, module) { /*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ *
+*/
+
+/**
+ * Network status
+ */
+module.exports = {
+        UNKNOWN: "unknown",
+        ETHERNET: "ethernet",
+        WIFI: "wifi",
+        CELL_2G: "2g",
+        CELL_3G: "3g",
+        CELL_4G: "4g",
+        CELL:"cellular",
+        NONE: "none"
+};
+
+});
diff --git a/src/lib/plugins/cordova-plugin-network-information/www/network.js b/src/lib/plugins/cordova-plugin-network-information/www/network.js
new file mode 100644 (file)
index 0000000..bfac2e3
--- /dev/null
@@ -0,0 +1,93 @@
+cordova.define("cordova-plugin-network-information.network", function(require, exports, module) { /*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 exec = require('cordova/exec'),
+    cordova = require('cordova'),
+    channel = require('cordova/channel'),
+    utils = require('cordova/utils');
+
+// Link the onLine property with the Cordova-supplied network info.
+// This works because we clobber the navigator object with our own
+// object in bootstrap.js.
+// Browser platform do not need to define this property, because
+// it is already supported by modern browsers
+if (cordova.platformId !== 'browser' && typeof navigator != 'undefined') {
+    utils.defineGetter(navigator, 'onLine', function() {
+        return this.connection.type != 'none';
+    });
+}
+
+function NetworkConnection() {
+    this.type = 'unknown';
+}
+
+/**
+ * Get connection info
+ *
+ * @param {Function} successCallback The function to call when the Connection data is available
+ * @param {Function} errorCallback The function to call when there is an error getting the Connection data. (OPTIONAL)
+ */
+NetworkConnection.prototype.getInfo = function(successCallback, errorCallback) {
+    exec(successCallback, errorCallback, "NetworkStatus", "getConnectionInfo", []);
+};
+
+var me = new NetworkConnection();
+var timerId = null;
+var timeout = 500;
+
+channel.createSticky('onCordovaConnectionReady');
+channel.waitForInitialization('onCordovaConnectionReady');
+
+channel.onCordovaReady.subscribe(function() {
+    me.getInfo(function(info) {
+        me.type = info;
+        if (info === "none") {
+            // set a timer if still offline at the end of timer send the offline event
+            timerId = setTimeout(function(){
+                cordova.fireDocumentEvent("offline");
+                timerId = null;
+            }, timeout);
+        } else {
+            // If there is a current offline event pending clear it
+            if (timerId !== null) {
+                clearTimeout(timerId);
+                timerId = null;
+            }
+            cordova.fireDocumentEvent("online");
+        }
+
+        // should only fire this once
+        if (channel.onCordovaConnectionReady.state !== 2) {
+            channel.onCordovaConnectionReady.fire();
+        }
+    },
+    function (e) {
+        // If we can't get the network info we should still tell Cordova
+        // to fire the deviceready event.
+        if (channel.onCordovaConnectionReady.state !== 2) {
+            channel.onCordovaConnectionReady.fire();
+        }
+        console.log("Error initializing Network Connection: " + e);
+    });
+});
+
+module.exports = me;
+
+});
index 660d226..41cb857 100644 (file)
  *    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"
-};
+// TODO: remove when added to public cordova repository -> begin
+var plugin_name = 'cordova-plugin-file.tizen.NetworkStatus';
 
-var type = Connection.UNKNOWN;
+cordova.define(plugin_name, function(require, exports, module) {
+// TODO: remove -> end
 
-var connection = {};
+function toCordovaConnectionType(type) {
+  switch (type) {
+    case 'NONE':
+      return Connection.NONE;
 
-Object.defineProperty(connection, 'type', {
-  get: function() { return type; },
-  set: function() {},
-  enumerable: true
-});
+    case '2G':
+      return Connection.CELL_2G;
 
-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
+    case '2.5G':
+      // TODO: 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;
+      return Connection.CELL_2G;
+
+    case '3G':
+      return Connection.CELL_3G;
+
+    case '4G':
+      return Connection.CELL_4G;
+
+    case 'WIFI':
+      return Connection.WIFI;
+
+    case 'ETHERNET':
+      return Connection.ETHERNET;
+
     default:
-      type = Connection.UNKNOWN;
-      break;
+      return Connection.UNKNOWN;
   }
+}
+
+function onSuccessCallback(info) {
+  var type = toCordovaConnectionType(info.networkType);
 
   if (Connection.NONE === type || Connection.UNKNOWN === type) {
-    document.dispatchEvent(new Event("offline"));
+    document.dispatchEvent(new Event('offline'));
   } else {
-    document.dispatchEvent(new Event("online"));
+    document.dispatchEvent(new Event('online'));
   }
 }
 
 function onErrorCallback(e) {
-  type = Connection.UNKNOWN;
-  console.error("Error appeared " + e.name + ", message "+ e.message);
+  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);
+tizen.systeminfo.addPropertyValueChangeListener('NETWORK', onSuccessCallback, onErrorCallback);
 
-_global.Connection = Connection;
-_global.navigator.connection = connection;
-_global.navigator.network = network;
+exports = {
+  getConnectionInfo: function(successCallback, errorCallback, args) {
+    tizen.systeminfo.getPropertyValue('NETWORK', function(info) {
+      successCallback(toCordovaConnectionType(info.networkType));
+    }, errorCallback);
+  }
+};
+
+require('cordova/exec/proxy').add('NetworkStatus', exports);
 
 console.log('Loaded cordova.networkinformation API');
 
+//TODO: remove when added to public cordova repository -> begin
+});
+
 exports = function(require) {
+  // this plugin is not loaded via cordova_plugins.js, we need to manually add
+  // it to module mapper
+  var mm = require('cordova/modulemapper');
+  mm.runs(plugin_name);
 };
+//TODO: remove -> end