Update check: the public GitHub releases, daily on start and from Settings › About
CI / check (push) Canceled after 0s
CI / check (push) Canceled after 0s
ReleaseCheck reads api.github.com's list of this repository's releases over plain HTTPS, compares with the installed version (a beta sorts below its release; -debug is left off), and offers the newest one the person may have: betas only to a beta install or with Include betas on. On start it runs at most once a day while Look for new versions is on (default: on in a beta, off in a release) and announces a version once, in a notification on its own low channel that opens the release page. Check for an update works in both builds; the full build's own installer stays for a found release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
18 files changed
+559
-22
No files matched your search
@@ -20,7 +20,9 @@ class LembasApp : Application() {
|
||||
NotificationChannel(CHANNEL_ASKS, getString(R.string.channel_asks), NotificationManager.IMPORTANCE_HIGH),
|
||||
NotificationChannel(CHANNEL_SCHEDULED, getString(R.string.channel_scheduled), NotificationManager.IMPORTANCE_DEFAULT),
|
||||
NotificationChannel(CHANNEL_SCHEDULED_FAILURES, getString(R.string.channel_scheduled_failures), NotificationManager.IMPORTANCE_HIGH),
|
||||
).onEach { it.lockscreenVisibility = android.app.Notification.VISIBILITY_PRIVATE },
|
||||
).onEach { it.lockscreenVisibility = android.app.Notification.VISIBILITY_PRIVATE } +
|
||||
// A new version of the app says nothing about the account.
|
||||
NotificationChannel(CHANNEL_UPDATES, getString(R.string.channel_updates), NotificationManager.IMPORTANCE_LOW),
|
||||
)
|
||||
// LLeMbas 2.0 has no reports, so 0.1's channel for them goes rather than sit empty in
|
||||
// the system's settings.
|
||||
@@ -32,6 +34,7 @@ class LembasApp : Application() {
|
||||
const val CHANNEL_ASKS = "asks"
|
||||
const val CHANNEL_SCHEDULED = "scheduled_results"
|
||||
const val CHANNEL_SCHEDULED_FAILURES = "scheduled_failures"
|
||||
const val CHANNEL_UPDATES = "updates"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,6 +50,7 @@ import eu.llembas.android.ui.scheduled.SchedulingModelsScreen
|
||||
import eu.llembas.android.ui.settings.SettingsScreen
|
||||
import eu.llembas.android.ui.signin.SignInScreen
|
||||
import eu.llembas.android.ui.theme.LembasTheme
|
||||
import eu.llembas.android.update.ReleaseCheck
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
@@ -71,6 +72,8 @@ class MainActivity : ComponentActivity() {
|
||||
super.onCreate(savedInstanceState)
|
||||
take(intent)
|
||||
followLanguage()
|
||||
// At most once a day, and only while Settings › About says so (update/ReleaseCheck.kt).
|
||||
lifecycleScope.launch { runCatching { ReleaseCheck(this@MainActivity).daily() } }
|
||||
if (Build.VERSION.SDK_INT >= 33 &&
|
||||
ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
|
||||
@@ -89,6 +89,13 @@ data class Prefs(
|
||||
val lastAssistChat: String = "",
|
||||
val lastAssistTitle: String = "",
|
||||
val lastAssistAt: Long = 0,
|
||||
/** Settings › About: look for new versions (null: on in a beta, off in a release). */
|
||||
val updateCheck: Boolean? = null,
|
||||
/** ... and include betas (null: on in a beta, off in a release). */
|
||||
val updateBetas: Boolean? = null,
|
||||
val updateCheckedAt: Long = 0,
|
||||
/** The last version a notification announced, so it is announced once. */
|
||||
val updateNotified: String = "",
|
||||
) {
|
||||
companion object {
|
||||
const val VOICE_LEMBAS = "lembas"
|
||||
|
||||
@@ -28,6 +28,7 @@ import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.Assistant
|
||||
import androidx.compose.material.icons.filled.CheckCircle
|
||||
import androidx.compose.material.icons.filled.Dns
|
||||
import androidx.compose.material.icons.filled.Download
|
||||
import androidx.compose.material.icons.filled.Info
|
||||
import androidx.compose.material.icons.filled.Mic
|
||||
import androidx.compose.material.icons.filled.Notifications
|
||||
@@ -35,9 +36,11 @@ import androidx.compose.material.icons.filled.NotificationsActive
|
||||
import androidx.compose.material.icons.filled.Palette
|
||||
import androidx.compose.material.icons.filled.PhoneAndroid
|
||||
import androidx.compose.material.icons.filled.RecordVoiceOver
|
||||
import androidx.compose.material.icons.filled.Science
|
||||
import androidx.compose.material.icons.filled.SystemUpdate
|
||||
import androidx.compose.material.icons.filled.Timer
|
||||
import androidx.compose.material.icons.filled.Translate
|
||||
import androidx.compose.material.icons.filled.Update
|
||||
import androidx.compose.material3.HorizontalDivider
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
@@ -77,7 +80,13 @@ import eu.llembas.android.auth.Prefs
|
||||
import eu.llembas.android.lembas
|
||||
import eu.llembas.android.push.Push
|
||||
import eu.llembas.android.ui.theme.LocalPalette
|
||||
import eu.llembas.android.update.AppVersion
|
||||
import eu.llembas.android.update.ReleaseCheck
|
||||
import eu.llembas.android.update.UpdateOffer
|
||||
import eu.llembas.android.update.UpdateResult
|
||||
import eu.llembas.android.update.Updater
|
||||
import eu.llembas.android.update.checksUpdates
|
||||
import eu.llembas.android.update.offersBetas
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/** What the settings page does; every action has a do-nothing default for the screenshots. */
|
||||
@@ -95,6 +104,10 @@ interface SettingsActions {
|
||||
fun assistantListens(on: Boolean) {}
|
||||
fun readAloud(on: Boolean) {}
|
||||
fun checkUpdate() {}
|
||||
fun openRelease() {}
|
||||
fun installUpdate() {}
|
||||
fun updateCheck(on: Boolean) {}
|
||||
fun updateBetas(on: Boolean) {}
|
||||
}
|
||||
|
||||
/** What the settings page shows. */
|
||||
@@ -105,8 +118,12 @@ data class SettingsState(
|
||||
val hasDistributor: Boolean = false,
|
||||
val pushNote: String? = null,
|
||||
val updateNote: String? = null,
|
||||
/** A newer release the last check found: the row opens its page, the full build can install it. */
|
||||
val updateFound: String? = null,
|
||||
val version: String = "",
|
||||
val selfUpdate: Boolean = false,
|
||||
/** Whether the app itself is a beta: the update settings' defaults follow it. */
|
||||
val beta: Boolean = false,
|
||||
/** Whether the phone has a speech recogniser of its own (Settings › Voice input › Phone). */
|
||||
val phoneRecognizer: Boolean = false,
|
||||
)
|
||||
@@ -121,6 +138,8 @@ fun SettingsScreen(onBack: () -> Unit, onAddInstance: () -> Unit) {
|
||||
val prefs by store.prefs.collectAsState(initial = Prefs())
|
||||
var pushNote by remember { mutableStateOf<String?>(null) }
|
||||
var updateNote by remember { mutableStateOf<String?>(null) }
|
||||
var updateFound by remember { mutableStateOf<UpdateOffer?>(null) }
|
||||
val beta = remember { AppVersion.parse(BuildConfig.VERSION_NAME)?.isBeta ?: false }
|
||||
val distributors = remember { Push.distributors(context) }
|
||||
val actions = remember {
|
||||
object : SettingsActions {
|
||||
@@ -182,8 +201,40 @@ fun SettingsScreen(onBack: () -> Unit, onAddInstance: () -> Unit) {
|
||||
}
|
||||
|
||||
override fun checkUpdate() {
|
||||
updateNote = context.getString(R.string.update_checking)
|
||||
scope.launch {
|
||||
when (val found = ReleaseCheck(context).check()) {
|
||||
is UpdateResult.Newer -> {
|
||||
updateFound = found.offer
|
||||
updateNote = context.getString(R.string.update_available, found.offer.version.toString())
|
||||
}
|
||||
|
||||
UpdateResult.Current -> {
|
||||
updateFound = null
|
||||
updateNote = context.getString(R.string.update_none)
|
||||
}
|
||||
|
||||
UpdateResult.Unreachable -> updateNote = context.getString(R.string.update_unreachable)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun openRelease() {
|
||||
updateFound?.let { offer -> runCatching { context.startActivity(ReleaseCheck.open(offer.page)) } }
|
||||
}
|
||||
|
||||
// The full build's own installer, for a release (llembas.eu names its APK and hash).
|
||||
override fun installUpdate() {
|
||||
scope.launch { updateNote = Updater(context).checkAndOffer() }
|
||||
}
|
||||
|
||||
override fun updateCheck(on: Boolean) {
|
||||
scope.launch { store.savePrefs { it.copy(updateCheck = on) } }
|
||||
}
|
||||
|
||||
override fun updateBetas(on: Boolean) {
|
||||
scope.launch { store.savePrefs { it.copy(updateBetas = on) } }
|
||||
}
|
||||
}
|
||||
}
|
||||
SettingsContent(
|
||||
@@ -194,8 +245,10 @@ fun SettingsScreen(onBack: () -> Unit, onAddInstance: () -> Unit) {
|
||||
hasDistributor = distributors.isNotEmpty(),
|
||||
pushNote = pushNote,
|
||||
updateNote = updateNote,
|
||||
updateFound = updateFound?.version?.toString(),
|
||||
version = BuildConfig.VERSION_NAME,
|
||||
selfUpdate = BuildConfig.SELF_UPDATE,
|
||||
beta = beta,
|
||||
phoneRecognizer = remember { PhoneRecognizer.available(context) },
|
||||
),
|
||||
actions,
|
||||
@@ -402,15 +455,42 @@ fun SettingsContent(state: SettingsState, actions: SettingsActions, onBack: () -
|
||||
item {
|
||||
Group(stringResource(R.string.settings_about)) {
|
||||
SettingRow(Icons.Filled.Info, stringResource(R.string.about_version, state.version), onClick = {})
|
||||
if (state.selfUpdate) {
|
||||
Rule()
|
||||
// A found release: the row opens its page; until then it checks.
|
||||
SettingRow(
|
||||
Icons.Filled.SystemUpdate,
|
||||
stringResource(R.string.update_check),
|
||||
subtitle = state.updateNote,
|
||||
onClick = if (state.updateFound != null) actions::openRelease else actions::checkUpdate,
|
||||
)
|
||||
// The full build installs a release itself; a beta is installed from its page.
|
||||
val found = state.updateFound
|
||||
if (state.selfUpdate && found != null && !found.contains("-")) {
|
||||
Rule()
|
||||
SettingRow(
|
||||
Icons.Filled.SystemUpdate,
|
||||
stringResource(R.string.update_check),
|
||||
subtitle = state.updateNote,
|
||||
onClick = actions::checkUpdate,
|
||||
Icons.Filled.Download,
|
||||
stringResource(R.string.update_install, found),
|
||||
onClick = actions::installUpdate,
|
||||
)
|
||||
}
|
||||
Rule()
|
||||
val checks = state.prefs.checksUpdates(state.beta)
|
||||
SettingRow(
|
||||
Icons.Filled.Update,
|
||||
stringResource(R.string.update_auto),
|
||||
subtitle = stringResource(R.string.update_auto_line),
|
||||
onClick = { actions.updateCheck(!checks) },
|
||||
trailing = { Switch(checked = checks, onCheckedChange = actions::updateCheck) },
|
||||
)
|
||||
Rule()
|
||||
val betas = state.prefs.offersBetas(state.beta)
|
||||
SettingRow(
|
||||
Icons.Filled.Science,
|
||||
stringResource(R.string.update_betas),
|
||||
subtitle = stringResource(R.string.update_betas_line),
|
||||
onClick = { actions.updateBetas(!betas) },
|
||||
trailing = { Switch(checked = betas, onCheckedChange = actions::updateBetas) },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
package eu.llembas.android.update
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Notification
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import eu.llembas.android.BuildConfig
|
||||
import eu.llembas.android.LembasApp
|
||||
import eu.llembas.android.R
|
||||
import eu.llembas.android.api.LembasJson
|
||||
import eu.llembas.android.api.SharedHttp
|
||||
import eu.llembas.android.api.await
|
||||
import eu.llembas.android.auth.Prefs
|
||||
import eu.llembas.android.lembas
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.builtins.ListSerializer
|
||||
import okhttp3.Request
|
||||
|
||||
/** A version as the releases name it: `1.2.3`, or `1.2.3-beta.4`, which sorts below `1.2.3`. */
|
||||
data class AppVersion(val major: Int, val minor: Int, val patch: Int, val beta: Int?) : Comparable<AppVersion> {
|
||||
val isBeta: Boolean get() = beta != null
|
||||
|
||||
override fun compareTo(other: AppVersion): Int = compareValuesBy(
|
||||
this,
|
||||
other,
|
||||
{ it.major },
|
||||
{ it.minor },
|
||||
{ it.patch },
|
||||
// A release sorts above every beta of itself.
|
||||
{ it.beta ?: Int.MAX_VALUE },
|
||||
)
|
||||
|
||||
override fun toString(): String = "$major.$minor.$patch" + (beta?.let { "-beta.$it" } ?: "")
|
||||
|
||||
companion object {
|
||||
private val SHAPE = Regex("""^v?(\d+)\.(\d+)\.(\d+)(?:-beta\.(\d+))?$""")
|
||||
|
||||
/**
|
||||
* A tag or a version name; a test build's `-debug…` is left off first. Anything else
|
||||
* (`-rc.1`, a typo) is not a version this check knows, and is never offered.
|
||||
*/
|
||||
fun parse(text: String): AppVersion? {
|
||||
val match = SHAPE.matchEntire(text.trim().substringBefore("-debug")) ?: return null
|
||||
val (a, b, c, beta) = match.destructured
|
||||
return AppVersion(a.toInt(), b.toInt(), c.toInt(), beta.takeIf { it.isNotEmpty() }?.toInt())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** One of GitHub's releases, as far as the check needs it. */
|
||||
@Serializable
|
||||
data class PublicRelease(
|
||||
@SerialName("tag_name") val tag: String,
|
||||
val prerelease: Boolean = false,
|
||||
val draft: Boolean = false,
|
||||
@SerialName("html_url") val page: String = "",
|
||||
)
|
||||
|
||||
/** A newer release to tell the person about: its version and the page its APKs are on. */
|
||||
data class UpdateOffer(val version: AppVersion, val page: String)
|
||||
|
||||
/** What a check found. */
|
||||
sealed interface UpdateResult {
|
||||
data class Newer(val offer: UpdateOffer) : UpdateResult
|
||||
|
||||
data object Current : UpdateResult
|
||||
|
||||
data object Unreachable : UpdateResult
|
||||
}
|
||||
|
||||
/**
|
||||
* The update check: the public releases of the app on GitHub, read over plain HTTPS -- no
|
||||
* Google service, no account, nothing sent but the request itself. It runs when the app starts,
|
||||
* at most once a day and only while Settings › About › *Look for new versions* is on (on by
|
||||
* default in a beta, off in a release), and when the person taps *Check for an update*. A newer
|
||||
* release becomes a notification that opens its page, where the APKs are; installing stays the
|
||||
* person's step (or F-Droid's, or Obtainium's).
|
||||
*
|
||||
* Betas are offered to a beta, or to anyone who turned *Include betas* on.
|
||||
*/
|
||||
class ReleaseCheck(private val context: Context) {
|
||||
suspend fun check(): UpdateResult = withContext(Dispatchers.IO) {
|
||||
val prefs = context.lembas.instances.prefs.first()
|
||||
val installed = AppVersion.parse(BuildConfig.VERSION_NAME) ?: return@withContext UpdateResult.Current
|
||||
try {
|
||||
val releases = SharedHttp.newCall(
|
||||
Request.Builder().url(RELEASES).header("Accept", "application/vnd.github+json").build(),
|
||||
).await().use {
|
||||
if (!it.isSuccessful) return@withContext UpdateResult.Unreachable
|
||||
LembasJson.decodeFromString(ListSerializer(PublicRelease.serializer()), it.body!!.string())
|
||||
}
|
||||
newest(releases, installed, prefs.offersBetas(installed.isBeta))
|
||||
?.let { UpdateResult.Newer(it) } ?: UpdateResult.Current
|
||||
} catch (e: Exception) {
|
||||
UpdateResult.Unreachable
|
||||
}
|
||||
}
|
||||
|
||||
/** On start: once a day at most, when the setting is on; a release is announced once. */
|
||||
suspend fun daily(now: Long = System.currentTimeMillis()) {
|
||||
val store = context.lembas.instances
|
||||
val prefs = store.prefs.first()
|
||||
val installed = AppVersion.parse(BuildConfig.VERSION_NAME) ?: return
|
||||
if (!prefs.checksUpdates(installed.isBeta) || !due(prefs.updateCheckedAt, now)) return
|
||||
val found = check()
|
||||
if (found == UpdateResult.Unreachable) return
|
||||
store.savePrefs { it.copy(updateCheckedAt = now) }
|
||||
val offer = (found as? UpdateResult.Newer)?.offer ?: return
|
||||
if (offer.version.toString() == prefs.updateNotified) return
|
||||
notify(offer)
|
||||
store.savePrefs { it.copy(updateNotified = offer.version.toString()) }
|
||||
}
|
||||
|
||||
fun notify(offer: UpdateOffer) {
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) return
|
||||
val page = PendingIntent.getActivity(
|
||||
context,
|
||||
NOTIFICATION,
|
||||
open(offer.page),
|
||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
||||
)
|
||||
val text = context.getString(R.string.update_notice_text)
|
||||
val notice = NotificationCompat.Builder(context, LembasApp.CHANNEL_UPDATES)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setContentTitle(context.getString(R.string.update_notice_title, offer.version.toString()))
|
||||
.setContentText(text)
|
||||
.setStyle(NotificationCompat.BigTextStyle().bigText(text))
|
||||
.setContentIntent(page)
|
||||
.setAutoCancel(true)
|
||||
.setCategory(Notification.CATEGORY_RECOMMENDATION)
|
||||
// Nothing from the account: the app's version is fine on a locked screen.
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||
.build()
|
||||
NotificationManagerCompat.from(context).notify(NOTIFICATION, notice)
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val REPOSITORY = "https://github.com/LLeMbas/LLeMbas-Android"
|
||||
const val RELEASES = "https://api.github.com/repos/LLeMbas/LLeMbas-Android/releases?per_page=30"
|
||||
private const val DAY_MS = 24L * 60 * 60 * 1000
|
||||
private const val NOTIFICATION = 0x11e4
|
||||
|
||||
/** The newest release later than [installed] that may be offered; drafts never are. */
|
||||
fun newest(releases: List<PublicRelease>, installed: AppVersion, betas: Boolean): UpdateOffer? = releases
|
||||
.asSequence()
|
||||
.filter { !it.draft }
|
||||
.mapNotNull { r -> AppVersion.parse(r.tag)?.let { it to r } }
|
||||
// A pre-release is a beta whatever its tag says, and a beta tag is one whatever GitHub says.
|
||||
.filter { (v, r) -> betas || (!v.isBeta && !r.prerelease) }
|
||||
.filter { (v, _) -> v > installed }
|
||||
.maxByOrNull { (v, _) -> v }
|
||||
?.let { (v, r) -> UpdateOffer(v, r.page.ifBlank { "$REPOSITORY/releases/tag/${r.tag}" }) }
|
||||
|
||||
fun due(last: Long, now: Long): Boolean = last <= 0 || now - last >= DAY_MS || now < last
|
||||
|
||||
fun open(url: String): Intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
}
|
||||
|
||||
/** *Look for new versions*: as chosen, else on in a beta and off in a release. */
|
||||
fun Prefs.checksUpdates(installedIsBeta: Boolean): Boolean = updateCheck ?: installedIsBeta
|
||||
|
||||
/** *Include betas*: as chosen, else on in a beta and off in a release. */
|
||||
fun Prefs.offersBetas(installedIsBeta: Boolean): Boolean = updateBetas ?: installedIsBeta
|
||||
Reference in new issue
Block a user