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.
This commit is contained in:
Inhishonor
2025-12-30 08:31:02 -08:00
parent 5687ec1ff7
commit d1473a60a6
@@ -339,7 +339,10 @@ class ConversationRepositoryImpl @Inject constructor(
override fun createConversation(addresses: Collection<String>, 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<String>, 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
}
}
}