From 3aa6e1ec2cf3a6dddb0f665b2d55f1e3cd8a5c95 Mon Sep 17 00:00:00 2001 From: efiten Date: Wed, 11 Nov 2026 21:37:10 +0100 Subject: [PATCH] android: tell on Home when a newer version exists Google Play updates silently and late, and a GitHub APK never updates by itself. At start the app asks Play's in-app update API (@capawesome/capacitor-app-update 8.1.1); for a Play install Play decides, and a tap starts the update in Play. Any other install compares its version with version.json of its deployment, now written by every web build. --- android/app/capacitor.build.gradle | 1 + android/capacitor.settings.gradle | 3 ++ docs/user-guide.md | 4 +++ index.html | 6 ++++ package-lock.json | 20 ++++++++++++++ package.json | 1 + src/app.js | 31 ++++++++++++++++++++- src/appupdate.js | 44 ++++++++++++++++++++++++++++++ src/platform.js | 4 +++ test/appupdate.test.mjs | 36 ++++++++++++++++++++++++ vite.config.js | 8 ++++++ 11 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/appupdate.js create mode 100644 test/appupdate.test.mjs diff --git a/android/app/capacitor.build.gradle b/android/app/capacitor.build.gradle index 84b6cfe..57c3224 100644 --- a/android/app/capacitor.build.gradle +++ b/android/app/capacitor.build.gradle @@ -15,6 +15,7 @@ dependencies { implementation project(':capacitor-filesystem') implementation project(':capacitor-local-notifications') implementation project(':capacitor-share') + implementation project(':capawesome-capacitor-app-update') } diff --git a/android/capacitor.settings.gradle b/android/capacitor.settings.gradle index 3d21277..e4956da 100644 --- a/android/capacitor.settings.gradle +++ b/android/capacitor.settings.gradle @@ -19,3 +19,6 @@ project(':capacitor-local-notifications').projectDir = new File('../node_modules include ':capacitor-share' project(':capacitor-share').projectDir = new File('../node_modules/@capacitor/share/android') + +include ':capawesome-capacitor-app-update' +project(':capawesome-capacitor-app-update').projectDir = new File('../node_modules/@capawesome/capacitor-app-update/android') diff --git a/docs/user-guide.md b/docs/user-guide.md index 3f8082f..d2a8456 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -90,6 +90,10 @@ uninstall or remove. The web app is called **CoreDrive RX Web** on the home scre app **CoreDrive RX**. A web app installed before that rename may keep its old name until Chrome refreshes it, so both icons can read "CoreDrive RX" for a while. +Google Play updates apps by itself, often only a day later, and without a message. So the app +checks at start: when a newer version exists, a blue note on Home says so. Tap it to update +through Google Play. An APK from GitHub gets the same note, with a link to the releases page. + Some phones close apps in the background to save battery, even while the capture notification shows. A OnePlus did that during a test walk, and capture stopped without a warning. Before you drive, open the phone's **Settings > Apps > CoreDrive RX > Battery** and choose **Unrestricted**, diff --git a/index.html b/index.html index f601680..c6a94a3 100644 --- a/index.html +++ b/index.html @@ -25,7 +25,11 @@ --bgchip-bg: #3a2e10; --bgchip-border: #6b5212; --bgchip-text: #e6a23c; + --updchip-bg: #10283a; + --updchip-border: #1d4a6b; + --updchip-text: #8cc8ff; } + .updchip { display: none; cursor: pointer; background: var(--updchip-bg); color: var(--updchip-text); border: 1px solid var(--updchip-border); border-radius: 8px; padding: 10px 12px; margin: 10px 0; font-size: 13px; overflow-wrap: anywhere; } .bgchip { display: none; cursor: pointer; background: var(--bgchip-bg); color: var(--bgchip-text); border: 1px solid var(--bgchip-border); border-radius: 8px; padding: 10px 12px; margin: 10px 0; font-size: 13px; } dialog.tip { max-width: 420px; width: calc(100% - 48px); box-sizing: border-box; background: var(--tip-bg); color: var(--tip-text); border: 1px solid var(--tip-border); border-radius: 10px; padding: 16px; font-size: 14px; line-height: 1.45; } dialog.tip::backdrop { background: var(--tip-backdrop); } @@ -133,6 +137,8 @@
+ +
diff --git a/package-lock.json b/package-lock.json index 2604afb..56f4825 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@capacitor/filesystem": "8.1.5", "@capacitor/local-notifications": "8.3.1", "@capacitor/share": "8.0.3", + "@capawesome/capacitor-app-update": "8.1.1", "@noble/ciphers": "2.0.1", "@noble/ed25519": "^3.2.0", "mqtt": "^5.10.1" @@ -164,6 +165,25 @@ "integrity": "sha512-/C1FUo8/OkKuAT4nCIu/34ny9siNHr9qtFezu4kxm6GY1wNFxrCFWjfYx5C1tUhVGz3fxBABegupkpjXvjCHrw==", "license": "ISC" }, + "node_modules/@capawesome/capacitor-app-update": { + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/@capawesome/capacitor-app-update/-/capacitor-app-update-8.1.1.tgz", + "integrity": "sha512-1TzM4hMN4wgCDu3O0UhaIIoVspUFNfNJ+q+K/EKSQRKkfCbG79S1Fn/sXwMdtMO1Me55LQxdjw1I9rctFtKBOQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/capawesome-team/" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/capawesome" + } + ], + "license": "MIT", + "peerDependencies": { + "@capacitor/core": ">=8.0.0" + } + }, "node_modules/@esbuild/aix-ppc64": { "version": "0.25.12", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.12.tgz", diff --git a/package.json b/package.json index 6536602..b0f806d 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "@capacitor/filesystem": "8.1.5", "@capacitor/local-notifications": "8.3.1", "@capacitor/share": "8.0.3", + "@capawesome/capacitor-app-update": "8.1.1", "@noble/ciphers": "2.0.1", "@noble/ed25519": "^3.2.0", "mqtt": "^5.10.1" diff --git a/src/app.js b/src/app.js index 5954495..e856944 100644 --- a/src/app.js +++ b/src/app.js @@ -7,7 +7,8 @@ // Home is a pure monitor (counters, status strip, last-reception SNR meter, recently // heard). Discover runs automatically with a traffic backoff (see monitor.js). Config // and diagnostics live on the Settings tab. -import { createTransport, createGps, isNativeApp, nativeHttpGet, nativeShareFile, qrScanner } from './platform.js'; +import { createTransport, createGps, isNativeApp, nativeHttpGet, nativeShareFile, qrScanner, appUpdater } from './platform.js'; +import { updateNotice, parseVersionJson } from './appupdate.js'; import { needsBatteryTip, markBatteryTipSeen } from './batterytip.js'; import { usableFix, startSpell, endSpell, spellText } from './background.js'; import { scanDeployment } from './qrscan.js'; @@ -282,6 +283,33 @@ function setupDeployment() { return base; } +// checkForUpdate shows the update note on Home when a newer version exists +// (src/appupdate.js decides). Android app only; runs once per start, never blocks it. +async function checkForUpdate(base) { + const up = appUpdater(); + let play = null; + try { play = await up.getAppUpdateInfo(); } catch (e) { dbg('update check: Google Play not asked (' + e.message + ')', 'st'); } + let deployVersion = null; + if (!play && base) { + try { + const r = await nativeHttpGet(base + 'version.json'); + if (r.status === 200) deployVersion = parseVersionJson(typeof r.data === 'string' ? r.data : JSON.stringify(r.data)); + } catch (e) { /* no network: the next start asks again */ } + } + const n = updateNotice({ appVersion: VERSION, play, deployVersion }); + if (!n) return; + dbg('update: ' + n.text, 'st'); + const el = els('updchip'); + el.textContent = n.text; + el.style.display = 'block'; + el.onclick = () => { + el.style.display = 'none'; + if (n.kind !== 'play') return; + const go = play.immediateUpdateAllowed ? up.performImmediateUpdate() : up.openAppStore(); + go.catch((e) => dbg('update: Google Play did not start the update (' + e.message + ')', 'no')); + }; +} + // scanDeploymentQr reads the deployment from a QR code and goes through the same check // as a typed URL. async function scanDeploymentQr() { @@ -2064,6 +2092,7 @@ window.addEventListener('DOMContentLoaded', async () => { const deployment = isNativeApp() ? setupDeployment() : null; if (deployment === '') switchView('settings'); if (deployment) dbg('deployment: ' + deployment, 'st'); + if (isNativeApp()) checkForUpdate(deployment); // not awaited try { await loadConfig(); applyConfigToSettings(); diff --git a/src/appupdate.js b/src/appupdate.js new file mode 100644 index 0000000..033e92c --- /dev/null +++ b/src/appupdate.js @@ -0,0 +1,44 @@ +// Update notice for the Android app. Google Play updates apps silently, often a day later, +// and a GitHub APK never updates by itself, so the app says when a newer version exists. +// +// Two sources, in order: +// 1. Google Play's in-app update check. It only answers for an install from Play; then +// it decides alone, also when it says "no update yet" (Play may still be rolling out). +// 2. Otherwise (a GitHub APK, or Play could not be asked): version.json of the +// deployment the app uses, written by every web build (vite.config.js). + +export const PLAY_UPDATE_AVAILABLE = 2; // AppUpdateAvailability.UPDATE_AVAILABLE +export const RELEASES_URL = 'https://github.com/efiten/coredrive-rx/releases'; + +// cmpVersion compares "x.y.z" strings: >0 when a is newer. Anything unparsable is 0. +export function cmpVersion(a, b) { + const p = (v) => (/^\d+\.\d+\.\d+$/.test(String(v)) ? String(v).split('.').map(Number) : null); + const x = p(a), y = p(b); + if (!x || !y) return 0; + for (let i = 0; i < 3; i++) if (x[i] !== y[i]) return x[i] - y[i]; + return 0; +} + +// updateNotice returns { kind: 'play' | 'info', text } or null. +// play: the Play update info, or null when Play could not be asked (not a Play install) +// deployVersion: version from the deployment's version.json, or null +export function updateNotice({ appVersion, play, deployVersion }) { + if (play) { + if (play.updateAvailability !== PLAY_UPDATE_AVAILABLE) return null; + return { kind: 'play', text: 'A new version of CoreDrive RX is ready in Google Play. Tap to update.' }; + } + if (cmpVersion(deployVersion, appVersion) <= 0) return null; + return { + kind: 'info', + text: 'CoreDrive RX ' + deployVersion + ' is out, you have ' + appVersion + '. Update in Google Play, or install the APK from ' + + RELEASES_URL + '. Tap to dismiss.', + }; +} + +// parseVersionJson reads {"version":"x.y.z"}; null for anything else. +export function parseVersionJson(text) { + try { + const v = JSON.parse(text).version; + return cmpVersion(v, v) === 0 && /^\d+\.\d+\.\d+$/.test(v) ? v : null; + } catch (e) { return null; } +} diff --git a/src/platform.js b/src/platform.js index a5db002..318e7b3 100644 --- a/src/platform.js +++ b/src/platform.js @@ -7,6 +7,7 @@ import { BarcodeScanner } from '@capacitor-mlkit/barcode-scanning'; import { Filesystem, Directory, Encoding } from '@capacitor/filesystem'; import { Share } from '@capacitor/share'; import { LocalNotifications } from '@capacitor/local-notifications'; +import { AppUpdate } from '@capawesome/capacitor-app-update'; import { WebBluetoothTransport } from './transport.js'; import { CapacitorBleTransport } from './transport-native.js'; import { Gps } from './gps.js'; @@ -33,6 +34,9 @@ export async function nativeShareFile(text, filename) { await Share.share({ title: filename, text: 'CoreDrive RX debug log', files: [uri] }); } +// Google Play's in-app update check, for src/appupdate.js. +export function appUpdater() { return AppUpdate; } + // The Google code scanner plugin, for src/qrscan.js. export function qrScanner() { return BarcodeScanner; } diff --git a/test/appupdate.test.mjs b/test/appupdate.test.mjs new file mode 100644 index 0000000..ae1ab6b --- /dev/null +++ b/test/appupdate.test.mjs @@ -0,0 +1,36 @@ +// The Android app says when a newer version exists: Google Play decides for a Play +// install, the deployment's version.json for any other install. +// Run: node --test +import { test } from 'node:test'; +import assert from 'node:assert'; +import { cmpVersion, updateNotice, parseVersionJson, PLAY_UPDATE_AVAILABLE } from '../src/appupdate.js'; + +test('versions compare per number, not as text', () => { + assert.ok(cmpVersion('1.25.0', '1.24.5') > 0); + assert.ok(cmpVersion('1.10.0', '1.9.9') > 0); + assert.equal(cmpVersion('1.25.0', '1.25.0'), 0); + assert.ok(cmpVersion('1.24.5', '1.25.0') < 0); + assert.equal(cmpVersion('dev', '1.25.0'), 0); + assert.equal(cmpVersion(null, '1.25.0'), 0); +}); + +test('a Play install follows Play, also when Play has nothing yet', () => { + assert.equal(updateNotice({ appVersion: '1.25.0', play: { updateAvailability: PLAY_UPDATE_AVAILABLE }, deployVersion: null }).kind, 'play'); + // the deployment is newer, but Play has not rolled it out to this user: no notice + assert.equal(updateNotice({ appVersion: '1.25.0', play: { updateAvailability: 1 }, deployVersion: '1.26.0' }), null); +}); + +test('any other install compares with the deployment', () => { + const n = updateNotice({ appVersion: '1.25.0', play: null, deployVersion: '1.26.0' }); + assert.equal(n.kind, 'info'); + assert.match(n.text, /1\.26\.0 is out, you have 1\.25\.0/); + assert.equal(updateNotice({ appVersion: '1.25.0', play: null, deployVersion: '1.25.0' }), null); + assert.equal(updateNotice({ appVersion: '1.25.0', play: null, deployVersion: '1.24.5' }), null); // older deployment + assert.equal(updateNotice({ appVersion: '1.25.0', play: null, deployVersion: null }), null); +}); + +test('version.json is read strictly', () => { + assert.equal(parseVersionJson('{"version":"1.25.0"}'), '1.25.0'); + assert.equal(parseVersionJson('{"version":"dev"}'), null); + assert.equal(parseVersionJson('404'), null); +}); diff --git a/vite.config.js b/vite.config.js index c17fd8b..042c934 100644 --- a/vite.config.js +++ b/vite.config.js @@ -8,4 +8,12 @@ export default defineConfig({ define: { __APP_VERSION__: JSON.stringify(pkg.version), }, + // version.json next to index.html: the Android app reads it from its deployment to + // tell when a newer version is out (src/appupdate.js). + plugins: [{ + name: 'version-json', + generateBundle() { + this.emitFile({ type: 'asset', fileName: 'version.json', source: JSON.stringify({ version: pkg.version }) + '\n' }); + }, + }], });