diff --git a/src/android/NetworkManager.java b/src/android/NetworkManager.java index 614b6e7..6e8ecae 100755 --- a/src/android/NetworkManager.java +++ b/src/android/NetworkManager.java @@ -34,6 +34,7 @@ Licensed to the Apache Software Foundation (ASF) under one import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; +import android.os.Build; import java.util.Locale; @@ -99,21 +100,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 +134,70 @@ 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; + } + } + + // 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) { + LOG.d(LOG_TAG, "Really no connectivity"); + } else { + LOG.d(LOG_TAG, "!!! Switching to unknown, Intent states there is a connectivity."); + sendUpdate(TYPE_UNKNOWN); + } + } + } + }; + } + + webView.getContext().registerReceiver(this.receiver, intentFilter); + } + + private void unregisterReceiver() { if (this.receiver != null) { try { webView.getContext().unregisterReceiver(this.receiver); @@ -158,10 +209,6 @@ public void onDestroy() { } } - //-------------------------------------------------------------------------- - // LOCAL METHODS - //-------------------------------------------------------------------------- - /** * Updates the JavaScript side whenever the connection changes * @@ -256,25 +303,25 @@ else if (type.toLowerCase().equals(TYPE_ETHERNET) || type.toLowerCase().startsWi 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)) { + 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)) { + type.equals(UMB) || + type.equals(HSPA_PLUS) || + type.equals(FOUR_G)) { return TYPE_4G; } }