Try to fix: https://github.com/apache/cordova-plugin-network-information/issues/64: working with the ConnectivityManager.NetworkCallback in case of Lollipop and above

This commit is contained in:
PVPoyer 2018-11-05 13:48:50 +01:00
parent 394452aad1
commit 08698002ce

View File

@ -28,12 +28,17 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import android.annotation.TargetApi;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.os.Build;
import android.text.TextUtils;
import java.util.Locale; import java.util.Locale;
@ -85,6 +90,7 @@ public class NetworkManager extends CordovaPlugin {
ConnectivityManager sockMan; ConnectivityManager sockMan;
BroadcastReceiver receiver; BroadcastReceiver receiver;
private ConnectivityManager.NetworkCallback lollipopAndAboveNetworkCallback;
private JSONObject lastInfo = null; private JSONObject lastInfo = null;
/** /**
@ -99,7 +105,11 @@ 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;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
this.registerConnectivityActionReceiver(); this.registerConnectivityActionReceiver();
} else {
this.registerLollipopAndAboveNetworkCallback();
}
} }
/** /**
@ -146,38 +156,50 @@ public class NetworkManager extends CordovaPlugin {
super.onResume(multitasking); super.onResume(multitasking);
this.unregisterReceiver(); this.unregisterReceiver();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
this.registerConnectivityActionReceiver(); this.registerConnectivityActionReceiver();
} else {
this.registerLollipopAndAboveNetworkCallback();
}
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// LOCAL METHODS // 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() { private void registerConnectivityActionReceiver() {
// We need to listen to connectivity events to update navigator.connection // We need to listen to connectivity events to update navigator.connection
IntentFilter intentFilter = new IntentFilter(); IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
if (this.receiver == null) { if (this.receiver == null) {
this.receiver = new BroadcastReceiver() { this.receiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
// (The null check is for the ARM Emulator, please use Intel Emulator for better results) updateConnectionInfoIfWebViewNotNull(sockMan.getActiveNetworkInfo());
if (NetworkManager.this.webView != null) {
updateConnectionInfo(sockMan.getActiveNetworkInfo());
}
String connectionType = null; String connectionType = determineCurrentConnectionType();
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)) { if (TYPE_NONE.equals(connectionType)) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
LOG.d(LOG_TAG, "Intent no connectivity: " + noConnectivity); LOG.d(LOG_TAG, "Intent no connectivity: " + noConnectivity);
@ -195,7 +217,23 @@ public class NetworkManager extends CordovaPlugin {
webView.getContext().registerReceiver(this.receiver, intentFilter); 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() { private void unregisterReceiver() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
if (this.receiver != null) { if (this.receiver != null) {
try { try {
webView.getContext().unregisterReceiver(this.receiver); webView.getContext().unregisterReceiver(this.receiver);
@ -205,6 +243,15 @@ public class NetworkManager extends CordovaPlugin {
receiver = null; 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 {
lollipopAndAboveNetworkCallback = null;
}
}
} }
/** /**
@ -213,12 +260,15 @@ public class NetworkManager extends CordovaPlugin {
* @param info the current active network info * @param info the current active network info
* @return * @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" // send update to javascript "navigator.network.connection"
// Jellybean sends its own info // Jellybean sends its own info
JSONObject thisInfo = this.getConnectionInfo(info); JSONObject thisInfo = this.getConnectionInfo(info);
if(!thisInfo.equals(lastInfo)) if (!thisInfo.equals(lastInfo)) {
{
String connectionType = ""; String connectionType = "";
try { try {
connectionType = thisInfo.get("type").toString(); 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 we are not connected to any network set type to none
if (!info.isConnected()) { if (!info.isConnected()) {
type = TYPE_NONE; type = TYPE_NONE;
} } else {
else {
type = getType(info); type = getType(info);
} }
extraInfo = info.getExtraInfo(); extraInfo = info.getExtraInfo();
@ -294,19 +343,16 @@ public class NetworkManager extends CordovaPlugin {
LOG.d(LOG_TAG, "wifi : " + WIFI); 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) ||
@ -315,16 +361,14 @@ 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 {
else {
return TYPE_NONE; return TYPE_NONE;
} }
return TYPE_UNKNOWN; return TYPE_UNKNOWN;