Add vcard to message

This commit is contained in:
moezbhatti
2018-12-25 22:44:47 -05:00
parent f6e07d9bc3
commit 14b2d2ef8b
11 changed files with 177 additions and 36 deletions
@@ -235,10 +235,9 @@ class MessageRepositoryImpl @Inject constructor(
parts += MMSPart("text", ContentType.TEXT_PLAIN, body.toByteArray())
}
// Add the GIFs as attachments. The app currently can't compress them, which may result
// in a lot of these messages failing to send
// TODO Add support for GIF compression
// Add the GIFs as attachments
parts += attachments
.mapNotNull { attachment -> attachment as? Attachment.Image }
.filter { attachment -> attachment.isGif(context) }
.mapNotNull { attachment -> attachment.getUri() }
.map { uri -> ImageUtils.compressGif(context, uri, prefs.mmsSize.get() * 1024) }
@@ -247,6 +246,7 @@ class MessageRepositoryImpl @Inject constructor(
// Compress the images and add them as attachments
var totalImageBytes = 0
parts += attachments
.mapNotNull { attachment -> attachment as? Attachment.Image }
.filter { attachment -> !attachment.isGif(context) }
.mapNotNull { attachment -> attachment.getUri() }
.mapNotNull { uri -> tryOrNull { imageRepository.loadImage(uri) } }
@@ -257,6 +257,11 @@ class MessageRepositoryImpl @Inject constructor(
}
.map { bitmap -> MMSPart("image", ContentType.IMAGE_JPEG, bitmap) }
// Send contacts
parts += attachments
.mapNotNull { attachment -> attachment as? Attachment.Contact }
.map { attachment -> attachment.vCard.toByteArray() }
.map { vCard -> MMSPart("contact", ContentType.TEXT_VCARD, vCard) }
val transaction = Transaction(context)
transaction.sendNewMessage(subId, threadId, addresses.map(PhoneNumberUtils::stripSeparators), parts, null)
@@ -47,7 +47,7 @@ class SendScheduledMessage @Inject constructor(
}
.map { message ->
val threadId = TelephonyCompat.getOrCreateThreadId(context, message.recipients)
val attachments = message.attachments.mapNotNull(Uri::parse).map { Attachment(it) }
val attachments = message.attachments.mapNotNull(Uri::parse).map { Attachment.Image(it) }
SendMessage.Params(message.subId, threadId, message.recipients, message.body, attachments)
}
.flatMap(sendMessage::buildObservable)
@@ -23,22 +23,32 @@ import android.net.Uri
import android.os.Build
import androidx.core.view.inputmethod.InputContentInfoCompat
data class Attachment(private val uri: Uri? = null, private val inputContent: InputContentInfoCompat? = null) {
sealed class Attachment {
fun getUri(): Uri? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
inputContent?.contentUri ?: uri
} else {
uri
data class Image(
private val uri: Uri? = null,
private val inputContent: InputContentInfoCompat? = null
) : Attachment() {
fun getUri(): Uri? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
inputContent?.contentUri ?: uri
} else {
uri
}
}
fun isGif(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && inputContent != null) {
inputContent.description.hasMimeType("image/gif")
} else {
context.contentResolver.getType(uri) == "image/gif"
}
}
}
fun isGif(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && inputContent != null) {
inputContent.description.hasMimeType("image/gif")
} else {
context.contentResolver.getType(uri) == "image/gif"
}
}
data class Contact(val vCard: String) : Attachment()
}
}
class Attachments(attachments: List<Attachment>): List<Attachment> by attachments
@@ -25,22 +25,39 @@ import com.bumptech.glide.Glide
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkAdapter
import com.moez.QKSMS.common.base.QkViewHolder
import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.model.Attachment
import ezvcard.Ezvcard
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.attachment_list_item.view.*
import kotlinx.android.synthetic.main.attachment_contact_list_item.view.*
import kotlinx.android.synthetic.main.attachment_image_list_item.view.*
import javax.inject.Inject
class AttachmentAdapter @Inject constructor(
private val context: Context
) : QkAdapter<Attachment>() {
companion object {
private const val VIEW_TYPE_IMAGE = 0
private const val VIEW_TYPE_CONTACT = 1
}
val attachmentDeleted: Subject<Attachment> = PublishSubject.create()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.attachment_list_item, parent, false)
val inflater = LayoutInflater.from(parent.context)
val view = when (viewType) {
VIEW_TYPE_IMAGE -> inflater.inflate(R.layout.attachment_image_list_item, parent, false)
.apply { thumbnailBounds.clipToOutline = true }
view.thumbnailBounds.clipToOutline = true
VIEW_TYPE_CONTACT -> inflater.inflate(R.layout.attachment_contact_list_item, parent, false)
else -> null!! // Impossible
}
return QkViewHolder(view).apply {
view.setOnClickListener {
@@ -54,7 +71,22 @@ class AttachmentAdapter @Inject constructor(
val attachment = getItem(position)
val view = holder.itemView
Glide.with(context).load(attachment.getUri()).into(view.thumbnail)
when (attachment) {
is Attachment.Image -> Glide.with(context)
.load(attachment.getUri())
.into(view.thumbnail)
is Attachment.Contact -> Observable.just(attachment.vCard)
.mapNotNull { vCard -> Ezvcard.parse(vCard).first() }
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { vcard -> view.name?.text = vcard.formattedName.value }
}
}
override fun getItemViewType(position: Int) = when (getItem(position)) {
is Attachment.Image -> VIEW_TYPE_IMAGE
is Attachment.Contact -> VIEW_TYPE_CONTACT
}
}
@@ -65,7 +65,6 @@ import java.text.SimpleDateFormat
import java.util.*
import javax.inject.Inject
class ComposeActivity : QkThemedActivity(), ComposeView {
companion object {
@@ -23,6 +23,7 @@ import android.net.Uri
import androidx.lifecycle.ViewModel
import com.moez.QKSMS.injection.ViewModelKey
import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.model.Attachments
import dagger.Module
import dagger.Provides
import dagger.multibindings.IntoMap
@@ -70,11 +71,11 @@ class ComposeActivityModule {
@Provides
@Named("attachments")
fun provideSharedAttachments(activity: ComposeActivity): List<Attachment> {
fun provideSharedAttachments(activity: ComposeActivity): Attachments {
val sharedImages = mutableListOf<Uri>()
activity.intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)?.run(sharedImages::add)
activity.intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)?.run(sharedImages::addAll)
return sharedImages.map { Attachment(it) }
return Attachments(sharedImages.map { Attachment.Image(it) })
}
@Provides
@@ -49,6 +49,7 @@ import com.moez.QKSMS.interactor.SendMessage
import com.moez.QKSMS.manager.ActiveConversationManager
import com.moez.QKSMS.manager.PermissionManager
import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.model.Attachments
import com.moez.QKSMS.model.Contact
import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.model.Message
@@ -75,13 +76,12 @@ import java.util.*
import javax.inject.Inject
import javax.inject.Named
class ComposeViewModel @Inject constructor(
@Named("query") private val query: String,
@Named("threadId") private val threadId: Long,
@Named("address") private val address: String,
@Named("text") private val sharedText: String,
@Named("attachments") private val sharedAttachments: List<Attachment>,
@Named("attachments") private val sharedAttachments: Attachments,
private val context: Context,
private val activeConversationManager: ActiveConversationManager,
private val addScheduledMessage: AddScheduledMessage,
@@ -506,8 +506,8 @@ class ComposeViewModel @Inject constructor(
// A photo was selected
Observable.merge(
view.attachmentSelectedIntent.map { uri -> Attachment(uri) },
view.inputContentIntent.map { inputContent -> Attachment(inputContent = inputContent) })
view.attachmentSelectedIntent.map { uri -> Attachment.Image(uri) },
view.inputContentIntent.map { inputContent -> Attachment.Image(inputContent = inputContent) })
.withLatestFrom(attachments) { attachment, attachments -> attachments + attachment }
.doOnNext { attachments.onNext(it) }
.autoDisposable(view.scope())
@@ -531,12 +531,14 @@ class ComposeViewModel @Inject constructor(
// Contact was selected for attachment
view.contactSelectedIntent
.map(::vCard)
.map { uri -> Attachment.Contact(getVCard(uri)!!) }
.withLatestFrom(attachments) { attachment, attachments -> attachments + attachment}
.subscribeOn(Schedulers.io())
.autoDisposable(view.scope())
.subscribe({}, { error ->
.subscribe(attachments::onNext) { error ->
context.makeToast(R.string.compose_contact_error)
Timber.w(error)
})
}
// Detach a photo
view.attachmentDeletedIntent
@@ -634,7 +636,7 @@ class ComposeViewModel @Inject constructor(
// Scheduling a message
state.scheduled != 0L -> {
newState { copy(scheduled = 0) }
val uris = attachments.map { it.getUri() }.map { it.toString() }
val uris = attachments.mapNotNull { it as? Attachment.Image }.map { it.getUri() }.map { it.toString() }
val params = AddScheduledMessage.Params(state.scheduled, subId, addresses, state.sendAsGroup, body, uris)
addScheduledMessage.execute(params)
context.makeToast(R.string.compose_scheduled_toast)
@@ -697,7 +699,7 @@ class ComposeViewModel @Inject constructor(
}
private fun vCard(contactData: Uri): String? {
private fun getVCard(contactData: Uri): String? {
val lookupKey = context.contentResolver.query(contactData, null, null, null, null)?.use { cursor ->
cursor.moveToFirst()
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY))
@@ -25,7 +25,7 @@ import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkAdapter
import com.moez.QKSMS.common.base.QkViewHolder
import com.moez.QKSMS.util.GlideApp
import kotlinx.android.synthetic.main.attachment_list_item.view.*
import kotlinx.android.synthetic.main.attachment_image_list_item.view.*
import javax.inject.Inject
class ScheduledMessageAttachmentAdapter @Inject constructor() : QkAdapter<Uri>() {
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (C) 2017 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="wrap_content"
android:layout_height="wrap_content"
tools:background="?attr/bubbleColor">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="56dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:background="@drawable/rounded_rectangle_outline_4dp"
android:backgroundTint="?android:attr/divider"
android:paddingEnd="8dp">
<ImageView
android:id="@+id/vCardAvatar"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:src="@drawable/ic_person_black_24dp"
android:tint="?android:attr/textColorSecondary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:textColor="?android:attr/textColorPrimary"
app:layout_constraintBottom_toTopOf="@id/label"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toEndOf="@id/vCardAvatar"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
app:textSize="primary"
tools:text="@tools:sample/full_names" />
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="@string/compose_vcard_label"
android:textColor="?android:attr/textColorTertiary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toEndOf="@id/vCardAvatar"
app:layout_constraintTop_toBottomOf="@id/name"
app:textSize="secondary" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="@+id/detach"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="top|end"
android:layout_marginTop="10dp"
android:layout_marginEnd="2dp"
android:background="@drawable/circle"
android:backgroundTint="?attr/bubbleColor"
android:src="@drawable/ic_cancel_black_24dp"
android:tint="?android:attr/textColorSecondary" />
</FrameLayout>
@@ -255,7 +255,7 @@
app:layout_constraintBottom_toTopOf="@id/message"
app:layout_constraintEnd_toEndOf="@id/messageBackground"
app:layout_constraintStart_toStartOf="@id/messageBackground"
tools:listitem="@layout/attachment_list_item" />
tools:listitem="@layout/attachment_image_list_item" />
<com.moez.QKSMS.common.widget.QkEditText
android:id="@+id/message"