From 629e7a2d72e30aa3ae797c5132729380b0227899 Mon Sep 17 00:00:00 2001 From: moezbhatti Date: Thu, 17 Oct 2019 01:31:44 -0400 Subject: [PATCH] Create generic file binder to allow viewing any unhandled mms part type --- .../QKSMS/extensions/MmsPartExtensions.kt | 4 +- .../com/moez/QKSMS/mapper/CursorToPartImpl.kt | 52 ++------- .../moez/QKSMS/migration/QkRealmMigration.kt | 4 + .../main/java/com/moez/QKSMS/model/Message.kt | 1 + .../main/java/com/moez/QKSMS/model/MmsPart.kt | 2 + .../java/com/moez/QKSMS/common/Navigator.kt | 4 +- .../QKSMS/feature/compose/MessagesAdapter.kt | 11 +- .../QKSMS/feature/compose/part/FileBinder.kt | 100 ++++++++++++++++++ .../feature/compose/part/PartsAdapter.kt | 10 +- .../QKSMS/feature/compose/part/VCardBinder.kt | 2 +- .../res/drawable/ic_attachment_black_24dp.xml | 27 +++++ .../main/res/layout/mms_file_list_item.xml | 71 +++++++++++++ .../main/res/layout/mms_vcard_list_item.xml | 1 + 13 files changed, 229 insertions(+), 60 deletions(-) create mode 100644 presentation/src/main/java/com/moez/QKSMS/feature/compose/part/FileBinder.kt create mode 100644 presentation/src/main/res/drawable/ic_attachment_black_24dp.xml create mode 100644 presentation/src/main/res/layout/mms_file_list_item.xml diff --git a/data/src/main/java/com/moez/QKSMS/extensions/MmsPartExtensions.kt b/data/src/main/java/com/moez/QKSMS/extensions/MmsPartExtensions.kt index a4d2135c..ea890c8c 100644 --- a/data/src/main/java/com/moez/QKSMS/extensions/MmsPartExtensions.kt +++ b/data/src/main/java/com/moez/QKSMS/extensions/MmsPartExtensions.kt @@ -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 \ No newline at end of file +fun MmsPart.isVCard() = ContentType.TEXT_VCARD == type diff --git a/data/src/main/java/com/moez/QKSMS/mapper/CursorToPartImpl.kt b/data/src/main/java/com/moez/QKSMS/mapper/CursorToPartImpl.kt index a6da71a7..7624f4cb 100644 --- a/data/src/main/java/com/moez/QKSMS/mapper/CursorToPartImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/mapper/CursorToPartImpl.kt @@ -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? { diff --git a/data/src/main/java/com/moez/QKSMS/migration/QkRealmMigration.kt b/data/src/main/java/com/moez/QKSMS/migration/QkRealmMigration.kt index 43147fea..bd57a451 100644 --- a/data/src/main/java/com/moez/QKSMS/migration/QkRealmMigration.kt +++ b/data/src/main/java/com/moez/QKSMS/migration/QkRealmMigration.kt @@ -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++ } diff --git a/domain/src/main/java/com/moez/QKSMS/model/Message.kt b/domain/src/main/java/com/moez/QKSMS/model/Message.kt index 8ab4c230..82d04059 100644 --- a/domain/src/main/java/com/moez/QKSMS/model/Message.kt +++ b/domain/src/main/java/com/moez/QKSMS/model/Message.kt @@ -105,6 +105,7 @@ open class Message : RealmObject() { isSms() -> body else -> parts + .filter { it.type == "text/plain" } .mapNotNull { it.text } .joinToString("\n") { text -> text } } diff --git a/domain/src/main/java/com/moez/QKSMS/model/MmsPart.kt b/domain/src/main/java/com/moez/QKSMS/model/MmsPart.kt index acc65510..d1afb35d 100644 --- a/domain/src/main/java/com/moez/QKSMS/model/MmsPart.kt +++ b/domain/src/main/java/com/moez/QKSMS/model/MmsPart.kt @@ -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") diff --git a/presentation/src/main/java/com/moez/QKSMS/common/Navigator.kt b/presentation/src/main/java/com/moez/QKSMS/common/Navigator.kt index f470ce2d..d4c3c3de 100644 --- a/presentation/src/main/java/com/moez/QKSMS/common/Navigator.kt +++ b/presentation/src/main/java/com/moez/QKSMS/common/Navigator.kt @@ -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) } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/MessagesAdapter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/MessagesAdapter.kt index b1dc02e9..67958f3d 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/MessagesAdapter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/MessagesAdapter.kt @@ -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 { diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/part/FileBinder.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/part/FileBinder.kt new file mode 100644 index 00000000..c2f3b03f --- /dev/null +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/part/FileBinder.kt @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2019 Moez Bhatti + * + * 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 . + */ +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)) + } + } + +} \ No newline at end of file diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/part/PartsAdapter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/part/PartsAdapter.kt index a2e4a047..ac02cf8d 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/part/PartsAdapter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/part/PartsAdapter.kt @@ -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 { diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/part/VCardBinder.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/part/VCardBinder.kt index 5d6234ac..9407f6af 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/part/VCardBinder.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/part/VCardBinder.kt @@ -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) diff --git a/presentation/src/main/res/drawable/ic_attachment_black_24dp.xml b/presentation/src/main/res/drawable/ic_attachment_black_24dp.xml new file mode 100644 index 00000000..d9940a26 --- /dev/null +++ b/presentation/src/main/res/drawable/ic_attachment_black_24dp.xml @@ -0,0 +1,27 @@ + + + + diff --git a/presentation/src/main/res/layout/mms_file_list_item.xml b/presentation/src/main/res/layout/mms_file_list_item.xml new file mode 100644 index 00000000..4c1550ee --- /dev/null +++ b/presentation/src/main/res/layout/mms_file_list_item.xml @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + + diff --git a/presentation/src/main/res/layout/mms_vcard_list_item.xml b/presentation/src/main/res/layout/mms_vcard_list_item.xml index 3a075bdc..b237b016 100644 --- a/presentation/src/main/res/layout/mms_vcard_list_item.xml +++ b/presentation/src/main/res/layout/mms_vcard_list_item.xml @@ -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" />