mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 19:38:52 +01:00
Insert new messages and changes into Realm directly
Don't use ContentProvider first
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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 com.moez.QKSMS.common.util
|
||||
|
||||
import com.moez.QKSMS.data.model.Message
|
||||
import io.realm.Realm
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* Helper class for generating incrementing ids for messages
|
||||
*/
|
||||
@Singleton
|
||||
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()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a valid ID that can be used to store a new message
|
||||
*/
|
||||
fun newId(): Long {
|
||||
maxValue++
|
||||
return maxValue
|
||||
}
|
||||
|
||||
}
|
||||
@@ -41,7 +41,7 @@ class SyncManager @Inject constructor(
|
||||
|
||||
sealed class Status {
|
||||
class Idle : Status()
|
||||
class Running(progress: Int = 0) : Status()
|
||||
class Running : Status()
|
||||
}
|
||||
|
||||
private val contentResolver = context.contentResolver
|
||||
|
||||
@@ -25,12 +25,13 @@ import android.provider.BaseColumns
|
||||
import android.provider.Telephony
|
||||
import android.provider.Telephony.Sms
|
||||
import android.provider.Telephony.TextBasedSmsColumns
|
||||
import com.moez.QKSMS.common.util.Keys
|
||||
import com.moez.QKSMS.common.util.MessageUtils
|
||||
import com.moez.QKSMS.common.util.extensions.asFlowable
|
||||
import com.moez.QKSMS.common.util.extensions.asMaybe
|
||||
import com.moez.QKSMS.common.util.extensions.insertOrUpdate
|
||||
import com.moez.QKSMS.common.util.extensions.mapNotNull
|
||||
import com.moez.QKSMS.data.mapper.CursorToConversation
|
||||
import com.moez.QKSMS.data.mapper.CursorToMessage
|
||||
import com.moez.QKSMS.data.mapper.CursorToRecipient
|
||||
import com.moez.QKSMS.data.model.Conversation
|
||||
import com.moez.QKSMS.data.model.InboxItem
|
||||
@@ -44,14 +45,13 @@ import io.reactivex.schedulers.Schedulers
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmResults
|
||||
import io.realm.Sort
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class MessageRepository @Inject constructor(
|
||||
private val context: Context,
|
||||
private val cursorToMessage: CursorToMessage,
|
||||
private val messageIds: Keys,
|
||||
private val cursorToConversation: CursorToConversation,
|
||||
private val cursorToRecipient: CursorToRecipient) {
|
||||
|
||||
@@ -101,6 +101,10 @@ class MessageRepository @Inject constructor(
|
||||
.findFirst()
|
||||
}
|
||||
|
||||
fun getOrCreateConversation(address: String): Maybe<Conversation> {
|
||||
return getOrCreateConversation(listOf(address))
|
||||
}
|
||||
|
||||
fun getOrCreateConversation(addresses: List<String>): Maybe<Conversation> {
|
||||
return Maybe.just(addresses)
|
||||
.map { recipients ->
|
||||
@@ -233,41 +237,190 @@ class MessageRepository @Inject constructor(
|
||||
realm.close()
|
||||
}
|
||||
|
||||
fun markSent(uri: Uri) {
|
||||
val values = ContentValues()
|
||||
values.put(Sms.TYPE, Sms.MESSAGE_TYPE_SENT)
|
||||
updateMessageFromUri(uri, values)
|
||||
fun insertSentSms(threadId: Long, address: String, body: String): Message {
|
||||
|
||||
// Insert the message to Realm
|
||||
val message = Message().apply {
|
||||
this.threadId = threadId
|
||||
this.address = address
|
||||
this.body = body
|
||||
this.date = System.currentTimeMillis()
|
||||
|
||||
id = messageIds.newId()
|
||||
boxId = Sms.MESSAGE_TYPE_OUTBOX
|
||||
type = "sms"
|
||||
read = true
|
||||
seen = true
|
||||
}
|
||||
val realm = Realm.getDefaultInstance()
|
||||
realm.executeTransaction { realm.insert(message) }
|
||||
realm.close()
|
||||
|
||||
// Insert the message to the native content provider
|
||||
val values = ContentValues().apply {
|
||||
put(Telephony.Sms.ADDRESS, address)
|
||||
put(Telephony.Sms.BODY, body)
|
||||
put(Telephony.Sms.DATE, System.currentTimeMillis())
|
||||
put(Telephony.Sms.READ, true)
|
||||
put(Telephony.Sms.SEEN, true)
|
||||
put(Telephony.Sms.TYPE, Telephony.Sms.MESSAGE_TYPE_OUTBOX)
|
||||
put(Telephony.Sms.THREAD_ID, threadId)
|
||||
}
|
||||
context.contentResolver.insert(Telephony.Sms.CONTENT_URI, values)
|
||||
|
||||
return message
|
||||
}
|
||||
|
||||
fun markFailed(uri: Uri, resultCode: Int) {
|
||||
val values = ContentValues()
|
||||
values.put(Sms.TYPE, Sms.MESSAGE_TYPE_FAILED)
|
||||
values.put(Sms.ERROR_CODE, resultCode)
|
||||
updateMessageFromUri(uri, values)
|
||||
fun insertReceivedSms(address: String, body: String, sentTime: Long): Message {
|
||||
|
||||
// Insert the message to Realm
|
||||
val realm = Realm.getDefaultInstance()
|
||||
val message = Message().apply {
|
||||
this.address = address
|
||||
this.body = body
|
||||
this.dateSent = sentTime
|
||||
this.date = System.currentTimeMillis()
|
||||
|
||||
id = messageIds.newId()
|
||||
threadId = getOrCreateConversation(address).blockingGet().id
|
||||
boxId = Sms.MESSAGE_TYPE_INBOX
|
||||
type = "sms"
|
||||
|
||||
realm.executeTransaction { realm.insert(this) }
|
||||
}
|
||||
realm.close()
|
||||
|
||||
// Insert the message to the native content provider
|
||||
ContentValues().apply {
|
||||
put(Telephony.Sms.ADDRESS, address)
|
||||
put(Telephony.Sms.BODY, body)
|
||||
put(Telephony.Sms.DATE_SENT, sentTime)
|
||||
put(Telephony.Sms.READ, false)
|
||||
put(Telephony.Sms.SEEN, false)
|
||||
|
||||
context.contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, this)
|
||||
}
|
||||
|
||||
return message
|
||||
}
|
||||
|
||||
fun markDelivered(uri: Uri) {
|
||||
val values = ContentValues()
|
||||
values.put("status", TextBasedSmsColumns.STATUS_COMPLETE)
|
||||
values.put("date_sent", Calendar.getInstance().timeInMillis)
|
||||
values.put("read", true)
|
||||
updateMessageFromUri(uri, values)
|
||||
fun markSent(id: Long): Flowable<*> {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
return realm.where(Message::class.java).equalTo("id", id)
|
||||
.findAllAsync()
|
||||
.asFlowable()
|
||||
.filter { it.isLoaded }
|
||||
.filter { it.isValid }
|
||||
.mapNotNull { it[0] }
|
||||
.take(1)
|
||||
.doOnNext { message ->
|
||||
// Update the message in realm
|
||||
realm.executeTransaction {
|
||||
message.boxId = Sms.MESSAGE_TYPE_SENT
|
||||
}
|
||||
}
|
||||
.map { message -> message.getUri() }
|
||||
.observeOn(Schedulers.computation())
|
||||
.doOnNext { uri ->
|
||||
// Update the message in the native ContentProvider
|
||||
val values = ContentValues()
|
||||
values.put(Sms.TYPE, Sms.MESSAGE_TYPE_SENT)
|
||||
context.contentResolver.update(uri, values, null, null)
|
||||
}
|
||||
}
|
||||
|
||||
fun markDeliveryFailed(uri: Uri, resultCode: Int) {
|
||||
val values = ContentValues()
|
||||
values.put("status", TextBasedSmsColumns.STATUS_FAILED)
|
||||
values.put("date_sent", Calendar.getInstance().timeInMillis)
|
||||
values.put("read", true)
|
||||
values.put("error_code", resultCode)
|
||||
updateMessageFromUri(uri, values)
|
||||
fun markFailed(id: Long, resultCode: Int): Flowable<*> {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
return realm.where(Message::class.java).equalTo("id", id)
|
||||
.findAllAsync()
|
||||
.asFlowable()
|
||||
.filter { it.isLoaded }
|
||||
.filter { it.isValid }
|
||||
.mapNotNull { it[0] }
|
||||
.take(1)
|
||||
.doOnNext { message ->
|
||||
// Update the message in realm
|
||||
realm.executeTransaction {
|
||||
message.boxId = Sms.MESSAGE_TYPE_FAILED
|
||||
message.errorCode = resultCode
|
||||
}
|
||||
}
|
||||
.map { message -> message.getUri() }
|
||||
.observeOn(Schedulers.computation())
|
||||
.doOnNext { uri ->
|
||||
// Update the message in the native ContentProvider
|
||||
val values = ContentValues()
|
||||
values.put(Sms.TYPE, Sms.MESSAGE_TYPE_FAILED)
|
||||
values.put(Sms.ERROR_CODE, resultCode)
|
||||
context.contentResolver.update(uri, values, null, null)
|
||||
}
|
||||
}
|
||||
|
||||
fun markDelivered(id: Long): Flowable<*> {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
return realm.where(Message::class.java).equalTo("id", id)
|
||||
.findAllAsync()
|
||||
.asFlowable()
|
||||
.filter { it.isLoaded }
|
||||
.filter { it.isValid }
|
||||
.mapNotNull { it[0] }
|
||||
.take(1)
|
||||
.doOnNext { message ->
|
||||
// Update the message in realm
|
||||
realm.executeTransaction {
|
||||
message.deliveryStatus = Message.DeliveryStatus.RECEIVED
|
||||
message.dateSent = System.currentTimeMillis()
|
||||
message.read = true
|
||||
}
|
||||
}
|
||||
.map { message -> message.getUri() }
|
||||
.observeOn(Schedulers.computation())
|
||||
.doOnNext { uri ->
|
||||
// Update the message in the native ContentProvider
|
||||
val values = ContentValues()
|
||||
values.put(Sms.STATUS, TextBasedSmsColumns.STATUS_COMPLETE)
|
||||
values.put(Sms.DATE_SENT, System.currentTimeMillis())
|
||||
values.put(Sms.READ, true)
|
||||
context.contentResolver.update(uri, values, null, null)
|
||||
}
|
||||
}
|
||||
|
||||
fun markDeliveryFailed(id: Long, resultCode: Int): Flowable<*> {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
return realm.where(Message::class.java).equalTo("id", id)
|
||||
.findAllAsync()
|
||||
.asFlowable()
|
||||
.filter { it.isLoaded }
|
||||
.filter { it.isValid }
|
||||
.mapNotNull { it[0] }
|
||||
.take(1)
|
||||
.doOnNext { message ->
|
||||
// Update the message in realm
|
||||
realm.executeTransaction {
|
||||
message.deliveryStatus = Message.DeliveryStatus.FAILED
|
||||
message.dateSent = System.currentTimeMillis()
|
||||
message.read = true
|
||||
message.errorCode = resultCode
|
||||
}
|
||||
}
|
||||
.map { message -> message.getUri() }
|
||||
.observeOn(Schedulers.computation())
|
||||
.doOnNext { uri ->
|
||||
// Update the message in the native ContentProvider
|
||||
val values = ContentValues()
|
||||
values.put(Sms.STATUS, TextBasedSmsColumns.STATUS_FAILED)
|
||||
values.put(Sms.DATE_SENT, System.currentTimeMillis())
|
||||
values.put(Sms.READ, true)
|
||||
values.put(Sms.ERROR_CODE, resultCode)
|
||||
context.contentResolver.update(uri, values, null, null)
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteMessage(messageId: Long) {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
realm.where(Message::class.java).equalTo("id", messageId).findFirst()?.let { message ->
|
||||
context.contentResolver.delete(message.getUri(), null, null)
|
||||
realm.executeTransaction { message.deleteFromRealm() }
|
||||
context.contentResolver.delete(message.getUri(), null, null)
|
||||
}
|
||||
realm.close()
|
||||
}
|
||||
@@ -316,24 +469,4 @@ class MessageRepository @Inject constructor(
|
||||
return conversation
|
||||
}
|
||||
|
||||
fun updateMessageFromUri(uri: Uri, values: ContentValues) {
|
||||
val contentResolver = context.contentResolver
|
||||
Flowable.just(values)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.map { contentResolver.update(uri, values, null, null) }
|
||||
.flatMap { addMessageFromUri(uri) }
|
||||
.subscribe()
|
||||
}
|
||||
|
||||
fun addMessageFromUri(uri: Uri): Flowable<Message> {
|
||||
val cursor = context.contentResolver.query(uri, null, null, null, "date DESC")
|
||||
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
||||
|
||||
// Map the cursor to a message
|
||||
return cursor.asFlowable()
|
||||
.map { cursorToMessage.map(Pair(it, columnsMap)) }
|
||||
.doOnNext { message -> getConversation(message.threadId) ?: getConversationFromCp(message.threadId) }
|
||||
.doOnNext { message -> message.insertOrUpdate() }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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 com.moez.QKSMS.domain.interactor
|
||||
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import javax.inject.Inject
|
||||
|
||||
class MarkDelivered @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Long, Any>() {
|
||||
|
||||
override fun buildObservable(params: Long): Flowable<Any> {
|
||||
return Flowable.just(Unit)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.flatMap { messageRepo.markDelivered(params) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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 com.moez.QKSMS.domain.interactor
|
||||
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import javax.inject.Inject
|
||||
|
||||
class MarkDeliveryFailed @Inject constructor(
|
||||
private val messageRepo: MessageRepository)
|
||||
: Interactor<MarkDeliveryFailed.Params, Any>() {
|
||||
|
||||
data class Params(val id: Long, val resultCode: Int)
|
||||
|
||||
override fun buildObservable(params: Params): Flowable<Any> {
|
||||
return Flowable.just(Unit)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.flatMap { messageRepo.markDeliveryFailed(params.id, params.resultCode) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,20 +18,19 @@
|
||||
*/
|
||||
package com.moez.QKSMS.domain.interactor
|
||||
|
||||
import android.net.Uri
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import javax.inject.Inject
|
||||
|
||||
class MarkFailed @Inject constructor(
|
||||
private val messageRepo: MessageRepository)
|
||||
: Interactor<MarkFailed.Params, Unit>() {
|
||||
class MarkFailed @Inject constructor(private val messageRepo: MessageRepository) : Interactor<MarkFailed.Params, Any>() {
|
||||
|
||||
data class Params(val uri: Uri, val resultCode: Int)
|
||||
data class Params(val id: Long, val resultCode: Int)
|
||||
|
||||
override fun buildObservable(params: Params): Flowable<Unit> {
|
||||
override fun buildObservable(params: Params): Flowable<Any> {
|
||||
return Flowable.just(Unit)
|
||||
.doOnNext { messageRepo.markFailed(params.uri, params.resultCode) }
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.flatMap { messageRepo.markFailed(params.id, params.resultCode) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,16 +18,17 @@
|
||||
*/
|
||||
package com.moez.QKSMS.domain.interactor
|
||||
|
||||
import android.net.Uri
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import javax.inject.Inject
|
||||
|
||||
class MarkSent @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Uri, Unit>() {
|
||||
class MarkSent @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Long, Any>() {
|
||||
|
||||
override fun buildObservable(params: Uri): Flowable<Unit> {
|
||||
override fun buildObservable(params: Long): Flowable<Any> {
|
||||
return Flowable.just(Unit)
|
||||
.doOnNext { messageRepo.markSent(params) }
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.flatMap { messageRepo.markSent(params) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
package com.moez.QKSMS.domain.interactor
|
||||
|
||||
import com.moez.QKSMS.common.util.Keys
|
||||
import com.moez.QKSMS.common.util.Permissions
|
||||
import com.moez.QKSMS.common.util.SyncManager
|
||||
import io.reactivex.Flowable
|
||||
@@ -27,13 +28,15 @@ import javax.inject.Inject
|
||||
|
||||
open class PartialSync @Inject constructor(
|
||||
private val syncManager: SyncManager,
|
||||
private val permissions: Permissions
|
||||
private val permissions: Permissions,
|
||||
private val keys: Keys
|
||||
) : Interactor<Unit, Long>() {
|
||||
|
||||
override fun buildObservable(params: Unit): Flowable<Long> {
|
||||
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") }
|
||||
|
||||
@@ -18,9 +18,6 @@
|
||||
*/
|
||||
package com.moez.QKSMS.domain.interactor
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.provider.Telephony
|
||||
import com.moez.QKSMS.common.util.NotificationManager
|
||||
import com.moez.QKSMS.common.util.extensions.mapNotNull
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
@@ -28,7 +25,6 @@ import io.reactivex.Flowable
|
||||
import javax.inject.Inject
|
||||
|
||||
class ReceiveMessage @Inject constructor(
|
||||
private val context: Context,
|
||||
private val messageRepo: MessageRepository,
|
||||
private val notificationManager: NotificationManager)
|
||||
: Interactor<ReceiveMessage.Params, Unit>() {
|
||||
@@ -36,17 +32,8 @@ class ReceiveMessage @Inject constructor(
|
||||
data class Params(val address: String, val body: String, val sentTime: Long)
|
||||
|
||||
override fun buildObservable(params: Params): Flowable<Unit> {
|
||||
val values = ContentValues()
|
||||
values.put(Telephony.Sms.ADDRESS, params.address)
|
||||
values.put(Telephony.Sms.BODY, params.body)
|
||||
values.put(Telephony.Sms.DATE_SENT, params.sentTime)
|
||||
values.put(Telephony.Sms.READ, false)
|
||||
values.put(Telephony.Sms.SEEN, false)
|
||||
|
||||
val contentResolver = context.contentResolver
|
||||
return Flowable.just(values)
|
||||
.map { contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values) } // Add the message to the db
|
||||
.flatMap { uri -> messageRepo.addMessageFromUri(uri) } // Copy message from realm to db
|
||||
return Flowable.just(params)
|
||||
.map { messageRepo.insertReceivedSms(params.address, params.body, params.sentTime) } // Add the message to the db
|
||||
.mapNotNull { message -> messageRepo.getConversation(message.threadId) } // Map message to conversation
|
||||
.doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
|
||||
.doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification
|
||||
|
||||
@@ -19,10 +19,8 @@
|
||||
package com.moez.QKSMS.domain.interactor
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.provider.Telephony
|
||||
import android.telephony.SmsManager
|
||||
import com.moez.QKSMS.common.util.Preferences
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
@@ -42,7 +40,6 @@ class SendMessage @Inject constructor(
|
||||
|
||||
override fun buildObservable(params: Params): Flowable<Unit> {
|
||||
|
||||
val contentResolver = context.contentResolver
|
||||
val smsManager = SmsManager.getDefault()
|
||||
|
||||
return Flowable.just(prefs.split.get())
|
||||
@@ -53,17 +50,15 @@ class SendMessage @Inject constructor(
|
||||
}
|
||||
}
|
||||
.flatMap { bodies -> bodies.toFlowable() }
|
||||
.map { bodies -> createContentValues(params.threadId, params.address, bodies) }
|
||||
.map { values -> Pair(values, contentResolver.insert(Telephony.Sms.CONTENT_URI, values)) }
|
||||
.doOnNext { (_, uri) -> messageRepo.addMessageFromUri(uri).blockingSubscribe() }
|
||||
.map { (values, uri) ->
|
||||
val sentIntent = Intent(context, MessageSentReceiver::class.java).putExtra("uri", uri.toString())
|
||||
val sentPI = PendingIntent.getBroadcast(context, uri.lastPathSegment.toInt(), sentIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
.map { body -> messageRepo.insertSentSms(params.threadId, params.address, body) }
|
||||
.map { message ->
|
||||
val sentIntent = Intent(context, MessageSentReceiver::class.java).putExtra("id", message.id)
|
||||
val sentPI = PendingIntent.getBroadcast(context, message.id.toInt(), sentIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
val deliveredIntent = Intent(context, MessageDeliveredReceiver::class.java).putExtra("uri", uri.toString())
|
||||
val deliveredPI = PendingIntent.getBroadcast(context, uri.lastPathSegment.toInt(), deliveredIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
val deliveredIntent = Intent(context, MessageDeliveredReceiver::class.java).putExtra("id", message.id)
|
||||
val deliveredPI = PendingIntent.getBroadcast(context, message.id.toInt(), deliveredIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
Triple(values.getAsString(Telephony.Sms.BODY), sentPI, deliveredPI)
|
||||
Triple(message.body, sentPI, deliveredPI)
|
||||
}
|
||||
.toList().toFlowable()
|
||||
.doOnNext { messages ->
|
||||
@@ -80,14 +75,4 @@ class SendMessage @Inject constructor(
|
||||
.map { Unit }
|
||||
}
|
||||
|
||||
private fun createContentValues(threadId: Long, address: String, body: String) = ContentValues().apply {
|
||||
put(Telephony.Sms.ADDRESS, address)
|
||||
put(Telephony.Sms.BODY, body)
|
||||
put(Telephony.Sms.DATE, System.currentTimeMillis())
|
||||
put(Telephony.Sms.READ, true)
|
||||
put(Telephony.Sms.SEEN, true)
|
||||
put(Telephony.Sms.TYPE, Telephony.Sms.MESSAGE_TYPE_OUTBOX)
|
||||
put(Telephony.Sms.THREAD_ID, threadId)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,25 +22,35 @@ import android.app.Activity
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import com.moez.QKSMS.common.di.appComponent
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.domain.interactor.MarkDelivered
|
||||
import com.moez.QKSMS.domain.interactor.MarkDeliveryFailed
|
||||
import javax.inject.Inject
|
||||
|
||||
class MessageDeliveredReceiver : BroadcastReceiver() {
|
||||
|
||||
@Inject lateinit var messageRepo: MessageRepository
|
||||
@Inject lateinit var markDelivered: MarkDelivered
|
||||
@Inject lateinit var markDeliveryFailed: MarkDeliveryFailed
|
||||
|
||||
init {
|
||||
appComponent.inject(this)
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val uri = Uri.parse(intent.getStringExtra("uri"))
|
||||
appComponent.inject(this)
|
||||
val id = intent.getLongExtra("id", 0L)
|
||||
|
||||
when (resultCode) {
|
||||
// TODO notify about delivery
|
||||
Activity.RESULT_OK -> messageRepo.markDelivered(uri)
|
||||
Activity.RESULT_OK -> {
|
||||
val pendingResult = goAsync()
|
||||
markDelivered.execute(id) { pendingResult.finish() }
|
||||
}
|
||||
|
||||
// TODO notify about delivery failure
|
||||
Activity.RESULT_CANCELED -> messageRepo.markDeliveryFailed(uri, resultCode)
|
||||
Activity.RESULT_CANCELED -> {
|
||||
val pendingResult = goAsync()
|
||||
markDeliveryFailed.execute(MarkDeliveryFailed.Params(id, resultCode)) { pendingResult.finish() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@ import android.app.Activity
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.telephony.SmsManager
|
||||
import com.moez.QKSMS.common.di.appComponent
|
||||
import com.moez.QKSMS.domain.interactor.MarkFailed
|
||||
@@ -37,15 +36,21 @@ class MessageSentReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
appComponent.inject(this)
|
||||
|
||||
val uri = Uri.parse(intent.getStringExtra("uri"))
|
||||
val id = intent.getLongExtra("id", 0L)
|
||||
|
||||
when (resultCode) {
|
||||
Activity.RESULT_OK -> markSent.execute(uri)
|
||||
Activity.RESULT_OK -> {
|
||||
val pendingResult = goAsync()
|
||||
markSent.execute(id) { pendingResult.finish() }
|
||||
}
|
||||
|
||||
SmsManager.RESULT_ERROR_GENERIC_FAILURE,
|
||||
SmsManager.RESULT_ERROR_NO_SERVICE,
|
||||
SmsManager.RESULT_ERROR_NULL_PDU,
|
||||
SmsManager.RESULT_ERROR_RADIO_OFF -> markFailed.execute(MarkFailed.Params(uri, resultCode))
|
||||
SmsManager.RESULT_ERROR_RADIO_OFF -> {
|
||||
val pendingResult = goAsync()
|
||||
markFailed.execute(MarkFailed.Params(id, resultCode)) { pendingResult.finish() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user