Skip to content

Commit

Permalink
Add API filter to ApkMirror
Browse files Browse the repository at this point in the history
  • Loading branch information
rumboalla committed Jul 17, 2023
1 parent 7589e47 commit a4f09fc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/com/apkupdater/repository/ApkMirrorRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -35,6 +36,8 @@ class ApkMirrorRepository(
else -> "arm"
}

private val api = Build.VERSION.SDK_INT

suspend fun updates(apps: List<AppInstalled>) = flow {
apps.chunked(100)
.map { appExists(it.getPackageNames()) }
Expand Down Expand Up @@ -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)!!)
}
Expand All @@ -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<String>().apply {
if (prefs.ignoreAlpha.get()) add("alpha")
if (prefs.ignoreBeta.get()) add("beta")
Expand Down
19 changes: 16 additions & 3 deletions app/src/main/java/com/apkupdater/repository/GitHubRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,39 @@ 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
) {

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))

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit a4f09fc

Please sign in to comment.