From cafdd673d5c0afe217af3e3bf1a063d47096a4a1 Mon Sep 17 00:00:00 2001 From: PVPoyer Date: Wed, 26 Sep 2018 22:08:58 +0200 Subject: [PATCH 1/6] Try to fix: https://github.com/apache/cordova-plugin-network-information/issues/64: in case of TYPE_NONE (android bug?) return TYPE_UNKNOWN if ConnectivityManager.EXTRA_NO_CONNECTIVITY from the intent return false. --- src/android/NetworkManager.java | 83 +++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 19 deletions(-) diff --git a/src/android/NetworkManager.java b/src/android/NetworkManager.java index 614b6e7..3fc5af1 100755 --- a/src/android/NetworkManager.java +++ b/src/android/NetworkManager.java @@ -99,21 +99,7 @@ public class NetworkManager extends CordovaPlugin { this.sockMan = (ConnectivityManager) cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); this.connectionCallbackContext = null; - // We need to listen to connectivity events to update navigator.connection - IntentFilter intentFilter = new IntentFilter(); - intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); - if (this.receiver == null) { - this.receiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - // (The null check is for the ARM Emulator, please use Intel Emulator for better results) - if(NetworkManager.this.webView != null) - updateConnectionInfo(sockMan.getActiveNetworkInfo()); - } - }; - webView.getContext().registerReceiver(this.receiver, intentFilter); - } - + this.registerConnectivityActionReceiver(); } /** @@ -147,6 +133,69 @@ public class NetworkManager extends CordovaPlugin { * Stop network receiver. */ public void onDestroy() { + this.unregisterReceiver(); + } + + @Override + public void onPause(boolean multitasking) { + this.unregisterReceiver(); + } + + @Override + public void onResume(boolean multitasking) { + super.onResume(multitasking); + + this.unregisterReceiver(); + this.registerConnectivityActionReceiver(); + } + + //-------------------------------------------------------------------------- + // LOCAL METHODS + //-------------------------------------------------------------------------- + + private void registerConnectivityActionReceiver() { + // We need to listen to connectivity events to update navigator.connection + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); + if (this.receiver == null) { + this.receiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + // (The null check is for the ARM Emulator, please use Intel Emulator for better results) + if (NetworkManager.this.webView != null) { + updateConnectionInfo(sockMan.getActiveNetworkInfo()); + } + + String connectionType = null; + if(NetworkManager.this.lastInfo == 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; + } + } + + if(TYPE_NONE.equals(connectionType)) { + boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); + LOG.d(LOG_TAG, "Intent no connectivity: " + noConnectivity); + if(noConnectivity) { + LOG.d(LOG_TAG, "Really no connectivity"); + } else { + LOG.d(LOG_TAG, "!!! Switching to unknown"); + sendUpdate(TYPE_UNKNOWN); + } + } + } + }; + } + + webView.getContext().registerReceiver(this.receiver, intentFilter); + } + + private void unregisterReceiver() { if (this.receiver != null) { try { webView.getContext().unregisterReceiver(this.receiver); @@ -158,10 +207,6 @@ public class NetworkManager extends CordovaPlugin { } } - //-------------------------------------------------------------------------- - // LOCAL METHODS - //-------------------------------------------------------------------------- - /** * Updates the JavaScript side whenever the connection changes * From 394452aad128530a9e8e9a96cdb22de2dc295bbe Mon Sep 17 00:00:00 2001 From: PVPoyer Date: Mon, 1 Oct 2018 21:14:21 +0200 Subject: [PATCH 2/6] more logging to trigger a new build. --- src/android/NetworkManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/NetworkManager.java b/src/android/NetworkManager.java index 3fc5af1..5e72be7 100755 --- a/src/android/NetworkManager.java +++ b/src/android/NetworkManager.java @@ -184,7 +184,7 @@ public class NetworkManager extends CordovaPlugin { if(noConnectivity) { LOG.d(LOG_TAG, "Really no connectivity"); } else { - LOG.d(LOG_TAG, "!!! Switching to unknown"); + LOG.d(LOG_TAG, "!!! Switching to unknown, Intent states there is a connectivity."); sendUpdate(TYPE_UNKNOWN); } } From 08698002ce3fc8bf80cde4876477893dc523f15e Mon Sep 17 00:00:00 2001 From: PVPoyer Date: Mon, 5 Nov 2018 13:48:50 +0100 Subject: [PATCH 3/6] Try to fix: https://github.com/apache/cordova-plugin-network-information/issues/64: working with the ConnectivityManager.NetworkCallback in case of Lollipop and above --- src/android/NetworkManager.java | 154 ++++++++++++++++++++------------ 1 file changed, 99 insertions(+), 55 deletions(-) diff --git a/src/android/NetworkManager.java b/src/android/NetworkManager.java index 5e72be7..5d04ed4 100755 --- a/src/android/NetworkManager.java +++ b/src/android/NetworkManager.java @@ -28,12 +28,17 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import android.annotation.TargetApi; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; +import android.net.Network; import android.net.NetworkInfo; +import android.net.NetworkRequest; +import android.os.Build; +import android.text.TextUtils; import java.util.Locale; @@ -85,6 +90,7 @@ public class NetworkManager extends CordovaPlugin { ConnectivityManager sockMan; BroadcastReceiver receiver; + private ConnectivityManager.NetworkCallback lollipopAndAboveNetworkCallback; private JSONObject lastInfo = null; /** @@ -99,16 +105,20 @@ public class NetworkManager extends CordovaPlugin { this.sockMan = (ConnectivityManager) cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); this.connectionCallbackContext = null; - this.registerConnectivityActionReceiver(); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + this.registerConnectivityActionReceiver(); + } else { + this.registerLollipopAndAboveNetworkCallback(); + } } /** * Executes the request and returns PluginResult. * - * @param action The action to execute. - * @param args JSONArry of arguments for the plugin. - * @param callbackContext The callback id used when calling back into JavaScript. - * @return True if the action was valid, false otherwise. + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackContext The callback id used when calling back into JavaScript. + * @return True if the action was valid, false otherwise. */ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { if (action.equals("getConnectionInfo")) { @@ -146,42 +156,54 @@ public class NetworkManager extends CordovaPlugin { super.onResume(multitasking); this.unregisterReceiver(); - this.registerConnectivityActionReceiver(); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + this.registerConnectivityActionReceiver(); + } else { + this.registerLollipopAndAboveNetworkCallback(); + } } //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + private void registerLollipopAndAboveNetworkCallback() { + NetworkRequest.Builder builder = new NetworkRequest.Builder(); + + lollipopAndAboveNetworkCallback = new ConnectivityManager.NetworkCallback() { + + @Override + public void onAvailable(Network network) { + updateConnectionInfoIfWebViewNotNull(sockMan.getActiveNetworkInfo()); + } + + @Override + public void onLost(Network network) { + updateConnectionInfoIfWebViewNotNull(sockMan.getActiveNetworkInfo()); + } + }; + sockMan.registerNetworkCallback(builder.build(), lollipopAndAboveNetworkCallback); + } + private void registerConnectivityActionReceiver() { // We need to listen to connectivity events to update navigator.connection IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); + if (this.receiver == null) { this.receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - // (The null check is for the ARM Emulator, please use Intel Emulator for better results) - if (NetworkManager.this.webView != null) { - updateConnectionInfo(sockMan.getActiveNetworkInfo()); - } + updateConnectionInfoIfWebViewNotNull(sockMan.getActiveNetworkInfo()); - String connectionType = null; - if(NetworkManager.this.lastInfo == 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; - } - } + String connectionType = determineCurrentConnectionType(); - if(TYPE_NONE.equals(connectionType)) { + LOG.d(LOG_TAG, "Intent " + intent.getAction()); + if (TYPE_NONE.equals(connectionType)) { boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); LOG.d(LOG_TAG, "Intent no connectivity: " + noConnectivity); - if(noConnectivity) { + if (noConnectivity) { LOG.d(LOG_TAG, "Really no connectivity"); } else { LOG.d(LOG_TAG, "!!! Switching to unknown, Intent states there is a connectivity."); @@ -195,14 +217,39 @@ public class NetworkManager extends CordovaPlugin { webView.getContext().registerReceiver(this.receiver, intentFilter); } - private void unregisterReceiver() { - if (this.receiver != null) { + private String determineCurrentConnectionType() { + String connectionType; + if (NetworkManager.this.lastInfo == null) { + connectionType = TYPE_NONE; + } else { try { - webView.getContext().unregisterReceiver(this.receiver); + connectionType = NetworkManager.this.lastInfo.get("type").toString(); + } catch (JSONException e) { + LOG.d(LOG_TAG, e.getLocalizedMessage()); + connectionType = TYPE_NONE; + } + } + return connectionType; + } + + private void unregisterReceiver() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + if (this.receiver != null) { + try { + webView.getContext().unregisterReceiver(this.receiver); + } catch (Exception e) { + LOG.e(LOG_TAG, "Error unregistering network receiver: " + e.getMessage(), e); + } finally { + receiver = null; + } + } + } else if (this.lollipopAndAboveNetworkCallback != null) { + try { + sockMan.unregisterNetworkCallback(this.lollipopAndAboveNetworkCallback); } catch (Exception e) { LOG.e(LOG_TAG, "Error unregistering network receiver: " + e.getMessage(), e); } finally { - receiver = null; + lollipopAndAboveNetworkCallback = null; } } } @@ -213,12 +260,15 @@ public class NetworkManager extends CordovaPlugin { * @param info the current active network info * @return */ - private void updateConnectionInfo(NetworkInfo info) { + private void updateConnectionInfoIfWebViewNotNull(NetworkInfo info) { + // (The null check is for the ARM Emulator, please use Intel Emulator for better results) + if (NetworkManager.this.webView == null) { + return; + } // send update to javascript "navigator.network.connection" // Jellybean sends its own info JSONObject thisInfo = this.getConnectionInfo(info); - if(!thisInfo.equals(lastInfo)) - { + if (!thisInfo.equals(lastInfo)) { String connectionType = ""; try { connectionType = thisInfo.get("type").toString(); @@ -244,8 +294,7 @@ public class NetworkManager extends CordovaPlugin { // If we are not connected to any network set type to none if (!info.isConnected()) { type = TYPE_NONE; - } - else { + } else { type = getType(info); } extraInfo = info.getExtraInfo(); @@ -294,37 +343,32 @@ public class NetworkManager extends CordovaPlugin { 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)) { + } else if (type.toLowerCase().equals(TYPE_ETHERNET) || type.toLowerCase().startsWith(TYPE_ETHERNET_SHORT)) { 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); if (type.equals(GSM) || - type.equals(GPRS) || - type.equals(EDGE) || - type.equals(TWO_G)) { + type.equals(GPRS) || + type.equals(EDGE) || + type.equals(TWO_G)) { return TYPE_2G; - } - else if (type.startsWith(CDMA) || - type.equals(UMTS) || - type.equals(ONEXRTT) || - type.equals(EHRPD) || - type.equals(HSUPA) || - type.equals(HSDPA) || - type.equals(HSPA) || - type.equals(THREE_G)) { + } else if (type.startsWith(CDMA) || + type.equals(UMTS) || + type.equals(ONEXRTT) || + type.equals(EHRPD) || + type.equals(HSUPA) || + type.equals(HSDPA) || + type.equals(HSPA) || + type.equals(THREE_G)) { return TYPE_3G; - } - else if (type.equals(LTE) || - type.equals(UMB) || - type.equals(HSPA_PLUS) || - type.equals(FOUR_G)) { + } else if (type.equals(LTE) || + type.equals(UMB) || + type.equals(HSPA_PLUS) || + type.equals(FOUR_G)) { return TYPE_4G; } } - } - else { + } else { return TYPE_NONE; } return TYPE_UNKNOWN; From acc02f2981d7fb5f75f28cfe93d038dbfd8a1aee Mon Sep 17 00:00:00 2001 From: PVPoyer Date: Mon, 5 Nov 2018 16:13:11 +0100 Subject: [PATCH 4/6] removed autolayout --- src/android/NetworkManager.java | 53 +++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/src/android/NetworkManager.java b/src/android/NetworkManager.java index 5d04ed4..7c612c9 100755 --- a/src/android/NetworkManager.java +++ b/src/android/NetworkManager.java @@ -115,10 +115,10 @@ public class NetworkManager extends CordovaPlugin { /** * Executes the request and returns PluginResult. * - * @param action The action to execute. - * @param args JSONArry of arguments for the plugin. - * @param callbackContext The callback id used when calling back into JavaScript. - * @return True if the action was valid, false otherwise. + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackContext The callback id used when calling back into JavaScript. + * @return True if the action was valid, false otherwise. */ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { if (action.equals("getConnectionInfo")) { @@ -173,15 +173,15 @@ public class NetworkManager extends CordovaPlugin { lollipopAndAboveNetworkCallback = new ConnectivityManager.NetworkCallback() { - @Override - public void onAvailable(Network network) { - updateConnectionInfoIfWebViewNotNull(sockMan.getActiveNetworkInfo()); - } + @Override + public void onAvailable(Network network) { + updateConnectionInfoIfWebViewNotNull(sockMan.getActiveNetworkInfo()); + } - @Override - public void onLost(Network network) { - updateConnectionInfoIfWebViewNotNull(sockMan.getActiveNetworkInfo()); - } + @Override + public void onLost(Network network) { + updateConnectionInfoIfWebViewNotNull(sockMan.getActiveNetworkInfo()); + } }; sockMan.registerNetworkCallback(builder.build(), lollipopAndAboveNetworkCallback); } @@ -200,10 +200,10 @@ public class NetworkManager extends CordovaPlugin { String connectionType = determineCurrentConnectionType(); LOG.d(LOG_TAG, "Intent " + intent.getAction()); - if (TYPE_NONE.equals(connectionType)) { + if(TYPE_NONE.equals(connectionType)) { boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); LOG.d(LOG_TAG, "Intent no connectivity: " + noConnectivity); - if (noConnectivity) { + if(noConnectivity) { LOG.d(LOG_TAG, "Really no connectivity"); } else { LOG.d(LOG_TAG, "!!! Switching to unknown, Intent states there is a connectivity."); @@ -219,7 +219,7 @@ public class NetworkManager extends CordovaPlugin { private String determineCurrentConnectionType() { String connectionType; - if (NetworkManager.this.lastInfo == null) { + if(NetworkManager.this.lastInfo == null) { connectionType = TYPE_NONE; } else { try { @@ -243,7 +243,7 @@ public class NetworkManager extends CordovaPlugin { receiver = null; } } - } else if (this.lollipopAndAboveNetworkCallback != null) { + } else if(this.lollipopAndAboveNetworkCallback != null) { try { sockMan.unregisterNetworkCallback(this.lollipopAndAboveNetworkCallback); } catch (Exception e) { @@ -268,7 +268,8 @@ public class NetworkManager extends CordovaPlugin { // send update to javascript "navigator.network.connection" // Jellybean sends its own info JSONObject thisInfo = this.getConnectionInfo(info); - if (!thisInfo.equals(lastInfo)) { + if(!thisInfo.equals(lastInfo)) + { String connectionType = ""; try { connectionType = thisInfo.get("type").toString(); @@ -294,7 +295,8 @@ public class NetworkManager extends CordovaPlugin { // If we are not connected to any network set type to none if (!info.isConnected()) { type = TYPE_NONE; - } else { + } + else { type = getType(info); } extraInfo = info.getExtraInfo(); @@ -343,16 +345,19 @@ public class NetworkManager extends CordovaPlugin { 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)) { + } + else if (type.toLowerCase().equals(TYPE_ETHERNET) || type.toLowerCase().startsWith(TYPE_ETHERNET_SHORT)) { 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); if (type.equals(GSM) || type.equals(GPRS) || type.equals(EDGE) || type.equals(TWO_G)) { return TYPE_2G; - } else if (type.startsWith(CDMA) || + } + else if (type.startsWith(CDMA) || type.equals(UMTS) || type.equals(ONEXRTT) || type.equals(EHRPD) || @@ -361,14 +366,16 @@ public class NetworkManager extends CordovaPlugin { type.equals(HSPA) || type.equals(THREE_G)) { return TYPE_3G; - } else if (type.equals(LTE) || + } + else if (type.equals(LTE) || type.equals(UMB) || type.equals(HSPA_PLUS) || type.equals(FOUR_G)) { return TYPE_4G; } } - } else { + } + else { return TYPE_NONE; } return TYPE_UNKNOWN; From 60ab69fe58fee52c8dae44e0cc00b2f4b6e239bd Mon Sep 17 00:00:00 2001 From: PVPoyer Date: Tue, 6 Nov 2018 10:34:50 +0100 Subject: [PATCH 5/6] Try to fix: https://github.com/apache/cordova-plugin-network-information/issues/64: working with the ConnectivityManager.NetworkCallback in case of Lollipop and above, but switching to unknown if type none in the onavailable callback. --- src/android/NetworkManager.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/android/NetworkManager.java b/src/android/NetworkManager.java index 7c612c9..db50de7 100755 --- a/src/android/NetworkManager.java +++ b/src/android/NetworkManager.java @@ -175,12 +175,24 @@ public class NetworkManager extends CordovaPlugin { @Override public void onAvailable(Network network) { + LOG.d(LOG_TAG, "In the on available: "); updateConnectionInfoIfWebViewNotNull(sockMan.getActiveNetworkInfo()); + + String connectionType = determineCurrentConnectionType(); + + if(TYPE_NONE.equals(connectionType)) { + LOG.d(LOG_TAG, "ConnectionType none but in the onAvailable"); + LOG.d(LOG_TAG, "!!! Switching to unknown, onAvailable states there is a connectivity."); + sendUpdate(TYPE_UNKNOWN); + } + LOG.d(LOG_TAG, "End the on available: "); } @Override public void onLost(Network network) { + LOG.d(LOG_TAG, "In the on lost: "); updateConnectionInfoIfWebViewNotNull(sockMan.getActiveNetworkInfo()); + LOG.d(LOG_TAG, "End the on lost: "); } }; sockMan.registerNetworkCallback(builder.build(), lollipopAndAboveNetworkCallback); From 9a45d638ab27049daa7f0731587cc1e8a09fd09b Mon Sep 17 00:00:00 2001 From: PVPoyer Date: Wed, 7 Nov 2018 10:28:18 +0100 Subject: [PATCH 6/6] The originally changes fixed the issue for Android 6+ devices, but broke anything below 6. API level/Android version check added. --- src/android/NetworkManager.java | 107 +++++++------------------------- 1 file changed, 23 insertions(+), 84 deletions(-) diff --git a/src/android/NetworkManager.java b/src/android/NetworkManager.java index db50de7..6e8ecae 100755 --- a/src/android/NetworkManager.java +++ b/src/android/NetworkManager.java @@ -28,17 +28,13 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import android.annotation.TargetApi; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; -import android.net.Network; import android.net.NetworkInfo; -import android.net.NetworkRequest; import android.os.Build; -import android.text.TextUtils; import java.util.Locale; @@ -90,7 +86,6 @@ public class NetworkManager extends CordovaPlugin { ConnectivityManager sockMan; BroadcastReceiver receiver; - private ConnectivityManager.NetworkCallback lollipopAndAboveNetworkCallback; private JSONObject lastInfo = null; /** @@ -105,11 +100,7 @@ public class NetworkManager extends CordovaPlugin { this.sockMan = (ConnectivityManager) cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); this.connectionCallbackContext = null; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - this.registerConnectivityActionReceiver(); - } else { - this.registerLollipopAndAboveNetworkCallback(); - } + this.registerConnectivityActionReceiver(); } /** @@ -156,63 +147,40 @@ public class NetworkManager extends CordovaPlugin { super.onResume(multitasking); this.unregisterReceiver(); - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - this.registerConnectivityActionReceiver(); - } else { - this.registerLollipopAndAboveNetworkCallback(); - } + this.registerConnectivityActionReceiver(); } //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- - @TargetApi(Build.VERSION_CODES.LOLLIPOP) - private void registerLollipopAndAboveNetworkCallback() { - NetworkRequest.Builder builder = new NetworkRequest.Builder(); - - lollipopAndAboveNetworkCallback = new ConnectivityManager.NetworkCallback() { - - @Override - public void onAvailable(Network network) { - LOG.d(LOG_TAG, "In the on available: "); - updateConnectionInfoIfWebViewNotNull(sockMan.getActiveNetworkInfo()); - - String connectionType = determineCurrentConnectionType(); - - if(TYPE_NONE.equals(connectionType)) { - LOG.d(LOG_TAG, "ConnectionType none but in the onAvailable"); - LOG.d(LOG_TAG, "!!! Switching to unknown, onAvailable states there is a connectivity."); - sendUpdate(TYPE_UNKNOWN); - } - LOG.d(LOG_TAG, "End the on available: "); - } - - @Override - public void onLost(Network network) { - LOG.d(LOG_TAG, "In the on lost: "); - updateConnectionInfoIfWebViewNotNull(sockMan.getActiveNetworkInfo()); - LOG.d(LOG_TAG, "End the on lost: "); - } - }; - sockMan.registerNetworkCallback(builder.build(), lollipopAndAboveNetworkCallback); - } - private void registerConnectivityActionReceiver() { // We need to listen to connectivity events to update navigator.connection IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); - if (this.receiver == null) { this.receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - updateConnectionInfoIfWebViewNotNull(sockMan.getActiveNetworkInfo()); + // (The null check is for the ARM Emulator, please use Intel Emulator for better results) + if (NetworkManager.this.webView != null) { + updateConnectionInfo(sockMan.getActiveNetworkInfo()); + } - String connectionType = determineCurrentConnectionType(); + String connectionType = null; + if(NetworkManager.this.lastInfo == 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; + } + } - LOG.d(LOG_TAG, "Intent " + intent.getAction()); - if(TYPE_NONE.equals(connectionType)) { + // Lollipop always returns false for the EXTRA_NO_CONNECTIVITY flag => fix for Android M and above. + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && TYPE_NONE.equals(connectionType)) { boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); LOG.d(LOG_TAG, "Intent no connectivity: " + noConnectivity); if(noConnectivity) { @@ -229,39 +197,14 @@ public class NetworkManager extends CordovaPlugin { webView.getContext().registerReceiver(this.receiver, intentFilter); } - private String determineCurrentConnectionType() { - String connectionType; - if(NetworkManager.this.lastInfo == 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; - } - } - return connectionType; - } - private void unregisterReceiver() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - if (this.receiver != null) { - try { - webView.getContext().unregisterReceiver(this.receiver); - } catch (Exception e) { - LOG.e(LOG_TAG, "Error unregistering network receiver: " + e.getMessage(), e); - } finally { - receiver = null; - } - } - } else if(this.lollipopAndAboveNetworkCallback != null) { + if (this.receiver != null) { try { - sockMan.unregisterNetworkCallback(this.lollipopAndAboveNetworkCallback); + webView.getContext().unregisterReceiver(this.receiver); } catch (Exception e) { LOG.e(LOG_TAG, "Error unregistering network receiver: " + e.getMessage(), e); } finally { - lollipopAndAboveNetworkCallback = null; + receiver = null; } } } @@ -272,11 +215,7 @@ public class NetworkManager extends CordovaPlugin { * @param info the current active network info * @return */ - private void updateConnectionInfoIfWebViewNotNull(NetworkInfo info) { - // (The null check is for the ARM Emulator, please use Intel Emulator for better results) - if (NetworkManager.this.webView == null) { - return; - } + private void updateConnectionInfo(NetworkInfo info) { // send update to javascript "navigator.network.connection" // Jellybean sends its own info JSONObject thisInfo = this.getConnectionInfo(info);