e-ntfy-android/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt

314 lines
15 KiB
Kotlin
Raw Normal View History

2021-10-31 20:19:25 +01:00
package io.heckel.ntfy.ui
import android.app.DownloadManager
2022-01-10 19:46:31 +01:00
import android.content.*
2022-01-09 04:17:41 +01:00
import android.graphics.BitmapFactory
import android.net.Uri
2022-01-10 19:46:31 +01:00
import android.provider.OpenableColumns
2021-11-29 01:28:58 +01:00
import android.util.Log
2021-10-31 20:19:25 +01:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
2021-11-27 22:18:09 +01:00
import androidx.core.content.ContextCompat
2021-10-31 20:19:25 +01:00
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import androidx.work.workDataOf
2021-10-31 20:19:25 +01:00
import io.heckel.ntfy.R
2022-01-10 19:46:31 +01:00
import io.heckel.ntfy.data.Attachment
2021-10-31 20:19:25 +01:00
import io.heckel.ntfy.data.Notification
2022-01-10 19:46:31 +01:00
import io.heckel.ntfy.data.PROGRESS_DONE
import io.heckel.ntfy.data.PROGRESS_NONE
import io.heckel.ntfy.msg.AttachmentDownloadWorker
2021-11-29 01:28:58 +01:00
import io.heckel.ntfy.util.*
2021-10-31 20:19:25 +01:00
import java.util.*
2022-01-10 19:46:31 +01:00
import kotlin.math.exp
2021-10-31 20:19:25 +01:00
2021-11-03 18:56:08 +01:00
class DetailAdapter(private val onClick: (Notification) -> Unit, private val onLongClick: (Notification) -> Unit) :
2021-10-31 20:19:25 +01:00
ListAdapter<Notification, DetailAdapter.DetailViewHolder>(TopicDiffCallback) {
2021-11-03 18:56:08 +01:00
val selected = mutableSetOf<String>() // Notification IDs
2021-10-31 20:19:25 +01:00
/* Creates and inflates view and return TopicViewHolder. */
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DetailViewHolder {
val view = LayoutInflater.from(parent.context)
2021-11-22 21:45:43 +01:00
.inflate(R.layout.fragment_detail_item, parent, false)
2021-11-03 18:56:08 +01:00
return DetailViewHolder(view, selected, onClick, onLongClick)
2021-10-31 20:19:25 +01:00
}
/* Gets current topic and uses it to bind view. */
override fun onBindViewHolder(holder: DetailViewHolder, position: Int) {
holder.bind(getItem(position))
}
2021-11-03 18:56:08 +01:00
fun toggleSelection(notificationId: String) {
if (selected.contains(notificationId)) {
selected.remove(notificationId)
} else {
selected.add(notificationId)
}
}
2021-10-31 20:19:25 +01:00
/* ViewHolder for Topic, takes in the inflated view and the onClick behavior. */
2021-11-03 18:56:08 +01:00
class DetailViewHolder(itemView: View, private val selected: Set<String>, val onClick: (Notification) -> Unit, val onLongClick: (Notification) -> Unit) :
2021-10-31 20:19:25 +01:00
RecyclerView.ViewHolder(itemView) {
private var notification: Notification? = null
2021-11-27 22:18:09 +01:00
private val priorityImageView: ImageView = itemView.findViewById(R.id.detail_item_priority_image)
2021-10-31 20:19:25 +01:00
private val dateView: TextView = itemView.findViewById(R.id.detail_item_date_text)
2021-11-27 22:18:09 +01:00
private val titleView: TextView = itemView.findViewById(R.id.detail_item_title_text)
2021-10-31 20:19:25 +01:00
private val messageView: TextView = itemView.findViewById(R.id.detail_item_message_text)
2022-01-10 19:46:31 +01:00
private val newDotImageView: View = itemView.findViewById(R.id.detail_item_new_dot)
private val tagsView: TextView = itemView.findViewById(R.id.detail_item_tags_text)
private val menuButton: ImageButton = itemView.findViewById(R.id.detail_item_menu_button)
2022-01-10 19:46:31 +01:00
private val attachmentImageView: ImageView = itemView.findViewById(R.id.detail_item_attachment_image)
private val attachmentBoxView: View = itemView.findViewById(R.id.detail_item_attachment_box)
private val attachmentIconView: ImageView = itemView.findViewById(R.id.detail_item_attachment_icon)
private val attachmentInfoView: TextView = itemView.findViewById(R.id.detail_item_attachment_info)
2021-10-31 20:19:25 +01:00
fun bind(notification: Notification) {
this.notification = notification
2021-11-27 22:18:09 +01:00
val context = itemView.context
2021-11-29 01:28:58 +01:00
val unmatchedTags = unmatchedTags(splitTags(notification.tags))
2021-10-31 20:19:25 +01:00
dateView.text = Date(notification.timestamp * 1000).toString()
2021-11-27 22:18:09 +01:00
messageView.text = formatMessage(notification)
2022-01-10 19:46:31 +01:00
newDotImageView.visibility = if (notification.notificationId == 0) View.GONE else View.VISIBLE
2021-10-31 20:19:25 +01:00
itemView.setOnClickListener { onClick(notification) }
2021-11-03 18:56:08 +01:00
itemView.setOnLongClickListener { onLongClick(notification); true }
2021-11-27 22:18:09 +01:00
if (notification.title != "") {
titleView.visibility = View.VISIBLE
titleView.text = formatTitle(notification)
} else {
titleView.visibility = View.GONE
}
2021-11-29 01:28:58 +01:00
if (unmatchedTags.isNotEmpty()) {
tagsView.visibility = View.VISIBLE
tagsView.text = context.getString(R.string.detail_item_tags, unmatchedTags.joinToString(", "))
2021-11-29 01:28:58 +01:00
} else {
tagsView.visibility = View.GONE
}
2021-11-03 18:56:08 +01:00
if (selected.contains(notification.id)) {
itemView.setBackgroundResource(R.color.primarySelectedRowColor);
}
2022-01-10 19:46:31 +01:00
renderPriority(context, notification)
maybeRenderAttachment(context, notification)
}
private fun renderPriority(context: Context, notification: Notification) {
2021-11-27 22:18:09 +01:00
when (notification.priority) {
1 -> {
priorityImageView.visibility = View.VISIBLE
priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_1_24dp))
2021-11-27 22:18:09 +01:00
}
2 -> {
priorityImageView.visibility = View.VISIBLE
priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_2_24dp))
2021-11-27 22:18:09 +01:00
}
3 -> {
priorityImageView.visibility = View.GONE
}
4 -> {
priorityImageView.visibility = View.VISIBLE
priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_4_24dp))
2021-11-27 22:18:09 +01:00
}
5 -> {
priorityImageView.visibility = View.VISIBLE
priorityImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_priority_5_24dp))
2021-11-27 22:18:09 +01:00
}
}
2022-01-10 19:46:31 +01:00
}
private fun maybeRenderAttachment(context: Context, notification: Notification) {
if (notification.attachment == null) {
menuButton.visibility = View.GONE
attachmentImageView.visibility = View.GONE
attachmentBoxView.visibility = View.GONE
return
2022-01-09 04:17:41 +01:00
}
2022-01-10 19:46:31 +01:00
val attachment = notification.attachment
val exists = if (attachment.contentUri != null) fileExists(context, attachment.contentUri) else false
maybeRenderAttachmentImage(context, attachment, exists)
renderAttachmentBox(context, notification, attachment, exists)
}
private fun renderAttachmentBox(context: Context, notification: Notification, attachment: Attachment, exists: Boolean) {
attachmentInfoView.text = formatAttachmentDetails(context, attachment, exists)
attachmentIconView.setImageResource(if (attachment.type?.startsWith("image/") == true) {
R.drawable.ic_file_image_gray_24dp
} else if (attachment.type?.startsWith("video/") == true) {
R.drawable.ic_file_video_gray_24dp
} else if (attachment.type?.startsWith("audio/") == true) {
R.drawable.ic_file_audio_gray_24dp
} else {
R.drawable.ic_file_document_gray_24dp
})
val menuButtonPopupMenu = createAttachmentPopup(context, menuButton, notification, attachment, exists) // Heavy lifting not during on-click
if (menuButtonPopupMenu != null) {
menuButton.setOnClickListener { menuButtonPopupMenu.show() }
menuButton.visibility = View.VISIBLE
2022-01-10 19:46:31 +01:00
} else {
menuButton.visibility = View.GONE
}
val attachmentBoxPopupMenu = createAttachmentPopup(context, attachmentBoxView, notification, attachment, exists) // Heavy lifting not during on-click
if (attachmentBoxPopupMenu != null) {
attachmentBoxView.setOnClickListener { attachmentBoxPopupMenu.show() }
} else {
attachmentBoxView.setOnClickListener {
Toast
.makeText(context, context.getString(R.string.detail_item_cannot_download), Toast.LENGTH_LONG)
.show()
}
}
attachmentBoxView.visibility = View.VISIBLE
}
private fun createAttachmentPopup(context: Context, anchor: View?, notification: Notification, attachment: Attachment, exists: Boolean): PopupMenu? {
val popup = PopupMenu(context, anchor)
popup.menuInflater.inflate(R.menu.menu_detail_attachment, popup.menu)
val downloadItem = popup.menu.findItem(R.id.detail_item_menu_download)
val openItem = popup.menu.findItem(R.id.detail_item_menu_open)
val browseItem = popup.menu.findItem(R.id.detail_item_menu_browse)
val copyUrlItem = popup.menu.findItem(R.id.detail_item_menu_copy_url)
val expired = attachment.expires != null && attachment.expires < System.currentTimeMillis()/1000
if (attachment.contentUri != null) {
openItem.setOnMenuItemClickListener {
try {
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(attachment.contentUri)))
} catch (e: ActivityNotFoundException) {
Toast
.makeText(context, context.getString(R.string.detail_item_cannot_open), Toast.LENGTH_LONG)
.show()
} catch (_: Exception) {
// URI parse exception and others; we don't care!
}
2022-01-10 19:46:31 +01:00
true
}
}
browseItem.setOnMenuItemClickListener {
context.startActivity(Intent(DownloadManager.ACTION_VIEW_DOWNLOADS))
true
}
copyUrlItem.setOnMenuItemClickListener {
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("attachment url", attachment.url)
clipboard.setPrimaryClip(clip)
Toast
.makeText(context, context.getString(R.string.detail_item_menu_copy_url_copied), Toast.LENGTH_LONG)
.show()
true
}
downloadItem.setOnMenuItemClickListener {
scheduleAttachmentDownload(context, notification)
true
}
openItem.isVisible = exists
browseItem.isVisible = exists
downloadItem.isVisible = !exists && !expired
copyUrlItem.isVisible = !expired
val noOptions = !openItem.isVisible && !browseItem.isVisible && !downloadItem.isVisible && !copyUrlItem.isVisible
if (noOptions) {
return null
}
return popup
}
2022-01-10 19:46:31 +01:00
private fun formatAttachmentDetails(context: Context, attachment: Attachment, exists: Boolean): String {
val name = queryAttachmentFilename(context, attachment)
val notYetDownloaded = !exists && attachment.progress == PROGRESS_NONE
val downloading = !exists && attachment.progress in 0..99
val deleted = !exists && attachment.progress == PROGRESS_DONE
val expired = attachment.expires != null && attachment.expires < System.currentTimeMillis()/1000
val expires = attachment.expires != null && attachment.expires > System.currentTimeMillis()/1000
val infos = mutableListOf<String>()
if (attachment.size != null) {
infos.add(formatBytes(attachment.size))
}
if (notYetDownloaded) {
if (expired) {
infos.add("not downloaded, link expired")
} else if (expires) {
infos.add("not downloaded, link expires ${formatDateShort(attachment.expires!!)}")
} else {
infos.add("not downloaded")
}
} else if (downloading) {
infos.add("${attachment.progress}% downloaded")
} else if (deleted) {
if (expired) {
infos.add("deleted, link expired")
} else if (expires) {
infos.add("deleted, link expires ${formatDateShort(attachment.expires!!)}")
} else {
infos.add("deleted")
}
2022-01-10 19:46:31 +01:00
}
return if (infos.size > 0) {
"$name\n${infos.joinToString(", ")}"
} else {
2022-01-10 19:46:31 +01:00
name
}
}
private fun queryAttachmentFilename(context: Context, attachment: Attachment): String {
if (attachment.contentUri == null) {
return attachment.name
}
try {
val resolver = context.applicationContext.contentResolver
val cursor = resolver.query(Uri.parse(attachment.contentUri), null, null, null, null) ?: return attachment.name
return cursor.use { c ->
val nameIndex = c.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME)
c.moveToFirst()
c.getString(nameIndex)
}
} catch (_: Exception) {
return attachment.name
}
}
private fun maybeRenderAttachmentImage(context: Context, att: Attachment, exists: Boolean) {
val fileIsImage = att.contentUri != null && exists && supportedImage(att.type)
if (!fileIsImage) {
attachmentImageView.visibility = View.GONE
return
}
try {
val resolver = context.applicationContext.contentResolver
val bitmapStream = resolver.openInputStream(Uri.parse(att.contentUri))
val bitmap = BitmapFactory.decodeStream(bitmapStream)
attachmentImageView.setImageBitmap(bitmap)
attachmentImageView.visibility = View.VISIBLE
} catch (_: Exception) {
attachmentImageView.visibility = View.GONE
}
}
private fun scheduleAttachmentDownload(context: Context, notification: Notification) {
Log.d(TAG, "Enqueuing work to download attachment")
val workManager = WorkManager.getInstance(context)
val workRequest = OneTimeWorkRequest.Builder(AttachmentDownloadWorker::class.java)
.setInputData(workDataOf("id" to notification.id))
.build()
workManager.enqueue(workRequest)
2021-10-31 20:19:25 +01:00
}
}
object TopicDiffCallback : DiffUtil.ItemCallback<Notification>() {
override fun areItemsTheSame(oldItem: Notification, newItem: Notification): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Notification, newItem: Notification): Boolean {
return oldItem == newItem
}
}
companion object {
const val TAG = "NtfyDetailAdapter"
}
2021-10-31 20:19:25 +01:00
}