Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conversation dates on import #265

Merged
merged 3 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions app/src/main/kotlin/org/fossify/messages/extensions/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1117,16 +1117,22 @@ fun Context.deleteSmsDraft(threadId: Long) {
}

fun Context.updateLastConversationMessage(threadId: Long) {
// update the date and the snippet of the thread, by triggering the
updateLastConversationMessage(setOf(threadId))
}

fun Context.updateLastConversationMessage(threadIds: Iterable<Long>) {
// update the date and the snippet of the threads, by triggering the
// following Android code (which runs even if no messages are deleted):
// https://android.googlesource.com/platform/packages/providers/TelephonyProvider/+/android14-release/src/com/android/providers/telephony/MmsSmsProvider.java#1409
val uri = Threads.CONTENT_URI
val selection =
"1 = 0" // always-false condition, because we don't actually want to delete any messages
try {
contentResolver.delete(uri, selection, null)
val newConversation = getConversations(threadId)[0]
insertOrUpdateConversation(newConversation)
for (threadId in threadIds) {
val newConversation = getConversations(threadId)[0]
insertOrUpdateConversation(newConversation)
}
} catch (e: Exception) {
}
}
Expand Down
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)
}
}
Loading