mirror of
https://github.com/greenart7c3/Amber.git
synced 2026-10-05 19:08:23 +00:00
Merge pull request #342 from greenart7c3/claude/offline-without-service-worker-9B8wk
Add application expiration cleanup and offline flavor check
This commit is contained in:
@@ -227,8 +227,10 @@ class Amber :
|
||||
|
||||
instance = this
|
||||
|
||||
stats.createNotificationChannel()
|
||||
TorManager.init(this)
|
||||
if (!BuildFlavorChecker.isOfflineFlavor()) {
|
||||
stats.createNotificationChannel()
|
||||
TorManager.init(this)
|
||||
}
|
||||
|
||||
isStartingAppState.value = true
|
||||
isStartingApp.value = true
|
||||
@@ -339,6 +341,7 @@ class Amber :
|
||||
}
|
||||
|
||||
fun startService() {
|
||||
if (BuildFlavorChecker.isOfflineFlavor()) return
|
||||
try {
|
||||
Log.d(TAG, "Starting ConnectivityService")
|
||||
val operation = PendingIntent.getForegroundService(
|
||||
@@ -370,7 +373,9 @@ class Amber :
|
||||
client.connect()
|
||||
}
|
||||
client.reconnect(wasActive)
|
||||
stats.updateNotification()
|
||||
if (!BuildFlavorChecker.isOfflineFlavor()) {
|
||||
stats.updateNotification()
|
||||
}
|
||||
}
|
||||
|
||||
fun getDatabase(npub: String): AppDatabase {
|
||||
|
||||
@@ -15,6 +15,7 @@ import androidx.core.app.NotificationChannelGroupCompat
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import com.greenart7c3.nostrsigner.Amber
|
||||
import com.greenart7c3.nostrsigner.BuildFlavorChecker
|
||||
import com.greenart7c3.nostrsigner.MainActivity
|
||||
import com.greenart7c3.nostrsigner.R
|
||||
import com.greenart7c3.nostrsigner.service.KillSwitchReceiver
|
||||
@@ -215,6 +216,7 @@ class AmberRelayStats(
|
||||
}
|
||||
|
||||
fun updateNotification() {
|
||||
if (BuildFlavorChecker.isOfflineFlavor()) return
|
||||
val notificationManager = NotificationManagerCompat.from(appContext)
|
||||
if (ActivityCompat.checkSelfPermission(appContext, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
|
||||
createNotification(
|
||||
|
||||
@@ -6,6 +6,7 @@ import androidx.work.CoroutineWorker
|
||||
import androidx.work.WorkerParameters
|
||||
import com.greenart7c3.nostrsigner.Amber
|
||||
import com.greenart7c3.nostrsigner.LocalPreferences
|
||||
import com.greenart7c3.nostrsigner.database.LogEntity
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils.ONE_WEEK
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
@@ -15,8 +16,10 @@ class ClearLogsWorker(appContext: Context, workerParams: WorkerParameters) : Cor
|
||||
override suspend fun doWork(): Result {
|
||||
LocalPreferences.allSavedAccounts(Amber.instance).forEach {
|
||||
try {
|
||||
val oneWeek = System.currentTimeMillis() - (ONE_WEEK * 1000L)
|
||||
val now = System.currentTimeMillis()
|
||||
val oneWeek = now - (ONE_WEEK * 1000L)
|
||||
val oneWeekAgo = TimeUtils.oneWeekAgo()
|
||||
|
||||
val historyDatabase = Amber.instance.getHistoryDatabase(it.npub)
|
||||
val deletedHistory = historyDatabase.dao().deleteOldHistory(oneWeekAgo)
|
||||
if (deletedHistory > 0) {
|
||||
@@ -28,9 +31,24 @@ class ClearLogsWorker(appContext: Context, workerParams: WorkerParameters) : Cor
|
||||
if (deletedLogs > 0) {
|
||||
Log.d(Amber.TAG, "Deleted $deletedLogs old log entries")
|
||||
}
|
||||
|
||||
val database = Amber.instance.getDatabase(it.npub)
|
||||
database.dao().updateExpiredPermissions(TimeUtils.now())
|
||||
val deleted = database.dao().deleteOldApplications(now / 1000)
|
||||
if (deleted > 0) {
|
||||
logDatabase.dao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = "",
|
||||
type = "deleteApplications",
|
||||
message = "Deleted $deleted expired applications",
|
||||
time = now,
|
||||
),
|
||||
)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
Log.e(Amber.TAG, "Error deleting old log entries", e)
|
||||
Log.e(Amber.TAG, "Error in ClearLogsWorker", e)
|
||||
}
|
||||
}
|
||||
return Result.success()
|
||||
|
||||
@@ -12,9 +12,6 @@ import android.util.Log
|
||||
import androidx.core.app.ServiceCompat
|
||||
import com.greenart7c3.nostrsigner.Amber
|
||||
import com.greenart7c3.nostrsigner.BuildFlavorChecker
|
||||
import com.greenart7c3.nostrsigner.LocalPreferences
|
||||
import com.greenart7c3.nostrsigner.database.LogEntity
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||
import java.util.Timer
|
||||
import java.util.TimerTask
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
@@ -117,25 +114,6 @@ class ConnectivityService : Service() {
|
||||
timer.schedule(
|
||||
object : TimerTask() {
|
||||
override fun run() {
|
||||
scope.launch {
|
||||
LocalPreferences.allSavedAccounts(Amber.instance).forEach { accountInfo ->
|
||||
val now = System.currentTimeMillis() / 1000
|
||||
Amber.instance.getDatabase(accountInfo.npub).dao().updateExpiredPermissions(TimeUtils.now())
|
||||
val deleted = Amber.instance.getDatabase(accountInfo.npub).dao().deleteOldApplications(now)
|
||||
if (deleted > 0) {
|
||||
Amber.instance.getLogDatabase(accountInfo.npub).dao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = "",
|
||||
type = "deleteApplications",
|
||||
message = "Deleted $deleted expired applications",
|
||||
time = System.currentTimeMillis(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (BuildFlavorChecker.isOfflineFlavor()) {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user