forked from upstream-mirrors/quik
Display status of outgoing messages
This commit is contained in:
@@ -14,6 +14,8 @@ import com.moez.QKSMS.data.sync.SyncManager
|
||||
import com.moez.QKSMS.receiver.MessageDeliveredReceiver
|
||||
import com.moez.QKSMS.receiver.MessageSentReceiver
|
||||
import com.moez.QKSMS.util.extensions.insertOrUpdate
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmResults
|
||||
import io.realm.Sort
|
||||
@@ -56,16 +58,32 @@ class MessageRepository(val context: Context) {
|
||||
.findAllSorted("date")
|
||||
}
|
||||
|
||||
fun insertMessage(address: String, body: String, time: Long) {
|
||||
val cv = ContentValues()
|
||||
|
||||
cv.put("address", address)
|
||||
cv.put("body", body)
|
||||
cv.put("date_sent", time)
|
||||
fun insertReceivedMessage(address: String, body: String, time: Long) {
|
||||
val values = ContentValues()
|
||||
values.put("address", address)
|
||||
values.put("body", body)
|
||||
values.put("date_sent", time)
|
||||
|
||||
val contentResolver = context.contentResolver
|
||||
val uri = contentResolver.insert(Uri.parse("content://sms/inbox"), cv)
|
||||
copyMessageToRealm(uri)
|
||||
Flowable.just(values)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.map { contentResolver.insert(Uri.parse("content://sms/inbox"), values) }
|
||||
.subscribe { uri -> copyMessageToRealm(uri) }
|
||||
}
|
||||
|
||||
fun markSent(uri: Uri) {
|
||||
val values = ContentValues()
|
||||
values.put("type", Telephony.TextBasedSmsColumns.MESSAGE_TYPE_SENT)
|
||||
values.put("read", true)
|
||||
updateMessage(values, uri)
|
||||
}
|
||||
|
||||
fun markFailed(uri: Uri, resultCode: Int) {
|
||||
val values = ContentValues()
|
||||
values.put("type", Telephony.TextBasedSmsColumns.MESSAGE_TYPE_FAILED)
|
||||
values.put("read", true)
|
||||
values.put("error_code", resultCode)
|
||||
updateMessage(values, uri)
|
||||
}
|
||||
|
||||
fun sendMessage(threadId: Long, address: String, body: String) {
|
||||
@@ -78,17 +96,29 @@ class MessageRepository(val context: Context) {
|
||||
values.put("thread_id", threadId)
|
||||
|
||||
val contentResolver = context.contentResolver
|
||||
val uri = contentResolver.insert(Uri.parse("content://sms/"), values)
|
||||
copyMessageToRealm(uri)
|
||||
Flowable.just(values)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.map { contentResolver.insert(Uri.parse("content://sms/"), values) }
|
||||
.subscribe { uri ->
|
||||
copyMessageToRealm(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)
|
||||
val sentIntent = Intent(context, MessageSentReceiver::class.java).putExtra("uri", uri.toString())
|
||||
val sentPI = PendingIntent.getBroadcast(context, uri.lastPathSegment.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("uri", uri.toString())
|
||||
val deliveredPI = PendingIntent.getBroadcast(context, uri.lastPathSegment.toInt(), deliveredIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
val smsManager = SmsManager.getDefault()
|
||||
smsManager.sendTextMessage(address, null, body, sentPI, deliveredPI)
|
||||
val smsManager = SmsManager.getDefault()
|
||||
smsManager.sendTextMessage(address, null, body, sentPI, deliveredPI)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateMessage(values: ContentValues, uri: Uri) {
|
||||
val contentResolver = context.contentResolver
|
||||
Flowable.just(values)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.map { contentResolver.update(uri, values, null, null) }
|
||||
.subscribe { copyMessageToRealm(uri) }
|
||||
}
|
||||
|
||||
private fun copyMessageToRealm(uri: Uri) {
|
||||
|
||||
Reference in New Issue
Block a user