Skip to content

Instantly share code, notes, and snippets.

@mwshubham
Created February 20, 2018 08:43
Show Gist options
  • Save mwshubham/3f2cbc4a16fc82d09265074e7bb8104c to your computer and use it in GitHub Desktop.
Save mwshubham/3f2cbc4a16fc82d09265074e7bb8104c to your computer and use it in GitHub Desktop.
Xmpp File transfer manager
@Deprecated("Using different methodology to send file")
fun sendFile(userId: EntityFullJid, file: File, description: String) {
mThreadHandler.post({ mConnection?.sendFile(userId, file, description) })
}
private fun setUpFileTransferManager() {
val fileTransferManager = FileTransferManager.getInstanceFor(xmppTcpConnection)
fileTransferManager.addFileTransferListener(this)
// val fileTransferNegotiator = FileTransferNegotiator.getInstanceFor(xmppTcpConnection);
// FileTransferNegotiator.IBB_ONLY = true;
}
// FileTransferListener
override fun fileTransferRequest(request: FileTransferRequest) {
Log.i(TAG, "fileTransferRequest()")
Log.d(TAG, "request: $request")
Log.d(TAG, "request fileName: ${request.fileName}")
Log.d(TAG, "request fileSize: ${request.fileSize}")
Log.d(TAG, "request description: ${request.description}")
Log.d(TAG, "request mimeType: ${request.mimeType}")
try {
val incomingFileTransfer = request.accept()
// todo check for write external storage permission...
val dir = File(Environment.getExternalStorageDirectory().absolutePath + "/Messia/Image/")
dir.mkdirs()
val file = File(dir, incomingFileTransfer.fileName)
incomingFileTransfer.recieveFile(file)
Timer().schedule(timerTask {
if (incomingFileTransfer.isDone) {
cancel()
}
if (incomingFileTransfer.status.equals(FileTransfer.Status.error)) {
Log.e(TAG, "incomingFileTransfer.error: ${incomingFileTransfer.error}")
Log.e(TAG, "incomingFileTransfer.error: ${incomingFileTransfer.exception.message}")
incomingFileTransfer.exception.printStackTrace()
cancel()
} else {
Log.i(TAG, "incomingFileTransfer.status: ${incomingFileTransfer.status}")
Log.i(TAG, "incomingFileTransfer.progress: ${incomingFileTransfer.progress}")
}
}, 0L, 2000L)
} catch (e: SmackException) {
Log.e(TAG, "SmackException in recieving file: ${e.message} ")
} catch (e: IOException) {
Log.e(TAG, "IOException in recieving file: ${e.message} ")
} catch (e: IllegalArgumentException) {
Log.e(TAG, "IllegalArgumentException in recieving file: ${e.message} ")
} catch (e: Exception) {
Log.e(TAG, "Exception in recieving file: ${e.message} ")
}
}
@Deprecated("Using different methodology to send file")
fun sendFile(userId: EntityFullJid, file: File, description: String) {
Log.i(TAG, "sendFile()")
Log.d(TAG, "userId: $userId")
Log.d(TAG, "file: $file")
Log.d(TAG, "description: $description")
try {
val manager = FileTransferManager.getInstanceFor(xmppTcpConnection)
val outgoingFileTransfer = manager.createOutgoingFileTransfer(userId)
outgoingFileTransfer.sendFile(file, description)
Timer().schedule(timerTask {
if (outgoingFileTransfer.isDone) cancel()
if (outgoingFileTransfer.status.equals(FileTransfer.Status.error)) {
Log.e(TAG, "outgoingFileTransfer.error: ${outgoingFileTransfer.error}")
Log.e(TAG, "outgoingFileTransfer.exception.message: ${outgoingFileTransfer.exception.message}")
outgoingFileTransfer.exception.printStackTrace()
cancel()
} else {
Log.i(TAG, "outgoingFileTransfer.status: ${outgoingFileTransfer.status}")
Log.i(TAG, "outgoingFileTransfer.progress: ${outgoingFileTransfer.progress}")
}
}, 0L, 2000L)
} catch (e: IllegalArgumentException) {
Log.e(TAG, "IllegalArgumentException in sending file: ${e.message}")
} catch (e: SmackException) {
Log.e(TAG, "SmackException in sending file: ${e.message}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment