Skip to content

Commit

Permalink
[messages] add log truncation worker
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Apr 8, 2024
1 parent 097b835 commit 1e0673e
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/me/capcom/smsgateway/data/dao/MessageDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ interface MessageDao {

@Query("UPDATE messagerecipient SET state = :state, error = :error WHERE messageId = :id")
fun updateRecipientsState(id: String, state: Message.State, error: String?)

@Query("DELETE FROM message WHERE createdAt < :until AND state <> 'Pending'")
suspend fun truncateLog(until: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import me.capcom.smsgateway.modules.encryption.EncryptionService
import me.capcom.smsgateway.modules.events.EventBus
import me.capcom.smsgateway.modules.messages.data.SendRequest
import me.capcom.smsgateway.modules.messages.events.MessageStateChangedEvent
import me.capcom.smsgateway.modules.messages.workers.LogTruncateWorker
import me.capcom.smsgateway.modules.messages.workers.SendMessagesWorker
import me.capcom.smsgateway.receivers.EventsReceiver
import java.util.Date
Expand All @@ -40,9 +41,11 @@ class MessagesService(

fun start() {
SendMessagesWorker.start(context)
LogTruncateWorker.start(context)
}

fun stop() {
LogTruncateWorker.stop(context)
SendMessagesWorker.stop(context)
}

Expand Down Expand Up @@ -117,6 +120,12 @@ class MessagesService(
updateState(id, phone, state, error)
}

suspend fun truncateLog() {
val lifetime = settings.logLifetimeDays ?: return

dao.truncateLog(System.currentTimeMillis() - lifetime * 86400000L)
}

internal suspend fun sendPendingMessages(): Boolean {
val messages = dao.selectPending()
if (messages.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ class MessagesSettings(
val limitValue: Int
get() = storage.get(LIMIT_VALUE) ?: 0

val logLifetimeDays: Int?
get() = storage.get<Int?>(LOG_LIFETIME_DAYS)?.takeIf { it > 0 }

companion object {
private const val SECONDS_BETWEEN_MESSAGES = "SECONDS_BETWEEN_MESSAGES"

private const val LIMIT_PERIOD = "limit_period"
private const val LIMIT_VALUE = "limit_value"

private const val LOG_LIFETIME_DAYS = "log_lifetime_days"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package me.capcom.smsgateway.modules.messages.workers

import android.content.Context
import androidx.work.Constraints
import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import me.capcom.smsgateway.modules.messages.MessagesService
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.util.concurrent.TimeUnit

class LogTruncateWorker(appContext: Context, params: WorkerParameters) : CoroutineWorker(
appContext,
params
), KoinComponent {
private val messagesSvc: MessagesService by inject()

override suspend fun doWork(): Result = try {
messagesSvc.truncateLog()
Result.success()
} catch (e: Throwable) {
e.printStackTrace()
Result.retry()
}

companion object {
private const val NAME = "LogTruncateWorker"

fun start(context: Context) {
val work = PeriodicWorkRequestBuilder<LogTruncateWorker>(1L, TimeUnit.DAYS)
.setConstraints(
Constraints.Builder()
.setRequiresBatteryNotLow(true)
.build()
)
.build()

WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(
NAME,
ExistingPeriodicWorkPolicy.KEEP,
work
)
}

fun stop(context: Context) {
WorkManager.getInstance(context)
.cancelUniqueWork(NAME)
}
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/me/capcom/smsgateway/ui/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}

if (preference.key == "messages.log_lifetime_days") {
(preference as EditTextPreference).setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER
it.setSelectAllOnFocus(true)
it.selectAll()
}
}

super.onDisplayPreferenceDialog(preference)
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/ic_log_lifetime.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<vector android:height="24dp" android:viewportHeight="512"
android:viewportWidth="512" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@color/white" android:pathData="M75,75L41,41C25.9,25.9 0,36.6 0,57.9L0,168c0,13.3 10.7,24 24,24L134.1,192c21.4,0 32.1,-25.9 17,-41l-30.8,-30.8C155,85.5 203,64 256,64c106,0 192,86 192,192s-86,192 -192,192c-40.8,0 -78.6,-12.7 -109.7,-34.4c-14.5,-10.1 -34.4,-6.6 -44.6,7.9s-6.6,34.4 7.9,44.6C151.2,495 201.7,512 256,512c141.4,0 256,-114.6 256,-256S397.4,0 256,0C185.3,0 121.3,28.7 75,75zM256,128c-13.3,0 -24,10.7 -24,24L232,256c0,6.4 2.5,12.5 7,17l72,72c9.4,9.4 24.6,9.4 33.9,0s9.4,-24.6 0,-33.9l-65,-65L279.9,152c0,-13.3 -10.7,-24 -24,-24z"/>
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<string name="private_token">Private Token</string>
<string name="ignored_for_public_server">Ignored for public server</string>
<string name="address_is">Address is %1$s</string>
<string name="log_lifetime_days">Log lifetime, days</string>
<string name="limits">Limits</string>
<string name="period">Period</string>
<string name="messages_count">Messages count</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
</PreferenceCategory>

<PreferenceCategory app:title="@string/messages_header">
<EditTextPreference
app:icon="@drawable/ic_log_lifetime"
app:key="messages.log_lifetime_days"
app:useSimpleSummaryProvider="true"
app:title="@string/log_lifetime_days" />

<SeekBarPreference
android:max="600"
Expand Down

0 comments on commit 1e0673e

Please sign in to comment.