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
|
* @constructor
|
||||||
*/
|
*/
|
||||||
function NetworkStatus() {
|
function NetworkStatus() {
|
||||||
this.code = null;
|
//this.code = null;
|
||||||
this.message = "";
|
//this.message = "";
|
||||||
};
|
};
|
||||||
|
|
||||||
NetworkStatus.NOT_REACHABLE = 0;
|
NetworkStatus.NOT_REACHABLE = 0;
|
||||||
@ -42,38 +42,11 @@ Network.prototype.updateReachability = function(reachability) {
|
|||||||
* @param {Object} options (isIpAddress:boolean)
|
* @param {Object} options (isIpAddress:boolean)
|
||||||
*/
|
*/
|
||||||
Network.prototype.isReachable = function(uri, callback, options) {
|
Network.prototype.isReachable = function(uri, callback, options) {
|
||||||
|
var isIpAddress = false;
|
||||||
// callback required
|
if (options && options.isIpAddress) {
|
||||||
if (typeof callback != "function") {
|
isIpAddress = options.isIpAddress;
|
||||||
console.log("Network Error: callback is not a function");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
PhoneGap.execAsync(callback, null, "Network Status", "isReachable", [uri, 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.addConstructor(function() {
|
PhoneGap.addConstructor(function() {
|
||||||
|
@ -14,6 +14,11 @@ import android.net.*;
|
|||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
|
|
||||||
public class NetworkManager implements Plugin {
|
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
|
WebView webView; // WebView object
|
||||||
DroidGap ctx; // DroidGap object
|
DroidGap ctx; // DroidGap object
|
||||||
@ -67,8 +72,8 @@ public class NetworkManager implements Plugin {
|
|||||||
return new PluginResult(status, b);
|
return new PluginResult(status, b);
|
||||||
}
|
}
|
||||||
else if (action.equals("isReachable")) {
|
else if (action.equals("isReachable")) {
|
||||||
boolean b = this.isReachable(args.getString(0));
|
int i = this.isReachable(args.getString(0), args.getBoolean(1));
|
||||||
return new PluginResult(status, b);
|
return new PluginResult(status, i);
|
||||||
}
|
}
|
||||||
return new PluginResult(status, result);
|
return new PluginResult(status, result);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
@ -122,6 +127,11 @@ public class NetworkManager implements Plugin {
|
|||||||
// LOCAL METHODS
|
// LOCAL METHODS
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a network connection exists.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public boolean isAvailable() {
|
public boolean isAvailable() {
|
||||||
NetworkInfo info = sockMan.getActiveNetworkInfo();
|
NetworkInfo info = sockMan.getActiveNetworkInfo();
|
||||||
boolean conn = false;
|
boolean conn = false;
|
||||||
@ -131,6 +141,11 @@ public class NetworkManager implements Plugin {
|
|||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a WIFI connection exists.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public boolean isWifiActive() {
|
public boolean isWifiActive() {
|
||||||
NetworkInfo info = sockMan.getActiveNetworkInfo();
|
NetworkInfo info = sockMan.getActiveNetworkInfo();
|
||||||
if (info != null) {
|
if (info != null) {
|
||||||
@ -140,18 +155,37 @@ public class NetworkManager implements Plugin {
|
|||||||
return false;
|
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) {
|
if (uri.indexOf("http://") == -1) {
|
||||||
uri = "http://" + uri;
|
uri = "http://" + uri;
|
||||||
}
|
}
|
||||||
boolean reached = isAvailable();
|
|
||||||
try {
|
if (isAvailable()) {
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
try {
|
||||||
HttpGet httpget = new HttpGet(uri);
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
httpclient.execute(httpget);
|
HttpGet httpget = new HttpGet(uri);
|
||||||
} catch (Exception e) {
|
httpclient.execute(httpget);
|
||||||
reached = false;
|
|
||||||
|
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