mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-19 15:12:51 +08:00
Change isReachable() to return NetworkStatus constant to reachableCallback(reachability) as specified in the API documentation.
This commit is contained in:
parent
1a9173d2c3
commit
063e189bb7
@ -4,8 +4,8 @@
|
||||
* @constructor
|
||||
*/
|
||||
function NetworkStatus() {
|
||||
this.code = null;
|
||||
this.message = "";
|
||||
//this.code = null;
|
||||
//this.message = "";
|
||||
};
|
||||
|
||||
NetworkStatus.NOT_REACHABLE = 0;
|
||||
@ -42,38 +42,11 @@ Network.prototype.updateReachability = function(reachability) {
|
||||
* @param {Object} options (isIpAddress:boolean)
|
||||
*/
|
||||
Network.prototype.isReachable = function(uri, callback, options) {
|
||||
|
||||
// callback required
|
||||
if (typeof callback != "function") {
|
||||
console.log("Network Error: callback is not a function");
|
||||
return;
|
||||
var isIpAddress = false;
|
||||
if (options && options.isIpAddress) {
|
||||
isIpAddress = options.isIpAddress;
|
||||
}
|
||||
|
||||
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.execAsync(callback, null, "Network Status", "isReachable", [uri, isIpAddress]);
|
||||
};
|
||||
|
||||
PhoneGap.addConstructor(function() {
|
||||
|
@ -14,6 +14,11 @@ import android.net.*;
|
||||
import android.webkit.WebView;
|
||||
|
||||
public class NetworkManager implements Plugin {
|
||||
|
||||
public static int NOT_REACHABLE = 0;
|
||||
public static int REACHABLE_VIA_CARRIER_DATA_NETWORK = 1;
|
||||
public static int REACHABLE_VIA_WIFI_NETWORK = 2;
|
||||
|
||||
|
||||
WebView webView; // WebView object
|
||||
DroidGap ctx; // DroidGap object
|
||||
@ -67,8 +72,8 @@ public class NetworkManager implements Plugin {
|
||||
return new PluginResult(status, b);
|
||||
}
|
||||
else if (action.equals("isReachable")) {
|
||||
boolean b = this.isReachable(args.getString(0));
|
||||
return new PluginResult(status, b);
|
||||
int i = this.isReachable(args.getString(0), args.getBoolean(1));
|
||||
return new PluginResult(status, i);
|
||||
}
|
||||
return new PluginResult(status, result);
|
||||
} catch (JSONException e) {
|
||||
@ -122,6 +127,11 @@ public class NetworkManager implements Plugin {
|
||||
// LOCAL METHODS
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Determine if a network connection exists.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isAvailable() {
|
||||
NetworkInfo info = sockMan.getActiveNetworkInfo();
|
||||
boolean conn = false;
|
||||
@ -131,6 +141,11 @@ public class NetworkManager implements Plugin {
|
||||
return conn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a WIFI connection exists.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isWifiActive() {
|
||||
NetworkInfo info = sockMan.getActiveNetworkInfo();
|
||||
if (info != null) {
|
||||
@ -140,18 +155,37 @@ public class NetworkManager implements Plugin {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isReachable(String uri) {
|
||||
/**
|
||||
* Determine if a URI is reachable over the network.
|
||||
*
|
||||
* @param uri
|
||||
* @param isIpAddress
|
||||
* @return
|
||||
*/
|
||||
public int isReachable(String uri, boolean isIpAddress) {
|
||||
int reachable = NOT_REACHABLE;
|
||||
|
||||
if (uri.indexOf("http://") == -1) {
|
||||
uri = "http://" + uri;
|
||||
}
|
||||
boolean reached = isAvailable();
|
||||
try {
|
||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||
HttpGet httpget = new HttpGet(uri);
|
||||
httpclient.execute(httpget);
|
||||
} catch (Exception e) {
|
||||
reached = false;
|
||||
|
||||
if (isAvailable()) {
|
||||
try {
|
||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||
HttpGet httpget = new HttpGet(uri);
|
||||
httpclient.execute(httpget);
|
||||
|
||||
if (isWifiActive()) {
|
||||
reachable = REACHABLE_VIA_WIFI_NETWORK;
|
||||
}
|
||||
else {
|
||||
reachable = REACHABLE_VIA_CARRIER_DATA_NETWORK;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
reachable = NOT_REACHABLE;
|
||||
}
|
||||
}
|
||||
return reached;
|
||||
|
||||
return reachable;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user