mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
feat(updates): install any kept Preview build, link builds to their PR and commit, flag stale test builds (#2624)
* feat(updates): install any kept Preview build, link builds to their PR and commit, flag stale test builds * fix(updates): address review of the Updates tab history and test-build watch * fix(updates): notify on switch to a followed PR, keep higher-version rebuilds installable * fix(ci): let full reruns replace every artifact, note only expired PR builds * fix(ci): leave the rebuild note only when the PR build is too old to rerun * fix(ci): poll unfinished PR builds together and queue version-bump runs * fix(ci): also rebuild PR builds that were running when the versionCode changed * fix(ci): rebuild fork and just-finished PR builds, recheck the PR head before a rerun * fix(ci): fail the rebuild sweep when finding candidates fails * fix(ci): time the bump from the rebuild run's creation and scan unfinished builds of any age * fix(ci): check the latest build of every open PR instead of scanning run history * fix(ci): count PR builds that finish between the version push and the rebuild run * fix(ci): use the push time from the event as the rebuild boundary
This commit is contained in:
1 parent
c847659789
commit
87c571f443
47 files changed
+1579
-87
No files matched your search
@@ -53,6 +53,7 @@ class App : Application() {
|
||||
com.droiddeck.launcher.session.SessionPrefs.applyNtsyncDefault(this)
|
||||
com.droiddeck.launcher.session.SessionPrefs.settleGpuDriverMode(this)
|
||||
com.droiddeck.launcher.update.AppUpdates.init(this)
|
||||
com.droiddeck.launcher.update.UpdateWatch.sync(this, com.droiddeck.launcher.update.AppUpdates.follow(this, null))
|
||||
com.droiddeck.launcher.core.DownloadSource.init(this)
|
||||
prefetchAddonPacks()
|
||||
GpuClockPin.clearLeftover(this)
|
||||
|
||||
@@ -17,6 +17,7 @@ import com.droiddeck.launcher.ui.UpdatesActions
|
||||
import com.droiddeck.launcher.ui.UpdatesState
|
||||
import com.droiddeck.launcher.update.AppUpdates
|
||||
import com.droiddeck.launcher.update.SelfInstaller
|
||||
import com.droiddeck.launcher.update.UpdateWatch
|
||||
|
||||
internal class UpdatesMenu(private val activity: Activity, private val ui: Handler) {
|
||||
var catalog by mutableStateOf<AppUpdates.Catalog?>(null)
|
||||
@@ -43,6 +44,7 @@ internal class UpdatesMenu(private val activity: Activity, private val ui: Handl
|
||||
.onFailure { error = activity.getString(R.string.upd_unknown_apps_failed); pending = null }
|
||||
},
|
||||
onDismissPermission = { askPermission = false; pending = null },
|
||||
onSeen = { r -> UpdateWatch.seen(activity, r) },
|
||||
)
|
||||
|
||||
val hasUpdate: Boolean get() = AppUpdates.hasUpdate(catalog, follow)
|
||||
@@ -77,6 +79,7 @@ internal class UpdatesMenu(private val activity: Activity, private val ui: Handl
|
||||
|
||||
private fun choose(f: AppUpdates.Follow) {
|
||||
AppUpdates.setFollow(activity, f)
|
||||
UpdateWatch.sync(activity, f, catalog?.let { AppUpdates.release(it, f) })
|
||||
follow = f
|
||||
error = null
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||
import androidx.compose.foundation.layout.FlowRow
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
@@ -31,7 +33,9 @@ import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Bolt
|
||||
import androidx.compose.material.icons.automirrored.filled.OpenInNew
|
||||
import androidx.compose.material.icons.outlined.CheckCircle
|
||||
import androidx.compose.material.icons.outlined.ContentCopy
|
||||
import androidx.compose.material.icons.outlined.Code
|
||||
import androidx.compose.material.icons.outlined.Download
|
||||
import androidx.compose.material.icons.outlined.Info
|
||||
@@ -50,6 +54,7 @@ import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
@@ -59,7 +64,12 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.platform.LocalClipboardManager
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.AnnotatedString
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
@@ -87,6 +97,7 @@ class UpdatesActions(
|
||||
val onInstall: (Release) -> Unit = {},
|
||||
val onAllowInstalls: () -> Unit = {},
|
||||
val onDismissPermission: () -> Unit = {},
|
||||
val onSeen: (Release) -> Unit = {},
|
||||
)
|
||||
|
||||
@OptIn(androidx.compose.foundation.ExperimentalFoundationApi::class)
|
||||
@@ -112,6 +123,13 @@ internal fun UpdatesPage(s: FrontEndState, a: FrontEndActions, modifier: Modifie
|
||||
if (u.checking) stringResource(R.string.common_checking) else if (checked != null) stringResource(R.string.upd_checked, ago(checked).replaceFirstChar { it.lowercase() }) else stringResource(R.string.upd_not_checked),
|
||||
fontSize = 13.sp, color = colors.onSurfaceVariant,
|
||||
)
|
||||
val clipboard = LocalClipboardManager.current
|
||||
val context = LocalContext.current
|
||||
val details = buildDetails(me, s.buildLabel)
|
||||
ToolIcon(Icons.Outlined.ContentCopy, stringResource(R.string.upd_copy_build)) {
|
||||
clipboard.setText(AnnotatedString(details))
|
||||
android.widget.Toast.makeText(context, context.getString(R.string.upd_copied), android.widget.Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
ToolIcon(Icons.Outlined.Refresh, stringResource(R.string.store_check_updates), busy = u.checking, enabled = !u.checking && u.stage == null, onClick = ua.onCheck)
|
||||
}
|
||||
BoxWithConstraints(Modifier.weight(1f).fillMaxWidth()) {
|
||||
@@ -169,6 +187,8 @@ private fun StatusPanel(s: FrontEndState, u: UpdatesState, ua: UpdatesActions, m
|
||||
offered && installBlock != null -> Look(colors.onSurfaceVariant, stringResource(R.string.upd_blocked_status), newBuild(u.follow, release!!), null)
|
||||
offer == Offer.UPDATE -> Look(AttentionAmber, stringResource(R.string.upd_available), newBuild(u.follow, release!!), null)
|
||||
offer == Offer.SWITCH -> Look(pal.signal, stringResource(R.string.upd_switch), newBuild(u.follow, release!!), null)
|
||||
offer == Offer.AHEAD && u.follow.channel == Channel.NIGHTLY -> Look(pal.signal, stringResource(R.string.upd_ahead_preview_status),
|
||||
stringResource(R.string.upd_ahead_preview_headline), stringResource(R.string.upd_ahead_preview_detail))
|
||||
offer == Offer.AHEAD -> Look(pal.signal, stringResource(R.string.upd_ahead_status), stringResource(R.string.upd_ahead_headline),
|
||||
stringResource(R.string.upd_ahead_detail))
|
||||
offer == Offer.GONE -> Look(AttentionAmber, stringResource(R.string.upd_gone_status), stringResource(R.string.upd_gone_headline),
|
||||
@@ -186,6 +206,12 @@ private fun StatusPanel(s: FrontEndState, u: UpdatesState, ua: UpdatesActions, m
|
||||
Text(look.headline, fontSize = 22.sp, fontWeight = FontWeight.Bold, color = colors.onBackground, modifier = Modifier.padding(top = 4.dp))
|
||||
if (offered) {
|
||||
ReleaseNotes(changeTitle(release!!.title.ifBlank { release.tag }), release.summary, release.tag)
|
||||
ReleaseFacts(release)
|
||||
}
|
||||
if (release != null && u.follow.channel == Channel.TEST && catalog != null) {
|
||||
androidx.compose.runtime.LaunchedEffect(release.tag, release.commit, release.apk?.sha256) { ua.onSeen(release) }
|
||||
if (!offered) ReleaseFacts(release)
|
||||
TestFreshness(catalog, release)
|
||||
}
|
||||
if (look.detail != null) Text(look.detail, fontSize = 14.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(top = 10.dp))
|
||||
if (installBlock != null && offered) {
|
||||
@@ -204,7 +230,7 @@ private fun StatusPanel(s: FrontEndState, u: UpdatesState, ua: UpdatesActions, m
|
||||
offered && installBlock != null -> Unit
|
||||
offer == Offer.UPDATE -> PrimaryButton(stringResource(R.string.upd_update_button, button), enabled = installable, main = true) { ua.onInstall(release!!) }
|
||||
offer == Offer.SWITCH -> PrimaryButton(stringResource(R.string.upd_install_button, button), enabled = installable, main = true) { ua.onInstall(release!!) }
|
||||
offer == Offer.AHEAD && installBlock == null ->
|
||||
offer == Offer.AHEAD && u.follow.channel == Channel.STABLE && installBlock == null ->
|
||||
SecondaryButton(stringResource(R.string.upd_install_anyway, release?.version.orEmpty()), enabled = installable) { ua.onInstall(release!!) }
|
||||
offer == Offer.GONE -> PrimaryButton(stringResource(R.string.upd_follow_preview), main = true) { ua.onFollow(Follow(Channel.NIGHTLY)) }
|
||||
}
|
||||
@@ -213,7 +239,7 @@ private fun StatusPanel(s: FrontEndState, u: UpdatesState, ua: UpdatesActions, m
|
||||
Text(stringResource(R.string.upd_stop_session), fontSize = 12.5.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(top = 8.dp))
|
||||
}
|
||||
if (u.follow.channel == Channel.NIGHTLY) {
|
||||
PreviewHistory(catalog, me)
|
||||
PreviewHistory(catalog, me, s, u, ua)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -273,6 +299,9 @@ private fun ChannelPicker(u: UpdatesState, ua: UpdatesActions, sessionRunning: B
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val catalog = u.catalog
|
||||
val tests = catalog?.tests.orEmpty()
|
||||
val me = remember { AppUpdates.installed() }
|
||||
val ready = tests.count { AppUpdates.installable(it, me) }
|
||||
var showTests by rememberSaveable { mutableStateOf(false) }
|
||||
Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||
ChannelCard(
|
||||
Icons.Outlined.Verified, stringResource(R.string.upd_stable), stringResource(R.string.upd_stable_hint),
|
||||
@@ -284,13 +313,11 @@ private fun ChannelPicker(u: UpdatesState, ua: UpdatesActions, sessionRunning: B
|
||||
) { ua.onFollow(Follow(Channel.NIGHTLY)) }
|
||||
ChannelCard(
|
||||
Icons.Outlined.Science, stringResource(R.string.upd_tests), stringResource(R.string.upd_tests_hint),
|
||||
if (tests.isEmpty()) stringResource(R.string.upd_tests_none) else pluralStringResource(R.plurals.upd_tests_count, tests.size, tests.size),
|
||||
if (tests.isEmpty()) stringResource(R.string.upd_tests_none) else pluralStringResource(R.plurals.upd_tests_count, ready, ready),
|
||||
u.follow.channel == Channel.TEST, enabled = tests.isNotEmpty() || u.follow.channel == Channel.TEST,
|
||||
) { tests.firstOrNull()?.let { ua.onFollow(Follow(Channel.TEST, it.pr)) } }
|
||||
AnimatedVisibility(u.follow.channel == Channel.TEST && tests.isNotEmpty(), enter = expandVertically(Motion.sp(1f)) + fadeIn(Motion.sp(1f)), exit = shrinkVertically(Motion.sp(1f)) + fadeOut(Motion.sp(1f))) {
|
||||
Column(Modifier.padding(start = 18.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
tests.forEach { t -> TestRow(t, u.follow.pr == t.pr) { ua.onFollow(Follow(Channel.TEST, t.pr)) } }
|
||||
}
|
||||
) { showTests = !showTests }
|
||||
AnimatedVisibility((showTests || u.follow.channel == Channel.TEST) && tests.isNotEmpty(), enter = expandVertically(Motion.sp(1f)) + fadeIn(Motion.sp(1f)), exit = shrinkVertically(Motion.sp(1f)) + fadeOut(Motion.sp(1f))) {
|
||||
TestList(tests, me, u, ua)
|
||||
}
|
||||
val context = androidx.compose.ui.platform.LocalContext.current
|
||||
val agentReady = com.droiddeck.launcher.agent.AgentHost.installed &&
|
||||
@@ -305,56 +332,211 @@ private fun ChannelPicker(u: UpdatesState, ua: UpdatesActions, sessionRunning: B
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PreviewHistory(catalog: AppUpdates.Catalog?, me: AppUpdates.Installed) {
|
||||
private fun PreviewHistory(catalog: AppUpdates.Catalog?, me: AppUpdates.Installed, s: FrontEndState, u: UpdatesState, ua: UpdatesActions) {
|
||||
val history = catalog?.let { AppUpdates.previewHistory(it, me) } ?: return
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val pal = LocalPalette.current
|
||||
val key = catalog?.recentPreviews?.firstOrNull()?.commit
|
||||
var expanded by rememberSaveable(key) { mutableStateOf(false) }
|
||||
val heading = when (history.kind) {
|
||||
AppUpdates.PreviewHistoryKind.CURRENT -> stringResource(R.string.upd_preview_current)
|
||||
AppUpdates.PreviewHistoryKind.BEHIND -> pluralStringResource(
|
||||
R.plurals.upd_preview_behind, history.buildsBehind, history.buildsBehind,
|
||||
)
|
||||
AppUpdates.PreviewHistoryKind.RECENT -> pluralStringResource(
|
||||
R.plurals.upd_preview_recent, history.changes.size, history.changes.size,
|
||||
R.plurals.upd_preview_recent, maxOf(history.builds.size, history.changes.size), maxOf(history.builds.size, history.changes.size),
|
||||
)
|
||||
}
|
||||
Column(Modifier.fillMaxWidth().padding(top = 18.dp)) {
|
||||
Text(heading, fontSize = 11.5.sp, fontWeight = FontWeight.Medium, color = colors.onSurfaceVariant)
|
||||
if (history.changes.isNotEmpty()) {
|
||||
val visible = if (expanded) history.changes else history.changes.take(3)
|
||||
Column(Modifier.padding(top = 3.dp), verticalArrangement = Arrangement.spacedBy(3.dp)) {
|
||||
visible.forEach { change ->
|
||||
Column {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
Text(
|
||||
changeTitle(change.title.ifBlank { change.commit.take(7) }),
|
||||
fontSize = 12.5.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground,
|
||||
maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f),
|
||||
)
|
||||
Text(ago(change.publishedAt), fontSize = 11.sp, color = colors.onSurfaceVariant)
|
||||
}
|
||||
if (change.summary.isNotBlank()) Text(
|
||||
change.summary, fontSize = 11.5.sp, color = colors.onSurfaceVariant,
|
||||
maxLines = 1, overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
if (history.builds.isNotEmpty()) {
|
||||
Text(stringResource(R.string.upd_preview_pick), fontSize = 11.5.sp, color = colors.onSurfaceVariant)
|
||||
PreviewBuilds(history, me, s, u, ua)
|
||||
} else if (history.changes.isNotEmpty()) {
|
||||
LegacyChanges(history.changes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PreviewBuilds(history: AppUpdates.PreviewHistory, me: AppUpdates.Installed, s: FrontEndState, u: UpdatesState, ua: UpdatesActions) {
|
||||
val builds = history.builds
|
||||
var open by rememberSaveable { mutableStateOf<String?>(null) }
|
||||
var shown by rememberSaveable(builds.firstOrNull()?.commit) {
|
||||
mutableIntStateOf(maxOf(5, if (history.installedIndex >= 0) history.installedIndex + 2 else 0))
|
||||
}
|
||||
Column(Modifier.padding(top = 6.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {
|
||||
builds.take(shown).forEachIndexed { index, build ->
|
||||
PreviewBuildRow(
|
||||
build, index, history.installedIndex, me, s, u, ua,
|
||||
expanded = open == build.commit,
|
||||
) { open = if (open == build.commit) null else build.commit }
|
||||
}
|
||||
}
|
||||
val more = builds.size - shown
|
||||
if (more > 0) MoreLink(stringResource(R.string.upd_show_more, more), "preview:history:more") { shown += 20 }
|
||||
else if (shown > 5) MoreLink(stringResource(R.string.upd_less), "preview:history:less") { shown = 5 }
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PreviewBuildRow(
|
||||
build: Release, index: Int, installedIndex: Int, me: AppUpdates.Installed,
|
||||
s: FrontEndState, u: UpdatesState, ua: UpdatesActions, expanded: Boolean, onToggle: () -> Unit,
|
||||
) {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val pal = LocalPalette.current
|
||||
val src = remember { MutableInteractionSource() }
|
||||
val hot = rememberHot(src)
|
||||
val installed = index == installedIndex
|
||||
val block = AppUpdates.installBlock(build, me)
|
||||
val tooOld = block != null && build.apk != null && build.apk.versionCode < me.versionCode
|
||||
Column(
|
||||
Modifier.fillMaxWidth().clip(Shape12)
|
||||
.background(if (hot) pal.signal.copy(alpha = 0.12f) else if (expanded) colors.surfaceVariant else Color.Transparent)
|
||||
.alpha(if (tooOld && !expanded) 0.55f else 1f),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
modifier = Modifier.fillMaxWidth().paneItem("preview:${build.commit}")
|
||||
.glideBorder(hot, Shape12, pal.signal)
|
||||
.hoverable(src).clickable(interactionSource = src, indication = null, role = Role.Button, onClick = onToggle)
|
||||
.controllerConfirm(onClick = onToggle)
|
||||
.padding(horizontal = 8.dp, vertical = 6.dp),
|
||||
) {
|
||||
Column(Modifier.weight(1f)) {
|
||||
Text(
|
||||
changeTitle(build.title.ifBlank { build.shortCommit }), fontSize = 12.5.sp, fontWeight = FontWeight.SemiBold,
|
||||
color = colors.onBackground, maxLines = if (expanded) Int.MAX_VALUE else 1, overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
Text(
|
||||
listOfNotNull(ago(build.committedAt.takeIf { it > 0 } ?: build.publishedAt), build.author.takeIf { it.isNotEmpty() }?.let { "@$it" }, build.shortCommit)
|
||||
.joinToString(" · "),
|
||||
fontSize = 11.sp, color = colors.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
if (installed) Chip(stringResource(R.string.upd_installed_tag), ok = true)
|
||||
}
|
||||
if (expanded) Column(Modifier.padding(start = 8.dp, end = 8.dp, bottom = 10.dp)) {
|
||||
if (build.summary.isNotBlank()) Text(build.summary, fontSize = 12.5.sp, color = colors.onSurfaceVariant)
|
||||
ReleaseFacts(build)
|
||||
Box(Modifier.padding(top = 10.dp)) {
|
||||
when {
|
||||
installed -> Unit
|
||||
block != null -> Text(block.message(LocalContext.current), fontSize = 12.5.sp, color = AttentionAmber)
|
||||
me.updatable -> {
|
||||
val size = build.apk?.size?.takeIf { it > 0 }?.let { " · ${megabytes(it)}" }.orEmpty()
|
||||
val label = if (installedIndex in 0 until index) stringResource(R.string.upd_rollback_this, size)
|
||||
else stringResource(R.string.upd_install_this, size)
|
||||
SecondaryButton(label, enabled = u.stage == null && !s.sessionRunning, compact = true) { ua.onInstall(build) }
|
||||
}
|
||||
}
|
||||
}
|
||||
if (history.changes.size > 3 || expanded) {
|
||||
val src = remember { MutableInteractionSource() }
|
||||
val hot = rememberHot(src)
|
||||
val toggle = { expanded = !expanded }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun LegacyChanges(changes: List<AppUpdates.PreviewChange>) {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
Column(Modifier.padding(top = 3.dp), verticalArrangement = Arrangement.spacedBy(3.dp)) {
|
||||
changes.take(10).forEach { change ->
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
Text(
|
||||
if (expanded) stringResource(R.string.upd_less) else stringResource(R.string.upd_more),
|
||||
fontSize = 12.sp, fontWeight = FontWeight.SemiBold, color = pal.signal,
|
||||
modifier = Modifier.padding(top = 1.dp).offset(x = (-6).dp).paneItem("preview:history:more")
|
||||
.clip(Shape12).glideBorder(hot, Shape12, pal.signal)
|
||||
.hoverable(src).clickable(interactionSource = src, indication = null, role = Role.Button, onClick = toggle)
|
||||
.controllerConfirm(onClick = toggle)
|
||||
.padding(horizontal = 6.dp, vertical = 4.dp),
|
||||
changeTitle(change.title.ifBlank { change.commit.take(7) }),
|
||||
fontSize = 12.5.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground,
|
||||
maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f),
|
||||
)
|
||||
Text(ago(change.publishedAt), fontSize = 11.sp, color = colors.onSurfaceVariant)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun MoreLink(text: String, id: String, onClick: () -> Unit) {
|
||||
val pal = LocalPalette.current
|
||||
val src = remember { MutableInteractionSource() }
|
||||
val hot = rememberHot(src)
|
||||
Text(
|
||||
text, fontSize = 12.sp, fontWeight = FontWeight.SemiBold, color = pal.signal,
|
||||
modifier = Modifier.padding(top = 2.dp).offset(x = (-6).dp).paneItem(id)
|
||||
.clip(Shape12).glideBorder(hot, Shape12, pal.signal)
|
||||
.hoverable(src).clickable(interactionSource = src, indication = null, role = Role.Button, onClick = onClick)
|
||||
.controllerConfirm(onClick = onClick)
|
||||
.padding(horizontal = 6.dp, vertical = 4.dp),
|
||||
)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
private fun ReleaseFacts(r: Release) {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val facts = listOfNotNull(
|
||||
r.author.takeIf { it.isNotEmpty() }?.let { "@$it" },
|
||||
stringResource(R.string.upd_version_code, r.versionCode),
|
||||
).joinToString(" · ")
|
||||
Text(facts, fontSize = 12.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(top = 8.dp))
|
||||
FlowRow(Modifier.padding(top = 2.dp), horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
if (r.linkedPr > 0) LinkPill(stringResource(R.string.upd_pr, r.linkedPr), "https://github.com/Droid-Deck/DroidDeck/pull/${r.linkedPr}")
|
||||
if (r.commit.length >= 7) LinkPill(stringResource(R.string.upd_link_commit, r.shortCommit), "https://github.com/Droid-Deck/DroidDeck/commit/${r.commit}")
|
||||
if (r.url.startsWith("https://github.com/Droid-Deck/")) LinkPill(stringResource(R.string.upd_link_release), r.url)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun TestFreshness(catalog: AppUpdates.Catalog, r: Release) {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
if (r.newerCommitPushed) Text(
|
||||
stringResource(R.string.upd_newer_commit, r.headCommit.take(7)), fontSize = 12.5.sp, color = AttentionAmber, modifier = Modifier.padding(top = 8.dp),
|
||||
)
|
||||
val behind = AppUpdates.previewsSince(catalog, r)
|
||||
if (behind > 0) Text(
|
||||
stringResource(R.string.upd_main_moved, behind), fontSize = 12.5.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(top = 4.dp),
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun LinkPill(label: String, url: String) {
|
||||
val ctx = LocalContext.current
|
||||
val pal = LocalPalette.current
|
||||
val src = remember { MutableInteractionSource() }
|
||||
val hot = rememberHot(src)
|
||||
val open = { openUrl(ctx, url) }
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(5.dp),
|
||||
modifier = Modifier.paneItem("link:$url").heightIn(min = 36.dp).offset(x = (-8).dp).clip(Shape12)
|
||||
.background(if (hot) pal.signal.copy(alpha = 0.14f) else Color.Transparent)
|
||||
.glideBorder(hot, Shape12, pal.signal)
|
||||
.hoverable(src).clickable(interactionSource = src, indication = null, role = Role.Button, onClick = open)
|
||||
.controllerConfirm(onClick = open).padding(horizontal = 8.dp),
|
||||
) {
|
||||
val tint = pal.signal
|
||||
Text(label, fontSize = 12.sp, fontWeight = FontWeight.Medium, color = tint, maxLines = 1)
|
||||
Icon(Icons.AutoMirrored.Filled.OpenInNew, contentDescription = null, tint = tint.copy(alpha = 0.65f), modifier = Modifier.size(11.dp))
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun TestList(tests: List<Release>, me: AppUpdates.Installed, u: UpdatesState, ua: UpdatesActions) {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
var query by rememberSaveable { mutableStateOf("") }
|
||||
val needle = query.trim().removePrefix("#").removePrefix("@")
|
||||
val matches = tests.filter {
|
||||
needle.isEmpty() || it.pr.toString().startsWith(needle) || it.title.contains(needle, ignoreCase = true) ||
|
||||
it.author.contains(needle, ignoreCase = true) || (needle.length >= 4 && it.commit.startsWith(needle, ignoreCase = true))
|
||||
}
|
||||
val (ready, old) = matches.partition { it.versionCode >= me.versionCode }
|
||||
val followedOld = u.follow.channel == Channel.TEST && old.any { it.pr == u.follow.pr }
|
||||
var showOld by rememberSaveable { mutableStateOf(false) }
|
||||
Column(Modifier.padding(start = 18.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
if (tests.size > 5 || query.isNotEmpty()) AdbTextField(
|
||||
value = query, onValueChange = { query = it }, label = stringResource(R.string.upd_tests_find),
|
||||
keyboardType = KeyboardType.Text, imeAction = ImeAction.Search, compact = true, showLabel = false,
|
||||
placeholder = stringResource(R.string.upd_tests_find), modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
if (matches.isEmpty()) Text(stringResource(R.string.upd_tests_no_match), fontSize = 12.5.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(4.dp))
|
||||
ready.forEach { t -> TestRow(t, u.follow.pr == t.pr && u.follow.channel == Channel.TEST) { ua.onFollow(Follow(Channel.TEST, t.pr)) } }
|
||||
if (old.isNotEmpty()) {
|
||||
MoreLink(stringResource(R.string.upd_tests_old, old.size, me.versionCode), "tests:old") { showOld = !showOld }
|
||||
if (showOld || followedOld || needle.isNotEmpty()) old.forEach { t ->
|
||||
TestRow(t, u.follow.pr == t.pr && u.follow.channel == Channel.TEST, tooOld = true) { ua.onFollow(Follow(Channel.TEST, t.pr)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -404,7 +586,7 @@ private fun RadioDot(selected: Boolean, size: androidx.compose.ui.unit.Dp = 22.d
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun TestRow(t: Release, selected: Boolean, onClick: () -> Unit) {
|
||||
private fun TestRow(t: Release, selected: Boolean, tooOld: Boolean = false, onClick: () -> Unit) {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val pal = LocalPalette.current
|
||||
val src = remember { MutableInteractionSource() }
|
||||
@@ -417,6 +599,7 @@ private fun TestRow(t: Release, selected: Boolean, onClick: () -> Unit) {
|
||||
.clip(Shape12)
|
||||
.background(colors.surface).background(if (hot) pal.signal.copy(alpha = 0.14f) else Color.Transparent)
|
||||
.glideBorder(hot, Shape12, pal.signal, pal.line)
|
||||
.alpha(if (tooOld && !selected) 0.6f else 1f)
|
||||
.hoverable(src)
|
||||
.clickable(interactionSource = src, indication = null, role = Role.RadioButton, onClick = onClick)
|
||||
.controllerConfirm(onClick = onClick)
|
||||
@@ -424,13 +607,29 @@ private fun TestRow(t: Release, selected: Boolean, onClick: () -> Unit) {
|
||||
) {
|
||||
Text("#${t.pr}", fontSize = 13.sp, fontWeight = FontWeight.Bold, color = if (selected) pal.signal else colors.onSurfaceVariant)
|
||||
Column(Modifier.weight(1f)) {
|
||||
Text(t.title.ifBlank { stringResource(R.string.upd_pr, t.pr) }, fontSize = 13.5.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis)
|
||||
Text(if (t.apk == null) stringResource(R.string.upd_not_built) else stringResource(R.string.upd_updated, ago(t.publishedAt)), fontSize = 12.sp, color = colors.onSurfaceVariant)
|
||||
Text(changeTitle(t.title.ifBlank { stringResource(R.string.upd_pr, t.pr) }), fontSize = 13.5.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis)
|
||||
val detail = when {
|
||||
t.apk == null -> stringResource(R.string.upd_not_built)
|
||||
tooOld -> stringResource(R.string.upd_tests_old_row, t.versionCode)
|
||||
else -> stringResource(R.string.upd_updated, ago(t.builtAt.takeIf { it > 0 } ?: t.publishedAt))
|
||||
}
|
||||
Text(
|
||||
listOfNotNull(detail, t.author.takeIf { it.isNotEmpty() }?.let { "@$it" }, t.shortCommit).joinToString(" · "),
|
||||
fontSize = 12.sp, color = colors.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
if (t.newerCommitPushed) Text(stringResource(R.string.upd_newer_commit_short), fontSize = 12.sp, color = AttentionAmber, maxLines = 1, overflow = TextOverflow.Ellipsis)
|
||||
}
|
||||
RadioDot(selected, 20.dp)
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildDetails(me: AppUpdates.Installed, label: String): String = buildString {
|
||||
append("DroidDeck ").append(me.version).append(" (versionCode ").append(me.versionCode).append(")\n")
|
||||
append(label)
|
||||
if (me.pr > 0) append("\nPR #").append(me.pr).append(" https://github.com/Droid-Deck/DroidDeck/pull/").append(me.pr)
|
||||
if (me.commit.length >= 7) append("\nCommit ").append(me.commit)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun channelName(f: Follow) = when (f.channel) {
|
||||
Channel.STABLE -> stringResource(R.string.upd_stable)
|
||||
|
||||
@@ -18,7 +18,7 @@ object AppUpdates {
|
||||
private const val TAG = "AppUpdates"
|
||||
private const val REPO = "Droid-Deck/DroidDeck"
|
||||
private const val CI_REPO = "Droid-Deck/DroidDeck-CI"
|
||||
private const val CATALOG_URL = "https://raw.githubusercontent.com/$CI_REPO/main/catalog.json"
|
||||
private val CATALOG_URL = "https://raw.githubusercontent.com/$CI_REPO/${BuildConfig.UPDATE_CATALOG_BRANCH}/catalog.json"
|
||||
private const val PREFS = "app_updates"
|
||||
private const val KEY_CATALOG = "catalog"
|
||||
private const val KEY_CHANNEL = "channel"
|
||||
@@ -49,7 +49,21 @@ object AppUpdates {
|
||||
val publishedAt: Long,
|
||||
val url: String,
|
||||
val apk: Apk?,
|
||||
)
|
||||
val author: String = "",
|
||||
val committedAt: Long = 0L,
|
||||
/** The PR a Preview build merged, 0 for a direct push. Test builds use [pr]. */
|
||||
val mergedPr: Int = 0,
|
||||
/** The PR's newest commit when the catalog was made; differs from [commit] while a rebuild is pending. */
|
||||
val headCommit: String = "",
|
||||
/** When the APK itself was uploaded; release-note edits leave it unchanged. */
|
||||
val builtAt: Long = 0L,
|
||||
) {
|
||||
val linkedPr: Int get() = if (pr > 0) pr else mergedPr
|
||||
val shortCommit: String get() = commit.take(7)
|
||||
val newerCommitPushed: Boolean get() =
|
||||
headCommit.length >= 7 && !headCommit.startsWith(commit, ignoreCase = true) &&
|
||||
!commit.startsWith(headCommit, ignoreCase = true)
|
||||
}
|
||||
|
||||
class PreviewChange(
|
||||
val commit: String,
|
||||
@@ -64,7 +78,11 @@ object AppUpdates {
|
||||
val tests: List<Release>,
|
||||
val checkedAt: Long,
|
||||
val recentPreviews: List<PreviewChange> = emptyList(),
|
||||
)
|
||||
val previews: List<Release> = emptyList(),
|
||||
) {
|
||||
/** Every published Preview build, newest commit first; older catalogs only name the newest. */
|
||||
val previewBuilds: List<Release> get() = previews.ifEmpty { listOfNotNull(preview) }
|
||||
}
|
||||
|
||||
class Installed(
|
||||
val commit: String,
|
||||
@@ -88,6 +106,8 @@ object AppUpdates {
|
||||
val kind: PreviewHistoryKind,
|
||||
val buildsBehind: Int,
|
||||
val changes: List<PreviewChange>,
|
||||
val builds: List<Release> = emptyList(),
|
||||
val installedIndex: Int = -1,
|
||||
)
|
||||
|
||||
enum class Offer {
|
||||
@@ -149,7 +169,13 @@ object AppUpdates {
|
||||
compareVersions(r.version ?: "", me.version) > 0 -> Offer.UPDATE
|
||||
else -> Offer.AHEAD
|
||||
}
|
||||
Channel.NIGHTLY -> if (me.pr != 0) Offer.SWITCH else Offer.UPDATE
|
||||
Channel.NIGHTLY -> when {
|
||||
me.pr != 0 -> Offer.SWITCH
|
||||
previewIndex(catalog, me) > 0 -> Offer.UPDATE
|
||||
previewIndex(catalog, me) < 0 && me.committedAt > 0 && r.committedAt > 0 &&
|
||||
me.committedAt > r.committedAt -> Offer.AHEAD
|
||||
else -> Offer.UPDATE
|
||||
}
|
||||
Channel.TEST -> if (me.pr == r.pr) Offer.UPDATE else Offer.SWITCH
|
||||
}
|
||||
}
|
||||
@@ -162,12 +188,39 @@ object AppUpdates {
|
||||
}
|
||||
|
||||
fun isRunning(r: Release, me: Installed = installed()): Boolean {
|
||||
if (me.commit.length < 7 || r.commit.length < 7 || r.pr != me.pr) return false
|
||||
if (me.commit.length < 7 || r.commit.length < 7 || r.pr != me.pr || r.versionCode > me.versionCode) return false
|
||||
return me.commit.startsWith(r.commit, ignoreCase = true) ||
|
||||
r.commit.startsWith(me.commit, ignoreCase = true)
|
||||
}
|
||||
|
||||
/** Where the installed build sits in [Catalog.previewBuilds], or -1 when it is not one of them. */
|
||||
fun previewIndex(catalog: Catalog, me: Installed = installed()): Int {
|
||||
if (me.pr != 0) return -1
|
||||
val builds = catalog.previewBuilds
|
||||
val matching = builds.indices.filter { matchesInstalledCommit(builds[it].commit, me.commit) }
|
||||
return matching.singleOrNull() ?: -1
|
||||
}
|
||||
|
||||
/** How many Preview builds landed on main after this test build was made. */
|
||||
fun previewsSince(catalog: Catalog, test: Release): Int =
|
||||
catalog.previews.count {
|
||||
(it.committedAt.takeIf { at -> at > 0 } ?: it.publishedAt) > (test.builtAt.takeIf { at -> at > 0 } ?: test.publishedAt)
|
||||
}
|
||||
|
||||
fun installable(r: Release, me: Installed = installed()): Boolean =
|
||||
me.updatable && installBlock(r, me) == null && !isRunning(r, me)
|
||||
|
||||
fun previewHistory(catalog: Catalog, me: Installed = installed()): PreviewHistory? {
|
||||
if (catalog.previews.isNotEmpty()) {
|
||||
val builds = catalog.previews
|
||||
val index = previewIndex(catalog, me)
|
||||
val kind = when {
|
||||
index == 0 -> PreviewHistoryKind.CURRENT
|
||||
index > 0 -> PreviewHistoryKind.BEHIND
|
||||
else -> PreviewHistoryKind.RECENT
|
||||
}
|
||||
return PreviewHistory(kind, maxOf(index, 0), emptyList(), builds, index)
|
||||
}
|
||||
if (!me.ciBuild || catalog.recentPreviews.isEmpty()) return null
|
||||
val stable = catalog.stable
|
||||
if (
|
||||
@@ -225,7 +278,8 @@ object AppUpdates {
|
||||
if (stable != null && isRunning(stable, me)) return Guess(Follow(Channel.STABLE), true)
|
||||
val publishedPreview = catalog != null && me.updatable && (
|
||||
catalog.preview?.let { isRunning(it, me) } == true ||
|
||||
catalog.recentPreviews.any { matchesInstalledCommit(it.commit, me.commit) }
|
||||
catalog.recentPreviews.any { matchesInstalledCommit(it.commit, me.commit) } ||
|
||||
catalog.previewBuilds.any { matchesInstalledCommit(it.commit, me.commit) }
|
||||
)
|
||||
if (stable?.version?.let { compareVersions(me.version, it) < 0 } == true) return Guess(Follow(Channel.STABLE), !publishedPreview)
|
||||
return Guess(Follow(if (publishedPreview) Channel.NIGHTLY else Channel.STABLE), false)
|
||||
@@ -237,7 +291,7 @@ object AppUpdates {
|
||||
prefs(context).edit().putString(KEY_CHANNEL, value).apply()
|
||||
}
|
||||
|
||||
private fun stored(context: Context): Follow? {
|
||||
fun stored(context: Context): Follow? {
|
||||
val v = prefs(context).getString(KEY_CHANNEL, null) ?: return null
|
||||
return when {
|
||||
v == "stable" -> Follow(Channel.STABLE)
|
||||
@@ -281,6 +335,7 @@ object AppUpdates {
|
||||
}
|
||||
val tests = root.optJSONArray("tests") ?: JSONArray()
|
||||
val recentPreviews = root.optJSONArray("recentPreviews") ?: JSONArray()
|
||||
val previews = root.optJSONArray("previews") ?: JSONArray()
|
||||
return Catalog(
|
||||
root.optJSONObject("stable")?.let { readPublishedRelease(it, packageName) },
|
||||
root.optJSONObject("preview")?.let { readPublishedRelease(it, packageName) },
|
||||
@@ -294,6 +349,10 @@ object AppUpdates {
|
||||
.sortedByDescending { it.publishedAt }
|
||||
.distinctBy { it.commit.lowercase() }
|
||||
.take(10),
|
||||
(0 until previews.length())
|
||||
.map { readPublishedRelease(previews.getJSONObject(it), packageName) }
|
||||
.filter { it.pr == 0 }
|
||||
.distinctBy { it.commit.lowercase() },
|
||||
)
|
||||
}
|
||||
|
||||
@@ -327,6 +386,11 @@ object AppUpdates {
|
||||
o.optLong("publishedAt"),
|
||||
o.optString("url"),
|
||||
apk,
|
||||
o.optString("author").takeIf { Regex("^[A-Za-z0-9-]{1,39}(\\[bot\\])?$").matches(it) }.orEmpty(),
|
||||
o.optLong("committedAt"),
|
||||
o.optInt("mergedPr").coerceAtLeast(0),
|
||||
o.optString("headCommit").takeIf { Regex("^[0-9a-fA-F]{7,40}$").matches(it) }.orEmpty(),
|
||||
o.optLong("builtAt"),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -425,6 +489,11 @@ object AppUpdates {
|
||||
.put("publishedAt", r.publishedAt)
|
||||
.put("url", r.url)
|
||||
.put("apk", r.apk?.let(::writeApk))
|
||||
.put("author", r.author)
|
||||
.put("committedAt", r.committedAt)
|
||||
.put("mergedPr", r.mergedPr)
|
||||
.put("headCommit", r.headCommit)
|
||||
.put("builtAt", r.builtAt)
|
||||
|
||||
private fun readCachedRelease(o: JSONObject): Release = Release(
|
||||
o.getString("tag"),
|
||||
@@ -447,6 +516,11 @@ object AppUpdates {
|
||||
it.getString("signerSha256"),
|
||||
)
|
||||
},
|
||||
o.optString("author"),
|
||||
o.optLong("committedAt"),
|
||||
o.optInt("mergedPr"),
|
||||
o.optString("headCommit"),
|
||||
o.optLong("builtAt"),
|
||||
)
|
||||
|
||||
internal fun writeCatalog(c: Catalog): JSONObject = JSONObject()
|
||||
@@ -454,6 +528,7 @@ object AppUpdates {
|
||||
.put("preview", c.preview?.let(::writeRelease))
|
||||
.put("tests", JSONArray().apply { c.tests.forEach { put(writeRelease(it)) } })
|
||||
.put("recentPreviews", JSONArray().apply { c.recentPreviews.forEach { put(writePreviewChange(it)) } })
|
||||
.put("previews", JSONArray().apply { c.previews.forEach { put(writeRelease(it)) } })
|
||||
.put("checkedAt", c.checkedAt)
|
||||
|
||||
private fun writePreviewChange(c: PreviewChange): JSONObject = JSONObject()
|
||||
@@ -466,6 +541,7 @@ object AppUpdates {
|
||||
val tests = o.optJSONArray("tests") ?: JSONArray()
|
||||
val preview = o.optJSONObject("preview") ?: o.optJSONObject("nightly")
|
||||
val recentPreviews = o.optJSONArray("recentPreviews") ?: JSONArray()
|
||||
val previews = o.optJSONArray("previews") ?: JSONArray()
|
||||
return Catalog(
|
||||
o.optJSONObject("stable")?.let(::readCachedRelease),
|
||||
preview?.let(::readCachedRelease),
|
||||
@@ -476,6 +552,7 @@ object AppUpdates {
|
||||
.sortedByDescending { it.publishedAt }
|
||||
.distinctBy { it.commit.lowercase() }
|
||||
.take(10),
|
||||
(0 until previews.length()).map { readCachedRelease(previews.getJSONObject(it)) },
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.droiddeck.launcher.update
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.app.job.JobInfo
|
||||
import android.app.job.JobParameters
|
||||
import android.app.job.JobScheduler
|
||||
import android.app.job.JobService
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.droiddeck.launcher.FrontEndActivity
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.stores.download.StoreDownloadService
|
||||
|
||||
/** Checks the catalog in the background while a PR test build is followed, and says when that PR has a new build. */
|
||||
class UpdateWatch : JobService() {
|
||||
override fun onStartJob(params: JobParameters): Boolean {
|
||||
Thread({
|
||||
runCatching { check(applicationContext) }
|
||||
jobFinished(params, false)
|
||||
}, "update-watch").start()
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onStopJob(params: JobParameters): Boolean = true
|
||||
|
||||
companion object {
|
||||
private const val JOB_ID = 41_230
|
||||
private const val NOTIFICATION_ID = 41_231
|
||||
private const val CHANNEL_ID = "test-builds"
|
||||
private const val PREFS = "app_updates"
|
||||
private const val KEY_SEEN = "watch_seen"
|
||||
private const val INTERVAL_MS = 60 * 60 * 1000L
|
||||
|
||||
fun sync(context: Context, follow: AppUpdates.Follow, current: AppUpdates.Release? = null) {
|
||||
val scheduler = context.getSystemService(JobScheduler::class.java) ?: return
|
||||
if (follow.channel != AppUpdates.Channel.TEST) {
|
||||
scheduler.cancel(JOB_ID)
|
||||
return
|
||||
}
|
||||
current?.let { seen(context, it) }
|
||||
if (scheduler.getPendingJob(JOB_ID) != null) return
|
||||
val job = JobInfo.Builder(JOB_ID, ComponentName(context, UpdateWatch::class.java))
|
||||
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
|
||||
.setPeriodic(INTERVAL_MS)
|
||||
.build()
|
||||
runCatching { scheduler.schedule(job) }
|
||||
}
|
||||
|
||||
fun seen(context: Context, release: AppUpdates.Release) {
|
||||
prefs(context).edit().putString(KEY_SEEN, key(release)).apply()
|
||||
}
|
||||
|
||||
private fun key(r: AppUpdates.Release) = "${r.pr}:${r.commit}:${r.versionCode}:${r.apk?.sha256.orEmpty()}"
|
||||
|
||||
|
||||
internal fun check(context: Context) {
|
||||
val catalog = AppUpdates.refresh(context)
|
||||
val follow = AppUpdates.follow(context, catalog)
|
||||
if (follow.channel != AppUpdates.Channel.TEST) return
|
||||
val release = AppUpdates.release(catalog, follow) ?: return
|
||||
if (prefs(context).getString(KEY_SEEN, null) == key(release)) return
|
||||
val me = AppUpdates.installed()
|
||||
val offer = AppUpdates.offer(catalog, follow, me)
|
||||
if (!me.updatable || AppUpdates.installBlock(release, me) != null ||
|
||||
(offer != AppUpdates.Offer.UPDATE && offer != AppUpdates.Offer.SWITCH)
|
||||
) return
|
||||
if (post(context, release)) seen(context, release)
|
||||
}
|
||||
|
||||
private fun post(context: Context, release: AppUpdates.Release): Boolean {
|
||||
val manager = context.getSystemService(NotificationManager::class.java) ?: return false
|
||||
manager.createNotificationChannel(
|
||||
NotificationChannel(CHANNEL_ID, context.getString(R.string.upd_watch_channel), NotificationManager.IMPORTANCE_DEFAULT),
|
||||
)
|
||||
if (!manager.areNotificationsEnabled() ||
|
||||
manager.getNotificationChannel(CHANNEL_ID)?.importance == NotificationManager.IMPORTANCE_NONE
|
||||
) return false
|
||||
val open = PendingIntent.getActivity(
|
||||
context, NOTIFICATION_ID,
|
||||
Intent(context, FrontEndActivity::class.java).putExtra(StoreDownloadService.EXTRA_NAV, "updates")
|
||||
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
|
||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
||||
)
|
||||
val text = context.getString(R.string.upd_watch_text, release.title.ifBlank { release.commit.take(7) })
|
||||
val notification = Notification.Builder(context, CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_stat_session)
|
||||
.setContentTitle(context.getString(R.string.upd_watch_title, release.pr))
|
||||
.setContentText(text)
|
||||
.setStyle(Notification.BigTextStyle().bigText(text))
|
||||
.setContentIntent(open)
|
||||
.setAutoCancel(true)
|
||||
.build()
|
||||
return runCatching { manager.notify(NOTIFICATION_ID, notification) }.isSuccess
|
||||
}
|
||||
|
||||
private fun prefs(context: Context) = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user