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

Verified Commit fb2b11d7 authored by Saalim Quadri's avatar Saalim Quadri
Browse files

feat: Add support for architecture based updates in application

parent 79a456a2
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

package foundation.e.apps.data.cleanapk.repositories

import android.os.Build
import foundation.e.apps.data.application.ApplicationRepository
import foundation.e.apps.data.application.data.Application
import foundation.e.apps.data.application.data.Home
@@ -106,6 +107,7 @@ class CleanApkAppsRepository @Inject constructor(

    override suspend fun getDownloadInfo(idOrPackageName: String, versionCode: Any?): Response<Download> {
        val version = versionCode?.let { it as String }
        return cleanApkRetrofit.getDownloadInfo(idOrPackageName, version, null)
        val arch = Build.SUPPORTED_ABIS.firstOrNull()
        return cleanApkRetrofit.getDownloadInfo(idOrPackageName, version, arch)
    }
}
+34 −2
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
package foundation.e.apps.data.gitlab.models

import android.content.Context
import android.os.Build
import android.text.format.Formatter
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.squareup.moshi.Json
@@ -32,7 +33,8 @@ data class SystemAppInfo(
    @Json(name = "version_code") val versionCode: Long,
    @Json(name = "min_sdk") val minSdk: Int,
    @Json(name = "version_name") val versionName: String,
    @Json(name = "url") val downloadUrl: String,
    @Json(name = "url") val downloadUrl: String? = null,
    @Json(name = "urls") val archDownloadUrls: Map<String, String>? = null,
    @Json(name = "size") val size: Long?,
    @Json(name = "author_name") val authorName: String?,
    val priority: Boolean?,
@@ -44,6 +46,8 @@ private const val RANDOM_SIZE = 1L

fun SystemAppInfo.toApplication(context: Context): Application {
    val apkSize = size ?: RANDOM_SIZE
    val selectedUrl = selectUrlForArchitecture()

    return Application(
        _id = UUID.randomUUID().toString(),
        author = authorName ?: "eFoundation",
@@ -54,8 +58,36 @@ fun SystemAppInfo.toApplication(context: Context): Application {
        package_name = packageName,
        originalSize = apkSize,
        appSize = Formatter.formatFileSize(context, apkSize),
        url = downloadUrl,
        url = selectedUrl,
        isSystemApp = true,
        filterLevel = FilterLevel.NONE,
    )
}

/**
 * Select the appropriate download URL based on device architecture.
 *
 * This function prioritizes architecture-specific URLs from [archDownloadUrls] based on
 * the device's [Build.SUPPORTED_ABIS], falling back to the generic [downloadUrl] if needed.
 *
 * @return The selected download URL
 * @throws IllegalStateException if no suitable URL is found
 */
private fun SystemAppInfo.selectUrlForArchitecture(): String {
    if (archDownloadUrls.isNullOrEmpty()) {
        return downloadUrl ?: error("No download URL provided for $packageName")
    }

    val compatibleUrl = Build.SUPPORTED_ABIS
        .asSequence()
        .mapNotNull { abi -> archDownloadUrls[abi] }
        .firstOrNull { url -> url.isNotBlank() }

    return compatibleUrl
        ?: downloadUrl?.takeIf { it.isNotBlank() }
        ?: error(
            "No compatible URL found for $packageName. " +
                "Device supports: ${Build.SUPPORTED_ABIS.joinToString()}, " +
                "Available: ${archDownloadUrls.keys.joinToString()}"
        )
}