From 063e189bb7b7b84d72e6863f0f43918e70697960 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Mon, 20 Sep 2010 22:25:57 -0500 Subject: [PATCH] Change isReachable() to return NetworkStatus constant to reachableCallback(reachability) as specified in the API documentation. --- framework/assets/js/network.js | 39 ++----------- .../src/com/phonegap/NetworkManager.java | 56 +++++++++++++++---- 2 files changed, 51 insertions(+), 44 deletions(-) diff --git a/framework/assets/js/network.js b/framework/assets/js/network.js index 720b8529..b3fb1f40 100755 --- a/framework/assets/js/network.js +++ b/framework/assets/js/network.js @@ -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() { diff --git a/framework/src/com/phonegap/NetworkManager.java b/framework/src/com/phonegap/NetworkManager.java index caf6efba..82c75fe5 100755 --- a/framework/src/com/phonegap/NetworkManager.java +++ b/framework/src/com/phonegap/NetworkManager.java @@ -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; } }