greatly improved application stability when connecting to a Wi-Fi direct server
This commit is contained in:
parent
c6b11effe3
commit
229cf816e3
3 changed files with 41 additions and 7 deletions
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<TaskClient?>,
|
||||
bwdManager: BwdManager,
|
||||
) {
|
||||
val selectedDevice = remember { mutableStateOf<WifiP2pDevice?>(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
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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<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 (peers != null) {
|
||||
// for every device in the list
|
||||
|
|
Loading…
Reference in a new issue