From d1473a60a6bc063ba8a619665384fc3806ee8189 Mon Sep 17 00:00:00 2001 From: Inhishonor Date: Tue, 30 Dec 2025 08:31:02 -0800 Subject: [PATCH] Provide a fallback conversation if content provider returns null In some cases, (mainly on HyperOS and variants) content provider will return null if an empty conversation is created. This was breaking the compose activity as it expects some object other than null. This commit changes the behavior to provide a new conversation object if createConversationFromCp ever returns null. --- .../repository/ConversationRepositoryImpl.kt | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt b/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt index 8a37752d..a103a7ad 100644 --- a/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt @@ -339,7 +339,10 @@ class ConversationRepositoryImpl @Inject constructor( override fun createConversation(addresses: Collection, sendAsGroup: Boolean) = TelephonyCompat.getOrCreateThreadId(context, addresses.toSet()) .takeIf { it != 0L } - ?.let { providerThreadId -> createConversationFromCp(providerThreadId, sendAsGroup) } + ?.let { providerThreadId -> + createConversationFromCp(providerThreadId, sendAsGroup) ?: + createEmptyConversation(providerThreadId, addresses, sendAsGroup) + } override fun getOrCreateConversation(threadId: Long, sendAsGroup: Boolean) = getConversation(threadId) ?: createConversation(threadId, sendAsGroup) @@ -511,6 +514,7 @@ class ConversationRepositoryImpl @Inject constructor( phoneNumberUtils.compare(it.address, address) } } + ?.let { realm.copyFromRealm(it) } } } @@ -533,4 +537,38 @@ class ConversationRepositoryImpl @Inject constructor( } } + /** + * In some cases [createConversationFromCp] will return null if there are no messages present in the convo. + * In order to allow the conversation to be accessed + * we need to create an empty conversation in Realm to match the conversation created in the content provider. + * + * This is a bit of a hack, but is necessary on devices running HyperOS or variants. + */ + private fun createEmptyConversation(threadId: Long, addresses: Collection, sendAsGroup: Boolean): Conversation { + Realm.getDefaultInstance().use { realm -> + val realmContacts = realm.where(Contact::class.java).findAll() + val matchedRecipients = addresses.map { address -> + Recipient().apply { + this.address = address + contact = realmContacts.firstOrNull { realmContact -> + realmContact.numbers.any { + phoneNumberUtils.compare(it.address, address) + } + } + ?.let { realm.copyFromRealm(it) } + } + } + val conversation = Conversation().apply { + id = threadId + recipients.clear() + recipients.addAll(matchedRecipients) + this.sendAsGroup = + if (recipients.size <= 1) false + else sendAsGroup + } + realm.executeTransaction { it.copyToRealmOrUpdate(conversation) } + return conversation + } + } + }