forked from upstream-mirrors/quik
sync all db data from single call, organize sync-related classes
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
package com.moez.QKSMS.data.sync
|
||||
|
||||
import android.database.Cursor
|
||||
import android.util.Log
|
||||
|
||||
@Suppress("unused")
|
||||
class ColumnsMap(val cursor: Cursor) {
|
||||
private val TAG = "ColumnsMap"
|
||||
private val DEBUG = false
|
||||
|
||||
var msgType = getColumnIndex(MessageSyncManager.COLUMN_MSG_TYPE)
|
||||
var msgId = getColumnIndex(MessageSyncManager.COLUMN_ID)
|
||||
var threadId = getColumnIndex(MessageSyncManager.COLUMN_THREAD_ID)
|
||||
var smsAddress = getColumnIndex(MessageSyncManager.COLUMN_SMS_ADDRESS)
|
||||
var smsBody = getColumnIndex(MessageSyncManager.COLUMN_SMS_BODY)
|
||||
var smsDate = getColumnIndex(MessageSyncManager.COLUMN_SMS_DATE)
|
||||
var smsDateSent = getColumnIndex(MessageSyncManager.COLUMN_SMS_DATE_SENT)
|
||||
var smsRead = getColumnIndex(MessageSyncManager.COLUMN_SMS_READ)
|
||||
var smsType = getColumnIndex(MessageSyncManager.COLUMN_SMS_TYPE)
|
||||
var smsStatus = getColumnIndex(MessageSyncManager.COLUMN_SMS_STATUS)
|
||||
var smsLocked = getColumnIndex(MessageSyncManager.COLUMN_SMS_LOCKED)
|
||||
var smsErrorCode = getColumnIndex(MessageSyncManager.COLUMN_SMS_ERROR_CODE)
|
||||
var mmsSubject = getColumnIndex(MessageSyncManager.COLUMN_MMS_SUBJECT)
|
||||
var mmsSubjectCharset = getColumnIndex(MessageSyncManager.COLUMN_MMS_SUBJECT_CHARSET)
|
||||
var mmsDate = getColumnIndex(MessageSyncManager.COLUMN_MMS_DATE)
|
||||
var mmsDateSent = getColumnIndex(MessageSyncManager.COLUMN_MMS_DATE_SENT)
|
||||
var mmsRead = getColumnIndex(MessageSyncManager.COLUMN_MMS_READ)
|
||||
var mmsMessageType = getColumnIndex(MessageSyncManager.COLUMN_MMS_MESSAGE_TYPE)
|
||||
var mmsMessageBox = getColumnIndex(MessageSyncManager.COLUMN_MMS_MESSAGE_BOX)
|
||||
var mmsDeliveryReport = getColumnIndex(MessageSyncManager.COLUMN_MMS_DELIVERY_REPORT)
|
||||
var mmsReadReport = getColumnIndex(MessageSyncManager.COLUMN_MMS_READ_REPORT)
|
||||
var mmsErrorType = getColumnIndex(MessageSyncManager.COLUMN_MMS_ERROR_TYPE)
|
||||
var mmsLocked = getColumnIndex(MessageSyncManager.COLUMN_MMS_LOCKED)
|
||||
var mmsStatus = getColumnIndex(MessageSyncManager.COLUMN_MMS_STATUS)
|
||||
var mmsTextOnly = getColumnIndex(MessageSyncManager.COLUMN_MMS_TEXT_ONLY)
|
||||
|
||||
private fun getColumnIndex(index: Int): Int {
|
||||
try {
|
||||
return cursor.getColumnIndexOrThrow(MessageSyncManager.PROJECTION[index])
|
||||
} catch (e: IllegalArgumentException) {
|
||||
if (DEBUG) Log.w(TAG, e.message)
|
||||
}
|
||||
|
||||
return index
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.moez.QKSMS.data.sync
|
||||
|
||||
import android.net.Uri
|
||||
|
||||
internal object ConversationColumns {
|
||||
|
||||
val URI: Uri = Uri.parse("content://mms-sms/conversations?simple=true")
|
||||
val PROJECTION = arrayOf(
|
||||
android.provider.Telephony.Threads._ID,
|
||||
android.provider.Telephony.Threads.DATE,
|
||||
android.provider.Telephony.Threads.MESSAGE_COUNT,
|
||||
android.provider.Telephony.Threads.RECIPIENT_IDS,
|
||||
android.provider.Telephony.Threads.SNIPPET,
|
||||
android.provider.Telephony.Threads.SNIPPET_CHARSET,
|
||||
android.provider.Telephony.Threads.READ,
|
||||
android.provider.Telephony.Threads.ERROR,
|
||||
android.provider.Telephony.Threads.HAS_ATTACHMENT)
|
||||
|
||||
val ID = 0
|
||||
val DATE = 1
|
||||
val MESSAGE_COUNT = 2
|
||||
val RECIPIENT_IDS = 3
|
||||
val SNIPPET = 4
|
||||
val SNIPPET_CS = 5
|
||||
val READ = 6
|
||||
val ERROR = 7
|
||||
val HAS_ATTACHMENT = 8
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.moez.QKSMS.data.sync
|
||||
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import com.moez.QKSMS.model.Conversation
|
||||
import io.realm.Realm
|
||||
|
||||
internal object ConversationSyncManager {
|
||||
val TAG = "SyncManager"
|
||||
|
||||
val ID = 0
|
||||
val DATE = 1
|
||||
val MESSAGE_COUNT = 2
|
||||
val RECIPIENT_IDS = 3
|
||||
val SNIPPET = 4
|
||||
val SNIPPET_CS = 5
|
||||
val READ = 6
|
||||
val ERROR = 7
|
||||
val HAS_ATTACHMENT = 8
|
||||
|
||||
val CONVERSATIONS_CONTENT_PROVIDER = Uri.parse("content://mms-sms/conversations?simple=true")
|
||||
val ALL_THREADS_PROJECTION = arrayOf(
|
||||
android.provider.Telephony.Threads._ID,
|
||||
android.provider.Telephony.Threads.DATE,
|
||||
android.provider.Telephony.Threads.MESSAGE_COUNT,
|
||||
android.provider.Telephony.Threads.RECIPIENT_IDS,
|
||||
android.provider.Telephony.Threads.SNIPPET,
|
||||
android.provider.Telephony.Threads.SNIPPET_CHARSET,
|
||||
android.provider.Telephony.Threads.READ,
|
||||
android.provider.Telephony.Threads.ERROR,
|
||||
android.provider.Telephony.Threads.HAS_ATTACHMENT)
|
||||
|
||||
fun copyToRealm(context: android.content.Context) {
|
||||
|
||||
val realm = Realm.getDefaultInstance()
|
||||
val cursor = context.contentResolver.query(ConversationSyncManager.CONVERSATIONS_CONTENT_PROVIDER, ConversationSyncManager.ALL_THREADS_PROJECTION, null, null, "date desc")
|
||||
|
||||
realm.executeTransaction {
|
||||
it.delete(Conversation::class.java)
|
||||
|
||||
while (cursor.moveToNext())
|
||||
it.insertOrUpdate(Conversation(
|
||||
cursor.getLong(ID),
|
||||
cursor.getLong(DATE),
|
||||
cursor.getInt(MESSAGE_COUNT),
|
||||
cursor.getInt(RECIPIENT_IDS),
|
||||
cursor.getString(SNIPPET) ?: "",
|
||||
cursor.getString(SNIPPET_CS) ?: "",
|
||||
cursor.getInt(READ),
|
||||
cursor.getInt(ERROR),
|
||||
cursor.getInt(HAS_ATTACHMENT)))
|
||||
}
|
||||
|
||||
Log.i(TAG, "Inserted ${cursor.count} conversations")
|
||||
cursor.close()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.moez.QKSMS.data.sync
|
||||
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import android.provider.BaseColumns
|
||||
import android.provider.Telephony
|
||||
import android.util.Log
|
||||
|
||||
@Suppress("unused")
|
||||
class MessageColumns(val cursor: Cursor) {
|
||||
private val TAG = "ColumnsMap"
|
||||
private val DEBUG = false
|
||||
|
||||
companion object {
|
||||
val URI: Uri = Uri.parse("content://mms-sms/conversations/")
|
||||
val PROJECTION = arrayOf(
|
||||
Telephony.MmsSms.TYPE_DISCRIMINATOR_COLUMN,
|
||||
BaseColumns._ID,
|
||||
Telephony.Sms.Conversations.THREAD_ID,
|
||||
|
||||
Telephony.Sms.ADDRESS,
|
||||
Telephony.Sms.BODY,
|
||||
Telephony.Sms.DATE,
|
||||
Telephony.Sms.DATE_SENT,
|
||||
Telephony.Sms.READ,
|
||||
Telephony.Sms.TYPE,
|
||||
Telephony.Sms.STATUS,
|
||||
Telephony.Sms.LOCKED,
|
||||
Telephony.Sms.ERROR_CODE,
|
||||
|
||||
Telephony.Mms.SUBJECT,
|
||||
Telephony.Mms.SUBJECT_CHARSET,
|
||||
Telephony.Mms.DATE,
|
||||
Telephony.Mms.DATE_SENT,
|
||||
Telephony.Mms.READ,
|
||||
Telephony.Mms.MESSAGE_TYPE,
|
||||
Telephony.Mms.MESSAGE_BOX,
|
||||
Telephony.Mms.DELIVERY_REPORT,
|
||||
Telephony.Mms.READ_REPORT,
|
||||
Telephony.MmsSms.PendingMessages.ERROR_TYPE,
|
||||
Telephony.Mms.LOCKED,
|
||||
Telephony.Mms.STATUS,
|
||||
Telephony.Mms.TEXT_ONLY)
|
||||
}
|
||||
|
||||
val msgType = getColumnIndex(0)
|
||||
val msgId = getColumnIndex(1)
|
||||
val threadId = getColumnIndex(2)
|
||||
|
||||
val smsAddress = getColumnIndex(3)
|
||||
val smsBody = getColumnIndex(4)
|
||||
val smsDate = getColumnIndex(5)
|
||||
val smsDateSent = getColumnIndex(6)
|
||||
val smsRead = getColumnIndex(7)
|
||||
val smsType = getColumnIndex(8)
|
||||
val smsStatus = getColumnIndex(9)
|
||||
val smsLocked = getColumnIndex(10)
|
||||
val smsErrorCode = getColumnIndex(11)
|
||||
|
||||
val mmsSubject = getColumnIndex(12)
|
||||
val mmsSubjectCharset = getColumnIndex(13)
|
||||
val mmsDate = getColumnIndex(14)
|
||||
val mmsDateSent = getColumnIndex(15)
|
||||
val mmsRead = getColumnIndex(16)
|
||||
val mmsMessageType = getColumnIndex(17)
|
||||
val mmsMessageBox = getColumnIndex(18)
|
||||
val mmsDeliveryReport = getColumnIndex(19)
|
||||
val mmsReadReport = getColumnIndex(20)
|
||||
val mmsErrorType = getColumnIndex(21)
|
||||
val mmsLocked = getColumnIndex(22)
|
||||
val mmsStatus = getColumnIndex(23)
|
||||
val mmsTextOnly = getColumnIndex(24)
|
||||
|
||||
private fun getColumnIndex(index: Int): Int {
|
||||
try {
|
||||
return cursor.getColumnIndexOrThrow(PROJECTION[index])
|
||||
} catch (e: IllegalArgumentException) {
|
||||
if (DEBUG) Log.w(TAG, e.message)
|
||||
}
|
||||
|
||||
return index
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package com.moez.QKSMS.data.sync
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.provider.BaseColumns
|
||||
import android.provider.Telephony
|
||||
import com.moez.QKSMS.model.Message
|
||||
import io.realm.Realm
|
||||
|
||||
|
||||
internal object MessageSyncManager {
|
||||
|
||||
val COLUMN_MSG_TYPE = 0
|
||||
val COLUMN_ID = 1
|
||||
val COLUMN_THREAD_ID = 2
|
||||
val COLUMN_SMS_ADDRESS = 3
|
||||
val COLUMN_SMS_BODY = 4
|
||||
val COLUMN_SMS_DATE = 5
|
||||
val COLUMN_SMS_DATE_SENT = 6
|
||||
val COLUMN_SMS_READ = 7
|
||||
val COLUMN_SMS_TYPE = 8
|
||||
val COLUMN_SMS_STATUS = 9
|
||||
val COLUMN_SMS_LOCKED = 10
|
||||
val COLUMN_SMS_ERROR_CODE = 11
|
||||
val COLUMN_MMS_SUBJECT = 12
|
||||
val COLUMN_MMS_SUBJECT_CHARSET = 13
|
||||
val COLUMN_MMS_DATE = 14
|
||||
val COLUMN_MMS_DATE_SENT = 15
|
||||
val COLUMN_MMS_READ = 16
|
||||
val COLUMN_MMS_MESSAGE_TYPE = 17
|
||||
val COLUMN_MMS_MESSAGE_BOX = 18
|
||||
val COLUMN_MMS_DELIVERY_REPORT = 19
|
||||
val COLUMN_MMS_READ_REPORT = 20
|
||||
val COLUMN_MMS_ERROR_TYPE = 21
|
||||
val COLUMN_MMS_LOCKED = 22
|
||||
val COLUMN_MMS_STATUS = 23
|
||||
val COLUMN_MMS_TEXT_ONLY = 24
|
||||
|
||||
val MMS_SMS_CONTENT_PROVIDER = Uri.parse("content://mms-sms/conversations/")
|
||||
|
||||
val PROJECTION = arrayOf(
|
||||
Telephony.MmsSms.TYPE_DISCRIMINATOR_COLUMN,
|
||||
BaseColumns._ID,
|
||||
Telephony.Sms.Conversations.THREAD_ID,
|
||||
|
||||
Telephony.Sms.ADDRESS,
|
||||
Telephony.Sms.BODY,
|
||||
Telephony.Sms.DATE,
|
||||
Telephony.Sms.DATE_SENT,
|
||||
Telephony.Sms.READ,
|
||||
Telephony.Sms.TYPE,
|
||||
Telephony.Sms.STATUS,
|
||||
Telephony.Sms.LOCKED,
|
||||
Telephony.Sms.ERROR_CODE,
|
||||
|
||||
Telephony.Mms.SUBJECT,
|
||||
Telephony.Mms.SUBJECT_CHARSET,
|
||||
Telephony.Mms.DATE,
|
||||
Telephony.Mms.DATE_SENT,
|
||||
Telephony.Mms.READ,
|
||||
Telephony.Mms.MESSAGE_TYPE,
|
||||
Telephony.Mms.MESSAGE_BOX,
|
||||
Telephony.Mms.DELIVERY_REPORT,
|
||||
Telephony.Mms.READ_REPORT,
|
||||
Telephony.MmsSms.PendingMessages.ERROR_TYPE,
|
||||
Telephony.Mms.LOCKED,
|
||||
Telephony.Mms.STATUS,
|
||||
Telephony.Mms.TEXT_ONLY)
|
||||
|
||||
fun copyToRealm(context: Context, threadId: Long) {
|
||||
val cursor = context.contentResolver.query(Uri.withAppendedPath(MMS_SMS_CONTENT_PROVIDER, threadId.toString()), PROJECTION, null, null, "date desc")
|
||||
val columnsMap = ColumnsMap(cursor)
|
||||
|
||||
Realm.getDefaultInstance().executeTransaction {
|
||||
while (cursor.moveToNext()) {
|
||||
|
||||
val type = cursor.getString(columnsMap.msgType)
|
||||
if (type != "sms" && type != "mms") continue // If we can't read the type, don't use this message
|
||||
|
||||
val isMms = type == "mms"
|
||||
val body = cursor.getString(columnsMap.smsBody) ?: ""
|
||||
val errorType = cursor.getInt(columnsMap.mmsErrorType)
|
||||
|
||||
val boxId: Int
|
||||
|
||||
if (isMms) {
|
||||
boxId = cursor.getInt(columnsMap.mmsMessageBox)
|
||||
} else {
|
||||
boxId = cursor.getInt(columnsMap.smsType)
|
||||
}
|
||||
|
||||
it.insertOrUpdate(Message(cursor.getLong(columnsMap.msgId), threadId, boxId, type, body, errorType))
|
||||
}
|
||||
}
|
||||
|
||||
cursor.close()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.moez.QKSMS.data.sync
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import com.moez.QKSMS.model.Conversation
|
||||
import com.moez.QKSMS.model.Message
|
||||
import io.realm.Realm
|
||||
|
||||
internal object SyncManager {
|
||||
private val TAG = "SyncManager"
|
||||
|
||||
fun copyToRealm(context: Context) {
|
||||
|
||||
val realm = Realm.getDefaultInstance()
|
||||
val contentResolver = context.contentResolver
|
||||
val conversationsCursor = contentResolver.query(ConversationColumns.URI, ConversationColumns.PROJECTION, null, null, "date desc")
|
||||
|
||||
realm.executeTransaction {
|
||||
it.delete(Conversation::class.java)
|
||||
it.delete(Message::class.java)
|
||||
|
||||
while (conversationsCursor.moveToNext()) {
|
||||
it.insertOrUpdate(Conversation(conversationsCursor))
|
||||
|
||||
val threadId = conversationsCursor.getLong(ConversationColumns.ID)
|
||||
val messagesUri = Uri.withAppendedPath(MessageColumns.URI, threadId.toString())
|
||||
val messagesCursor = contentResolver.query(messagesUri, MessageColumns.PROJECTION, null, null, "date desc")
|
||||
val columnsMap = MessageColumns(messagesCursor)
|
||||
|
||||
Log.i(TAG, "Inserting conversation with ${messagesCursor.count} messages")
|
||||
while (messagesCursor.moveToNext()) {
|
||||
val type = messagesCursor.getString(columnsMap.msgType)
|
||||
if (type == "sms" || type == "mms") { // If we can't read the type, don't use this message
|
||||
it.insertOrUpdate(Message(threadId, messagesCursor, columnsMap))
|
||||
}
|
||||
}
|
||||
|
||||
messagesCursor.close()
|
||||
}
|
||||
}
|
||||
|
||||
Log.i(TAG, "Inserted ${conversationsCursor.count} conversations")
|
||||
conversationsCursor.close()
|
||||
realm.close()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user