From 229cf816e3ecb99b3565aaf851290896cfcbcc7c Mon Sep 17 00:00:00 2001 From: Faraphel Date: Sun, 30 Jun 2024 11:44:48 +0200 Subject: [PATCH] greatly improved application stability when connecting to a Wi-Fi direct server --- .../communication/authentication/client.kt | 5 ++++ .../connection/wifiP2p/role/client.kt | 18 ++++++++++--- .../connectivity/WifiP2pDeviceListWidget.kt | 25 ++++++++++++++++--- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/faraphel/tasks_valider/ui/screen/communication/authentication/client.kt b/app/src/main/java/com/faraphel/tasks_valider/ui/screen/communication/authentication/client.kt index 8859f3d..7c49317 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/ui/screen/communication/authentication/client.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/ui/screen/communication/authentication/client.kt @@ -25,6 +25,7 @@ import com.faraphel.tasks_valider.database.entities.error.HttpException import com.faraphel.tasks_valider.ui.screen.scan.qr.ScanBarcodeScreen import com.journeyapps.barcodescanner.BarcodeResult import okhttp3.HttpUrl.Companion.toHttpUrl +import java.net.ConnectException /** @@ -142,5 +143,9 @@ fun authenticate( } } } + } catch (exception: ConnectException) { + activity.runOnUiThread { + Toast.makeText(activity, "Could not connect to the server: $exception", Toast.LENGTH_SHORT).show() + } } } diff --git a/app/src/main/java/com/faraphel/tasks_valider/ui/screen/communication/connection/wifiP2p/role/client.kt b/app/src/main/java/com/faraphel/tasks_valider/ui/screen/communication/connection/wifiP2p/role/client.kt index 90d9c38..a823f55 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/ui/screen/communication/connection/wifiP2p/role/client.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/ui/screen/communication/connection/wifiP2p/role/client.kt @@ -3,6 +3,7 @@ package com.faraphel.tasks_valider.ui.screen.communication.connection.wifiP2p.ro import android.app.Activity import android.net.wifi.p2p.WifiP2pConfig import android.net.wifi.p2p.WifiP2pDevice +import android.widget.Toast import androidx.compose.foundation.layout.Column import androidx.compose.runtime.* import com.faraphel.tasks_valider.connectivity.bwd.BwdManager @@ -10,9 +11,7 @@ import com.faraphel.tasks_valider.connectivity.task.TaskClient import com.faraphel.tasks_valider.connectivity.task.session.TaskSession import com.faraphel.tasks_valider.ui.screen.communication.DEFAULT_SERVER_PORT import com.faraphel.tasks_valider.ui.screen.communication.authentication.AuthenticationClientScreen -import com.faraphel.tasks_valider.ui.screen.communication.connection.internet.role.CommunicationInternetClientContent import com.faraphel.tasks_valider.ui.screen.task.TaskSessionController -import com.faraphel.tasks_valider.ui.screen.task.TaskSessionScreen import com.faraphel.tasks_valider.ui.widgets.connectivity.WifiP2pDeviceListWidget @@ -23,7 +22,7 @@ fun CommunicationWifiP2pClientScreen(activity: Activity, bwdManager: BwdManager) // if the client is not connected, show the connection screen if (client.value == null) - return CommunicationWifiP2pClientContent(client, bwdManager) + return CommunicationWifiP2pClientContent(activity, client, bwdManager) // if the user is not authenticated, show the authentication screen if (session.value == null) return AuthenticationClientScreen(activity, client.value!!, session) @@ -39,11 +38,13 @@ fun CommunicationWifiP2pClientScreen(activity: Activity, bwdManager: BwdManager) @Composable fun CommunicationWifiP2pClientContent( + activity: Activity, client: MutableState, bwdManager: BwdManager, ) { val selectedDevice = remember { mutableStateOf(null) } + // if the device is not selected if (selectedDevice.value == null) { Column { WifiP2pDeviceListWidget( @@ -66,11 +67,20 @@ fun CommunicationWifiP2pClientContent( } bwdManager.connect(config) { bwdManager.requestConnectionInfo { connectionInfo -> + // if the group has not been formed correctly, the host did not approve the connection + if (!connectionInfo.groupFormed) { + selectedDevice.value = null + Toast.makeText(activity, "Require host approval.", Toast.LENGTH_LONG).show() + return@requestConnectionInfo + } + + // create a connection to the server // TODO(Faraphel): check if the server is reachable client.value = TaskClient( - connectionInfo.groupOwnerAddress.toString(), + connectionInfo.groupOwnerAddress.hostAddress!!, DEFAULT_SERVER_PORT ) } } + } \ No newline at end of file diff --git a/app/src/main/java/com/faraphel/tasks_valider/ui/widgets/connectivity/WifiP2pDeviceListWidget.kt b/app/src/main/java/com/faraphel/tasks_valider/ui/widgets/connectivity/WifiP2pDeviceListWidget.kt index 9c09b82..9bf4826 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/ui/widgets/connectivity/WifiP2pDeviceListWidget.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/ui/widgets/connectivity/WifiP2pDeviceListWidget.kt @@ -2,10 +2,14 @@ package com.faraphel.tasks_valider.ui.widgets.connectivity import android.net.wifi.p2p.WifiP2pDevice import android.net.wifi.p2p.WifiP2pDeviceList -import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.* import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.MutableState +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp /** @@ -20,9 +24,24 @@ fun WifiP2pDeviceListWidget( filter: ((WifiP2pDevice) -> Boolean)? = null, deviceState: MutableState? = null, ) { - Text(text = "Devices (${peers?.deviceList?.size ?: 0})") + Column( + modifier = Modifier.fillMaxSize(), + horizontalAlignment = Alignment.CenterHorizontally + ) { + // title + Text( + text = "Devices", + fontSize = 32.sp, + ) + // found device count + Text( + text = "(${peers?.deviceList?.size ?: 0})", + fontSize = 24.sp, + ) + + // separator + Spacer(modifier = Modifier.height(24.dp)) - Column { // if there are peers to display if (peers != null) { // for every device in the list