Skip to content
Snippets Groups Projects
Commit bbebf46f authored by akwizgran's avatar akwizgran
Browse files

Try to provide hotspot and discover peers at the same time.

parent e9bcde52
Branches
No related tags found
No related merge requests found
......@@ -59,49 +59,23 @@ public class MainActivity extends AppCompatActivity {
((MeshApplication) getApplication()).getAppComponent().inject(this);
setContentView(R.layout.activity_main);
Button hotspotButton = findViewById(R.id.hotspotButton);
Button discoveryButton = findViewById(R.id.discoveryButton);
LiveData<Status> hotspotStatus = wifiDirectService.getHotspotStatus();
LiveData<Status> discoveryStatus = wifiDirectService.getDiscoveryStatus();
hotspotStatus.observe(this, status -> {
if (status == STARTING) {
hotspotButton.setText(R.string.start_hotspot);
hotspotButton.setEnabled(false);
discoveryButton.setEnabled(false);
} else if (status == STARTED) {
hotspotButton.setText(R.string.stop_hotspot);
hotspotButton.setEnabled(true);
discoveryButton.setEnabled(false);
} else if (status == STOPPING) {
hotspotButton.setText(R.string.stop_hotspot);
hotspotButton.setEnabled(false);
discoveryButton.setEnabled(false);
} else if (status == STOPPED) {
hotspotButton.setText(R.string.start_hotspot);
hotspotButton.setEnabled(discoveryStatus.getValue() == STOPPED);
discoveryButton.setEnabled(discoveryStatus.getValue() == STOPPED);
}
});
discoveryStatus.observe(this, status -> {
if (status == STARTING) {
discoveryButton.setText(R.string.start_discovery);
discoveryButton.setEnabled(false);
hotspotButton.setEnabled(false);
} else if (status == STARTED) {
discoveryButton.setText(R.string.stop_discovery);
discoveryButton.setEnabled(true);
hotspotButton.setEnabled(false);
} else if (status == STOPPING) {
discoveryButton.setText(R.string.stop_discovery);
discoveryButton.setEnabled(false);
hotspotButton.setEnabled(false);
} else if (status == STOPPED) {
discoveryButton.setText(R.string.start_discovery);
discoveryButton.setEnabled(hotspotStatus.getValue() == STOPPED);
hotspotButton.setEnabled(hotspotStatus.getValue() == STOPPED);
hotspotButton.setEnabled(true);
}
});
hotspotButton.setOnClickListener(v -> onHotspotButtonClicked());
discoveryButton.setOnClickListener(v -> onDiscoveryButtonClicked());
}
@Override
......@@ -173,10 +147,4 @@ public class MainActivity extends AppCompatActivity {
if (status == STARTED) wifiDirectService.stopHotspot();
else if (status == STOPPED) wifiDirectService.startHotspot();
}
private void onDiscoveryButtonClicked() {
Status status = wifiDirectService.getDiscoveryStatus().getValue();
if (status == STARTED) wifiDirectService.stopDiscovery();
else if (status == STOPPED) wifiDirectService.startDiscovery();
}
}
\ No newline at end of file
......@@ -5,18 +5,28 @@ import org.briarproject.publicmesh.nullsafety.NotNullByDefault;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import static java.util.Collections.emptyList;
import static java.util.Collections.list;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.publicmesh.util.LogUtils.logException;
@NotNullByDefault
public class IoUtils {
private static final Logger LOG = getLogger(IoUtils.class.getName());
public static void tryToClose(@Nullable Socket s, Logger logger,
Level level) {
try {
......@@ -63,4 +73,16 @@ public class IoUtils {
ipBytes[3] = (byte) ((ip >> 24) & 0xFF);
return ipBytes;
}
public static List<NetworkInterface> getNetworkInterfaces() {
try {
Enumeration<NetworkInterface> ifaces =
NetworkInterface.getNetworkInterfaces();
// Despite what the docs say, the return value can be null
return ifaces == null ? emptyList() : list(ifaces);
} catch (SocketException e) {
logException(LOG, WARNING, e);
return emptyList();
}
}
}
......@@ -10,12 +10,6 @@ public interface WifiDirectService {
LiveData<Status> getHotspotStatus();
void startDiscovery();
void stopDiscovery();
LiveData<Status> getDiscoveryStatus();
enum Status {
STARTING,
STARTED,
......
......@@ -11,20 +11,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start_hotspot"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintBottom_toTopOf="@id/discoveryButton"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/discoveryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start_discovery"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/hotspotButton" />
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment