mirror of
https://github.com/SimpleMobileTools/Simple-SMS-Messenger.git
synced 2025-06-05 21:49:22 +02:00
Refactor import/export
This commit is contained in:
@ -9,6 +9,10 @@ import com.simplemobiletools.smsmessenger.R
|
|||||||
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
||||||
import com.simplemobiletools.smsmessenger.extensions.config
|
import com.simplemobiletools.smsmessenger.extensions.config
|
||||||
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter
|
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter
|
||||||
|
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.IMPORT_FAIL
|
||||||
|
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 kotlinx.android.synthetic.main.dialog_import_messages.view.import_mms_checkbox
|
import kotlinx.android.synthetic.main.dialog_import_messages.view.import_mms_checkbox
|
||||||
import kotlinx.android.synthetic.main.dialog_import_messages.view.import_sms_checkbox
|
import kotlinx.android.synthetic.main.dialog_import_messages.view.import_sms_checkbox
|
||||||
|
|
||||||
@ -36,8 +40,13 @@ class ImportMessagesDialog(
|
|||||||
config.importSms = view.import_sms_checkbox.isChecked
|
config.importSms = view.import_sms_checkbox.isChecked
|
||||||
config.importMms = view.import_mms_checkbox.isChecked
|
config.importMms = view.import_mms_checkbox.isChecked
|
||||||
ensureBackgroundThread {
|
ensureBackgroundThread {
|
||||||
MessagesImporter(activity).importMessages(path){
|
MessagesImporter(activity).importMessages(path) { result ->
|
||||||
|
when (result) {
|
||||||
|
IMPORT_FAIL -> activity.toast(R.string.importing_failed)
|
||||||
|
IMPORT_OK -> activity.toast(R.string.importing_successful)
|
||||||
|
IMPORT_PARTIAL -> activity.toast(R.string.importing_some_entries_failed)
|
||||||
|
IMPORT_NOTHING_NEW -> activity.toast(R.string.no_entries_for_importing)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
dismiss()
|
dismiss()
|
||||||
callback.invoke(true)
|
callback.invoke(true)
|
||||||
|
@ -255,32 +255,6 @@ fun Context.getConversations(threadId: Long? = null, privateContacts: ArrayList<
|
|||||||
return conversations
|
return conversations
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.getAllMessages(): List<SmsBackup> {
|
|
||||||
val uri = Sms.CONTENT_URI
|
|
||||||
val sortOrder = "${Sms._ID}"
|
|
||||||
|
|
||||||
val messages= mutableListOf<SmsBackup>()
|
|
||||||
queryCursor(uri, null, null, null, sortOrder, showErrors = true) { cursor ->
|
|
||||||
val senderNumber = cursor.getStringValue(Sms.ADDRESS)
|
|
||||||
val id = cursor.getLongValue(Sms._ID)
|
|
||||||
val body = cursor.getStringValue(Sms.BODY)
|
|
||||||
val person = cursor.getStringValue(Sms.PERSON)
|
|
||||||
val protocol = cursor.getStringValue(Sms.PROTOCOL)
|
|
||||||
val type = cursor.getIntValue(Sms.TYPE)
|
|
||||||
val date = (cursor.getLongValue(Sms.DATE) / 1000)
|
|
||||||
val read = cursor.getIntValue(Sms.READ)
|
|
||||||
val thread = cursor.getLongValue(Sms.THREAD_ID)
|
|
||||||
val subscriptionId = cursor.getIntValue(Sms.SUBSCRIPTION_ID)
|
|
||||||
val status = cursor.getIntValue(Sms.STATUS)
|
|
||||||
|
|
||||||
messages.add(SmsBackup(id, senderNumber, body, "", date,
|
|
||||||
0, 0,0 , person, protocol, read, Any(), 0, 0, status, subscriptionId, Any(), thread, type))
|
|
||||||
}
|
|
||||||
|
|
||||||
return messages
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fun Context.getConversationIds(): List<Long> {
|
fun Context.getConversationIds(): List<Long> {
|
||||||
val uri = Uri.parse("${Threads.CONTENT_URI}?simple=true")
|
val uri = Uri.parse("${Threads.CONTENT_URI}?simple=true")
|
||||||
val projection = arrayOf(Threads._ID)
|
val projection = arrayOf(Threads._ID)
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.simplemobiletools.smsmessenger.extensions.gson
|
||||||
|
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.GsonBuilder
|
||||||
|
import com.google.gson.reflect.TypeToken
|
||||||
|
|
||||||
|
private val gsonBuilder = GsonBuilder().registerTypeAdapter(object: TypeToken<Map<String, Any>>(){}.type, MapDeserializerDoubleAsIntFix())
|
||||||
|
val gson : Gson = gsonBuilder.create()
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.simplemobiletools.smsmessenger.extensions
|
package com.simplemobiletools.smsmessenger.extensions.gson
|
||||||
|
|
||||||
import com.google.gson.*
|
import com.google.gson.*
|
||||||
import java.math.BigDecimal
|
import java.math.BigDecimal
|
||||||
@ -58,42 +58,3 @@ private fun <T> JsonElement.safeConversion(converter: () -> T?): T? {
|
|||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun JsonObject.optGet(key: String): JsonElement? = get(key)
|
|
||||||
|
|
||||||
fun JsonObject.optGetJsonArray(key: String): JsonArray? = getAsJsonArray(key)
|
|
||||||
|
|
||||||
fun JsonObject.optGetJsonObject(key: String): JsonObject? = getAsJsonObject(key)
|
|
||||||
|
|
||||||
fun JsonObject.optGetJsonPrimitive(key: String): JsonPrimitive? = getAsJsonPrimitive(key)
|
|
||||||
|
|
||||||
fun JsonObject.optString(key: String) = optGet(key)?.asString
|
|
||||||
|
|
||||||
fun JsonObject.optLong(key: String) = optGet(key)?.asLong
|
|
||||||
|
|
||||||
fun JsonObject.optBoolean(key: String) = optGet(key)?.asBoolean
|
|
||||||
|
|
||||||
fun JsonObject.optFloat(key: String) = optGet(key)?.asFloat
|
|
||||||
|
|
||||||
fun JsonObject.optDouble(key: String) = optGet(key)?.asDouble
|
|
||||||
|
|
||||||
fun JsonObject.optJsonObject(key: String) = optGet(key)?.asJsonObject
|
|
||||||
|
|
||||||
fun JsonObject.optJsonArray(key: String) = optGet(key)?.asJsonArray
|
|
||||||
|
|
||||||
fun JsonObject.optJsonPrimitive(key: String) = optGet(key)?.asJsonPrimitive
|
|
||||||
|
|
||||||
fun JsonObject.optInt(key: String) = optGet(key)?.asInt
|
|
||||||
|
|
||||||
fun JsonObject.optBigDecimal(key: String) = optGet(key)?.asBigDecimal
|
|
||||||
|
|
||||||
fun JsonObject.optBigInteger(key: String) = optGet(key)?.asBigInteger
|
|
||||||
|
|
||||||
fun JsonObject.optByte(key: String) = optGet(key)?.asByte
|
|
||||||
|
|
||||||
fun JsonObject.optShort(key: String) = optGet(key)?.asShort
|
|
||||||
|
|
||||||
fun JsonObject.optJsonNull(key: String) = optGet(key)?.asJsonNull
|
|
||||||
|
|
||||||
fun JsonObject.optCharacter(key: String) = optGet(key)?.asCharacter
|
|
||||||
|
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.simplemobiletools.smsmessenger.extensions.gson
|
||||||
|
|
||||||
|
import com.google.gson.JsonArray
|
||||||
|
import com.google.gson.JsonElement
|
||||||
|
import com.google.gson.JsonObject
|
||||||
|
import com.google.gson.JsonPrimitive
|
||||||
|
|
||||||
|
fun JsonObject.optGet(key: String): JsonElement? = get(key)
|
||||||
|
|
||||||
|
fun JsonObject.optGetJsonArray(key: String): JsonArray? = getAsJsonArray(key)
|
||||||
|
|
||||||
|
fun JsonObject.optGetJsonObject(key: String): JsonObject? = getAsJsonObject(key)
|
||||||
|
|
||||||
|
fun JsonObject.optGetJsonPrimitive(key: String): JsonPrimitive? = getAsJsonPrimitive(key)
|
||||||
|
|
||||||
|
fun JsonObject.optString(key: String) = optGet(key)?.asString
|
||||||
|
|
||||||
|
fun JsonObject.optLong(key: String) = optGet(key)?.asLong
|
||||||
|
|
||||||
|
fun JsonObject.optBoolean(key: String) = optGet(key)?.asBoolean
|
||||||
|
|
||||||
|
fun JsonObject.optFloat(key: String) = optGet(key)?.asFloat
|
||||||
|
|
||||||
|
fun JsonObject.optDouble(key: String) = optGet(key)?.asDouble
|
||||||
|
|
||||||
|
fun JsonObject.optJsonObject(key: String) = optGet(key)?.asJsonObject
|
||||||
|
|
||||||
|
fun JsonObject.optJsonArray(key: String) = optGet(key)?.asJsonArray
|
||||||
|
|
||||||
|
fun JsonObject.optJsonPrimitive(key: String) = optGet(key)?.asJsonPrimitive
|
||||||
|
|
||||||
|
fun JsonObject.optInt(key: String) = optGet(key)?.asInt
|
||||||
|
|
||||||
|
fun JsonObject.optBigDecimal(key: String) = optGet(key)?.asBigDecimal
|
||||||
|
|
||||||
|
fun JsonObject.optBigInteger(key: String) = optGet(key)?.asBigInteger
|
||||||
|
|
||||||
|
fun JsonObject.optByte(key: String) = optGet(key)?.asByte
|
||||||
|
|
||||||
|
fun JsonObject.optShort(key: String) = optGet(key)?.asShort
|
||||||
|
|
||||||
|
fun JsonObject.optJsonNull(key: String) = optGet(key)?.asJsonNull
|
||||||
|
|
||||||
|
fun JsonObject.optCharacter(key: String) = optGet(key)?.asCharacter
|
||||||
|
|
@ -0,0 +1,58 @@
|
|||||||
|
package com.simplemobiletools.smsmessenger.extensions.gson
|
||||||
|
|
||||||
|
import com.google.gson.JsonDeserializationContext
|
||||||
|
import com.google.gson.JsonDeserializer
|
||||||
|
import com.google.gson.JsonElement
|
||||||
|
import com.google.gson.JsonParseException
|
||||||
|
import com.google.gson.internal.LinkedTreeMap
|
||||||
|
import java.lang.reflect.Type
|
||||||
|
import kotlin.math.ceil
|
||||||
|
|
||||||
|
// https://stackoverflow.com/a/36529534/10552591
|
||||||
|
class MapDeserializerDoubleAsIntFix : JsonDeserializer<Map<String, Any>?> {
|
||||||
|
@Throws(JsonParseException::class)
|
||||||
|
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): Map<String, Any>? {
|
||||||
|
return read(json) as Map<String, Any>?
|
||||||
|
}
|
||||||
|
|
||||||
|
fun read(element: JsonElement): Any? {
|
||||||
|
when {
|
||||||
|
element.isJsonArray -> {
|
||||||
|
val list: MutableList<Any?> = ArrayList()
|
||||||
|
val arr = element.asJsonArray
|
||||||
|
for (anArr in arr) {
|
||||||
|
list.add(read(anArr))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
element.isJsonObject -> {
|
||||||
|
val map: MutableMap<String, Any?> = LinkedTreeMap()
|
||||||
|
val obj = element.asJsonObject
|
||||||
|
val entitySet = obj.entrySet()
|
||||||
|
for ((key, value) in entitySet) {
|
||||||
|
map[key] = read(value)
|
||||||
|
}
|
||||||
|
return map
|
||||||
|
}
|
||||||
|
element.isJsonPrimitive -> {
|
||||||
|
val prim = element.asJsonPrimitive
|
||||||
|
when {
|
||||||
|
prim.isBoolean -> {
|
||||||
|
return prim.asBoolean
|
||||||
|
}
|
||||||
|
prim.isString -> {
|
||||||
|
return prim.asString
|
||||||
|
}
|
||||||
|
prim.isNumber -> {
|
||||||
|
val num = prim.asNumber
|
||||||
|
// here you can handle double int/long values
|
||||||
|
// and return any type you want
|
||||||
|
// this solution will transform 3.0 float to long values
|
||||||
|
return if (ceil(num.toDouble()) == num.toLong().toDouble()) num.toLong() else num.toDouble()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
@ -25,7 +25,7 @@ class MessagesExporter(private val context: Context) {
|
|||||||
return@ensureBackgroundThread
|
return@ensureBackgroundThread
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* We should have json in this format
|
* We should have json in this format
|
||||||
* {
|
* {
|
||||||
* "threadId" : {
|
* "threadId" : {
|
||||||
|
@ -3,11 +3,11 @@ package com.simplemobiletools.smsmessenger.helpers
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.google.gson.Gson
|
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
import com.simplemobiletools.commons.extensions.queryCursor
|
import com.simplemobiletools.commons.extensions.queryCursor
|
||||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||||
import com.simplemobiletools.smsmessenger.extensions.*
|
import com.simplemobiletools.smsmessenger.extensions.*
|
||||||
|
import com.simplemobiletools.smsmessenger.extensions.gson.gson
|
||||||
import com.simplemobiletools.smsmessenger.models.ExportedMessage
|
import com.simplemobiletools.smsmessenger.models.ExportedMessage
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
@ -20,7 +20,6 @@ class MessagesImporter(private val context: Context) {
|
|||||||
IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL, IMPORT_NOTHING_NEW
|
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
|
||||||
|
|
||||||
@ -31,9 +30,6 @@ class MessagesImporter(private val context: Context) {
|
|||||||
return@ensureBackgroundThread
|
return@ensureBackgroundThread
|
||||||
}
|
}
|
||||||
|
|
||||||
//read data from path
|
|
||||||
// parse json
|
|
||||||
// write data to sql db
|
|
||||||
val inputStream = if (path.contains("/")) {
|
val inputStream = if (path.contains("/")) {
|
||||||
File(path).inputStream()
|
File(path).inputStream()
|
||||||
} else {
|
} else {
|
||||||
@ -55,23 +51,15 @@ class MessagesImporter(private val context: Context) {
|
|||||||
|
|
||||||
// add mms
|
// add mms
|
||||||
if (config.importMms) {
|
if (config.importMms) {
|
||||||
message.sms.forEach(messageWriter::writeMmsMessage)
|
message.mms.forEach(messageWriter::writeMmsMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// messageWriter.updateAllSmsThreads()
|
|
||||||
|
|
||||||
val conversations = context.getConversations()
|
|
||||||
val conversationIds = context.getConversationIds()
|
|
||||||
Log.w(TAG, "conversations = $conversations")
|
|
||||||
Log.w(TAG, "conversationIds = $conversationIds")
|
|
||||||
context.queryCursor(Telephony.Sms.CONTENT_URI) { cursor ->
|
context.queryCursor(Telephony.Sms.CONTENT_URI) { cursor ->
|
||||||
val json = cursor.rowsToJson()
|
val json = cursor.rowsToJson()
|
||||||
Log.w(TAG, "messages = $json")
|
Log.w(TAG, "messages = $json")
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshMessages()
|
refreshMessages()
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "importMessages: ", e)
|
Log.e(TAG, "importMessages: ", e)
|
||||||
|
@ -13,8 +13,8 @@ import com.simplemobiletools.commons.extensions.getIntValue
|
|||||||
import com.simplemobiletools.commons.extensions.getStringValue
|
import com.simplemobiletools.commons.extensions.getStringValue
|
||||||
import com.simplemobiletools.commons.extensions.queryCursor
|
import com.simplemobiletools.commons.extensions.queryCursor
|
||||||
import com.simplemobiletools.commons.helpers.isQPlus
|
import com.simplemobiletools.commons.helpers.isQPlus
|
||||||
import com.simplemobiletools.smsmessenger.extensions.optLong
|
import com.simplemobiletools.smsmessenger.extensions.gson.optLong
|
||||||
import com.simplemobiletools.smsmessenger.extensions.optString
|
import com.simplemobiletools.smsmessenger.extensions.gson.optString
|
||||||
import com.simplemobiletools.smsmessenger.extensions.rowsToJson
|
import com.simplemobiletools.smsmessenger.extensions.rowsToJson
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
@ -23,16 +23,15 @@ class MessagesReader(private val context: Context) {
|
|||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "MessagesReader"
|
private const val TAG = "MessagesReader"
|
||||||
}
|
}
|
||||||
|
|
||||||
fun forEachSms(threadId: Long, block: (JsonObject) -> Unit) {
|
fun forEachSms(threadId: Long, block: (JsonObject) -> Unit) {
|
||||||
forEachThreadMessage(Telephony.Sms.CONTENT_URI, threadId, block)
|
forEachThreadMessage(Telephony.Sms.CONTENT_URI, threadId, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun forEachMms(threadId: Long, includeAttachment: Boolean = true, block: (JsonObject) -> Unit) {
|
fun forEachMms(threadId: Long, includeNonTextAttachments: Boolean = false, block: (JsonObject) -> Unit) {
|
||||||
forEachThreadMessage(Telephony.Mms.CONTENT_URI, threadId) { obj ->
|
forEachThreadMessage(Telephony.Mms.CONTENT_URI, threadId) { obj ->
|
||||||
if (includeAttachment) {
|
obj.add(Telephony.CanonicalAddressesColumns.ADDRESS, getMMSAddresses(obj.getAsJsonPrimitive(Telephony.Mms._ID).asLong))
|
||||||
obj.add("parts", getParts(obj.getAsJsonPrimitive("_id").asLong))
|
obj.add("parts", getParts(obj.getAsJsonPrimitive(Telephony.Mms._ID).asLong, includeNonTextAttachments))
|
||||||
}
|
|
||||||
obj.add(Telephony.CanonicalAddressesColumns.ADDRESS, getMMSAddresses(obj.getAsJsonPrimitive("_id").asLong))
|
|
||||||
block(obj)
|
block(obj)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,7 +58,7 @@ class MessagesReader(private val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
private fun getParts(mmsId: Long): JsonArray {
|
private fun getParts(mmsId: Long, includeNonPlainTextParts: Boolean): JsonArray {
|
||||||
val jsonArray = JsonArray()
|
val jsonArray = JsonArray()
|
||||||
val uri = if (isQPlus()) {
|
val uri = if (isQPlus()) {
|
||||||
Telephony.Mms.Part.CONTENT_URI
|
Telephony.Mms.Part.CONTENT_URI
|
||||||
@ -67,23 +66,27 @@ class MessagesReader(private val context: Context) {
|
|||||||
Uri.parse("content://mms/part")
|
Uri.parse("content://mms/part")
|
||||||
}
|
}
|
||||||
|
|
||||||
val selection = "${Telephony.Mms.Part.MSG_ID}=$mmsId"
|
Log.d(TAG, "getParts: includeNonPlainTextParts=$includeNonPlainTextParts")
|
||||||
context.queryCursor(uri, emptyArray(), selection) { cursor ->
|
|
||||||
|
val selection = "${Telephony.Mms.Part.MSG_ID}= ?"
|
||||||
|
val selectionArgs = arrayOf(mmsId.toString())
|
||||||
|
context.queryCursor(uri, emptyArray(), selection, selectionArgs) { cursor ->
|
||||||
val part = cursor.rowsToJson()
|
val part = cursor.rowsToJson()
|
||||||
|
|
||||||
val hasTextValue = (part.has(Telephony.Mms.Part.TEXT) && !part.get(Telephony.Mms.Part.TEXT).optString.isNullOrEmpty())
|
|
||||||
|
|
||||||
when {
|
when {
|
||||||
hasTextValue -> {
|
(part.has(Telephony.Mms.Part.TEXT) && !part.get(Telephony.Mms.Part.TEXT).optString.isNullOrEmpty()) -> {
|
||||||
part.addProperty(MMS_CONTENT, "")
|
Log.d(TAG, "getParts: Add plain text part: $includeNonPlainTextParts")
|
||||||
|
part.addProperty(MMS_CONTENT, part.get(Telephony.Mms.Part.TEXT).optString)
|
||||||
}
|
}
|
||||||
|
|
||||||
part.get(Telephony.Mms.Part.CONTENT_TYPE).optString?.startsWith("text/") == true -> {
|
includeNonPlainTextParts && part.get(Telephony.Mms.Part.CONTENT_TYPE).optString?.startsWith("text/") == true -> {
|
||||||
|
Log.d(TAG, "getParts: Adding text mime= ${part.get(Telephony.Mms.Part.CONTENT_TYPE)}")
|
||||||
part.addProperty(MMS_CONTENT, usePart(part.get(Telephony.Mms.Part._ID).asLong) { stream ->
|
part.addProperty(MMS_CONTENT, usePart(part.get(Telephony.Mms.Part._ID).asLong) { stream ->
|
||||||
stream.readBytes().toString(Charsets.UTF_8)
|
stream.readBytes().toString(Charsets.UTF_8)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
else -> {
|
|
||||||
|
includeNonPlainTextParts -> {
|
||||||
|
Log.d(TAG, "getParts: Adding: other mime= ${part.get(Telephony.Mms.Part.CONTENT_TYPE)}")
|
||||||
part.addProperty(MMS_CONTENT, usePart(part.get(Telephony.Mms.Part._ID).asLong) { stream ->
|
part.addProperty(MMS_CONTENT, usePart(part.get(Telephony.Mms.Part._ID).asLong) { stream ->
|
||||||
Base64.encodeToString(stream.readBytes(), Base64.DEFAULT)
|
Base64.encodeToString(stream.readBytes(), Base64.DEFAULT)
|
||||||
})
|
})
|
||||||
@ -122,18 +125,17 @@ class MessagesReader(private val context: Context) {
|
|||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
private fun getMMSAddresses(messageId: Long): JsonArray {
|
private fun getMMSAddresses(messageId: Long): JsonArray {
|
||||||
val jsonArray = JsonArray()
|
val jsonArray = JsonArray()
|
||||||
val addressUri = if (isQPlus()) {
|
val addressUri = Uri.parse("content://mms/$messageId/addr")
|
||||||
Telephony.Mms.Addr.getAddrUriForMessage(messageId.toString())
|
|
||||||
} else {
|
|
||||||
Uri.parse("content://mms/$messageId/addr")
|
|
||||||
}
|
|
||||||
|
|
||||||
val projection = arrayOf(Telephony.Mms.Addr.ADDRESS, Telephony.Mms.Addr.TYPE)
|
val projection = arrayOf(Telephony.Mms.Addr.ADDRESS, Telephony.Mms.Addr.TYPE)
|
||||||
val selection = "${Telephony.Mms.Addr.MSG_ID}=$messageId"
|
val selection = "${Telephony.Mms.Addr.MSG_ID}=$messageId"
|
||||||
|
|
||||||
context.queryCursor(addressUri, projection, selection) { cursor ->
|
context.queryCursor(addressUri, projection, selection) { cursor ->
|
||||||
when (cursor.getIntValue(Telephony.Mms.Addr.TYPE)) {
|
when (val type = cursor.getIntValue(Telephony.Mms.Addr.TYPE)) {
|
||||||
PduHeaders.FROM, PduHeaders.TO, PduHeaders.CC, PduHeaders.BCC -> jsonArray.add(cursor.getStringValue(Telephony.Mms.Addr.ADDRESS))
|
PduHeaders.FROM, PduHeaders.TO, PduHeaders.CC, PduHeaders.BCC -> {
|
||||||
|
val obj = JsonObject()
|
||||||
|
obj.addProperty(Telephony.Mms.Addr.ADDRESS, cursor.getStringValue(Telephony.Mms.Addr.ADDRESS))
|
||||||
|
obj.addProperty(Telephony.Mms.Addr.TYPE, type)
|
||||||
|
jsonArray.add(obj)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,15 +15,15 @@ class MessagesWriter(private val context: Context) {
|
|||||||
|
|
||||||
private val contentResolver = context.contentResolver
|
private val contentResolver = context.contentResolver
|
||||||
|
|
||||||
fun writeSmsMessage(map: Map<String, String>) {
|
fun writeSmsMessage(smsMap: Map<String, Any>) {
|
||||||
Log.w(TAG, "writeSmsMessage: map=$map")
|
Log.w(TAG, "writeSmsMessage: map=$smsMap")
|
||||||
val contentValues = map.toContentValues()
|
val contentValues = smsMap.toContentValues()
|
||||||
contentValues.remove(Telephony.Sms._ID)
|
contentValues.remove(Telephony.Sms._ID)
|
||||||
replaceThreadId(contentValues)
|
replaceSmsThreadId(contentValues)
|
||||||
Log.d(TAG, "writeSmsMessage: contentValues=$contentValues")
|
Log.d(TAG, "writeSmsMessage: contentValues=$contentValues")
|
||||||
val type = contentValues.getAsInteger(Telephony.Sms.TYPE)
|
val type = contentValues.getAsInteger(Telephony.Sms.TYPE)
|
||||||
Log.d(TAG, "writeSmsMessage: type=$type")
|
Log.d(TAG, "writeSmsMessage: type=$type")
|
||||||
if ((type == Telephony.Sms.MESSAGE_TYPE_INBOX || type == Telephony.Sms.MESSAGE_TYPE_SENT) && !smsExist(map)) {
|
if ((type == Telephony.Sms.MESSAGE_TYPE_INBOX || type == Telephony.Sms.MESSAGE_TYPE_SENT) && !smsExist(smsMap)) {
|
||||||
Log.d(TAG, "writeSmsMessage: Inserting SMS...")
|
Log.d(TAG, "writeSmsMessage: Inserting SMS...")
|
||||||
val uri = Telephony.Sms.CONTENT_URI
|
val uri = Telephony.Sms.CONTENT_URI
|
||||||
Log.d(TAG, "writeSmsMessage: uri=$uri")
|
Log.d(TAG, "writeSmsMessage: uri=$uri")
|
||||||
@ -31,21 +31,17 @@ class MessagesWriter(private val context: Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun replaceThreadId(contentValues: ContentValues) {
|
private fun replaceSmsThreadId(contentValues: ContentValues) {
|
||||||
val address = contentValues.get(Telephony.Sms.ADDRESS)
|
val address = contentValues.get(Telephony.Sms.ADDRESS)
|
||||||
val threadId = context.getThreadId(address.toString())
|
val threadId = context.getThreadId(address.toString())
|
||||||
contentValues.put(Telephony.Sms.THREAD_ID, threadId)
|
contentValues.put(Telephony.Sms.THREAD_ID, threadId)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun writeMmsMessage(map: Map<String, Any>) {
|
private fun smsExist(smsMap: Map<String, Any>): Boolean {
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun smsExist(sms: Map<String, String>): Boolean {
|
|
||||||
val uri = Telephony.Sms.CONTENT_URI
|
val uri = Telephony.Sms.CONTENT_URI
|
||||||
val projection = arrayOf(Telephony.Sms._ID)
|
val projection = arrayOf(Telephony.Sms._ID)
|
||||||
val selection = "${Telephony.Sms.DATE} = ? AND ${Telephony.Sms.ADDRESS} = ? AND ${Telephony.Sms.TYPE} = ?"
|
val selection = "${Telephony.Sms.DATE} = ? AND ${Telephony.Sms.ADDRESS} = ? AND ${Telephony.Sms.TYPE} = ?"
|
||||||
val selectionArgs = arrayOf(sms[Telephony.Sms.DATE] as String, sms[Telephony.Sms.ADDRESS] as String, sms[Telephony.Sms.TYPE] as String)
|
val selectionArgs = arrayOf(smsMap[Telephony.Sms.DATE].toString(), smsMap[Telephony.Sms.ADDRESS].toString(), smsMap[Telephony.Sms.TYPE].toString())
|
||||||
|
|
||||||
var exists = false
|
var exists = false
|
||||||
context.queryCursor(uri, projection, selection, selectionArgs) {
|
context.queryCursor(uri, projection, selection, selectionArgs) {
|
||||||
@ -57,29 +53,25 @@ class MessagesWriter(private val context: Context) {
|
|||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Map<String, String>.toSmsContentValues(): ContentValues {
|
|
||||||
val values = ContentValues()
|
|
||||||
val address = get(Telephony.Sms.ADDRESS) as String
|
|
||||||
values.put(Telephony.Sms.ADDRESS, address)
|
|
||||||
values.put(Telephony.Sms.DATE, get(Telephony.Sms.DATE))
|
|
||||||
values.put(Telephony.Sms.DATE_SENT, get(Telephony.Sms.DATE_SENT))
|
|
||||||
values.put(Telephony.Sms.BODY, get(Telephony.Sms.BODY) as String)
|
|
||||||
values.put(Telephony.Sms.TYPE, get(Telephony.Sms.TYPE))
|
|
||||||
values.put(Telephony.Sms.PROTOCOL, get(Telephony.Sms.PROTOCOL))
|
|
||||||
values.put(Telephony.Sms.SERVICE_CENTER, get(Telephony.Sms.SERVICE_CENTER))
|
|
||||||
values.put(Telephony.Sms.STATUS, get(Telephony.Sms.STATUS))
|
|
||||||
values.put(Telephony.Sms.READ, get(Telephony.Sms.READ))
|
|
||||||
values.put(Telephony.Sms.CREATOR, get(Telephony.Sms.CREATOR))
|
|
||||||
values.put(Telephony.Sms.LOCKED, get(Telephony.Sms.LOCKED))
|
|
||||||
values.put(Telephony.Sms.SUBSCRIPTION_ID, get(Telephony.Sms.SUBSCRIPTION_ID))
|
|
||||||
values.put(Telephony.Sms.THREAD_ID, context.getThreadId(address))
|
|
||||||
return values
|
|
||||||
}
|
|
||||||
|
|
||||||
fun updateAllSmsThreads(){
|
fun updateAllSmsThreads(){
|
||||||
// thread dates + states might be wrong, we need to force a full update
|
// thread dates + states might be wrong, we need to force a full update
|
||||||
// unfortunately there's no direct way to do that in the SDK, but passing a
|
// unfortunately there's no direct way to do that in the SDK, but passing a
|
||||||
// negative conversation id to delete should to the trick
|
// negative conversation id to delete should to the trick
|
||||||
contentResolver.delete(Telephony.Sms.Conversations.CONTENT_URI.buildUpon().appendPath("-1").build(), null, null)
|
contentResolver.delete(Telephony.Sms.Conversations.CONTENT_URI.buildUpon().appendPath("-1").build(), null, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun writeMmsMessage(mmsMap: Map<String, Any>) {
|
||||||
|
val contentValues = mmsMap.toContentValues()
|
||||||
|
contentValues.remove(Telephony.Mms._ID)
|
||||||
|
replaceMmsThreadId(contentValues, mmsMap)
|
||||||
|
Telephony.Mms.MESSAGE_TYPE
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun replaceMmsThreadId(contentValues: ContentValues, mmsMap: Map<String, Any>) {
|
||||||
|
val addresses = (mmsMap[Telephony.Mms.Addr.ADDRESS] as? List<*>)?.map { it.toString() }?.toHashSet()
|
||||||
|
val threadId = context.getThreadId(addresses ?: setOf())
|
||||||
|
contentValues.put(Telephony.Mms.THREAD_ID, threadId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ data class ExportedMessage(
|
|||||||
@SerializedName("threadId")
|
@SerializedName("threadId")
|
||||||
val threadId: Long,
|
val threadId: Long,
|
||||||
@SerializedName("sms")
|
@SerializedName("sms")
|
||||||
val sms: List<Map<String, String>>,
|
val sms: List<Map<String, Any>>,
|
||||||
@SerializedName("mms")
|
@SerializedName("mms")
|
||||||
val mms: List<Map<String, Any>>,
|
val mms: List<Map<String, Any>>,
|
||||||
)
|
)
|
||||||
|
@ -1,46 +0,0 @@
|
|||||||
package com.simplemobiletools.smsmessenger.models
|
|
||||||
|
|
||||||
|
|
||||||
import android.provider.Telephony
|
|
||||||
import com.google.gson.annotations.SerializedName
|
|
||||||
|
|
||||||
data class SmsBackup(
|
|
||||||
@SerializedName(Telephony.Sms._ID)
|
|
||||||
val id: Long,
|
|
||||||
@SerializedName(Telephony.Sms.ADDRESS)
|
|
||||||
val address: String,
|
|
||||||
@SerializedName(Telephony.Sms.BODY)
|
|
||||||
val body: String,
|
|
||||||
@SerializedName(Telephony.Sms.CREATOR)
|
|
||||||
val creator: String,
|
|
||||||
@SerializedName(Telephony.Sms.DATE)
|
|
||||||
val date: Long,
|
|
||||||
@SerializedName(Telephony.Sms.DATE_SENT)
|
|
||||||
val dateSent: Int,
|
|
||||||
@SerializedName(Telephony.Sms.ERROR_CODE)
|
|
||||||
val errorCode: Int,
|
|
||||||
@SerializedName(Telephony.Sms.LOCKED)
|
|
||||||
val locked: Int,
|
|
||||||
@SerializedName(Telephony.Sms.PERSON)
|
|
||||||
val person: String,
|
|
||||||
@SerializedName(Telephony.Sms.PROTOCOL)
|
|
||||||
val protocol: String,
|
|
||||||
@SerializedName("read")
|
|
||||||
val read: Int,
|
|
||||||
@SerializedName("reply_path_present")
|
|
||||||
val replyPathPresent: Any,
|
|
||||||
@SerializedName("seen")
|
|
||||||
val seen: Int,
|
|
||||||
@SerializedName("service_center")
|
|
||||||
val serviceCenter: Any,
|
|
||||||
@SerializedName("status")
|
|
||||||
val status: Int,
|
|
||||||
@SerializedName("sub_id")
|
|
||||||
val subId: Int,
|
|
||||||
@SerializedName("subject")
|
|
||||||
val subject: Any,
|
|
||||||
@SerializedName("thread_id")
|
|
||||||
val threadId: Long,
|
|
||||||
@SerializedName("type")
|
|
||||||
val type: Int
|
|
||||||
)
|
|
Reference in New Issue
Block a user