Skip to content

Commit

Permalink
DRAFT: Fix p2p sync bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ekigamba committed Jul 19, 2022
1 parent 25b88cc commit c509c23
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 8 deletions.
12 changes: 7 additions & 5 deletions p2p-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -181,23 +180,26 @@ 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 {
snapshot(MavenPublication) {
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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,6 @@ interface DataSharingStrategy {
fun onResume(isScanning: Boolean = false)

fun onPause()

fun onStop()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -183,15 +186,15 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
)
}

listenForWifiP2pIntents()
listenForWifiP2pEventsIntents()
initiatePeerDiscovery(onDeviceFound)
}

private fun requestConnectionInfo() {
wifiP2pManager.requestConnectionInfo(wifiP2pChannel) { onConnectionInfoAvailable(it, null) }
}

private fun listenForWifiP2pIntents() {
private fun listenForWifiP2pEventsIntents() {
wifiP2pReceiver?.also {
context.registerReceiver(
it,
Expand Down Expand Up @@ -266,6 +269,7 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
)
Timber.d("Peer discovery initiated")
}

private fun requestDeviceInfo() {
wifiP2pChannel?.also { wifiP2pChannel ->
if (ActivityCompat.checkSelfPermission(
Expand All @@ -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
Expand Down Expand Up @@ -632,7 +681,7 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {

override fun onResume(isScanning: Boolean) {
if (isScanning) {
listenForWifiP2pIntents()
listenForWifiP2pEventsIntents()
initiatePeerDiscoveryOnceAccessFineLocationGranted()
requestDeviceInfo()
requestConnectionInfo()
Expand Down Expand Up @@ -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)
}
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -440,6 +442,8 @@ class P2PDeviceSearchActivity : AppCompatActivity(), P2pModeSelectContract.View
keepScreenOn(true)
p2PSenderViewModel.sendDeviceDetails(getCurrentConnectedDevice())
showTransferProgressDialog()

throw RuntimeException("Oh! An error occurred!")
}

interactiveDialog.setCancelable(false)
Expand Down Expand Up @@ -592,4 +596,11 @@ class P2PDeviceSearchActivity : AppCompatActivity(), P2pModeSelectContract.View
}
}
}

override fun onStop() {
super.onStop()

//dataSharingStrategy.onStop()
}

}

0 comments on commit c509c23

Please sign in to comment.