Qt: bring over the WebView build's home screen
The WebView build saved its home in its page's localStorage, which stays in the app's data now that the Qt build is dev.ultra.launcher. On first start HomeActivity reads the ultra.home.* keys through a throwaway WebView on the same origin and hands them to QML: apps by component, widgets with their px heights turned into rows, pins kept when the Qt home is still empty. The widget host id goes back to the WebView build's, so its widgets stay bound. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hiQsCq11CwCPnKPp9D
This commit is contained in:
8 files changed
+130
-2
No files matched your search
@@ -68,7 +68,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
public class HomeActivity extends QtActivity {
|
||||
/** The GitHub release the updater installs from. */
|
||||
static final String RELEASE = "launcher-qt-latest";
|
||||
static final int HOST_ID = 0x0172;
|
||||
// The WebView build's host id: its widgets are bound to it, so they carry over.
|
||||
static final int HOST_ID = 0x0171;
|
||||
static final int REQ_BIND = 1, REQ_CONFIGURE = 2;
|
||||
|
||||
private static volatile HomeActivity self;
|
||||
@@ -141,9 +142,60 @@ public class HomeActivity extends QtActivity {
|
||||
}
|
||||
qtReady = true;
|
||||
Native.insets(a.insetTop, a.insetBottom);
|
||||
a.readLegacyHome();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The WebView build kept its home screen in its page's localStorage, which lives on in
|
||||
* this app's data dir. Read it once, through a throwaway WebView on the same origin,
|
||||
* and hand the ultra.home.* keys to QML, which merges them into the Qt home.
|
||||
*/
|
||||
private void readLegacyHome() {
|
||||
final android.content.SharedPreferences prefs = getSharedPreferences("qt", MODE_PRIVATE);
|
||||
if (prefs.getBoolean("legacyRead", false)) return;
|
||||
final android.webkit.WebView w;
|
||||
try {
|
||||
w = new android.webkit.WebView(this);
|
||||
} catch (RuntimeException e) {
|
||||
Log.w("ultra", "no WebView to read the old home from", e);
|
||||
return;
|
||||
}
|
||||
w.getSettings().setJavaScriptEnabled(true);
|
||||
w.getSettings().setDomStorageEnabled(true);
|
||||
final boolean[] done = {false};
|
||||
w.setWebViewClient(new android.webkit.WebViewClient() {
|
||||
@Override
|
||||
public android.webkit.WebResourceResponse shouldInterceptRequest(
|
||||
android.webkit.WebView v, android.webkit.WebResourceRequest r) {
|
||||
return new android.webkit.WebResourceResponse("text/html", "utf-8",
|
||||
new java.io.ByteArrayInputStream("<!doctype html><title>.</title>".getBytes()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageFinished(android.webkit.WebView v, String url) {
|
||||
if (done[0]) return;
|
||||
done[0] = true;
|
||||
v.evaluateJavascript(LEGACY_JS, value -> {
|
||||
String json = null;
|
||||
try {
|
||||
json = new JSONArray("[" + value + "]").optString(0, null);
|
||||
} catch (JSONException e) {
|
||||
Log.w("ultra", "old home unreadable: " + value);
|
||||
}
|
||||
prefs.edit().putBoolean("legacyRead", true).apply();
|
||||
if (json != null && json.startsWith("{")) Native.legacyHome(json);
|
||||
v.destroy();
|
||||
});
|
||||
}
|
||||
});
|
||||
w.loadUrl("https://appassets.androidplatform.net/index.html");
|
||||
}
|
||||
|
||||
private static final String LEGACY_JS =
|
||||
"(function(){var o={};for(var i=0;i<localStorage.length;i++){var k=localStorage.key(i);"
|
||||
+ "if(k.indexOf('ultra.home.')===0)o[k]=localStorage.getItem(k);}return JSON.stringify(o);})()";
|
||||
|
||||
private void onInsets(WindowInsets insets) {
|
||||
if (Build.VERSION.SDK_INT >= 30) {
|
||||
Insets bars = insets.getInsets(WindowInsets.Type.systemBars() | WindowInsets.Type.displayCutout());
|
||||
|
||||
@@ -17,4 +17,5 @@ final class Native {
|
||||
static native void widgetGrab(int id);
|
||||
static native void widgetDrag(int x, int y);
|
||||
static native void widgetDrop(boolean moved);
|
||||
static native void legacyHome(String json); // the WebView build's ultra.home.* keys
|
||||
}
|
||||
@@ -137,3 +137,25 @@ function widgetCols(px, colW, cols) {
|
||||
var k = Math.max(1, Math.ceil((px + CGAP - 8) / (colW + CGAP)));
|
||||
return k >= cols ? 0 : k;
|
||||
}
|
||||
|
||||
// The WebView build's saved home (its ultra.home.* localStorage keys) as Qt slots.
|
||||
// Widget heights were px; they become whole rows.
|
||||
function legacy(kv) {
|
||||
var out = [];
|
||||
var n = parseInt(kv["ultra.home.n"], 10) || 0;
|
||||
var num = function (k, i, d) {
|
||||
var v = parseInt(kv["ultra.home." + k + "." + i], 10);
|
||||
return isNaN(v) ? d : v;
|
||||
};
|
||||
for (var i = 0; i < n; i++) {
|
||||
var str = kv["ultra.home.str." + i];
|
||||
if (typeof str !== "string" || !str) continue;
|
||||
var x = num("cx", i, -1), y = num("cy", i, -1);
|
||||
if (num("kind", i, 0) === 1)
|
||||
out.push({kind: "widget", str: str, wid: num("wid", i, 0), w: Math.max(0, num("w", i, 0)),
|
||||
rows: widgetRows(num("h", i, 120)), x: x, y: y});
|
||||
else
|
||||
out.push({kind: "app", str: str, x: x, y: y});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -91,6 +91,26 @@ Window {
|
||||
home = out;
|
||||
}
|
||||
|
||||
// Bring over the WebView build's home screen (once, on Android). An empty Qt home
|
||||
// takes it as it was; otherwise whatever isn't here yet goes after it, unpinned.
|
||||
function importLegacy(json) {
|
||||
let kv;
|
||||
try { kv = JSON.parse(json); } catch (e) { return; }
|
||||
const old = L.legacy(kv || {});
|
||||
if (old.length === 0) return;
|
||||
const has = {};
|
||||
for (const s of home) has[s.kind === "widget" ? "w" + s.wid : "a" + s.str] = true;
|
||||
const fresh = home.length === 0;
|
||||
for (const s of old) {
|
||||
if (home.length >= homeCap) break;
|
||||
if (has[s.kind === "widget" ? "w" + s.wid : "a" + s.str]) continue;
|
||||
if (!fresh) { s.x = -1; s.y = -1; }
|
||||
home.push(s);
|
||||
}
|
||||
commit();
|
||||
say("Brought over your home screen");
|
||||
}
|
||||
|
||||
function loadApps() {
|
||||
const list = Host.apps();
|
||||
const rows = {};
|
||||
@@ -391,6 +411,7 @@ Window {
|
||||
}
|
||||
function onWidgetDrop(moved) { root.drop(moved) }
|
||||
function onToast(text) { root.say(text) }
|
||||
function onLegacyHome(json) { root.importLegacy(json) }
|
||||
}
|
||||
|
||||
Component.onCompleted: {
|
||||
|
||||
@@ -55,6 +55,13 @@ void jWidgetDrag(JNIEnv *, jclass, jint x, jint y) {
|
||||
}
|
||||
void jWidgetDrop(JNIEnv *, jclass, jboolean moved) { post([=] { emit g_backend->widgetDrop(moved); }); }
|
||||
|
||||
void jLegacyHome(JNIEnv *env, jclass, jstring json) {
|
||||
const char *c = env->GetStringUTFChars(json, nullptr);
|
||||
const QString s = QString::fromUtf8(c);
|
||||
env->ReleaseStringUTFChars(json, c);
|
||||
post([=] { emit g_backend->legacyHome(s); });
|
||||
}
|
||||
|
||||
void registerNatives() {
|
||||
static bool done = false;
|
||||
if (done) return;
|
||||
@@ -69,6 +76,7 @@ void registerNatives() {
|
||||
{"widgetGrab", "(I)V", reinterpret_cast<void *>(jWidgetGrab)},
|
||||
{"widgetDrag", "(II)V", reinterpret_cast<void *>(jWidgetDrag)},
|
||||
{"widgetDrop", "(Z)V", reinterpret_cast<void *>(jWidgetDrop)},
|
||||
{"legacyHome", "(Ljava/lang/String;)V", reinterpret_cast<void *>(jLegacyHome)},
|
||||
};
|
||||
QJniEnvironment env;
|
||||
env.registerNativeMethods("dev/ultra/launcher/Native", methods, int(std::size(methods)));
|
||||
|
||||
@@ -73,6 +73,7 @@ signals:
|
||||
void widgetGrab(int id);
|
||||
void widgetDrag(qreal x, qreal y); // global logical px
|
||||
void widgetDrop(bool moved);
|
||||
void legacyHome(const QString &json); // the WebView build's saved home, read once
|
||||
void toast(const QString &text); // desktop stand-in for an action
|
||||
|
||||
private:
|
||||
|
||||
@@ -346,6 +346,16 @@ int main(int argc, char *argv[]) {
|
||||
js("loadHome()");
|
||||
eq(homeLabels(), "Gmail,Chrome,Phone", "the layout is saved");
|
||||
|
||||
// The WebView build's saved home merges in: new slots go after, unpinned.
|
||||
emit backend.legacyHome(QStringLiteral(
|
||||
"{\"ultra.home.n\":\"3\",\"ultra.home.str.0\":\"com.example.clock/.MainActivity\","
|
||||
"\"ultra.home.cx.0\":\"4\",\"ultra.home.cy.0\":\"4\",\"ultra.home.str.1\":\"com.example.chrome/.MainActivity\","
|
||||
"\"ultra.home.kind.2\":\"1\",\"ultra.home.str.2\":\"Weather\",\"ultra.home.wid.2\":\"7\",\"ultra.home.h.2\":\"200\"}"));
|
||||
settle(50);
|
||||
eq(homeLabels(), "Gmail,Chrome,Phone,Clock,[Weather]", "the old home is brought over");
|
||||
eq(js("home[3].x"), -1, "unpinned when merged");
|
||||
eq(js("home[4].rows"), 3, "widget px become rows");
|
||||
|
||||
// back closes things in order; home closes everything
|
||||
swipe(QPointF(206, 400), QPointF(206, 600));
|
||||
js("back()");
|
||||
|
||||
@@ -3,7 +3,7 @@ import { readFileSync } from "node:fs";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
const src = readFileSync(new URL("../qml/Layout.js", import.meta.url), "utf8").replace(".pragma library", "");
|
||||
const L = new Function(src + "; return {widgetRows, slotW, slotH, pack, box, cellAt, pin, clearCells, move, fit, widgetCols};")();
|
||||
const L = new Function(src + "; return {widgetRows, slotW, slotH, pack, box, cellAt, pin, clearCells, move, fit, widgetCols, legacy};")();
|
||||
|
||||
const app = (x = -1, y = -1) => ({ kind: "app", str: "a", x, y });
|
||||
const widget = (w, rows, x = -1, y = -1) => ({ kind: "widget", str: "W", wid: 1, w, rows, x, y });
|
||||
@@ -61,3 +61,16 @@ assert.equal(L.widgetCols(400, 70, 5), 0);
|
||||
assert.deepEqual(L.box({ col: 1, row: 2, w: 2, h: 1 }, 70), { x: 78, y: 144, w: 148, h: 64 });
|
||||
assert.deepEqual(L.cellAt(80, 150, 70), [1, 2]);
|
||||
console.log("layout ok");
|
||||
|
||||
// The WebView build's saved home converts: apps by component, widgets with px heights to rows.
|
||||
assert.deepEqual(L.legacy({
|
||||
"ultra.home.n": "3",
|
||||
"ultra.home.kind.0": "0", "ultra.home.str.0": "com.a/.Main", "ultra.home.cx.0": "-1", "ultra.home.cy.0": "-1",
|
||||
"ultra.home.kind.1": "1", "ultra.home.str.1": "Clock", "ultra.home.wid.1": "12", "ultra.home.h.1": "136",
|
||||
"ultra.home.w.1": "3", "ultra.home.cx.1": "1", "ultra.home.cy.1": "2",
|
||||
"ultra.home.kind.2": "0",
|
||||
}), [
|
||||
{ kind: "app", str: "com.a/.Main", x: -1, y: -1 },
|
||||
{ kind: "widget", str: "Clock", wid: 12, w: 3, rows: 2, x: 1, y: 2 },
|
||||
]);
|
||||
assert.deepEqual(L.legacy({}), []);
|
||||
Reference in new issue
Block a user