mirror of
https://gitee.com/shuto/cordova-plugin-network-information.git
synced 2025-01-19 05:52:49 +08:00
The originally changes fixed the issue for Android 6+ devices, but broke anything below 6. API level/Android version check added.
This commit is contained in:
parent
60ab69fe58
commit
9a45d638ab
@ -28,17 +28,13 @@ 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.os.Build;
|
||||||
import android.text.TextUtils;
|
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
@ -90,7 +86,6 @@ 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -105,11 +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;
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
this.registerConnectivityActionReceiver();
|
||||||
this.registerConnectivityActionReceiver();
|
|
||||||
} else {
|
|
||||||
this.registerLollipopAndAboveNetworkCallback();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -156,63 +147,40 @@ 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) {
|
|
||||||
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() {
|
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) {
|
||||||
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());
|
// Lollipop always returns false for the EXTRA_NO_CONNECTIVITY flag => fix for Android M and above.
|
||||||
if(TYPE_NONE.equals(connectionType)) {
|
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && 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);
|
||||||
if(noConnectivity) {
|
if(noConnectivity) {
|
||||||
@ -229,39 +197,14 @@ 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 {
|
|
||||||
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 {
|
try {
|
||||||
sockMan.unregisterNetworkCallback(this.lollipopAndAboveNetworkCallback);
|
webView.getContext().unregisterReceiver(this.receiver);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.e(LOG_TAG, "Error unregistering network receiver: " + e.getMessage(), e);
|
LOG.e(LOG_TAG, "Error unregistering network receiver: " + e.getMessage(), e);
|
||||||
} finally {
|
} finally {
|
||||||
lollipopAndAboveNetworkCallback = null;
|
receiver = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -272,11 +215,7 @@ public class NetworkManager extends CordovaPlugin {
|
|||||||
* @param info the current active network info
|
* @param info the current active network info
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private void updateConnectionInfoIfWebViewNotNull(NetworkInfo info) {
|
private void updateConnectionInfo(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);
|
||||||
|
Loading…
Reference in New Issue
Block a user