From 1b145d21b2ece9e140ac148b9053784e8fd1f9c9 Mon Sep 17 00:00:00 2001 From: Inhishonor Date: Wed, 17 Dec 2025 14:15:59 -0800 Subject: [PATCH 1/3] Fix threading issues in the contact functions --- .../java/com/moez/QKSMS/repository/ContactRepositoryImpl.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/data/src/main/java/com/moez/QKSMS/repository/ContactRepositoryImpl.kt b/data/src/main/java/com/moez/QKSMS/repository/ContactRepositoryImpl.kt index 97e86a54..4989fb21 100644 --- a/data/src/main/java/com/moez/QKSMS/repository/ContactRepositoryImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/repository/ContactRepositoryImpl.kt @@ -113,8 +113,6 @@ class ContactRepositoryImpl @Inject constructor( .filter { it.isLoaded } .filter { it.isValid } .map { realm.copyFromRealm(it) } - .subscribeOn(AndroidSchedulers.mainThread()) - .observeOn(Schedulers.io()) .map { contacts -> if (mobileOnly) { contacts.map { contact -> @@ -140,6 +138,7 @@ class ContactRepositoryImpl @Inject constructor( } } } + .subscribeOn(Schedulers.io()) } override fun getUnmanagedContactGroups(): Observable> { From 5687ec1ff7a3945f2221f79f8146cad2df31f4cf Mon Sep 17 00:00:00 2001 From: Inhishonor Date: Wed, 17 Dec 2025 15:02:10 -0800 Subject: [PATCH 2/3] Add better error handling for conversation loading --- .../com/moez/QKSMS/feature/compose/ComposeViewModel.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt index 9228a630..b61e8d63 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt @@ -178,10 +178,15 @@ class ComposeViewModel @Inject constructor( // monitors convos and triggers when wanted convo is present conversationRepo.getConversations(false) .asObservable() - .filter { conversations -> conversations.isLoaded } + .filter { conversations -> conversations.isLoaded && conversations.isValid} .mapNotNull { conversationRepo.getConversation(addresses) } .doOnNext { newState { copy(loading = false) } } - } + } + .doOnError { e -> + Timber.e(e, "Error while resolving conversation") + newState { copy(loading = false) } + } + // Merges two potential conversation sources (constructor threadId and contact selection) // into a single stream of conversations. If the conversation was deleted, notify the From d1473a60a6bc063ba8a619665384fc3806ee8189 Mon Sep 17 00:00:00 2001 From: Inhishonor Date: Tue, 30 Dec 2025 08:31:02 -0800 Subject: [PATCH 3/3] 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 + } + } + }