back up messages
This commit is contained in:
parent
b74a511a5e
commit
7f32115afe
7 changed files with 417 additions and 2 deletions
|
|
@ -0,0 +1,44 @@
|
|||
package com.simplemobiletools.smsmessenger.helpers
|
||||
|
||||
import com.google.gson.JsonArray
|
||||
import com.google.gson.JsonNull
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonPrimitive
|
||||
import com.google.gson.stream.JsonWriter
|
||||
|
||||
class JsonObjectWriter(private val writer: JsonWriter) {
|
||||
|
||||
fun dump(obj: JsonObject) {
|
||||
writer.beginObject()
|
||||
for (key in obj.keySet()) {
|
||||
writer.name(key)
|
||||
val keyObj = obj.get(key)
|
||||
dump(keyObj)
|
||||
}
|
||||
writer.endObject()
|
||||
}
|
||||
|
||||
private fun dump(arr: JsonArray) {
|
||||
writer.beginArray()
|
||||
for (i in 0 until arr.size()) {
|
||||
dump(arr.get(i))
|
||||
}
|
||||
writer.endArray()
|
||||
}
|
||||
|
||||
private fun dump(obj: Any) {
|
||||
when (obj) {
|
||||
is JsonNull -> writer.nullValue()
|
||||
is JsonPrimitive -> {
|
||||
when{
|
||||
obj.isString -> writer.value(obj.asString)
|
||||
obj.isBoolean -> writer.value(obj.asNumber)
|
||||
obj.isNumber -> writer.value(obj.asBoolean)
|
||||
obj.isNumber -> writer.value(obj.asBoolean)
|
||||
}
|
||||
}
|
||||
is JsonArray -> dump(obj)
|
||||
is JsonObject -> dump(obj)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
package com.simplemobiletools.smsmessenger.helpers
|
||||
|
||||
import android.content.Context
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||
import com.simplemobiletools.smsmessenger.extensions.config
|
||||
import com.simplemobiletools.smsmessenger.extensions.getConversationIds
|
||||
import java.io.OutputStream
|
||||
|
||||
class MessagesExporter(private val context: Context) {
|
||||
enum class ExportResult {
|
||||
EXPORT_FAIL, EXPORT_OK
|
||||
}
|
||||
|
||||
private val config = context.config
|
||||
private val messageReader = MessagesReader(context)
|
||||
|
||||
fun exportMessages(
|
||||
outputStream: OutputStream?,
|
||||
callback: (result: ExportResult) -> Unit,
|
||||
) {
|
||||
ensureBackgroundThread {
|
||||
if (outputStream == null) {
|
||||
callback.invoke(ExportResult.EXPORT_FAIL)
|
||||
return@ensureBackgroundThread
|
||||
}
|
||||
|
||||
/*
|
||||
* We should have json in this format
|
||||
* {
|
||||
* "threadId" : {
|
||||
* "threadId": ""
|
||||
* "sms": [{ smses }],
|
||||
* "mms": [{ mmses }]
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* */
|
||||
val writer = JsonWriter(outputStream.bufferedWriter())
|
||||
writer.use {
|
||||
try {
|
||||
var written = 0
|
||||
writer.beginObject()
|
||||
val conversationIds = context.getConversationIds()
|
||||
for(threadId in conversationIds){
|
||||
writer.name(threadId.toString())
|
||||
|
||||
writer.beginObject()
|
||||
writer.name("threadId")
|
||||
writer.value(threadId)
|
||||
if(config.exportSms){
|
||||
writer.name("sms")
|
||||
writer.beginArray()
|
||||
//write all sms
|
||||
messageReader.forEachSms(threadId){
|
||||
JsonObjectWriter(writer).dump(it)
|
||||
written++
|
||||
}
|
||||
writer.endArray()
|
||||
}
|
||||
|
||||
if(config.exportMms){
|
||||
writer.name("mms")
|
||||
writer.beginArray()
|
||||
//write all mms
|
||||
messageReader.forEachMms(threadId){
|
||||
JsonObjectWriter(writer).dump(it)
|
||||
written++
|
||||
}
|
||||
|
||||
writer.endArray()
|
||||
}
|
||||
|
||||
writer.endObject()
|
||||
}
|
||||
writer.endObject()
|
||||
callback.invoke(ExportResult.EXPORT_OK)
|
||||
} catch (e: Exception) {
|
||||
callback.invoke(ExportResult.EXPORT_FAIL)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,143 @@
|
|||
package com.simplemobiletools.smsmessenger.helpers
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.provider.Telephony
|
||||
import android.util.Base64
|
||||
import android.util.Log
|
||||
import com.google.android.mms.pdu_alt.PduHeaders
|
||||
import com.google.gson.JsonArray
|
||||
import com.google.gson.JsonObject
|
||||
import com.simplemobiletools.commons.extensions.getIntValue
|
||||
import com.simplemobiletools.commons.extensions.getStringValue
|
||||
import com.simplemobiletools.commons.extensions.queryCursor
|
||||
import com.simplemobiletools.commons.helpers.isQPlus
|
||||
import com.simplemobiletools.smsmessenger.extensions.optLong
|
||||
import com.simplemobiletools.smsmessenger.extensions.optString
|
||||
import com.simplemobiletools.smsmessenger.extensions.rowsToJson
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
|
||||
class MessagesReader(private val context: Context) {
|
||||
companion object {
|
||||
private const val TAG = "MessagesReader"
|
||||
private const val MMS_CONTENT = "mms_content"
|
||||
}
|
||||
fun forEachSms(threadId: Long, block: (JsonObject) -> Unit) {
|
||||
forEachThreadMessage(Telephony.Sms.CONTENT_URI, threadId, block)
|
||||
}
|
||||
|
||||
fun forEachMms(threadId: Long, includeAttachment: Boolean = true, block: (JsonObject) -> Unit) {
|
||||
forEachThreadMessage(Telephony.Mms.CONTENT_URI, threadId) { obj ->
|
||||
if (includeAttachment) {
|
||||
obj.add("parts", getParts(obj.getAsJsonPrimitive("_id").asLong))
|
||||
}
|
||||
obj.add(Telephony.CanonicalAddressesColumns.ADDRESS, getMMSAddresses(obj.getAsJsonPrimitive("_id").asLong))
|
||||
block(obj)
|
||||
}
|
||||
}
|
||||
|
||||
private fun forEachThreadMessage(contentUri: Uri, threadId: Long, block: (JsonObject) -> Unit) {
|
||||
val selection = "${Telephony.Sms.THREAD_ID} = ?"
|
||||
val selectionArgs = arrayOf(threadId.toString())
|
||||
context.queryCursor(contentUri, null, selection, selectionArgs) { cursor ->
|
||||
val json = cursor.rowsToJson()
|
||||
forceMillisDate(json, "date")
|
||||
forceMillisDate(json, "date_sent")
|
||||
block(json)
|
||||
}
|
||||
}
|
||||
|
||||
private fun forceMillisDate(message: JsonObject, field: String) {
|
||||
/* sometimes the sms are in millis and the mms in secs... */
|
||||
if (message.get(field).isJsonPrimitive) {
|
||||
val value = message.get(field).optLong
|
||||
if (value != null && value != 0L && value < 500000000000L) { // 500000000000 = Tuesday, 5 November 1985 00:53:20 GMT
|
||||
message.addProperty(field, value * 1000)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
private fun getParts(mmsId: Long): JsonArray {
|
||||
val jsonArray = JsonArray()
|
||||
val uri = if (isQPlus()) {
|
||||
Telephony.Mms.Part.CONTENT_URI
|
||||
} else {
|
||||
Uri.parse("content://mms/part")
|
||||
}
|
||||
|
||||
val selection = "${Telephony.Mms.Part.MSG_ID}=$mmsId"
|
||||
context.queryCursor(uri, emptyArray(), selection) { cursor ->
|
||||
val part = cursor.rowsToJson()
|
||||
|
||||
val hasTextValue = (part.has(Telephony.Mms.Part.TEXT) && !part.get(Telephony.Mms.Part.TEXT).optString.isNullOrEmpty())
|
||||
|
||||
when {
|
||||
hasTextValue -> {
|
||||
part.addProperty(MMS_CONTENT, "")
|
||||
}
|
||||
|
||||
part.get(Telephony.Mms.Part.CONTENT_TYPE).optString?.startsWith("text/") == true -> {
|
||||
part.addProperty(MMS_CONTENT, usePart(part.get(Telephony.Mms.Part._ID).asLong) { stream ->
|
||||
stream.readBytes().toString(Charsets.UTF_8)
|
||||
})
|
||||
}
|
||||
else -> {
|
||||
part.addProperty(MMS_CONTENT, usePart(part.get(Telephony.Mms.Part._ID).asLong) { stream ->
|
||||
Base64.encodeToString(stream.readBytes(), Base64.DEFAULT)
|
||||
})
|
||||
}
|
||||
}
|
||||
jsonArray.add(part)
|
||||
}
|
||||
|
||||
return jsonArray
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
private fun usePart(partId: Long, block: (InputStream) -> String): String {
|
||||
val partUri = if (isQPlus()) {
|
||||
Telephony.Mms.Part.CONTENT_URI.buildUpon().appendPath(partId.toString()).build()
|
||||
} else {
|
||||
Uri.parse("content://mms/part/$partId")
|
||||
}
|
||||
try {
|
||||
val stream = context.contentResolver.openInputStream(partUri)
|
||||
if (stream == null) {
|
||||
val msg = "failed opening stream for mms part $partUri"
|
||||
Log.e(TAG, msg)
|
||||
return ""
|
||||
}
|
||||
stream.use {
|
||||
return block(stream)
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
val msg = "failed to read MMS part on $partUri"
|
||||
Log.e(TAG, msg, e)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
private fun getMMSAddresses(messageId: Long): JsonArray {
|
||||
val jsonArray = JsonArray()
|
||||
val addressUri = if (isQPlus()) {
|
||||
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 selection = "${Telephony.Mms.Addr.MSG_ID}=$messageId"
|
||||
|
||||
context.queryCursor(addressUri, projection, selection) { cursor ->
|
||||
when (cursor.getIntValue(Telephony.Mms.Addr.TYPE)) {
|
||||
PduHeaders.FROM, PduHeaders.TO, PduHeaders.CC, PduHeaders.BCC -> jsonArray.add(cursor.getStringValue(Telephony.Mms.Addr.ADDRESS))
|
||||
}
|
||||
}
|
||||
|
||||
return jsonArray
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue