Updating Network Connection API to match spec released on June 7th

This commit is contained in:
macdonst 2011-06-08 15:03:03 -04:00
parent 315b5a59b3
commit fbb6b22de6
4 changed files with 27 additions and 88 deletions

View File

@ -14,8 +14,6 @@
<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.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" />

View File

@ -65,20 +65,18 @@ Network.prototype.isReachable = function(uri, callback, options) {
*/ */
var Connection = function() { var Connection = function() {
this.type = null; this.type = null;
this.networkName = null;
this._firstRun = true; this._firstRun = true;
this._timer = null; this._timer = null;
this.timeout = 500; this.timeout = 500;
var me = this; var me = this;
this.getInfo( this.getInfo(
function(info) { function(type) {
// Need to send events if we are on or offline // 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 // set a timer if still offline at the end of timer send the offline event
me._timer = setTimeout(function(){ me._timer = setTimeout(function(){
me.type = info.type; me.type = type;
me.networkName = info.networkName;
PhoneGap.fireEvent('offline'); PhoneGap.fireEvent('offline');
me._timer = null; me._timer = null;
}, me.timeout); }, me.timeout);
@ -88,8 +86,7 @@ var Connection = function() {
clearTimeout(me._timer); clearTimeout(me._timer);
me._timer = null; me._timer = null;
} }
me.type = info.type; me.type = type;
me.networkName = info.networkName;
PhoneGap.fireEvent('online'); PhoneGap.fireEvent('online');
} }

View File

@ -14,9 +14,7 @@ import org.json.JSONObject;
import com.phonegap.api.PhonegapActivity; import com.phonegap.api.PhonegapActivity;
import com.phonegap.api.Plugin; import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult; import com.phonegap.api.PluginResult;
import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.telephony.TelephonyManager;
public class Device extends Plugin { public class Device extends Plugin {
@ -117,33 +115,12 @@ public class Device extends Plugin {
return Device.phonegapVersion; return Device.phonegapVersion;
} }
public String getLine1Number(){ public String getModel() {
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()
{
String model = android.os.Build.MODEL; String model = android.os.Build.MODEL;
return model; return model;
} }
public String getProductName()
{ public String getProductName() {
String productname = android.os.Build.PRODUCT; String productname = android.os.Build.PRODUCT;
return productname; return productname;
} }
@ -158,8 +135,7 @@ public class Device extends Plugin {
return osversion; return osversion;
} }
public String getSDKVersion() public String getSDKVersion() {
{
String sdkversion = android.os.Build.VERSION.SDK; String sdkversion = android.os.Build.VERSION.SDK;
return sdkversion; return sdkversion;
} }

View File

@ -11,7 +11,6 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject;
import com.phonegap.api.PhonegapActivity; import com.phonegap.api.PhonegapActivity;
import com.phonegap.api.Plugin; import com.phonegap.api.Plugin;
@ -21,10 +20,8 @@ import android.content.BroadcastReceiver;
import android.content.Context; 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.ConnectivityManager;
import android.net.wifi.WifiInfo; import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log; import android.util.Log;
public class NetworkManager extends Plugin { public class NetworkManager extends Plugin {
@ -57,14 +54,10 @@ public class NetworkManager extends Plugin {
public static final String TYPE_NONE = "none"; 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;
WifiManager wifiManager;
BroadcastReceiver receiver; BroadcastReceiver receiver;
/** /**
@ -83,8 +76,6 @@ public class NetworkManager extends Plugin {
public void setContext(PhonegapActivity ctx) { public void setContext(PhonegapActivity ctx) {
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.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
@ -175,10 +166,8 @@ public class NetworkManager extends Plugin {
* @return * @return
*/ */
private void updateConnectionInfo(NetworkInfo info) { private void updateConnectionInfo(NetworkInfo info) {
JSONObject connection = this.getConnectionInfo(info);
// send update to javascript "navigator.network.connection" // 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 * @param info the current active network info
* @return a JSONObject that represents the network info * @return a JSONObject that represents the network info
*/ */
private JSONObject getConnectionInfo(NetworkInfo info) { private String getConnectionInfo(NetworkInfo info) {
JSONObject connection = new JSONObject(); String type = TYPE_NONE;
try {
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); type = TYPE_NONE;
connection.put(NETWORK_NAME, null);
} }
else { else {
// If we are connected check which type type = getType(info);
// 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 return type;
else {
// Determine the correct type, 2G, 3G, 4G
connection.put(TYPE, getType(info));
connection.put(NETWORK_NAME, telephonyManager.getNetworkOperatorName());
}
}
}
}
catch (JSONException e) {
// this should never happen
Log.e(LOG_TAG, e.getMessage(), e);
}
return connection;
} }
/** /**
@ -230,8 +195,8 @@ public class NetworkManager extends Plugin {
* *
* @param connection the network info to set as navigator.connection * @param connection the network info to set as navigator.connection
*/ */
private void sendUpdate(JSONObject connection) { private void sendUpdate(String type) {
PluginResult result = new PluginResult(PluginResult.Status.OK, connection); PluginResult result = new PluginResult(PluginResult.Status.OK, type);
result.setKeepCallback(true); result.setKeepCallback(true);
this.success(result, this.connectionCallbackId); this.success(result, this.connectionCallbackId);
} }
@ -246,7 +211,10 @@ public class NetworkManager extends Plugin {
if (info != null) { if (info != null) {
String type = info.getTypeName(); 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(); type = info.getSubtypeName();
if (type.toLowerCase().equals(GSM) || if (type.toLowerCase().equals(GSM) ||
type.toLowerCase().equals(GPRS) || type.toLowerCase().equals(GPRS) ||