From a67aeed5711fbd5ec6571d1d515a2221060f7ff8 Mon Sep 17 00:00:00 2001 From: macdonst Date: Thu, 9 Jun 2011 03:03:03 +0800 Subject: [PATCH] Updating Network Connection API to match spec released on June 7th --- framework/AndroidManifest.xml | 2 - framework/assets/js/network.js | 11 ++- framework/src/com/phonegap/Device.java | 32 ++------- .../src/com/phonegap/NetworkManager.java | 70 +++++-------------- 4 files changed, 27 insertions(+), 88 deletions(-) diff --git a/framework/AndroidManifest.xml b/framework/AndroidManifest.xml index 2807d65b..53d7afb5 100644 --- a/framework/AndroidManifest.xml +++ b/framework/AndroidManifest.xml @@ -14,8 +14,6 @@ - - diff --git a/framework/assets/js/network.js b/framework/assets/js/network.js index d93c95ba..be94402b 100755 --- a/framework/assets/js/network.js +++ b/framework/assets/js/network.js @@ -65,20 +65,18 @@ Network.prototype.isReachable = function(uri, callback, options) { */ var Connection = function() { this.type = null; - this.networkName = null; this._firstRun = true; this._timer = null; this.timeout = 500; var me = this; this.getInfo( - function(info) { + function(type) { // Need to send events if we are on or offline - if (info.type == "none") { + if (type == "none") { // set a timer if still offline at the end of timer send the offline event me._timer = setTimeout(function(){ - me.type = info.type; - me.networkName = info.networkName; + me.type = type; PhoneGap.fireEvent('offline'); me._timer = null; }, me.timeout); @@ -88,8 +86,7 @@ var Connection = function() { clearTimeout(me._timer); me._timer = null; } - me.type = info.type; - me.networkName = info.networkName; + me.type = type; PhoneGap.fireEvent('online'); } diff --git a/framework/src/com/phonegap/Device.java b/framework/src/com/phonegap/Device.java index ac0a8ecc..035058fe 100755 --- a/framework/src/com/phonegap/Device.java +++ b/framework/src/com/phonegap/Device.java @@ -14,9 +14,7 @@ import org.json.JSONObject; import com.phonegap.api.PhonegapActivity; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; -import android.content.Context; import android.provider.Settings; -import android.telephony.TelephonyManager; public class Device extends Plugin { @@ -116,34 +114,13 @@ public class Device extends Plugin { public String getPhonegapVersion() { return Device.phonegapVersion; } - - public String getLine1Number(){ - TelephonyManager operator = (TelephonyManager)this.ctx.getSystemService(Context.TELEPHONY_SERVICE); - return operator.getLine1Number(); - } - public String getDeviceId(){ - TelephonyManager operator = (TelephonyManager)this.ctx.getSystemService(Context.TELEPHONY_SERVICE); - return operator.getDeviceId(); - } - - public String getSimSerialNumber(){ - TelephonyManager operator = (TelephonyManager)this.ctx.getSystemService(Context.TELEPHONY_SERVICE); - return operator.getSimSerialNumber(); - } - - public String getSubscriberId(){ - TelephonyManager operator = (TelephonyManager)this.ctx.getSystemService(Context.TELEPHONY_SERVICE); - return operator.getSubscriberId(); - } - - public String getModel() - { + public String getModel() { String model = android.os.Build.MODEL; return model; } - public String getProductName() - { + + public String getProductName() { String productname = android.os.Build.PRODUCT; return productname; } @@ -158,8 +135,7 @@ public class Device extends Plugin { return osversion; } - public String getSDKVersion() - { + public String getSDKVersion() { String sdkversion = android.os.Build.VERSION.SDK; return sdkversion; } diff --git a/framework/src/com/phonegap/NetworkManager.java b/framework/src/com/phonegap/NetworkManager.java index f05d089c..c2b756bc 100755 --- a/framework/src/com/phonegap/NetworkManager.java +++ b/framework/src/com/phonegap/NetworkManager.java @@ -11,7 +11,6 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; -import org.json.JSONObject; import com.phonegap.api.PhonegapActivity; import com.phonegap.api.Plugin; @@ -21,10 +20,8 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.net.*; -import android.net.wifi.WifiInfo; -import android.net.wifi.WifiManager; -import android.telephony.TelephonyManager; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.util.Log; public class NetworkManager extends Plugin { @@ -57,14 +54,10 @@ public class NetworkManager extends Plugin { public static final String TYPE_NONE = "none"; private static final String LOG_TAG = "NetworkManager"; - private static final String NETWORK_NAME = "networkName"; - private static final String TYPE = "type"; private String connectionCallbackId; ConnectivityManager sockMan; - TelephonyManager telephonyManager; - WifiManager wifiManager; BroadcastReceiver receiver; /** @@ -83,8 +76,6 @@ public class NetworkManager extends Plugin { public void setContext(PhonegapActivity ctx) { super.setContext(ctx); this.sockMan = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); - this.telephonyManager = ((TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE)); - this.wifiManager = ((WifiManager) ctx.getSystemService(Context.WIFI_SERVICE)); this.connectionCallbackId = null; // We need to listen to connectivity events to update navigator.connection @@ -175,10 +166,8 @@ public class NetworkManager extends Plugin { * @return */ private void updateConnectionInfo(NetworkInfo info) { - JSONObject connection = this.getConnectionInfo(info); - // send update to javascript "navigator.network.connection" - sendUpdate(connection); + sendUpdate(this.getConnectionInfo(info)); } /** @@ -187,42 +176,18 @@ public class NetworkManager extends Plugin { * @param info the current active network info * @return a JSONObject that represents the network info */ - private JSONObject getConnectionInfo(NetworkInfo info) { - JSONObject connection = new JSONObject(); - - try { - if (info != null) { - // If we are not connected to any network set type to none - if (!info.isConnected()) { - connection.put(TYPE, TYPE_NONE); - connection.put(NETWORK_NAME, null); - } - else { - // If we are connected check which type - // First off is wifi - if (info.getTypeName().toLowerCase().equals(WIFI)) { - connection.put(TYPE, TYPE_WIFI); - WifiInfo wifiInfo = this.wifiManager.getConnectionInfo(); - if (wifiInfo != null) { - connection.put(NETWORK_NAME, wifiInfo.getSSID()); - } else { - connection.put(NETWORK_NAME, null); - } - } - // Otherwise it must be one of the mobile network protocols - else { - // Determine the correct type, 2G, 3G, 4G - connection.put(TYPE, getType(info)); - connection.put(NETWORK_NAME, telephonyManager.getNetworkOperatorName()); - } - } + private String getConnectionInfo(NetworkInfo info) { + String type = TYPE_NONE; + if (info != null) { + // If we are not connected to any network set type to none + if (!info.isConnected()) { + type = TYPE_NONE; + } + else { + type = getType(info); } } - catch (JSONException e) { - // this should never happen - Log.e(LOG_TAG, e.getMessage(), e); - } - return connection; + return type; } /** @@ -230,8 +195,8 @@ public class NetworkManager extends Plugin { * * @param connection the network info to set as navigator.connection */ - private void sendUpdate(JSONObject connection) { - PluginResult result = new PluginResult(PluginResult.Status.OK, connection); + private void sendUpdate(String type) { + PluginResult result = new PluginResult(PluginResult.Status.OK, type); result.setKeepCallback(true); this.success(result, this.connectionCallbackId); } @@ -246,7 +211,10 @@ public class NetworkManager extends Plugin { if (info != null) { String type = info.getTypeName(); - if (type.toLowerCase().equals(MOBILE)) { + if (type.toLowerCase().equals(WIFI)) { + return TYPE_WIFI; + } + else if (type.toLowerCase().equals(MOBILE)) { type = info.getSubtypeName(); if (type.toLowerCase().equals(GSM) || type.toLowerCase().equals(GPRS) ||