From 24f979394f9c2d4070aafceba662e55eb9faf7fb Mon Sep 17 00:00:00 2001 From: macdonst Date: Tue, 12 Jul 2011 11:26:12 +0800 Subject: [PATCH] Remove deprecated Network.isReachable (use the Network Information API instead) --- example/main.js | 37 +++---- framework/assets/js/network.js | 52 +-------- .../src/com/phonegap/NetworkManager.java | 101 ++---------------- 3 files changed, 24 insertions(+), 166 deletions(-) diff --git a/example/main.js b/example/main.js index ae447aa9..110d3089 100644 --- a/example/main.js +++ b/example/main.js @@ -88,16 +88,6 @@ function close() { viewport.style.display = "none"; } -// This is just to do this. -function readFile() { - navigator.file.read('/sdcard/phonegap.txt', fail, fail); -} - -function writeFile() { - navigator.file.write('foo.txt', "This is a test of writing to a file", - fail, fail); -} - function contacts_success(contacts) { alert(contacts.length + ' contacts returned.' @@ -109,27 +99,24 @@ function get_contacts() { var obj = new ContactFindOptions(); obj.filter = ""; obj.multiple = true; - obj.limit = 5; navigator.service.contacts.find( [ "displayName", "name" ], contacts_success, fail, obj); } -var networkReachableCallback = function(reachability) { - // There is no consistency on the format of reachability - var networkState = reachability.code || reachability; - - var currentState = {}; - currentState[NetworkStatus.NOT_REACHABLE] = 'No network connection'; - currentState[NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK] = 'Carrier data connection'; - currentState[NetworkStatus.REACHABLE_VIA_WIFI_NETWORK] = 'WiFi connection'; - - confirm("Connection type:\n" + currentState[networkState]); -}; - function check_network() { - navigator.network.isReachable("www.mobiledevelopersolutions.com", - networkReachableCallback, {}); + var networkState = navigator.network.connection.type; + + var states = {}; + states[Connection.UNKNOWN] = 'Unknown connection'; + states[Connection.ETHERNET] = 'Ethernet connection'; + states[Connection.WIFI] = 'WiFi connection'; + states[Connection.CELL_2G] = 'Cell 2G connection'; + states[Connection.CELL_3G] = 'Cell 3G connection'; + states[Connection.CELL_4G] = 'Cell 4G connection'; + states[Connection.NONE] = 'No network connection'; + + confirm('Connection type:\n ' + states[networkState]); } function init() { diff --git a/framework/assets/js/network.js b/framework/assets/js/network.js index be94402b..b17a3434 100755 --- a/framework/assets/js/network.js +++ b/framework/assets/js/network.js @@ -9,56 +9,6 @@ if (!PhoneGap.hasResource("network")) { PhoneGap.addResource("network"); -/** - * This class contains information about any NetworkStatus. - * @constructor - */ -var NetworkStatus = function() { - //this.code = null; - //this.message = ""; -}; - -NetworkStatus.NOT_REACHABLE = 0; -NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK = 1; -NetworkStatus.REACHABLE_VIA_WIFI_NETWORK = 2; - -/** - * This class provides access to device Network data (reachability). - * @constructor - */ -var Network = function() { - /** - * The last known Network status. - * { hostName: string, ipAddress: string, - remoteHostStatus: int(0/1/2), internetConnectionStatus: int(0/1/2), localWiFiConnectionStatus: int (0/2) } - */ - this.lastReachability = null; -}; - -/** - * 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} callback - * @param {Object} options (isIpAddress:boolean) - */ -Network.prototype.isReachable = function(uri, callback, options) { - var isIpAddress = false; - if (options && options.isIpAddress) { - isIpAddress = options.isIpAddress; - } - PhoneGap.exec(callback, null, "Network Status", "isReachable", [uri, isIpAddress]); -}; - /** * This class contains information about the current network Connection. * @constructor @@ -123,7 +73,7 @@ Connection.prototype.getInfo = function(successCallback, errorCallback) { PhoneGap.addConstructor(function() { if (typeof navigator.network === "undefined") { - navigator.network = new Network(); + navigator.network = new Object(); } if (typeof navigator.network.connection === "undefined") { navigator.network.connection = new Connection(); diff --git a/framework/src/com/phonegap/NetworkManager.java b/framework/src/com/phonegap/NetworkManager.java index 62140f31..2d5ceaed 100755 --- a/framework/src/com/phonegap/NetworkManager.java +++ b/framework/src/com/phonegap/NetworkManager.java @@ -7,10 +7,7 @@ */ package com.phonegap; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; -import org.json.JSONException; import com.phonegap.api.PhonegapActivity; import com.phonegap.api.Plugin; @@ -102,32 +99,18 @@ public class NetworkManager extends Plugin { * @return A PluginResult object with a status and message. */ public PluginResult execute(String action, JSONArray args, String callbackId) { - PluginResult.Status status = PluginResult.Status.OK; - String result = ""; - try { - if (action.equals("isAvailable")) { - boolean b = this.isAvailable(); - return new PluginResult(status, b); - } - else if (action.equals("isWifiActive")) { - boolean b = this.isWifiActive(); - return new PluginResult(status, b); - } - else if (action.equals("isReachable")) { - int i = this.isReachable(args.getString(0), args.getBoolean(1)); - return new PluginResult(status, i); - } - else if (action.equals("getConnectionInfo")) { - this.connectionCallbackId = callbackId; - NetworkInfo info = sockMan.getActiveNetworkInfo(); - PluginResult pluginResult = new PluginResult(status, this.getConnectionInfo(info)); - pluginResult.setKeepCallback(true); - return pluginResult; - } - return new PluginResult(status, result); - } catch (JSONException e) { - return new PluginResult(PluginResult.Status.JSON_EXCEPTION); + PluginResult.Status status = PluginResult.Status.INVALID_ACTION; + String result = "Unsupported Operation: " + action; + + if (action.equals("getConnectionInfo")) { + this.connectionCallbackId = callbackId; + NetworkInfo info = sockMan.getActiveNetworkInfo(); + PluginResult pluginResult = new PluginResult(status, this.getConnectionInfo(info)); + pluginResult.setKeepCallback(true); + return pluginResult; } + + return new PluginResult(status, result); } /** @@ -236,66 +219,4 @@ public class NetworkManager extends Plugin { } return TYPE_UNKNOWN; } - - /** - * Determine if a network connection exists. - * - * @return - */ - public boolean isAvailable() { - NetworkInfo info = sockMan.getActiveNetworkInfo(); - boolean conn = false; - if (info != null) { - conn = info.isConnected(); - } - return conn; - } - - /** - * Determine if a WIFI connection exists. - * - * @return - */ - public boolean isWifiActive() { - NetworkInfo info = sockMan.getActiveNetworkInfo(); - if (info != null) { - String type = info.getTypeName(); - return type.equals("WIFI"); - } - return false; - } - - /** - * 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.indexOf("https://") == -1) { - uri = "http://" + uri; - } - - if (this.isAvailable()) { - try { - DefaultHttpClient httpclient = new DefaultHttpClient(); - HttpGet httpget = new HttpGet(uri); - httpclient.execute(httpget); - - if (this.isWifiActive()) { - reachable = REACHABLE_VIA_WIFI_NETWORK; - } - else { - reachable = REACHABLE_VIA_CARRIER_DATA_NETWORK; - } - } catch (Exception e) { - reachable = NOT_REACHABLE; - } - } - - return reachable; - } }