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

This commit is contained in:
macdonst 2011-06-09 03:03:03 +08:00 committed by Joe Bowser
parent 66f3018767
commit 8a1ab69235
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_FINE_LOCATION" />
<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.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

View File

@ -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');
}

View File

@ -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;
}

View File

@ -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) ||