forked from github/cordova-android
Modify network queries to use async plugin.
This commit is contained in:
parent
0a7762743e
commit
d72c77d6f3
@ -1,16 +1,3 @@
|
|||||||
com.phonegap.NetworkManagerProxy = function() {
|
|
||||||
this.className = "com.phonegap.NetworkManager";
|
|
||||||
};
|
|
||||||
com.phonegap.NetworkManagerProxy.prototype.isAvailable = function() {
|
|
||||||
return PhoneGap.exec(this.className, "isAvailable", []);
|
|
||||||
};
|
|
||||||
com.phonegap.NetworkManagerProxy.prototype.isWifiActive = function() {
|
|
||||||
return PhoneGap.exec(this.className, "isWifiActive", []);
|
|
||||||
};
|
|
||||||
com.phonegap.NetworkManagerProxy.prototype.isReachable = function(uri) {
|
|
||||||
return PhoneGap.exec(this.className, "isReachable", [uri]);
|
|
||||||
};
|
|
||||||
com.phonegap.NetworkManager = new com.phonegap.NetworkManagerProxy();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class contains information about any NetworkStatus.
|
* This class contains information about any NetworkStatus.
|
||||||
@ -42,32 +29,54 @@ function Network() {
|
|||||||
* Called by the geolocation framework when the reachability status has changed.
|
* Called by the geolocation framework when the reachability status has changed.
|
||||||
* @param {Reachibility} reachability The current reachability status.
|
* @param {Reachibility} reachability The current reachability status.
|
||||||
*/
|
*/
|
||||||
|
// TODO: Callback from native code not implemented for Android
|
||||||
Network.prototype.updateReachability = function(reachability) {
|
Network.prototype.updateReachability = function(reachability) {
|
||||||
this.lastReachability = reachability;
|
this.lastReachability = reachability;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Determine if a URI is reachable over the network.
|
||||||
|
|
||||||
* @param {Object} uri
|
* @param {Object} uri
|
||||||
* @param {Function} win
|
* @param {Function} callback
|
||||||
* @param {Object} options (isIpAddress:boolean)
|
* @param {Object} options (isIpAddress:boolean)
|
||||||
*/
|
*/
|
||||||
Network.prototype.isReachable = function(uri, win, options) {
|
Network.prototype.isReachable = function(uri, callback, options) {
|
||||||
var status = new NetworkStatus();
|
|
||||||
if(com.phonegap.NetworkManager.isReachable(uri)) {
|
// callback required
|
||||||
if (com.phonegap.NetworkManager.isWifiActive()) {
|
if (typeof callback != "function") {
|
||||||
status.code = NetworkStatus.REACHABLE_VIA_WIFI_NETWORK;
|
console.log("Network Error: callback is not a function");
|
||||||
}
|
return;
|
||||||
else {
|
|
||||||
status.code = NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
status.code = NetworkStatus.NOT_REACHABLE;
|
PhoneGap.execAsync(
|
||||||
}
|
function(status) {
|
||||||
win(status);
|
|
||||||
|
// If reachable, the check for wifi vs carrier
|
||||||
|
if (status) {
|
||||||
|
PhoneGap.execAsync(
|
||||||
|
function(wifi) {
|
||||||
|
var s = new NetworkStatus();
|
||||||
|
if (wifi) {
|
||||||
|
s.code = NetworkStatus.REACHABLE_VIA_WIFI_NETWORK;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
s.code = NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK;
|
||||||
|
}
|
||||||
|
callback(s);
|
||||||
|
}, null, "Network Status", "isWifiActive", []);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If not
|
||||||
|
else {
|
||||||
|
var s = new NetworkStatus();
|
||||||
|
s.code = NetworkStatus.NOT_REACHABLE;
|
||||||
|
callback(s);
|
||||||
|
}
|
||||||
|
}, null, "Network Status", "isReachable", [uri]);
|
||||||
};
|
};
|
||||||
|
|
||||||
PhoneGap.addConstructor(function() {
|
PhoneGap.addConstructor(function() {
|
||||||
if (typeof navigator.network == "undefined") navigator.network = new Network();
|
if (typeof navigator.network == "undefined") navigator.network = new Network();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
7
framework/src/com/phonegap/NetworkManager.java
Normal file → Executable file
7
framework/src/com/phonegap/NetworkManager.java
Normal file → Executable file
@ -76,7 +76,14 @@ public class NetworkManager implements Plugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identifies if action to be executed returns a value.
|
||||||
|
*
|
||||||
|
* @param action The action to execute
|
||||||
|
* @return T=returns value
|
||||||
|
*/
|
||||||
public boolean hasReturnValue(String action) {
|
public boolean hasReturnValue(String action) {
|
||||||
|
// All methods take a while, so always use async
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user