Ensure that MMS have unique keys

This commit is contained in:
Moez Bhatti
2018-01-05 00:09:45 -05:00
parent 20f83fccf9
commit 613e3096a2
5 changed files with 27 additions and 12 deletions
+7 -7
View File
@@ -32,18 +32,18 @@ class Keys @Inject constructor() {
private var maxValue: Long = 0L
init {
refresh()
}
/**
* Should be called upon initialization, and after a sync
*/
fun refresh() {
val realm = Realm.getDefaultInstance()
maxValue = realm.where(Message::class.java).max("id")?.toLong() ?: 0L
realm.close()
}
/**
* Should be called when a new sync is being started
*/
fun reset() {
maxValue = 0L
}
/**
* Returns a valid ID that can be used to store a new message
*/
@@ -22,12 +22,16 @@ import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.Telephony.*
import common.util.Keys
import data.model.Message
import data.model.Message.DeliveryStatus
import timber.log.Timber
import javax.inject.Inject
class CursorToMessage @Inject constructor(val context: Context) : Mapper<Pair<Cursor, CursorToMessage.MessageColumns>, Message> {
class CursorToMessage @Inject constructor(
private val context: Context,
private val keys: Keys
) : Mapper<Pair<Cursor, CursorToMessage.MessageColumns>, Message> {
override fun map(from: Pair<Cursor, MessageColumns>): Message {
val cursor = from.first
@@ -39,7 +43,8 @@ class CursorToMessage @Inject constructor(val context: Context) : Mapper<Pair<Cu
else -> cursor.getString(columnsMap.msgType)
}
id = cursor.getLong(columnsMap.msgId)
id = keys.newId()
contentId = cursor.getLong(columnsMap.msgId)
when (type) {
"sms" -> {
+7 -1
View File
@@ -30,6 +30,12 @@ open class Message : RealmObject() {
enum class AttachmentType { TEXT, IMAGE, VIDEO, AUDIO, SLIDESHOW, NOT_LOADED }
@PrimaryKey var id: Long = 0
// The MMS-SMS content provider returns messages where duplicate ids can exist. This is because
// SMS and MMS are stored in separate tables. We can't use these ids as our realm message id
// since it's our primary key for the single message object, so we'll store the original id in
// case we need to access the original message item in the content provider
var contentId: Long = 0
var threadId: Long = 0
var address: String = ""
var boxId: Int = 0
@@ -70,7 +76,7 @@ open class Message : RealmObject() {
fun getUri(): Uri {
val baseUri = if (isMms()) Mms.CONTENT_URI else Sms.CONTENT_URI
return ContentUris.withAppendedId(baseUri, id)
return ContentUris.withAppendedId(baseUri, contentId)
}
fun isMms(): Boolean = type == "mms"
+6 -1
View File
@@ -18,16 +18,21 @@
*/
package interactor
import common.util.Keys
import common.util.SyncManager
import io.reactivex.Flowable
import timber.log.Timber
import java.util.concurrent.TimeUnit
import javax.inject.Inject
class FullSync @Inject constructor(private val syncManager: SyncManager) : Interactor<Unit, Long>() {
class FullSync @Inject constructor(
private val syncManager: SyncManager,
private val keys: Keys
) : Interactor<Unit, Long>() {
override fun buildObservable(params: Unit): Flowable<Long> {
return Flowable.just(System.currentTimeMillis())
.doOnNext { keys.reset() }
.doOnNext { syncManager.performSync(true) }
.map { startTime -> System.currentTimeMillis() - startTime }
.map { elapsed -> TimeUnit.MILLISECONDS.toSeconds(elapsed) }
@@ -36,7 +36,6 @@ open class PartialSync @Inject constructor(
return Flowable.just(System.currentTimeMillis())
.skipWhile { !permissions.hasSmsAndContacts() }
.doOnNext { syncManager.performSync() }
.doOnNext { keys.refresh() }
.map { startTime -> System.currentTimeMillis() - startTime }
.map { elapsed -> TimeUnit.MILLISECONDS.toSeconds(elapsed) }
.doOnNext { seconds -> Timber.v("Completed sync in $seconds seconds") }