From e382c1b2ddd89d2ed4d040702176ca1a64b732a7 Mon Sep 17 00:00:00 2001 From: TMC Date: Sat, 14 Nov 2026 18:50:50 +0100 Subject: [PATCH] Updater: fixes from the pre-release audit - Reopening About during a download no longer deletes the partial APK; restoring a finished download only looks at finished files, keeps the newest version and deletes older ones. - A download runs the Android 17 local-network check as a check does; on a block it offers "Allow access" for the download too, and retries the download afterwards. - A 404 or other server error is a failed check, never "You have the latest version", and deletes nothing. - TalkBack hears the results of a check and a download; during a download the card keeps a disabled button, so focus does not jump. These take effect from the update after 0.16.0: 0.15.0's own updater downloads 0.16.0. Co-Authored-By: Claude Opus 5.5 --- flexera/src/app/flexera/ui/FlexeraAbout.kt | 135 +++++++++++++----- .../src/app/flexera/update/FlexeraUpdater.kt | 69 +++++++-- 2 files changed, 153 insertions(+), 51 deletions(-) diff --git a/flexera/src/app/flexera/ui/FlexeraAbout.kt b/flexera/src/app/flexera/ui/FlexeraAbout.kt index 1ab4dc75af..8eb95dcd9a 100644 --- a/flexera/src/app/flexera/ui/FlexeraAbout.kt +++ b/flexera/src/app/flexera/ui/FlexeraAbout.kt @@ -42,6 +42,8 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue +import androidx.compose.runtime.key +import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue @@ -55,7 +57,9 @@ import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.LiveRegionMode import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.semantics.liveRegion import androidx.compose.ui.semantics.semantics import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp @@ -285,9 +289,14 @@ private class UpdateAction( @Composable private fun UpdateCard(state: UpdateState) { val context = LocalContext.current + // How many checks and downloads the owner has started from the card; TalkBack reads out how + // they went (see announce below). A download that is under way when the card appears was + // started from it too. + var asked by remember { mutableIntStateOf(if (state is UpdateState.Downloading) 1 else 0) } // Android 17 asks the owner before an app may reach devices on the local network, and an - // update server at home is one. The answer starts the check again; a "no" that Android no - // longer asks about leads to the app's page in the phone's settings instead. + // update server at home is one. The answer starts again what Android kept from the server, + // the download or the check; a "no" that Android no longer asks about leads to the app's + // page in the phone's settings instead. val askForLocalNetwork = rememberLauncherForActivityResult( ActivityResultContracts.RequestPermission(), ) { granted -> @@ -295,7 +304,13 @@ private fun UpdateCard(state: UpdateState) { if (granted || activity == null || activity.shouldShowRequestPermissionRationale(FlexeraUpdater.LOCAL_NETWORK_PERMISSION) ) { - FlexeraUpdater.check(context) + asked++ + // A failed download still knows its release, so it needs no new check first. + if (state is UpdateState.Failed && state.release != null) { + FlexeraUpdater.download(context) + } else { + FlexeraUpdater.check(context) + } } else { openAppSettings(context) } @@ -304,12 +319,23 @@ private fun UpdateCard(state: UpdateState) { val check = UpdateAction( label = stringResource(id = R.string.flexera_update_check), icon = Icons.Rounded.Refresh, - onClick = { FlexeraUpdater.check(context) }, + onClick = { + asked++ + FlexeraUpdater.check(context) + }, ) val download = UpdateAction( label = stringResource(id = R.string.flexera_update_download), icon = Icons.Rounded.Download, - onClick = { FlexeraUpdater.download(context) }, + onClick = { + asked++ + FlexeraUpdater.download(context) + }, + ) + val allowLocalNetwork = UpdateAction( + label = stringResource(id = R.string.flexera_update_allow_local_network), + icon = null, + onClick = { askForLocalNetwork.launch(FlexeraUpdater.LOCAL_NETWORK_PERMISSION) }, ) val content = when (state) { @@ -344,6 +370,14 @@ private fun UpdateCard(state: UpdateState) { title = stringResource(id = R.string.flexera_update_downloading, state.release.versionName), body = stringResource(id = R.string.flexera_update_progress, (state.progress * 100).toInt()), progress = state.progress, + // The button stays, disabled, so that TalkBack keeps its place on it until the + // download ends and the button offers what comes next. + action = UpdateAction( + label = stringResource(id = R.string.flexera_update_downloading_button), + icon = null, + onClick = {}, + busy = true, + ), ) is UpdateState.ReadyToInstall -> UpdateCardContent( @@ -363,39 +397,52 @@ private fun UpdateCard(state: UpdateState) { title = stringResource(id = R.string.flexera_update_failed_title), body = checkFailureText(state), action = if (state.problem == CheckProblem.LOCAL_NETWORK) { - UpdateAction( - label = stringResource(id = R.string.flexera_update_allow_local_network), - icon = null, - onClick = { askForLocalNetwork.launch(FlexeraUpdater.LOCAL_NETWORK_PERMISSION) }, - ) + allowLocalNetwork } else { UpdateAction( label = stringResource(id = R.string.flexera_update_try_again), icon = Icons.Rounded.Refresh, - onClick = { FlexeraUpdater.check(context) }, + onClick = check.onClick, ) }, ) } else { + // A download that Android kept from the server would only fail again until the + // owner allows local network access, so that is what the button asks for. + val blocked = state.problem == CheckProblem.LOCAL_NETWORK UpdateCardContent( title = stringResource(id = R.string.flexera_update_available, release.versionName), - body = stringResource( - id = if (state.failure == UpdateFailure.WRONG_FILE) { - R.string.flexera_update_wrong_file - } else { - R.string.flexera_update_download_failed - }, - ), - action = UpdateAction( - label = stringResource(id = R.string.flexera_update_try_again), - icon = Icons.Rounded.Download, - onClick = download.onClick, - ), + body = when { + state.failure == UpdateFailure.WRONG_FILE -> stringResource(id = R.string.flexera_update_wrong_file) + blocked -> stringResource( + id = R.string.flexera_update_download_failed_local_network, + FlexeraUpdater.SERVER_HOST, + ) + else -> stringResource(id = R.string.flexera_update_download_failed) + }, + action = if (blocked) { + allowLocalNetwork + } else { + UpdateAction( + label = stringResource(id = R.string.flexera_update_try_again), + icon = Icons.Rounded.Download, + onClick = download.onClick, + ) + }, ) } } } + // Once the owner has started a check or a download from the card, TalkBack reads out each + // outcome it shows, so that they hear how it went, also when it went as the time before. Not + // what the card says when the screen opens, which is read with the rest of the screen, and + // not the steps in between: a download would be read out at every percent. + val announce = asked > 0 && when (state) { + UpdateState.Idle, UpdateState.Checking, is UpdateState.Downloading -> false + UpdateState.UpToDate, is UpdateState.Available, is UpdateState.ReadyToInstall, is UpdateState.Failed -> true + } + val title = content.title val body = content.body val progress = content.progress @@ -406,20 +453,34 @@ private fun UpdateCard(state: UpdateState) { .background(MaterialTheme.colorScheme.surfaceContainer) .padding(16.dp), ) { - if (title != null) { - Text( - text = title, - style = MaterialTheme.typography.titleMedium, - color = MaterialTheme.colorScheme.onSurface, - ) - } - if (body != null) { - if (title != null) Spacer(Modifier.height(4.dp)) - Text( - text = body, - style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant, - ) + // The title and the body are read as one. The region is made anew each time it starts or + // stops being read out: Compose tells TalkBack about a new region once, as it is laid + // out, but about one that turns live as its text changes once for each change, and + // TalkBack could read it out twice. It is made anew for each step the owner starts as + // well: a step that fails as the one before did may change nothing on the card, since + // offline a check can be over before "Checking" is ever drawn. + key(announce, asked) { + Column( + modifier = Modifier.semantics(mergeDescendants = true) { + if (announce) liveRegion = LiveRegionMode.Polite + }, + ) { + if (title != null) { + Text( + text = title, + style = MaterialTheme.typography.titleMedium, + color = MaterialTheme.colorScheme.onSurface, + ) + } + if (body != null) { + if (title != null) Spacer(Modifier.height(4.dp)) + Text( + text = body, + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + } } if (progress != null) { Spacer(Modifier.height(16.dp)) diff --git a/flexera/src/app/flexera/update/FlexeraUpdater.kt b/flexera/src/app/flexera/update/FlexeraUpdater.kt index f17ff7637f..4711e6ec3d 100644 --- a/flexera/src/app/flexera/update/FlexeraUpdater.kt +++ b/flexera/src/app/flexera/update/FlexeraUpdater.kt @@ -30,6 +30,7 @@ import java.lang.ref.WeakReference import java.net.HttpURLConnection import java.net.InetAddress import java.net.NetworkInterface +import java.net.SocketTimeoutException import java.net.URL import java.net.UnknownHostException import javax.net.ssl.SSLException @@ -118,7 +119,8 @@ sealed interface UpdateState { /** * [release] is set when the failure happened after a newer version had been found. For a * failed check, [problem] says why and [detail] carries the error's own words, for - * whoever has to find the cause. + * whoever has to find the cause. A failed download sets [problem] only to + * [CheckProblem.LOCAL_NETWORK], the one cause the owner can remove from the About screen. */ data class Failed( val failure: UpdateFailure, @@ -188,6 +190,9 @@ object FlexeraUpdater { private const val UPDATES_DIR = "updates" private const val APK_PREFIX = "FlexeraLauncher-" + + /** Added to the APK's name while it downloads, so that only a finished file ends in ".apk". */ + private const val PARTIAL_SUFFIX = ".part" private const val CHANNEL_ID = "flexera_updates" private const val NOTIFICATION_ID = 0xF1E @@ -224,6 +229,11 @@ object FlexeraUpdater { fun start(app: Application) { if (!isSupported) return scope.launch { + // A half-written APK at this point is one that an earlier process did not finish: this + // process has only just started, so no download can be running yet. + updatesDir(app) + .listFiles { file -> file.name.startsWith(APK_PREFIX) && file.name.endsWith(PARTIAL_SUFFIX) } + ?.forEach { it.delete() } restoreDownload(app) // The process may have been restarted while the owner was allowing installs. installIfRequested() @@ -296,7 +306,7 @@ object FlexeraUpdater { return@launch } - if (release == null || !isNewer(release.versionName, BuildConfig.VERSION_NAME)) { + if (!isNewer(release.versionName, BuildConfig.VERSION_NAME)) { clearDownloads(app) mutableState.value = UpdateState.UpToDate return@launch @@ -330,13 +340,26 @@ object FlexeraUpdater { scope.launch { val target = apkFile(app, release) try { + // The APK comes from the same server as the check, so Android may keep this app + // from it in the same way. Say at once what is needed, as the check does, rather + // than wait out the time limit. + if (blockedFromLocalNetwork(app)) throw LocalNetworkBlockedException() downloadApk(app, release, target) { progress -> mutableState.value = UpdateState.Downloading(release, progress) } } catch (error: Exception) { Log.w(TAG, "Update download failed", error) clearInstallRequest(app) - mutableState.value = UpdateState.Failed(UpdateFailure.DOWNLOAD, release) + // As after a failed check: a server that did not answer in time may still be one + // that Android kept this app from, where the look before could not tell (the phone + // joined the home Wi-Fi during the download, say). + val blocked = error is LocalNetworkBlockedException || + (error is SocketTimeoutException && blockedFromLocalNetwork(app)) + mutableState.value = UpdateState.Failed( + UpdateFailure.DOWNLOAD, + release, + problem = if (blocked) CheckProblem.LOCAL_NETWORK else null, + ) return@launch } if (isValidUpdate(app, target)) { @@ -428,22 +451,37 @@ object FlexeraUpdater { /** * Picks up an update that was downloaded before the launcher process was restarted, so the * About screen can offer "Install" straight away. Also clears files left over from updates - * that are already installed. + * that are already installed. It does nothing unless the updater is idle, and it looks only at + * finished downloads: the About screen calls it each time it opens, also while a download runs + * or waits to be installed, and those files must stay. */ @JvmStatic fun restoreDownload(context: Context) { - if (!isSupported) return + if (!isSupported || mutableState.value != UpdateState.Idle) return val app = context.applicationContext - val candidates = updatesDir(app).listFiles { file -> file.name.startsWith(APK_PREFIX) }.orEmpty() + val candidates = updatesDir(app) + .listFiles { file -> file.name.startsWith(APK_PREFIX) && file.name.endsWith(".apk") } + .orEmpty() + // An update that was not installed before a newer one came out leaves two downloads here, + // listed in no set order. Only the newest is kept, so the card never offers a version that + // a later download has already replaced. var restored: UpdateState.ReadyToInstall? = null + var restoredVersionCode = 0L for (file in candidates) { - val info = if (file.name.endsWith(".apk")) app.packageManager.getPackageArchiveInfo(file.absolutePath, 0) else null + val info = app.packageManager.getPackageArchiveInfo(file.absolutePath, 0) val versionName = info?.versionName - if (versionName != null && restored == null && isValidUpdate(app, file)) { - restored = UpdateState.ReadyToInstall(FlexeraRelease(versionName, "", "", file.length()), file) - } else { + if (info == null || versionName == null || !isValidUpdate(app, file)) { file.delete() + continue } + val versionCode = PackageInfoCompat.getLongVersionCode(info) + if (restored != null && versionCode <= restoredVersionCode) { + file.delete() + continue + } + restored?.file?.delete() + restored = UpdateState.ReadyToInstall(FlexeraRelease(versionName, "", "", file.length()), file) + restoredVersionCode = versionCode } if (restored != null) mutableState.compareAndSet(UpdateState.Idle, restored) } @@ -554,12 +592,15 @@ object FlexeraUpdater { private const val MAX_DETAIL_LENGTH = 200 - /** Returns the latest published release, or null when nothing has been published yet. */ - private fun fetchLatestRelease(context: Context): FlexeraRelease? { + /** Returns the latest published release. */ + private fun fetchLatestRelease(context: Context): FlexeraRelease { val connection = open(context, LATEST_RELEASE_URL) try { val status = connection.responseCode - if (status == HttpURLConnection.HTTP_NOT_FOUND) return null + // "Not found" is an error too: releases have been published for a long time, so it + // means the repository was moved or made private, or a proxy stopped passing the + // question on. Taken as "nothing newer", it would report the app up to date and delete + // an update that waits to be installed, and nothing would say that updates stopped. if (status != HttpURLConnection.HTTP_OK) throw ServerErrorException(status) val json = JSONObject(connection.inputStream.bufferedReader().use { it.readText() }) @@ -586,7 +627,7 @@ object FlexeraUpdater { private fun downloadApk(context: Context, release: FlexeraRelease, target: File, onProgress: (Float) -> Unit) { target.parentFile?.mkdirs() - val partial = File(target.path + ".part") + val partial = File(target.path + PARTIAL_SUFFIX) val connection = open(context, release.apkUrl) try { val status = connection.responseCode