transfer wip

This commit is contained in:
tom5079
2024-06-30 13:40:57 -07:00
parent 9f103dcffe
commit 290b7fb158
4 changed files with 54 additions and 38 deletions

View File

@@ -31,9 +31,11 @@ class WifiDirectBroadcastReceiver(
when (intent?.action) {
WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION -> {
val state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1)
Log.d("PUPILD", "Wifi P2P state changed: $state")
viewModel.setWifiP2pEnabled(state == WifiP2pManager.WIFI_P2P_STATE_ENABLED)
}
WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION -> {
Log.d("PUPILD", "Wifi P2P peers changed")
manager.requestPeers(channel) { peers ->
viewModel.setPeers(peers)
}
@@ -42,6 +44,8 @@ class WifiDirectBroadcastReceiver(
// Respond to new connection or disconnections
val networkInfo = intent.getParcelableExtraCompat<android.net.NetworkInfo>(WifiP2pManager.EXTRA_NETWORK_INFO)
Log.d("PUPILD", "Wifi P2P connection changed: $networkInfo ${networkInfo?.isConnected}")
if (networkInfo?.isConnected == true) {
manager.requestConnectionInfo(channel) { info ->
viewModel.setConnectionInfo(info)
@@ -52,6 +56,7 @@ class WifiDirectBroadcastReceiver(
}
WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION -> {
// Respond to this device's wifi state changing
Log.d("PUPILD", "Wifi P2P this device changed")
viewModel.setThisDevice(intent.getParcelableExtraCompat(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE))
}
}

View File

@@ -13,6 +13,7 @@ import io.ktor.network.sockets.aSocket
import io.ktor.network.sockets.openReadChannel
import io.ktor.network.sockets.openWriteChannel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
@@ -89,6 +90,7 @@ class TransferClientService : Service() {
}
}.onFailure {
Log.d("PUPILD", "Connection closed with error $it")
channel.close()
socket.close()
stopSelf(startId)
}
@@ -105,12 +107,18 @@ class TransferClientService : Service() {
inner class Binder: android.os.Binder() {
@OptIn(DelicateCoroutinesApi::class)
suspend fun sendPacket(packet: TransferPacket): Result<TransferPacket.ListResponse> = runCatching {
val response = withTimeout(1000) { suspendCoroutine { continuation ->
channel.trySendBlocking(packet to continuation)
} }
check(job != null) { "Service not running" }
check(!channel.isClosedForSend) { "Service not running" }
response as? TransferPacket.ListResponse ?: throw IllegalStateException("Invalid response")
val response = suspendCoroutine { continuation ->
check (channel.trySend(packet to continuation).isSuccess) { "Service not running" }
}
check (response is TransferPacket.ListResponse) { "Invalid response" }
response
}
}

View File

@@ -129,6 +129,32 @@ class TransferActivity : AppCompatActivity(R.layout.transfer_activity) {
}
}
private suspend fun WifiP2pManager.disconnect() {
suspendCoroutine { continuation ->
removeGroup(channel, object : WifiP2pManager.ActionListener {
override fun onSuccess() {
continuation.resume(Unit)
}
override fun onFailure(reason: Int) {
continuation.resume(Unit)
}
})
}
suspendCoroutine { continuation ->
cancelConnect(channel, object: WifiP2pManager.ActionListener {
override fun onSuccess() {
continuation.resume(Unit)
}
override fun onFailure(reason: Int) {
continuation.resume(Unit)
}
})
}
}
@SuppressLint("SourceLockedOrientationActivity")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -177,7 +203,7 @@ class TransferActivity : AppCompatActivity(R.layout.transfer_activity) {
}
})
supportFragmentManager.commit {
supportFragmentManager.commit(true) {
replace(R.id.fragment_container_view, TransferTargetFragment())
}
@@ -194,12 +220,13 @@ class TransferActivity : AppCompatActivity(R.layout.transfer_activity) {
viewModel.setStep(TransferStep.SELECT_DATA)
}
TransferStep.DIRECTION -> {
supportFragmentManager.commit {
manager.disconnect()
supportFragmentManager.commit(true) {
replace(R.id.fragment_container_view, TransferDirectionFragment())
}
}
TransferStep.PERMISSION -> {
supportFragmentManager.commit {
supportFragmentManager.commit(true) {
replace(R.id.fragment_container_view, TransferPermissionFragment())
}
}
@@ -208,30 +235,6 @@ class TransferActivity : AppCompatActivity(R.layout.transfer_activity) {
if (!checkPermission()) { return@step }
runCatching {
suspendCoroutine { continuation ->
manager.removeGroup(channel, object: WifiP2pManager.ActionListener {
override fun onSuccess() {
continuation.resume(Unit)
}
override fun onFailure(reason: Int) {
continuation.resume(Unit)
}
})
}
suspendCoroutine { continuation ->
manager.cancelConnect(channel, object: WifiP2pManager.ActionListener {
override fun onSuccess() {
continuation.resume(Unit)
}
override fun onFailure(reason: Int) {
continuation.resume(Unit)
}
})
}
suspendCoroutine { continuation ->
manager.createGroup(channel, object: WifiP2pManager.ActionListener {
override fun onSuccess() {
@@ -244,7 +247,7 @@ class TransferActivity : AppCompatActivity(R.layout.transfer_activity) {
})
}
supportFragmentManager.commit {
supportFragmentManager.commit(true) {
replace(R.id.fragment_container_view, TransferWaitForConnectionFragment())
}
@@ -272,17 +275,17 @@ class TransferActivity : AppCompatActivity(R.layout.transfer_activity) {
Log.e("PUPILD", "Failed to create group", it)
}
supportFragmentManager.commit {
supportFragmentManager.commit(true) {
replace(R.id.fragment_container_view, TransferWaitForConnectionFragment())
}
}
TransferStep.CONNECTED -> {
supportFragmentManager.commit {
supportFragmentManager.commit(true) {
replace(R.id.fragment_container_view, TransferConnectedFragment())
}
}
TransferStep.SELECT_DATA -> {
supportFragmentManager.commit {
supportFragmentManager.commit(true) {
replace(R.id.fragment_container_view, TransferSelectDataFragment())
}
}

View File

@@ -52,9 +52,9 @@
app:defaultValue="8"
app:useSimpleSummaryProvider="true"/>
<!-- <Preference-->
<!-- app:key="transfer_data"-->
<!-- app:title="@string/settings_transfer_data"/>-->
<Preference
app:key="transfer_data"
app:title="@string/settings_transfer_data"/>
<SwitchPreferenceCompat
app:key="nomedia"