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
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) {
binding.toolbar.avatar.setTextAvatar(sessionName)
} else {
@ -119,7 +120,7 @@ class ChatActivity : ServiceBoundActivity() {
}
airaService.receiveFileTransfers[sessionId]?.let {
if (it.shouldAsk) {
it.ask(this@ChatActivity)
it.ask(this@ChatActivity, ipName)
}
}
binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount)
@ -184,7 +185,7 @@ class ChatActivity : ServiceBoundActivity() {
override fun onAskLargeFiles(sessionId: Int, filesReceiver: FilesReceiver): Boolean {
return if (this@ChatActivity.sessionId == sessionId) {
runOnUiThread {
filesReceiver.ask(this@ChatActivity)
filesReceiver.ask(this@ChatActivity, sessionName ?: airaService.sessions[sessionId]!!.ip)
}
true
} else {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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