diff --git a/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/PeerGroupHandshake.kt b/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/PeerGroupHandshake.kt index c7140bb92..c280952e3 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/PeerGroupHandshake.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/PeerGroupHandshake.kt @@ -30,7 +30,6 @@ import java.net.InetAddress import java.net.InetSocketAddress import java.net.ServerSocket import java.net.Socket -import java.util.ArrayList /** * Helper class for the local file sharing module. @@ -105,43 +104,9 @@ internal class PeerGroupHandshake(private val wifiDirectManager: WifiDirectManag private fun exchangeFileTransferMetadata(outputStream: OutputStream, inputStream: InputStream) { if (wifiDirectManager.isFileSender) { - try { - ObjectOutputStream(outputStream).use { objectOutputStream -> - // Send total number of files which will be transferred - objectOutputStream.writeObject(wifiDirectManager.totalFilesForTransfer) - // Send the names of each of those files, in order - wifiDirectManager.getFilesForTransfer().forEach { fileItem -> - objectOutputStream.writeObject(fileItem.fileName) - Log.d(TAG, "Sending " + fileItem.fileUri.toString()) - } - } - } catch (e: Exception) { - e.printStackTrace() - } + SenderHandShake().handShake(wifiDirectManager, outputStream) } else { // Device is not the file sender - try { - ObjectInputStream(inputStream).use { objectInputStream -> - // Read the number of files - val totalFilesObject = objectInputStream.readObject().toString() - if (totalFilesObject.javaClass == String::class.java) { - val total: Int = totalFilesObject.toInt() - if (BuildConfig.DEBUG) Log.d(TAG, "Metadata: $total files") - // Read names of each of those files, in order - val fileItems = sequence { - yieldAll(generateSequence(1) { it + 1 }.map { - (objectInputStream.readObject() as? String)?.let { fileName -> - if (BuildConfig.DEBUG) Log.d(TAG, "Expecting $fileName") - FileItem(fileName = fileName) - } - }) - }.take(total) - val arrayListOfFileItems = ArrayList(fileItems.toList().filterNotNull()) - wifiDirectManager.setFilesForTransfer(arrayListOfFileItems) - } - } - } catch (e: Exception) { - e.printStackTrace() - } + ReceiverHandShake().handShake(wifiDirectManager, inputStream) } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/ReceiverHandShake.kt b/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/ReceiverHandShake.kt new file mode 100644 index 000000000..c55515113 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/ReceiverHandShake.kt @@ -0,0 +1,57 @@ +/* + * Kiwix Android + * Copyright (c) 2020 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.localFileTransfer + +import android.util.Log +import org.kiwix.kiwixmobile.core.BuildConfig +import java.io.InputStream +import java.io.ObjectInputStream +import java.util.ArrayList + +class ReceiverHandShake { + fun handShake(wifiDirectManager: WifiDirectManager, inputStream: InputStream) { + try { + ObjectInputStream(inputStream).use { objectInputStream -> + // Read the number of files + val totalFilesObject = objectInputStream.readObject().toString() + if (totalFilesObject.javaClass == String::class.java) { + val total: Int = totalFilesObject.toInt() + if (BuildConfig.DEBUG) Log.d(TAG, "Metadata: $total files") + // Read names of each of those files, in order + val fileItems = sequence { + yieldAll(generateSequence(1) { it + 1 }.map { + (objectInputStream.readObject() as? String)?.let { fileName -> + if (BuildConfig.DEBUG) Log.d(TAG, "Expecting $fileName") + FileItem(fileName = fileName) + } + }) + }.take(total) + val arrayListOfFileItems = ArrayList(fileItems.toList().filterNotNull()) + wifiDirectManager.setFilesForTransfer(arrayListOfFileItems) + } + } + } catch (e: Exception) { + e.printStackTrace() + } + } + + companion object { + private const val TAG = "ReceiverHandshake" + } +} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/SenderHandShake.kt b/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/SenderHandShake.kt new file mode 100644 index 000000000..e1a2d9031 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/localFileTransfer/SenderHandShake.kt @@ -0,0 +1,45 @@ +/* + * Kiwix Android + * Copyright (c) 2020 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.localFileTransfer + +import android.util.Log +import java.io.ObjectOutputStream +import java.io.OutputStream + +class SenderHandShake { + fun handShake(wifiDirectManager: WifiDirectManager, outputStream: OutputStream) { + try { + ObjectOutputStream(outputStream).use { objectOutputStream -> + // Send total number of files which will be transferred + objectOutputStream.writeObject(wifiDirectManager.totalFilesForTransfer) + // Send the names of each of those files, in order + wifiDirectManager.getFilesForTransfer().forEach { fileItem -> + objectOutputStream.writeObject(fileItem.fileName) + Log.d(TAG, "Sending " + fileItem.fileUri.toString()) + } + } + } catch (e: Exception) { + e.printStackTrace() + } + } + + companion object { + private const val TAG = "SenderHandShake" + } +}