Add ability to send messages

This commit is contained in:
Moez Bhatti
2017-09-20 00:19:55 -04:00
parent f639a652bb
commit 9a1ae958e0
8 changed files with 84 additions and 9 deletions
@@ -1,9 +1,11 @@
package com.moez.QKSMS.data.repository
import android.content.ContentResolver
import android.content.ContentValues
import android.content.Context
import android.net.Uri
import android.provider.Telephony
import android.telephony.SmsManager
import com.moez.QKSMS.data.model.Message
import com.moez.QKSMS.data.sync.MessageColumns
import io.realm.Realm
@@ -19,16 +21,36 @@ class MessageRepository(val context: Context) {
}
fun insertMessage(address: String, body: String, time: Long) {
val contentResolver = context.contentResolver
val cv = ContentValues()
cv.put("address", address)
cv.put("body", body)
cv.put("date_sent", time)
val contentResolver = context.contentResolver
val uri = contentResolver.insert(Uri.parse("content://sms/inbox"), cv)
copyLatestMessageToRealm(uri, contentResolver)
}
// TODO this is really sloppy, it should be fixed
fun sendMessage(threadId: Long, address: String, body: String) {
val smsManager = SmsManager.getDefault()
smsManager.sendTextMessage(address, null, body, null, null)
val values = ContentValues()
values.put("address", address)
values.put("body", body)
values.put("date", System.currentTimeMillis())
values.put("read", 1)
values.put("type", 4)
values.put("thread_id", threadId)
val contentResolver = context.contentResolver
val uri = contentResolver.insert(Uri.parse("content://sms/"), values)
copyLatestMessageToRealm(uri, contentResolver)
}
// TODO this is really sloppy, it should be fixed
private fun copyLatestMessageToRealm(uri: Uri, contentResolver: ContentResolver) {
val projection = arrayOf(Telephony.Sms.Conversations.THREAD_ID)
val cursor = contentResolver.query(uri, projection, null, null, null)