Refactor import/export
This commit is contained in:
parent
9656207135
commit
25a3a2f47b
12 changed files with 179 additions and 187 deletions
|
|
@ -255,32 +255,6 @@ fun Context.getConversations(threadId: Long? = null, privateContacts: ArrayList<
|
|||
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> {
|
||||
val uri = Uri.parse("${Threads.CONTENT_URI}?simple=true")
|
||||
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 java.math.BigDecimal
|
||||
|
|
@ -58,42 +58,3 @@ private fun <T> JsonElement.safeConversion(converter: () -> T?): T? {
|
|||
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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue