Use WifiManager to get wifi network information.

This ensures we bind to the wifi interface even if it doesn't have internet access and there's another interface with internet access (e.g. mobile data).
This commit is contained in:
akwizgran
2018-03-26 13:58:10 +01:00
parent 235183a3af
commit e0a1fa559d

View File

@@ -4,21 +4,25 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.Backoff;
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginCallback;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import static android.content.Context.CONNECTIVITY_SERVICE;
import static android.content.Context.WIFI_SERVICE;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
import static android.net.ConnectivityManager.TYPE_WIFI;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
@NotNullByDefault
class AndroidLanTcpPlugin extends LanTcpPlugin {
@@ -27,6 +31,8 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
Logger.getLogger(AndroidLanTcpPlugin.class.getName());
private final Context appContext;
@Nullable
private final WifiManager wifiManager;
@Nullable
private volatile BroadcastReceiver networkStateReceiver = null;
@@ -36,6 +42,8 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
int maxIdleTime) {
super(ioExecutor, backoff, callback, maxLatency, maxIdleTime);
this.appContext = appContext;
wifiManager = (WifiManager) appContext.getApplicationContext()
.getSystemService(WIFI_SERVICE);
}
@Override
@@ -56,20 +64,40 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
tryToClose(socket);
}
@Override
protected Collection<InetAddress> getLocalIpAddresses() {
if (wifiManager == null) return emptyList();
WifiInfo info = wifiManager.getConnectionInfo();
if (info == null || info.getIpAddress() == 0) return emptyList();
return singletonList(intToInetAddress(info.getIpAddress()));
}
private InetAddress intToInetAddress(int ip) {
byte[] ipBytes = new byte[4];
ipBytes[0] = (byte) (ip & 0xFF);
ipBytes[1] = (byte) ((ip >> 8) & 0xFF);
ipBytes[2] = (byte) ((ip >> 16) & 0xFF);
ipBytes[3] = (byte) ((ip >> 24) & 0xFF);
try {
return InetAddress.getByAddress(ipBytes);
} catch (UnknownHostException e) {
// Should only be thrown if address has illegal length
throw new AssertionError(e);
}
}
private class NetworkStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent i) {
if (!running) return;
Object o = ctx.getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) o;
NetworkInfo net = cm.getActiveNetworkInfo();
if (net != null && net.getType() == TYPE_WIFI && net.isConnected()) {
LOG.info("Connected to Wi-Fi");
if (socket == null || socket.isClosed()) bind();
} else {
LOG.info("Not connected to Wi-Fi");
if (!running || wifiManager == null) return;
WifiInfo info = wifiManager.getConnectionInfo();
if (info == null || info.getIpAddress() == 0) {
LOG.info("Not connected to wifi");
tryToClose(socket);
} else {
LOG.info("Connected to wifi");
if (socket == null || socket.isClosed()) bind();
}
}
}