Commit c2e428ea authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Yet another attempt to have a working and reliable network manager

parent 9b7dab62
......@@ -169,7 +169,7 @@ public class AndroidPlatformHelper {
public synchronized void onWifiOnlyEnabled(boolean enabled) {
mWifiOnly = enabled;
updateNetworkReachability(null);
updateNetworkReachability();
}
public synchronized Object getPowerManager() {
......@@ -468,7 +468,7 @@ public class AndroidPlatformHelper {
mDnsServers = servers;
}
public synchronized void updateNetworkReachability(Network activeNetwork) {
public synchronized void updateNetworkReachability() {
if (mNativePtr == 0) {
Log.w("[Platform Helper] Native pointer has been reset, stopping there");
return;
......@@ -480,11 +480,6 @@ public class AndroidPlatformHelper {
return;
}
NetworkInfo activeNetworkInfo = activeNetwork == null ? null : mConnectivityManager.getNetworkInfo(activeNetwork);
if (activeNetworkInfo != null) {
Log.i("[Platform Helper] Notified network state " + activeNetworkInfo.getState() + " / " + activeNetworkInfo.getDetailedState() + ", isAvailable() " + activeNetworkInfo.isAvailable());
}
boolean connected = mNetworkManager.isCurrentlyConnected(mContext, mConnectivityManager, mWifiOnly);
if (!connected) {
Log.i("[Platform Helper] No connectivity: setting network unreachable");
......@@ -495,14 +490,14 @@ public class AndroidPlatformHelper {
int port = mNetworkManager.getProxyPort(mContext, mConnectivityManager);
setHttpProxy(mNativePtr, host, port);
if (!mUsingHttpProxy) {
Log.i("[Platform Helper] Proxy wasn't set before, disable network reachability first");
Log.i("[Platform Helper] Proxy wasn't set before, disabling network reachability first");
setNetworkReachable(mNativePtr, false);
}
mUsingHttpProxy = true;
} else {
setHttpProxy(mNativePtr, "", 0);
if (mUsingHttpProxy) {
Log.i("[Platform Helper] Proxy was set before, disable network reachability first");
Log.i("[Platform Helper] Proxy was set before, disabling network reachability first");
setNetworkReachable(mNativePtr, false);
}
mUsingHttpProxy = false;
......@@ -517,25 +512,9 @@ public class AndroidPlatformHelper {
}
Network network = mNetworkManager.getActiveNetwork(mConnectivityManager);
Log.i("[Platform Helper] Active network type is " + networkInfo.getTypeName());
Log.i("[Platform Helper] Active network state " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
Log.i("[Platform Helper] Active network type is " + networkInfo.getTypeName() + ", state " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
if (networkInfo.getState() == NetworkInfo.State.DISCONNECTED && networkInfo.getDetailedState() == NetworkInfo.DetailedState.BLOCKED) {
Log.w("[Platform Helper] Active network is in bad state, try to use the one notified by the system");
if (activeNetwork != null) {
if (activeNetworkInfo.isAvailable() && !(activeNetworkInfo.getState() == NetworkInfo.State.DISCONNECTED && activeNetworkInfo.getDetailedState() == NetworkInfo.DetailedState.BLOCKED)) {
Log.i("[Platform Helper] Using notified network instead");
networkInfo = activeNetworkInfo;
network = activeNetwork;
} else {
Log.e("[Platform Helper] Notified network seems in bad state as well, set reachability to false");
setNetworkReachable(mNativePtr, false);
return;
}
} else {
Log.e("[Platform Helper] No network was passed to this updateNetworkReachability call, set reachability to false");
setNetworkReachable(mNativePtr, false);
return;
}
Log.w("[Platform Helper] Active network is in bad state...");
}
// Update DNS servers lists
......@@ -543,11 +522,12 @@ public class AndroidPlatformHelper {
int currentNetworkType = networkInfo.getType();
if (mLastNetworkType != -1 && mLastNetworkType != currentNetworkType) {
Log.i("[Platform Helper] Network type has changed (last one was " + networkTypeToString(mLastNetworkType) + "), disable network reachability first");
Log.i("[Platform Helper] Network type has changed (last one was " + networkTypeToString(mLastNetworkType) + "), disabling network reachability first");
setNetworkReachable(mNativePtr, false);
}
mLastNetworkType = currentNetworkType;
Log.i("[Platform Helper] Network reachability enabled");
setNetworkReachable(mNativePtr, true);
}
}
......@@ -595,7 +575,7 @@ public class AndroidPlatformHelper {
Log.i("[Platform Helper] Registering interactivity receiver");
mContext.registerReceiver(mInteractivityReceiver, mInteractivityIntentFilter);
updateNetworkReachability(null);
updateNetworkReachability();
}
private synchronized void stopNetworkMonitoring() {
......
......@@ -44,7 +44,7 @@ public class DozeReceiver extends android.content.BroadcastReceiver {
Log.i("[Platform Helper] Doze mode enabled: " + dozeM);
if (mHelper != null) {
mHelper.setDozeModeEnabled(dozeM);
mHelper.updateNetworkReachability(null);
mHelper.updateNetworkReachability();
}
}
}
......
......@@ -46,7 +46,7 @@ public class NetworkManager extends BroadcastReceiver implements NetworkManagerI
mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Log.i("[Platform Helper] [Network Manager] Broadcast receiver called");
if (mHelper != null) {
mHelper.updateNetworkReachability(null);
mHelper.updateNetworkReachability();
}
}
......@@ -71,9 +71,9 @@ public class NetworkManager extends BroadcastReceiver implements NetworkManagerI
NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo();
boolean connected = false;
for (NetworkInfo networkInfo : networkInfos) {
Log.i("[Platform Helper] [Network Manager] Found network type: " + networkInfo.getTypeName() + ", isAvailable() = " + networkInfo.isAvailable() + ", isConnected() = " + networkInfo.isConnected());
if (networkInfo.isAvailable()) {
Log.i("[Platform Helper] [Network Manager] Network is available, state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
Log.i("[Platform Helper] [Network Manager] Found network type: " + networkInfo.getTypeName() + ", isConnectedOrConnecting() = " + networkInfo.isConnectedOrConnecting());
if (networkInfo.isConnectedOrConnecting()) {
Log.i("[Platform Helper] [Network Manager] Network state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
if (networkInfo.getType() != ConnectivityManager.TYPE_WIFI && wifiOnly) {
Log.i("[Platform Helper] [Network Manager] Wifi only mode enabled, skipping");
} else {
......
......@@ -44,19 +44,19 @@ public class NetworkManagerAbove21 implements NetworkManagerInterface {
@Override
public void onAvailable(Network network) {
Log.i("[Platform Helper] [Network Manager 21] A network is available");
mHelper.updateNetworkReachability(network);
mHelper.updateNetworkReachability();
}
@Override
public void onLost(Network network) {
Log.i("[Platform Helper] [Network Manager 21] A network has been lost");
mHelper.updateNetworkReachability(null);
mHelper.updateNetworkReachability();
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
Log.i("[Platform Helper] [Network Manager 21] onCapabilitiesChanged " + network.toString() + ", " + networkCapabilities.toString());
mHelper.updateNetworkReachability(network);
mHelper.updateNetworkReachability();
}
@Override
......@@ -100,9 +100,9 @@ public class NetworkManagerAbove21 implements NetworkManagerInterface {
boolean connected = false;
for (Network network : networks) {
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
Log.i("[Platform Helper] [Network Manager 21] Found network type: " + networkInfo.getTypeName() + ", isAvailable() = " + networkInfo.isAvailable() + ", isConnected() = " + networkInfo.isConnected());
if (networkInfo.isAvailable()) {
Log.i("[Platform Helper] [Network Manager 21] Network is available, state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
Log.i("[Platform Helper] [Network Manager 21] Found network type: " + networkInfo.getTypeName() + ", isConnectedOrConnecting() = " + networkInfo.isConnectedOrConnecting());
if (networkInfo.isConnectedOrConnecting()) {
Log.i("[Platform Helper] [Network Manager 21] Network state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
if (networkInfo.getType() != ConnectivityManager.TYPE_WIFI && wifiOnly) {
Log.i("[Platform Helper] [Network Manager 21] Wifi only mode enabled, skipping");
} else {
......
......@@ -44,19 +44,19 @@ public class NetworkManagerAbove23 implements NetworkManagerInterface {
@Override
public void onAvailable(Network network) {
Log.i("[Platform Helper] [Network Manager 23] A network is available");
mHelper.updateNetworkReachability(network);
mHelper.updateNetworkReachability();
}
@Override
public void onLost(Network network) {
Log.i("[Platform Helper] [Network Manager 23] A network has been lost");
mHelper.updateNetworkReachability(null);
mHelper.updateNetworkReachability();
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
Log.i("[Platform Helper] [Network Manager 23] onCapabilitiesChanged " + network.toString() + ", " + networkCapabilities.toString());
mHelper.updateNetworkReachability(network);
mHelper.updateNetworkReachability();
}
@Override
......@@ -105,9 +105,9 @@ public class NetworkManagerAbove23 implements NetworkManagerInterface {
boolean connected = false;
for (Network network : networks) {
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
Log.i("[Platform Helper] [Network Manager 23] Found network type: " + networkInfo.getTypeName() + ", isAvailable() = " + networkInfo.isAvailable() + ", isConnected() = " + networkInfo.isConnected());
if (networkInfo.isAvailable()) {
Log.i("[Platform Helper] [Network Manager 23] Network is available, state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
Log.i("[Platform Helper] [Network Manager 23] Found network type: " + networkInfo.getTypeName() + ", isConnectedOrConnecting() = " + networkInfo.isConnectedOrConnecting());
if (networkInfo.isConnectedOrConnecting()) {
Log.i("[Platform Helper] [Network Manager 23] Network state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
if (networkInfo.getType() != ConnectivityManager.TYPE_WIFI && wifiOnly) {
Log.i("[Platform Helper] [Network Manager 23] Wifi only mode enabled, skipping");
} else {
......
......@@ -47,19 +47,19 @@ public class NetworkManagerAbove24 implements NetworkManagerInterface {
@Override
public void onAvailable(Network network) {
Log.i("[Platform Helper] [Network Manager 24] A network is available");
mHelper.updateNetworkReachability(network);
mHelper.updateNetworkReachability();
}
@Override
public void onLost(Network network) {
Log.i("[Platform Helper] [Network Manager 24] A network has been lost");
mHelper.updateNetworkReachability(null);
mHelper.updateNetworkReachability();
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
Log.i("[Platform Helper] [Network Manager 24] onCapabilitiesChanged " + network.toString() + ", " + networkCapabilities.toString());
mHelper.updateNetworkReachability(network);
mHelper.updateNetworkReachability();
}
@Override
......@@ -120,9 +120,9 @@ public class NetworkManagerAbove24 implements NetworkManagerInterface {
boolean connected = false;
for (Network network : networks) {
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
Log.i("[Platform Helper] [Network Manager 24] Found network type: " + networkInfo.getTypeName() + ", isAvailable() = " + networkInfo.isAvailable() + ", isConnected() = " + networkInfo.isConnected());
if (networkInfo.isAvailable()) {
Log.i("[Platform Helper] [Network Manager 24] Network is available, state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
Log.i("[Platform Helper] [Network Manager 24] Found network type: " + networkInfo.getTypeName() + ", isConnectedOrConnecting() = " + networkInfo.isConnectedOrConnecting());
if (networkInfo.isConnectedOrConnecting()) {
Log.i("[Platform Helper] [Network Manager 24] Network is state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
if (networkInfo.getType() != ConnectivityManager.TYPE_WIFI && wifiOnly) {
Log.i("[Platform Helper] [Network Manager 24] Wifi only mode enabled, skipping");
} else {
......
......@@ -47,19 +47,19 @@ public class NetworkManagerAbove26 implements NetworkManagerInterface {
@Override
public void onAvailable(Network network) {
Log.i("[Platform Helper] [Network Manager 26] A network is available");
mHelper.updateNetworkReachability(network);
mHelper.updateNetworkReachability();
}
@Override
public void onLost(Network network) {
Log.i("[Platform Helper] [Network Manager 26] A network has been lost");
mHelper.updateNetworkReachability(null);
mHelper.updateNetworkReachability();
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
Log.i("[Platform Helper] [Network Manager 26] onCapabilitiesChanged " + network.toString() + ", " + networkCapabilities.toString());
mHelper.updateNetworkReachability(network);
mHelper.updateNetworkReachability();
}
@Override
......@@ -120,9 +120,9 @@ public class NetworkManagerAbove26 implements NetworkManagerInterface {
boolean connected = false;
for (Network network : networks) {
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
Log.i("[Platform Helper] [Network Manager 26] Found network type: " + networkInfo.getTypeName() + ", isAvailable() = " + networkInfo.isAvailable() + ", isConnected() = " + networkInfo.isConnected());
if (networkInfo.isAvailable()) {
Log.i("[Platform Helper] [Network Manager 26] Network is available, state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
Log.i("[Platform Helper] [Network Manager 26] Found network type: " + networkInfo.getTypeName() + ", isConnectedOrConnecting() = " + networkInfo.isConnectedOrConnecting());
if (networkInfo.isConnectedOrConnecting()) {
Log.i("[Platform Helper] [Network Manager 26] Network is state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
if (networkInfo.getType() != ConnectivityManager.TYPE_WIFI && wifiOnly) {
Log.i("[Platform Helper] [Network Manager 26] Wifi only mode enabled, skipping");
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment