refactor: Move getApkVersion to LawnchairUtils.kt

This commit is contained in:
Pun Butrach
2025-12-07 20:23:46 +07:00
parent 4fa4d320f7
commit 5c48860ef9
3 changed files with 87 additions and 61 deletions
@@ -6,6 +6,7 @@ import android.provider.Settings
import android.util.Log
import androidx.core.content.FileProvider
import androidx.core.net.toUri
import app.lawnchair.util.getApkVersionComparison
import com.android.launcher3.BuildConfig
import com.android.launcher3.Utilities
import java.io.File
@@ -216,64 +217,6 @@ private fun Context.requestInstallPermission() {
}
}
/**
* Parses a version code into [Major, Minor, Stage, Release, Patch].
* Handles both 8-digit (AA_BB_CC_DD) and 10-digit (AA_BB_CC_DD_EE) formats.
*
* Lawnchair format has: [Major, Minor, Stage, Release]
*
* pE format has: [Major, Minor, Stage, Release, Patch]
*/
fun versionParser(version: Long): List<Int> {
var ver = version
// If version is less than 1 Billion (1,000,000,000), it is likely the 8-digit format
// (AA_BB_CC_DD). Multiply by 100 to shift it to 10-digit format equivalent
// (AA_BB_CC_DD_00), so the math below works for both.
if (ver < 1_000_000_000L) {
ver *= 100
}
val patch = (ver % 100).toInt() // EE
val release = ((ver / 100) % 100).toInt() // DD
val stage = ((ver / 10000) % 100).toInt() // CC
val minor = ((ver / 1000000) % 100).toInt() // BB
val major = ((ver / 100000000)).toInt() // AA
return listOf(major, minor, stage, release, patch)
}
/**
* Get both current and APK version for the purpose of comparing them.
* Returns a [Pair] of (current build version, apk build version) or null if parsing fails.
*/
fun Context.getApkVersionComparison(apkFile: File): Pair<List<Int>, List<Int>>? {
val pm = packageManager
val info = pm.getPackageArchiveInfo(apkFile.absolutePath, 0)
?: return null
val apkVersionCode = if (Utilities.ATLEAST_P) {
info.longVersionCode
} else {
@Suppress("DEPRECATION")
info.versionCode.toLong()
}
val currentVersionCode = if (Utilities.ATLEAST_P) {
pm.getPackageInfo(packageName, 0).longVersionCode
} else {
BuildConfig.VERSION_CODE.toLong()
}
val apkParsed = versionParser(apkVersionCode)
val currentParsed = versionParser(currentVersionCode)
Log.d("UpdateCheck", "Current: $currentParsed, APK: $apkParsed")
return Pair(currentParsed, apkParsed)
}
/**
* Checks if the downloaded APK file has a higher Major (AA) version than the currently
* installed build.
@@ -24,9 +24,8 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import app.lawnchair.ui.util.preview.PreviewLawnchair
import app.lawnchair.util.getApkVersionComparison
import com.android.launcher3.R
import java.io.File
@@ -132,7 +131,6 @@ fun UpdateSection(
},
)
}
}
}
}
@@ -38,6 +38,7 @@ import android.net.Uri
import android.os.Build
import android.os.Looper
import android.provider.OpenableColumns
import android.util.Log
import android.util.Size
import android.view.View
import android.widget.TextView
@@ -48,6 +49,8 @@ import app.lawnchair.preferences.PreferenceManager
import app.lawnchair.preferences2.PreferenceManager2
import app.lawnchair.theme.color.ColorOption
import app.lawnchair.theme.color.tokens.ColorTokens
import app.lawnchair.ui.preferences.about.versionParser
import com.android.launcher3.BuildConfig
import com.android.launcher3.R
import com.android.launcher3.Utilities
import com.android.launcher3.util.Executors.MAIN_EXECUTOR
@@ -55,6 +58,7 @@ import com.android.launcher3.util.Themes
import com.android.systemui.shared.system.QuickStepContract
import com.patrykmichalik.opto.core.firstBlocking
import java.io.ByteArrayOutputStream
import java.io.File
import java.util.Locale
import java.util.concurrent.Callable
import java.util.concurrent.ExecutionException
@@ -272,6 +276,87 @@ fun Context.getDefaultResolveInfo(): ResolveInfo? {
return packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)
}
/**
* Parses a version code into [Major, Minor, Stage, Release, Patch].
* Handles both 8-digit (AA_BB_CC_DD) and 10-digit (AA_BB_CC_DD_EE) formats.
*
* Lawnchair format has: [Major, Minor, Stage, Release]
*
* pE format has: [Major, Minor, Stage, Release, Patch]
*/
private fun versionParser(version: Long): List<Int> {
var ver = version
// If version is less than 1 Billion (1,000,000,000), it is likely the 8-digit format
// (AA_BB_CC_DD). Multiply by 100 to shift it to 10-digit format equivalent
// (AA_BB_CC_DD_00), so the math below works for both.
if (ver < 1_000_000_000L) {
ver *= 100
}
val patch = (ver % 100).toInt() // EE
val release = ((ver / 100) % 100).toInt() // DD
val stage = ((ver / 10000) % 100).toInt() // CC
val minor = ((ver / 1000000) % 100).toInt() // BB
val major = ((ver / 100000000)).toInt() // AA
return listOf(major, minor, stage, release, patch)
}
// pE-TODO: Make this actually sensible because the writing is really non-sense after re-reading for fourth time
/**
* Get both current and APK version for the purpose of comparing them.
* Returns a [Pair] of (current build version, apk build version) or null if parsing fails.
*/
fun Context.getApkVersionComparison(apkFile: File): Pair<List<Int>, List<Int>>? {
val pm = packageManager
val info = pm.getPackageArchiveInfo(apkFile.absolutePath, 0)
?: return null
val apkVersionCode = if (Utilities.ATLEAST_P) {
info.longVersionCode
} else {
@Suppress("DEPRECATION")
info.versionCode.toLong()
}
val currentVersionCode = if (Utilities.ATLEAST_P) {
pm.getPackageInfo(packageName, 0).longVersionCode
} else {
BuildConfig.VERSION_CODE.toLong()
}
val apkParsed = versionParser(apkVersionCode)
val currentParsed = versionParser(currentVersionCode)
Log.d("UpdateCheck", "Current: $currentParsed, APK: $apkParsed")
return Pair(currentParsed, apkParsed)
}
// pE-TODO: Make this actually sensible because the writing is really non-sense after re-reading for fourth time
/**
* Get current version for the purpose of comparing them.
* Returns a [Pair] of (current build version, apk build version else null)
*/
fun Context.getApkVersionComparison(): Pair<List<Int>, Nothing?> {
val pm = packageManager
val currentVersionCode = if (Utilities.ATLEAST_P) {
pm.getPackageInfo(packageName, 0).longVersionCode
} else {
BuildConfig.VERSION_CODE.toLong()
}
val currentParsed = versionParser(currentVersionCode)
Log.d("UpdateCheck", "Current: $currentParsed")
return Pair(currentParsed, null)
}
fun Drawable.toBitmap(): Bitmap {
if (this is BitmapDrawable) {
return bitmap