Explicit IO context for DB/WS/notify path. replyScope already uses Dispatchers.IO; this makes the offload local and explicit. Keep return@withContext only because nothing follows the withContext block today.
This commit is contained in:
1 file changed
+32
-26
@@ -13,6 +13,7 @@ import kotlinx.coroutines.CoroutineScope
|
|||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import kotlinx.coroutines.withTimeout
|
import kotlinx.coroutines.withTimeout
|
||||||
|
|
||||||
open class NotificationReplyReceiver : BroadcastReceiver() {
|
open class NotificationReplyReceiver : BroadcastReceiver() {
|
||||||
@@ -60,33 +61,38 @@ open class NotificationReplyReceiver : BroadcastReceiver() {
|
|||||||
replyScope.launch {
|
replyScope.launch {
|
||||||
try {
|
try {
|
||||||
withTimeout(5000L) {
|
withTimeout(5000L) {
|
||||||
val db =
|
withContext(Dispatchers.IO) {
|
||||||
com.m57.hermescontrol.data.local.HermesDatabase
|
val db =
|
||||||
.get(context)
|
com.m57.hermescontrol.data.local.HermesDatabase
|
||||||
val dao = db.chatMessageDao()
|
.get(context)
|
||||||
if (!dao.sessionExists(sessionId)) {
|
val dao = db.chatMessageDao()
|
||||||
android.util.Log.w("NotificationReply", "Ignoring reply for unknown session: $sessionId")
|
if (!dao.sessionExists(sessionId)) {
|
||||||
return@withTimeout
|
android.util.Log.w(
|
||||||
|
"NotificationReply",
|
||||||
|
"Ignoring reply for unknown session: $sessionId",
|
||||||
|
)
|
||||||
|
return@withContext
|
||||||
|
}
|
||||||
|
|
||||||
|
HermesWsClient.sendMessage(sessionId, replyText)
|
||||||
|
|
||||||
|
val entity =
|
||||||
|
com.m57.hermescontrol.data.local.ChatMessageEntity(
|
||||||
|
id =
|
||||||
|
java.util.UUID
|
||||||
|
.randomUUID()
|
||||||
|
.toString(),
|
||||||
|
sessionId = sessionId,
|
||||||
|
role = "USER",
|
||||||
|
content = replyText,
|
||||||
|
timestamp = System.currentTimeMillis(),
|
||||||
|
)
|
||||||
|
dao.upsert(entity)
|
||||||
|
|
||||||
|
val repliedNotification = buildReplyNotification(context)
|
||||||
|
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
|
manager.notify(ChatNotificationService.PENDING_NOTIFICATION_ID, repliedNotification)
|
||||||
}
|
}
|
||||||
|
|
||||||
HermesWsClient.sendMessage(sessionId, replyText)
|
|
||||||
|
|
||||||
val entity =
|
|
||||||
com.m57.hermescontrol.data.local.ChatMessageEntity(
|
|
||||||
id =
|
|
||||||
java.util.UUID
|
|
||||||
.randomUUID()
|
|
||||||
.toString(),
|
|
||||||
sessionId = sessionId,
|
|
||||||
role = "USER",
|
|
||||||
content = replyText,
|
|
||||||
timestamp = System.currentTimeMillis(),
|
|
||||||
)
|
|
||||||
dao.upsert(entity)
|
|
||||||
|
|
||||||
val repliedNotification = buildReplyNotification(context)
|
|
||||||
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
||||||
manager.notify(ChatNotificationService.PENDING_NOTIFICATION_ID, repliedNotification)
|
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
android.util.Log.e("NotificationReply", "Failed to process reply", e)
|
android.util.Log.e("NotificationReply", "Failed to process reply", e)
|
||||||
|
|||||||
Reference in new issue
Block a user