Merge branch 'main' into docs/progress-log-0928

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-01 00:04:08 -04:00
commit ca1ceb5e28
18 files changed
+1246 -226

No files matched your search

+13 -1
View File
@@ -387,7 +387,11 @@ jobs:
success) success)
echo "### Build passed" echo "### Build passed"
echo echo
echo "The \`droiddeck-apk\` artifact is [on this run]($RUN_URL), signed with the public test key. A maintainer can run **PR Release Publish** from \`main\` to create an APK that installs over the release." echo "The \`droiddeck-apk\` artifact is [on this run]($RUN_URL), signed with the public test key."
echo
echo "**Maintainer:** add the \`publish-test\` label to publish a release-signed tester build that installs over Stable or Preview and appears in DroidDeck's **Test builds** list. The label is removed automatically after publishing."
echo
echo "Fallback: [open **PR Release Publish**]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/workflows/pr-release-publish.yml) on \`main\` and enter PR \`$PR\`."
;; ;;
failure) failure)
echo "### ERROR: Build failed" echo "### ERROR: Build failed"
@@ -515,6 +519,8 @@ jobs:
notes="$(cat notes-head.md) notes="$(cat notes-head.md)
Signed build of $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/commit/$GITHUB_SHA - in main, not yet in a DroidDeck release; for testing. Signed build of $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/commit/$GITHUB_SHA - in main, not yet in a DroidDeck release; for testing.
Commit: $GITHUB_SHA
VersionCode: $(sed -n 's/^ *versionCode \([0-9][0-9]*\)$/\1/p' app/build.gradle)
Each apk installs over a DroidDeck release and keeps its data. \`standard\` is the normal app; the others are the same app under another package name, installed beside it. Each apk installs over a DroidDeck release and keeps its data. \`standard\` is the normal app; the others are the same app under another package name, installed beside it.
SHA-256: SHA-256:
@@ -576,3 +582,9 @@ jobs:
jq -n --arg content "$content" '{content: $content, allowed_mentions: {parse: []}}' | jq -n --arg content "$content" '{content: $content, allowed_mentions: {parse: []}}' |
curl -fsS -H 'Content-Type: application/json' -d @- "$WEBHOOK" -o /dev/null curl -fsS -H 'Content-Type: application/json' -d @- "$WEBHOOK" -o /dev/null
echo "Posted: $content" echo "Posted: $content"
catalog:
needs: announce
if: ${{ always() && github.event_name != 'pull_request' && github.ref == 'refs/heads/main' && needs.announce.result != 'skipped' }}
uses: ./.github/workflows/update-catalog.yml
secrets: inherit
+19 -9
View File
@@ -2,14 +2,16 @@ name: PR Release Cleanup
run-name: remove PR #${{ github.event.pull_request.number }}'s test build run-name: remove PR #${{ github.event.pull_request.number }}'s test build
# A merged or closed PR's test build leaves DroidDeck-CI, so the app's Updates page stops offering it # A merged or closed PR's test build leaves DroidDeck-CI, so the app's Updates page stops offering it
# (a merged fix is in the next Nightly). pull_request_target for the publisher app's secrets; nothing # (a merged fix is in the next Preview). pull_request_target for the publisher app's secrets; nothing
# from the PR is checked out or run. # from the PR is checked out or run.
on: on:
pull_request_target: pull_request_target:
types: [ closed ] types: [ closed ]
branches: [ main ] branches: [ main ]
permissions: {} permissions:
contents: read
pull-requests: read
jobs: jobs:
cleanup: cleanup:
@@ -30,10 +32,18 @@ jobs:
PR_NUMBER: ${{ github.event.pull_request.number }} PR_NUMBER: ${{ github.event.pull_request.number }}
run: | run: |
set -euo pipefail set -euo pipefail
tag="pr-$PR_NUMBER" found=0
if gh release view "$tag" -R Droid-Deck/DroidDeck-CI >/dev/null 2>&1; then for tag in "pr-$PR_NUMBER" "pr-$PR_NUMBER.0"; do
gh release delete "$tag" -R Droid-Deck/DroidDeck-CI --yes --cleanup-tag if gh release view "$tag" -R Droid-Deck/DroidDeck-CI >/dev/null 2>&1; then
echo "Deleted $tag" gh release delete "$tag" -R Droid-Deck/DroidDeck-CI --yes --cleanup-tag
else echo "Deleted $tag"
echo "No test build for PR #$PR_NUMBER" found=1
fi fi
done
[ "$found" = 1 ] || echo "No test build for PR #$PR_NUMBER"
catalog:
needs: cleanup
if: ${{ always() }}
uses: ./.github/workflows/update-catalog.yml
secrets: inherit
+116 -11
View File
@@ -1,30 +1,39 @@
name: PR Release Publish name: PR Release Publish
run-name: sign PR #${{ inputs.pr_number }} with the release key run-name: "sign PR #${{ github.event_name == 'pull_request_target' && github.event.pull_request.number || inputs.pr_number }} with the release key"
on: on:
workflow_dispatch: workflow_dispatch:
inputs: inputs:
pr_number: pr_number:
description: Open pull request description: Open pull request number
required: true required: true
type: number type: string
pull_request_target:
types: [labeled]
branches: [main]
permissions: permissions:
actions: read actions: read
contents: read contents: read
pull-requests: read issues: write
pull-requests: write
concurrency: concurrency:
group: pr-release-${{ inputs.pr_number }} group: pr-release-${{ github.event_name == 'pull_request_target' && github.event.pull_request.number || inputs.pr_number }}
cancel-in-progress: false cancel-in-progress: false
jobs: jobs:
authorize: authorize:
if: github.ref == 'refs/heads/main' if: >-
(github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main') ||
(github.event_name == 'pull_request_target' && github.event.label.name == 'publish-test')
runs-on: ubuntu-24.04 runs-on: ubuntu-24.04
outputs: outputs:
pr_number: ${{ steps.gate.outputs.pr_number }}
run_id: ${{ steps.gate.outputs.run_id }} run_id: ${{ steps.gate.outputs.run_id }}
head_sha: ${{ steps.gate.outputs.head_sha }} head_sha: ${{ steps.gate.outputs.head_sha }}
base_sha: ${{ steps.gate.outputs.base_sha }}
version_code: ${{ steps.gate.outputs.version_code }}
label: ${{ steps.gate.outputs.label }} label: ${{ steps.gate.outputs.label }}
variants: ${{ steps.variants.outputs.list }} variants: ${{ steps.variants.outputs.list }}
steps: steps:
@@ -35,13 +44,23 @@ jobs:
- name: The packages a signed build ships as - name: The packages a signed build ships as
id: variants id: variants
run: echo "list=$(awk '!/^#/ && NF { print $1 }' tools/release/variants.txt | jq -R . | jq -sc .)" >> "$GITHUB_OUTPUT" run: echo "list=$(awk '!/^#/ && NF { print $1 }' tools/release/variants.txt | jq -R . | jq -sc .)" >> "$GITHUB_OUTPUT"
- name: Check the PR and build - name: Check the requester, PR, and build
id: gate id: gate
env: env:
GH_TOKEN: ${{ github.token }} GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ inputs.pr_number }} EVENT_NAME: ${{ github.event_name }}
REQUESTER: ${{ github.actor }}
PR_NUMBER: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.number || inputs.pr_number }}
run: | run: |
set -euo pipefail set -euo pipefail
[[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]] || { echo "::error::Invalid PR number '$PR_NUMBER'"; exit 1; }
if [ "$EVENT_NAME" = pull_request_target ]; then
permission=$(gh api "repos/$GITHUB_REPOSITORY/collaborators/$REQUESTER/permission" --jq .permission)
case "$permission" in
admin|maintain|write) ;;
*) echo "::error::@$REQUESTER does not have permission to publish release-signed test builds"; exit 1 ;;
esac
fi
pr=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER") pr=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER")
jq -e '.state == "open" and .draft == false and .base.ref == "main"' <<<"$pr" >/dev/null jq -e '.state == "open" and .draft == false and .base.ref == "main"' <<<"$pr" >/dev/null
head=$(jq -r '.head.sha' <<<"$pr") head=$(jq -r '.head.sha' <<<"$pr")
@@ -85,8 +104,11 @@ jobs:
.head.sha == $head and .base.sha == $base .head.sha == $head and .base.sha == $base
' <<<"$current" >/dev/null ' <<<"$current" >/dev/null
{ {
echo "pr_number=$PR_NUMBER"
echo "run_id=$run_id" echo "run_id=$run_id"
echo "head_sha=$head" echo "head_sha=$head"
echo "base_sha=$base"
echo "version_code=${candidate##* }"
echo "label=PR.$PR_NUMBER-${head:0:7}" echo "label=PR.$PR_NUMBER-${head:0:7}"
} >> "$GITHUB_OUTPUT" } >> "$GITHUB_OUTPUT"
@@ -107,7 +129,8 @@ jobs:
permissions: permissions:
actions: read actions: read
contents: read contents: read
pull-requests: read issues: write
pull-requests: write
steps: steps:
- uses: actions/download-artifact@v4 - uses: actions/download-artifact@v4
with: with:
@@ -119,7 +142,7 @@ jobs:
- name: Describe the change - name: Describe the change
env: env:
GH_TOKEN: ${{ github.token }} GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ inputs.pr_number }} PR_NUMBER: ${{ needs.authorize.outputs.pr_number }}
run: | run: |
set -euo pipefail set -euo pipefail
pr=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER") pr=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER")
@@ -143,10 +166,13 @@ jobs:
owner: Droid-Deck owner: Droid-Deck
repositories: DroidDeck-CI repositories: DroidDeck-CI
- name: Publish to DroidDeck-CI - name: Publish to DroidDeck-CI
id: publish
env: env:
GH_TOKEN: ${{ steps.ci-token.outputs.token }} GH_TOKEN: ${{ steps.ci-token.outputs.token }}
PR_NUMBER: ${{ inputs.pr_number }} PR_NUMBER: ${{ needs.authorize.outputs.pr_number }}
HEAD_SHA: ${{ needs.authorize.outputs.head_sha }} HEAD_SHA: ${{ needs.authorize.outputs.head_sha }}
BASE_SHA: ${{ needs.authorize.outputs.base_sha }}
VERSION_CODE: ${{ needs.authorize.outputs.version_code }}
run: | run: |
set -euo pipefail set -euo pipefail
repo=Droid-Deck/DroidDeck-CI repo=Droid-Deck/DroidDeck-CI
@@ -157,6 +183,8 @@ jobs:
Source: https://github.com/$GITHUB_REPOSITORY/pull/$PR_NUMBER Source: https://github.com/$GITHUB_REPOSITORY/pull/$PR_NUMBER
Commit: $HEAD_SHA Commit: $HEAD_SHA
Base: $BASE_SHA
VersionCode: $VERSION_CODE
Each apk installs over a DroidDeck release and keeps its data. \`standard\` is the normal app; the others are the same app under another package name, installed beside it. Each apk installs over a DroidDeck release and keeps its data. \`standard\` is the normal app; the others are the same app under another package name, installed beside it.
SHA-256: SHA-256:
@@ -175,3 +203,80 @@ jobs:
while IFS= read -r id; do while IFS= read -r id; do
gh api --method DELETE "repos/$repo/releases/assets/$id" >/dev/null gh api --method DELETE "repos/$repo/releases/assets/$id" >/dev/null
done done
published=$(gh release view "$tag" -R "$repo" --json tagName,assets)
jq -e --arg tag "$tag" '.tagName == $tag and (.assets | length) > 0' <<<"$published" >/dev/null
release="$GITHUB_SERVER_URL/$repo/releases/tag/$tag"
apk=$(cd signed && ls DroidDeck-*.apk | awk 'length(a) == 0 || length($0) < length(a) { a = $0 } END { print a }')
echo "release=$release" >> "$GITHUB_OUTPUT"
echo "apk=$GITHUB_SERVER_URL/$repo/releases/download/$tag/$apk" >> "$GITHUB_OUTPUT"
- name: Clear the publish request label
if: ${{ github.event_name == 'pull_request_target' && steps.publish.outcome == 'success' }}
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ needs.authorize.outputs.pr_number }}
run: |
gh api --method DELETE "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/labels/publish-test"
- name: Report the signed build on the PR
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ needs.authorize.outputs.pr_number }}
HEAD_SHA: ${{ needs.authorize.outputs.head_sha }}
RELEASE: ${{ steps.publish.outputs.release }}
APK: ${{ steps.publish.outputs.apk }}
run: |
set -euo pipefail
short=${HEAD_SHA:0:7}
mark='<!-- droiddeck-pr-release -->'
body=$(cat <<EOF
$mark
## Signed test build
A release-signed build of this PR at \`$short\` is ready. It installs over Stable or Preview and keeps app data.
[Download the standard APK]($APK) · [All variants and checksums]($RELEASE)
It is also available in **DroidDeck → Updates → Test builds → PR #$PR_NUMBER**.
Pushes to this PR do not re-sign automatically. After a new green build, a maintainer can add the \`publish-test\` label again.
EOF
)
id=$(gh api "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" --paginate \
--jq ".[] | select(.body | startswith(\"$mark\")) | .id" | head -1 || true)
if [ -n "$id" ]; then
gh api -X PATCH "repos/$GITHUB_REPOSITORY/issues/comments/$id" -f body="$body" >/dev/null
else
gh api -X POST "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" -f body="$body" >/dev/null
fi
- name: Post to Discord
if: ${{ always() && steps.publish.outcome == 'success' }}
env:
WEBHOOK: ${{ secrets.DISCORD_BUILDS_WEBHOOK }}
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ needs.authorize.outputs.pr_number }}
HEAD_SHA: ${{ needs.authorize.outputs.head_sha }}
RELEASE: ${{ steps.publish.outputs.release }}
APK: ${{ steps.publish.outputs.apk }}
run: |
set -euo pipefail
if [ -z "$WEBHOOK" ]; then
echo "::notice::No DISCORD_BUILDS_WEBHOOK secret; nothing posted"
exit 0
fi
short=${HEAD_SHA:0:7}
title=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq .title)
what="[#$PR_NUMBER]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$PR_NUMBER): $title"
get=$(printf 'Download: [standard APK](<%s>) · [all variants](<%s>)' "$APK" "$RELEASE")
content=$(printf '**New signed PR test build** \`pr-%s\` · \`%s\`\n%s\n_Not merged — for testing._\n%s' "$PR_NUMBER" "$short" "$what" "$get")
jq -n --arg content "$content" '{content: $content, allowed_mentions: {parse: []}}' |
curl -fsS -H 'Content-Type: application/json' -d @- "$WEBHOOK" -o /dev/null
echo "Posted: $content"
catalog:
needs: publish
if: ${{ always() && needs.publish.result != 'skipped' }}
uses: ./.github/workflows/update-catalog.yml
secrets: inherit
+42
View File
@@ -0,0 +1,42 @@
name: Update app catalog
on:
workflow_call:
workflow_dispatch:
release:
types: [published, edited, deleted, unpublished]
permissions:
contents: read
pull-requests: read
concurrency:
group: update-app-catalog
cancel-in-progress: false
jobs:
catalog:
runs-on: ubuntu-24.04
timeout-minutes: 5
steps:
# Always use the current trusted generator, even when a release event points at an older tag.
- uses: actions/checkout@v4
with:
ref: main
persist-credentials: false
- uses: actions/create-github-app-token@v2
id: ci-token
with:
app-id: ${{ vars.DROIDDECK_CI_APP_ID }}
private-key: ${{ secrets.DROIDDECK_CI_APP_KEY }}
owner: Droid-Deck
repositories: DroidDeck-CI
- name: Regenerate catalog.json
env:
SOURCE_GITHUB_TOKEN: ${{ github.token }}
CI_GITHUB_TOKEN: ${{ steps.ci-token.outputs.token }}
run: |
set -euo pipefail
python3 tools/release/update_catalog.py
+2 -2
View File
@@ -13,13 +13,13 @@ DroidDeck brings the SteamOS experience to Android: Valve's Steam client in Big
## Requirements and install ## Requirements and install
Use Android 9 or newer on a supported Adreno device (730 or newer, or 8xx). Mali, Xclipse, PowerVR, and Adreno 710 are unsupported. No root is required. Allow about 3 GB for the runtime and 1.1 GB more for the desktop and emulators. Install the APK from [Releases](https://github.com/The412Banner/DroidDeck/releases), install the Linux runtime, then press **Play** and sign in. Steam downloads on first launch. Install **Desktop & apps** to use the desktop and emulators. The **Store** installs Linux apps and games from Flathub (ARM64 builds) with Flatpak; see [docs/development/flatpak.md](docs/development/flatpak.md). Use Android 9 or newer on a supported Adreno device (730 or newer, or 8xx). Mali, Xclipse, PowerVR, and Adreno 710 are unsupported. No root is required. Allow about 3 GB for the runtime and 1.1 GB more for the desktop and emulators. Install the APK from [Releases](https://github.com/Droid-Deck/DroidDeck/releases), install the Linux runtime, then press **Play** and sign in. Steam downloads on first launch. Install **Desktop & apps** to use the desktop and emulators. The **Store** installs Linux apps and games from Flathub (ARM64 builds) with Flatpak; see [docs/development/flatpak.md](docs/development/flatpak.md).
On Android 12+, if Steam exits without a log, turn off **Restrict child processes** in Developer options. On Android 12+, if Steam exits without a log, turn off **Restrict child processes** in Developer options.
## Community ## Community
Join the [DroidDeck Discord](https://discord.gg/JRGAvawjsm) for help, nightly builds, and device reports. Bug reports go in its **#bug-reports** forum; attach the session folder from `Download/DroidDeck/` so the logs come with it. Join the [DroidDeck Discord](https://discord.gg/JRGAvawjsm) for help, Preview builds, and device reports. Bug reports go in its **#bug-reports** forum; attach the session folder from `Download/DroidDeck/` so the logs come with it.
## Build ## Build
+3
View File
@@ -55,6 +55,8 @@ def sourceCommit = providers.environmentVariable('DROIDDECK_SOURCE_COMMIT').getO
?: providers.environmentVariable('GITHUB_SHA').getOrNull() ?: gitValue(['rev-parse', 'HEAD']) ?: '' ?: providers.environmentVariable('GITHUB_SHA').getOrNull() ?: gitValue(['rev-parse', 'HEAD']) ?: ''
def prNumber = (providers.environmentVariable('DROIDDECK_PR_NUMBER').getOrNull() ?: '0').with { it.isInteger() ? it : '0' } def prNumber = (providers.environmentVariable('DROIDDECK_PR_NUMBER').getOrNull() ?: '0').with { it.isInteger() ? it : '0' }
def ciBuild = providers.environmentVariable('GITHUB_ACTIONS').getOrNull() == 'true' def ciBuild = providers.environmentVariable('GITHUB_ACTIONS').getOrNull() == 'true'
// The release key's certificate: a build signed with it, from CI or a computer, updates in place.
def releaseSigner = rootProject.file('keystore/release-signer.sha256').text.trim()
// When the source was committed, for "built 3 hours ago": fixed per commit, so it costs no rebuilds. // When the source was committed, for "built 3 hours ago": fixed per commit, so it costs no rebuilds.
def commitTime = (gitValue(['show', '-s', '--format=%ct', 'HEAD']) ?: '0').with { it.isLong() ? it : '0' } def commitTime = (gitValue(['show', '-s', '--format=%ct', 'HEAD']) ?: '0').with { it.isLong() ? it : '0' }
// tools/release/variants.txt as "package=suffix;...", so the updater picks the apk of the package it runs as. // tools/release/variants.txt as "package=suffix;...", so the updater picks the apk of the package it runs as.
@@ -83,6 +85,7 @@ android {
buildConfigField "String", "SOURCE_COMMIT", "\"${sourceCommit}\"" buildConfigField "String", "SOURCE_COMMIT", "\"${sourceCommit}\""
buildConfigField "int", "PR_NUMBER", "${prNumber}" buildConfigField "int", "PR_NUMBER", "${prNumber}"
buildConfigField "boolean", "CI_BUILD", "${ciBuild}" buildConfigField "boolean", "CI_BUILD", "${ciBuild}"
buildConfigField "String", "RELEASE_SIGNER", "\"${releaseSigner}\""
buildConfigField "long", "COMMIT_TIME", "${commitTime}L * 1000L" buildConfigField "long", "COMMIT_TIME", "${commitTime}L * 1000L"
buildConfigField "String", "VARIANT_SUFFIXES", "\"${variantSuffixes}\"" buildConfigField "String", "VARIANT_SUFFIXES", "\"${variantSuffixes}\""
@@ -17,6 +17,7 @@ class App : Application() {
// Before anything else touches the session prefs: a new install's first read decides it. // Before anything else touches the session prefs: a new install's first read decides it.
com.droiddeck.launcher.session.SessionPrefs.settleDeckModeDefault(this) com.droiddeck.launcher.session.SessionPrefs.settleDeckModeDefault(this)
com.droiddeck.launcher.session.SessionPrefs.settleGpuDriverMode(this) com.droiddeck.launcher.session.SessionPrefs.settleGpuDriverMode(this)
com.droiddeck.launcher.update.AppUpdates.init(this)
GpuClockPin.clearLeftover(this) GpuClockPin.clearLeftover(this)
BwrapSpawner.start(this) BwrapSpawner.start(this)
} }
@@ -91,6 +91,7 @@ internal class UpdatesMenu(private val activity: Activity, private val ui: Handl
private fun install(r: AppUpdates.Release) { private fun install(r: AppUpdates.Release) {
if (stage != null) return if (stage != null) return
val apk = r.apk ?: run { error = "This build has no download for this copy of DroidDeck"; return } val apk = r.apk ?: run { error = "This build has no download for this copy of DroidDeck"; return }
AppUpdates.installBlock(r)?.let { error = it; return }
if (SessionState.running) { error = "Stop the running session first, then update"; return } if (SessionState.running) { error = "Stop the running session first, then update"; return }
if (!SelfInstaller.canInstall(activity)) { pending = r; askPermission = true; return } if (!SelfInstaller.canInstall(activity)) { pending = r; askPermission = true; return }
error = null error = null
@@ -18,11 +18,15 @@ import kotlin.math.abs
* from Android's own battery API - the same on every device - into a directory the session binds * from Android's own battery API - the same on every device - into a directory the session binds
* over `/sys/class/power_supply`, refreshed every few seconds while the session runs. * over `/sys/class/power_supply`, refreshed every few seconds while the session runs.
* *
* Steam's Quick Access Menu takes its time estimates from neither: it reads the files in `/run/vpower` (as on
* SteamOS), and with those absent showed "?h ?m" for projected battery life and for time to full.
* The same estimates are written there too, into [vpowerDir] in the guest's own `/run`.
*
* Values: charge in µAh from the charge counter; energy in µWh from charge × voltage; power in µW * Values: charge in µAh from the charge counter; energy in µWh from charge × voltage; power in µW
* from |current| × voltage; time to empty from charge ÷ |average current| while discharging. Where * from |current| × voltage; time to empty from charge ÷ |average current| while discharging. Where
* a device reports no current, the client still gets status and percentage. * a device reports no current, the client still gets status and percentage.
*/ */
class BatteryComponent(val dir: File) : SessionPart() { class BatteryComponent(val dir: File, private val vpowerDir: File) : SessionPart() {
@Volatile private var running = false @Volatile private var running = false
private var thread: Thread? = null private var thread: Thread? = null
@@ -42,6 +46,8 @@ class BatteryComponent(val dir: File) : SessionPart() {
running = false running = false
thread?.interrupt() thread?.interrupt()
thread = null thread = null
// Stale estimates would outlive the session in the rootfs; Steam falls back to "?h ?m".
for (name in VPOWER_FILES) File(vpowerDir, name).delete()
} }
private fun write() { private fun write() {
@@ -115,6 +121,17 @@ class BatteryComponent(val dir: File) : SessionPart() {
for ((k, v) in attrs) uevent.append("POWER_SUPPLY_").append(k.uppercase()).append('=').append(v).append('\n') for ((k, v) in attrs) uevent.append("POWER_SUPPLY_").append(k.uppercase()).append('=').append(v).append('\n')
put(File(bat, "uevent"), uevent.toString()) put(File(bat, "uevent"), uevent.toString())
} }
val plugged = (sticky?.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) ?: 0) != 0
vpowerDir.mkdirs()
val vpower = linkedMapOf(
"ac_status" to if (plugged) "Connected" else "Disconnected",
"battery_percent" to "%.2f".format(java.util.Locale.ROOT, pct.toDouble()),
"battery_status" to statusText,
// Steam reads a separate file for each direction; -1 = no estimate.
"secs_until_battery_full" to (if (timeToFullS > 0) timeToFullS else -1L).toString(),
"secs_until_shutdown_request" to (if (timeToEmptyS > 0) timeToEmptyS else -1L).toString(),
)
for ((k, v) in vpower) put(File(vpowerDir, k), v + "\n")
} catch (t: Throwable) { } catch (t: Throwable) {
Log.w(TAG, "battery: could not write", t) Log.w(TAG, "battery: could not write", t)
} }
@@ -130,5 +147,9 @@ class BatteryComponent(val dir: File) : SessionPart() {
companion object { companion object {
private const val TAG = "SessionService" private const val TAG = "SessionService"
private const val PERIOD_MS = 5_000L private const val PERIOD_MS = 5_000L
private val VPOWER_FILES = listOf(
"ac_status", "battery_percent", "battery_status",
"secs_until_battery_full", "secs_until_shutdown_request",
)
} }
} }
@@ -674,7 +674,11 @@ class SessionService : Service() {
// /sys/class/power_supply/BAT<n>/..., a laptop's or a Deck's naming; Android's supply is // /sys/class/power_supply/BAT<n>/..., a laptop's or a Deck's naming; Android's supply is
// called "battery" and its files differ, so the client sees no battery at all. A directory // called "battery" and its files differ, so the client sees no battery at all. A directory
// of our own, written from Android's battery API every few seconds, is bound over it. // of our own, written from Android's battery API every few seconds, is bound over it.
val battery = BatteryComponent(File(filesDir, "session/sys/power_supply")) // Steam's time estimates come from /run/vpower instead, written straight into the rootfs.
val battery = BatteryComponent(
File(filesDir, "session/sys/power_supply"),
File(LinuxRuntime.rootDir(this), "run/vpower"),
)
battery.attach(this) battery.attach(this)
components.add(battery) components.add(battery)
binds.add(battery.dir.path + ":/sys/class/power_supply") binds.add(battery.dir.path + ":/sys/class/power_supply")
@@ -112,10 +112,15 @@ internal fun UpdatesPage(s: FrontEndState, a: FrontEndActions, modifier: Modifie
if (LocalNarrowPane.current) { if (LocalNarrowPane.current) {
StatusPanel(s, u, ua, me) StatusPanel(s, u, ua, me)
Box(Modifier.height(18.dp)) Box(Modifier.height(18.dp))
ChannelLabel()
ChannelPicker(u, ua) ChannelPicker(u, ua)
} else Row(horizontalArrangement = Arrangement.spacedBy(18.dp)) { } else {
Box(Modifier.weight(1f)) { ChannelPicker(u, ua) } // The label sits over both columns, so the status card lines up with the first channel.
Box(Modifier.weight(1.15f)) { StatusPanel(s, u, ua, me) } ChannelLabel()
Row(horizontalArrangement = Arrangement.spacedBy(18.dp)) {
Box(Modifier.weight(1f)) { ChannelPicker(u, ua) }
Box(Modifier.weight(1.15f)) { StatusPanel(s, u, ua, me) }
}
} }
} }
} }
@@ -140,20 +145,21 @@ private fun StatusPanel(s: FrontEndState, u: UpdatesState, ua: UpdatesActions, m
val pal = LocalPalette.current val pal = LocalPalette.current
val catalog = u.catalog val catalog = u.catalog
val release = catalog?.let { AppUpdates.release(it, u.follow) } val release = catalog?.let { AppUpdates.release(it, u.follow) }
val offer = if (me.ci) catalog?.let { AppUpdates.offer(it, u.follow, me) } else null val offer = if (me.updatable) catalog?.let { AppUpdates.offer(it, u.follow, me) } else null
val name = channelName(u.follow) val name = channelName(u.follow)
val offered = release != null && (offer == Offer.UPDATE || offer == Offer.SWITCH) val offered = release != null && (offer == Offer.UPDATE || offer == Offer.SWITCH)
val installBlock = release?.let { AppUpdates.installBlock(it, me) }
class Look(val tint: Color, val status: String, val headline: String, val detail: String?) class Look(val tint: Color, val status: String, val headline: String, val detail: String?)
val look = when { val look = when {
!me.ci -> Look(colors.onSurfaceVariant, "Local build", "Built on a computer", !me.updatable -> Look(colors.onSurfaceVariant, "Signed differently", "Can't update in place",
"It's signed differently from the builds here, so Android won't update it in place. Uninstall it to switch.") "This copy isn't signed with DroidDeck's release key, so Android won't install the builds here over it. Uninstall it to switch.")
catalog == null -> Look(colors.onSurfaceVariant, if (u.checking) "Checking…" else "Not checked yet", "Updates", null) catalog == null -> Look(colors.onSurfaceVariant, if (u.checking) "Checking…" else "Not checked yet", "Updates", null)
offer == Offer.UPDATE -> Look(AttentionAmber, "Update available", newBuild(u.follow, release!!), null) offer == Offer.UPDATE -> Look(AttentionAmber, "Update available", newBuild(u.follow, release!!), null)
offer == Offer.SWITCH -> Look(pal.signal, "Ready to switch", newBuild(u.follow, release!!), null) offer == Offer.SWITCH -> Look(pal.signal, "Ready to switch", newBuild(u.follow, release!!), null)
offer == Offer.AHEAD -> Look(pal.signal, "Ahead of Stable", "You're ahead of Stable", offer == Offer.AHEAD -> Look(pal.signal, "Ahead of Stable", "You're ahead of Stable",
"This build is newer than the last Stable release. You'll move onto Stable with its next one.") "This build is newer than the last Stable release. You'll move onto Stable with its next one.")
offer == Offer.GONE -> Look(AttentionAmber, "Test ended", "This test has ended", offer == Offer.GONE -> Look(AttentionAmber, "Test ended", "This test has ended",
"Its fix was merged or dropped. Follow Nightly to keep getting the newest fixes.") "Its fix was merged or dropped. Follow Preview to keep getting the newest fixes.")
else -> Look(pal.good, "Up to date", "You have the latest $name", null) else -> Look(pal.good, "Up to date", "You have the latest $name", null)
} }
Column( Column(
@@ -169,8 +175,11 @@ private fun StatusPanel(s: FrontEndState, u: UpdatesState, ua: UpdatesActions, m
ReleaseNotes(changeTitle(release!!.title.ifBlank { release.tag }), release.summary, release.tag) ReleaseNotes(changeTitle(release!!.title.ifBlank { release.tag }), release.summary, release.tag)
} }
if (look.detail != null) Text(look.detail, fontSize = 14.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(top = 10.dp)) if (look.detail != null) Text(look.detail, fontSize = 14.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(top = 10.dp))
if (installBlock != null && (offered || offer == Offer.AHEAD)) {
Text(installBlock, fontSize = 13.sp, color = AttentionAmber, modifier = Modifier.padding(top = 10.dp))
}
if (u.error != null) Text(u.error, fontSize = 13.sp, color = pal.error, modifier = Modifier.padding(top = 12.dp)) if (u.error != null) Text(u.error, fontSize = 13.sp, color = pal.error, modifier = Modifier.padding(top = 12.dp))
val installable = release?.apk != null && !s.sessionRunning val installable = release?.apk != null && installBlock == null && !s.sessionRunning
val button = release?.apk?.size?.takeIf { it > 0 }?.let { " · ${megabytes(it)}" }.orEmpty() val button = release?.apk?.size?.takeIf { it > 0 }?.let { " · ${megabytes(it)}" }.orEmpty()
Box(Modifier.padding(top = 14.dp)) { Box(Modifier.padding(top = 14.dp)) {
when { when {
@@ -181,8 +190,9 @@ private fun StatusPanel(s: FrontEndState, u: UpdatesState, ua: UpdatesActions, m
} }
offer == Offer.UPDATE -> PrimaryButton("Update$button", enabled = installable, main = true) { ua.onInstall(release!!) } offer == Offer.UPDATE -> PrimaryButton("Update$button", enabled = installable, main = true) { ua.onInstall(release!!) }
offer == Offer.SWITCH -> PrimaryButton("Install$button", enabled = installable, main = true) { ua.onInstall(release!!) } offer == Offer.SWITCH -> PrimaryButton("Install$button", enabled = installable, main = true) { ua.onInstall(release!!) }
offer == Offer.AHEAD -> SecondaryButton("Install Stable ${release?.version.orEmpty()} anyway", enabled = installable) { ua.onInstall(release!!) } offer == Offer.AHEAD && installBlock == null ->
offer == Offer.GONE -> PrimaryButton("Follow Nightly", main = true) { ua.onFollow(Follow(Channel.NIGHTLY)) } SecondaryButton("Install Stable ${release?.version.orEmpty()} anyway", enabled = installable) { ua.onInstall(release!!) }
offer == Offer.GONE -> PrimaryButton("Follow Preview", main = true) { ua.onFollow(Follow(Channel.NIGHTLY)) }
} }
} }
if (s.sessionRunning && (offered || offer == Offer.AHEAD)) { if (s.sessionRunning && (offered || offer == Offer.AHEAD)) {
@@ -221,10 +231,10 @@ private fun ReleaseNotes(title: String, notes: String, key: String) {
} }
} }
/** The headline for a build on offer: "DroidDeck 0.3.0", "New Nightly build", "PR #93 test build". */ /** The headline for a build on offer: "DroidDeck 0.3.0", "New Preview build", "PR #93 test build". */
private fun newBuild(f: Follow, r: Release) = when (f.channel) { private fun newBuild(f: Follow, r: Release) = when (f.channel) {
Channel.STABLE -> "DroidDeck ${r.version ?: r.tag}" Channel.STABLE -> "DroidDeck ${r.version ?: r.tag}"
Channel.NIGHTLY -> "New Nightly build" Channel.NIGHTLY -> "New Preview build"
Channel.TEST -> "PR #${r.pr} test build" Channel.TEST -> "PR #${r.pr} test build"
} }
@@ -235,20 +245,27 @@ private fun changeTitle(t: String): String =
private fun megabytes(bytes: Long) = "${(bytes + 524_288) / 1_048_576} MB" private fun megabytes(bytes: Long) = "${(bytes + 524_288) / 1_048_576} MB"
/** The three channels as cards to pick from; Test builds opens its list of PRs under it. */ /** The three channels as cards to pick from; Test builds opens its list of PRs under it. */
@Composable
private fun ChannelLabel() {
Text(
"UPDATE CHANNEL", fontSize = 12.sp, fontWeight = FontWeight.SemiBold, letterSpacing = 1.5.sp,
color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(start = 2.dp, top = 2.dp, bottom = 10.dp),
)
}
@Composable @Composable
private fun ChannelPicker(u: UpdatesState, ua: UpdatesActions) { private fun ChannelPicker(u: UpdatesState, ua: UpdatesActions) {
val colors = MaterialTheme.colorScheme val colors = MaterialTheme.colorScheme
val catalog = u.catalog val catalog = u.catalog
val tests = catalog?.tests.orEmpty() val tests = catalog?.tests.orEmpty()
Column(verticalArrangement = Arrangement.spacedBy(10.dp)) { Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
Text("UPDATE CHANNEL", fontSize = 12.sp, fontWeight = FontWeight.SemiBold, letterSpacing = 1.5.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(start = 2.dp, top = 2.dp))
ChannelCard( ChannelCard(
Icons.Outlined.Verified, "Stable", "Tested releases, for most people", Icons.Outlined.Verified, "Stable", "Tested releases, for most people",
catalog?.stable?.let { "${it.version ?: it.tag} · ${ago(it.publishedAt)}" }, u.follow.channel == Channel.STABLE, catalog?.stable?.let { "${it.version ?: it.tag} · ${ago(it.publishedAt)}" }, u.follow.channel == Channel.STABLE,
) { ua.onFollow(Follow(Channel.STABLE)) } ) { ua.onFollow(Follow(Channel.STABLE)) }
ChannelCard( ChannelCard(
Icons.Outlined.Bolt, "Nightly", "Newest fixes, the odd new bug", Icons.Outlined.Bolt, "Preview", "Newest main-branch fixes, before Stable",
catalog?.nightly?.let { ago(it.publishedAt) }, u.follow.channel == Channel.NIGHTLY, catalog?.preview?.let { ago(it.publishedAt) }, u.follow.channel == Channel.NIGHTLY,
) { ua.onFollow(Follow(Channel.NIGHTLY)) } ) { ua.onFollow(Follow(Channel.NIGHTLY)) }
ChannelCard( ChannelCard(
Icons.Outlined.Science, "Test builds", "Try a fix before it's released", Icons.Outlined.Science, "Test builds", "Try a fix before it's released",
@@ -331,7 +348,7 @@ private fun TestRow(t: Release, selected: Boolean, onClick: () -> Unit) {
private fun channelName(f: Follow) = when (f.channel) { private fun channelName(f: Follow) = when (f.channel) {
Channel.STABLE -> "Stable" Channel.STABLE -> "Stable"
Channel.NIGHTLY -> "Nightly" Channel.NIGHTLY -> "Preview"
Channel.TEST -> "the PR #${f.pr} test" Channel.TEST -> "the PR #${f.pr} test"
} }
@@ -1,73 +1,121 @@
package com.droiddeck.launcher.update package com.droiddeck.launcher.update
import android.content.Context import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import com.droiddeck.launcher.BuildConfig import com.droiddeck.launcher.BuildConfig
import com.droiddeck.launcher.core.Hashes
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import java.io.IOException import java.io.IOException
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.net.URL import java.net.URL
import java.time.Instant import java.security.MessageDigest
/** /**
* DroidDeck's own builds, on three channels: Stable (the org repo's latest release), Nightly (the * DroidDeck's published builds. CI turns GitHub Releases into one static catalog in DroidDeck-CI;
* newest `main-<sha>` release on DroidDeck-CI, one per merge to main) and Test (a `pr-<n>` release * the app consumes that contract instead of inferring channels from tags and release-note prose.
* on DroidDeck-CI, a signed build of an open PR). All three are signed with the release key, so any
* of them installs over any other and keeps the app's data.
* *
* Which build is newer is decided without a diff of the repo (GitHub's compare answers in * [Channel.NIGHTLY] is intentionally kept as the internal enum/pref value for upgrades from older
* megabytes): main is linear, so the newest main build is the newest; Stable is ordered by its * DroidDeck versions. The channel is presented to users as Preview.
* version; a test build is newer when its PR's head moved. The last check is remembered, so the
* page shows it without going online.
*/ */
object AppUpdates { object AppUpdates {
private const val REPO = "Droid-Deck/DroidDeck" private const val REPO = "Droid-Deck/DroidDeck"
private const val CI_REPO = "Droid-Deck/DroidDeck-CI" private const val CI_REPO = "Droid-Deck/DroidDeck-CI"
private const val CATALOG_URL = "https://raw.githubusercontent.com/$CI_REPO/main/catalog.json"
private const val PREFS = "app_updates" private const val PREFS = "app_updates"
private const val KEY_CATALOG = "catalog" private const val KEY_CATALOG = "catalog"
private const val KEY_CHANNEL = "channel" private const val KEY_CHANNEL = "channel"
private const val CATALOG_SCHEMA = 1
enum class Channel { STABLE, NIGHTLY, TEST } enum class Channel { STABLE, NIGHTLY, TEST }
/** The channel the user follows; [pr] is the followed test build's PR, for [Channel.TEST]. */
data class Follow(val channel: Channel, val pr: Int = 0) data class Follow(val channel: Channel, val pr: Int = 0)
class Apk(val name: String, val url: String, val size: Long, val sha256: String) class Apk(
val name: String,
/** val url: String,
* One published build. [commit] is a full or short SHA of the source it was built from; [pr] is val size: Long,
* 0 for a main or stable build. [apk] is the file for the package this app runs as, or null when val sha256: String,
* the build has none (a variant a release predates). val packageName: String,
*/ val versionCode: Int,
class Release( val signerSha256: String,
val tag: String, val title: String, val summary: String, val commit: String, val pr: Int,
val version: String?, val publishedAt: Long, val url: String, val apk: Apk?,
) )
class Catalog(val stable: Release?, val nightly: Release?, val tests: List<Release>, val checkedAt: Long) class Release(
val tag: String,
val title: String,
val summary: String,
val commit: String,
val pr: Int,
val version: String?,
val versionCode: Int,
val publishedAt: Long,
val url: String,
val apk: Apk?,
)
/** What is running: the commit it was built from, its PR (0 for none), whether CI made it, and when it was committed. */ class Catalog(
class Installed(val commit: String, val pr: Int, val version: String, val ci: Boolean, val committedAt: Long = 0L) val stable: Release?,
val preview: Release?,
val tests: List<Release>,
val checkedAt: Long,
)
class Installed(
val commit: String,
val pr: Int,
val version: String,
val versionCode: Int,
val updatable: Boolean,
val committedAt: Long = 0L,
)
enum class Offer { enum class Offer {
/** This is the build the channel has. */
CURRENT, CURRENT,
/** The channel has a newer build of what is running. */
UPDATE, UPDATE,
/** The channel's build is a different line (a test build from Nightly, or back); installing moves over. */
SWITCH, SWITCH,
/** What is running is newer than the channel's build (a Nightly past the last Stable). */
AHEAD, AHEAD,
/** The followed test build is gone: its PR was merged or closed. */
GONE, GONE,
} }
fun installed(): Installed = Installed(BuildConfig.SOURCE_COMMIT, BuildConfig.PR_NUMBER, BuildConfig.VERSION_NAME, BuildConfig.CI_BUILD, BuildConfig.COMMIT_TIME) @Volatile private var releaseSigned = BuildConfig.CI_BUILD
fun init(context: Context) {
releaseSigned = runCatching {
signers(context).any { it.equals(BuildConfig.RELEASE_SIGNER, ignoreCase = true) }
}.getOrDefault(BuildConfig.CI_BUILD)
}
private fun signers(context: Context): List<String> {
val pm = context.packageManager
@Suppress("DEPRECATION")
val certs = if (Build.VERSION.SDK_INT >= 28) {
val info = pm.getPackageInfo(
context.packageName,
PackageManager.GET_SIGNING_CERTIFICATES,
).signingInfo ?: return emptyList()
if (info.hasMultipleSigners()) info.apkContentsSigners else info.signingCertificateHistory
} else {
pm.getPackageInfo(context.packageName, PackageManager.GET_SIGNATURES).signatures
} ?: return emptyList()
return certs.map { cert ->
MessageDigest.getInstance("SHA-256").digest(cert.toByteArray())
.joinToString("") { "%02x".format(it) }
}
}
fun installed(): Installed = Installed(
BuildConfig.SOURCE_COMMIT,
BuildConfig.PR_NUMBER,
BuildConfig.VERSION_NAME,
BuildConfig.VERSION_CODE,
releaseSigned,
BuildConfig.COMMIT_TIME,
)
fun release(catalog: Catalog, follow: Follow): Release? = when (follow.channel) { fun release(catalog: Catalog, follow: Follow): Release? = when (follow.channel) {
Channel.STABLE -> catalog.stable Channel.STABLE -> catalog.stable
Channel.NIGHTLY -> catalog.nightly Channel.NIGHTLY -> catalog.preview
Channel.TEST -> catalog.tests.firstOrNull { it.pr == follow.pr } Channel.TEST -> catalog.tests.firstOrNull { it.pr == follow.pr }
} }
@@ -85,19 +133,27 @@ object AppUpdates {
} }
} }
/** The dot on the rail: the followed channel has a newer build of what is running. */ fun hasUpdate(catalog: Catalog?, follow: Follow): Boolean {
fun hasUpdate(catalog: Catalog?, follow: Follow): Boolean = if (catalog == null) return false
catalog != null && installed().ci && offer(catalog, follow) == Offer.UPDATE val me = installed()
val r = release(catalog, follow) ?: return false
return me.updatable && offer(catalog, follow, me) == Offer.UPDATE && installBlock(r, me) == null
}
fun isRunning(r: Release, me: Installed = installed()): Boolean { 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) return false
return me.commit.startsWith(r.commit, ignoreCase = true) || r.commit.startsWith(me.commit, ignoreCase = true) return me.commit.startsWith(r.commit, ignoreCase = true) ||
r.commit.startsWith(me.commit, ignoreCase = true)
}
/** Why Android cannot install this published build over [me], before downloading anything. */
fun installBlock(r: Release, me: Installed = installed()): String? = when {
r.apk == null -> "This build has no download for this copy of DroidDeck."
r.apk.versionCode < me.versionCode ->
"Android won't install it over this build because its versionCode ${r.apk.versionCode} is below the installed ${me.versionCode}."
else -> null
} }
/**
* The channel followed. Until the user picks one it is worked out once from what is running and
* kept: a test build follows its PR, the Stable release follows Stable, and a main build Nightly.
*/
fun follow(context: Context, catalog: Catalog?): Follow { fun follow(context: Context, catalog: Catalog?): Follow {
stored(context)?.let { return it } stored(context)?.let { return it }
val me = installed() val me = installed()
@@ -105,7 +161,7 @@ object AppUpdates {
me.pr != 0 -> Follow(Channel.TEST, me.pr) me.pr != 0 -> Follow(Channel.TEST, me.pr)
catalog == null -> return Follow(Channel.STABLE) catalog == null -> return Follow(Channel.STABLE)
catalog.stable != null && isRunning(catalog.stable, me) -> Follow(Channel.STABLE) catalog.stable != null && isRunning(catalog.stable, me) -> Follow(Channel.STABLE)
me.ci -> Follow(Channel.NIGHTLY) me.updatable -> Follow(Channel.NIGHTLY)
else -> Follow(Channel.STABLE) else -> Follow(Channel.STABLE)
} }
setFollow(context, inferred) setFollow(context, inferred)
@@ -113,7 +169,9 @@ object AppUpdates {
} }
fun setFollow(context: Context, follow: Follow) { fun setFollow(context: Context, follow: Follow) {
val value = if (follow.channel == Channel.TEST) "test:${follow.pr}" else follow.channel.name.lowercase() // Keep "nightly" on disk so an older DroidDeck still understands the preference after rollback.
val value = if (follow.channel == Channel.TEST) "test:${follow.pr}"
else follow.channel.name.lowercase()
prefs(context).edit().putString(KEY_CHANNEL, value).apply() prefs(context).edit().putString(KEY_CHANNEL, value).apply()
} }
@@ -121,129 +179,115 @@ object AppUpdates {
val v = prefs(context).getString(KEY_CHANNEL, null) ?: return null val v = prefs(context).getString(KEY_CHANNEL, null) ?: return null
return when { return when {
v == "stable" -> Follow(Channel.STABLE) v == "stable" -> Follow(Channel.STABLE)
v == "nightly" -> Follow(Channel.NIGHTLY) v == "nightly" || v == "preview" -> Follow(Channel.NIGHTLY)
v.startsWith("test:") -> v.removePrefix("test:").toIntOrNull()?.let { Follow(Channel.TEST, it) } v.startsWith("test:") -> v.removePrefix("test:").toIntOrNull()?.let {
Follow(Channel.TEST, it)
}
else -> null else -> null
} }
} }
/** The last check, or null before the first. */
fun cached(context: Context): Catalog? { fun cached(context: Context): Catalog? {
val raw = prefs(context).getString(KEY_CATALOG, null) ?: return null val raw = prefs(context).getString(KEY_CATALOG, null) ?: return null
return runCatching { readCatalog(JSONObject(raw)) }.getOrNull() return runCatching { readCachedCatalog(JSONObject(raw)) }.getOrNull()
} }
/** Read every channel now and remember the result: three small requests, four for an annotated tag. */ /** One static request, rather than several unauthenticated GitHub API requests. */
fun refresh(context: Context): Catalog { fun refresh(context: Context): Catalog {
val suffix = variantSuffix(context.packageName) val checkedAt = System.currentTimeMillis()
val stable = runCatching { stable(suffix) }.getOrElse { if (it is NoRelease) null else throw it } val catalog = readPublishedCatalog(
val ci = JSONArray(get("https://api.github.com/repos/$CI_REPO/releases?per_page=50")) JSONObject(get("$CATALOG_URL?checked=$checkedAt")),
var nightly: Release? = null context.packageName,
val tests = ArrayList<Release>() checkedAt,
for (i in 0 until ci.length()) { )
val r = ci.getJSONObject(i)
if (r.optBoolean("draft")) continue
val tag = r.getString("tag_name")
when {
tag.startsWith("main-") -> {
val candidate = ciRelease(r, suffix, tag.removePrefix("main-"), 0)
if (nightly == null || candidate.publishedAt > nightly.publishedAt) nightly = candidate
}
tag.startsWith("pr-") -> {
val pr = tag.removePrefix("pr-").toIntOrNull() ?: continue
val body = r.optString("body")
// Only builds of this repo's PRs: a test build from before the org move names another.
if (!body.contains("github.com/$REPO/pull/$pr")) continue
val commit = Regex("""Commit: `?([0-9a-f]{7,40})""").find(body)?.groupValues?.get(1) ?: continue
tests.add(ciRelease(r, suffix, commit, pr))
}
}
}
tests.sortByDescending { it.publishedAt }
val catalog = Catalog(stable, nightly, tests, System.currentTimeMillis())
prefs(context).edit().putString(KEY_CATALOG, writeCatalog(catalog).toString()).apply() prefs(context).edit().putString(KEY_CATALOG, writeCatalog(catalog).toString()).apply()
return catalog return catalog
} }
private class NoRelease : IOException("no stable release") internal fun readPublishedCatalog(
root: JSONObject,
private fun stable(suffix: String): Release { packageName: String,
val r = try { checkedAt: Long,
JSONObject(get("https://api.github.com/repos/$REPO/releases/latest")) ): Catalog {
} catch (e: IOException) { val schema = root.optInt("schema")
if (e.message?.contains("HTTP 404") == true) throw NoRelease() else throw e val sourceRepo = root.optString("sourceRepo")
val ciRepo = root.optString("ciRepo")
if (schema != CATALOG_SCHEMA || sourceRepo != REPO || ciRepo != CI_REPO) {
throw IOException(
"The update catalog has an unsupported format (schema $schema, source $sourceRepo, CI $ciRepo)",
)
} }
val tag = r.getString("tag_name") val tests = root.optJSONArray("tests") ?: JSONArray()
// The tag's commit; an annotated tag points at a tag object first. return Catalog(
var obj = JSONObject(get("https://api.github.com/repos/$REPO/git/ref/tags/$tag")).getJSONObject("object") root.optJSONObject("stable")?.let { readPublishedRelease(it, packageName) },
if (obj.getString("type") == "tag") obj = JSONObject(get(obj.getString("url"))).getJSONObject("object") root.optJSONObject("preview")?.let { readPublishedRelease(it, packageName) },
val (title, summary) = describeStable(r.optString("name").ifBlank { tag }, r.optString("body")) (0 until tests.length())
return Release(tag, title, summary, obj.getString("sha"), 0, tag.removePrefix("v"), published(r), .map { readPublishedRelease(tests.getJSONObject(it), packageName) }
r.optString("html_url"), apk(r, suffix)) .filter { it.pr > 0 }
.sortedByDescending { it.publishedAt },
checkedAt,
)
} }
private fun ciRelease(r: JSONObject, suffix: String, commit: String, pr: Int): Release { private fun readPublishedRelease(o: JSONObject, packageName: String): Release {
val (title, summary) = describeCi(r.optString("name"), r.optString("body")) val commit = o.getString("commit")
return Release(r.getString("tag_name"), title, summary, commit, pr, null, published(r), r.optString("html_url"), apk(r, suffix)) if (!Regex("^[0-9a-fA-F]{7,40}$").matches(commit)) {
} throw IOException("The update catalog contains an invalid commit")
private fun published(r: JSONObject): Long =
runCatching { Instant.parse(r.optString("published_at")).toEpochMilli() }.getOrDefault(0L)
/** The apk for this package: `<name><suffix>.apk`, and for the standard app the one with no other variant's suffix. */
private fun apk(r: JSONObject, suffix: String): Apk? {
val others = variantSuffixes().values.filter { it.isNotEmpty() && it != suffix }
val assets = r.optJSONArray("assets") ?: return null
for (i in 0 until assets.length()) {
val a = assets.getJSONObject(i)
val name = a.getString("name")
if (!name.endsWith("$suffix.apk") || others.any { name.endsWith("$it.apk") }) continue
// Only a file GitHub has a sha256 for: the download is checked against it.
val sha = Hashes.githubSha256(a.optString("digest")) ?: continue
return Apk(name, a.getString("browser_download_url"), a.optLong("size"), sha)
} }
return null val versionCode = o.getInt("versionCode")
if (versionCode <= 0) throw IOException("The update catalog contains an invalid versionCode")
val apk = o.optJSONObject("apks")?.optJSONObject(packageName)?.let {
readPublishedApk(it, packageName)
}
if (apk != null && apk.versionCode != versionCode) {
throw IOException("The update catalog disagrees about the APK versionCode")
}
return Release(
o.getString("tag"),
o.optString("title"),
o.optString("summary"),
commit,
o.optInt("pr"),
if (o.isNull("version")) null else o.optString("version").ifEmpty { null },
versionCode,
o.optLong("publishedAt"),
o.optString("url"),
apk,
)
} }
private fun variantSuffixes(): Map<String, String> = BuildConfig.VARIANT_SUFFIXES.split(';') private fun readPublishedApk(o: JSONObject, packageName: String): Apk {
.mapNotNull { e -> e.split('=', limit = 2).takeIf { it.size == 2 }?.let { it[0] to it[1] } }.toMap() val actualPackage = o.getString("packageName")
val versionCode = o.getInt("versionCode")
private fun variantSuffix(packageName: String): String = variantSuffixes()[packageName] ?: "" val signer = o.getString("signerSha256").lowercase()
val sha = o.getString("sha256").lowercase()
/** val url = o.getString("url")
* A CI release's notes open with what it is: "**Merged to main** - [PR #n](…): title (merged …)" if (actualPackage != packageName) throw IOException("The update catalog names the wrong Android package")
* or "**Not merged** - a test build of open [PR #n](…): title", then a paragraph from the PR. if (versionCode <= 0) throw IOException("The update catalog has an invalid APK versionCode")
*/ if (!Regex("^[0-9a-f]{64}$").matches(sha)) {
internal fun describeCi(name: String, body: String): Pair<String, String> { throw IOException("The update catalog has an invalid checksum")
val paragraphs = paragraphs(body) }
val head = paragraphs.firstOrNull().orEmpty() if (!signer.equals(BuildConfig.RELEASE_SIGNER, ignoreCase = true)) {
val title = head.substringAfter("): ", "").substringBeforeLast(" (merged").ifBlank { plain(head) }.ifBlank { name } throw IOException("The update catalog is not signed for DroidDeck's release key")
val summary = paragraphs.getOrNull(1)?.takeUnless { it.startsWith("Signed build") || it.startsWith("Source:") }.orEmpty() }
return plain(title) to lines(summary) val allowed = listOf(
"https://github.com/$REPO/releases/download/",
"https://github.com/$CI_REPO/releases/download/",
)
if (allowed.none { url.startsWith(it) }) {
throw IOException("The update catalog has an unexpected download URL")
}
return Apk(
o.getString("name"),
url,
o.optLong("size"),
sha,
actualPackage,
versionCode,
signer,
)
} }
/** A stable release: its name, and the first paragraph of its notes that is prose rather than a heading or picture. */
private fun describeStable(name: String, body: String): Pair<String, String> =
name to lines(paragraphs(body).firstOrNull { !it.startsWith("#") && !it.startsWith("![") && !it.startsWith("<") }.orEmpty())
private fun paragraphs(body: String): List<String> =
body.replace("\r", "").split(Regex("\n\\s*\n")).map { it.trim() }.filter { it.isNotEmpty() }
/** A paragraph's lines as text, a list item's marker as a bullet: the page shows them collapsed or in full. */
private fun lines(md: String): String = md.lines().map { line ->
val item = Regex("""^\s*[-*]\s+""").find(line)
if (item != null) "• " + plain(line.substring(item.range.last + 1)) else plain(line)
}.filter { it.isNotEmpty() }.joinToString("\n")
/** Markdown down to text: links to their words, emphasis and code marks gone, on one line. */
private fun plain(md: String): String = md
.replace(Regex("""!\[[^]]*]\([^)]*\)"""), "")
.replace(Regex("""\[([^]]*)]\([^)]*\)"""), "$1")
.replace(Regex("""[*_`]{1,3}"""), "")
.replace(Regex("""\s+"""), " ")
.trim()
/** Dotted versions, number by number: "0.10.0" is past "0.9.2"; a missing part counts as 0. */
fun compareVersions(a: String, b: String): Int { fun compareVersions(a: String, b: String): Int {
val x = a.removePrefix("v").split('.', '-').map { it.toIntOrNull() ?: 0 } val x = a.removePrefix("v").split('.', '-').map { it.toIntOrNull() ?: 0 }
val y = b.removePrefix("v").split('.', '-').map { it.toIntOrNull() ?: 0 } val y = b.removePrefix("v").split('.', '-').map { it.toIntOrNull() ?: 0 }
@@ -258,40 +302,79 @@ object AppUpdates {
val c = URL(url).openConnection() as HttpURLConnection val c = URL(url).openConnection() as HttpURLConnection
c.connectTimeout = 15_000 c.connectTimeout = 15_000
c.readTimeout = 20_000 c.readTimeout = 20_000
c.setRequestProperty("Accept", "application/vnd.github+json") c.setRequestProperty("Accept", "application/json")
c.setRequestProperty("User-Agent", "DroidDeck-app") c.setRequestProperty("User-Agent", "DroidDeck-app")
c.setRequestProperty("Cache-Control", "no-cache")
try { try {
val code = c.responseCode val code = c.responseCode
if (code == 403 || code == 429) throw IOException("GitHub's rate limit was hit - try again later") if (code != 200) throw IOException("The update catalog answered HTTP $code")
if (code != 200) throw IOException("GitHub answered HTTP $code")
return c.inputStream.bufferedReader().use { it.readText() } return c.inputStream.bufferedReader().use { it.readText() }
} finally { } finally {
c.disconnect() c.disconnect()
} }
} }
private fun writeRelease(r: Release): JSONObject = JSONObject() private fun writeApk(a: Apk): JSONObject = JSONObject()
.put("tag", r.tag).put("title", r.title).put("summary", r.summary).put("commit", r.commit).put("pr", r.pr) .put("name", a.name)
.put("version", r.version).put("publishedAt", r.publishedAt).put("url", r.url) .put("url", a.url)
.put("apk", r.apk?.let { JSONObject().put("name", it.name).put("url", it.url).put("size", it.size).put("sha256", it.sha256) }) .put("size", a.size)
.put("sha256", a.sha256)
.put("packageName", a.packageName)
.put("versionCode", a.versionCode)
.put("signerSha256", a.signerSha256)
private fun readRelease(o: JSONObject): Release = Release( private fun writeRelease(r: Release): JSONObject = JSONObject()
o.getString("tag"), o.optString("title"), o.optString("summary"), o.getString("commit"), o.optInt("pr"), .put("tag", r.tag)
o.optString("version").ifEmpty { null }, o.optLong("publishedAt"), o.optString("url"), .put("title", r.title)
o.optJSONObject("apk")?.let { Apk(it.getString("name"), it.getString("url"), it.optLong("size"), it.getString("sha256")) }, .put("summary", r.summary)
.put("commit", r.commit)
.put("pr", r.pr)
.put("version", r.version)
.put("versionCode", r.versionCode)
.put("publishedAt", r.publishedAt)
.put("url", r.url)
.put("apk", r.apk?.let(::writeApk))
private fun readCachedRelease(o: JSONObject): Release = Release(
o.getString("tag"),
o.optString("title"),
o.optString("summary"),
o.getString("commit"),
o.optInt("pr"),
if (o.isNull("version")) null else o.optString("version").ifEmpty { null },
o.getInt("versionCode"),
o.optLong("publishedAt"),
o.optString("url"),
o.optJSONObject("apk")?.let {
Apk(
it.getString("name"),
it.getString("url"),
it.optLong("size"),
it.getString("sha256"),
it.getString("packageName"),
it.getInt("versionCode"),
it.getString("signerSha256"),
)
},
) )
private fun writeCatalog(c: Catalog): JSONObject = JSONObject() private fun writeCatalog(c: Catalog): JSONObject = JSONObject()
.put("stable", c.stable?.let(::writeRelease)).put("nightly", c.nightly?.let(::writeRelease)) .put("stable", c.stable?.let(::writeRelease))
.put("tests", JSONArray().apply { c.tests.forEach { put(writeRelease(it)) } }).put("checkedAt", c.checkedAt) .put("preview", c.preview?.let(::writeRelease))
.put("tests", JSONArray().apply { c.tests.forEach { put(writeRelease(it)) } })
.put("checkedAt", c.checkedAt)
private fun readCatalog(o: JSONObject): Catalog { private fun readCachedCatalog(o: JSONObject): Catalog {
val tests = o.optJSONArray("tests") ?: JSONArray() val tests = o.optJSONArray("tests") ?: JSONArray()
val preview = o.optJSONObject("preview") ?: o.optJSONObject("nightly")
return Catalog( return Catalog(
o.optJSONObject("stable")?.let(::readRelease), o.optJSONObject("nightly")?.let(::readRelease), o.optJSONObject("stable")?.let(::readCachedRelease),
(0 until tests.length()).map { readRelease(tests.getJSONObject(it)) }, o.optLong("checkedAt"), preview?.let(::readCachedRelease),
(0 until tests.length()).map { readCachedRelease(tests.getJSONObject(it)) },
o.optLong("checkedAt"),
) )
} }
private fun prefs(context: Context) = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) private fun prefs(context: Context) =
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
} }
@@ -4,9 +4,11 @@ import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageInstaller import android.content.pm.PackageInstaller
import android.content.pm.PackageManager
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.provider.Settings import android.provider.Settings
import com.droiddeck.launcher.BuildConfig
import com.droiddeck.launcher.core.FileUtils import com.droiddeck.launcher.core.FileUtils
import com.droiddeck.launcher.core.Hashes import com.droiddeck.launcher.core.Hashes
import java.io.File import java.io.File
@@ -14,6 +16,7 @@ import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.net.URL import java.net.URL
import java.security.MessageDigest
/** /**
* Installs one of DroidDeck's own builds over itself: download into the cache, check the file * Installs one of DroidDeck's own builds over itself: download into the cache, check the file
@@ -60,9 +63,13 @@ object SelfInstaller {
} }
} }
} }
if (apk.size > 0 && target.length() != apk.size) {
throw IOException("The download size didn't match the published APK and was discarded - try again")
}
if (!apk.sha256.equals(Hashes.sha256(target), ignoreCase = true)) { if (!apk.sha256.equals(Hashes.sha256(target), ignoreCase = true)) {
throw IOException("The download didn't match its checksum and was discarded - try again") throw IOException("The download didn't match its checksum and was discarded - try again")
} }
validateDownloaded(context, target, apk)
return target return target
} catch (e: Exception) { } catch (e: Exception) {
FileUtils.delete(target) FileUtils.delete(target)
@@ -72,6 +79,48 @@ object SelfInstaller {
} }
} }
/** Inspect the actual downloaded APK before giving it to Android's installer. */
private fun validateDownloaded(context: Context, file: File, apk: AppUpdates.Apk) {
if (apk.packageName != context.packageName) {
throw IOException("The update is for Android package ${apk.packageName}, not ${context.packageName}")
}
if (!apk.signerSha256.equals(BuildConfig.RELEASE_SIGNER, ignoreCase = true)) {
throw IOException("The update metadata doesn't name DroidDeck's release signing key")
}
@Suppress("DEPRECATION")
val flags = if (Build.VERSION.SDK_INT >= 28) {
PackageManager.GET_SIGNING_CERTIFICATES
} else {
PackageManager.GET_SIGNATURES
}
val info = context.packageManager.getPackageArchiveInfo(file.absolutePath, flags)
?: throw IOException("Android couldn't read the downloaded APK")
if (info.packageName != context.packageName) {
throw IOException("The downloaded APK is for ${info.packageName}, not ${context.packageName}")
}
@Suppress("DEPRECATION")
val code = if (Build.VERSION.SDK_INT >= 28) info.longVersionCode else info.versionCode.toLong()
if (code != apk.versionCode.toLong()) {
throw IOException("The downloaded APK says versionCode $code, not the published ${apk.versionCode}")
}
if (code < BuildConfig.VERSION_CODE.toLong()) {
throw IOException("Android won't install versionCode $code over the installed ${BuildConfig.VERSION_CODE}")
}
// Before Android 9 this lineage intentionally presents the public legacy test key. Android
// 9+ understands the v3 hand-over and must see DroidDeck's private release key as current.
if (Build.VERSION.SDK_INT >= 28) {
val signing = info.signingInfo ?: throw IOException("The downloaded APK has no signing information")
val digests = signing.apkContentsSigners.orEmpty().map { cert ->
MessageDigest.getInstance("SHA-256").digest(cert.toByteArray())
.joinToString("") { "%02x".format(it) }
}
if (digests.none { it.equals(BuildConfig.RELEASE_SIGNER, ignoreCase = true) }) {
throw IOException("The downloaded APK isn't signed with DroidDeck's release key")
}
}
}
/** Writes [apk] into an install session and commits it; the answer comes as an [ACTION_STATUS] broadcast. */ /** Writes [apk] into an install session and commits it; the answer comes as an [ACTION_STATUS] broadcast. */
fun install(context: Context, apk: File) { fun install(context: Context, apk: File) {
val installer = context.packageManager.packageInstaller val installer = context.packageManager.packageInstaller
@@ -1,26 +1,69 @@
package com.droiddeck.launcher.update package com.droiddeck.launcher.update
import com.droiddeck.launcher.BuildConfig
import com.droiddeck.launcher.update.AppUpdates.Channel import com.droiddeck.launcher.update.AppUpdates.Channel
import com.droiddeck.launcher.update.AppUpdates.Follow import com.droiddeck.launcher.update.AppUpdates.Follow
import com.droiddeck.launcher.update.AppUpdates.Installed import com.droiddeck.launcher.update.AppUpdates.Installed
import com.droiddeck.launcher.update.AppUpdates.Offer import com.droiddeck.launcher.update.AppUpdates.Offer
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.json.JSONObject
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import java.io.IOException
@RunWith(RobolectricTestRunner::class)
class AppUpdatesTest { class AppUpdatesTest {
private fun release(commit: String, pr: Int = 0, version: String? = null) = private fun apk(versionCode: Int = 9) = AppUpdates.Apk(
AppUpdates.Release("t", "title", "", commit, pr, version, 0L, "", null) "DroidDeck.apk",
"https://github.com/Droid-Deck/DroidDeck-CI/releases/download/t/DroidDeck.apk",
10,
"a".repeat(64),
"com.droiddeck.launcher",
versionCode,
BuildConfig.RELEASE_SIGNER,
)
private val stable = release("aaaaaaa1111111111111111111111111111111", version = "0.2.0") private fun release(
commit: String,
pr: Int = 0,
version: String? = null,
versionCode: Int = 9,
withApk: Boolean = true,
) = AppUpdates.Release(
"t",
"title",
"",
commit,
pr,
version,
versionCode,
0L,
"",
if (withApk) apk(versionCode) else null,
)
private val stable = release(
"aaaaaaa1111111111111111111111111111111",
version = "0.2.0",
)
private val catalog = AppUpdates.Catalog( private val catalog = AppUpdates.Catalog(
stable = stable, stable = stable,
nightly = release("bbbbbbb"), preview = release("bbbbbbb"),
tests = listOf(release("ccccccc2222222222222222222222222222222", pr = 93)), tests = listOf(release("ccccccc2222222222222222222222222222222", pr = 93)),
checkedAt = 0L, checkedAt = 0L,
) )
private fun me(commit: String, pr: Int = 0, version: String = "0.2.0") = Installed(commit, pr, version, ci = true) private fun me(
commit: String,
pr: Int = 0,
version: String = "0.2.0",
versionCode: Int = 9,
) = Installed(commit, pr, version, versionCode, updatable = true)
@Test @Test
fun theStableBuildIsCurrentOnStable() { fun theStableBuildIsCurrentOnStable() {
@@ -28,17 +71,20 @@ class AppUpdatesTest {
} }
@Test @Test
fun aNightlyPastStableIsAheadNotBehind() { fun aPreviewPastStableIsAheadNotBehind() {
assertEquals(Offer.AHEAD, AppUpdates.offer(catalog, Follow(Channel.STABLE), me("ddddddd999"))) assertEquals(Offer.AHEAD, AppUpdates.offer(catalog, Follow(Channel.STABLE), me("ddddddd999")))
} }
@Test @Test
fun aNewerStableVersionIsAnUpdate() { fun aNewerStableVersionIsAnUpdate() {
assertEquals(Offer.UPDATE, AppUpdates.offer(catalog, Follow(Channel.STABLE), me("eeeeeee999", version = "0.1.9"))) assertEquals(
Offer.UPDATE,
AppUpdates.offer(catalog, Follow(Channel.STABLE), me("eeeeeee999", version = "0.1.9")),
)
} }
@Test @Test
fun nightlyMatchesItsShortShaAndOffersTheNewest() { fun previewMatchesItsShortShaAndOffersTheNewest() {
assertEquals(Offer.CURRENT, AppUpdates.offer(catalog, Follow(Channel.NIGHTLY), me("bbbbbbb0123456789"))) assertEquals(Offer.CURRENT, AppUpdates.offer(catalog, Follow(Channel.NIGHTLY), me("bbbbbbb0123456789")))
assertEquals(Offer.UPDATE, AppUpdates.offer(catalog, Follow(Channel.NIGHTLY), me(stable.commit))) assertEquals(Offer.UPDATE, AppUpdates.offer(catalog, Follow(Channel.NIGHTLY), me(stable.commit)))
} }
@@ -52,7 +98,7 @@ class AppUpdatesTest {
} }
@Test @Test
fun aTestBuildIsNeverTheMainBuildOfTheSameCommit() { fun aTestBuildIsNeverThePreviewBuildOfTheSameCommit() {
assertEquals(Offer.SWITCH, AppUpdates.offer(catalog, Follow(Channel.NIGHTLY), me("bbbbbbb", pr = 5))) assertEquals(Offer.SWITCH, AppUpdates.offer(catalog, Follow(Channel.NIGHTLY), me("bbbbbbb", pr = 5)))
} }
@@ -64,20 +110,77 @@ class AppUpdatesTest {
} }
@Test @Test
fun ciNotesGiveTheChangeAndItsSummary() { fun aVersionCodeDowngradeIsBlockedBeforeDownload() {
val (title, summary) = AppUpdates.describeCi( val older = release("ddddddd", versionCode = 8)
"main 8a61a18", assertNotNull(AppUpdates.installBlock(older, me("eeeeeee", versionCode = 9)))
"**Merged to main** - [PR #95](https://github.com/Droid-Deck/DroidDeck/pull/95): fix: keep `Steam` alive (merged 2026-09-30)\n\n" + assertNull(AppUpdates.installBlock(stable, me("eeeeeee", versionCode = 9)))
"Steam no longer **exits** on resume.\n\nSigned build of https://github.com/x - in main.", }
@Test
fun aMissingVariantIsBlockedBeforeDownload() {
val missing = release("ddddddd", withApk = false)
assertNotNull(AppUpdates.installBlock(missing, me("eeeeeee")))
}
private fun publishedCatalog(signer: String = BuildConfig.RELEASE_SIGNER): JSONObject {
val raw = """
{
"schema": 1,
"sourceRepo": "Droid-Deck/DroidDeck",
"ciRepo": "Droid-Deck/DroidDeck-CI",
"stable": null,
"preview": {
"tag": "main-deadbee",
"title": "Preview",
"summary": "",
"commit": "deadbee1234567890",
"pr": 0,
"version": null,
"versionCode": 9,
"publishedAt": 1,
"url": "https://github.com/Droid-Deck/DroidDeck-CI/releases/tag/main-deadbee",
"apks": {
"com.droiddeck.launcher": {
"name": "DroidDeck-main-deadbee.apk",
"url": "https://github.com/Droid-Deck/DroidDeck-CI/releases/download/main-deadbee/DroidDeck-main-deadbee.apk",
"size": 10,
"sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"packageName": "com.droiddeck.launcher",
"versionCode": 9,
"signerSha256": "__SIGNER__"
}
}
},
"tests": []
}
""".trimIndent().replace("__SIGNER__", signer)
return JSONObject(raw)
}
@Test
fun publishedCatalogSelectsThisPackagesApk() {
val parsed = AppUpdates.readPublishedCatalog(
publishedCatalog(),
"com.droiddeck.launcher",
123L,
) )
assertEquals("fix: keep Steam alive", title) assertEquals("main-deadbee", parsed.preview!!.tag)
assertEquals("Steam no longer exits on resume.", summary) assertEquals("com.droiddeck.launcher", parsed.preview!!.apk!!.packageName)
val (prTitle, prSummary) = AppUpdates.describeCi( assertEquals(9, parsed.preview!!.apk!!.versionCode)
"PR #93", assertNull(parsed.preview!!.version)
"**Not merged** - a test build of open [PR #93](https://github.com/Droid-Deck/DroidDeck/pull/93): fix: report Adreno 840v2\n\n" + assertEquals(123L, parsed.checkedAt)
"Source: https://github.com/Droid-Deck/DroidDeck/pull/93\nCommit: 93382ff", }
)
assertEquals("fix: report Adreno 840v2", prTitle) @Test
assertEquals("", prSummary) fun publishedCatalogRejectsAnotherSigner() {
try {
AppUpdates.readPublishedCatalog(
publishedCatalog("0".repeat(64)),
"com.droiddeck.launcher",
123L,
)
fail("wrong signer should be rejected")
} catch (_: IOException) {
}
} }
} }
+58
View File
@@ -0,0 +1,58 @@
/*
* Adreno revisions the runtime's Turnip does not know, reported as the revision it does.
*
* Retail SM8850 phones report their A840 as chip_id 0x44050A21 (KGSL gpu_model "Adreno840v2").
* Mesa only lists 0x44050A31, so the runtime's glibc Turnip refuses the device outright
* ("device (chip_id = 44050A21, gpu_id = 6860) is unsupported") and gamescope exits before Steam
* starts. Banners-Turnip fixes its own builds by registering the v2 id on the same A840 GPU info
* (patches/a840v2.py); answering KGSL's device-info query with the known id has the same effect
* on any Turnip in the session, including the one the runtime was built with. A driver that
* already knows the v2 id resolves the rewritten one to the same entry.
*
* Only the KGSL device-info property is touched. Its layout is the kernel's uapi (msm_kgsl.h).
*/
#define _GNU_SOURCE 1
#include <stddef.h>
#include <sys/ioctl.h>
struct kgsl_devinfo {
unsigned int device_id;
unsigned int chip_id;
unsigned int mmu_enabled;
unsigned long gmem_gpubaseaddr;
unsigned int gpu_id;
size_t gmem_sizebytes;
};
struct kgsl_device_getproperty {
unsigned int type;
void *value;
size_t sizebytes;
};
#define KGSL_IOC_TYPE 0x09
#define IOCTL_KGSL_DEVICE_GETPROPERTY _IOWR(KGSL_IOC_TYPE, 0x2, struct kgsl_device_getproperty)
#define KGSL_PROP_DEVICE_INFO 0x1
static const struct {
unsigned int reported;
unsigned int known;
} CHIP_IDS[] = {
{0x44050A21, 0x44050A31}, /* Adreno 840v2 -> Adreno 840 */
};
void bl_kgsl_chip_id_fixup(unsigned long request, void *arg, int rc) {
if (rc != 0 || request != IOCTL_KGSL_DEVICE_GETPROPERTY || arg == NULL) return;
struct kgsl_device_getproperty *prop = arg;
if (prop->type != KGSL_PROP_DEVICE_INFO || prop->value == NULL
|| prop->sizebytes < sizeof(struct kgsl_devinfo)) {
return;
}
struct kgsl_devinfo *info = prop->value;
for (size_t i = 0; i < sizeof(CHIP_IDS) / sizeof(CHIP_IDS[0]); i++) {
if (info->chip_id == CHIP_IDS[i].reported) {
info->chip_id = CHIP_IDS[i].known;
return;
}
}
}
+3 -2
View File
@@ -349,6 +349,7 @@ static int is_link_query(unsigned long request) {
|| request == SIOCGIFBRDADDR; || request == SIOCGIFBRDADDR;
} }
void bl_kgsl_chip_id_fixup(unsigned long request, void *arg, int rc); /* kgslid.c */
int bl_kgsl_poll(int (*real)(int, unsigned long, void *), int fd, unsigned long request, int bl_kgsl_poll(int (*real)(int, unsigned long, void *), int fd, unsigned long request,
void *arg, int *rc) __attribute__((visibility("hidden"))); void *arg, int *rc) __attribute__((visibility("hidden")));
@@ -364,8 +365,8 @@ int ioctl(int fd, unsigned long request, ...) {
if (real == NULL) real = (ioctl_fn) dlsym(RTLD_NEXT, "ioctl"); if (real == NULL) real = (ioctl_fn) dlsym(RTLD_NEXT, "ioctl");
int rc; int rc;
if (bl_kgsl_poll(real, fd, request, arg, &rc)) return rc; if (!bl_kgsl_poll(real, fd, request, arg, &rc)) rc = real(fd, request, arg);
rc = real(fd, request, arg); bl_kgsl_chip_id_fixup(request, arg, rc);
if (arg == NULL || !is_link_query(request)) return rc; if (arg == NULL || !is_link_query(request)) return rc;
int saved = errno; int saved = errno;
if (answer_ifreq(request, (struct ifreq *) arg, rc)) return 0; if (answer_ifreq(request, (struct ifreq *) arg, rc)) return 0;
+419
View File
@@ -0,0 +1,419 @@
#!/usr/bin/env python3
"""Build and publish the one-file update catalog consumed by DroidDeck.
The catalog is derived from GitHub releases, but the Android app never has to infer
channels, parse release notes, dereference tags, or spend unauthenticated API quota.
"""
from __future__ import annotations
import argparse
import base64
import datetime as dt
import json
import os
import re
import time
import urllib.error
import urllib.parse
import urllib.request
API = "https://api.github.com"
SHA256 = re.compile(r"^[0-9a-f]{64}$")
PR_TAG = re.compile(r"^pr-([1-9][0-9]*)$")
MAIN_TAG = re.compile(r"^main-([0-9a-f]{7,40})$")
class ApiError(RuntimeError):
def __init__(self, status: int, message: str):
super().__init__(f"GitHub API {status}: {message}")
self.status = status
class GitHub:
def __init__(self, token: str):
self.token = token
def request(self, method: str, path: str, body=None):
data = None
headers = {
"Accept": "application/vnd.github+json",
"User-Agent": "DroidDeck-update-catalog",
"X-GitHub-Api-Version": "2022-11-28",
}
if self.token:
headers["Authorization"] = f"Bearer {self.token}"
if body is not None:
data = json.dumps(body).encode()
headers["Content-Type"] = "application/json"
req = urllib.request.Request(API + path, data=data, headers=headers, method=method)
try:
with urllib.request.urlopen(req, timeout=30) as response:
raw = response.read()
return json.loads(raw) if raw else None
except urllib.error.HTTPError as e:
raw = e.read().decode(errors="replace")
raise ApiError(e.code, raw[:500]) from e
def get(self, path: str):
return self.request("GET", path)
def put(self, path: str, body):
return self.request("PUT", path, body)
def q(value: str) -> str:
return urllib.parse.quote(value, safe="")
def repo_file(gh: GitHub, repo: str, path: str, ref: str) -> str:
obj = gh.get(f"/repos/{repo}/contents/{q(path)}?ref={q(ref)}")
if obj.get("encoding") != "base64":
raise RuntimeError(f"{repo}:{ref}:{path} was not base64 content")
return base64.b64decode(obj["content"]).decode()
def resolve_tag(gh: GitHub, repo: str, tag: str) -> str:
obj = gh.get(f"/repos/{repo}/git/ref/tags/{q(tag)}")["object"]
while obj["type"] == "tag":
obj = gh.get(f"/repos/{repo}/git/tags/{q(obj['sha'])}")["object"]
if obj["type"] != "commit":
raise RuntimeError(f"{repo} tag {tag} points to {obj['type']}, not a commit")
return obj["sha"]
def parse_gradle(text: str) -> tuple[int, str | None]:
code = re.search(r"(?m)^\s*versionCode\s+(\d+)\s*$", text)
name = re.search(r'(?m)^\s*versionName\s+"([^"]+)"\s*$', text)
if not code:
raise RuntimeError("app/build.gradle has no numeric versionCode")
return int(code.group(1)), name.group(1) if name else None
def parse_variants(text: str) -> list[tuple[str, str]]:
out = []
for raw in text.splitlines():
line = raw.strip()
if not line or line.startswith("#"):
continue
fields = line.split()
if len(fields) < 3:
raise RuntimeError(f"bad variants.txt row: {raw!r}")
out.append((fields[1], "" if fields[2] == "-" else fields[2]))
if not out:
raise RuntimeError("variants.txt has no packages")
return out
def published_millis(release: dict, prefer_updated: bool = False) -> int:
value = (
release.get("updated_at") if prefer_updated else None
) or release.get("published_at") or release.get("created_at")
if not value:
return 0
return int(dt.datetime.fromisoformat(value.replace("Z", "+00:00")).timestamp() * 1000)
def paragraphs(markdown: str) -> list[str]:
return [p.strip() for p in re.split(r"\n\s*\n", markdown.replace("\r", "")) if p.strip()]
def plain(markdown: str) -> str:
text = re.sub(r"!\[[^]]*]\([^)]*\)", "", markdown)
text = re.sub(r"\[([^]]*)]\([^)]*\)", r"\1", text)
text = re.sub(r"[*_`]{1,3}", "", text)
return re.sub(r"\s+", " ", text).strip()
def prose_lines(markdown: str) -> str:
lines = []
for raw in markdown.splitlines():
match = re.match(r"^\s*[-*]\s+", raw)
value = ("• " + plain(raw[match.end():])) if match else plain(raw)
if value:
lines.append(value)
return "\n".join(lines)
def ci_description(release: dict) -> tuple[str, str]:
ps = paragraphs(release.get("body") or "")
head = ps[0] if ps else ""
title = ""
if "): " in head:
title = head.split("): ", 1)[1]
title = re.sub(r" \(merged[^)]*\)$", "", title)
title = plain(title or head or release.get("name") or release["tag_name"])
summary = ps[1] if len(ps) > 1 else ""
if summary.startswith("Signed build") or summary.startswith("Source:"):
summary = ""
return title, prose_lines(summary)
def stable_description(release: dict) -> tuple[str, str]:
name = release.get("name") or release["tag_name"]
summary = ""
for p in paragraphs(release.get("body") or ""):
if not (p.startswith("#") or p.startswith("![") or p.startswith("<")):
summary = prose_lines(p)
break
return name, summary
def source_meta(source: GitHub, repo: str, ref: str):
gradle = repo_file(source, repo, "app/build.gradle", ref)
code, version = parse_gradle(gradle)
variants = parse_variants(repo_file(source, repo, "tools/release/variants.txt", ref))
signer = repo_file(source, repo, "keystore/release-signer.sha256", ref).strip().lower()
if not SHA256.fullmatch(signer):
raise RuntimeError(f"{repo}:{ref} has an invalid release signer digest")
return code, version, variants, signer
def asset_for_suffix(assets: list[dict], suffix: str, all_suffixes: list[str]):
others = [s for s in all_suffixes if s and s != suffix]
for asset in assets:
name = asset.get("name", "")
if not name.endswith(f"{suffix}.apk"):
continue
if any(name.endswith(f"{other}.apk") for other in others):
continue
digest = (asset.get("digest") or "").lower()
if not digest.startswith("sha256:") or not SHA256.fullmatch(digest[7:]):
continue
return {
"name": name,
"url": asset["browser_download_url"],
"size": int(asset.get("size") or 0),
"sha256": digest[7:],
}
return None
def release_entry(
release: dict,
commit: str,
pr: int,
version: str | None,
version_code: int,
variants: list[tuple[str, str]],
signer: str,
title: str,
summary: str,
prefer_updated: bool = False,
):
suffixes = [suffix for _, suffix in variants]
apks = {}
for package, suffix in variants:
asset = asset_for_suffix(release.get("assets") or [], suffix, suffixes)
if not asset:
continue
asset.update(
{
"packageName": package,
"versionCode": version_code,
"signerSha256": signer,
}
)
apks[package] = asset
return {
"tag": release["tag_name"],
"title": title,
"summary": summary,
"commit": commit,
"pr": pr,
"version": version,
"versionCode": version_code,
"publishedAt": published_millis(release, prefer_updated),
"url": release.get("html_url") or "",
"apks": apks,
}
def release_body_value(body: str, key: str):
match = re.search(rf"(?mi)^\s*{re.escape(key)}:\s*`?([^\s`]+)", body or "")
return match.group(1) if match else None
def main_commit(release: dict) -> str | None:
body = release.get("body") or ""
explicit = release_body_value(body, "Commit")
if explicit and re.fullmatch(r"[0-9a-f]{7,40}", explicit):
return explicit
match = re.search(r"/commit/([0-9a-f]{40})", body)
return match.group(1) if match else None
def build_catalog(source: GitHub, ci: GitHub, source_repo: str, ci_repo: str) -> dict:
try:
stable_release = source.get(f"/repos/{source_repo}/releases/latest")
except ApiError as e:
if e.status != 404:
raise
stable_release = None
stable = None
if stable_release:
stable_commit = resolve_tag(source, source_repo, stable_release["tag_name"])
code, version_name, variants, signer = source_meta(source, source_repo, stable_commit)
title, summary = stable_description(stable_release)
stable = release_entry(
stable_release,
stable_commit,
0,
version_name or stable_release["tag_name"].removeprefix("v"),
code,
variants,
signer,
title,
summary,
)
releases = ci.get(f"/repos/{ci_repo}/releases?per_page=100")
previews = []
tests = []
for release in releases:
if release.get("draft"):
continue
tag = release.get("tag_name") or ""
main_match = MAIN_TAG.fullmatch(tag)
if main_match:
commit = main_commit(release)
if not commit:
continue
try:
code, _, variants, signer = source_meta(source, source_repo, commit)
except ApiError:
continue
title, summary = ci_description(release)
previews.append(
release_entry(release, commit, 0, None, code, variants, signer, title, summary)
)
continue
pr_match = PR_TAG.fullmatch(tag)
if not pr_match:
continue
pr = int(pr_match.group(1))
body = release.get("body") or ""
if f"github.com/{source_repo}/pull/{pr}" not in body:
continue
try:
pull = source.get(f"/repos/{source_repo}/pulls/{pr}")
except ApiError:
continue
if pull.get("state") != "open" or pull.get("draft") or pull.get("base", {}).get("ref") != "main":
continue
commit = release_body_value(body, "Commit")
if not commit or not re.fullmatch(r"[0-9a-f]{7,40}", commit):
continue
base_ref = release_body_value(body, "Base") or pull["base"]["sha"]
try:
base_code, _, variants, signer = source_meta(source, source_repo, base_ref)
except ApiError:
continue
version_code_text = release_body_value(body, "VersionCode")
if version_code_text and version_code_text.isdigit():
version_code = int(version_code_text)
else:
# Legacy test releases did not record it. If the PR did not change build.gradle,
# the base value is exact; otherwise use the PR head's value.
version_code = base_code
try:
changed = source.get(f"/repos/{source_repo}/pulls/{pr}/files?per_page=100")
if any(item.get("filename") == "app/build.gradle" for item in changed):
head_repo = pull["head"]["repo"]["full_name"]
version_code = parse_gradle(repo_file(source, head_repo, "app/build.gradle", pull["head"]["sha"]))[0]
except (ApiError, KeyError):
pass
title, summary = ci_description(release)
tests.append(
release_entry(
release,
commit,
pr,
None,
version_code,
variants,
signer,
title,
summary,
prefer_updated=True,
)
)
previews.sort(key=lambda r: r["publishedAt"], reverse=True)
tests.sort(key=lambda r: r["publishedAt"], reverse=True)
return {
"schema": 1,
"sourceRepo": source_repo,
"ciRepo": ci_repo,
"generatedAt": int(time.time() * 1000),
"stable": stable,
"preview": previews[0] if previews else None,
"tests": tests,
}
def current_file(ci: GitHub, repo: str, path: str):
try:
return ci.get(f"/repos/{repo}/contents/{q(path)}?ref=main")
except ApiError as e:
if e.status == 404:
return None
raise
def publish_catalog(source: GitHub, ci: GitHub, source_repo: str, ci_repo: str, path: str):
for attempt in range(4):
catalog = build_catalog(source, ci, source_repo, ci_repo)
content = (json.dumps(catalog, indent=2, sort_keys=False) + "\n").encode()
old = current_file(ci, ci_repo, path)
body = {
"message": "ci: refresh DroidDeck update catalog",
"content": base64.b64encode(content).decode(),
"branch": "main",
}
if old:
body["sha"] = old["sha"]
try:
old_bytes = base64.b64decode(old["content"])
old_json = json.loads(old_bytes)
old_json.pop("generatedAt", None)
new_json = dict(catalog)
new_json.pop("generatedAt", None)
if old_json == new_json:
print("catalog contents are already current")
return catalog
except Exception:
pass
try:
ci.put(f"/repos/{ci_repo}/contents/{q(path)}", body)
print(f"published https://raw.githubusercontent.com/{ci_repo}/main/{path}")
return catalog
except ApiError as e:
if e.status not in (409, 422) or attempt == 3:
raise
time.sleep(1 + attempt)
raise RuntimeError("catalog publish retry loop exhausted")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--source-repo", default="Droid-Deck/DroidDeck")
parser.add_argument("--ci-repo", default="Droid-Deck/DroidDeck-CI")
parser.add_argument("--path", default="catalog.json")
parser.add_argument("--dry-run", action="store_true")
args = parser.parse_args()
source = GitHub(os.environ.get("SOURCE_GITHUB_TOKEN", ""))
ci = GitHub(os.environ.get("CI_GITHUB_TOKEN", os.environ.get("SOURCE_GITHUB_TOKEN", "")))
if args.dry_run:
print(json.dumps(build_catalog(source, ci, args.source_repo, args.ci_repo), indent=2))
else:
if not os.environ.get("CI_GITHUB_TOKEN"):
raise SystemExit("CI_GITHUB_TOKEN is required to publish")
publish_catalog(source, ci, args.source_repo, args.ci_repo, args.path)
if __name__ == "__main__":
main()
+91
View File
@@ -0,0 +1,91 @@
import unittest
from tools.release.update_catalog import (
asset_for_suffix,
ci_description,
parse_gradle,
parse_variants,
published_millis,
release_body_value,
)
class UpdateCatalogTest(unittest.TestCase):
def test_gradle_metadata(self):
code, name = parse_gradle("""android {
defaultConfig {
versionCode 12
versionName "0.3.1"
}
}
""")
self.assertEqual(12, code)
self.assertEqual("0.3.1", name)
def test_variants(self):
rows = parse_variants("""
# name package suffix
standard com.droiddeck.launcher -
pubg com.tencent.ig -pubg
""")
self.assertEqual(
[("com.droiddeck.launcher", ""), ("com.tencent.ig", "-pubg")],
rows,
)
def test_standard_asset_does_not_take_a_variant(self):
assets = [
{
"name": "DroidDeck-main-deadbee-pubg.apk",
"size": 10,
"digest": "sha256:" + "a" * 64,
"browser_download_url": "https://example/pubg",
},
{
"name": "DroidDeck-main-deadbee.apk",
"size": 11,
"digest": "sha256:" + "b" * 64,
"browser_download_url": "https://example/standard",
},
]
standard = asset_for_suffix(assets, "", ["", "-pubg"])
pubg = asset_for_suffix(assets, "-pubg", ["", "-pubg"])
self.assertEqual("DroidDeck-main-deadbee.apk", standard["name"])
self.assertEqual("DroidDeck-main-deadbee-pubg.apk", pubg["name"])
def test_ci_description(self):
title, summary = ci_description(
{
"tag_name": "main-deadbee",
"name": "main deadbee",
"body": (
"**Merged to main** - [PR #9](https://github.com/Droid-Deck/DroidDeck/pull/9): "
"fix: keep Steam alive (merged 2026-09-30)\n\n"
"Steam no longer **exits** on resume.\n\n"
"Signed build of https://example"
),
}
)
self.assertEqual("fix: keep Steam alive", title)
self.assertEqual("Steam no longer exits on resume.", summary)
def test_release_body_metadata(self):
tick = chr(96)
body = f"Commit: {tick}abcdef1234567{tick}\nBase: 1234567\nVersionCode: 42\n"
self.assertEqual("abcdef1234567", release_body_value(body, "Commit"))
self.assertEqual("1234567", release_body_value(body, "Base"))
self.assertEqual("42", release_body_value(body, "VersionCode"))
def test_republished_pr_uses_updated_time_but_other_channels_use_publish_time(self):
release = {
"published_at": "2026-09-30T10:00:00Z",
"updated_at": "2026-09-30T12:00:00Z",
}
self.assertLess(
published_millis(release),
published_millis(release, prefer_updated=True),
)
if __name__ == "__main__":
unittest.main()