* 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:
Pieter Van Poyer 2020-07-08 15:14:41 +02:00 committed by GitHub
parent 2b5d16efc2
commit 3066e3cf88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -86,7 +86,7 @@ public class NetworkManager extends CordovaPlugin {
ConnectivityManager sockMan; ConnectivityManager sockMan;
BroadcastReceiver receiver; BroadcastReceiver receiver;
private JSONObject lastInfo = null; private String lastTypeOfNetwork;
/** /**
* 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
@ -115,13 +115,7 @@ public class NetworkManager extends CordovaPlugin {
if (action.equals("getConnectionInfo")) { if (action.equals("getConnectionInfo")) {
this.connectionCallbackContext = callbackContext; this.connectionCallbackContext = callbackContext;
NetworkInfo info = sockMan.getActiveNetworkInfo(); NetworkInfo info = sockMan.getActiveNetworkInfo();
String connectionType = ""; String connectionType = this.getTypeOfNetworkFallbackToTypeNoneIfNotConnected(info);
try {
connectionType = this.getConnectionInfo(info).get("type").toString();
} catch (JSONException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
}
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, connectionType); PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, connectionType);
pluginResult.setKeepCallback(true); pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult); callbackContext.sendPluginResult(pluginResult);
@ -167,16 +161,11 @@ public class NetworkManager extends CordovaPlugin {
updateConnectionInfo(sockMan.getActiveNetworkInfo()); updateConnectionInfo(sockMan.getActiveNetworkInfo());
} }
String connectionType = null; String connectionType;
if(NetworkManager.this.lastInfo == null) { if (NetworkManager.this.lastTypeOfNetwork == null) {
connectionType = TYPE_NONE; connectionType = TYPE_NONE;
} else { } else {
try { connectionType = NetworkManager.this.lastTypeOfNetwork;
connectionType = NetworkManager.this.lastInfo.get("type").toString();
} catch (JSONException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
connectionType = TYPE_NONE;
}
} }
// Lollipop always returns false for the EXTRA_NO_CONNECTIVITY flag => fix for Android M and above. // 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) { private void updateConnectionInfo(NetworkInfo info) {
// send update to javascript "navigator.connection" // send update to javascript "navigator.connection"
// Jellybean sends its own info // Jellybean sends its own info
JSONObject thisInfo = this.getConnectionInfo(info); String currentNetworkType = this.getTypeOfNetworkFallbackToTypeNoneIfNotConnected(info);
if(!thisInfo.equals(lastInfo)) if (currentNetworkType.equals(this.lastTypeOfNetwork)) {
{ LOG.d(LOG_TAG, "Networkinfo state didn't change, there is no event propagated to the JavaScript side.");
String connectionType = ""; } else {
try { sendUpdate(currentNetworkType);
connectionType = thisInfo.get("type").toString(); this.lastTypeOfNetwork = currentNetworkType;
} catch (JSONException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
}
sendUpdate(connectionType);
lastInfo = thisInfo;
} }
} }
/** /**
* Get the latest network connection information * Gets the type of network connection of the NetworkInfo input
* *
* @param info the current active network info * @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) { private String getTypeOfNetworkFallbackToTypeNoneIfNotConnected(NetworkInfo info) {
String type = TYPE_NONE; // the info might still be null in this part of the code
String extraInfo = ""; String type;
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()) {
@ -250,22 +233,12 @@ public class NetworkManager extends CordovaPlugin {
else { else {
type = getType(info); type = getType(info);
} }
extraInfo = info.getExtraInfo(); } else {
type = TYPE_NONE;
} }
LOG.d(LOG_TAG, "Connection Type: " + type); LOG.d(LOG_TAG, "Connection Type: " + type);
LOG.d(LOG_TAG, "Connection Extra Info: " + extraInfo); return type;
JSONObject connectionInfo = new JSONObject();
try {
connectionInfo.put("type", type);
connectionInfo.put("extraInfo", extraInfo);
} catch (JSONException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
}
return connectionInfo;
} }
/** /**
@ -289,26 +262,21 @@ public class NetworkManager extends CordovaPlugin {
* @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) {
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, "toLower : " + type);
LOG.d(LOG_TAG, "wifi : " + WIFI);
if (type.equals(WIFI)) { if (type.equals(WIFI)) {
return TYPE_WIFI; return TYPE_WIFI;
} } else if (type.toLowerCase().equals(TYPE_ETHERNET) || type.toLowerCase().startsWith(TYPE_ETHERNET_SHORT)) {
else if (type.toLowerCase().equals(TYPE_ETHERNET) || type.toLowerCase().startsWith(TYPE_ETHERNET_SHORT)) {
return TYPE_ETHERNET; return TYPE_ETHERNET;
} } else if (type.equals(MOBILE) || type.equals(CELLULAR)) {
else if (type.equals(MOBILE) || type.equals(CELLULAR)) {
type = info.getSubtypeName().toLowerCase(Locale.US); type = info.getSubtypeName().toLowerCase(Locale.US);
if (type.equals(GSM) || if (type.equals(GSM) ||
type.equals(GPRS) || type.equals(GPRS) ||
type.equals(EDGE) || type.equals(EDGE) ||
type.equals(TWO_G)) { type.equals(TWO_G)) {
return TYPE_2G; return TYPE_2G;
} } else if (type.startsWith(CDMA) ||
else if (type.startsWith(CDMA) ||
type.equals(UMTS) || type.equals(UMTS) ||
type.equals(ONEXRTT) || type.equals(ONEXRTT) ||
type.equals(EHRPD) || type.equals(EHRPD) ||
@ -317,18 +285,13 @@ public class NetworkManager extends CordovaPlugin {
type.equals(HSPA) || type.equals(HSPA) ||
type.equals(THREE_G)) { type.equals(THREE_G)) {
return TYPE_3G; return TYPE_3G;
} } else if (type.equals(LTE) ||
else if (type.equals(LTE) ||
type.equals(UMB) || type.equals(UMB) ||
type.equals(HSPA_PLUS) || type.equals(HSPA_PLUS) ||
type.equals(FOUR_G)) { type.equals(FOUR_G)) {
return TYPE_4G; return TYPE_4G;
} }
} }
}
else {
return TYPE_NONE;
}
return TYPE_UNKNOWN; return TYPE_UNKNOWN;
} }
} }