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
@@ -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)