Skip to content

Commit

Permalink
- improve check logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobi823 committed Jan 28, 2025
1 parent 6b02a4a commit 5a15be8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class AppUpdater(context: Context, workerParams: WorkerParameters) : CoroutineWo

private suspend fun isUpdateCheckPossible(app: App): kotlin.Result<Boolean> {

if (isStopped) {
return failure(AppUpdaterNonRetryableException("WorkRequest is stopped."))
}
if (!FileDownloader.isUrlAvailable(app.findImpl().hostnameForInternetCheck)) {
return failure(AppUpdaterRetryableException("Simple network test was not successful. Retry later."))
}
Expand All @@ -147,9 +150,6 @@ class AppUpdater(context: Context, workerParams: WorkerParameters) : CoroutineWo
if (!BackgroundSettings.isUpdateCheckOnMeteredAllowed && isNetworkMetered(applicationContext)) {
return failure(AppUpdaterRetryableException("No unmetered network available for app download. Retry later."))
}
if (isStopped) {
return failure(AppUpdaterNonRetryableException("WorkRequest is stopped."))
}
if (!BackgroundSettings.isUpdateCheckEnabled) {
return failure(AppUpdaterNonRetryableException("Background update check is disabled."))
}
Expand Down Expand Up @@ -213,7 +213,6 @@ class AppUpdater(context: Context, workerParams: WorkerParameters) : CoroutineWo
onUpdate(update)
}
}

download.await()
}
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ import androidx.work.WorkRequest.Companion.MAX_BACKOFF_MILLIS
import androidx.work.WorkerParameters
import de.marmaro.krt.ffupdater.FFUpdater.Companion.LOG_TAG
import de.marmaro.krt.ffupdater.app.App
import de.marmaro.krt.ffupdater.device.DeviceAbiExtractor
import de.marmaro.krt.ffupdater.device.InstalledAppsCache
import de.marmaro.krt.ffupdater.notification.NotificationBuilder.showGeneralErrorNotification
import de.marmaro.krt.ffupdater.settings.BackgroundSettings
import de.marmaro.krt.ffupdater.settings.DataStoreHelper
import de.marmaro.krt.ffupdater.settings.ForegroundSettings
import de.marmaro.krt.ffupdater.utils.max
import java.time.Duration
import java.util.concurrent.TimeUnit.MINUTES
Expand Down Expand Up @@ -74,7 +72,8 @@ class BackgroundWork(context: Context, workerParams: WorkerParameters) : Corouti
return Result.failure()
}

val apps = findApps()
InstalledAppsCache.updateCache(applicationContext)
val apps = InstalledAppsCache.getAppsApplicableForBackgroundUpdate(applicationContext)
logInfo("Enqueuing work requests for: {$apps}")
val appWorkRequests = generateWorkRequestsForApps(apps)

Expand All @@ -87,16 +86,6 @@ class BackgroundWork(context: Context, workerParams: WorkerParameters) : Corouti
return Result.success()
}

@Suppress("ConvertCallChainIntoSequence")
private suspend fun findApps(): List<App> {
InstalledAppsCache.updateCache(applicationContext)
return InstalledAppsCache.getInstalledAppsWithCorrectFingerprint(applicationContext)
.filter { it !in BackgroundSettings.excludedAppsFromUpdateCheck }
.filter { it !in ForegroundSettings.hiddenApps }
.filter { DeviceAbiExtractor.supportsOneOf(it.findImpl().supportedAbis) }.map { it.findImpl() }
.filter { !it.wasInstalledByOtherApp(applicationContext) }.map { it.app }.toList()
}

private fun generateWorkRequestsForApps(apps: List<App>): List<OneTimeWorkRequest> {
val appWorkRequests = apps.sortedBy { it.installationChronology }.map { AppUpdater.createWorkRequest(it) }
val workFinishedListener = AppUpdaterSuccessListener.createWorkRequest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import androidx.annotation.Keep
import de.marmaro.krt.ffupdater.FFUpdater.Companion.LOG_TAG
import de.marmaro.krt.ffupdater.app.App
import de.marmaro.krt.ffupdater.app.entity.InstallationStatus
import de.marmaro.krt.ffupdater.settings.BackgroundSettings
import de.marmaro.krt.ffupdater.settings.ForegroundSettings
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
Expand All @@ -20,6 +22,14 @@ object InstalledAppsCache {
private val mutex = Mutex()
private var lastUpdate = 0L

suspend fun getAppsApplicableForBackgroundUpdate(context: Context): List<App> {
return getInstalledAppsWithCorrectFingerprint(context).asSequence()
.filter { it !in BackgroundSettings.excludedAppsFromUpdateCheck }
.filter { it !in ForegroundSettings.hiddenApps }
.filter { DeviceAbiExtractor.supportsOneOf(it.findImpl().supportedAbis) }.map { it.findImpl() }
.filter { !it.wasInstalledByOtherApp(context) }.map { it.app }.toList()
}

suspend fun getInstalledAppsWithCorrectFingerprint(context: Context): List<App> {
initializeCacheIfNecessary(context.applicationContext)
return installedCorrectFingerprint.toImmutableList()
Expand Down

0 comments on commit 5a15be8

Please sign in to comment.