Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-12035 (android) Fix bug [cordova-plugin-network-information] connection info is not reliable on Android 6 #74

Merged
merged 6 commits into from
Apr 6, 2019
111 changes: 79 additions & 32 deletions src/android/NetworkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -158,10 +209,6 @@ public void onDestroy() {
}
}

//--------------------------------------------------------------------------
// LOCAL METHODS
//--------------------------------------------------------------------------

/**
* Updates the JavaScript side whenever the connection changes
*
Expand Down Expand Up @@ -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;
}
}
Expand Down