diff --git a/app/src/main/java/com/apkupdater/repository/ApkMirrorRepository.kt b/app/src/main/java/com/apkupdater/repository/ApkMirrorRepository.kt index 8504def6..fd965093 100644 --- a/app/src/main/java/com/apkupdater/repository/ApkMirrorRepository.kt +++ b/app/src/main/java/com/apkupdater/repository/ApkMirrorRepository.kt @@ -27,6 +27,7 @@ class ApkMirrorRepository( private val service: ApkMirrorService, private val prefs: Prefs ) { + private val arch = when { Build.SUPPORTED_ABIS.contains("x86") -> "x86" Build.SUPPORTED_ABIS.contains("x86_64") -> "x86" @@ -35,6 +36,8 @@ class ApkMirrorRepository( else -> "arm" } + private val api = Build.VERSION.SDK_INT + suspend fun updates(apps: List) = flow { apps.chunked(100) .map { appExists(it.getPackageNames()) } @@ -81,9 +84,11 @@ class ApkMirrorRepository( .filter { it.exists == true } .mapNotNull { data -> data.apks + .asSequence() .filter { filterSignature(it, apps.getSignature(data.pname))} .mapNotNull { filterArch(it) } .filter { it.versionCode > apps.getVersionCode(data.pname) } + .filter { filterMinApi(it) } .maxByOrNull { it.versionCode } ?.toAppUpdate(apps.getApp(data.pname)!!) } @@ -101,6 +106,13 @@ class ApkMirrorRepository( else -> null } + private fun filterMinApi(apk: AppExistsResponseApk) = runCatching { + when { + apk.minapi.toInt() > api -> false + else -> true + } + }.getOrDefault(true) + private fun buildIgnoreList() = mutableListOf().apply { if (prefs.ignoreAlpha.get()) add("alpha") if (prefs.ignoreBeta.get()) add("beta") diff --git a/app/src/main/java/com/apkupdater/repository/GitHubRepository.kt b/app/src/main/java/com/apkupdater/repository/GitHubRepository.kt index 500c3dd5..d07544d3 100644 --- a/app/src/main/java/com/apkupdater/repository/GitHubRepository.kt +++ b/app/src/main/java/com/apkupdater/repository/GitHubRepository.kt @@ -7,6 +7,7 @@ import com.apkupdater.data.ui.GitHubSource import com.apkupdater.service.GitHubService import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.flow +import java.util.Scanner class GitHubRepository( private val service: GitHubService @@ -14,19 +15,31 @@ class GitHubRepository( suspend fun updates() = flow { val releases = service.getReleases() + val versions = getVersions(releases[0].name) + if (releases[0].name != BuildConfig.VERSION_NAME) { emit(listOf(AppUpdate( name = "ApkUpdater", - packageName = "com.apkupdater3", - version = releases[0].name, - versionCode = 0L, + packageName = BuildConfig.APPLICATION_ID, + version = versions.first, + versionCode = versions.second, source = GitHubSource, link = releases[0].assets[0].browser_download_url ))) + } else { + // We need to emit empty so it can be combined later + emit(listOf()) } }.catch { emit(emptyList()) Log.e("GitHubRepository", "Error fetching releases.", it) } + private fun getVersions(name: String) = runCatching { + val scanner = Scanner(name) + val version = scanner.next() + val versionCode = scanner.next().trim('(', ')').toLong() + Pair(version, versionCode) + }.getOrDefault(Pair(name, 0L)) + } diff --git a/app/src/main/java/com/apkupdater/repository/UpdatesRepository.kt b/app/src/main/java/com/apkupdater/repository/UpdatesRepository.kt index 2281d7fb..599bf057 100644 --- a/app/src/main/java/com/apkupdater/repository/UpdatesRepository.kt +++ b/app/src/main/java/com/apkupdater/repository/UpdatesRepository.kt @@ -16,9 +16,8 @@ class UpdatesRepository( appsRepository.getApps().collect { result -> result.onSuccess { apps -> listOf(apkMirrorRepository.updates(apps), gitHubRepository.updates()) - .combine { updates -> - emit(updates.flatMap { it }) - }.collect() + .combine { updates -> emit(updates.flatMap { it }) } + .collect() }.onFailure { Log.e("UpdatesRepository", "Error getting apps", it) }