MessagesImporter and MessagesReader refactoring

This commit is contained in:
merkost 2023-07-18 16:23:46 +10:00
parent 4d378e819c
commit 679236e3fa
2 changed files with 114 additions and 156 deletions

View File

@ -1,102 +1,46 @@
package com.simplemobiletools.smsmessenger.helpers package com.simplemobiletools.smsmessenger.helpers
import android.content.Context import android.content.Context
import android.util.JsonToken
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.smsmessenger.extensions.config import com.simplemobiletools.smsmessenger.extensions.config
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.IMPORT_FAIL import com.simplemobiletools.smsmessenger.models.*
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.IMPORT_NOTHING_NEW
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.IMPORT_OK
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.IMPORT_PARTIAL
import com.simplemobiletools.smsmessenger.models.MmsBackup
import com.simplemobiletools.smsmessenger.models.SmsBackup
import java.io.File
class MessagesImporter(private val context: Context) { class MessagesImporter(private val context: Context) {
enum class ImportResult {
IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL, IMPORT_NOTHING_NEW
}
private val gson = Gson()
private val messageWriter = MessagesWriter(context) private val messageWriter = MessagesWriter(context)
private val config = context.config private val config = context.config
private var messagesImported = 0 private var messagesImported = 0
private var messagesFailed = 0 private var messagesFailed = 0
fun importMessages(path: String, onProgress: (total: Int, current: Int) -> Unit = { _, _ -> }, callback: (result: ImportResult) -> Unit) { fun importMessages(messagesBackup: List<MessagesBackup>, callback: (result: ImportResult) -> Unit) {
ensureBackgroundThread { ensureBackgroundThread {
try { try {
val inputStream = if (path.contains("/")) { messagesBackup.forEach { message ->
File(path).inputStream()
} else {
context.assets.open(path)
}
inputStream.bufferedReader().use { reader ->
val jsonReader = gson.newJsonReader(reader)
val smsMessageType = object : TypeToken<SmsBackup>() {}.type
val mmsMessageType = object : TypeToken<MmsBackup>() {}.type
jsonReader.beginArray()
while (jsonReader.hasNext()) {
jsonReader.beginObject()
while (jsonReader.hasNext()) {
val nextToken = jsonReader.peek()
if (nextToken.ordinal == JsonToken.NAME.ordinal) {
val msgType = jsonReader.nextName()
if ((!msgType.equals("sms") && !msgType.equals("mms")) ||
(msgType.equals("sms") && !config.importSms) ||
(msgType.equals("mms") && !config.importMms)
) {
jsonReader.skipValue()
continue
}
jsonReader.beginArray()
while (jsonReader.hasNext()) {
try { try {
if (msgType.equals("sms")) { if (message.backupType == BackupType.SMS && config.importSms) {
val message = gson.fromJson<SmsBackup>(jsonReader, smsMessageType) messageWriter.writeSmsMessage(message as SmsBackup)
messageWriter.writeSmsMessage(message) } else if (message.backupType == BackupType.MMS && config.importMms) {
} else { messageWriter.writeMmsMessage(message as MmsBackup)
val message = gson.fromJson<MmsBackup>(jsonReader, mmsMessageType)
messageWriter.writeMmsMessage(message)
} }
else return@forEach
messagesImported++ messagesImported++
} catch (e: Exception) { } catch (e: Exception) {
context.showErrorToast(e) context.showErrorToast(e)
messagesFailed++ messagesFailed++
} }
} }
jsonReader.endArray()
} else {
jsonReader.skipValue()
}
}
jsonReader.endObject()
refreshMessages() refreshMessages()
}
jsonReader.endArray()
}
} catch (e: Exception) { } catch (e: Exception) {
context.showErrorToast(e) context.showErrorToast(e)
messagesFailed++
} }
callback.invoke( callback.invoke(
when { when {
messagesImported == 0 && messagesFailed == 0 -> IMPORT_NOTHING_NEW messagesImported == 0 && messagesFailed == 0 -> ImportResult.IMPORT_NOTHING_NEW
messagesFailed > 0 && messagesImported > 0 -> IMPORT_PARTIAL messagesFailed > 0 && messagesImported > 0 -> ImportResult.IMPORT_PARTIAL
messagesFailed > 0 -> IMPORT_FAIL messagesFailed > 0 -> ImportResult.IMPORT_FAIL
else -> IMPORT_OK else -> ImportResult.IMPORT_OK
} }
) )
} }

View File

@ -9,32 +9,41 @@ import android.util.Base64
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.isQPlus import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.commons.helpers.isRPlus import com.simplemobiletools.commons.helpers.isRPlus
import com.simplemobiletools.smsmessenger.models.MmsAddress import com.simplemobiletools.smsmessenger.extensions.getConversationIds
import com.simplemobiletools.smsmessenger.models.MmsBackup import com.simplemobiletools.smsmessenger.models.*
import com.simplemobiletools.smsmessenger.models.MmsPart
import com.simplemobiletools.smsmessenger.models.SmsBackup
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
class MessagesReader(private val context: Context) { class MessagesReader(private val context: Context) {
fun forEachSms(threadId: Long, block: (SmsBackup) -> Unit) {
fun getMessagesToExport(
getSms: Boolean,
getMms: Boolean,
callback: (messages: List<MessagesBackup>) -> Unit
) {
val conversationIds = context.getConversationIds()
var smsMessages = listOf<SmsBackup>()
var mmsMessages = listOf<MmsBackup>()
if (getSms) {
smsMessages = getSmsMessages(conversationIds)
}
if (getMms) {
mmsMessages = getMmsMessages(conversationIds)
}
callback(smsMessages + mmsMessages)
}
private fun getSmsMessages(threadIds: List<Long>): List<SmsBackup> {
val projection = arrayOf( val projection = arrayOf(
Sms.SUBSCRIPTION_ID, Sms.SUBSCRIPTION_ID, Sms.ADDRESS, Sms.BODY, Sms.DATE, Sms.DATE_SENT, Sms.LOCKED, Sms.PROTOCOL, Sms.READ, Sms.STATUS, Sms.TYPE, Sms.SERVICE_CENTER
Sms.ADDRESS,
Sms.BODY,
Sms.DATE,
Sms.DATE_SENT,
Sms.LOCKED,
Sms.PROTOCOL,
Sms.READ,
Sms.STATUS,
Sms.TYPE,
Sms.SERVICE_CENTER
) )
val selection = "${Sms.THREAD_ID} = ?" val selection = "${Sms.THREAD_ID} = ?"
val selectionArgs = arrayOf(threadId.toString()) val smsList = mutableListOf<SmsBackup>()
context.queryCursor(Sms.CONTENT_URI, projection, selection, selectionArgs) { cursor ->
threadIds.map { it.toString() }.forEach { threadId ->
context.queryCursor(Sms.CONTENT_URI, projection, selection, arrayOf(threadId)) { cursor ->
val subscriptionId = cursor.getLongValue(Sms.SUBSCRIPTION_ID) val subscriptionId = cursor.getLongValue(Sms.SUBSCRIPTION_ID)
val address = cursor.getStringValue(Sms.ADDRESS) val address = cursor.getStringValue(Sms.ADDRESS)
val body = cursor.getStringValueOrNull(Sms.BODY) val body = cursor.getStringValueOrNull(Sms.BODY)
@ -46,12 +55,14 @@ class MessagesReader(private val context: Context) {
val status = cursor.getIntValue(Sms.STATUS) val status = cursor.getIntValue(Sms.STATUS)
val type = cursor.getIntValue(Sms.TYPE) val type = cursor.getIntValue(Sms.TYPE)
val serviceCenter = cursor.getStringValueOrNull(Sms.SERVICE_CENTER) val serviceCenter = cursor.getStringValueOrNull(Sms.SERVICE_CENTER)
block(SmsBackup(subscriptionId, address, body, date, dateSent, locked, protocol, read, status, type, serviceCenter)) smsList.add(SmsBackup(subscriptionId, address, body, date, dateSent, locked, protocol, read, status, type, serviceCenter))
} }
} }
// all mms from simple sms are non-text messages return smsList
fun forEachMms(threadId: Long, includeTextOnlyAttachment: Boolean = false, block: (MmsBackup) -> Unit) { }
private fun getMmsMessages(threadIds: List<Long>, includeTextOnlyAttachment: Boolean = false): List<MmsBackup> {
val projection = arrayOf( val projection = arrayOf(
Mms._ID, Mms._ID,
Mms.CREATOR, Mms.CREATOR,
@ -71,19 +82,19 @@ class MessagesReader(private val context: Context) {
Mms.SUBSCRIPTION_ID, Mms.SUBSCRIPTION_ID,
Mms.TRANSACTION_ID Mms.TRANSACTION_ID
) )
val selection = if (includeTextOnlyAttachment) { val selection = if (includeTextOnlyAttachment) {
"${Mms.THREAD_ID} = ? AND ${Mms.TEXT_ONLY} = ?" "${Mms.THREAD_ID} = ? AND ${Mms.TEXT_ONLY} = ?"
} else { } else {
"${Mms.THREAD_ID} = ?" "${Mms.THREAD_ID} = ?"
} }
val mmsList = mutableListOf<MmsBackup>()
threadIds.map { it.toString() }.forEach { threadId ->
val selectionArgs = if (includeTextOnlyAttachment) { val selectionArgs = if (includeTextOnlyAttachment) {
arrayOf(threadId.toString(), "1") arrayOf(threadId, "1")
} else { } else {
arrayOf(threadId.toString()) arrayOf(threadId)
} }
context.queryCursor(Mms.CONTENT_URI, projection, selection, selectionArgs) { cursor -> context.queryCursor(Mms.CONTENT_URI, projection, selection, selectionArgs) { cursor ->
val mmsId = cursor.getLongValue(Mms._ID) val mmsId = cursor.getLongValue(Mms._ID)
val creator = cursor.getStringValueOrNull(Mms.CREATOR) val creator = cursor.getStringValueOrNull(Mms.CREATOR)
@ -106,7 +117,7 @@ class MessagesReader(private val context: Context) {
val parts = getParts(mmsId) val parts = getParts(mmsId)
val addresses = getMmsAddresses(mmsId) val addresses = getMmsAddresses(mmsId)
block( mmsList.add(
MmsBackup( MmsBackup(
creator, creator,
contentType, contentType,
@ -131,6 +142,8 @@ class MessagesReader(private val context: Context) {
) )
} }
} }
return mmsList
}
@SuppressLint("NewApi") @SuppressLint("NewApi")
private fun getParts(mmsId: Long): List<MmsPart> { private fun getParts(mmsId: Long): List<MmsPart> {
@ -172,6 +185,7 @@ class MessagesReader(private val context: Context) {
stream.readBytes().toString(Charsets.UTF_8) stream.readBytes().toString(Charsets.UTF_8)
} }
} }
else -> { else -> {
usePart(partId) { stream -> usePart(partId) { stream ->
Base64.encodeToString(stream.readBytes(), Base64.DEFAULT) Base64.encodeToString(stream.readBytes(), Base64.DEFAULT)