Updating Connection object to conform with recently released spec

- Replacing currentNW and homeNW with networkName.
- Changing Connection constants to strings instead of ints.
- Firing online/offline events on network change.
This commit is contained in:
macdonst 2011-06-03 01:11:51 +08:00 committed by Joe Bowser
parent 3c90a9a23c
commit 0280d5dd82
3 changed files with 73 additions and 38 deletions

View File

@ -14,15 +14,18 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" /> <uses-feature android:name="android.hardware.camera.autofocus" />

View File

@ -65,29 +65,52 @@ Network.prototype.isReachable = function(uri, callback, options) {
*/ */
var Connection = function() { var Connection = function() {
this.type = null; this.type = null;
this.homeNW = null; this.networkName = null;
this.currentNW = null; this._firstRun = true;
this._timer = null;
this.timeout = 500;
var me = this; var me = this;
this.getInfo( this.getInfo(
function(info) { function(info) {
// Need to send events if we are on or offline
if (info.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.type = info.type;
me.homeNW = info.homeNW; me.networkName = info.networkName;
me.currentNW = info.currentNW; PhoneGap.fireEvent('offline');
me._timer = null;
}, me.timeout);
} else {
// If there is a current offline event pending clear it
if (me._timer != null) {
clearTimeout(me._timer);
me._timer = null;
}
me.type = info.type;
me.networkName = info.networkName;
PhoneGap.fireEvent('online');
}
// should only fire this once
if (me._firstRun) {
me._firstRun = false;
PhoneGap.onPhoneGapConnectionReady.fire(); PhoneGap.onPhoneGapConnectionReady.fire();
}
}, },
function(e) { function(e) {
console.log("Error initializing Network Connection: " + e); console.log("Error initializing Network Connection: " + e);
}); });
}; };
Connection.UNKNOWN = 0; Connection.UNKNOWN = "unknown";
Connection.ETHERNET = 1; Connection.ETHERNET = "ethernet";
Connection.WIFI = 2; Connection.WIFI = "wifi";
Connection.CELL_2G = 3; Connection.CELL_2G = "2g";
Connection.CELL_3G = 4; Connection.CELL_3G = "3g";
Connection.CELL_4G = 5; Connection.CELL_4G = "4g";
Connection.NONE = 20; Connection.NONE = "none";
/** /**
* Get connection info * Get connection info

View File

@ -22,6 +22,8 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.net.*; import android.net.*;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.util.Log; import android.util.Log;
@ -46,19 +48,23 @@ public class NetworkManager extends Plugin {
public static final String LTE = "lte"; public static final String LTE = "lte";
public static final String UMB = "umb"; public static final String UMB = "umb";
// return types // return types
public static final int TYPE_UNKNOWN = 0; public static final String TYPE_UNKNOWN = "unknown";
public static final int TYPE_ETHERNET = 1; public static final String TYPE_ETHERNET = "ethernet";
public static final int TYPE_WIFI = 2; public static final String TYPE_WIFI = "wifi";
public static final int TYPE_2G = 3; public static final String TYPE_2G = "2g";
public static final int TYPE_3G = 4; public static final String TYPE_3G = "3g";
public static final int TYPE_4G = 5; public static final String TYPE_4G = "4g";
public static final int TYPE_NONE = 20; public static final String TYPE_NONE = "none";
private static final String LOG_TAG = "NetworkManager"; private static final String LOG_TAG = "NetworkManager";
private static final String NETWORK_NAME = "networkName";
private static final String TYPE = "type";
private String connectionCallbackId; private String connectionCallbackId;
ConnectivityManager sockMan; ConnectivityManager sockMan;
TelephonyManager telephonyManager; TelephonyManager telephonyManager;
WifiManager wifiManager;
BroadcastReceiver receiver; BroadcastReceiver receiver;
/** /**
@ -78,6 +84,7 @@ public class NetworkManager extends Plugin {
super.setContext(ctx); super.setContext(ctx);
this.sockMan = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); this.sockMan = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
this.telephonyManager = ((TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE)); this.telephonyManager = ((TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE));
this.wifiManager = ((WifiManager) ctx.getSystemService(Context.WIFI_SERVICE));
this.connectionCallbackId = null; this.connectionCallbackId = null;
// We need to listen to connectivity events to update navigator.connection // We need to listen to connectivity events to update navigator.connection
@ -151,7 +158,7 @@ public class NetworkManager extends Plugin {
try { try {
this.ctx.unregisterReceiver(this.receiver); this.ctx.unregisterReceiver(this.receiver);
} catch (Exception e) { } catch (Exception e) {
System.out.println("Error unregistering network receiver: " + e.getMessage()); Log.e(LOG_TAG, "Error unregistering network receiver: " + e.getMessage(), e);
} }
} }
} }
@ -170,7 +177,7 @@ public class NetworkManager extends Plugin {
private void updateConnectionInfo(NetworkInfo info) { private void updateConnectionInfo(NetworkInfo info) {
JSONObject connection = this.getConnectionInfo(info); JSONObject connection = this.getConnectionInfo(info);
// send update to javascript "navigator.connection" // send update to javascript "navigator.network.connection"
sendUpdate(connection); sendUpdate(connection);
} }
@ -187,24 +194,26 @@ public class NetworkManager extends Plugin {
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
if (!info.isConnected()) { if (!info.isConnected()) {
connection.put("type", TYPE_NONE); connection.put(TYPE, TYPE_NONE);
connection.put("homeNW", null); connection.put(NETWORK_NAME, null);
connection.put("currentNW", null);
} }
else { else {
// If we are connected check which type // If we are connected check which type
// First off is wifi // First off is wifi
if (info.getTypeName().toLowerCase().equals(WIFI)) { if (info.getTypeName().toLowerCase().equals(WIFI)) {
connection.put("type", TYPE_WIFI); connection.put(TYPE, TYPE_WIFI);
connection.put("homeNW", null); WifiInfo wifiInfo = this.wifiManager.getConnectionInfo();
connection.put("currentNW", null); 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 // Otherwise it must be one of the mobile network protocols
else { else {
// Determine the correct type, 2G, 3G, 4G // Determine the correct type, 2G, 3G, 4G
connection.put("type", getType(info)); connection.put(TYPE, getType(info));
connection.put("homeNW", telephonyManager.getSimOperatorName()); connection.put(NETWORK_NAME, telephonyManager.getNetworkOperatorName());
connection.put("currentNW", telephonyManager.getNetworkOperatorName());
} }
} }
} }
@ -233,7 +242,7 @@ public class NetworkManager extends Plugin {
* @param info the network info so we can determine connection type. * @param info the network info so we can determine connection type.
* @return the type of mobile network we are on * @return the type of mobile network we are on
*/ */
private int getType(NetworkInfo info) { private String getType(NetworkInfo info) {
if (info != null) { if (info != null) {
String type = info.getTypeName(); String type = info.getTypeName();