Implemented Connection with Wi-Fi Direct and IP #9

Merged
faraphel merged 7 commits from connection into main 2024-06-30 18:01:26 +02:00
3 changed files with 41 additions and 7 deletions
Showing only changes of commit 229cf816e3 - Show all commits

View file

@ -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.faraphel.tasks_valider.ui.screen.scan.qr.ScanBarcodeScreen
import com.journeyapps.barcodescanner.BarcodeResult import com.journeyapps.barcodescanner.BarcodeResult
import okhttp3.HttpUrl.Companion.toHttpUrl 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()
}
} }
} }

View file

@ -3,6 +3,7 @@ package com.faraphel.tasks_valider.ui.screen.communication.connection.wifiP2p.ro
import android.app.Activity import android.app.Activity
import android.net.wifi.p2p.WifiP2pConfig import android.net.wifi.p2p.WifiP2pConfig
import android.net.wifi.p2p.WifiP2pDevice import android.net.wifi.p2p.WifiP2pDevice
import android.widget.Toast
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.* import androidx.compose.runtime.*
import com.faraphel.tasks_valider.connectivity.bwd.BwdManager 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.connectivity.task.session.TaskSession
import com.faraphel.tasks_valider.ui.screen.communication.DEFAULT_SERVER_PORT 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.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.TaskSessionController
import com.faraphel.tasks_valider.ui.screen.task.TaskSessionScreen
import com.faraphel.tasks_valider.ui.widgets.connectivity.WifiP2pDeviceListWidget 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 the client is not connected, show the connection screen
if (client.value == null) if (client.value == null)
return CommunicationWifiP2pClientContent(client, bwdManager) return CommunicationWifiP2pClientContent(activity, client, bwdManager)
// if the user is not authenticated, show the authentication screen // if the user is not authenticated, show the authentication screen
if (session.value == null) if (session.value == null)
return AuthenticationClientScreen(activity, client.value!!, session) return AuthenticationClientScreen(activity, client.value!!, session)
@ -39,11 +38,13 @@ fun CommunicationWifiP2pClientScreen(activity: Activity, bwdManager: BwdManager)
@Composable @Composable
fun CommunicationWifiP2pClientContent( fun CommunicationWifiP2pClientContent(
activity: Activity,
client: MutableState<TaskClient?>, client: MutableState<TaskClient?>,
bwdManager: BwdManager, bwdManager: BwdManager,
) { ) {
val selectedDevice = remember { mutableStateOf<WifiP2pDevice?>(null) } val selectedDevice = remember { mutableStateOf<WifiP2pDevice?>(null) }
// if the device is not selected
if (selectedDevice.value == null) { if (selectedDevice.value == null) {
Column { Column {
WifiP2pDeviceListWidget( WifiP2pDeviceListWidget(
@ -66,11 +67,20 @@ fun CommunicationWifiP2pClientContent(
} }
bwdManager.connect(config) { bwdManager.connect(config) {
bwdManager.requestConnectionInfo { connectionInfo -> 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 // TODO(Faraphel): check if the server is reachable
client.value = TaskClient( client.value = TaskClient(
connectionInfo.groupOwnerAddress.toString(), connectionInfo.groupOwnerAddress.hostAddress!!,
DEFAULT_SERVER_PORT DEFAULT_SERVER_PORT
) )
} }
} }
} }

View file

@ -2,10 +2,14 @@ package com.faraphel.tasks_valider.ui.widgets.connectivity
import android.net.wifi.p2p.WifiP2pDevice import android.net.wifi.p2p.WifiP2pDevice
import android.net.wifi.p2p.WifiP2pDeviceList 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.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState 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, filter: ((WifiP2pDevice) -> Boolean)? = null,
deviceState: MutableState<WifiP2pDevice?>? = null, deviceState: MutableState<WifiP2pDevice?>? = 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 there are peers to display
if (peers != null) { if (peers != null) {
// for every device in the list // for every device in the list