From a74154e8d73602c24b55276941ae1753bc15587a Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Tue, 21 Feb 2023 12:25:23 +0300 Subject: [PATCH 1/3] Fix NPE crash when sending completes --- .../p2p/data_sharing/WifiDirectDataSharingStrategy.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/WifiDirectDataSharingStrategy.kt b/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/WifiDirectDataSharingStrategy.kt index cf40ecda..a595b475 100644 --- a/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/WifiDirectDataSharingStrategy.kt +++ b/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/WifiDirectDataSharingStrategy.kt @@ -399,6 +399,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, From f7334513c07cfbdcb6435e965d46309e8aea711c Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Tue, 21 Feb 2023 12:39:41 +0300 Subject: [PATCH 2/3] Fix paired is false on Receiver devices --- .../p2p/data_sharing/DataSharingStrategy.kt | 5 +++++ .../WifiDirectDataSharingStrategy.kt | 18 +++++++++++++++++- .../p2p/search/ui/p2p/P2PViewModel.kt | 6 +++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/DataSharingStrategy.kt b/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/DataSharingStrategy.kt index 37187fa0..8f1a95d2 100644 --- a/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/DataSharingStrategy.kt +++ b/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/DataSharingStrategy.kt @@ -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 @@ -107,4 +108,8 @@ interface DataSharingStrategy { fun connectionTimeout(): Int = 30 fun isPairingInitiated(): Boolean + + fun getDeviceRole(): DeviceRole + + fun setDeviceRole(deviceRole: DeviceRole) } diff --git a/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/WifiDirectDataSharingStrategy.kt b/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/WifiDirectDataSharingStrategy.kt index a595b475..505eef96 100644 --- a/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/WifiDirectDataSharingStrategy.kt +++ b/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/WifiDirectDataSharingStrategy.kt @@ -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 @@ -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 @@ -302,9 +304,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") @@ -891,6 +895,10 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener { // This is handled onConnectionInfoAvailable inside WifiDirectDataSharingStrategy // paired = true + if (_deviceRole == DeviceRole.RECEIVER) { + paired = true + } + onConnected.onSuccess(null) } else { @@ -1142,6 +1150,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 diff --git a/p2p-lib/src/main/java/org/smartregister/p2p/search/ui/p2p/P2PViewModel.kt b/p2p-lib/src/main/java/org/smartregister/p2p/search/ui/p2p/P2PViewModel.kt index 96c7c76e..82daa852 100644 --- a/p2p-lib/src/main/java/org/smartregister/p2p/search/ui/p2p/P2PViewModel.kt +++ b/p2p-lib/src/main/java/org/smartregister/p2p/search/ui/p2p/P2PViewModel.kt @@ -47,7 +47,11 @@ class P2PViewModel( val p2PState: LiveData 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 From 7d40f91e4cac6def0b1dae6c231cfd1b17b713b2 Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Tue, 21 Feb 2023 12:48:31 +0300 Subject: [PATCH 3/3] Stop searching when devices connect --- .../p2p/data_sharing/WifiDirectDataSharingStrategy.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/WifiDirectDataSharingStrategy.kt b/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/WifiDirectDataSharingStrategy.kt index 505eef96..aff5c097 100644 --- a/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/WifiDirectDataSharingStrategy.kt +++ b/p2p-lib/src/main/java/org/smartregister/p2p/data_sharing/WifiDirectDataSharingStrategy.kt @@ -193,6 +193,7 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener { val exception = Exception("$reason: ${getWifiP2pReason(reason)}") onDeviceFound?.failed(exception) onSearchingFailed(exception) + isSearchingDevices = false } } ) @@ -900,6 +901,11 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener { } onConnected.onSuccess(null) + + + // Let's stop searching once connected to another device + stopSearchingDevices(null) + } else { if (paired) { @@ -1036,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