From 8d75d5b133895bfc735ae77786285a1d4fbac7de Mon Sep 17 00:00:00 2001
From: Naveen <snaveen935@gmail.com>
Date: Sun, 23 Oct 2022 18:34:43 +0530
Subject: [PATCH] Improve unknown attachment preview

---
 .../smsmessenger/adapters/ThreadAdapter.kt    | 85 ++++++++-----------
 .../smsmessenger/extensions/String.kt         |  4 +
 .../main/res/drawable/ic_document_vector.xml  |  5 ++
 .../item_received_unknown_attachment.xml      | 23 -----
 .../res/layout/item_unknown_attachment.xml    | 51 +++++++++++
 5 files changed, 96 insertions(+), 72 deletions(-)
 create mode 100644 app/src/main/res/drawable/ic_document_vector.xml
 delete mode 100644 app/src/main/res/layout/item_received_unknown_attachment.xml
 create mode 100644 app/src/main/res/layout/item_unknown_attachment.xml

diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt
index cace2918..06366948 100644
--- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt
@@ -44,13 +44,12 @@ import kotlinx.android.synthetic.main.item_received_message.view.thread_mesage_a
 import kotlinx.android.synthetic.main.item_received_message.view.thread_message_body
 import kotlinx.android.synthetic.main.item_received_message.view.thread_message_holder
 import kotlinx.android.synthetic.main.item_received_message.view.thread_message_play_outline
-import kotlinx.android.synthetic.main.item_received_unknown_attachment.view.*
 import kotlinx.android.synthetic.main.item_sent_message.view.*
-import kotlinx.android.synthetic.main.item_sent_unknown_attachment.view.*
 import kotlinx.android.synthetic.main.item_thread_date_time.view.*
 import kotlinx.android.synthetic.main.item_thread_error.view.*
 import kotlinx.android.synthetic.main.item_thread_sending.view.*
 import kotlinx.android.synthetic.main.item_thread_success.view.*
+import kotlinx.android.synthetic.main.item_unknown_attachment.view.*
 import java.util.*
 
 class ThreadAdapter(
@@ -286,12 +285,10 @@ class ThreadAdapter(
             if (message.attachment?.attachments?.isNotEmpty() == true) {
                 for (attachment in message.attachment.attachments) {
                     val mimetype = attachment.mimetype
-                    if (mimetype.isImageMimeType() || mimetype.startsWith("video/")) {
-                        setupImageView(holder, view, message, attachment)
-                    } else if (mimetype.isVCardMimeType()) {
-                        setupVCardView(holder, view, message, attachment)
-                    } else {
-                        setupFileView(holder, view, message, attachment)
+                    when {
+                        mimetype.isImageMimeType() || mimetype.isVideoMimeType() -> setupImageView(holder, view, message, attachment)
+                        mimetype.isVCardMimeType() -> setupVCardView(holder, view, message, attachment)
+                        else -> setupFileView(holder, view, message, attachment)
                     }
 
                     thread_message_play_outline.beVisibleIf(mimetype.startsWith("video/"))
@@ -454,51 +451,41 @@ class ThreadAdapter(
         val mimetype = attachment.mimetype
         val uri = attachment.getUri()
         parent.apply {
-            if (message.isReceivedMessage()) {
-                val attachmentView = layoutInflater.inflate(R.layout.item_received_unknown_attachment, null).apply {
-                    thread_received_attachment_label.apply {
-                        if (attachment.filename.isNotEmpty()) {
-                            thread_received_attachment_label.text = attachment.filename
-                        }
-                        setTextColor(textColor)
-                        setOnClickListener {
-                            if (actModeCallback.isSelectable) {
-                                holder.viewClicked(message)
-                            } else {
-                                launchViewIntent(uri, mimetype, attachment.filename)
-                            }
-                        }
-                        setOnLongClickListener {
-                            holder.viewLongClicked()
-                            true
-                        }
+            val attachmentView = layoutInflater.inflate(R.layout.item_unknown_attachment, null).apply {
+                if (attachment.filename.isNotEmpty()) {
+                    filename.text = attachment.filename
+                }
+
+                val size = context.contentResolver
+                    .openInputStream(uri)
+                    ?.use { it.readBytes() }
+                    ?.size
+
+                if (size != null) {
+                    file_size.beVisible()
+                    file_size.text = size.formatSize()
+                } else {
+                    file_size.beGone()
+                }
+
+                background.applyColorFilter(textColor)
+                filename.setTextColor(textColor)
+                file_size.setTextColor(textColor)
+                icon.background.setTint(properPrimaryColor)
+
+                setOnClickListener {
+                    if (actModeCallback.isSelectable) {
+                        holder.viewClicked(message)
+                    } else {
+                        launchViewIntent(uri, mimetype, attachment.filename)
                     }
                 }
-                thread_mesage_attachments_holder.addView(attachmentView)
-            } else {
-                val background = context.getProperPrimaryColor()
-                val attachmentView = layoutInflater.inflate(R.layout.item_sent_unknown_attachment, null).apply {
-                    thread_sent_attachment_label.apply {
-                        this.background.applyColorFilter(background)
-                        setTextColor(background.getContrastColor())
-                        if (attachment.filename.isNotEmpty()) {
-                            thread_sent_attachment_label.text = attachment.filename
-                        }
-                        setOnClickListener {
-                            if (actModeCallback.isSelectable) {
-                                holder.viewClicked(message)
-                            } else {
-                                launchViewIntent(uri, mimetype, attachment.filename)
-                            }
-                        }
-                        setOnLongClickListener {
-                            holder.viewLongClicked()
-                            true
-                        }
-                    }
+                setOnLongClickListener {
+                    holder.viewLongClicked()
+                    true
                 }
-                thread_mesage_attachments_holder.addView(attachmentView)
             }
+            thread_mesage_attachments_holder.addView(attachmentView)
         }
     }
 
diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/String.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/String.kt
index 89fb0f03..ac2b8948 100644
--- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/String.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/String.kt
@@ -15,6 +15,10 @@ fun String.isImageMimeType(): Boolean {
     return lowercase().startsWith("image")
 }
 
+fun String.isVideoMimeType(): Boolean {
+    return lowercase().startsWith("video")
+}
+
 fun String.isVCardMimeType(): Boolean {
     val lowercase = lowercase()
     return lowercase.endsWith("x-vcard") || lowercase.endsWith("vcard")
diff --git a/app/src/main/res/drawable/ic_document_vector.xml b/app/src/main/res/drawable/ic_document_vector.xml
new file mode 100644
index 00000000..e7ef3d4b
--- /dev/null
+++ b/app/src/main/res/drawable/ic_document_vector.xml
@@ -0,0 +1,5 @@
+<vector android:height="24dp" android:tint="#FFFFFF"
+    android:viewportHeight="24" android:viewportWidth="24"
+    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
+    <path android:fillColor="@android:color/white" android:pathData="M14,2L6,2c-1.1,0 -1.99,0.9 -1.99,2L4,20c0,1.1 0.89,2 1.99,2L18,22c1.1,0 2,-0.9 2,-2L20,8l-6,-6zM16,18L8,18v-2h8v2zM16,14L8,14v-2h8v2zM13,9L13,3.5L18.5,9L13,9z"/>
+</vector>
diff --git a/app/src/main/res/layout/item_received_unknown_attachment.xml b/app/src/main/res/layout/item_received_unknown_attachment.xml
deleted file mode 100644
index b56653ca..00000000
--- a/app/src/main/res/layout/item_received_unknown_attachment.xml
+++ /dev/null
@@ -1,23 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@+id/thread_received_attachment_wrapper"
-    android:layout_width="match_parent"
-    android:layout_height="wrap_content">
-
-    <TextView
-        android:id="@+id/thread_received_attachment_label"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_alignParentStart="true"
-        android:background="@drawable/item_received_background"
-        android:drawableStart="@drawable/ic_attach_file_vector"
-        android:drawablePadding="@dimen/small_margin"
-        android:gravity="center_vertical"
-        android:paddingStart="@dimen/small_margin"
-        android:paddingTop="@dimen/normal_margin"
-        android:paddingEnd="@dimen/normal_margin"
-        android:paddingBottom="@dimen/normal_margin"
-        android:text="@string/attachment"
-        android:textSize="@dimen/normal_text_size" />
-
-</RelativeLayout>
diff --git a/app/src/main/res/layout/item_unknown_attachment.xml b/app/src/main/res/layout/item_unknown_attachment.xml
new file mode 100644
index 00000000..8b884084
--- /dev/null
+++ b/app/src/main/res/layout/item_unknown_attachment.xml
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:id="@+id/thread_received_attachment_wrapper"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:background="@drawable/section_holder_stroke"
+    android:foreground="@drawable/ripple_all_corners"
+    android:orientation="vertical"
+    android:padding="@dimen/normal_margin">
+
+    <androidx.appcompat.widget.AppCompatImageView
+        android:id="@+id/icon"
+        android:layout_width="@dimen/normal_icon_size"
+        android:layout_height="@dimen/normal_icon_size"
+        android:background="@drawable/circle_background"
+        android:padding="@dimen/normal_margin"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent"
+        app:srcCompat="@drawable/ic_document_vector" />
+
+    <androidx.appcompat.widget.AppCompatTextView
+        android:id="@+id/filename"
+        android:layout_width="0dp"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="@dimen/normal_margin"
+        android:ellipsize="middle"
+        android:singleLine="true"
+        android:text="@string/attachment"
+        android:textSize="@dimen/bigger_text_size"
+        android:textStyle="bold"
+        app:layout_constraintBottom_toTopOf="@id/file_size"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toEndOf="@id/icon"
+        app:layout_constraintTop_toTopOf="@id/icon"
+        tools:text="Events_2022_02_16.ics" />
+
+    <androidx.appcompat.widget.AppCompatTextView
+        android:id="@+id/file_size"
+        android:layout_width="0dp"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="@dimen/normal_margin"
+        android:textSize="@dimen/bigger_text_size"
+        app:layout_constraintBottom_toBottomOf="@id/icon"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toEndOf="@id/icon"
+        tools:text="2.18 KB" />
+
+</androidx.constraintlayout.widget.ConstraintLayout>