forked from upstream-mirrors/quik
Allow for nullable cursor when syncing message
This commit is contained in:
@@ -23,7 +23,6 @@ import android.content.ContentUris
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
import android.telephony.PhoneNumberUtils
|
import android.telephony.PhoneNumberUtils
|
||||||
import io.reactivex.Flowable
|
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import mapper.CursorToContact
|
import mapper.CursorToContact
|
||||||
import mapper.CursorToConversation
|
import mapper.CursorToConversation
|
||||||
@@ -35,7 +34,6 @@ import model.Message
|
|||||||
import model.MmsPart
|
import model.MmsPart
|
||||||
import model.Recipient
|
import model.Recipient
|
||||||
import model.SyncLog
|
import model.SyncLog
|
||||||
import util.extensions.asFlowable
|
|
||||||
import util.extensions.insertOrUpdate
|
import util.extensions.insertOrUpdate
|
||||||
import util.extensions.map
|
import util.extensions.map
|
||||||
import util.extensions.mapWhile
|
import util.extensions.mapWhile
|
||||||
@@ -138,14 +136,17 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
status = Status.Idle()
|
status = Status.Idle()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun syncMessage(uri: Uri): Flowable<Message> {
|
override fun syncMessage(uri: Uri): Message? {
|
||||||
|
|
||||||
|
// If we don't have a valid type, return null
|
||||||
val type = when {
|
val type = when {
|
||||||
uri.toString().contains("mms") -> "mms"
|
uri.toString().contains("mms") -> "mms"
|
||||||
uri.toString().contains("sms") -> "sms"
|
uri.toString().contains("sms") -> "sms"
|
||||||
else -> return Flowable.empty()
|
else -> return null
|
||||||
}
|
}
|
||||||
|
|
||||||
val id = tryOrNull { ContentUris.parseId(uri) } ?: return Flowable.empty()
|
// If we don't have a valid id, return null
|
||||||
|
val id = tryOrNull { ContentUris.parseId(uri) } ?: return null
|
||||||
|
|
||||||
// Check if the message already exists, so we can reuse the id
|
// Check if the message already exists, so we can reuse the id
|
||||||
val existingId = Realm.getDefaultInstance().use { realm ->
|
val existingId = Realm.getDefaultInstance().use { realm ->
|
||||||
@@ -164,15 +165,19 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
else -> ContentUris.withAppendedId(Telephony.Sms.CONTENT_URI, id)
|
else -> ContentUris.withAppendedId(Telephony.Sms.CONTENT_URI, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
val cursor = contentResolver.query(stableUri, null, null, null, null)
|
return contentResolver.query(stableUri, null, null, null, null)?.use { cursor ->
|
||||||
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
|
||||||
|
|
||||||
// Map the cursor to a message
|
// If there are no rows, return null. Otherwise, we've moved to the first row
|
||||||
return cursor.asFlowable()
|
if (!cursor.moveToFirst()) return null
|
||||||
.map { cursorToMessage.map(Pair(it, columnsMap)) }
|
|
||||||
.doOnNext { message -> existingId?.let { message.id = it } }
|
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
||||||
.doOnNext { message -> messageRepo.getOrCreateConversation(message.threadId) }
|
cursorToMessage.map(Pair(cursor, columnsMap)).apply {
|
||||||
.doOnNext { message -> message.insertOrUpdate() }
|
existingId?.let { this.id = it }
|
||||||
|
|
||||||
|
messageRepo.getOrCreateConversation(threadId)
|
||||||
|
insertOrUpdate()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun syncContacts() {
|
override fun syncContacts() {
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class ReceiveMms @Inject constructor(
|
|||||||
|
|
||||||
override fun buildObservable(params: Uri): Flowable<*> {
|
override fun buildObservable(params: Uri): Flowable<*> {
|
||||||
return Flowable.just(params)
|
return Flowable.just(params)
|
||||||
.flatMap { uri -> syncManager.syncMessage(uri) } // Sync the message
|
.mapNotNull { uri -> syncManager.syncMessage(uri) } // Sync the message
|
||||||
.filter { message ->
|
.filter { message ->
|
||||||
// Because we use the smsmms library for receiving and storing MMS, we'll need
|
// Because we use the smsmms library for receiving and storing MMS, we'll need
|
||||||
// to check if it should be blocked after we've pulled it into realm. If it
|
// to check if it should be blocked after we've pulled it into realm. If it
|
||||||
|
|||||||
@@ -22,13 +22,14 @@ import android.net.Uri
|
|||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import model.Message
|
import model.Message
|
||||||
import repository.SyncRepository
|
import repository.SyncRepository
|
||||||
|
import util.extensions.mapNotNull
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class SyncMessage @Inject constructor(private val syncManager: SyncRepository) : Interactor<Uri>() {
|
class SyncMessage @Inject constructor(private val syncManager: SyncRepository) : Interactor<Uri>() {
|
||||||
|
|
||||||
override fun buildObservable(params: Uri): Flowable<Message> {
|
override fun buildObservable(params: Uri): Flowable<Message> {
|
||||||
return Flowable.just(params)
|
return Flowable.just(params)
|
||||||
.flatMap { uri -> syncManager.syncMessage(uri) }
|
.mapNotNull { uri -> syncManager.syncMessage(uri) }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -19,14 +19,13 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import io.reactivex.Flowable
|
|
||||||
import model.Message
|
import model.Message
|
||||||
|
|
||||||
interface SyncRepository {
|
interface SyncRepository {
|
||||||
|
|
||||||
fun syncMessages(fullSync: Boolean = false)
|
fun syncMessages(fullSync: Boolean = false)
|
||||||
|
|
||||||
fun syncMessage(uri: Uri): Flowable<Message>
|
fun syncMessage(uri: Uri): Message?
|
||||||
|
|
||||||
fun syncContacts()
|
fun syncContacts()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user