mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-20 23:56:20 +08:00
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.
|
||||
@ -42,32 +29,54 @@ function Network() {
|
||||
* Called by the geolocation framework when the reachability status has changed.
|
||||
* @param {Reachibility} reachability The current reachability status.
|
||||
*/
|
||||
// TODO: Callback from native code not implemented for Android
|
||||
Network.prototype.updateReachability = function(reachability) {
|
||||
this.lastReachability = reachability;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* Determine if a URI is reachable over the network.
|
||||
|
||||
* @param {Object} uri
|
||||
* @param {Function} win
|
||||
* @param {Function} callback
|
||||
* @param {Object} options (isIpAddress:boolean)
|
||||
*/
|
||||
Network.prototype.isReachable = function(uri, win, options) {
|
||||
var status = new NetworkStatus();
|
||||
if(com.phonegap.NetworkManager.isReachable(uri)) {
|
||||
if (com.phonegap.NetworkManager.isWifiActive()) {
|
||||
status.code = NetworkStatus.REACHABLE_VIA_WIFI_NETWORK;
|
||||
}
|
||||
else {
|
||||
status.code = NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK;
|
||||
}
|
||||
Network.prototype.isReachable = function(uri, callback, options) {
|
||||
|
||||
// callback required
|
||||
if (typeof callback != "function") {
|
||||
console.log("Network Error: callback is not a function");
|
||||
return;
|
||||
}
|
||||
else {
|
||||
status.code = NetworkStatus.NOT_REACHABLE;
|
||||
}
|
||||
win(status);
|
||||
|
||||
PhoneGap.execAsync(
|
||||
function(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() {
|
||||
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) {
|
||||
// All methods take a while, so always use async
|
||||
return false;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user