mirror of
https://github.com/SimpleMobileTools/Simple-SMS-Messenger.git
synced 2025-03-10 00:20:14 +01:00
Import SMS
This commit is contained in:
parent
7f32115afe
commit
9656207135
@ -401,7 +401,7 @@ class MainActivity : SimpleActivity() {
|
||||
ImportMessagesDialog(this, path) { refresh ->
|
||||
if (refresh) {
|
||||
runOnUiThread {
|
||||
// refresh
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import android.view.ViewGroup
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.simplemobiletools.commons.dialogs.FilePickerDialog
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||
import com.simplemobiletools.smsmessenger.R
|
||||
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
||||
import com.simplemobiletools.smsmessenger.extensions.config
|
||||
@ -58,14 +57,11 @@ class ExportMessagesDialog(
|
||||
return@setOnClickListener
|
||||
}
|
||||
|
||||
ensureBackgroundThread {
|
||||
config.lastExportPath = file.absolutePath.getParentPath()
|
||||
config.exportSms = view.export_sms_checkbox.isChecked
|
||||
config.exportMms = view.export_mms_checkbox.isChecked
|
||||
|
||||
callback(file)
|
||||
dismiss()
|
||||
}
|
||||
config.exportSms = view.export_sms_checkbox.isChecked
|
||||
config.exportMms = view.export_mms_checkbox.isChecked
|
||||
config.lastExportPath = file.absolutePath.getParentPath()
|
||||
callback(file)
|
||||
dismiss()
|
||||
}
|
||||
else -> activity.toast(R.string.invalid_name)
|
||||
}
|
||||
|
@ -3,9 +3,12 @@ package com.simplemobiletools.smsmessenger.dialogs
|
||||
import android.view.ViewGroup
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
||||
import com.simplemobiletools.commons.extensions.toast
|
||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||
import com.simplemobiletools.smsmessenger.R
|
||||
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
||||
import com.simplemobiletools.smsmessenger.extensions.config
|
||||
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter
|
||||
import kotlinx.android.synthetic.main.dialog_import_messages.view.import_mms_checkbox
|
||||
import kotlinx.android.synthetic.main.dialog_import_messages.view.import_sms_checkbox
|
||||
|
||||
@ -29,8 +32,16 @@ class ImportMessagesDialog(
|
||||
.create().apply {
|
||||
activity.setupDialogStuff(view, this, R.string.import_messages) {
|
||||
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
|
||||
dismiss()
|
||||
callback.invoke(true)
|
||||
activity.toast(R.string.importing)
|
||||
config.importSms = view.import_sms_checkbox.isChecked
|
||||
config.importMms = view.import_mms_checkbox.isChecked
|
||||
ensureBackgroundThread {
|
||||
MessagesImporter(activity).importMessages(path){
|
||||
|
||||
}
|
||||
dismiss()
|
||||
callback.invoke(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -73,7 +73,11 @@ fun Context.getMessages(threadId: Long): ArrayList<Message> {
|
||||
val blockedNumbers = getBlockedNumbers()
|
||||
var messages = ArrayList<Message>()
|
||||
queryCursor(uri, projection, selection, selectionArgs, sortOrder, showErrors = true) { cursor ->
|
||||
val senderNumber = cursor.getStringValue(Sms.ADDRESS) ?: return@queryCursor
|
||||
val senderNumber = cursor.getStringValue(Sms.ADDRESS)
|
||||
|
||||
if(senderNumber == null){
|
||||
return@queryCursor
|
||||
}
|
||||
|
||||
val isNumberBlocked = if (blockStatus.containsKey(senderNumber)) {
|
||||
blockStatus[senderNumber]!!
|
||||
@ -251,6 +255,32 @@ 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)
|
||||
|
@ -4,8 +4,7 @@ import android.database.Cursor
|
||||
import com.google.gson.JsonNull
|
||||
import com.google.gson.JsonObject
|
||||
|
||||
fun Cursor.
|
||||
rowsToJson(): JsonObject {
|
||||
fun Cursor.rowsToJson(): JsonObject {
|
||||
val obj = JsonObject()
|
||||
for (i in 0 until columnCount) {
|
||||
val key = getColumnName(i)
|
||||
|
@ -22,6 +22,7 @@ const val EXPORT_MIME_TYPE = "application/json"
|
||||
const val EXPORT_FILE_EXT = ".json"
|
||||
const val IMPORT_SMS = "import_sms"
|
||||
const val IMPORT_MMS = "import_mms"
|
||||
const val MMS_CONTENT = "mms_content"
|
||||
|
||||
private const val PATH = "com.simplemobiletools.smsmessenger.action."
|
||||
const val MARK_AS_READ = PATH + "mark_as_read"
|
||||
|
@ -8,37 +8,36 @@ import com.google.gson.stream.JsonWriter
|
||||
|
||||
class JsonObjectWriter(private val writer: JsonWriter) {
|
||||
|
||||
fun dump(obj: JsonObject) {
|
||||
fun write(obj: JsonObject) {
|
||||
writer.beginObject()
|
||||
for (key in obj.keySet()) {
|
||||
writer.name(key)
|
||||
val keyObj = obj.get(key)
|
||||
dump(keyObj)
|
||||
write(keyObj)
|
||||
}
|
||||
writer.endObject()
|
||||
}
|
||||
|
||||
private fun dump(arr: JsonArray) {
|
||||
private fun write(arr: JsonArray) {
|
||||
writer.beginArray()
|
||||
for (i in 0 until arr.size()) {
|
||||
dump(arr.get(i))
|
||||
write(arr.get(i))
|
||||
}
|
||||
writer.endArray()
|
||||
}
|
||||
|
||||
private fun dump(obj: Any) {
|
||||
private fun write(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)
|
||||
obj.isBoolean -> writer.value(obj.asBoolean)
|
||||
obj.isNumber -> writer.value(obj.asNumber)
|
||||
}
|
||||
}
|
||||
is JsonArray -> dump(obj)
|
||||
is JsonObject -> dump(obj)
|
||||
is JsonArray -> write(obj)
|
||||
is JsonObject -> write(obj)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ class MessagesExporter(private val context: Context) {
|
||||
writer.beginArray()
|
||||
//write all sms
|
||||
messageReader.forEachSms(threadId){
|
||||
JsonObjectWriter(writer).dump(it)
|
||||
JsonObjectWriter(writer).write(it)
|
||||
written++
|
||||
}
|
||||
writer.endArray()
|
||||
@ -64,7 +64,7 @@ class MessagesExporter(private val context: Context) {
|
||||
writer.beginArray()
|
||||
//write all mms
|
||||
messageReader.forEachMms(threadId){
|
||||
JsonObjectWriter(writer).dump(it)
|
||||
JsonObjectWriter(writer).write(it)
|
||||
written++
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,83 @@
|
||||
package com.simplemobiletools.smsmessenger.helpers
|
||||
|
||||
import android.content.Context
|
||||
import android.provider.Telephony
|
||||
import android.util.Log
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import com.simplemobiletools.commons.extensions.queryCursor
|
||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||
import com.simplemobiletools.smsmessenger.extensions.*
|
||||
import com.simplemobiletools.smsmessenger.models.ExportedMessage
|
||||
import java.io.File
|
||||
|
||||
class MessagesImporter(private val context: Context) {
|
||||
companion object {
|
||||
private const val TAG = "MessagesImporter"
|
||||
}
|
||||
|
||||
enum class ImportResult {
|
||||
IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL, IMPORT_NOTHING_NEW
|
||||
}
|
||||
|
||||
private val gson = Gson()
|
||||
private val messageWriter = MessagesWriter(context)
|
||||
private val config = context.config
|
||||
|
||||
fun importMessages(path: String, callback: (result: ImportResult) -> Unit) {
|
||||
ensureBackgroundThread {
|
||||
if (path.isEmpty()) {
|
||||
callback.invoke(ImportResult.IMPORT_FAIL)
|
||||
return@ensureBackgroundThread
|
||||
}
|
||||
|
||||
//read data from path
|
||||
// parse json
|
||||
// write data to sql db
|
||||
val inputStream = if (path.contains("/")) {
|
||||
File(path).inputStream()
|
||||
} else {
|
||||
context.assets.open(path)
|
||||
}
|
||||
|
||||
inputStream.bufferedReader().use {
|
||||
try {
|
||||
val json = it.readText()
|
||||
Log.d(TAG, "importMessages: json== ${json.length}")
|
||||
val type = object : TypeToken<Map<String, ExportedMessage>>() {}.type
|
||||
val data = gson.fromJson<Map<String, ExportedMessage>>(json, type)
|
||||
Log.d(TAG, "importMessages: ${data.size}")
|
||||
for (message in data.values) {
|
||||
// add sms
|
||||
if (config.importSms) {
|
||||
message.sms.forEach(messageWriter::writeSmsMessage)
|
||||
}
|
||||
|
||||
// add mms
|
||||
if (config.importMms) {
|
||||
message.sms.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 ->
|
||||
val json = cursor.rowsToJson()
|
||||
Log.w(TAG, "messages = $json")
|
||||
}
|
||||
|
||||
refreshMessages()
|
||||
|
||||
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "importMessages: ", e)
|
||||
callback.invoke(ImportResult.IMPORT_FAIL)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -22,7 +22,6 @@ 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)
|
||||
|
@ -0,0 +1,85 @@
|
||||
package com.simplemobiletools.smsmessenger.helpers
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.provider.Telephony
|
||||
import android.util.Log
|
||||
import com.simplemobiletools.commons.extensions.queryCursor
|
||||
import com.simplemobiletools.smsmessenger.extensions.getThreadId
|
||||
import com.simplemobiletools.smsmessenger.extensions.toContentValues
|
||||
|
||||
class MessagesWriter(private val context: Context) {
|
||||
companion object {
|
||||
private const val TAG = "MessagesWriter"
|
||||
}
|
||||
|
||||
private val contentResolver = context.contentResolver
|
||||
|
||||
fun writeSmsMessage(map: Map<String, String>) {
|
||||
Log.w(TAG, "writeSmsMessage: map=$map")
|
||||
val contentValues = map.toContentValues()
|
||||
contentValues.remove(Telephony.Sms._ID)
|
||||
replaceThreadId(contentValues)
|
||||
Log.d(TAG, "writeSmsMessage: contentValues=$contentValues")
|
||||
val type = contentValues.getAsInteger(Telephony.Sms.TYPE)
|
||||
Log.d(TAG, "writeSmsMessage: type=$type")
|
||||
if ((type == Telephony.Sms.MESSAGE_TYPE_INBOX || type == Telephony.Sms.MESSAGE_TYPE_SENT) && !smsExist(map)) {
|
||||
Log.d(TAG, "writeSmsMessage: Inserting SMS...")
|
||||
val uri = Telephony.Sms.CONTENT_URI
|
||||
Log.d(TAG, "writeSmsMessage: uri=$uri")
|
||||
contentResolver.insert(Telephony.Sms.CONTENT_URI, contentValues)
|
||||
}
|
||||
}
|
||||
|
||||
private fun replaceThreadId(contentValues: ContentValues) {
|
||||
val address = contentValues.get(Telephony.Sms.ADDRESS)
|
||||
val threadId = context.getThreadId(address.toString())
|
||||
contentValues.put(Telephony.Sms.THREAD_ID, threadId)
|
||||
}
|
||||
|
||||
fun writeMmsMessage(map: Map<String, Any>) {
|
||||
|
||||
}
|
||||
|
||||
private fun smsExist(sms: Map<String, String>): Boolean {
|
||||
val uri = Telephony.Sms.CONTENT_URI
|
||||
val projection = arrayOf(Telephony.Sms._ID)
|
||||
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)
|
||||
|
||||
var exists = false
|
||||
context.queryCursor(uri, projection, selection, selectionArgs) {
|
||||
exists = it.count > 0
|
||||
Log.i(TAG, "smsExist After: $exists")
|
||||
}
|
||||
Log.i(TAG, "smsExist: $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(){
|
||||
// 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
|
||||
// negative conversation id to delete should to the trick
|
||||
contentResolver.delete(Telephony.Sms.Conversations.CONTENT_URI.buildUpon().appendPath("-1").build(), null, null)
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.simplemobiletools.smsmessenger.models
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class ExportedMessage(
|
||||
@SerializedName("threadId")
|
||||
val threadId: Long,
|
||||
@SerializedName("sms")
|
||||
val sms: List<Map<String, String>>,
|
||||
@SerializedName("mms")
|
||||
val mms: List<Map<String, Any>>,
|
||||
)
|
@ -0,0 +1,46 @@
|
||||
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
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user