Skip to content

Commit

Permalink
Add message details menu button
Browse files Browse the repository at this point in the history
Adds a "Properties" menu button in conversation, when one message
is selected, which displays details about the message:
- Type (SMS or MMS)
- Sender phone number (or receiver if it is a sent message)
- Used SIM
- Date sent at
- Date received at (if it is an incoming message)

This closes traccar#19
  • Loading branch information
esensar committed Jul 10, 2023
1 parent 9942fb7 commit bdd506c
Show file tree
Hide file tree
Showing 56 changed files with 530 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,7 @@ class ThreadActivity : SimpleActivity() {
status = STATUS_NONE,
participants = participants,
date = (scheduledDateTime.millis / 1000).toInt(),
dateSent = (scheduledDateTime.millis / 1000).toInt(),
read = false,
threadId = threadId,
isMMS = isMmsMessage(text),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.simplemobiletools.smsmessenger.activities.NewConversationActivity
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
import com.simplemobiletools.smsmessenger.activities.ThreadActivity
import com.simplemobiletools.smsmessenger.activities.VCardViewerActivity
import com.simplemobiletools.smsmessenger.dialogs.MessageDetailsDialog
import com.simplemobiletools.smsmessenger.dialogs.SelectTextDialog
import com.simplemobiletools.smsmessenger.extensions.*
import com.simplemobiletools.smsmessenger.helpers.*
Expand Down Expand Up @@ -82,6 +83,7 @@ class ThreadAdapter(
findItem(R.id.cab_share).isVisible = isOneItemSelected && hasText
findItem(R.id.cab_forward_message).isVisible = isOneItemSelected
findItem(R.id.cab_select_text).isVisible = isOneItemSelected && hasText
findItem(R.id.cab_properties).isVisible = isOneItemSelected
}
}

Expand All @@ -98,6 +100,7 @@ class ThreadAdapter(
R.id.cab_select_text -> selectText()
R.id.cab_delete -> askConfirmDelete()
R.id.cab_select_all -> selectAll()
R.id.cab_properties -> showMessageDetails()
}
}

Expand Down Expand Up @@ -184,6 +187,12 @@ class ThreadAdapter(
}
}

private fun showMessageDetails() {
val message = getSelectedItems().firstOrNull() as? Message ?: return
MessageDetailsDialog(activity, message)
}


private fun askConfirmDelete() {
val itemsCnt = selectedKeys.size

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.simplemobiletools.smsmessenger.models.Conversation
import com.simplemobiletools.smsmessenger.models.Message
import com.simplemobiletools.smsmessenger.models.MessageAttachment

@Database(entities = [Conversation::class, Attachment::class, MessageAttachment::class, Message::class], version = 7)
@Database(entities = [Conversation::class, Attachment::class, MessageAttachment::class, Message::class], version = 8)
@TypeConverters(Converters::class)
abstract class MessagesDatabase : RoomDatabase() {

Expand All @@ -44,6 +44,7 @@ abstract class MessagesDatabase : RoomDatabase() {
.addMigrations(MIGRATION_4_5)
.addMigrations(MIGRATION_5_6)
.addMigrations(MIGRATION_6_7)
.addMigrations(MIGRATION_7_8)
.build()
}
}
Expand Down Expand Up @@ -115,5 +116,14 @@ abstract class MessagesDatabase : RoomDatabase() {
}
}
}

private val MIGRATION_7_8 = object : Migration(7, 8) {
override fun migrate(database: SupportSQLiteDatabase) {
database.apply {
execSQL("ALTER TABLE messages ADD COLUMN date_sent INTEGER NOT NULL DEFAULT 0")
execSQL("UPDATE messages SET date_sent = date")
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.simplemobiletools.smsmessenger.dialogs

import android.annotation.SuppressLint
import android.telephony.SubscriptionInfo
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.getAlertDialogBuilder
import com.simplemobiletools.commons.extensions.getTimeFormat
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.extensions.config
import com.simplemobiletools.smsmessenger.extensions.subscriptionManagerCompat
import com.simplemobiletools.smsmessenger.models.Message
import kotlinx.android.synthetic.main.dialog_message_details.view.dialog_message_details_text_value
import org.joda.time.DateTime

class MessageDetailsDialog(val activity: BaseSimpleActivity, val message: Message) {
init {
@SuppressLint("MissingPermission")
val availableSIMs = activity.subscriptionManagerCompat().activeSubscriptionInfoList

@SuppressLint("SetTextI18n")
val view = activity.layoutInflater.inflate(R.layout.dialog_message_details, null).apply {
dialog_message_details_text_value.text = """
${activity.getString(R.string.message_details_type)}: ${message.getMessageType()}
${message.getReceiverOrSenderLabel()}: ${message.getReceiverOrSenderPhoneNumbers()}
SIM: ${message.getSIM(availableSIMs)}
${activity.getString(R.string.message_details_sent_at)}: ${message.getSentAt()}
${message.getReceivedAtLine()}
""".trimIndent().trimEnd()
}

activity.getAlertDialogBuilder()
.setPositiveButton(R.string.ok) { _, _ -> }
.apply {
activity.setupDialogStuff(view, this, R.string.message_details)
}
}

private fun Message.getMessageType(): String = if (isMMS) "MMS" else "SMS"

private fun Message.getReceiverOrSenderLabel(): String {
return if (isReceivedMessage()) {
activity.getString(R.string.message_details_sender)
} else {
activity.getString(R.string.message_details_receiver)
}
}

private fun Message.getReceiverOrSenderPhoneNumbers(): String {
return participants.joinToString(", ") { it.phoneNumbers.first().value }
}

private fun Message.getSIM(availableSIMs: List<SubscriptionInfo>): String {
return availableSIMs.firstOrNull { it.subscriptionId == subscriptionId }?.displayName?.toString() ?: activity.getString(R.string.unknown)
}

private fun Message.getSentAt(): String {
return DateTime(dateSent * 1000L).toString(activity.config.dateFormat + " " + activity.getTimeFormat())
}

private fun Message.getReceivedAtLine(): String {
return if (isReceivedMessage()) {
"${activity.getString(R.string.message_details_received_at)}: ${getReceivedAt()}"
} else {
""
}
}

private fun Message.getReceivedAt(): String {
return DateTime(date * 1000L).toString(activity.config.dateFormat + " " + activity.getTimeFormat())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fun Context.getMessages(
Sms.TYPE,
Sms.ADDRESS,
Sms.DATE,
Sms.DATE_SENT,
Sms.READ,
Sms.THREAD_ID,
Sms.SUBSCRIPTION_ID,
Expand Down Expand Up @@ -106,6 +107,7 @@ fun Context.getMessages(
val senderName = namePhoto.name
val photoUri = namePhoto.photoUri ?: ""
val date = (cursor.getLongValue(Sms.DATE) / 1000).toInt()
val dateSent = (cursor.getLongValue(Sms.DATE_SENT) / 1000).toInt()
val read = cursor.getIntValue(Sms.READ) == 1
val thread = cursor.getLongValue(Sms.THREAD_ID)
val subscriptionId = cursor.getIntValue(Sms.SUBSCRIPTION_ID)
Expand All @@ -117,7 +119,23 @@ fun Context.getMessages(
}
val isMMS = false
val message =
Message(id, body, type, status, ArrayList(participants), date, read, thread, isMMS, null, senderNumber, senderName, photoUri, subscriptionId)
Message(
id,
body,
type,
status,
ArrayList(participants),
date,
dateSent,
read,
thread,
isMMS,
null,
senderNumber,
senderName,
photoUri,
subscriptionId
)
messages.add(message)
}

Expand Down Expand Up @@ -148,6 +166,7 @@ fun Context.getMMS(threadId: Long? = null, getImageResolutions: Boolean = false,
val projection = arrayOf(
Mms._ID,
Mms.DATE,
Mms.DATE_SENT,
Mms.READ,
Mms.MESSAGE_BOX,
Mms.THREAD_ID,
Expand Down Expand Up @@ -175,6 +194,7 @@ fun Context.getMMS(threadId: Long? = null, getImageResolutions: Boolean = false,
val mmsId = cursor.getLongValue(Mms._ID)
val type = cursor.getIntValue(Mms.MESSAGE_BOX)
val date = cursor.getLongValue(Mms.DATE).toInt()
val dateSent = cursor.getLongValue(Mms.DATE_SENT).toInt()
val read = cursor.getIntValue(Mms.READ) == 1
val threadId = cursor.getLongValue(Mms.THREAD_ID)
val subscriptionId = cursor.getIntValue(Mms.SUBSCRIPTION_ID)
Expand Down Expand Up @@ -202,7 +222,23 @@ fun Context.getMMS(threadId: Long? = null, getImageResolutions: Boolean = false,
}

val message =
Message(mmsId, body, type, status, participants, date, read, threadId, isMMS, attachment, senderNumber, senderName, senderPhotoUri, subscriptionId)
Message(
mmsId,
body,
type,
status,
participants,
date,
dateSent,
read,
threadId,
isMMS,
attachment,
senderNumber,
senderName,
senderPhotoUri,
subscriptionId
)
messages.add(message)

participants.forEach {
Expand Down Expand Up @@ -560,13 +596,24 @@ fun Context.getNameAndPhotoFromPhoneNumber(number: String): NamePhoto {
return NamePhoto(number, null)
}

fun Context.insertNewSMS(address: String, subject: String, body: String, date: Long, read: Int, threadId: Long, type: Int, subscriptionId: Int): Long {
fun Context.insertNewSMS(
address: String,
subject: String,
body: String,
date: Long,
dateSent: Long,
read: Int,
threadId: Long,
type: Int,
subscriptionId: Int
): Long {
val uri = Sms.CONTENT_URI
val contentValues = ContentValues().apply {
put(Sms.ADDRESS, address)
put(Sms.SUBJECT, subject)
put(Sms.BODY, body)
put(Sms.DATE, date)
put(Sms.DATE_SENT, dateSent)
put(Sms.READ, read)
put(Sms.THREAD_ID, threadId)
put(Sms.TYPE, type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ data class Message(
@ColumnInfo(name = "status") val status: Int,
@ColumnInfo(name = "participants") val participants: ArrayList<SimpleContact>,
@ColumnInfo(name = "date") val date: Int,
@ColumnInfo(name = "date_sent") val dateSent: Int,
@ColumnInfo(name = "read") val read: Boolean,
@ColumnInfo(name = "thread_id") val threadId: Long,
@ColumnInfo(name = "is_mms") val isMMS: Boolean,
Expand Down Expand Up @@ -58,6 +59,7 @@ data class Message(
return old.body == new.body &&
old.threadId == new.threadId &&
old.date == new.date &&
old.dateSent == new.dateSent &&
old.isMMS == new.isMMS &&
old.attachment == new.attachment &&
old.senderPhoneNumber == new.senderPhoneNumber &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SmsReceiver : BroadcastReceiver() {
var body = ""
var subject = ""
var date = 0L
var dateSent = 0L
var threadId = 0L
var status = Telephony.Sms.STATUS_NONE
val type = Telephony.Sms.MESSAGE_TYPE_INBOX
Expand All @@ -38,32 +39,43 @@ class SmsReceiver : BroadcastReceiver() {
status = it.status
body += it.messageBody
date = System.currentTimeMillis()
dateSent = it.timestampMillis
threadId = context.getThreadId(address)
}

if (context.baseConfig.blockUnknownNumbers) {
val simpleContactsHelper = SimpleContactsHelper(context)
simpleContactsHelper.exists(address, privateCursor) { exists ->
if (exists) {
handleMessage(context, address, subject, body, date, read, threadId, type, subscriptionId, status)
handleMessage(context, address, subject, body, date, dateSent, read, threadId, type, subscriptionId, status)
}
}
} else {
handleMessage(context, address, subject, body, date, read, threadId, type, subscriptionId, status)
handleMessage(context, address, subject, body, date, dateSent, read, threadId, type, subscriptionId, status)
}
}
}

private fun handleMessage(
context: Context, address: String, subject: String, body: String, date: Long, read: Int, threadId: Long, type: Int, subscriptionId: Int, status: Int
context: Context,
address: String,
subject: String,
body: String,
date: Long,
dateSent: Long,
read: Int,
threadId: Long,
type: Int,
subscriptionId: Int,
status: Int
) {
val photoUri = SimpleContactsHelper(context).getPhotoUriFromPhoneNumber(address)
val bitmap = context.getNotificationBitmap(photoUri)
Handler(Looper.getMainLooper()).post {
if (!context.isNumberBlocked(address)) {
val privateCursor = context.getMyContactsCursor(favoritesOnly = false, withPhoneNumbersOnly = true)
ensureBackgroundThread {
val newMessageId = context.insertNewSMS(address, subject, body, date, read, threadId, type, subscriptionId)
val newMessageId = context.insertNewSMS(address, subject, body, date, dateSent, read, threadId, type, subscriptionId)

val conversation = context.getConversations(threadId).firstOrNull() ?: return@ensureBackgroundThread
try {
Expand All @@ -81,6 +93,7 @@ class SmsReceiver : BroadcastReceiver() {
val participant = SimpleContact(0, 0, senderName, photoUri, arrayListOf(phoneNumber), ArrayList(), ArrayList())
val participants = arrayListOf(participant)
val messageDate = (date / 1000).toInt()
val messageSentDate = (dateSent / 1000).toInt()

val message =
Message(
Expand All @@ -90,6 +103,7 @@ class SmsReceiver : BroadcastReceiver() {
status,
participants,
messageDate,
messageSentDate,
false,
threadId,
false,
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/dialog_message_details.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<com.simplemobiletools.commons.views.MyTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dialog_message_details_text_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/big_margin"
android:paddingTop="@dimen/big_margin"
android:paddingEnd="@dimen/big_margin"
android:textIsSelectable="true"
android:textSize="@dimen/big_text_size"
tools:text="My sample text" />
5 changes: 5 additions & 0 deletions app/src/main/res/menu/cab_thread.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
android:icon="@drawable/ic_save_vector"
android:title="@string/save_as"
app:showAsAction="ifRoom" />
<item
android:id="@+id/cab_properties"
android:icon="@drawable/ic_info_vector"
android:title="@string/properties"
app:showAsAction="ifRoom" />
<item
android:id="@+id/cab_forward_message"
android:showAsAction="never"
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
<string name="schedule_send_warning">استمر في تشغيل الهاتف وتأكد من عدم وجود شيء يقتل التطبيق أثناء وجوده في الخلفية.</string>
<string name="update_message">Update message</string>
<string name="send_now">ارسل الان</string>
<!-- Message details -->
<string name="message_details">Message details</string>
<string name="message_details_sender">Sender</string>
<string name="message_details_receiver">Receiver</string>
<string name="message_details_type">Type</string>
<string name="message_details_sent_at">Sent at</string>
<string name="message_details_received_at">Received at</string>
<!-- Notifications -->
<string name="channel_received_sms">تم تلقي الرسائل القصيرة</string>
<string name="new_message">رسالة جديدة</string>
Expand Down Expand Up @@ -119,4 +126,4 @@
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
-->
</resources>
</resources>
Loading

0 comments on commit bdd506c

Please sign in to comment.