From 95e101eb656d01625e0f7789171504011c81d029 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Sat, 8 Jan 2022 22:17:41 -0500 Subject: [PATCH] WIP:Image preview in detail view --- .../io/heckel/ntfy/msg/NotificationService.kt | 11 ++++------- .../java/io/heckel/ntfy/ui/DetailAdapter.kt | 19 +++++++++++++++++++ app/src/main/java/io/heckel/ntfy/util/Util.kt | 4 ++++ .../main/res/layout/fragment_detail_item.xml | 13 ++++++++++--- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/io/heckel/ntfy/msg/NotificationService.kt b/app/src/main/java/io/heckel/ntfy/msg/NotificationService.kt index 22b708a..e69c83b 100644 --- a/app/src/main/java/io/heckel/ntfy/msg/NotificationService.kt +++ b/app/src/main/java/io/heckel/ntfy/msg/NotificationService.kt @@ -14,10 +14,7 @@ import io.heckel.ntfy.data.Notification import io.heckel.ntfy.data.Subscription import io.heckel.ntfy.ui.DetailActivity import io.heckel.ntfy.ui.MainActivity -import io.heckel.ntfy.util.formatBytes -import io.heckel.ntfy.util.formatDateShort -import io.heckel.ntfy.util.formatMessage -import io.heckel.ntfy.util.formatTitle +import io.heckel.ntfy.util.* class NotificationService(val context: Context) { private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager @@ -73,7 +70,7 @@ class NotificationService(val context: Context) { if (att.name != null) infos.add(att.name) if (att.size != null) infos.add(formatBytes(att.size)) //if (att.expires != null && att.expires != 0L) infos.add(formatDateShort(att.expires)) - if (progress >= 0) infos.add("${progress}%") + if (progress in 0..99) infos.add("${progress}%") if (infos.size == 0) return message if (progress < 100) return "Downloading ${infos.joinToString(", ")}\n${message}" return "${message}\nFile: ${infos.joinToString(", ")}" @@ -90,8 +87,8 @@ class NotificationService(val context: Context) { private fun setStyle(builder: NotificationCompat.Builder, notification: Notification, message: String) { val contentUri = notification.attachment?.contentUri - val isImage = listOf("image/jpeg", "image/png").contains(notification.attachment?.type) - if (contentUri != null && isImage) { + val isSupportedImage = supportedImage(notification.attachment?.type) + if (contentUri != null && isSupportedImage) { try { val resolver = context.applicationContext.contentResolver val bitmapStream = resolver.openInputStream(Uri.parse(contentUri)) diff --git a/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt b/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt index 662e022..dc85524 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt @@ -1,11 +1,14 @@ package io.heckel.ntfy.ui +import android.graphics.BitmapFactory +import android.net.Uri import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView +import androidx.core.app.NotificationCompat import androidx.core.content.ContextCompat import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter @@ -49,6 +52,7 @@ class DetailAdapter(private val onClick: (Notification) -> Unit, private val onL private val messageView: TextView = itemView.findViewById(R.id.detail_item_message_text) private val newImageView: View = itemView.findViewById(R.id.detail_item_new_dot) private val tagsView: TextView = itemView.findViewById(R.id.detail_item_tags) + private val imageView: ImageView = itemView.findViewById(R.id.detail_item_image) fun bind(notification: Notification) { this.notification = notification @@ -97,6 +101,21 @@ class DetailAdapter(private val onClick: (Notification) -> Unit, private val onL priorityImageView.setImageDrawable(ContextCompat.getDrawable(ctx, R.drawable.ic_priority_5_24dp)) } } + // 📄 + val contentUri = notification.attachment?.contentUri + if (contentUri != null && supportedImage(notification.attachment.type)) { + try { + val resolver = itemView.context.applicationContext.contentResolver + val bitmapStream = resolver.openInputStream(Uri.parse(contentUri)) + val bitmap = BitmapFactory.decodeStream(bitmapStream) + imageView.setImageBitmap(bitmap) + imageView.visibility = View.VISIBLE + } catch (_: Exception) { + imageView.visibility = View.GONE + } + } else { + imageView.visibility = View.GONE + } } } diff --git a/app/src/main/java/io/heckel/ntfy/util/Util.kt b/app/src/main/java/io/heckel/ntfy/util/Util.kt index 04a19d1..b39c89b 100644 --- a/app/src/main/java/io/heckel/ntfy/util/Util.kt +++ b/app/src/main/java/io/heckel/ntfy/util/Util.kt @@ -144,3 +144,7 @@ fun formatBytes(bytes: Long): String { value *= java.lang.Long.signum(bytes).toLong() return java.lang.String.format("%.1f %ciB", value / 1024.0, ci.current()) } + +fun supportedImage(mimeType: String?): Boolean { + return listOf("image/jpeg", "image/png").contains(mimeType) +} diff --git a/app/src/main/res/layout/fragment_detail_item.xml b/app/src/main/res/layout/fragment_detail_item.xml index c1fa5e8..994e645 100644 --- a/app/src/main/res/layout/fragment_detail_item.xml +++ b/app/src/main/res/layout/fragment_detail_item.xml @@ -47,11 +47,10 @@ android:layout_height="wrap_content" android:id="@+id/detail_item_tags" android:textAppearance="@style/TextAppearance.AppCompat.Small" - app:layout_constraintBottom_toBottomOf="parent" - android:layout_marginBottom="10dp" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="10dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="10dp" - app:layout_constraintTop_toBottomOf="@id/detail_item_message_text"/> + app:layout_constraintTop_toBottomOf="@id/detail_item_message_text" + app:layout_constraintBottom_toTopOf="@id/detail_item_image"/> +