Skip to content

Commit

Permalink
Fix self-upgrade on API <=28
Browse files Browse the repository at this point in the history
Remove obsolete api level checks as Flutter's minSdkVersion is 21 (Lollipop)
  • Loading branch information
evgfilim1 committed May 27, 2024
1 parent 916cb2a commit b5e39e7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
### Исправлено

- Отображалась ошибка при ситуации со вторым повторным голосованием в режиме отладки.
- Обновление внутри приложения для устройств на базе Android 9 и старше приводило к падению.

## [v0.3.0-rc.2] - 2024-04-12

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import java.io.File
class SelfUpdater(private val context: Context) {
private var reporter: UpdateProgressReporter? = null

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun registerSessionCallback(sessionCallback: UpdateProgressReporter) {
context.packageManager.packageInstaller.registerSessionCallback(sessionCallback)
reporter = sessionCallback
Expand All @@ -25,13 +24,9 @@ class SelfUpdater(private val context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !context.packageManager.canRequestPackageInstalls()) {
error("Cannot request package installs, check app permissions")
}
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> doUpdateLollipop(path)
else -> doUpdateLegacy(path)
}
doUpdateLollipop(path)
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun doUpdateLollipop(path: String) {
val file = File(path)
if (!file.isFile) {
Expand Down Expand Up @@ -73,14 +68,6 @@ class SelfUpdater(private val context: Context) {
session.commit(pendingIntent.intentSender)
}

private fun doUpdateLegacy(path: String) {
Intent(Intent.ACTION_VIEW).apply {
data = Uri.fromFile(File(path))
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}.let(context::startActivity)
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun handleIntent(intent: Intent) {
if (intent.action != PACKAGE_INSTALLED_ACTION) {
error("Unexpected intent action: ${intent.action}")
Expand All @@ -96,7 +83,7 @@ class SelfUpdater(private val context: Context) {
if (installIntent.`package` !in KNOWN_INSTALLERS) {
error("Unexpected package received from installer: ${installIntent.`package`}")
}
if (installIntent.action != "android.content.pm.action.CONFIRM_INSTALL") {
if (installIntent.action !in KNOWN_ACTIONS) {
error("Unexpected action received from installer: ${installIntent.action}")
}

Expand Down Expand Up @@ -124,5 +111,9 @@ class SelfUpdater(private val context: Context) {
"com.android.packageinstaller",
"com.google.android.packageinstaller",
)
private val KNOWN_ACTIONS = setOf(
"android.content.pm.action.CONFIRM_INSTALL",
"android.content.pm.action.CONFIRM_PERMISSIONS", // I get it on Android 9
)
}
}

0 comments on commit b5e39e7

Please sign in to comment.