Merge branch 'master' of git://github.com/phonegap/phonegap-android

This commit is contained in:
macdonst 2011-08-17 13:45:24 -04:00
commit 3666490347

View File

@ -22,201 +22,209 @@ import android.net.NetworkInfo;
import android.util.Log; import android.util.Log;
public class NetworkManager extends Plugin { public class NetworkManager extends Plugin {
public static int NOT_REACHABLE = 0; public static int NOT_REACHABLE = 0;
public static int REACHABLE_VIA_CARRIER_DATA_NETWORK = 1; public static int REACHABLE_VIA_CARRIER_DATA_NETWORK = 1;
public static int REACHABLE_VIA_WIFI_NETWORK = 2; public static int REACHABLE_VIA_WIFI_NETWORK = 2;
public static final String WIFI = "wifi"; public static final String WIFI = "wifi";
public static final String WIMAX = "wimax"; public static final String WIMAX = "wimax";
// mobile // mobile
public static final String MOBILE = "mobile"; public static final String MOBILE = "mobile";
// 2G network types // 2G network types
public static final String GSM = "gsm"; public static final String GSM = "gsm";
public static final String GPRS = "gprs"; public static final String GPRS = "gprs";
public static final String EDGE = "edge"; public static final String EDGE = "edge";
// 3G network types // 3G network types
public static final String CDMA = "cdma"; public static final String CDMA = "cdma";
public static final String UMTS = "umts"; public static final String UMTS = "umts";
// 4G network types public static final String HSPA = "hspa";
public static final String LTE = "lte"; public static final String HSUPA = "hsupa";
public static final String UMB = "umb"; public static final String HSDPA = "hsdpa";
// return types // 4G network types
public static final String TYPE_UNKNOWN = "unknown"; public static final String LTE = "lte";
public static final String TYPE_ETHERNET = "ethernet"; public static final String UMB = "umb";
public static final String TYPE_WIFI = "wifi"; public static final String HSPA_PLUS = "hspa+";
public static final String TYPE_2G = "2g"; // return types
public static final String TYPE_3G = "3g"; public static final String TYPE_UNKNOWN = "unknown";
public static final String TYPE_4G = "4g"; public static final String TYPE_ETHERNET = "ethernet";
public static final String TYPE_NONE = "none"; public static final String TYPE_WIFI = "wifi";
public static final String TYPE_2G = "2g";
private static final String LOG_TAG = "NetworkManager"; public static final String TYPE_3G = "3g";
public static final String TYPE_4G = "4g";
public static final String TYPE_NONE = "none";
private static final String LOG_TAG = "NetworkManager";
private String connectionCallbackId; private String connectionCallbackId;
ConnectivityManager sockMan; ConnectivityManager sockMan;
BroadcastReceiver receiver; BroadcastReceiver receiver;
/** /**
* Constructor. * Constructor.
*/ */
public NetworkManager() { public NetworkManager() {
this.receiver = null; this.receiver = null;
} }
/** /**
* Sets the context of the Command. This can then be used to do things like * Sets the context of the Command. This can then be used to do things like
* get file paths associated with the Activity. * get file paths associated with the Activity.
* *
* @param ctx The context of the main Activity. * @param ctx The context of the main Activity.
*/ */
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.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
IntentFilter intentFilter = new IntentFilter() ; IntentFilter intentFilter = new IntentFilter() ;
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
if (this.receiver == null) { if (this.receiver == null) {
this.receiver = new BroadcastReceiver() { this.receiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
updateConnectionInfo((NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO)); updateConnectionInfo((NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO));
} }
}; };
ctx.registerReceiver(this.receiver, intentFilter); ctx.registerReceiver(this.receiver, intentFilter);
} }
} }
/** /**
* Executes the request and returns PluginResult. * Executes the request and returns PluginResult.
* *
* @param action The action to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the plugin. * @param args JSONArry of arguments for the plugin.
* @param callbackId The callback id used when calling back into JavaScript. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message. * @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args, String callbackId) { public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.INVALID_ACTION; PluginResult.Status status = PluginResult.Status.INVALID_ACTION;
String result = "Unsupported Operation: " + action; String result = "Unsupported Operation: " + action;
if (action.equals("getConnectionInfo")) { if (action.equals("getConnectionInfo")) {
this.connectionCallbackId = callbackId; this.connectionCallbackId = callbackId;
NetworkInfo info = sockMan.getActiveNetworkInfo(); NetworkInfo info = sockMan.getActiveNetworkInfo();
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, this.getConnectionInfo(info)); PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, this.getConnectionInfo(info));
pluginResult.setKeepCallback(true); pluginResult.setKeepCallback(true);
return pluginResult; return pluginResult;
} }
return new PluginResult(status, result); return new PluginResult(status, result);
} }
/** /**
* Identifies if action to be executed returns a value and should be run synchronously. * Identifies if action to be executed returns a value and should be run synchronously.
* *
* @param action The action to execute * @param action The action to execute
* @return T=returns value * @return T=returns value
*/ */
public boolean isSynch(String action) { public boolean isSynch(String action) {
// All methods take a while, so always use async // All methods take a while, so always use async
return false; return false;
} }
/** /**
* Stop network receiver. * Stop network receiver.
*/ */
public void onDestroy() { public void onDestroy() {
if (this.receiver != null) { if (this.receiver != null) {
try { try {
this.ctx.unregisterReceiver(this.receiver); this.ctx.unregisterReceiver(this.receiver);
} catch (Exception e) { } catch (Exception e) {
Log.e(LOG_TAG, "Error unregistering network receiver: " + e.getMessage(), e); Log.e(LOG_TAG, "Error unregistering network receiver: " + e.getMessage(), e);
} }
} }
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// LOCAL METHODS // LOCAL METHODS
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/** /**
* Updates the JavaScript side whenever the connection changes * Updates the JavaScript side whenever the connection changes
* *
* @param info the current active network info * @param info the current active network info
* @return * @return
*/ */
private void updateConnectionInfo(NetworkInfo info) { private void updateConnectionInfo(NetworkInfo info) {
// send update to javascript "navigator.network.connection" // send update to javascript "navigator.network.connection"
sendUpdate(this.getConnectionInfo(info)); sendUpdate(this.getConnectionInfo(info));
} }
/** /**
* Get the latest network connection information * Get the latest network connection information
* *
* @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 String 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
if (!info.isConnected()) { if (!info.isConnected()) {
type = TYPE_NONE; type = TYPE_NONE;
} }
else { else {
type = getType(info); type = getType(info);
} }
} }
return type; return type;
} }
/** /**
* Create a new plugin result and send it back to JavaScript * Create a new plugin result and send it back to JavaScript
* *
* @param connection the network info to set as navigator.connection * @param connection the network info to set as navigator.connection
*/ */
private void sendUpdate(String type) { private void sendUpdate(String type) {
PluginResult result = new PluginResult(PluginResult.Status.OK, type); PluginResult result = new PluginResult(PluginResult.Status.OK, type);
result.setKeepCallback(true); result.setKeepCallback(true);
this.success(result, this.connectionCallbackId); this.success(result, this.connectionCallbackId);
} }
/** /**
* Determine the type of connection * Determine the type of connection
* *
* @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 String getType(NetworkInfo info) { private String getType(NetworkInfo info) {
if (info != null) { if (info != null) {
String type = info.getTypeName(); String type = info.getTypeName();
if (type.toLowerCase().equals(WIFI)) { if (type.toLowerCase().equals(WIFI)) {
return TYPE_WIFI; return TYPE_WIFI;
} }
else if (type.toLowerCase().equals(MOBILE)) { 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) ||
type.toLowerCase().equals(EDGE)) { type.toLowerCase().equals(EDGE)) {
return TYPE_2G; return TYPE_2G;
} }
else if (type.toLowerCase().equals(CDMA) || else if (type.toLowerCase().equals(CDMA) ||
type.toLowerCase().equals(UMTS)) { type.toLowerCase().equals(UMTS) ||
return TYPE_3G; type.toLowerCase().equals(HSUPA) ||
} type.toLowerCase().equals(HSDPA) ||
else if (type.toLowerCase().equals(LTE) || type.toLowerCase().equals(HSPA)) {
type.toLowerCase().equals(UMB)) { return TYPE_3G;
return TYPE_4G; }
} else if (type.toLowerCase().equals(LTE) ||
} type.toLowerCase().equals(UMB) ||
} type.toLowerCase().equals(HSPA_PLUS)) {
else { return TYPE_4G;
return TYPE_NONE; }
} }
return TYPE_UNKNOWN; }
} else {
return TYPE_NONE;
}
return TYPE_UNKNOWN;
}
} }