2020-04-16 15:44:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Proton Technologies AG
|
2020-09-17 09:47:11 +00:00
|
|
|
*
|
2020-04-16 15:44:53 +00:00
|
|
|
* This file is part of ProtonMail.
|
2020-09-17 09:47:11 +00:00
|
|
|
*
|
2020-04-16 15:44:53 +00:00
|
|
|
* ProtonMail 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.
|
2020-09-17 09:47:11 +00:00
|
|
|
*
|
2020-04-16 15:44:53 +00:00
|
|
|
* ProtonMail 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.
|
2020-09-17 09:47:11 +00:00
|
|
|
*
|
2020-04-16 15:44:53 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with ProtonMail. If not, see https://www.gnu.org/licenses/.
|
|
|
|
*/
|
|
|
|
package ch.protonmail.android.activities.messageDetails.attachments
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.ArrayAdapter
|
2020-09-17 09:47:11 +00:00
|
|
|
import androidx.annotation.DrawableRes
|
2020-04-16 15:44:53 +00:00
|
|
|
import ch.protonmail.android.R
|
|
|
|
import ch.protonmail.android.api.models.room.messages.Attachment
|
|
|
|
import ch.protonmail.android.views.messageDetails.AttachmentDetailView
|
|
|
|
|
2020-11-09 08:43:03 +00:00
|
|
|
class MessageDetailsAttachmentListAdapter(
|
|
|
|
context: Context,
|
|
|
|
private val downloadListener: (String) -> Unit
|
|
|
|
) : ArrayAdapter<Attachment>(context, R.layout.view_attachment_detail) {
|
|
|
|
private val downloadingIds = mutableSetOf<String>()
|
|
|
|
private var pgpEncrypted: Boolean = false
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-11-09 08:43:03 +00:00
|
|
|
@DrawableRes
|
|
|
|
private fun String?.getIconRes(): Int {
|
|
|
|
return when (this) {
|
|
|
|
"image/jpeg", "image/pjpeg" -> R.drawable.mail_attachment_jpeg
|
|
|
|
"image/png" -> R.drawable.mail_attachment_png
|
|
|
|
"application/zip", "application/x-compressed", "application/x-zip-compressed", "multipart/x-zip" -> R.drawable.mail_attachment_zip
|
|
|
|
"text/plain" -> R.drawable.mail_attachment_txt
|
|
|
|
"application/pdf" -> R.drawable.mail_attachment_pdf
|
|
|
|
"application/msword" -> R.drawable.mail_attachment_doc
|
|
|
|
else -> R.drawable.mail_attachment_file
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-11-09 08:43:03 +00:00
|
|
|
fun setList(attachmentsList: List<Attachment>) {
|
|
|
|
downloadingIds.clear()
|
|
|
|
clear()
|
|
|
|
addAll(attachmentsList)
|
|
|
|
}
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-11-09 08:43:03 +00:00
|
|
|
fun setDownloaded(attachmentId: String, isDownloaded: Boolean) {
|
|
|
|
if (isDownloaded) {
|
|
|
|
downloadingIds.remove(attachmentId)
|
|
|
|
} else {
|
|
|
|
downloadingIds.add(attachmentId)
|
|
|
|
}
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-11-09 08:43:03 +00:00
|
|
|
fun setIsPgpEncrypted(isPgpEncrypted: Boolean) {
|
|
|
|
pgpEncrypted = isPgpEncrypted
|
|
|
|
}
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-11-09 08:43:03 +00:00
|
|
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-11-09 08:43:03 +00:00
|
|
|
val view = convertView as? AttachmentDetailView ?: AttachmentDetailView(context)
|
|
|
|
val attachment = getItem(position) ?: return view
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-11-09 08:43:03 +00:00
|
|
|
val fileName = attachment.fileName
|
|
|
|
val fileSize = attachment.fileSize
|
|
|
|
val mimeType = attachment.mimeType
|
|
|
|
val attachmentSpecificIcon = mimeType.getIconRes()
|
|
|
|
val headers = attachment.headers
|
|
|
|
val showWarningIcon = headers != null && headers.contentEncryption == "on-delivery" && pgpEncrypted
|
|
|
|
val attachmentId = attachment.attachmentId
|
|
|
|
val isDownloading = downloadingIds.contains(attachmentId)
|
|
|
|
view.bind(fileName, fileSize, attachmentSpecificIcon, showWarningIcon, isDownloading)
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-11-09 08:43:03 +00:00
|
|
|
val onClickListener = View.OnClickListener { downloadListener(attachmentId!!) }
|
|
|
|
view.setOnClickListener(onClickListener)
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-11-09 08:43:03 +00:00
|
|
|
return view
|
|
|
|
}
|
2020-04-16 15:44:53 +00:00
|
|
|
}
|