Update the launcher in place from the launcher-latest release
CI stamps each APK's versionCode with the workflow run number and uploads version.json next to it. The launcher checks that file on start (at most every 6 hours) and from a new "Check for updates" item in the home menu; when it is newer, it streams the APK into a PackageInstaller session and commits it, so Android asks to confirm (or, on Android 12+ once the launcher installed itself, may install it without asking). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K2oqEoAKVaiTwCNSqRHQUn
This commit is contained in:
9 files changed
+222
-5
No files matched your search
@@ -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."
|
||||
@@ -10,8 +10,10 @@ android {
|
||||
applicationId "dev.ultra.launcher"
|
||||
minSdk 26
|
||||
targetSdk 35
|
||||
versionCode 1
|
||||
versionName "0.1"
|
||||
// CI passes -PversionCode=<workflow run number>; 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 {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<!-- The updater downloads new builds of the launcher and installs them over itself. -->
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
|
||||
|
||||
<!-- Android 11+ package visibility: list every launchable activity. -->
|
||||
<queries>
|
||||
<intent>
|
||||
@@ -32,6 +36,10 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<receiver
|
||||
android:name=".Updater$InstallReceiver"
|
||||
android:exported="false" />
|
||||
|
||||
<!-- Opens Recents for the Windows button; the user turns it on in Accessibility. -->
|
||||
<service
|
||||
android:name=".RecentsService"
|
||||
|
||||
@@ -75,7 +75,8 @@ import java.util.concurrent.Executors;
|
||||
* wicon?p=PROV the provider's icon as a PNG
|
||||
*
|
||||
* and exposes window.Android (launch, appInfo, addWidget, removeWidget, placeWidgets,
|
||||
* widgetLimits, excludeGestures, openSettings, pickWallpaper, openRecents, haptic)
|
||||
* widgetLimits, excludeGestures, openSettings, pickWallpaper, openRecents, checkUpdates,
|
||||
* haptic)
|
||||
* for the app's @jscall imports and index.html.
|
||||
*
|
||||
* Widgets are real AppWidgetHostViews in a layer above the WebView. The page reports where
|
||||
@@ -83,6 +84,8 @@ import java.util.concurrent.Executors;
|
||||
* changes, the home button, back, a new widget and a long press on a widget reach
|
||||
* the page as window.ultraHome.reload() / home() / back() / widgetAdded(id) /
|
||||
* widgetMenu(id).
|
||||
*
|
||||
* Updater keeps the launcher itself up to date from the launcher-latest release.
|
||||
*/
|
||||
public class MainActivity extends Activity {
|
||||
static final String HOST = "appassets.androidplatform.net";
|
||||
@@ -181,6 +184,7 @@ public class MainActivity extends Activity {
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
widgetHost.startListening();
|
||||
Updater.check(this, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -795,6 +799,10 @@ public class MainActivity extends Activity {
|
||||
@JavascriptInterface public void openRecents() {
|
||||
main.post(MainActivity.this::openRecents);
|
||||
}
|
||||
|
||||
@JavascriptInterface public void checkUpdates() {
|
||||
Updater.check(MainActivity.this, true);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ serving
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
package dev.ultra.launcher;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageInstaller;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* Updates the launcher from the rolling "launcher-latest" GitHub prerelease. CI uploads
|
||||
* version.json ({"versionCode": N, "commit": SHA}) next to ultra-launcher.apk, with N the
|
||||
* workflow run number, which is also the APK's versionCode. When N is newer than the installed
|
||||
* build, the APK is streamed into a PackageInstaller session and committed. Android then asks
|
||||
* the user to confirm (or, on Android 12+ once this app installed itself, may not ask at all).
|
||||
*
|
||||
* The home screen checks on start at most every CHECK_EVERY; the home menu's
|
||||
* "Check for updates" checks now and reports what it found.
|
||||
*/
|
||||
final class Updater {
|
||||
static final String BASE = "https://github.com/codegod100/launcher/releases/download/launcher-latest/";
|
||||
static final long CHECK_EVERY = 6 * 60 * 60 * 1000L;
|
||||
static final String EXTRA_VERSION = "dev.ultra.launcher.VERSION";
|
||||
|
||||
private static final AtomicBoolean busy = new AtomicBoolean();
|
||||
private static final Handler main = new Handler(Looper.getMainLooper());
|
||||
|
||||
private Updater() {}
|
||||
|
||||
private static SharedPreferences prefs(Context c) {
|
||||
return c.getSharedPreferences("updater", Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
/** Check in the background; manual checks always run and say what happened. */
|
||||
static void check(Context ctx, boolean manual) {
|
||||
Context c = ctx.getApplicationContext();
|
||||
long now = System.currentTimeMillis();
|
||||
if (!manual && now - prefs(c).getLong("lastCheck", 0) < CHECK_EVERY) return;
|
||||
if (!busy.compareAndSet(false, true)) {
|
||||
if (manual) toast(c, "Already checking for updates");
|
||||
return;
|
||||
}
|
||||
prefs(c).edit().putLong("lastCheck", now).apply();
|
||||
if (manual) toast(c, "Checking for updates…");
|
||||
new Thread(() -> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
+10
@@ -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")
|
||||
|
||||
Binary file not shown.
@@ -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(),
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user