back up messages
This commit is contained in:
parent
b74a511a5e
commit
7f32115afe
7 changed files with 417 additions and 2 deletions
|
|
@ -35,9 +35,9 @@ import com.simplemobiletools.smsmessenger.interfaces.MessagesDao
|
|||
import com.simplemobiletools.smsmessenger.models.*
|
||||
import com.simplemobiletools.smsmessenger.receivers.DirectReplyReceiver
|
||||
import com.simplemobiletools.smsmessenger.receivers.MarkAsReadReceiver
|
||||
import me.leolin.shortcutbadger.ShortcutBadger
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
import me.leolin.shortcutbadger.ShortcutBadger
|
||||
|
||||
val Context.config: Config get() = Config.newInstance(applicationContext)
|
||||
|
||||
|
|
@ -251,6 +251,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} DESC"
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
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
|
||||
}
|
||||
|
|
@ -0,0 +1,99 @@
|
|||
package com.simplemobiletools.smsmessenger.extensions
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue