mirror of
https://gitee.com/shuto/cordova-plugin-network-information.git
synced 2025-01-18 21:52:48 +08:00
Android - Fixes https://github.com/apache/cordova-plugin-network-information#110 (#114)
* Try to fix https://github.com/apache/cordova-plugin-network-information/issues/110 - The android JSONObject doesn't provide an equal implementation for JSON-Object. * undo autoreformatting of android studio * Android - remove JSONObject and extraInfo. KIS. * Update src/android/NetworkManager.java Co-authored-by: Tim Brust <ratchet.player@gmx.de> * Update src/android/NetworkManager.java Co-authored-by: Tim Brust <ratchet.player@gmx.de> * Update src/android/NetworkManager.java Co-authored-by: Tim Brust <ratchet.player@gmx.de> * Android - code review PR * Android - code review PR -> remove obsolete logging of constant WIFI Co-authored-by: Tim Brust <ratchet.player@gmx.de>
This commit is contained in:
parent
2b5d16efc2
commit
3066e3cf88
@ -86,7 +86,7 @@ public class NetworkManager extends CordovaPlugin {
|
||||
|
||||
ConnectivityManager sockMan;
|
||||
BroadcastReceiver receiver;
|
||||
private JSONObject lastInfo = null;
|
||||
private String lastTypeOfNetwork;
|
||||
|
||||
/**
|
||||
* Sets the context of the Command. This can then be used to do things like
|
||||
@ -115,13 +115,7 @@ public class NetworkManager extends CordovaPlugin {
|
||||
if (action.equals("getConnectionInfo")) {
|
||||
this.connectionCallbackContext = callbackContext;
|
||||
NetworkInfo info = sockMan.getActiveNetworkInfo();
|
||||
String connectionType = "";
|
||||
try {
|
||||
connectionType = this.getConnectionInfo(info).get("type").toString();
|
||||
} catch (JSONException e) {
|
||||
LOG.d(LOG_TAG, e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
String connectionType = this.getTypeOfNetworkFallbackToTypeNoneIfNotConnected(info);
|
||||
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, connectionType);
|
||||
pluginResult.setKeepCallback(true);
|
||||
callbackContext.sendPluginResult(pluginResult);
|
||||
@ -167,16 +161,11 @@ public class NetworkManager extends CordovaPlugin {
|
||||
updateConnectionInfo(sockMan.getActiveNetworkInfo());
|
||||
}
|
||||
|
||||
String connectionType = null;
|
||||
if(NetworkManager.this.lastInfo == null) {
|
||||
String connectionType;
|
||||
if (NetworkManager.this.lastTypeOfNetwork == null) {
|
||||
connectionType = TYPE_NONE;
|
||||
} else {
|
||||
try {
|
||||
connectionType = NetworkManager.this.lastInfo.get("type").toString();
|
||||
} catch (JSONException e) {
|
||||
LOG.d(LOG_TAG, e.getLocalizedMessage());
|
||||
connectionType = TYPE_NONE;
|
||||
}
|
||||
connectionType = NetworkManager.this.lastTypeOfNetwork;
|
||||
}
|
||||
|
||||
// Lollipop always returns false for the EXTRA_NO_CONNECTIVITY flag => fix for Android M and above.
|
||||
@ -218,30 +207,24 @@ public class NetworkManager extends CordovaPlugin {
|
||||
private void updateConnectionInfo(NetworkInfo info) {
|
||||
// send update to javascript "navigator.connection"
|
||||
// Jellybean sends its own info
|
||||
JSONObject thisInfo = this.getConnectionInfo(info);
|
||||
if(!thisInfo.equals(lastInfo))
|
||||
{
|
||||
String connectionType = "";
|
||||
try {
|
||||
connectionType = thisInfo.get("type").toString();
|
||||
} catch (JSONException e) {
|
||||
LOG.d(LOG_TAG, e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
sendUpdate(connectionType);
|
||||
lastInfo = thisInfo;
|
||||
String currentNetworkType = this.getTypeOfNetworkFallbackToTypeNoneIfNotConnected(info);
|
||||
if (currentNetworkType.equals(this.lastTypeOfNetwork)) {
|
||||
LOG.d(LOG_TAG, "Networkinfo state didn't change, there is no event propagated to the JavaScript side.");
|
||||
} else {
|
||||
sendUpdate(currentNetworkType);
|
||||
this.lastTypeOfNetwork = currentNetworkType;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latest network connection information
|
||||
* Gets the type of network connection of the NetworkInfo input
|
||||
*
|
||||
* @param info the current active network info
|
||||
* @return a JSONObject that represents the network info
|
||||
* @return type the type of network
|
||||
*/
|
||||
private JSONObject getConnectionInfo(NetworkInfo info) {
|
||||
String type = TYPE_NONE;
|
||||
String extraInfo = "";
|
||||
private String getTypeOfNetworkFallbackToTypeNoneIfNotConnected(NetworkInfo info) {
|
||||
// the info might still be null in this part of the code
|
||||
String type;
|
||||
if (info != null) {
|
||||
// If we are not connected to any network set type to none
|
||||
if (!info.isConnected()) {
|
||||
@ -250,22 +233,12 @@ public class NetworkManager extends CordovaPlugin {
|
||||
else {
|
||||
type = getType(info);
|
||||
}
|
||||
extraInfo = info.getExtraInfo();
|
||||
} else {
|
||||
type = TYPE_NONE;
|
||||
}
|
||||
|
||||
LOG.d(LOG_TAG, "Connection Type: " + type);
|
||||
LOG.d(LOG_TAG, "Connection Extra Info: " + extraInfo);
|
||||
|
||||
JSONObject connectionInfo = new JSONObject();
|
||||
|
||||
try {
|
||||
connectionInfo.put("type", type);
|
||||
connectionInfo.put("extraInfo", extraInfo);
|
||||
} catch (JSONException e) {
|
||||
LOG.d(LOG_TAG, e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
return connectionInfo;
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -289,26 +262,21 @@ public class NetworkManager extends CordovaPlugin {
|
||||
* @return the type of mobile network we are on
|
||||
*/
|
||||
private String getType(NetworkInfo info) {
|
||||
if (info != null) {
|
||||
String type = info.getTypeName().toLowerCase(Locale.US);
|
||||
String type = info.getTypeName().toLowerCase(Locale.US);
|
||||
|
||||
LOG.d(LOG_TAG, "toLower : " + type.toLowerCase());
|
||||
LOG.d(LOG_TAG, "wifi : " + WIFI);
|
||||
if (type.equals(WIFI)) {
|
||||
return TYPE_WIFI;
|
||||
}
|
||||
else if (type.toLowerCase().equals(TYPE_ETHERNET) || type.toLowerCase().startsWith(TYPE_ETHERNET_SHORT)) {
|
||||
return TYPE_ETHERNET;
|
||||
}
|
||||
else if (type.equals(MOBILE) || type.equals(CELLULAR)) {
|
||||
type = info.getSubtypeName().toLowerCase(Locale.US);
|
||||
if (type.equals(GSM) ||
|
||||
LOG.d(LOG_TAG, "toLower : " + type);
|
||||
if (type.equals(WIFI)) {
|
||||
return TYPE_WIFI;
|
||||
} else if (type.toLowerCase().equals(TYPE_ETHERNET) || type.toLowerCase().startsWith(TYPE_ETHERNET_SHORT)) {
|
||||
return TYPE_ETHERNET;
|
||||
} else if (type.equals(MOBILE) || type.equals(CELLULAR)) {
|
||||
type = info.getSubtypeName().toLowerCase(Locale.US);
|
||||
if (type.equals(GSM) ||
|
||||
type.equals(GPRS) ||
|
||||
type.equals(EDGE) ||
|
||||
type.equals(TWO_G)) {
|
||||
return TYPE_2G;
|
||||
}
|
||||
else if (type.startsWith(CDMA) ||
|
||||
return TYPE_2G;
|
||||
} else if (type.startsWith(CDMA) ||
|
||||
type.equals(UMTS) ||
|
||||
type.equals(ONEXRTT) ||
|
||||
type.equals(EHRPD) ||
|
||||
@ -316,19 +284,14 @@ public class NetworkManager extends CordovaPlugin {
|
||||
type.equals(HSDPA) ||
|
||||
type.equals(HSPA) ||
|
||||
type.equals(THREE_G)) {
|
||||
return TYPE_3G;
|
||||
}
|
||||
else if (type.equals(LTE) ||
|
||||
return TYPE_3G;
|
||||
} else if (type.equals(LTE) ||
|
||||
type.equals(UMB) ||
|
||||
type.equals(HSPA_PLUS) ||
|
||||
type.equals(FOUR_G)) {
|
||||
return TYPE_4G;
|
||||
}
|
||||
return TYPE_4G;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return TYPE_NONE;
|
||||
}
|
||||
return TYPE_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user