Sync MMS on sending and on failure

This commit is contained in:
Moez Bhatti
2018-01-14 18:59:53 -05:00
parent c4b672f260
commit 45f1cb1bc8
11 changed files with 229 additions and 139 deletions
@@ -53,6 +53,7 @@ interface AppComponent {
fun inject(receiver: MarkReadReceiver)
fun inject(receiver: MmsReceivedReceiver)
fun inject(receiver: MmsSentReceiver)
fun inject(receiver: MmsUpdatedReceiver)
fun inject(receiver: RemoteMessagingReceiver)
fun inject(view: AvatarView)
+24 -5
View File
@@ -18,6 +18,7 @@
*/
package common.util
import android.content.ContentUris
import android.content.Context
import android.net.Uri
import android.provider.Telephony
@@ -107,14 +108,28 @@ class SyncManager @Inject constructor(
}
fun syncMessage(uri: Uri): Flowable<Message> {
val id = uri.lastPathSegment
val isMms = uri.toString().contains("mms")
val id = ContentUris.parseId(uri)
val type = when {
uri.toString().contains("mms") -> "mms"
uri.toString().contains("sms") -> "sms"
else -> return Flowable.empty()
}
// Check if the message already exists, so we can reuse the id
val existingId = Realm.getDefaultInstance().use { realm ->
realm.refresh()
realm.where(Message::class.java)
.equalTo("type", type)
.equalTo("contentId", id)
.findFirst()
?.id
}
// The uri might be something like content://mms/inbox/id
// The box might change though, so we should just use the mms/id uri
val stableUri = when(isMms) {
true -> Uri.withAppendedPath(Telephony.Mms.CONTENT_URI, id)
false -> Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, id)
val stableUri = when (type) {
"mms" -> ContentUris.withAppendedId(Telephony.Mms.CONTENT_URI, id)
else -> ContentUris.withAppendedId(Telephony.Sms.CONTENT_URI, id)
}
val cursor = context.contentResolver.query(stableUri, null, null, null, null)
@@ -123,6 +138,10 @@ class SyncManager @Inject constructor(
// Map the cursor to a message
return cursor.asFlowable()
.map { cursorToMessage.map(Pair(it, columnsMap)) }
.map { message ->
existingId?.let { message.id = it }
message
}
.doOnNext { message -> messageRepo.getOrCreateConversation(message.threadId) }
.doOnNext { message -> message.insertOrUpdate() }
}
+1 -1
View File
@@ -139,7 +139,7 @@ open class Message : RealmObject() {
}
fun isFailedMessage(): Boolean {
val isFailedMms = isMms() && errorType >= MmsSms.ERR_TYPE_GENERIC_PERMANENT
val isFailedMms = isMms() && (errorType >= MmsSms.ERR_TYPE_GENERIC_PERMANENT || boxId == Mms.MESSAGE_BOX_FAILED)
val isFailedSms = isSms() && boxId == Sms.MESSAGE_TYPE_FAILED
return isFailedMms || isFailedSms
}
@@ -24,7 +24,7 @@ import data.model.Message
import io.reactivex.Flowable
import javax.inject.Inject
class MarkMmsSent @Inject constructor(private val syncManager: SyncManager) : Interactor<Uri, Message>() {
class SyncMessage @Inject constructor(private val syncManager: SyncManager): Interactor<Uri, Message>() {
override fun buildObservable(params: Uri): Flowable<Message> {
return Flowable.just(params)
@@ -134,7 +134,6 @@ class MessagesAdapter @Inject constructor(
bindMmsPreview(view, position)
bindStatus(view, position)
bindGrouping(view, position)
view.messageBackground.invalidateOutline()
}
private fun bindMmsPreview(view: View, position: Int) {
@@ -23,12 +23,12 @@ import android.content.Intent
import android.net.Uri
import com.klinker.android.send_message.MmsSentReceiver
import common.di.appComponent
import interactor.MarkMmsSent
import interactor.SyncMessage
import javax.inject.Inject
class MmsSentReceiver : MmsSentReceiver() {
@Inject lateinit var markMmsSent: MarkMmsSent
@Inject lateinit var syncMessage: SyncMessage
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
@@ -36,7 +36,7 @@ class MmsSentReceiver : MmsSentReceiver() {
Uri.parse(intent.getStringExtra("content_uri"))?.let { uri ->
val pendingResult = goAsync()
markMmsSent.execute(uri) { pendingResult.finish() }
syncMessage.execute(uri) { pendingResult.finish() }
}
}
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package presentation.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.Uri
import common.di.appComponent
import interactor.SyncMessage
import javax.inject.Inject
class MmsUpdatedReceiver : BroadcastReceiver() {
companion object {
const val URI = "uri"
}
@Inject lateinit var syncMessage: SyncMessage
override fun onReceive(context: Context, intent: Intent) {
appComponent.inject(this)
intent.getStringExtra(URI)?.let { uriString ->
val pendingResult = goAsync()
syncMessage.execute(Uri.parse(uriString)) { pendingResult.finish() }
}
}
}