Cached extra info to better detect changes.

This commit is contained in:
Max Woghiren 2014-06-03 17:08:16 -04:00
parent f27b34c2a9
commit 1cec040b30

View File

@ -24,6 +24,8 @@ import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult; import org.apache.cordova.PluginResult;
import org.apache.cordova.CordovaWebView; import org.apache.cordova.CordovaWebView;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
@ -75,7 +77,7 @@ public class NetworkManager extends CordovaPlugin {
ConnectivityManager sockMan; ConnectivityManager sockMan;
BroadcastReceiver receiver; BroadcastReceiver receiver;
private String lastStatus = ""; private JSONObject lastInfo = null;
/** /**
* Constructor. * Constructor.
@ -126,7 +128,12 @@ public class NetworkManager extends CordovaPlugin {
if (action.equals("getConnectionInfo")) { if (action.equals("getConnectionInfo")) {
this.connectionCallbackContext = callbackContext; this.connectionCallbackContext = callbackContext;
NetworkInfo info = sockMan.getActiveNetworkInfo(); NetworkInfo info = sockMan.getActiveNetworkInfo();
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, this.getConnectionInfo(info)); String connectionType = "";
try {
connectionType = this.getConnectionInfo(info).get("type").toString();
} catch (JSONException e) { }
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, connectionType);
pluginResult.setKeepCallback(true); pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult); callbackContext.sendPluginResult(pluginResult);
return true; return true;
@ -161,13 +168,17 @@ public class NetworkManager extends CordovaPlugin {
private void updateConnectionInfo(NetworkInfo info) { private void updateConnectionInfo(NetworkInfo info) {
// send update to javascript "navigator.network.connection" // send update to javascript "navigator.network.connection"
// Jellybean sends its own info // Jellybean sends its own info
String thisStatus = this.getConnectionInfo(info); JSONObject thisInfo = this.getConnectionInfo(info);
if(!thisStatus.equals(lastStatus)) if(!thisInfo.equals(lastInfo))
{ {
sendUpdate(thisStatus); String connectionType = "";
lastStatus = thisStatus; try {
} connectionType = thisInfo.get("type").toString();
} catch (JSONException e) { }
sendUpdate(connectionType);
lastInfo = thisInfo;
}
} }
/** /**
@ -176,7 +187,7 @@ public class NetworkManager extends CordovaPlugin {
* @param info the current active network info * @param info the current active network info
* @return a JSONObject that represents the network info * @return a JSONObject that represents the network info
*/ */
private String getConnectionInfo(NetworkInfo info) { private JSONObject getConnectionInfo(NetworkInfo info) {
String type = TYPE_NONE; String type = TYPE_NONE;
if (info != null) { if (info != null) {
// If we are not connected to any network set type to none // If we are not connected to any network set type to none
@ -187,8 +198,19 @@ public class NetworkManager extends CordovaPlugin {
type = getType(info); type = getType(info);
} }
} }
String extraInfo = info.getExtraInfo();
Log.d("CordovaNetworkManager", "Connection Type: " + type); Log.d("CordovaNetworkManager", "Connection Type: " + type);
return type; Log.d("CordovaNetworkManager", "Connection Extra Info: " + extraInfo);
JSONObject connectionInfo = new JSONObject();
try {
connectionInfo.put("type", type);
connectionInfo.put("extraInfo", extraInfo);
} catch (JSONException e) { }
return connectionInfo;
} }
/** /**
@ -247,3 +269,4 @@ public class NetworkManager extends CordovaPlugin {
return TYPE_UNKNOWN; return TYPE_UNKNOWN;
} }
} }