mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-18 20:08:53 +01:00
Add ability to send messages
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user