From cafdd673d5c0afe217af3e3bf1a063d47096a4a1 Mon Sep 17 00:00:00 2001 From: PVPoyer Date: Wed, 26 Sep 2018 22:08:58 +0200 Subject: [PATCH] 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 void initialize(CordovaInterface cordova, CordovaWebView webView) { 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 boolean execute(String action, JSONArray args, CallbackContext callbackCo * 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 void onDestroy() { } } - //-------------------------------------------------------------------------- - // LOCAL METHODS - //-------------------------------------------------------------------------- - /** * Updates the JavaScript side whenever the connection changes *