Skip to content

Commit

Permalink
transfer wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tom5079 committed Jun 30, 2024
1 parent 9f103dc commit 290b7fb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -89,6 +90,7 @@ class TransferClientService : Service() {
}
}.onFailure {
Log.d("PUPILD", "Connection closed with error $it")
channel.close()
socket.close()
stopSelf(startId)
}
Expand All @@ -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
}
}

Expand Down
65 changes: 34 additions & 31 deletions app/src/main/java/xyz/quaver/pupil/ui/TransferActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -177,7 +203,7 @@ class TransferActivity : AppCompatActivity(R.layout.transfer_activity) {
}
})

supportFragmentManager.commit {
supportFragmentManager.commit(true) {
replace(R.id.fragment_container_view, TransferTargetFragment())
}

Expand All @@ -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())
}
}
Expand All @@ -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() {
Expand All @@ -244,7 +247,7 @@ class TransferActivity : AppCompatActivity(R.layout.transfer_activity) {
})
}

supportFragmentManager.commit {
supportFragmentManager.commit(true) {
replace(R.id.fragment_container_view, TransferWaitForConnectionFragment())
}

Expand Down Expand Up @@ -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())
}
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 290b7fb

Please sign in to comment.