mirror of
https://gitee.com/shuto/cordova-plugin-network-information.git
synced 2025-01-18 21:52:48 +08:00
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:
commit
db0d4b54ff
@ -34,6 +34,7 @@ import android.content.Intent;
|
||||
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 class NetworkManager extends CordovaPlugin {
|
||||
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 class NetworkManager extends CordovaPlugin {
|
||||
* 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 class NetworkManager extends CordovaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// LOCAL METHODS
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* 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)) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user