2010-10-19 04:31:16 +08:00
|
|
|
/*
|
|
|
|
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
|
|
|
|
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2010, Nitobi Software Inc.
|
2011-05-21 01:50:20 +08:00
|
|
|
* Copyright (c) 2010-2011, IBM Corporation
|
2010-10-19 04:31:16 +08:00
|
|
|
*/
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2011-03-31 02:29:24 +08:00
|
|
|
if (!PhoneGap.hasResource("network")) {
|
|
|
|
PhoneGap.addResource("network");
|
|
|
|
|
2009-11-18 02:38:49 +08:00
|
|
|
/**
|
|
|
|
* This class contains information about any NetworkStatus.
|
|
|
|
* @constructor
|
|
|
|
*/
|
2011-05-21 01:50:20 +08:00
|
|
|
var NetworkStatus = function() {
|
2010-09-21 11:25:57 +08:00
|
|
|
//this.code = null;
|
|
|
|
//this.message = "";
|
2011-03-31 02:29:24 +08:00
|
|
|
};
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2009-11-18 02:38:49 +08:00
|
|
|
NetworkStatus.NOT_REACHABLE = 0;
|
|
|
|
NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK = 1;
|
|
|
|
NetworkStatus.REACHABLE_VIA_WIFI_NETWORK = 2;
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2009-11-18 02:38:49 +08:00
|
|
|
/**
|
|
|
|
* This class provides access to device Network data (reachability).
|
|
|
|
* @constructor
|
|
|
|
*/
|
2011-05-21 01:50:20 +08:00
|
|
|
var Network = function() {
|
2009-11-18 02:38:49 +08:00
|
|
|
/**
|
|
|
|
* The last known Network status.
|
2010-10-21 12:53:33 +08:00
|
|
|
* { hostName: string, ipAddress: string,
|
|
|
|
remoteHostStatus: int(0/1/2), internetConnectionStatus: int(0/1/2), localWiFiConnectionStatus: int (0/2) }
|
2009-11-18 02:38:49 +08:00
|
|
|
*/
|
2010-10-21 12:53:33 +08:00
|
|
|
this.lastReachability = null;
|
2011-03-31 02:29:24 +08:00
|
|
|
};
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2009-11-18 02:38:49 +08:00
|
|
|
/**
|
|
|
|
* Called by the geolocation framework when the reachability status has changed.
|
|
|
|
* @param {Reachibility} reachability The current reachability status.
|
|
|
|
*/
|
2010-09-11 03:20:34 +08:00
|
|
|
// TODO: Callback from native code not implemented for Android
|
2009-11-18 02:38:49 +08:00
|
|
|
Network.prototype.updateReachability = function(reachability) {
|
|
|
|
this.lastReachability = reachability;
|
|
|
|
};
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2010-02-24 03:07:30 +08:00
|
|
|
/**
|
2010-09-11 03:20:34 +08:00
|
|
|
* Determine if a URI is reachable over the network.
|
|
|
|
|
2010-02-24 03:07:30 +08:00
|
|
|
* @param {Object} uri
|
2010-09-11 03:20:34 +08:00
|
|
|
* @param {Function} callback
|
2010-02-24 03:07:30 +08:00
|
|
|
* @param {Object} options (isIpAddress:boolean)
|
|
|
|
*/
|
2010-09-11 03:20:34 +08:00
|
|
|
Network.prototype.isReachable = function(uri, callback, options) {
|
2010-09-21 11:25:57 +08:00
|
|
|
var isIpAddress = false;
|
|
|
|
if (options && options.isIpAddress) {
|
|
|
|
isIpAddress = options.isIpAddress;
|
2010-09-07 02:13:09 +08:00
|
|
|
}
|
2010-10-21 12:53:33 +08:00
|
|
|
PhoneGap.exec(callback, null, "Network Status", "isReachable", [uri, isIpAddress]);
|
2010-02-24 03:07:30 +08:00
|
|
|
};
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2011-04-26 06:15:17 +08:00
|
|
|
/**
|
|
|
|
* This class contains information about the current network Connection.
|
|
|
|
* @constructor
|
|
|
|
*/
|
2011-05-21 01:50:20 +08:00
|
|
|
var Connection = function() {
|
2011-04-26 06:15:17 +08:00
|
|
|
this.type = null;
|
2011-06-03 01:11:51 +08:00
|
|
|
this.networkName = null;
|
|
|
|
this._firstRun = true;
|
|
|
|
this._timer = null;
|
|
|
|
this.timeout = 500;
|
2011-04-26 06:15:17 +08:00
|
|
|
|
|
|
|
var me = this;
|
|
|
|
this.getInfo(
|
|
|
|
function(info) {
|
2011-06-03 01:11:51 +08:00
|
|
|
// Need to send events if we are on or offline
|
|
|
|
if (info.type == "none") {
|
|
|
|
// set a timer if still offline at the end of timer send the offline event
|
|
|
|
me._timer = setTimeout(function(){
|
|
|
|
me.type = info.type;
|
|
|
|
me.networkName = info.networkName;
|
|
|
|
PhoneGap.fireEvent('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 = info.type;
|
|
|
|
me.networkName = info.networkName;
|
|
|
|
PhoneGap.fireEvent('online');
|
|
|
|
}
|
|
|
|
|
|
|
|
// should only fire this once
|
|
|
|
if (me._firstRun) {
|
|
|
|
me._firstRun = false;
|
|
|
|
PhoneGap.onPhoneGapConnectionReady.fire();
|
|
|
|
}
|
2011-04-26 06:15:17 +08:00
|
|
|
},
|
|
|
|
function(e) {
|
|
|
|
console.log("Error initializing Network Connection: " + e);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-06-03 01:11:51 +08:00
|
|
|
Connection.UNKNOWN = "unknown";
|
|
|
|
Connection.ETHERNET = "ethernet";
|
|
|
|
Connection.WIFI = "wifi";
|
|
|
|
Connection.CELL_2G = "2g";
|
|
|
|
Connection.CELL_3G = "3g";
|
|
|
|
Connection.CELL_4G = "4g";
|
|
|
|
Connection.NONE = "none";
|
2011-04-26 06:15:17 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
PhoneGap.exec(successCallback, errorCallback, "Network Status", "getConnectionInfo", []);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-02-24 03:07:30 +08:00
|
|
|
PhoneGap.addConstructor(function() {
|
2011-02-03 00:46:19 +08:00
|
|
|
if (typeof navigator.network === "undefined") {
|
|
|
|
navigator.network = new Network();
|
|
|
|
}
|
2011-05-18 03:02:45 +08:00
|
|
|
if (typeof navigator.network.connection === "undefined") {
|
|
|
|
navigator.network.connection = new Connection();
|
2011-04-26 06:15:17 +08:00
|
|
|
}
|
2010-09-11 03:20:34 +08:00
|
|
|
});
|
2011-05-21 01:50:20 +08:00
|
|
|
}
|