From c509c236f63451711d2973b59773b40d6f96d563 Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Tue, 19 Jul 2022 17:24:00 +0300 Subject: [PATCH] DRAFT: Fix p2p sync bugs --- p2p-lib/build.gradle | 12 +-- .../p2p/data_sharing/DataSharingStrategy.kt | 2 + .../WifiDirectDataSharingStrategy.kt | 75 ++++++++++++++++++- .../p2p/search/ui/P2PDeviceSearchActivity.kt | 11 +++ 4 files changed, 92 insertions(+), 8 deletions(-) diff --git a/p2p-lib/build.gradle b/p2p-lib/build.gradle index 9cdb1855..002bf792 100644 --- a/p2p-lib/build.gradle +++ b/p2p-lib/build.gradle @@ -99,7 +99,6 @@ dependencies { configuration -> testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.1' androidTestImplementation 'androidx.test.ext:junit:1.1.3' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' } def roomDependencies(configuration) { @@ -181,15 +180,18 @@ coveralls { sourceDirs = ["$project.projectDir/src/main/java"] } + spotless { kotlin { - target '**/*.kt' - ktlint(versions.ktlint).userData(['indent_size': '2', 'continuation_indent_size': '2']) + //target '**/*.kt' + /*ktlint(versions.ktlint).userData(['indent_size': '2', 'continuation_indent_size': '2']) ktfmt().googleStyle() - licenseHeaderFile "${project.rootProject.projectDir}/license-header.txt" + licenseHeaderFile "${project.rootProject.projectDir}/license-header.txt"*/ } } + + afterEvaluate { publishing { publications { @@ -197,7 +199,7 @@ afterEvaluate { from(components["release"]) artifactId = "p2p-lib" groupId = "org.smartregister" - version = "0.3.0-SNAPSHOT" + version = "0.3.1-CRASH-SNAPSHOT" pom { name.set("Peer to Peer Library") } 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 2532cf13..ff2e15ea 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 @@ -95,4 +95,6 @@ interface DataSharingStrategy { fun onResume(isScanning: Boolean = false) fun onPause() + + fun onStop() } 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 b4b6fbcc..e8a0ba3d 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 @@ -101,6 +101,9 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener { requestAccessFineLocationIfNotGranted() } + // Check if already connected and disconnect + requestDeviceInfo2() + wifiP2pChannel = wifiP2pManager.initialize(context, context.mainLooper, null) wifiP2pChannel?.also { channel -> wifiP2pReceiver = @@ -183,7 +186,7 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener { ) } - listenForWifiP2pIntents() + listenForWifiP2pEventsIntents() initiatePeerDiscovery(onDeviceFound) } @@ -191,7 +194,7 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener { wifiP2pManager.requestConnectionInfo(wifiP2pChannel) { onConnectionInfoAvailable(it, null) } } - private fun listenForWifiP2pIntents() { + private fun listenForWifiP2pEventsIntents() { wifiP2pReceiver?.also { context.registerReceiver( it, @@ -266,6 +269,7 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener { ) Timber.d("Peer discovery initiated") } + private fun requestDeviceInfo() { wifiP2pChannel?.also { wifiP2pChannel -> if (ActivityCompat.checkSelfPermission( @@ -287,6 +291,51 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener { } } } + + private fun requestDeviceInfo2() { + wifiP2pChannel?.also { wifiP2pChannel -> + if (ActivityCompat.checkSelfPermission( + context, + android.Manifest.permission.ACCESS_FINE_LOCATION + ) != PackageManager.PERMISSION_GRANTED + ) { + return handleAccessFineLocationNotGranted() + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + wifiP2pManager.requestDeviceInfo(wifiP2pChannel) { + if (it != null && it.status == WifiP2pDevice.CONNECTED) { + disconnect(WifiDirectDevice(it), object: DataSharingStrategy.OperationListener { + override fun onSuccess(device: DeviceInfo?) { + Timber.e("Successfully connected from Wifi-Direct") + } + + override fun onFailure(device: DeviceInfo?, ex: Exception) { + Timber.e(ex, "Successfully disconnect from Wifi-Direct") + } + }) + } + } + } else { + wifiP2pManager.requestConnectionInfo(wifiP2pChannel) { + if (it != null && it.groupFormed) { + wifiP2pManager.removeGroup( + wifiP2pChannel, + object : WifiP2pManager.ActionListener { + override fun onSuccess() { + Timber.e("Successfully connected from Wifi-Direct") + } + + override fun onFailure(reason: Int) { + Timber.e(Exception(getWifiP2pReason(reason)), "Successfully disconnect from Wifi-Direct") + } + }) + } + } + } + } + } + override fun connect( device: DeviceInfo, operationListener: DataSharingStrategy.OperationListener @@ -632,7 +681,7 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener { override fun onResume(isScanning: Boolean) { if (isScanning) { - listenForWifiP2pIntents() + listenForWifiP2pEventsIntents() initiatePeerDiscoveryOnceAccessFineLocationGranted() requestDeviceInfo() requestConnectionInfo() @@ -792,4 +841,24 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener { return wifiP2pDevice.deviceAddress } } + + override fun onStop() { + closeSocketAndStreams() + + requestedDisconnection = true + wifiP2pManager.removeGroup( + wifiP2pChannel, + object : WifiP2pManager.ActionListener { + override fun onSuccess() { + Timber.i("Device successfully disconnected") + paired = false + } + + override fun onFailure(reason: Int) { + val exception = Exception("Error #$reason: ${getWifiP2pReason(reason)}") + Timber.e(exception) + } + } + ) + } } diff --git a/p2p-lib/src/main/java/org/smartregister/p2p/search/ui/P2PDeviceSearchActivity.kt b/p2p-lib/src/main/java/org/smartregister/p2p/search/ui/P2PDeviceSearchActivity.kt index 0a547df0..9e7baf5c 100644 --- a/p2p-lib/src/main/java/org/smartregister/p2p/search/ui/P2PDeviceSearchActivity.kt +++ b/p2p-lib/src/main/java/org/smartregister/p2p/search/ui/P2PDeviceSearchActivity.kt @@ -104,6 +104,8 @@ class P2PDeviceSearchActivity : AppCompatActivity(), P2pModeSelectContract.View Timber.plant(Timber.DebugTree()) } + Timber.e("Just a random log message") + title = getString(R.string.device_to_device_sync) supportActionBar?.setHomeAsUpIndicator(android.R.drawable.ic_menu_close_clear_cancel) @@ -440,6 +442,8 @@ class P2PDeviceSearchActivity : AppCompatActivity(), P2pModeSelectContract.View keepScreenOn(true) p2PSenderViewModel.sendDeviceDetails(getCurrentConnectedDevice()) showTransferProgressDialog() + + throw RuntimeException("Oh! An error occurred!") } interactiveDialog.setCancelable(false) @@ -592,4 +596,11 @@ class P2PDeviceSearchActivity : AppCompatActivity(), P2pModeSelectContract.View } } } + + override fun onStop() { + super.onStop() + + //dataSharingStrategy.onStop() + } + }