diff --git a/.github/workflows/launcher.yml b/.github/workflows/launcher.yml index 5868fb9..ba624a2 100644 --- a/.github/workflows/launcher.yml +++ b/.github/workflows/launcher.yml @@ -49,7 +49,9 @@ jobs: - uses: gradle/actions/setup-gradle@v5 - name: Build the APK working-directory: android - run: ./gradlew --no-daemon assembleDebug + # The run number only grows, so each build's versionCode is newer than the last + # (it restarts if this workflow is renamed; bump past the old numbers then). + run: ./gradlew --no-daemon assembleDebug -PversionCode=${{ github.run_number }} - uses: actions/upload-artifact@v5 with: name: ultra-launcher-apk @@ -61,7 +63,9 @@ jobs: GH_TOKEN: ${{ github.token }} run: | cp android/app/build/outputs/apk/debug/app-debug.apk ultra-launcher.apk + # The launcher's updater reads this to tell whether the APK is newer than it. + echo '{"versionCode": ${{ github.run_number }}, "commit": "'"$GITHUB_SHA"'"}' > version.json gh release delete launcher-latest --yes --cleanup-tag || true - gh release create launcher-latest ultra-launcher.apk --prerelease --target "$GITHUB_SHA" \ + gh release create launcher-latest ultra-launcher.apk version.json --prerelease --target "$GITHUB_SHA" \ --title "ultra launcher (latest build)" \ --notes "Debug-signed APK of android/ built from $GITHUB_SHA. Install it, press Home, and pick ultra Home." diff --git a/android/app/build.gradle b/android/app/build.gradle index f03dc5b..3217375 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -10,8 +10,10 @@ android { applicationId "dev.ultra.launcher" minSdk 26 targetSdk 35 - versionCode 1 - versionName "0.1" + // CI passes -PversionCode=; the in-app updater compares it + // with the versionCode in the launcher-latest release's version.json. + versionCode((project.findProperty("versionCode") ?: "1") as Integer) + versionName("0.1." + versionCode) } compileOptions { diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 59880fe..15b17b7 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,6 +1,10 @@ + + + + @@ -32,6 +36,10 @@ + + { + try { + run(c, manual); + } catch (Exception e) { + Log.w("ultra", "update check", e); + if (manual) toast(c, "Couldn't check for updates"); + } finally { + busy.set(false); + } + }, "updater").start(); + } + + private static void run(Context c, boolean manual) throws Exception { + JSONObject v = new JSONObject(new String(get(BASE + "version.json"), StandardCharsets.UTF_8)); + long latest = v.getLong("versionCode"); + long installed = installedVersion(c); + if (latest <= installed) { + if (manual) toast(c, "ultra Home is up to date (build " + installed + ")"); + return; + } + // An install the user cancelled isn't offered again until there is a newer one, or they ask. + if (!manual && latest == prefs(c).getLong("declined", -1)) return; + toast(c, "Downloading launcher update (build " + latest + ")…"); + install(c, latest); + } + + @SuppressWarnings("deprecation") + static long installedVersion(Context c) throws PackageManager.NameNotFoundException { + PackageInfo p = c.getPackageManager().getPackageInfo(c.getPackageName(), 0); + return Build.VERSION.SDK_INT >= 28 ? p.getLongVersionCode() : p.versionCode; + } + + private static void install(Context c, long version) throws IOException { + PackageInstaller pi = c.getPackageManager().getPackageInstaller(); + PackageInstaller.SessionParams params = + new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL); + params.setAppPackageName(c.getPackageName()); + if (Build.VERSION.SDK_INT >= 31) { + params.setRequireUserAction(PackageInstaller.SessionParams.USER_ACTION_NOT_REQUIRED); + } + int id = pi.createSession(params); + try (PackageInstaller.Session s = pi.openSession(id)) { + HttpURLConnection conn = open(BASE + "ultra-launcher.apk"); + try (InputStream in = conn.getInputStream(); + OutputStream out = s.openWrite("ultra-launcher.apk", 0, conn.getContentLengthLong())) { + byte[] buf = new byte[64 * 1024]; + for (int n; (n = in.read(buf)) > 0; ) out.write(buf, 0, n); + s.fsync(out); + } finally { + conn.disconnect(); + } + Intent done = new Intent(c, InstallReceiver.class).putExtra(EXTRA_VERSION, version); + int flags = PendingIntent.FLAG_UPDATE_CURRENT + | (Build.VERSION.SDK_INT >= 31 ? PendingIntent.FLAG_MUTABLE : 0); + s.commit(PendingIntent.getBroadcast(c, id, done, flags).getIntentSender()); + } catch (IOException | RuntimeException e) { + pi.abandonSession(id); + throw e; + } + } + + private static HttpURLConnection open(String url) throws IOException { + HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); + conn.setConnectTimeout(15000); + conn.setReadTimeout(30000); + conn.setUseCaches(false); + conn.setInstanceFollowRedirects(true); // github.com redirects assets to its download host + int code = conn.getResponseCode(); + if (code != HttpURLConnection.HTTP_OK) { + conn.disconnect(); + throw new IOException("HTTP " + code + " for " + url); + } + return conn; + } + + private static byte[] get(String url) throws IOException { + HttpURLConnection conn = open(url); + try (InputStream in = conn.getInputStream()) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buf = new byte[8192]; + for (int n; (n = in.read(buf)) > 0; ) out.write(buf, 0, n); + return out.toByteArray(); + } finally { + conn.disconnect(); + } + } + + static void toast(Context c, String msg) { + main.post(() -> Toast.makeText(c, msg, Toast.LENGTH_LONG).show()); + } + + /** Hears back from the install session: shows Android's confirm screen, or notes the outcome. */ + public static final class InstallReceiver extends BroadcastReceiver { + @Override + @SuppressWarnings("deprecation") + public void onReceive(Context c, Intent intent) { + int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE); + switch (status) { + case PackageInstaller.STATUS_PENDING_USER_ACTION: + Intent confirm = intent.getParcelableExtra(Intent.EXTRA_INTENT); + if (confirm != null) c.startActivity(confirm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); + break; + case PackageInstaller.STATUS_SUCCESS: + break; // the new build replaces this process + case PackageInstaller.STATUS_FAILURE_ABORTED: + prefs(c).edit().putLong("declined", intent.getLongExtra(EXTRA_VERSION, -1)).apply(); + break; + default: + String why = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE); + Log.w("ultra", "update install failed: " + status + " " + why); + Toast.makeText(c, "Launcher update failed" + (why == null ? "" : ": " + why), + Toast.LENGTH_LONG).show(); + } + } + } +} diff --git a/tests/e2e.cjs b/tests/e2e.cjs index 6b7bcb0..646d2cc 100644 --- a/tests/e2e.cjs +++ b/tests/e2e.cjs @@ -21,6 +21,11 @@ const BASE = process.env.BASE || "http://localhost:8000"; await p.mouse.move(r.x + r.width / 2, r.y + r.height * fy); await p.mouse.down(); await p.waitForTimeout(650); await p.mouse.up(); await settle(); }; + // the home menu's update check goes to the shell and closes the menu + await longpress(".hint"); + await p.click(".sheet-item >> text=Check for updates"); await settle(); + assert.equal(await p.textContent("#toast"), "Check for updates"); + assert.ok(!(await p.$(".sheet")), "the menu closes"); const homeApps = () => p.$$eval(".slots .name", (ns) => ns.map((n) => n.textContent)); // long press on the blank home: add apps through the drawer's pick mode await longpress(".hint"); diff --git a/web/app.py b/web/app.py index 9332c1f..7c2b0e6 100644 --- a/web/app.py +++ b/web/app.py @@ -51,6 +51,11 @@ def open_recents() -> None: """Show the system's list of open windows (Recents).""" pass +@jscall +def check_updates() -> None: + """Look for a newer launcher build and install it (the shell shows its own progress).""" + pass + @jscall def haptic() -> None: pass @@ -95,6 +100,7 @@ CHOOSE_FIRST_WIDGET: Const[i32] = 39 RESIZE: Const[i32] = 40 # arg: columns * 4096 + height in px for the widget being resized RESIZE_DONE: Const[i32] = 41 GRAB: Const[i32] = 42 # arg: the widget slot showing handles, picked up by its body +CHECK_UPDATES: Const[i32] = 43 CAP: Const[i32] = 512 HOME_CAP: Const[i32] = 40 @@ -553,6 +559,9 @@ def update(msg: i32, arg: i32) -> None: sheet = NONE elif msg == RECENTS: open_recents() + elif msg == CHECK_UPDATES: + check_updates() + sheet = NONE elif msg == NOTICE_DONE: if arg == notice_gen: s_clear(notice) @@ -804,6 +813,7 @@ def view_sheet() -> None: sheet_btn("Add widget", PICK_WIDGET, 0) sheet_btn("Wallpaper", WALLPAPER, 0) sheet_btn("Settings", SETTINGS, 0) + sheet_btn("Check for updates", CHECK_UPDATES, 0) elif sheet == SHEET_ITEM and item >= 0: el("h2") cls("sheet-title") diff --git a/web/app.wasm b/web/app.wasm index 010cdaf..157c2fb 100644 Binary files a/web/app.wasm and b/web/app.wasm differ diff --git a/web/index.html b/web/index.html index f5afffd..184191a 100644 --- a/web/index.html +++ b/web/index.html @@ -32,6 +32,7 @@ openSettings: () => toast("Open system settings"), pickWallpaper: () => toast("Pick a wallpaper"), openRecents: () => toast("Show open windows"), + checkUpdates: () => toast("Check for updates"), showKeyboard: () => {}, hideKeyboard: () => {}, haptic: () => navigator.vibrate?.(10), @@ -125,6 +126,7 @@ open_settings: () => host.openSettings(), pick_wallpaper: () => host.pickWallpaper(), open_recents: () => host.openRecents(), + check_updates: () => host.checkUpdates(), haptic: () => host.haptic(), }), });