Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix p2p crashes #66

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.smartregister.p2p.data_sharing

import android.app.Activity
import kotlinx.coroutines.CoroutineScope
import org.smartregister.p2p.authentication.model.DeviceRole
import org.smartregister.p2p.payload.PayloadContract
import org.smartregister.p2p.utils.DispatcherProvider

Expand Down Expand Up @@ -107,4 +108,8 @@ interface DataSharingStrategy {
fun connectionTimeout(): Int = 30

fun isPairingInitiated(): Boolean

fun getDeviceRole(): DeviceRole

fun setDeviceRole(deviceRole: DeviceRole)
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.smartregister.p2p.WifiP2pBroadcastReceiver
import org.smartregister.p2p.authentication.model.DeviceRole
import org.smartregister.p2p.payload.BytePayload
import org.smartregister.p2p.payload.PayloadContract
import org.smartregister.p2p.payload.StringPayload
Expand Down Expand Up @@ -91,6 +92,7 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {

val successPairingListeners: MutableSet<() -> Unit> = mutableSetOf()
val failedPairingListeners: MutableSet<(Exception) -> Unit> = mutableSetOf()
var _deviceRole = DeviceRole.SENDER

override fun setDispatcherProvider(dispatcherProvider: DispatcherProvider) {
this.dispatcherProvider = dispatcherProvider
Expand Down Expand Up @@ -191,6 +193,7 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
val exception = Exception("$reason: ${getWifiP2pReason(reason)}")
onDeviceFound?.failed(exception)
onSearchingFailed(exception)
isSearchingDevices = false
}
}
)
Expand Down Expand Up @@ -302,9 +305,11 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
// both onConnectionInfoAvailable implementations
val successPairingListener = {
disableConnectionCountdown(pairingTimeout!!)
Timber.i("Device successfully paired")
Timber.i("Device successfully paired on device [$_deviceRole]")

currentDevice = wifiDirectDevice

// This only happens on the sender device
paired = true
pairingInitiated = false
Timber.e("connect() successfully paired with pairing inititiated $pairingInitiated")
Expand Down Expand Up @@ -399,6 +404,14 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
device: DeviceInfo,
operationListener: DataSharingStrategy.OperationListener
) {
// TODO: Remove and fix this correctly
if (wifiP2pChannel == null) {
val ex = Exception("wifiP2Channel was null")
onDisconnectFailed(device, ex)
operationListener.onFailure(device, ex)
return
}

requestedDisconnection = true
wifiP2pManager.removeGroup(
wifiP2pChannel,
Expand Down Expand Up @@ -883,7 +896,16 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
// This is handled onConnectionInfoAvailable inside WifiDirectDataSharingStrategy
// paired = true

if (_deviceRole == DeviceRole.RECEIVER) {
paired = true
}

onConnected.onSuccess(null)


// Let's stop searching once connected to another device
stopSearchingDevices(null)

} else {

if (paired) {
Expand Down Expand Up @@ -1020,6 +1042,8 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
Timber.e(ex)
operationListener?.onFailure(null, ex)

isSearchingDevices = true


if (!paired) {
// Set this to null so that wifi direct is turned on during on resume
Expand Down Expand Up @@ -1134,6 +1158,14 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
closeSocketAndStreams()
}

override fun getDeviceRole(): DeviceRole {
return _deviceRole
}

override fun setDeviceRole(deviceRole: DeviceRole) {
_deviceRole = deviceRole
}

companion object {

const val CONNECT_TO_SERVER_RETRIES = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ class P2PViewModel(
val p2PState: LiveData<P2PState>
get() = _p2PState

var deviceRole: DeviceRole = DeviceRole.SENDER
var deviceRole: DeviceRole
get() = dataSharingStrategy.getDeviceRole()
set(dRole) {
dataSharingStrategy.setDeviceRole(dRole)
}
private var currentConnectedDevice: DeviceInfo? = null
private var requestDisconnection = false
private var isSenderSyncComplete = false
Expand Down