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

Verified Commit 834224ba authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

chore: remove update timing traces

Baseline timing logs are no longer needed.
parent 29313bae
Loading
Loading
Loading
Loading
Loading
+0 −76
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@ import foundation.e.apps.domain.model.install.Status
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import timber.log.Timber
import java.util.Locale
import javax.inject.Inject
import javax.inject.Singleton

@@ -63,13 +62,6 @@ class SystemAppsUpdatesRepository @Inject constructor(

    private val systemAppProjectList = mutableListOf<SystemAppProject>()

    private companion object {
        private const val NANOS_IN_MILLI = 1_000_000L
        private const val MILLIS_IN_SECOND = 1000L
        private const val SECONDS_IN_MINUTE = 60L
        private const val MINUTES_IN_HOUR = 60L
    }

    private fun getUpdatableSystemApps(): List<String> {
        return systemAppProjectList.map { it.packageName }
    }
@@ -170,29 +162,13 @@ class SystemAppsUpdatesRepository @Inject constructor(
        sdkLevel: Int,
        device: String,
    ): Application? {
        val appStartMs = nowMs()
        val systemAppProject = systemAppProjectList.find { it.packageName == packageName }
        val releaseStartMs = nowMs()
        val detailsUrl = systemAppProject?.let {
            getReleaseDetailsUrl(it, releaseType)
        }
        Timber.tag("FAHIM").i(
            "System updates release lookup, package=%s, duration=%s, urlFound=%s",
            packageName,
            formatDuration(nowMs() - releaseStartMs),
            detailsUrl != null
        )

        val infoStartMs = nowMs()
        val systemAppInfo = detailsUrl?.let {
            getSystemAppInfo(packageName, it)
        }
        Timber.tag("FAHIM").i(
            "System updates app info fetch, package=%s, duration=%s, hasInfo=%s",
            packageName,
            formatDuration(nowMs() - infoStartMs),
            systemAppInfo != null
        )
        val isBlocked = systemAppInfo?.let {
            isSystemAppBlocked(it, sdkLevel, device)
        } == true
@@ -206,14 +182,6 @@ class SystemAppsUpdatesRepository @Inject constructor(
            null
        }

        if (application != null) {
            Timber.tag("FAHIM").i(
                "System updates application built, package=%s, duration=%s",
                packageName,
                formatDuration(nowMs() - appStartMs)
            )
        }

        return application
    }

@@ -275,16 +243,11 @@ class SystemAppsUpdatesRepository @Inject constructor(

    suspend fun getSystemUpdates(): List<Application> = coroutineScope {
        val updateList = mutableListOf<Application>()
        val overallStartMs = nowMs()
        val releaseType = getSystemReleaseType()
        val sdkLevel = getSdkLevel()
        val device = getDevice()

        val updatableApps = getUpdatableSystemApps()
        Timber.tag("FAHIM").i(
            "System updates start, totalApps=%d",
            updatableApps.size
        )

        val fetchTasks = updatableApps.map { packageName ->
            async {
@@ -296,12 +259,6 @@ class SystemAppsUpdatesRepository @Inject constructor(
            deferred.await()?.let { updateList.add(it) }
        }

        Timber.tag("FAHIM").i(
            "System updates done, updatable=%d, duration=%s",
            updateList.size,
            formatDuration(nowMs() - overallStartMs)
        )

        return@coroutineScope updateList
    }

@@ -311,15 +268,7 @@ class SystemAppsUpdatesRepository @Inject constructor(
        sdkLevel: Int,
        device: String,
    ): Application? {
        val perAppStartMs = nowMs()
        val installed = appLoungePackageManager.isInstalled(packageName)
        if (!installed) {
            // Don't install for system apps which are removed (by root or otherwise)
            Timber.tag("FAHIM").i(
                "System updates skip (not installed), package=%s",
                packageName
            )
        }

        val result = if (installed) {
            handleNetworkResult {
@@ -335,12 +284,6 @@ class SystemAppsUpdatesRepository @Inject constructor(
        }

        if (result != null) {
            Timber.tag("FAHIM").i(
                "System updates fetch done, package=%s, duration=%s, success=%s",
                packageName,
                formatDuration(nowMs() - perAppStartMs),
                result.isSuccess()
            )
            if (!result.isSuccess()) {
                Timber.e("Failed to get system app info for $packageName - ${result.message}")
            }
@@ -359,25 +302,6 @@ class SystemAppsUpdatesRepository @Inject constructor(
        }
        return updateApp
    }

    private fun nowMs(): Long {
        return System.nanoTime() / NANOS_IN_MILLI
    }

    private fun formatDuration(durationMs: Long): String {
        val totalSeconds = durationMs / MILLIS_IN_SECOND
        val milliseconds = durationMs % MILLIS_IN_SECOND
        val seconds = totalSeconds % SECONDS_IN_MINUTE
        val totalMinutes = totalSeconds / SECONDS_IN_MINUTE
        val minutes = totalMinutes % MINUTES_IN_HOUR
        val hours = totalMinutes / MINUTES_IN_HOUR

        return if (hours > 0) {
            String.format(Locale.US, "%d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds)
        } else {
            String.format(Locale.US, "%02d:%02d.%03d", minutes, seconds, milliseconds)
        }
    }
}

private class UnsupportedAndroidApiException(message: String) : RuntimeException(message)
+24 −250
Original line number Diff line number Diff line
@@ -38,8 +38,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.util.Locale
import javax.inject.Inject

@Suppress("LongParameterList")
@@ -57,11 +55,6 @@ class UpdatesManagerImpl @Inject constructor(
        const val PACKAGE_NAME_F_DROID = "org.fdroid.fdroid"
        const val PACKAGE_NAME_F_DROID_PRIVILEGED = "org.fdroid.fdroid.privileged"
        const val PACKAGE_NAME_ANDROID_VENDING = "com.android.vending"

        private const val NANOS_IN_MILLI = 1_000_000L
        private const val MILLIS_IN_SECOND = 1000L
        private const val SECONDS_IN_MINUTE = 60L
        private const val MINUTES_IN_HOUR = 60L
    }

    private val userApplications: List<ApplicationInfo>
@@ -69,21 +62,13 @@ class UpdatesManagerImpl @Inject constructor(

    @Suppress("LongMethod")
    suspend fun getUpdates(): Pair<List<Application>, ResultStatus> = coroutineScope {
        val overallStartMs = nowMs()

        val openSourceInstalledApps = logInstalledApps("open-source") {
            getOpenSourceInstalledApps()
        }.toMutableList()

        val gPlayInstalledApps = logInstalledApps("Play Store") {
            getGPlayInstalledApps()
        }.toMutableList()
        val openSourceInstalledApps = getOpenSourceInstalledApps().toMutableList()
        val gPlayInstalledApps = getGPlayInstalledApps().toMutableList()

        if (appPreferencesRepository.shouldUpdateAppsFromOtherStores()) {
            withContext(Dispatchers.IO) {
                val otherStoresInstalledApps = logInstalledApps("other-store") {
                    getAppsFromOtherStores(openSourceInstalledApps, gPlayInstalledApps)
                }.toMutableList()
                val otherStoresInstalledApps =
                    getAppsFromOtherStores(openSourceInstalledApps, gPlayInstalledApps).toMutableList()

                val cleanApkAppsByPackage = getCleanApkDetailsByPackage(otherStoresInstalledApps)

@@ -108,7 +93,6 @@ class UpdatesManagerImpl @Inject constructor(

        val openSourceDeferred = if (openSourceInstalledApps.isNotEmpty()) {
            async {
                logUpdatesFetch("open-source", openSourceInstalledApps.size) {
                getUpdatesFromApi {
                    applicationRepository.getApplicationDetails(
                        openSourceInstalledApps,
@@ -116,7 +100,6 @@ class UpdatesManagerImpl @Inject constructor(
                    )
                }
            }
            }
        } else {
            null
        }
@@ -125,21 +108,17 @@ class UpdatesManagerImpl @Inject constructor(
            gPlayInstalledApps.isNotEmpty()
        ) {
            async {
                logUpdatesFetch("Play Store", gPlayInstalledApps.size) {
                getUpdatesFromApi {
                    getGPlayUpdates(gPlayInstalledApps)
                }
            }
            }
        } else {
            null
        }

        val systemAppsDeferred = async {
            logSystemAppsFetch("system apps") {
            getSystemAppUpdates()
        }
        }

        val openSourceResult = openSourceDeferred?.await()
        val gplayResult = gplayDeferred?.await()
@@ -157,26 +136,15 @@ class UpdatesManagerImpl @Inject constructor(
            status = gplayResult.second
        }

        Timber.tag("FAHIM").i(
            "Updates total fetch done, totalApps=%d, duration=%s",
            updateList.size,
            formatDuration(nowMs() - overallStartMs)
        )

        return@coroutineScope Pair(updateList, status)
    }

    suspend fun getUpdatesOSS(): Pair<List<Application>, ResultStatus> = coroutineScope {
        val overallStartMs = nowMs()

        val openSourceInstalledApps = logInstalledApps("OSS open-source") {
            getOpenSourceInstalledApps()
        }.toMutableList()
        val openSourceInstalledApps = getOpenSourceInstalledApps().toMutableList()

        if (appPreferencesRepository.shouldUpdateAppsFromOtherStores()) {
            val otherStoresInstalledApps = logInstalledApps("OSS other-store") {
                getAppsFromOtherStores(openSourceInstalledApps, emptyList())
            }.toMutableList()
            val otherStoresInstalledApps =
                getAppsFromOtherStores(openSourceInstalledApps, emptyList()).toMutableList()

            val cleanApkAppsByPackage = getCleanApkDetailsByPackage(otherStoresInstalledApps)

@@ -193,7 +161,6 @@ class UpdatesManagerImpl @Inject constructor(

        val openSourceDeferred = if (openSourceInstalledApps.isNotEmpty()) {
            async {
                logUpdatesFetch("OSS open-source", openSourceInstalledApps.size) {
                getUpdatesFromApi {
                    applicationRepository.getApplicationDetails(
                        openSourceInstalledApps,
@@ -201,16 +168,13 @@ class UpdatesManagerImpl @Inject constructor(
                    )
                }
            }
            }
        } else {
            null
        }

        val systemAppsDeferred = async {
            logSystemAppsFetch("OSS system apps") {
            getSystemAppUpdates()
        }
        }

        val openSourceResult = openSourceDeferred?.await()
        val systemApps = systemAppsDeferred.await()
@@ -223,12 +187,6 @@ class UpdatesManagerImpl @Inject constructor(

        val status = openSourceResult?.second ?: ResultStatus.OK

        Timber.tag("FAHIM").i(
            "Updates OSS total fetch done, totalApps=%d, duration=%s",
            updateList.size,
            formatDuration(nowMs() - overallStartMs)
        )

        return@coroutineScope Pair(updateList, status)
    }

@@ -359,39 +317,19 @@ class UpdatesManagerImpl @Inject constructor(
        installedPackageNames: List<String>,
        cleanApkAppsByPackage: Map<String, Application?>,
    ): Map<String, String> = coroutineScope {
        val startMs = nowMs()
        Timber.tag("FAHIM").i(
            "Updates signature scan start, packages=%d",
            installedPackageNames.size
        )
        val appsAndSignatures = hashMapOf<String, String>()
        val candidates = installedPackageNames.map { packageName ->
            async {
                val perPackageStartMs = nowMs()
                val signature = updateAppsWithPGPSignature(packageName, cleanApkAppsByPackage)
                val duration = formatDuration(nowMs() - perPackageStartMs)
                Triple(packageName, signature, duration)
                packageName to updateAppsWithPGPSignature(packageName, cleanApkAppsByPackage)
            }
        }

        candidates.forEach { deferred ->
            val (packageName, signature, duration) = deferred.await()
            val added = signature != null
            val (packageName, signature) = deferred.await()
            if (signature != null) {
                appsAndSignatures[packageName] = signature
            }
            Timber.tag("FAHIM").i(
                "Updates signature candidate done, package=%s, added=%s, duration=%s",
                packageName,
                added,
                duration
            )
        }
        Timber.tag("FAHIM").i(
            "Updates signature scan done, candidates=%d, duration=%s",
            appsAndSignatures.size,
            formatDuration(nowMs() - startMs)
        )
        return@coroutineScope appsAndSignatures
    }

@@ -399,70 +337,30 @@ class UpdatesManagerImpl @Inject constructor(
        packageName: String,
        cleanApkAppsByPackage: Map<String, Application?>,
    ): String? {
        val appDetailsStartMs = nowMs()
        val cleanApkApplication = cleanApkAppsByPackage[packageName]
        val apps = when {
            cleanApkApplication != null -> listOf(cleanApkApplication)
            cleanApkAppsByPackage.containsKey(packageName) -> emptyList()
            else -> applicationRepository.getApplicationDetails(listOf(packageName), Source.OPEN_SOURCE).first
        }
        Timber.tag("FAHIM").i(
            "Updates signature app details done, package=%s, duration=%s, resultCount=%d",
            packageName,
            formatDuration(nowMs() - appDetailsStartMs),
            apps.size
        )
        val app = apps.firstOrNull()?.takeIf { it.package_name.isNotBlank() }
        val signature = if (app == null) {
            null
        } else {
            val signatureStartMs = nowMs()
            val pgpSignature = getPgpSignature(app)
            Timber.tag("FAHIM").i(
                "Updates signature fetch done, package=%s, duration=%s, blank=%s",
                packageName,
                formatDuration(nowMs() - signatureStartMs),
                pgpSignature.isBlank()
            )
            pgpSignature
        }
        return signature
    }

    private suspend fun getPgpSignature(cleanApkApplication: Application): String {
        val signatureStartMs = nowMs()
        val signatureVersionStartMs = nowMs()
        val installedVersionSignature = calculateSignatureVersion(cleanApkApplication)
        Timber.tag("FAHIM").i(
            "Updates signature version done, package=%s, duration=%s, version=%s",
            cleanApkApplication.package_name,
            formatDuration(nowMs() - signatureVersionStartMs),
            installedVersionSignature
        )

        val downloadInfoStartMs = nowMs()
        val downloadInfoResult = handleNetworkResult {
            applicationRepository
                .getOSSDownloadInfo(cleanApkApplication._id, installedVersionSignature)
                .body()?.download_data
        }
        Timber.tag("FAHIM").i(
            "Updates signature download info done, package=%s, duration=%s, hasData=%s",
            cleanApkApplication.package_name,
            formatDuration(nowMs() - downloadInfoStartMs),
            downloadInfoResult.data != null
        )

        val pgpSignature = downloadInfoResult.data?.signature ?: ""

        Timber.tag("FAHIM").i(
            "Updates PGP signature done, package=%s, duration=%s, blank=%s",
            cleanApkApplication.package_name,
            formatDuration(nowMs() - signatureStartMs),
            pgpSignature.isBlank()
        )

        return pgpSignature
        return downloadInfoResult.data?.signature ?: ""
    }

    /**
@@ -476,7 +374,6 @@ class UpdatesManagerImpl @Inject constructor(
        installedPackageNames: List<String>,
        cleanApkAppsByPackage: Map<String, Application?>,
    ): List<String> = coroutineScope {
        val signatureStartMs = nowMs()
        val fDroidAppsAndSignatures = getFDroidAppsAndSignatures(
            installedPackageNames,
            cleanApkAppsByPackage,
@@ -485,10 +382,6 @@ class UpdatesManagerImpl @Inject constructor(
        val fDroidUpdatablePackageNames = mutableListOf<String>()
        val verificationTasks = fDroidAppsAndSignatures.mapNotNull { (packageName, signature) ->
            if (signature.isEmpty()) {
                Timber.tag("FAHIM").i(
                    "Updates signature skipped, package=%s, reason=empty_signature",
                    packageName
                )
                return@mapNotNull null
            }

@@ -504,14 +397,6 @@ class UpdatesManagerImpl @Inject constructor(
            }
        }

        Timber.tag("FAHIM").i(
            "Updates signature matching done, input=%d, candidates=%d, matched=%d, duration=%s",
            installedPackageNames.size,
            fDroidAppsAndSignatures.size,
            fDroidUpdatablePackageNames.size,
            formatDuration(nowMs() - signatureStartMs)
        )

        return@coroutineScope fDroidUpdatablePackageNames
    }

@@ -519,20 +404,11 @@ class UpdatesManagerImpl @Inject constructor(
        packageName: String,
        signature: String,
    ): Pair<String, Boolean> {
        val baseApkStartMs = nowMs()
        val baseApkPath = appLoungePackageManager.getBaseApkPath(packageName)
        val baseApkDuration = formatDuration(nowMs() - baseApkStartMs)
        Timber.tag("FAHIM").i(
            "Updates base APK path fetched, package=%s, duration=%s, hasPath=%s",
            packageName,
            baseApkDuration,
            baseApkPath.isNotEmpty()
        )
        if (baseApkPath.isEmpty()) {
            return Pair(packageName, false)
        }

        val verifyStartMs = nowMs()
        val verified = withContext(Dispatchers.IO) {
            ApkSignatureManager.verifyFdroidSignature(
                context,
@@ -541,12 +417,6 @@ class UpdatesManagerImpl @Inject constructor(
                packageName,
            )
        }
        Timber.tag("FAHIM").i(
            "Updates signature verified, package=%s, duration=%s, matched=%s",
            packageName,
            formatDuration(nowMs() - verifyStartMs),
            verified
        )
        return Pair(packageName, verified)
    }

@@ -589,22 +459,9 @@ class UpdatesManagerImpl @Inject constructor(
        val packageName = latestCleanapkApp.package_name
        val latestSignatureVersion = latestCleanapkApp.latest_downloaded_version

        Timber.tag("FAHIM").i(
            "Updates signature latest version, package=%s, version=%s",
            packageName,
            latestSignatureVersion
        )

        val installedVersionCode = appLoungePackageManager.getVersionCode(packageName)
        val installedVersionName = appLoungePackageManager.getVersionName(packageName)

        Timber.tag("FAHIM").i(
            "Updates signature installed version, package=%s, code=%s, name=%s",
            packageName,
            installedVersionCode,
            installedVersionName
        )

        val latestSignatureVersionNumber = try {
            latestSignatureVersion.split("_")[1].toInt()
        } catch (e: Exception) {
@@ -613,35 +470,18 @@ class UpdatesManagerImpl @Inject constructor(

        // Received list has build info of the latest version at the bottom.
        // We want it at the top.
        val buildInfoStartMs = nowMs()
        val builds = handleNetworkResult {
            fDroidRepository.getBuildVersionInfo(packageName).asReversed()
        }.data
        Timber.tag("FAHIM").i(
            "Updates build info fetched, package=%s, duration=%s, buildCount=%d",
            packageName,
            formatDuration(nowMs() - buildInfoStartMs),
            builds?.size ?: 0
        )

        val matchingIndex = builds?.find {
            it.versionCode == installedVersionCode && it.versionName == installedVersionName
        }?.run {
            builds.indexOf(this)
        } ?: run {
            Timber.tag("FAHIM").i(
                "Updates build info match missing, package=%s",
                packageName
            )
            return ""
        }

        Timber.tag("FAHIM").i(
            "Updates build info match found, package=%s, index=%d",
            packageName,
            matchingIndex
        )

        /* If latest latest signature version is (say) "update_33"
         * corresponding to (say) versionCode 10, and we need to find signature
         * version of (say) versionCode 7, then we calculate signature version as:
@@ -653,70 +493,4 @@ class UpdatesManagerImpl @Inject constructor(
    fun getApplicationCategoryPreference(): List<String> {
        return applicationRepository.getSelectedAppTypes()
    }

    private inline fun logInstalledApps(
        label: String,
        fetch: () -> List<String>,
    ): List<String> {
        val startMs = nowMs()
        val apps = fetch()
        Timber.tag("FAHIM").i(
            "Updates %s installed apps, count=%d, duration=%s",
            label,
            apps.size,
            formatDuration(nowMs() - startMs)
        )
        return apps
    }

    private suspend fun logUpdatesFetch(
        label: String,
        appCount: Int,
        fetch: suspend () -> Pair<List<Application>, ResultStatus>,
    ): Pair<List<Application>, ResultStatus> {
        val startMs = nowMs()
        val result = fetch()
        Timber.tag("FAHIM").i(
            "Updates %s fetch done, apps=%d, duration=%s, status=%s",
            label,
            appCount,
            formatDuration(nowMs() - startMs),
            result.second
        )
        return result
    }

    private suspend fun logSystemAppsFetch(
        label: String,
        fetch: suspend () -> List<Application>,
    ): List<Application> {
        val startMs = nowMs()
        val apps = fetch()
        Timber.tag("FAHIM").i(
            "Updates %s fetch done, apps=%d, duration=%s",
            label,
            apps.size,
            formatDuration(nowMs() - startMs)
        )
        return apps
    }

    private fun nowMs(): Long {
        return System.nanoTime() / NANOS_IN_MILLI
    }

    private fun formatDuration(durationMs: Long): String {
        val totalSeconds = durationMs / MILLIS_IN_SECOND
        val milliseconds = durationMs % MILLIS_IN_SECOND
        val seconds = totalSeconds % SECONDS_IN_MINUTE
        val totalMinutes = totalSeconds / SECONDS_IN_MINUTE
        val minutes = totalMinutes % MINUTES_IN_HOUR
        val hours = totalMinutes / MINUTES_IN_HOUR

        return if (hours > 0) {
            String.format(Locale.US, "%d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds)
        } else {
            String.format(Locale.US, "%02d:%02d.%03d", minutes, seconds, milliseconds)
        }
    }
}
+0 −69

File changed.

Preview size limit exceeded, changes collapsed.