Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.anindra.messages.ui.previews

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.unit.dp
import com.anindra.messages.ui.ContactBubble
import com.anindra.messages.ui.ContactCardBubble
import com.anindra.messages.ui.theme.MessagesTheme

@PreviewLightDark
@Composable
private fun IncomingContactCardPreview() {
MessagesTheme(mode = "system") {
Column(Modifier.width(300.dp).padding(12.dp)) {
ContactCardBubble(name = "Alex Rivera", number = "+1 555 333 4444", isMe = false)
}
}
}

@PreviewLightDark
@Composable
private fun OutgoingContactCardPreview() {
MessagesTheme(mode = "system") {
Column(Modifier.width(300.dp).padding(12.dp)) {
ContactCardBubble(name = "Priya Raman", number = "+1 555 775 010", isMe = true)
}
}
}

@PreviewLightDark
@Composable
private fun ContactBubblePreview() {
MessagesTheme(mode = "system") {
Column(Modifier.width(300.dp).padding(12.dp)) {
ContactBubble(uri = "content://preview/contact", isMe = false)
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/anindra/messages/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ class AppViewModel(app: Application) : AndroidViewModel(app) {
val handedOff = SmsSender.sendMms(
getApplication(), stored.map { it.id }, recipients, uris,
settings.simSubscriptionId, stored.first().body,
kinds.map { if (it == "audio") "audio/mp4" else null }
kinds.map { MmsSupport.outgoingMime(it) }
)
if (!handedOff) {
stored.forEach { repo.markMessageStatusSuspend(it.id, "failed") }
Expand Down
31 changes: 30 additions & 1 deletion app/src/main/java/com/anindra/messages/data/MmsSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,35 @@ object MmsSupport {
else -> "file"
}

/** The kind a contact card is stored as, for its own bubble. */
const val KIND_CONTACT = "contact"

/** What a contact card is declared as on the wire; the receiving phone
* turns a part of this type into a contact rather than into a file. */
const val VCARD_MIME = com.anindra.messages.mms.pdu.ContentTypes.VCARD

/** The MIME an outgoing attachment of this kind is sent with, or null when
* the picked file's own type is the right one.
*
* The recorder writes an MPEG-4 alias the carriers do not name, and a
* contact is a cache file whose type has to be declared as the vCard the
* receiving phone looks for. */
fun outgoingMime(kind: String): String? = when (kind) {
KIND_CONTACT -> VCARD_MIME
"audio" -> "audio/mp4"
else -> null
}

/** What the conversation list shows for an attachment just sent or received:
* the preview line the thread gets when the message carries no text. */
fun previewFor(kind: String): String = when (kind) {
"image" -> "Photo"
"video" -> "Video"
"audio" -> "Voice message"
KIND_CONTACT -> "Contact"
else -> "Attachment"
}

/** The sentence [content] writes when it could not show every attachment.
* Only the provider's parts can say whether it is still true. */
private const val NOTICE_MARKER = "additional/unsupported attachment(s) not displayed"
Expand Down Expand Up @@ -410,7 +439,7 @@ object MmsSupport {
"audio/mp4" to "m4a", "audio/m4a" to "m4a", "audio/x-m4a" to "m4a",
"audio/mp4a-latm" to "m4a", "audio/aac" to "aac", "audio/3gpp" to "3gp",
"audio/amr" to "amr", "audio/mpeg" to "mp3", "audio/ogg" to "ogg",
"audio/wav" to "wav", "audio/x-wav" to "wav"
"audio/wav" to "wav", "audio/x-wav" to "wav", "text/x-vcard" to "vcf"
)

/** The fallback a saved attachment gets when neither the resolver nor the
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/anindra/messages/data/Repository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1753,7 +1753,7 @@ class Repository(private val context: Context) {
database.endTransaction()
}
val firstKind = mediaTypes.firstOrNull() ?: "image"
touchConversation(conversationId, if (firstKind == "image") "Photo" else "Voice message", now, isMe = true)
touchConversation(conversationId, MmsSupport.previewFor(firstKind), now, isMe = true)
return out
}

Expand Down
33 changes: 27 additions & 6 deletions app/src/main/java/com/anindra/messages/ui/ChatScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,13 @@ internal fun ChatBubble(
}
}
}
} else if (msg.mediaType == MmsSupport.KIND_CONTACT && msg.mediaUri.isNotBlank()) {
ContactBubble(
uri = msg.mediaUri,
isMe = msg.isMe,
isSelected = isSelected,
onLongPress = onLongPress
)
} else if (msg.mediaType == "audio" && msg.mediaUri.isNotBlank()) {
VoiceBubble(
uri = msg.mediaUri,
Expand Down Expand Up @@ -1229,16 +1236,23 @@ fun ChatScreen(
if (uri != null && uri !in pendingMedia) pendingMedia.add(uri)
}

// A contact is sent as a vCard body, so the picker only has to hand back an
// id.
// A contact leaves as a vCard attachment, so the receiving phone shows a
// contact it can save. The picker only has to hand back a contact id.
val contactLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.PickContact()
) { uri ->
if (uri != null) {
ContactShare.read(context, uri)?.let { (name, number) ->
if (ContactShare.isSendable(number)) {
vm.send(conversationId, ContactShare.vCard(name, number), currentSimId)
}
val picked = ContactShare.read(context, uri)
val card = picked?.takeIf { ContactShare.isSendable(it.second) }
?.let { (name, number) -> ContactShare.writeCard(context, name, number) }
if (card != null) {
vm.sendMediaMessage(conversationId, card, "", MmsSupport.KIND_CONTACT)
} else {
Toast.makeText(
context,
context.getString(R.string.chat_contact_unavailable),
Toast.LENGTH_SHORT
).show()
}
}
}
Expand Down Expand Up @@ -3416,6 +3430,13 @@ fun MessageRow(
}
}
}
} else if (msg.mediaType == MmsSupport.KIND_CONTACT && msg.mediaUri.isNotBlank()) {
ContactBubble(
uri = msg.mediaUri,
isMe = msg.isMe,
isSelected = isSelected,
onLongPress = { onLongPress() }
)
} else if (msg.mediaType == "audio" && msg.mediaUri.isNotBlank()) {
VoiceBubble(
uri = msg.mediaUri,
Expand Down
112 changes: 112 additions & 0 deletions app/src/main/java/com/anindra/messages/ui/ContactBubble.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.anindra.messages.ui

import android.net.Uri
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Person
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.anindra.messages.R
import com.anindra.messages.ui.theme.incomingBubble
import com.anindra.messages.ui.theme.outgoingBubble
import com.anindra.messages.ui.theme.selectedBubble

/**
* A contact card in the conversation: the name and number the card carries, not
* the properties it is made of.
*
* The card is read back from the attachment itself rather than kept in the
* message body, so it stays right after the provider holds the part and the
* original cache file is gone.
*/
@Composable
internal fun ContactBubble(
uri: String,
isMe: Boolean,
isSelected: Boolean = false,
onLongPress: () -> Unit = {}
) {
val context = LocalContext.current
val card = remember(uri) { ContactShare.readCard(context, Uri.parse(uri)) }
ContactCardBubble(
name = card?.first,
number = card?.second,
isMe = isMe,
isSelected = isSelected,
onLongPress = onLongPress
)
}

/** The card itself, without the attachment behind it. */
@OptIn(ExperimentalFoundationApi::class)
@Composable
internal fun ContactCardBubble(
name: String?,
number: String?,
isMe: Boolean,
isSelected: Boolean = false,
onLongPress: () -> Unit = {}
) {
val cs = MaterialTheme.colorScheme
val label = stringResource(R.string.chat_media_contact)

Surface(
color = if (isSelected) cs.selectedBubble else if (isMe) cs.outgoingBubble else cs.incomingBubble,
shape = RoundedCornerShape(16.dp),
modifier = Modifier
.widthIn(max = 260.dp)
.combinedClickable(onClick = {}, onLongClick = onLongPress)
) {
Row(
Modifier.padding(horizontal = 12.dp, vertical = 10.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
Icons.Rounded.Person,
contentDescription = label,
tint = cs.onPrimaryContainer,
modifier = Modifier
.size(36.dp)
.background(cs.primaryContainer, CircleShape)
.padding(6.dp)
)
Spacer(Modifier.width(10.dp))
Column {
Text(
text = name ?: label,
style = MaterialTheme.typography.bodyLarge,
fontWeight = FontWeight.SemiBold,
color = if (isMe) cs.onPrimaryContainer else cs.onSurface
)
if (number != null) {
Text(
text = number,
style = MaterialTheme.typography.bodyMedium,
color = if (isMe) cs.onPrimaryContainer else cs.onSurfaceVariant
)
}
}
}
}
}
51 changes: 0 additions & 51 deletions app/src/main/java/com/anindra/messages/ui/ContactCard.kt

This file was deleted.

Loading
Loading