Skip to content

Commit

Permalink
Fix conversation dates on import
Browse files Browse the repository at this point in the history
When we insert a message, Android's Telephony provider sets the
conversation date to the current time rather than the time of the
message that was inserted.[1] This commit implements a workaround that
fixes the conversation timestamps.

Fixes FossifyOrg#146, FossifyOrg#42.

[1] https://android.googlesource.com/platform/packages/providers/TelephonyProvider/+/android14-release/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java#134
  • Loading branch information
tom93 committed Dec 27, 2024
1 parent bfd5450 commit 3ad36c0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class MessagesImporter(private val activity: SimpleActivity) {
messagesFailed++
}
}
messageWriter.fixCoversationDates()
refreshMessages()
} catch (e: Exception) {
activity.showErrorToast(e)
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/kotlin/org/fossify/messages/helpers/MessagesWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.klinker.android.send_message.Utils
import org.fossify.commons.extensions.getLongValue
import org.fossify.commons.extensions.queryCursor
import org.fossify.commons.helpers.isRPlus
import org.fossify.messages.extensions.updateLastConversationMessage
import org.fossify.messages.models.MmsAddress
import org.fossify.messages.models.MmsBackup
import org.fossify.messages.models.MmsPart
Expand All @@ -19,12 +20,14 @@ import org.fossify.messages.models.SmsBackup
class MessagesWriter(private val context: Context) {
private val INVALID_ID = -1L
private val contentResolver = context.contentResolver
private val modifiedThreadIds = mutableSetOf<Long>()

fun writeSmsMessage(smsBackup: SmsBackup) {
val contentValues = smsBackup.toContentValues()
val threadId = Utils.getOrCreateThreadId(context, smsBackup.address)
contentValues.put(Sms.THREAD_ID, threadId)
if (!smsExist(smsBackup)) {
modifiedThreadIds.add(threadId)
contentResolver.insert(Sms.CONTENT_URI, contentValues)
}
}
Expand All @@ -50,6 +53,7 @@ class MessagesWriter(private val context: Context) {
if (threadId != INVALID_ID) {
contentValues.put(Mms.THREAD_ID, threadId)
if (!mmsExist(mmsBackup)) {
modifiedThreadIds.add(threadId)
contentResolver.insert(Mms.CONTENT_URI, contentValues)
}
val messageId = getMmsId(mmsBackup)
Expand Down Expand Up @@ -152,4 +156,16 @@ class MessagesWriter(private val context: Context) {
}
return exists
}

/** Fixes the timestamps of all conversations modified by previous writes. */
fun fixCoversationDates() {
// This method should be called after messages are written, to set the correct conversation
// timestamps.
//
// It is necessary because when we insert a message, Android's Telephony provider sets the
// conversation date to the current time rather than the time of the message that was
// inserted. Source code reference:
// https://android.googlesource.com/platform/packages/providers/TelephonyProvider/+/android14-release/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java#134
context.updateLastConversationMessage(modifiedThreadIds)
}
}

0 comments on commit 3ad36c0

Please sign in to comment.