2010-10-19 04:31:16 +08:00
|
|
|
/*
|
2011-10-28 05:04:39 +08:00
|
|
|
* 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
|
2010-10-19 04:31:16 +08:00
|
|
|
*
|
2011-10-28 05:04:39 +08:00
|
|
|
* 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.
|
2010-10-19 04:31:16 +08:00
|
|
|
*/
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2011-10-28 05:04:39 +08:00
|
|
|
|
2011-03-31 02:29:24 +08:00
|
|
|
if (!PhoneGap.hasResource("network")) {
|
|
|
|
PhoneGap.addResource("network");
|
|
|
|
|
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._firstRun = true;
|
|
|
|
this._timer = null;
|
|
|
|
this.timeout = 500;
|
2011-04-26 06:15:17 +08:00
|
|
|
|
|
|
|
var me = this;
|
|
|
|
this.getInfo(
|
2011-06-09 03:03:03 +08:00
|
|
|
function(type) {
|
2011-06-03 01:11:51 +08:00
|
|
|
// Need to send events if we are on or offline
|
2011-11-08 06:54:15 +08:00
|
|
|
if (type === "none") {
|
2011-06-03 01:11:51 +08:00
|
|
|
// set a timer if still offline at the end of timer send the offline event
|
|
|
|
me._timer = setTimeout(function(){
|
2011-06-09 03:03:03 +08:00
|
|
|
me.type = type;
|
2011-08-12 03:49:51 +08:00
|
|
|
PhoneGap.fireDocumentEvent('offline');
|
2011-06-03 01:11:51 +08:00
|
|
|
me._timer = null;
|
|
|
|
}, me.timeout);
|
|
|
|
} else {
|
|
|
|
// If there is a current offline event pending clear it
|
2011-11-08 06:54:15 +08:00
|
|
|
if (me._timer !== null) {
|
2011-06-03 01:11:51 +08:00
|
|
|
clearTimeout(me._timer);
|
|
|
|
me._timer = null;
|
|
|
|
}
|
2011-06-09 03:03:03 +08:00
|
|
|
me.type = type;
|
2011-08-12 03:49:51 +08:00
|
|
|
PhoneGap.fireDocumentEvent('online');
|
2011-06-03 01:11:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// should only fire this once
|
|
|
|
if (me._firstRun) {
|
|
|
|
me._firstRun = false;
|
|
|
|
PhoneGap.onPhoneGapConnectionReady.fire();
|
|
|
|
}
|
2011-04-26 06:15:17 +08:00
|
|
|
},
|
|
|
|
function(e) {
|
2011-08-27 04:04:57 +08:00
|
|
|
// If we can't get the network info we should still tell PhoneGap
|
|
|
|
// to fire the deviceready event.
|
|
|
|
if (me._firstRun) {
|
|
|
|
me._firstRun = false;
|
|
|
|
PhoneGap.onPhoneGapConnectionReady.fire();
|
|
|
|
}
|
2011-04-26 06:15:17 +08:00
|
|
|
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") {
|
2011-11-08 06:54:15 +08:00
|
|
|
navigator.network = {};
|
2011-02-03 00:46:19 +08:00
|
|
|
}
|
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
|
|
|
}
|