blob: 3ab93370c3e1e3998032244b92d6129de501ad0d [file] [log] [blame]
/*
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.
*/
if (!Cordova.hasResource("network")) {
Cordova.addResource("network");
/**
* This class contains information about the current network Connection.
* @constructor
*/
var Connection = function()
{
this.type = null;
this._firstRun = true;
this._timer = null;
this.timeout = 500;
var me = this;
this.getInfo(
function(type) {
//console.log("getInfo result" + type);
// Need to send events if we are on or offline
if (type == "none") {
// set a timer if still offline at the end of timer send the offline event
me._timer = setTimeout(function(){
me.type = type;
//console.log("Cordova.fireEvent::offline");
Cordova.fireEvent(document,'offline');
me._timer = null;
}, me.timeout);
} else {
// If there is a current offline event pending clear it
if (me._timer != null) {
clearTimeout(me._timer);
me._timer = null;
}
me.type = type;
//console.log("Cordova.fireEvent::online " + me.type);
Cordova.fireEvent(document,'online');
}
// should only fire this once
if (me._firstRun)
{
me._firstRun = false;
//console.log("onCordovaConnectionReady");
Cordova.onCordovaConnectionReady.fire();
}
},
function(e) {
console.log("Error initializing Network Connection: " + e);
});
};
Connection.UNKNOWN = "unknown";
Connection.ETHERNET = "ethernet";
Connection.WIFI = "wifi";
Connection.CELL_2G = "2g";
Connection.CELL_3G = "3g";
Connection.CELL_4G = "4g";
Connection.NONE = "none";
Connection.CELL = "cellular";
/**
* 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)
*/
Connection.prototype.getInfo = function(successCallback, errorCallback) {
// Get info
Cordova.exec(successCallback, errorCallback, "Connection", "getConnectionInfo", []);
};
Cordova.onCordovaInit.subscribeOnce(function() {
navigator.network = navigator.network || {};
if (typeof navigator.network.connection === "undefined") {
navigator.network.connection = new Connection();
}
});
}