Create generic file binder to allow viewing any unhandled mms part type

This commit is contained in:
moezbhatti
2019-10-17 01:31:44 -04:00
parent d46193abba
commit 629e7a2d72
13 changed files with 229 additions and 60 deletions
@@ -27,6 +27,6 @@ fun MmsPart.isImage() = ContentType.isImageType(type)
fun MmsPart.isVideo() = ContentType.isVideoType(type)
fun MmsPart.isText() = ContentType.isTextType(type)
fun MmsPart.isText() = ContentType.TEXT_PLAIN == type
fun MmsPart.isVCard() = ContentType.TEXT_VCARD == type
fun MmsPart.isVCard() = ContentType.TEXT_VCARD == type
@@ -18,20 +18,13 @@
*/
package com.moez.QKSMS.mapper
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.Telephony
import com.moez.QKSMS.extensions.isImage
import com.moez.QKSMS.extensions.isSmil
import com.moez.QKSMS.extensions.isText
import com.moez.QKSMS.extensions.isVideo
import androidx.core.database.getIntOrNull
import androidx.core.database.getStringOrNull
import com.moez.QKSMS.model.MmsPart
import com.moez.QKSMS.util.tryOrNull
import timber.log.Timber
import java.io.BufferedReader
import java.io.InputStreamReader
import javax.inject.Inject
class CursorToPartImpl @Inject constructor(private val context: Context) : CursorToPart {
@@ -42,41 +35,12 @@ class CursorToPartImpl @Inject constructor(private val context: Context) : Curso
override fun map(from: Cursor) = MmsPart().apply {
id = from.getLong(from.getColumnIndexOrThrow(Telephony.Mms.Part._ID))
// Type will sometimes return null, resulting in a crash if we don't default to an empty string
type = from.getString(from.getColumnIndexOrThrow(Telephony.Mms.Part.CONTENT_TYPE)) ?: ""
val data = from.getString(from.getColumnIndexOrThrow(Telephony.Mms.Part._DATA))
when {
isSmil() || isImage() || isVideo() -> {
// Do nothing special
}
isText() -> {
text = if (data == null) {
from.getString(from.getColumnIndexOrThrow("text"))
} else {
val uri = ContentUris.withAppendedId(CONTENT_URI, id)
val sb = StringBuilder()
val inputStream = tryOrNull(false) { context.contentResolver.openInputStream(uri) }
inputStream?.use {
val isr = InputStreamReader(inputStream, "UTF-8")
val reader = BufferedReader(isr)
var temp = reader.readLine()
while (temp != null) {
sb.append(temp)
temp = reader.readLine()
}
}
sb.toString()
}
}
else -> Timber.v("Unhandled type: $type")
}
type = from.getStringOrNull(from.getColumnIndexOrThrow(Telephony.Mms.Part.CONTENT_TYPE)) ?: "*/*"
seq = from.getIntOrNull(from.getColumnIndexOrThrow(Telephony.Mms.Part.SEQ)) ?: -1
name = from.getStringOrNull(from.getColumnIndexOrThrow(Telephony.Mms.Part.NAME))
?: from.getStringOrNull(from.getColumnIndexOrThrow(Telephony.Mms.Part.CONTENT_LOCATION))
?.split("/")?.last()
text = from.getStringOrNull(from.getColumnIndexOrThrow(Telephony.Mms.Part.TEXT))
}
override fun getPartsCursor(messageId: Long): Cursor? {
@@ -85,6 +85,10 @@ class QkRealmMigration : RealmMigration {
?.addField("blockingClient", Integer::class.java)
?.addField("blockReason", String::class.java)
realm.schema.get("MmsPart")
?.addField("seq", Integer::class.java, FieldAttribute.REQUIRED)
?.addField("name", String::class.java)
version++
}
@@ -105,6 +105,7 @@ open class Message : RealmObject() {
isSms() -> body
else -> parts
.filter { it.type == "text/plain" }
.mapNotNull { it.text }
.joinToString("\n") { text -> text }
}
@@ -28,6 +28,8 @@ open class MmsPart : RealmObject() {
@PrimaryKey var id: Long = 0
var type: String = ""
var seq: Int = -1
var name: String? = null
var text: String? = null
@LinkingObjects("parts")
@@ -245,9 +245,9 @@ class Navigator @Inject constructor(
startActivityExternal(intent)
}
fun saveVcard(uri: Uri) {
fun openFile(uri: Uri, contentType: String) {
val intent = Intent(Intent.ACTION_VIEW)
.setDataAndType(uri, "text/x-vcard")
.setDataAndType(uri, contentType)
startActivityExternal(intent)
}
@@ -47,9 +47,8 @@ import com.moez.QKSMS.common.util.extensions.setPadding
import com.moez.QKSMS.common.util.extensions.setTint
import com.moez.QKSMS.common.util.extensions.setVisible
import com.moez.QKSMS.compat.SubscriptionManagerCompat
import com.moez.QKSMS.extensions.isImage
import com.moez.QKSMS.extensions.isVCard
import com.moez.QKSMS.extensions.isVideo
import com.moez.QKSMS.extensions.isSmil
import com.moez.QKSMS.extensions.isText
import com.moez.QKSMS.feature.compose.BubbleUtils.canGroup
import com.moez.QKSMS.feature.compose.BubbleUtils.getBubble
import com.moez.QKSMS.feature.compose.part.PartsAdapter
@@ -226,7 +225,7 @@ class MessagesAdapter @Inject constructor(
view.simIndex.setVisible(message.subId != previous?.subId && simIndex != -1)
// Bind the grouping
val media = message.parts.filter { it.isImage() || it.isVideo() || it.isVCard() }
val media = message.parts.filter { !it.isSmil() && !it.isText() }
view.setPadding(bottom = if (canGroup(message, next)) 0 else 16.dpToPx(context))
// Bind the avatar
@@ -242,9 +241,9 @@ class MessagesAdapter @Inject constructor(
false -> {
val subject = message.getCleansedSubject()
val body = message.parts
.filter { part -> !part.isVCard() }
.filter { part -> part.isText() }
.mapNotNull { part -> part.text }
.filter { part -> part.isNotBlank() }
.filter { text -> text.isNotBlank() }
.joinToString("\n")
when {
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2019 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS 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.
*
* QKSMS 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.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.feature.compose.part
import android.annotation.SuppressLint
import android.content.ContentUris
import android.content.Context
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import com.moez.QKSMS.R
import com.moez.QKSMS.common.Navigator
import com.moez.QKSMS.common.util.Colors
import com.moez.QKSMS.common.util.extensions.resolveThemeColor
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
import com.moez.QKSMS.common.util.extensions.setTint
import com.moez.QKSMS.feature.compose.BubbleUtils
import com.moez.QKSMS.mapper.CursorToPartImpl
import com.moez.QKSMS.model.Message
import com.moez.QKSMS.model.MmsPart
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.mms_file_list_item.view.*
class FileBinder(
private val context: Context,
private val navigator: Navigator,
private val theme: Colors.Theme
) : PartBinder {
override val partLayout = R.layout.mms_file_list_item
// This is the last binder we check. If we're here, we can bind the part
override fun canBindPart(part: MmsPart) = true
@SuppressLint("CheckResult")
override fun bindPart(
view: View,
part: MmsPart,
message: Message,
canGroupWithPrevious: Boolean,
canGroupWithNext: Boolean
) {
val uri = ContentUris.withAppendedId(CursorToPartImpl.CONTENT_URI, part.id)
val bubble = BubbleUtils.getBubble(false, canGroupWithPrevious, canGroupWithNext, message.isMe())
view.setOnClickListener { navigator.openFile(uri, part.type) }
view.fileBackground.setBackgroundResource(bubble)
Observable.just(part.getUri())
.map(context.contentResolver::openInputStream)
.map { inputStream -> inputStream.use { it.available() } }
.map { bytes ->
when (bytes) {
in 0..999 -> "$bytes B"
in 1000..999999 -> "${"%.1f".format(bytes / 1000f)} KB"
in 1000000..9999999 -> "${"%.1f".format(bytes / 1000000f)} MB"
else -> "${"%.1f".format(bytes / 1000000000f)} GB"
}
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { size -> view.size.text = size }
view.filename.text = part.name
val params = view.fileBackground.layoutParams as FrameLayout.LayoutParams
if (!message.isMe()) {
view.fileBackground.layoutParams = params.apply { gravity = Gravity.START }
view.fileBackground.setBackgroundTint(theme.theme)
view.icon.setTint(theme.textPrimary)
view.filename.setTextColor(theme.textPrimary)
view.size.setTextColor(theme.textTertiary)
} else {
view.fileBackground.layoutParams = params.apply { gravity = Gravity.END }
view.fileBackground.setBackgroundTint(view.context.resolveThemeColor(R.attr.bubbleColor))
view.icon.setTint(view.context.resolveThemeColor(android.R.attr.textColorSecondary))
view.filename.setTextColor(view.context.resolveThemeColor(android.R.attr.textColorPrimary))
view.size.setTextColor(view.context.resolveThemeColor(android.R.attr.textColorTertiary))
}
}
}
@@ -27,9 +27,8 @@ import com.moez.QKSMS.common.base.QkAdapter
import com.moez.QKSMS.common.base.QkViewHolder
import com.moez.QKSMS.common.util.Colors
import com.moez.QKSMS.common.util.extensions.forwardTouches
import com.moez.QKSMS.extensions.isImage
import com.moez.QKSMS.extensions.isVCard
import com.moez.QKSMS.extensions.isVideo
import com.moez.QKSMS.extensions.isSmil
import com.moez.QKSMS.extensions.isText
import com.moez.QKSMS.feature.compose.BubbleUtils.canGroup
import com.moez.QKSMS.model.Message
import com.moez.QKSMS.model.MmsPart
@@ -39,7 +38,8 @@ class PartsAdapter(context: Context, navigator: Navigator, theme: Colors.Theme)
private val partBinders = listOf(
MediaBinder(context, navigator),
VCardBinder(context, navigator, theme)
VCardBinder(context, navigator, theme),
FileBinder(context, navigator, theme)
)
private lateinit var message: Message
@@ -54,7 +54,7 @@ class PartsAdapter(context: Context, navigator: Navigator, theme: Colors.Theme)
this.next = next
this.messageView = messageView
this.bodyVisible = messageView.body.visibility == View.VISIBLE
this.data = message.parts.filter { it.isImage() || it.isVideo() || it.isVCard() }
this.data = message.parts.filter { !it.isSmil() && !it.isText() }
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
@@ -61,7 +61,7 @@ class VCardBinder(
val uri = ContentUris.withAppendedId(CursorToPartImpl.CONTENT_URI, part.id)
val bubble = BubbleUtils.getBubble(false, canGroupWithPrevious, canGroupWithNext, message.isMe())
view.setOnClickListener { navigator.saveVcard(uri) }
view.setOnClickListener { navigator.openFile(uri, part.type) }
view.vCardBackground.setBackgroundResource(bubble)
Observable.just(uri)
@@ -0,0 +1,27 @@
<!--
~ Copyright (C) 2019 Moez Bhatti <moez.bhatti@gmail.com>
~
~ This file is part of QKSMS.
~
~ QKSMS 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.
~
~ QKSMS 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.
~
~ You should have received a copy of the GNU General Public License
~ along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M2,12.5C2,9.46 4.46,7 7.5,7H18c2.21,0 4,1.79 4,4s-1.79,4 -4,4H9.5C8.12,15 7,13.88 7,12.5S8.12,10 9.5,10H17v2H9.41c-0.55,0 -0.55,1 0,1H18c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2H7.5C5.57,9 4,10.57 4,12.5S5.57,16 7.5,16H17v2H7.5C4.46,18 2,15.54 2,12.5z"/>
</vector>
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2019 Moez Bhatti <moez.bhatti@gmail.com>
~
~ This file is part of QKSMS.
~
~ QKSMS 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.
~
~ QKSMS 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.
~
~ You should have received a copy of the GNU General Public License
~ along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
-->
<FrameLayout 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/fileBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="12dp"
android:src="@drawable/ic_attachment_black_24dp"
android:tint="?android:attr/textColorSecondary"
app:layout_constraintBottom_toBottomOf="@id/fileBackground"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/fileBackground" />
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/filename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="12dp"
android:paddingTop="8dp"
android:paddingEnd="12dp"
app:layout_constraintTop_toTopOf="parent"
android:textColor="?android:attr/textColorPrimary"
app:layout_constraintStart_toEndOf="@id/icon"
app:textSize="primary"
tools:text="Resume.pdf" />
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="12dp"
android:paddingEnd="12dp"
tools:text="35 KB"
android:paddingBottom="8dp"
android:textColor="?android:attr/textColorTertiary"
app:layout_constraintStart_toStartOf="@id/filename"
app:layout_constraintTop_toBottomOf="@id/filename"
app:textSize="secondary" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
@@ -49,6 +49,7 @@
android:paddingEnd="12dp"
android:textColor="?android:attr/textColorPrimary"
app:layout_constraintStart_toEndOf="@id/vCardAvatar"
app:layout_constraintTop_toTopOf="parent"
app:textSize="primary"
tools:text="@tools:sample/full_names" />