Skip to content

Commit

Permalink
Add network detection library #69
Browse files Browse the repository at this point in the history
  • Loading branch information
bsenyk committed Feb 3, 2016
1 parent 57abd8b commit 27319d6
Show file tree
Hide file tree
Showing 10 changed files with 605 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"cordova-plugin-file-transfer",
"cordova-plugin-zip",
"cordova-plugin-app-version",
"cordova-plugin-network-information",
"com.lampa.startapp",
"cordova-plugin-customurlscheme --variable URL_SCHEME=openmentoring",
"de.appplant.cordova.plugin.local-notification"
],
Expand Down
31 changes: 30 additions & 1 deletion platforms/android/android.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
{
"xml": "<feature name=\"startApp\"><param name=\"android-package\" value=\"com.lampa.startapp.startApp\" /></feature>",
"count": 1
},
{
"xml": "<feature name=\"NetworkStatus\"><param name=\"android-package\" value=\"org.apache.cordova.networkinformation.NetworkManager\" /></feature>",
"count": 1
}
],
"/widget": [
Expand Down Expand Up @@ -107,6 +111,10 @@
{
"xml": "<uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" />",
"count": 2
},
{
"xml": "<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\" />",
"count": 1
}
],
"/manifest/application/activity": [
Expand Down Expand Up @@ -162,6 +170,9 @@
},
"com.lampa.startapp": {
"PACKAGE_NAME": "org.iilab.openmentoring"
},
"cordova-plugin-network-information": {
"PACKAGE_NAME": "org.iilab.openmentoring"
}
},
"dependent_plugins": {},
Expand Down Expand Up @@ -446,6 +457,23 @@
"merges": [
"navigator.startApp"
]
},
{
"file": "plugins/cordova-plugin-network-information/www/network.js",
"id": "cordova-plugin-network-information.network",
"pluginId": "cordova-plugin-network-information",
"clobbers": [
"navigator.connection",
"navigator.network.connection"
]
},
{
"file": "plugins/cordova-plugin-network-information/www/Connection.js",
"id": "cordova-plugin-network-information.Connection",
"pluginId": "cordova-plugin-network-information",
"clobbers": [
"Connection"
]
}
],
"plugin_metadata": {
Expand All @@ -462,6 +490,7 @@
"cordova-plugin-file-transfer": "1.5.0",
"cordova-plugin-zip": "3.0.0",
"cordova-plugin-customurlscheme": "4.1.2",
"com.lampa.startapp": "0.0.5"
"com.lampa.startapp": "0.0.5",
"cordova-plugin-network-information": "1.2.0"
}
}
20 changes: 19 additions & 1 deletion platforms/android/assets/www/cordova_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,23 @@ module.exports = [
"merges": [
"navigator.startApp"
]
},
{
"file": "plugins/cordova-plugin-network-information/www/network.js",
"id": "cordova-plugin-network-information.network",
"pluginId": "cordova-plugin-network-information",
"clobbers": [
"navigator.connection",
"navigator.network.connection"
]
},
{
"file": "plugins/cordova-plugin-network-information/www/Connection.js",
"id": "cordova-plugin-network-information.Connection",
"pluginId": "cordova-plugin-network-information",
"clobbers": [
"Connection"
]
}
];
module.exports.metadata =
Expand All @@ -298,7 +315,8 @@ module.exports.metadata =
"cordova-plugin-file-transfer": "1.5.0",
"cordova-plugin-zip": "3.0.0",
"cordova-plugin-customurlscheme": "4.1.2",
"com.lampa.startapp": "0.0.5"
"com.lampa.startapp": "0.0.5",
"cordova-plugin-network-information": "1.2.0"
}
// BOTTOM OF METADATA
});
Original file line number Diff line number Diff line change
@@ -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"
};

});
Original file line number Diff line number Diff line change
@@ -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;

});
20 changes: 19 additions & 1 deletion platforms/android/platform_www/cordova_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,23 @@ module.exports = [
"merges": [
"navigator.startApp"
]
},
{
"file": "plugins/cordova-plugin-network-information/www/network.js",
"id": "cordova-plugin-network-information.network",
"pluginId": "cordova-plugin-network-information",
"clobbers": [
"navigator.connection",
"navigator.network.connection"
]
},
{
"file": "plugins/cordova-plugin-network-information/www/Connection.js",
"id": "cordova-plugin-network-information.Connection",
"pluginId": "cordova-plugin-network-information",
"clobbers": [
"Connection"
]
}
];
module.exports.metadata =
Expand All @@ -298,7 +315,8 @@ module.exports.metadata =
"cordova-plugin-file-transfer": "1.5.0",
"cordova-plugin-zip": "3.0.0",
"cordova-plugin-customurlscheme": "4.1.2",
"com.lampa.startapp": "0.0.5"
"com.lampa.startapp": "0.0.5",
"cordova-plugin-network-information": "1.2.0"
}
// BOTTOM OF METADATA
});
Original file line number Diff line number Diff line change
@@ -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"
};

});
Loading

0 comments on commit 27319d6

Please sign in to comment.