Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit f9cc9249 authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

EN: Run cleanup in IO thread, improve performance

parent a016feba
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
@@ -8,6 +8,9 @@ package org.microg.gms.nearby.exposurenotification
import android.content.Context
import android.content.Intent
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.microg.gms.common.ForegroundServiceContext

class CleanupService : LifecycleService() {
@@ -15,12 +18,18 @@ class CleanupService : LifecycleService() {
        ForegroundServiceContext.completeForegroundService(this, intent, TAG)
        super.onStartCommand(intent, flags, startId)
        if (isNeeded(this)) {
            lifecycleScope.launchWhenStarted {
                launch(Dispatchers.IO) {
                    ExposureDatabase.with(this@CleanupService) {
                        it.dailyCleanup()
                    }
            ExposurePreferences(this).lastCleanup = System.currentTimeMillis()
                    ExposurePreferences(this@CleanupService).lastCleanup = System.currentTimeMillis()
                }
                stopSelf()
            }
        } else {
            stopSelf()
        }
        return START_NOT_STICKY
    }

+10 −3
Original line number Diff line number Diff line
@@ -62,6 +62,9 @@ class ExposureDatabase private constructor(private val context: Context) : SQLit
        if (oldVersion < 3) {
            db.execSQL("CREATE TABLE $TABLE_APP_PERMS(package TEXT NOT NULL, sig TEXT NOT NULL, perm TEXT NOT NULL, timestamp INTEGER NOT NULL);")
        }
        if (oldVersion < 4) {
            db.execSQL("CREATE INDEX IF NOT EXISTS index_${TABLE_DIAGNOSIS}_tcid ON $TABLE_DIAGNOSIS(tcid);")
        }
    }

    fun SQLiteDatabase.delete(table: String, whereClause: String, args: LongArray): Int =
@@ -75,11 +78,15 @@ class ExposureDatabase private constructor(private val context: Context) : SQLit
        try {
            val rollingStartTime = currentRollingStartNumber * ROLLING_WINDOW_LENGTH * 1000 - TimeUnit.DAYS.toMillis(KEEP_DAYS.toLong())
            val advertisements = delete(TABLE_ADVERTISEMENTS, "timestamp < ?", longArrayOf(rollingStartTime))
            Log.d(TAG, "Deleted on daily cleanup: $advertisements adv")
            val appLogEntries = delete(TABLE_APP_LOG, "timestamp < ?", longArrayOf(rollingStartTime))
            Log.d(TAG, "Deleted on daily cleanup: $appLogEntries applogs")
            val temporaryExposureKeys = delete(TABLE_TEK, "(rollingStartNumber + rollingPeriod) < ?", longArrayOf(rollingStartTime / ROLLING_WINDOW_LENGTH_MS))
            val checkedTemporaryExposureKeys = delete(TABLE_TEK_CHECK, "(rollingStartNumber + rollingPeriod) < ?", longArrayOf(rollingStartTime / ROLLING_WINDOW_LENGTH_MS))
            Log.d(TAG, "Deleted on daily cleanup: $temporaryExposureKeys teks")
            val checkedTemporaryExposureKeys = delete(TABLE_TEK_CHECK, "rollingStartNumber < ?", longArrayOf(rollingStartTime / ROLLING_WINDOW_LENGTH_MS - ROLLING_PERIOD))
            Log.d(TAG, "Deleted on daily cleanup: $checkedTemporaryExposureKeys cteks")
            val appPerms = delete(TABLE_APP_PERMS, "timestamp < ?", longArrayOf(System.currentTimeMillis() - CONFIRM_PERMISSION_VALIDITY))
            Log.d(TAG, "Deleted on daily cleanup: $advertisements adv, $appLogEntries applogs, $temporaryExposureKeys teks, $checkedTemporaryExposureKeys cteks, $appPerms perms")
            Log.d(TAG, "Deleted on daily cleanup: $appPerms perms")
            setTransactionSuccessful()
        } finally {
            endTransaction()
@@ -553,7 +560,7 @@ class ExposureDatabase private constructor(private val context: Context) : SQLit

    companion object {
        private const val DB_NAME = "exposure.db"
        private const val DB_VERSION = 3
        private const val DB_VERSION = 4
        private const val TABLE_ADVERTISEMENTS = "advertisements"
        private const val TABLE_APP_LOG = "app_log"
        private const val TABLE_TEK = "tek"