fix: prompt to uninstall newer aliu

This commit is contained in:
Diamond 2022-11-06 06:24:45 -08:00
parent ee9f36877d
commit e03a528e0e
No known key found for this signature in database
GPG key ID: DCBE5952BB3B6420
5 changed files with 43 additions and 12 deletions

View file

@ -3,8 +3,10 @@ package com.aliucord.manager.installer.util
import android.annotation.SuppressLint
import android.app.Application
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInstaller
import android.net.Uri
import android.os.Build
import com.aliucord.manager.installer.service.InstallService
import java.io.File
@ -36,3 +38,12 @@ fun PackageInstaller.installApks(application: Application, vararg apks: File) {
session.commit(contentIntent.intentSender)
session.close()
}
fun Context.uninstallApk(packageName: String) {
val packageURI = Uri.parse("package:$packageName")
val uninstallIntent = Intent(Intent.ACTION_DELETE, packageURI).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
startActivity(uninstallIntent)
}

View file

@ -1,8 +1,6 @@
package com.aliucord.manager.ui.viewmodel
import android.app.Application
import android.content.Intent
import android.net.Uri
import android.util.Log
import androidx.compose.runtime.*
import androidx.compose.ui.res.stringResource
@ -13,9 +11,10 @@ import com.aliucord.manager.BuildConfig
import com.aliucord.manager.R
import com.aliucord.manager.domain.manager.PreferencesManager
import com.aliucord.manager.domain.repository.GithubRepository
import com.aliucord.manager.installer.util.uninstallApk
import com.aliucord.manager.network.dto.Commit
import com.aliucord.manager.network.utils.fold
import com.aliucord.manager.util.getPackageVersionCode
import com.aliucord.manager.util.getPackageVersion
import com.aliucord.manager.util.showToast
import kotlinx.coroutines.*
@ -71,7 +70,7 @@ class HomeViewModel(
private suspend fun _fetchInstalledVersion() {
try {
val (versionName, versionCode) = application.getPackageVersionCode(preferences.packageName)
val (versionName, versionCode) = application.getPackageVersion(preferences.packageName)
withContext(Dispatchers.Main) {
installedVersion = versionName.split("-")[0].trim()
@ -126,12 +125,7 @@ class HomeViewModel(
}
fun uninstallAliucord() {
val packageURI = Uri.parse("package:${preferences.packageName}")
val uninstallIntent = Intent(Intent.ACTION_DELETE, packageURI).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
application.startActivity(uninstallIntent)
application.uninstallApk(preferences.packageName)
}
enum class VersionType {

View file

@ -101,6 +101,8 @@ class InstallViewModel(
delay(20000)
returnToHome = true
} catch (t: Throwable) {
stacktrace = Log.getStackTraceString(t)
Log.e(
BuildConfig.TAG,
"$debugInfo\n\n${Log.getStackTraceString(t)}"
@ -119,6 +121,24 @@ class InstallViewModel(
?.forEach { it.second.deleteRecursively() }
}
private suspend fun uninstallNewAliucord(targetVersion: Int) {
val (_, versionCode) = try {
application.getPackageVersion(preferences.packageName)
} catch (t: Throwable) {
return
}
if (targetVersion < versionCode) {
application.uninstallApk(preferences.packageName)
withContext(Dispatchers.Main) {
application.showToast(R.string.installer_uninstall_new)
}
throw Error("Pleaser uninstall newer Aliucord prior to installing")
}
}
override fun onCleared() {
if (installationRunning.getAndSet(false)) {
installJob.cancel("ViewModel cleared")
@ -150,6 +170,7 @@ class InstallViewModel(
val patchedDir = discordCacheDir.resolve("patched").also { it.deleteRecursively() }
clearOldCache(supportedVersion.toInt())
uninstallNewAliucord(supportedVersion.toInt())
// Download base.apk
val baseApkFile = step(InstallStep.DL_BASE_APK) {
@ -400,6 +421,11 @@ class InstallViewModel(
val discordCacheDir = externalCacheDir.resolve(dataJson.versionCode)
val patchedDir = discordCacheDir.resolve("patched").also { it.deleteRecursively() }
dataJson.versionCode.toInt().also {
clearOldCache(it)
uninstallNewAliucord(it)
}
// Download base.apk
val baseApkFile = step(InstallStep.DL_KT_APK) {
discordCacheDir.resolve("base.apk").let { file ->
@ -579,7 +605,6 @@ class InstallViewModel(
return value.value
} catch (t: Throwable) {
steps[step] = steps[step]!!.copy(status = InstallStatus.UNSUCCESSFUL)
stacktrace = Log.getStackTraceString(t)
currentStep = step
throw t

View file

@ -39,7 +39,7 @@ fun Context.showToast(@StringRes text: Int, vararg args: Any, length: Int = Toas
/**
* @return (versionName, versionCode)
*/
fun Context.getPackageVersionCode(pkg: String): Pair<String, Int> {
fun Context.getPackageVersion(pkg: String): Pair<String, Int> {
@Suppress("DEPRECATION")
return packageManager.getPackageInfo(pkg, 0)
.let { it.versionName to it.versionCode }

View file

@ -99,6 +99,7 @@
<string name="installer_success">Successfully installed Aliucord</string>
<string name="installer_aborted">Aborted Aliucord installation</string>
<string name="installer_failed">Installation failed with error %o</string>
<string name="installer_uninstall_new">Please uninstall your current version of Aliucord in order to continue!</string>
<string name="install_group_apk_dl">Download APKs</string>
<string name="install_group_lib_dl">Download Libraries</string>