Merge branch 'master' into fix/wrong-sender-name

This commit is contained in:
Tibor Kaputa 2021-09-24 18:41:38 +02:00 committed by GitHub
commit 7fae2d8324
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
53 changed files with 1775 additions and 55 deletions

View file

@ -1,5 +1,7 @@
package com.simplemobiletools.smsmessenger.extensions
import android.content.ContentValues
inline fun <T> List<T>.indexOfFirstOrNull(predicate: (T) -> Boolean): Int? {
var index = 0
for (item in this) {
@ -9,3 +11,22 @@ inline fun <T> List<T>.indexOfFirstOrNull(predicate: (T) -> Boolean): Int? {
}
return null
}
fun Map<String, Any>.toContentValues(): ContentValues {
val contentValues = ContentValues()
for (item in entries) {
when (val value = item.value) {
is String -> contentValues.put(item.key, value)
is Byte -> contentValues.put(item.key, value)
is Short -> contentValues.put(item.key, value)
is Int -> contentValues.put(item.key, value)
is Long -> contentValues.put(item.key, value)
is Float -> contentValues.put(item.key, value)
is Double -> contentValues.put(item.key, value)
is Boolean -> contentValues.put(item.key, value)
is ByteArray -> contentValues.put(item.key, value)
}
}
return contentValues
}

View file

@ -255,6 +255,20 @@ fun Context.getConversations(threadId: Long? = null, privateContacts: ArrayList<
return conversations
}
fun Context.getConversationIds(): List<Long> {
val uri = Uri.parse("${Threads.CONTENT_URI}?simple=true")
val projection = arrayOf(Threads._ID)
val selection = "${Threads.MESSAGE_COUNT} > ?"
val selectionArgs = arrayOf("0")
val sortOrder = "${Threads.DATE} ASC"
val conversationIds = mutableListOf<Long>()
queryCursor(uri, projection, selection, selectionArgs, sortOrder, true) { cursor ->
val id = cursor.getLongValue(Threads._ID)
conversationIds.add(id)
}
return conversationIds
}
// based on https://stackoverflow.com/a/6446831/1967672
@SuppressLint("NewApi")
fun Context.getMmsAttachment(id: Long): MessageAttachment {

View file

@ -0,0 +1,20 @@
package com.simplemobiletools.smsmessenger.extensions
import android.database.Cursor
import com.google.gson.JsonNull
import com.google.gson.JsonObject
fun Cursor.rowsToJson(): JsonObject {
val obj = JsonObject()
for (i in 0 until columnCount) {
val key = getColumnName(i)
when (getType(i)) {
Cursor.FIELD_TYPE_INTEGER -> obj.addProperty(key, getLong(i))
Cursor.FIELD_TYPE_FLOAT -> obj.addProperty(key, getFloat(i))
Cursor.FIELD_TYPE_STRING -> obj.addProperty(key, getString(i))
Cursor.FIELD_TYPE_NULL -> obj.add(key, JsonNull.INSTANCE)
}
}
return obj
}

View file

@ -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()

View file

@ -0,0 +1,60 @@
package com.simplemobiletools.smsmessenger.extensions.gson
import com.google.gson.*
import java.math.BigDecimal
import java.math.BigInteger
val JsonElement.optString: String?
get() = safeConversion { asString }
val JsonElement.optLong: Long?
get() = safeConversion { asLong }
val JsonElement.optBoolean: Boolean?
get() = safeConversion { asBoolean }
val JsonElement.optFloat: Float?
get() = safeConversion { asFloat }
val JsonElement.optDouble: Double?
get() = safeConversion { asDouble }
val JsonElement.optJsonObject: JsonObject?
get() = safeConversion { asJsonObject }
val JsonElement.optJsonArray: JsonArray?
get() = safeConversion { asJsonArray }
val JsonElement.optJsonPrimitive: JsonPrimitive?
get() = safeConversion { asJsonPrimitive }
val JsonElement.optInt: Int?
get() = safeConversion { asInt }
val JsonElement.optBigDecimal: BigDecimal?
get() = safeConversion { asBigDecimal }
val JsonElement.optBigInteger: BigInteger?
get() = safeConversion { asBigInteger }
val JsonElement.optByte: Byte?
get() = safeConversion { asByte }
val JsonElement.optShort: Short?
get() = safeConversion { asShort }
val JsonElement.optJsonNull: JsonNull?
get() = safeConversion { asJsonNull }
val JsonElement.optCharacter: Char?
get() = safeConversion { asCharacter }
private fun <T> JsonElement.safeConversion(converter: () -> T?): T? {
return try {
converter()
} catch (e: Exception) {
null
}
}

View file

@ -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

View file

@ -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
}
}