Merge pull request #74 from PieterVanPoyer/master

CB-12035 (android) Fix bug [cordova-plugin-network-information] connection info is not reliable on Android 6
This commit is contained in:
Jesse MacFadyen 2019-04-05 17:06:54 -07:00 committed by GitHub
commit db0d4b54ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,6 +34,7 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.Build;
import java.util.Locale; import java.util.Locale;
@ -99,21 +100,7 @@ public class NetworkManager extends CordovaPlugin {
this.sockMan = (ConnectivityManager) cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); this.sockMan = (ConnectivityManager) cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
this.connectionCallbackContext = null; this.connectionCallbackContext = null;
// We need to listen to connectivity events to update navigator.connection this.registerConnectivityActionReceiver();
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);
}
} }
/** /**
@ -147,6 +134,70 @@ public class NetworkManager extends CordovaPlugin {
* Stop network receiver. * Stop network receiver.
*/ */
public void onDestroy() { 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) { if (this.receiver != null) {
try { try {
webView.getContext().unregisterReceiver(this.receiver); webView.getContext().unregisterReceiver(this.receiver);
@ -158,10 +209,6 @@ public class NetworkManager extends CordovaPlugin {
} }
} }
//--------------------------------------------------------------------------
// LOCAL METHODS
//--------------------------------------------------------------------------
/** /**
* Updates the JavaScript side whenever the connection changes * Updates the JavaScript side whenever the connection changes
* *
@ -256,25 +303,25 @@ public class NetworkManager extends CordovaPlugin {
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) ||
type.equals(HSUPA) || type.equals(HSUPA) ||
type.equals(HSDPA) || type.equals(HSDPA) ||
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;
} }
} }