Display file transfer index

This commit is contained in:
Matéo Duparc 2021-05-30 17:16:52 +02:00
parent aa4ae9ee92
commit 60f9376e83
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
7 changed files with 28 additions and 24 deletions

View File

@ -99,7 +99,8 @@ class ChatActivity : ServiceBoundActivity() {
sessionName = contact.name sessionName = contact.name
contact.avatar contact.avatar
} }
binding.toolbar.title.text = sessionName ?: airaService.sessions[sessionId]!!.ip val ipName = sessionName ?: airaService.sessions[sessionId]!!.ip
binding.toolbar.title.text = ipName
if (avatar == null) { if (avatar == null) {
binding.toolbar.avatar.setTextAvatar(sessionName) binding.toolbar.avatar.setTextAvatar(sessionName)
} else { } else {
@ -119,7 +120,7 @@ class ChatActivity : ServiceBoundActivity() {
} }
airaService.receiveFileTransfers[sessionId]?.let { airaService.receiveFileTransfers[sessionId]?.let {
if (it.shouldAsk) { if (it.shouldAsk) {
it.ask(this@ChatActivity) it.ask(this@ChatActivity, ipName)
} }
} }
binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount) binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount)
@ -184,7 +185,7 @@ class ChatActivity : ServiceBoundActivity() {
override fun onAskLargeFiles(sessionId: Int, filesReceiver: FilesReceiver): Boolean { override fun onAskLargeFiles(sessionId: Int, filesReceiver: FilesReceiver): Boolean {
return if (this@ChatActivity.sessionId == sessionId) { return if (this@ChatActivity.sessionId == sessionId) {
runOnUiThread { runOnUiThread {
filesReceiver.ask(this@ChatActivity) filesReceiver.ask(this@ChatActivity, sessionName ?: airaService.sessions[sessionId]!!.ip)
} }
true true
} else { } else {

View File

@ -73,7 +73,7 @@ class MainActivity : ServiceBoundActivity() {
override fun onAskLargeFiles(sessionId: Int, filesReceiver: FilesReceiver): Boolean { override fun onAskLargeFiles(sessionId: Int, filesReceiver: FilesReceiver): Boolean {
runOnUiThread { runOnUiThread {
filesReceiver.ask(this@MainActivity) filesReceiver.ask(this@MainActivity, airaService.getNameOf(sessionId))
} }
return true return true
} }

View File

@ -169,7 +169,7 @@ class AIRAService : Service() {
private fun sendLargeFilesTo(sessionId: Int, files: MutableList<SendFile>) { private fun sendLargeFilesTo(sessionId: Int, files: MutableList<SendFile>) {
if (sendFileTransfers[sessionId] == null && receiveFileTransfers[sessionId] == null) { if (sendFileTransfers[sessionId] == null && receiveFileTransfers[sessionId] == null) {
val filesSender = FilesSender(files, this, notificationManager, getNameOf(sessionId)) val filesSender = FilesSender(files, this, notificationManager)
initFileTransferNotification(sessionId, filesSender.fileTransferNotification, filesSender.files[0]) initFileTransferNotification(sessionId, filesSender.fileTransferNotification, filesSender.files[0])
sendFileTransfers[sessionId] = filesSender sendFileTransfers[sessionId] = filesSender
sendTo(sessionId, Protocol.askLargeFiles(files)) sendTo(sessionId, Protocol.askLargeFiles(files))
@ -188,7 +188,7 @@ class AIRAService : Service() {
return sessions.contains(sessionId) return sessions.contains(sessionId)
} }
private fun getNameOf(sessionId: Int): String { fun getNameOf(sessionId: Int): String {
return contacts[sessionId]?.name ?: savedNames[sessionId] ?: sessions[sessionId]!!.ip return contacts[sessionId]?.name ?: savedNames[sessionId] ?: sessions[sessionId]!!.ip
} }
@ -648,7 +648,7 @@ class AIRAService : Service() {
initFileTransferNotification( initFileTransferNotification(
sessionId, sessionId,
filesReceiver.fileTransferNotification, filesReceiver.fileTransferNotification,
nextFile nextFile,
) )
} }
} else { } else {
@ -677,7 +677,7 @@ class AIRAService : Service() {
initFileTransferNotification( initFileTransferNotification(
sessionId, sessionId,
filesSender.fileTransferNotification, filesSender.fileTransferNotification,
nextFile nextFile,
) )
encryptNextChunk(session, filesSender) encryptNextChunk(session, filesSender)
filesSender.nextChunk?.let { filesSender.nextChunk?.let {
@ -704,14 +704,13 @@ class AIRAService : Service() {
Protocol.ASK_LARGE_FILES -> { Protocol.ASK_LARGE_FILES -> {
if (!receiveFileTransfers.containsKey(sessionId) && !sendFileTransfers.containsKey(sessionId)) { if (!receiveFileTransfers.containsKey(sessionId) && !sendFileTransfers.containsKey(sessionId)) {
Protocol.parseAskFiles(buffer)?.let { files -> Protocol.parseAskFiles(buffer)?.let { files ->
val sessionName = getNameOf(sessionId)
val filesReceiver = FilesReceiver( val filesReceiver = FilesReceiver(
files, files,
{ filesReceiver -> { filesReceiver ->
initFileTransferNotification( initFileTransferNotification(
sessionId, sessionId,
filesReceiver.fileTransferNotification, filesReceiver.fileTransferNotification,
filesReceiver.files[0] filesReceiver.files[0],
) )
sendTo(sessionId, Protocol.acceptLargeFiles()) sendTo(sessionId, Protocol.acceptLargeFiles())
}, { filesReceiver -> }, { filesReceiver ->
@ -721,7 +720,6 @@ class AIRAService : Service() {
}, },
this, this,
notificationManager, notificationManager,
sessionName
) )
receiveFileTransfers[sessionId] = filesReceiver receiveFileTransfers[sessionId] = filesReceiver
var shouldSendNotification = true var shouldSendNotification = true
@ -735,7 +733,7 @@ class AIRAService : Service() {
.setCategory(NotificationCompat.CATEGORY_EVENT) .setCategory(NotificationCompat.CATEGORY_EVENT)
.setSmallIcon(R.drawable.ic_launcher) .setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.download_file_request)) .setContentTitle(getString(R.string.download_file_request))
.setContentText(getString(R.string.want_to_send_files, sessionName)) .setContentText(getString(R.string.want_to_send_files, getNameOf(sessionId)))
.setOngoing(true) //not cancelable .setOngoing(true) //not cancelable
.setContentIntent( .setContentIntent(
PendingIntent.getActivity(this, 0, Intent(this, ChatActivity::class.java).apply { PendingIntent.getActivity(this, 0, Intent(this, ChatActivity::class.java).apply {

View File

@ -11,22 +11,26 @@ import sushi.hardcore.aira.R
class FileTransferNotification( class FileTransferNotification(
private val context: Context, private val context: Context,
private val notificationManager: NotificationManagerCompat, private val notificationManager: NotificationManagerCompat,
private val sessionName: String private val total: Int,
) { ) {
private var fileName: String? = null
private var fileSize = -1 private var fileSize = -1
private var index = 0
private var transferred = 0 private var transferred = 0
private lateinit var notificationBuilder: NotificationCompat.Builder private lateinit var notificationBuilder: NotificationCompat.Builder
private var notificationId = -1 private var notificationId = -1
private var isEnded = false private var isEnded = false
fun initFileTransferNotification(id: Int, fileName: String, size: Int, cancelIntent: Intent) { fun initFileTransferNotification(id: Int, fileName: String, size: Int, cancelIntent: Intent) {
this.fileName = fileName
fileSize = size fileSize = size
index += 1
transferred = 0 transferred = 0
notificationBuilder = NotificationCompat.Builder(context, AIRAService.FILE_TRANSFER_NOTIFICATION_CHANNEL_ID) notificationBuilder = NotificationCompat.Builder(context, AIRAService.FILE_TRANSFER_NOTIFICATION_CHANNEL_ID)
.setCategory(NotificationCompat.CATEGORY_PROGRESS) .setCategory(NotificationCompat.CATEGORY_PROGRESS)
.setSmallIcon(R.drawable.ic_launcher) .setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(sessionName) .setContentTitle(fileName)
.setContentText(fileName) .setContentText("0% ($index/$total)")
.setOngoing(true) .setOngoing(true)
.setProgress(fileSize, 0, true) .setProgress(fileSize, 0, true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
@ -49,7 +53,10 @@ class FileTransferNotification(
fun updateNotificationProgress(size: Int) { fun updateNotificationProgress(size: Int) {
transferred += size transferred += size
notificationBuilder.setProgress(fileSize, transferred, false) val percent = (transferred.toFloat()/fileSize)*100
notificationBuilder
.setContentText("${"%.2f".format(percent)}% ($index/$total)")
.setProgress(fileSize, transferred, false)
synchronized(this) { synchronized(this) {
if (!isEnded) { if (!isEnded) {
notificationManager.notify(notificationId, notificationBuilder.build()) notificationManager.notify(notificationId, notificationBuilder.build())
@ -64,7 +71,7 @@ class FileTransferNotification(
NotificationCompat.Builder(context, AIRAService.FILE_TRANSFER_NOTIFICATION_CHANNEL_ID) NotificationCompat.Builder(context, AIRAService.FILE_TRANSFER_NOTIFICATION_CHANNEL_ID)
.setCategory(NotificationCompat.CATEGORY_EVENT) .setCategory(NotificationCompat.CATEGORY_EVENT)
.setSmallIcon(R.drawable.ic_launcher) .setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(sessionName) .setContentTitle(fileName)
.setContentText(context.getString(string)) .setContentText(context.getString(string))
.build() .build()
) )

View File

@ -15,12 +15,11 @@ class FilesReceiver(
private val onAborted: (FilesReceiver) -> Unit, private val onAborted: (FilesReceiver) -> Unit,
context: Context, context: Context,
notificationManager: NotificationManagerCompat, notificationManager: NotificationManagerCompat,
private val sessionName: String ): FilesTransfer(context, notificationManager, files.size) {
): FilesTransfer(context, notificationManager, sessionName) {
var shouldAsk = true var shouldAsk = true
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
fun ask(activity: AppCompatActivity) { fun ask(activity: AppCompatActivity, sessionName: String) {
val dialogBinding = DialogAskFileBinding.inflate(activity.layoutInflater) val dialogBinding = DialogAskFileBinding.inflate(activity.layoutInflater)
dialogBinding.textTitle.text = activity.getString(R.string.want_to_send_files, sessionName)+':' dialogBinding.textTitle.text = activity.getString(R.string.want_to_send_files, sessionName)+':'
val filesInfo = StringBuilder() val filesInfo = StringBuilder()

View File

@ -7,8 +7,7 @@ class FilesSender(
val files: List<SendFile>, val files: List<SendFile>,
context: Context, context: Context,
notificationManager: NotificationManagerCompat, notificationManager: NotificationManagerCompat,
sessionName: String ): FilesTransfer(context, notificationManager, files.size) {
): FilesTransfer(context, notificationManager, sessionName) {
val lastChunkSizes = mutableListOf<Int>() val lastChunkSizes = mutableListOf<Int>()
var nextChunk: ByteArray? = null var nextChunk: ByteArray? = null
val msgQueue = mutableListOf<ByteArray>() val msgQueue = mutableListOf<ByteArray>()

View File

@ -3,7 +3,7 @@ package sushi.hardcore.aira.background_service
import android.content.Context import android.content.Context
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
open class FilesTransfer(context: Context, notificationManager: NotificationManagerCompat, sessionName: String) { open class FilesTransfer(context: Context, notificationManager: NotificationManagerCompat, total: Int) {
val fileTransferNotification = FileTransferNotification(context, notificationManager, sessionName) val fileTransferNotification = FileTransferNotification(context, notificationManager, total)
var index = 0 var index = 0
} }