From 83cdf0f623dda257743c8f7120ebf362add8d363 Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 28 Feb 2023 18:58:10 +0530 Subject: [PATCH 1/3] Avoid resetting conversation name --- .../smsmessenger/activities/MainActivity.kt | 6 ++---- .../smsmessenger/extensions/Context.kt | 14 +++++++++++++- .../smsmessenger/receivers/MmsReceiver.kt | 2 +- .../smsmessenger/receivers/SmsReceiver.kt | 2 +- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt index 37764fcd..a432afb5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt @@ -307,10 +307,8 @@ class MainActivity : SimpleActivity() { } if (conv != null) { val lastModified = maxOf(cachedConv.date, conv.date) - val usesCustomTitle = cachedConv.usesCustomTitle - val title = if (usesCustomTitle) cachedConv.title else conv.title - val conversation = conv.copy(date = lastModified, title = title, usesCustomTitle = usesCustomTitle) - conversationsDB.insertOrUpdate(conversation) + val conversation = conv.copy(date = lastModified) + insertOrUpdateConversation(conversation) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt index 6ed5c9ea..163f99ed 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -824,7 +824,7 @@ fun Context.updateLastConversationMessage(threadId: Long) { try { contentResolver.delete(uri, selection, selectionArgs) val newConversation = getConversations(threadId)[0] - conversationsDB.insertOrUpdate(newConversation) + insertOrUpdateConversation(newConversation) } catch (e: Exception) { } } @@ -878,6 +878,18 @@ fun Context.subscriptionManagerCompat(): SubscriptionManager { return getSystemService(SubscriptionManager::class.java) } +fun Context.insertOrUpdateConversation(conversation: Conversation) { + val cachedConv = conversationsDB.getConversationWithThreadId(conversation.threadId) + val updatedConv = if (cachedConv != null) { + val usesCustomTitle = cachedConv.usesCustomTitle + val title = if (usesCustomTitle) cachedConv.title else conversation.title + conversation.copy(title = title, usesCustomTitle = usesCustomTitle) + } else { + conversation + } + conversationsDB.insertOrUpdate(updatedConv) +} + fun Context.renameConversation(conversation: Conversation, newTitle: String): Conversation { val updatedConv = conversation.copy(title = newTitle, usesCustomTitle = true) try { diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/MmsReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/MmsReceiver.kt index 8c2acf0b..dbf08b8f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/MmsReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/MmsReceiver.kt @@ -41,7 +41,7 @@ class MmsReceiver : com.klinker.android.send_message.MmsReceivedReceiver() { context.showReceivedMessageNotification(address, mms.body, mms.threadId, glideBitmap) val conversation = context.getConversations(mms.threadId).firstOrNull() ?: return@post ensureBackgroundThread { - context.conversationsDB.insertOrUpdate(conversation) + context.insertOrUpdateConversation(conversation) context.updateUnreadCountBadge(context.conversationsDB.getUnreadConversations()) refreshMessages() } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/SmsReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/SmsReceiver.kt index bded30f1..089945e6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/SmsReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/SmsReceiver.kt @@ -67,7 +67,7 @@ class SmsReceiver : BroadcastReceiver() { val conversation = context.getConversations(threadId).firstOrNull() ?: return@ensureBackgroundThread try { - context.conversationsDB.insertOrUpdate(conversation) + context.insertOrUpdateConversation(conversation) } catch (ignored: Exception) { } From c94b8852cde24392addef06eb332c1e91a4b5243 Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 28 Feb 2023 19:10:11 +0530 Subject: [PATCH 2/3] Avoid resetting conversation name in tmp threads --- .../smsmessenger/activities/MainActivity.kt | 1 + .../smsmessenger/activities/ThreadActivity.kt | 4 ++-- .../smsmessenger/extensions/Context.kt | 24 ++++++++++++++----- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt index a432afb5..4cb54d99 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt @@ -298,6 +298,7 @@ class MainActivity : SimpleActivity() { .forEach { message -> messagesDB.insertOrUpdate(message.copy(threadId = newConversation.threadId)) } + insertOrUpdateConversation(newConversation, cachedConversation) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt index c94cfd4e..eb0da9aa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt @@ -494,7 +494,7 @@ class ThreadActivity : SimpleActivity() { if (messages.isNotEmpty() && messages.all { it.isScheduled }) { val scheduledMessage = messages.last() val fakeThreadId = generateRandomId() - createTemporaryThread(scheduledMessage, fakeThreadId) + createTemporaryThread(scheduledMessage, fakeThreadId, conversation) updateScheduledMessagesThreadId(messages, fakeThreadId) threadId = fakeThreadId } @@ -1198,7 +1198,7 @@ class ThreadActivity : SimpleActivity() { if (messages.isEmpty()) { // create a temporary thread until a real message is sent threadId = message.threadId - createTemporaryThread(message, message.threadId) + createTemporaryThread(message, message.threadId, conversation) } val conversation = conversationsDB.getConversationWithThreadId(threadId) if (conversation != null) { diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt index 163f99ed..2aa8604b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -878,11 +878,17 @@ fun Context.subscriptionManagerCompat(): SubscriptionManager { return getSystemService(SubscriptionManager::class.java) } -fun Context.insertOrUpdateConversation(conversation: Conversation) { - val cachedConv = conversationsDB.getConversationWithThreadId(conversation.threadId) +fun Context.insertOrUpdateConversation( + conversation: Conversation, + cachedConv: Conversation? = conversationsDB.getConversationWithThreadId(conversation.threadId) +) { val updatedConv = if (cachedConv != null) { val usesCustomTitle = cachedConv.usesCustomTitle - val title = if (usesCustomTitle) cachedConv.title else conversation.title + val title = if (usesCustomTitle) { + cachedConv.title + } else { + conversation.title + } conversation.copy(title = title, usesCustomTitle = usesCustomTitle) } else { conversation @@ -900,21 +906,27 @@ fun Context.renameConversation(conversation: Conversation, newTitle: String): Co return updatedConv } -fun Context.createTemporaryThread(message: Message, threadId: Long = generateRandomId()) { +fun Context.createTemporaryThread(message: Message, threadId: Long = generateRandomId(), cachedConv: Conversation?) { val simpleContactHelper = SimpleContactsHelper(this) val addresses = message.participants.getAddresses() val photoUri = if (addresses.size == 1) simpleContactHelper.getPhotoUriFromPhoneNumber(addresses.first()) else "" + val title = if (cachedConv != null && cachedConv.usesCustomTitle) { + cachedConv.title + } else { + message.participants.getThreadTitle() + } val conversation = Conversation( threadId = threadId, snippet = message.body, date = message.date, read = true, - title = message.participants.getThreadTitle(), + title = title, photoUri = photoUri, isGroupConversation = addresses.size > 1, phoneNumber = addresses.first(), - isScheduled = true + isScheduled = true, + usesCustomTitle = cachedConv?.usesCustomTitle == true ) try { conversationsDB.insertOrUpdate(conversation) From eeb87836e290539ed4fbccffe0c8497ddb2e645d Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 28 Feb 2023 19:23:19 +0530 Subject: [PATCH 3/3] Handle temporary threads in conversations details --- .../smsmessenger/activities/ConversationDetailsActivity.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ConversationDetailsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ConversationDetailsActivity.kt index fae05c68..59f115a5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ConversationDetailsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ConversationDetailsActivity.kt @@ -34,7 +34,12 @@ class ConversationDetailsActivity : SimpleActivity() { threadId = intent.getLongExtra(THREAD_ID, 0L) ensureBackgroundThread { conversation = conversationsDB.getConversationWithThreadId(threadId) - participants = getThreadParticipants(threadId, null) + participants = if (conversation != null && conversation!!.isScheduled) { + val message = messagesDB.getThreadMessages(conversation!!.threadId).firstOrNull() + message?.participants ?: arrayListOf() + } else { + getThreadParticipants(threadId, null) + } runOnUiThread { setupTextViews() setupParticipants()