Add real-time layout sync and refresh default layout
This commit is contained in:
1 parent
274b8538b7
commit
f79e30897b
20 files changed
+288
-76
No files matched your search
+5
-4
@@ -25,7 +25,7 @@ java -jar .\backend\target\vibe-launcher-backend.jar
|
|||||||
|
|
||||||
开发时可运行 `pnpm --dir frontend dev`,打开 Vite 给出的地址,管理 API 默认代理到本机 8080。使用其他本地端口时,在启动 Vite 前设置 `LAUNCHER_API_TARGET`。需要直接双击打开离线编辑器时可使用 `frontend/canvas.html`。
|
开发时可运行 `pnpm --dir frontend dev`,打开 Vite 给出的地址,管理 API 默认代理到本机 8080。使用其他本地端口时,在启动 Vite 前设置 `LAUNCHER_API_TARGET`。需要直接双击打开离线编辑器时可使用 `frontend/canvas.html`。
|
||||||
|
|
||||||
版本历史支持「载入编辑」和「回滚」。回滚会以选定历史布局生成新版本,版本号继续递增。两个管理员基于同一版本同时提交时,先提交者生效。另一方看到冲突提示后,可先导出本地编辑,再读取新版进行合并。
|
「恢复默认」会把内置默认布局发布为新版本,并保留发布历史。版本历史支持「载入编辑」和「回滚」。回滚会以选定历史布局生成新版本,版本号继续递增。两个管理员基于同一版本同时提交时,先提交者生效。另一方看到冲突提示后,可先导出本地编辑,再读取新版进行合并。
|
||||||
|
|
||||||
默认数据库位于 `backend/data/layouts.sqlite`。可用 `LAYOUT_DB` 指定持久化路径,用 `HOST`、`PORT` 配置监听地址。正式环境在 HTTPS 反向代理后运行服务,并复用固定令牌。发布历史以 SQLite 数据库为准。已有 Node.js 版数据库时,先停止旧服务,再让 Spring Boot 的 `LAYOUT_DB` 指向原数据库的绝对路径,或在项目根目录沿用默认路径。保留原有令牌即可让已配置的 Android 端继续访问。部署时可单独复制 JAR,并通过 `LAYOUT_DB` 指向服务器上的持久化目录。
|
默认数据库位于 `backend/data/layouts.sqlite`。可用 `LAYOUT_DB` 指定持久化路径,用 `HOST`、`PORT` 配置监听地址。正式环境在 HTTPS 反向代理后运行服务,并复用固定令牌。发布历史以 SQLite 数据库为准。已有 Node.js 版数据库时,先停止旧服务,再让 Spring Boot 的 `LAYOUT_DB` 指向原数据库的绝对路径,或在项目根目录沿用默认路径。保留原有令牌即可让已配置的 Android 端继续访问。部署时可单独复制 JAR,并通过 `LAYOUT_DB` 指向服务器上的持久化目录。
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ $env:LAUNCHER_DEVICE_TOKEN = '<设备令牌>'
|
|||||||
|
|
||||||
同步设置页的「测试第三方小组件」是 Debug 排障入口:枚举设备上已安装的 AppWidget Provider,点「绑定」拉起系统授权弹窗,授权成功后在页面内预览真实组件并回显绑定状态。普通 Launcher 页面仍按布局里的 `widget` 元素渲染,桌面回到前台或收到新布局时,会自动尝试绑定已获系统许可且无需强制配置的组件。仍需授权或配置时,点击组件占位块继续。同一 provider 可以绑定到多个实例,已绑定实例会随应用重启恢复。
|
同步设置页的「测试第三方小组件」是 Debug 排障入口:枚举设备上已安装的 AppWidget Provider,点「绑定」拉起系统授权弹窗,授权成功后在页面内预览真实组件并回显绑定状态。普通 Launcher 页面仍按布局里的 `widget` 元素渲染,桌面回到前台或收到新布局时,会自动尝试绑定已获系统许可且无需强制配置的组件。仍需授权或配置时,点击组件占位块继续。同一 provider 可以绑定到多个实例,已绑定实例会随应用重启恢复。
|
||||||
|
|
||||||
用系统应用列表打开「布局同步」。填写服务根地址和设备令牌,点「保存并同步」,然后回到桌面。测试时可以调整任意一个现有图标的位置,发布后确认手机的位置和版本号都更新。桌面处于前台时每分钟查询一次,回到桌面时立即查询。清空两项连接设置并保存可以关闭同步,已应用的布局仍可离线使用。
|
用系统应用列表打开「布局同步」。填写服务根地址和设备令牌,点「保存并同步」,然后回到桌面。测试时可以调整任意一个现有图标的位置,发布后确认手机的位置和版本号都更新。桌面处于前台时通过 SSE 接收布局版本变化;收到事件、连接恢复或回到桌面时会检查更新。每分钟查询一次作为兜底。清空两项连接设置并保存可以关闭同步,已应用的布局仍可离线使用。
|
||||||
|
|
||||||
| 环境 | Android 中填写的服务根地址 |
|
| 环境 | Android 中填写的服务根地址 |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
@@ -74,6 +74,7 @@ $env:LAUNCHER_DEVICE_TOKEN = '<设备令牌>'
|
|||||||
| --- | --- |
|
| --- | --- |
|
||||||
| `GET /healthz` | 服务健康状态 |
|
| `GET /healthz` | 服务健康状态 |
|
||||||
| `GET /api/layout` | 下发 v3 布局。设备可传 `If-None-Match`,版本内容相同时返回 304 |
|
| `GET /api/layout` | 下发 v3 布局。设备可传 `If-None-Match`,版本内容相同时返回 304 |
|
||||||
|
| `GET /api/layout/events` | 设备 SSE 事件流。收到 `layout-changed` 后使用原有布局下发接口同步 |
|
||||||
| `GET /api/admin/layout` | 获取线上布局及其 `ETag` |
|
| `GET /api/admin/layout` | 获取线上布局及其 `ETag` |
|
||||||
| `GET /api/admin/widget-providers` | 读取系统与第三方 AppWidget Provider 目录 |
|
| `GET /api/admin/widget-providers` | 读取系统与第三方 AppWidget Provider 目录 |
|
||||||
| `PUT /api/admin/widget-providers` | 幂等导入目录。每项可用组件字符串,或包含 `component`、`label`、`appLabel` 和可选 `category` 的对象(`system` 或 `third-party`) |
|
| `PUT /api/admin/widget-providers` | 幂等导入目录。每项可用组件字符串,或包含 `component`、`label`、`appLabel` 和可选 `category` 的对象(`system` 或 `third-party`) |
|
||||||
@@ -88,7 +89,7 @@ $env:LAUNCHER_DEVICE_TOKEN = '<设备令牌>'
|
|||||||
|
|
||||||
服务端回归测试可从项目根目录运行 `mvn -f .\backend\pom.xml test`,覆盖鉴权、校验、条件下载、发布冲突、回滚、历史分页、错误响应和数据库重开后的持久化。
|
服务端回归测试可从项目根目录运行 `mvn -f .\backend\pom.xml test`,覆盖鉴权、校验、条件下载、发布冲突、回滚、历史分页、错误响应和数据库重开后的持久化。
|
||||||
|
|
||||||
`LayoutApiTest` 检查 Spring Boot 返回 Vue 页面及其 JS、CSS 资源。`UniformityTest` 核验 Android 内置默认布局和服务端协议限制,包括基准尺寸 1080×1920、顶部安全区 132、底部安全区 72、最多 20 页及每页最多 80 个元素。
|
`LayoutApiTest` 检查 Spring Boot 返回 Vue 页面及其 JS、CSS 资源。`UniformityTest` 核验 Android 内置默认布局和服务端协议限制,包括基准尺寸 1080×1920、顶部和底部安全区各 72、最多 20 页及每页最多 80 个元素。
|
||||||
|
|
||||||
## 生产部署
|
## 生产部署
|
||||||
|
|
||||||
@@ -98,7 +99,7 @@ $env:LAUNCHER_DEVICE_TOKEN = '<设备令牌>'
|
|||||||
| --- | --- |
|
| --- | --- |
|
||||||
| 服务器 | `175.178.213.60`,Debian 12,k3s 单节点,SSH 密钥登录 |
|
| 服务器 | `175.178.213.60`,Debian 12,k3s 单节点,SSH 密钥登录 |
|
||||||
| 命名空间 / 工作负载 | `vibe-launcher` / `deploy/vibe-launcher-backend`,单副本 `Recreate` |
|
| 命名空间 / 工作负载 | `vibe-launcher` / `deploy/vibe-launcher-backend`,单副本 `Recreate` |
|
||||||
| 镜像 | `docker.io/library/vibe-launcher-backend:1.1.0-deploy8`,`imagePullPolicy: IfNotPresent` |
|
| 镜像 | `docker.io/library/vibe-launcher-backend:1.1.0-deploy11`,`imagePullPolicy: IfNotPresent` |
|
||||||
| 端口 | 容器 8080,Service NodePort **30830**(30800 是 youlai-boot,30810 是 tts-server,30820 是 compose-remote-server) |
|
| 端口 | 容器 8080,Service NodePort **30830**(30800 是 youlai-boot,30810 是 tts-server,30820 是 compose-remote-server) |
|
||||||
| 公网入口 | `https://api.ttstd.com/launcher/`(灰云直连源站,不走 Cloudflare);nginx 剥掉 `/launcher/` 前缀后反代 `127.0.0.1:30830` |
|
| 公网入口 | `https://api.ttstd.com/launcher/`(灰云直连源站,不走 Cloudflare);nginx 剥掉 `/launcher/` 前缀后反代 `127.0.0.1:30830` |
|
||||||
| 数据 | 宿主机 `/opt/vibe-launcher/data/layouts.sqlite` 挂到容器 `/data`,Pod 重建不丢版本历史 |
|
| 数据 | 宿主机 `/opt/vibe-launcher/data/layouts.sqlite` 挂到容器 `/data`,Pod 重建不丢版本历史 |
|
||||||
|
|||||||
@@ -82,6 +82,7 @@ dependencies {
|
|||||||
|
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
|
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1")
|
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1")
|
||||||
|
implementation("com.squareup.okhttp3:okhttp:4.12.0")
|
||||||
|
|
||||||
debugImplementation("androidx.compose.ui:ui-tooling")
|
debugImplementation("androidx.compose.ui:ui-tooling")
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
"canvas": {
|
"canvas": {
|
||||||
"designWidth": 1080,
|
"designWidth": 1080,
|
||||||
"designHeight": 1920,
|
"designHeight": 1920,
|
||||||
"safeTop": 132,
|
"safeTop": 72,
|
||||||
"safeBottom": 72
|
"safeBottom": 72
|
||||||
},
|
},
|
||||||
"fit": {
|
"fit": {
|
||||||
@@ -27,7 +27,7 @@
|
|||||||
"h": 320,
|
"h": 320,
|
||||||
"z": 1,
|
"z": 1,
|
||||||
"payload": {
|
"payload": {
|
||||||
"provider": "com.android.deskclock/.ClockWidget",
|
"provider": "com.android.deskclock/com.android.alarmclock.DigitalAppWidgetProvider",
|
||||||
"label": "时钟"
|
"label": "时钟"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -40,7 +40,7 @@
|
|||||||
"h": 168,
|
"h": 168,
|
||||||
"z": 2,
|
"z": 2,
|
||||||
"payload": {
|
"payload": {
|
||||||
"package": "com.demo.app0",
|
"package": "com.tencent.mm",
|
||||||
"label": "微信"
|
"label": "微信"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -53,7 +53,7 @@
|
|||||||
"h": 168,
|
"h": 168,
|
||||||
"z": 2,
|
"z": 2,
|
||||||
"payload": {
|
"payload": {
|
||||||
"package": "com.demo.app1",
|
"package": "com.android.camera2",
|
||||||
"label": "相机"
|
"label": "相机"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -66,7 +66,7 @@
|
|||||||
"h": 168,
|
"h": 168,
|
||||||
"z": 2,
|
"z": 2,
|
||||||
"payload": {
|
"payload": {
|
||||||
"package": "com.demo.app2",
|
"package": "com.android.gallery3d",
|
||||||
"label": "相册"
|
"label": "相册"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -79,7 +79,7 @@
|
|||||||
"h": 168,
|
"h": 168,
|
||||||
"z": 2,
|
"z": 2,
|
||||||
"payload": {
|
"payload": {
|
||||||
"package": "com.demo.app3",
|
"package": "com.android.settings",
|
||||||
"label": "设置"
|
"label": "设置"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -92,7 +92,7 @@
|
|||||||
"h": 168,
|
"h": 168,
|
||||||
"z": 2,
|
"z": 2,
|
||||||
"payload": {
|
"payload": {
|
||||||
"package": "com.demo.app4",
|
"package": "com.android.chrome",
|
||||||
"label": "浏览器"
|
"label": "浏览器"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -105,8 +105,8 @@
|
|||||||
"h": 168,
|
"h": 168,
|
||||||
"z": 2,
|
"z": 2,
|
||||||
"payload": {
|
"payload": {
|
||||||
"package": "com.demo.app5",
|
"package": "com.tencent.qqmusic",
|
||||||
"label": "音乐"
|
"label": "QQ音乐"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -118,8 +118,8 @@
|
|||||||
"h": 168,
|
"h": 168,
|
||||||
"z": 2,
|
"z": 2,
|
||||||
"payload": {
|
"payload": {
|
||||||
"package": "com.demo.app6",
|
"package": "com.android.dialer",
|
||||||
"label": "地图"
|
"label": "电话"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -131,7 +131,7 @@
|
|||||||
"h": 168,
|
"h": 168,
|
||||||
"z": 2,
|
"z": 2,
|
||||||
"payload": {
|
"payload": {
|
||||||
"package": "com.demo.app7",
|
"package": "com.android.calendar",
|
||||||
"label": "日历"
|
"label": "日历"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -144,8 +144,8 @@
|
|||||||
"h": 300,
|
"h": 300,
|
||||||
"z": 1,
|
"z": 1,
|
||||||
"payload": {
|
"payload": {
|
||||||
"provider": "com.android.weather/.WeatherWidget",
|
"provider": "com.android.quicksearchbox/com.android.quicksearchbox.SearchWidgetProvider",
|
||||||
"label": "天气"
|
"label": "搜索"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -157,60 +157,60 @@
|
|||||||
"h": 300,
|
"h": 300,
|
||||||
"z": 1,
|
"z": 1,
|
||||||
"payload": {
|
"payload": {
|
||||||
"provider": "com.spotify/.MusicWidget",
|
"provider": "com.tencent.qqmusic/com.tencent.qqmusiccommon.WidgetProviderSmall",
|
||||||
"label": "音乐"
|
"label": "QQ音乐"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "s0",
|
"id": "s0",
|
||||||
"type": "shortcut",
|
"type": "app_icon",
|
||||||
"x": 150,
|
"x": 150,
|
||||||
"y": 1650,
|
"y": 1650,
|
||||||
"w": 132,
|
"w": 132,
|
||||||
"h": 168,
|
"h": 168,
|
||||||
"z": 2,
|
"z": 2,
|
||||||
"payload": {
|
"payload": {
|
||||||
"label": "扫一扫",
|
"package": "com.android.contacts",
|
||||||
"intent": "demo://0"
|
"label": "联系人"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "s1",
|
"id": "s1",
|
||||||
"type": "shortcut",
|
"type": "app_icon",
|
||||||
"x": 366,
|
"x": 366,
|
||||||
"y": 1650,
|
"y": 1650,
|
||||||
"w": 132,
|
"w": 132,
|
||||||
"h": 168,
|
"h": 168,
|
||||||
"z": 2,
|
"z": 2,
|
||||||
"payload": {
|
"payload": {
|
||||||
"label": "支付",
|
"package": "com.android.messaging",
|
||||||
"intent": "demo://1"
|
"label": "短信"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "s2",
|
"id": "s2",
|
||||||
"type": "shortcut",
|
"type": "app_icon",
|
||||||
"x": 582,
|
"x": 582,
|
||||||
"y": 1650,
|
"y": 1650,
|
||||||
"w": 132,
|
"w": 132,
|
||||||
"h": 168,
|
"h": 168,
|
||||||
"z": 2,
|
"z": 2,
|
||||||
"payload": {
|
"payload": {
|
||||||
"label": "钱包",
|
"package": "com.android.documentsui",
|
||||||
"intent": "demo://2"
|
"label": "文件"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "s3",
|
"id": "s3",
|
||||||
"type": "shortcut",
|
"type": "app_icon",
|
||||||
"x": 798,
|
"x": 798,
|
||||||
"y": 1650,
|
"y": 1650,
|
||||||
"w": 132,
|
"w": 132,
|
||||||
"h": 168,
|
"h": 168,
|
||||||
"z": 2,
|
"z": 2,
|
||||||
"payload": {
|
"payload": {
|
||||||
"label": "更多",
|
"package": "com.tencent.mobileqq",
|
||||||
"intent": "demo://3"
|
"label": "QQ"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ class LauncherViewModel(app: Application) : AndroidViewModel(app) {
|
|||||||
companion object {
|
companion object {
|
||||||
/** 首帧占位:一页、零元素、壁纸透出。绝不做 IO */
|
/** 首帧占位:一页、零元素、壁纸透出。绝不做 IO */
|
||||||
fun placeholderLayout() = LauncherLayout(
|
fun placeholderLayout() = LauncherLayout(
|
||||||
canvas = CanvasDef(1080, 1920, 132, 72),
|
canvas = CanvasDef(),
|
||||||
fit = FitDef("contain", 0.5f),
|
fit = FitDef("contain", 0.5f),
|
||||||
pages = listOf(PageDef(0, BackgroundDef("wallpaper"), emptyList())),
|
pages = listOf(PageDef(0, BackgroundDef("wallpaper"), emptyList())),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -155,6 +155,35 @@ class MainActivity : ComponentActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lifecycleScope.launch {
|
||||||
|
repeatOnLifecycle(Lifecycle.State.RESUMED) {
|
||||||
|
val remote = (application as LauncherApp).graph.remote
|
||||||
|
var reconnectDelay = 1_000L
|
||||||
|
while (true) {
|
||||||
|
val connection = remote.connection()
|
||||||
|
if (connection.server.isBlank()) break
|
||||||
|
try {
|
||||||
|
remote.layoutEvents(connection).collect {
|
||||||
|
reconnectDelay = 1_000L
|
||||||
|
try {
|
||||||
|
remote.sync()
|
||||||
|
} catch (error: CancellationException) {
|
||||||
|
throw error
|
||||||
|
} catch (error: Exception) {
|
||||||
|
Log.w(TAG, "Layout event sync failed: ${error.javaClass.simpleName}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error: CancellationException) {
|
||||||
|
throw error
|
||||||
|
} catch (error: Exception) {
|
||||||
|
Log.w(TAG, "Layout event stream unavailable: ${error.javaClass.simpleName}")
|
||||||
|
}
|
||||||
|
delay(reconnectDelay)
|
||||||
|
reconnectDelay = (reconnectDelay * 2).coerceAtMost(60_000L)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 首帧埋点:onPreDraw 比 onResume 准,它是"即将真正绘制"的时刻
|
// 首帧埋点:onPreDraw 比 onResume 准,它是"即将真正绘制"的时刻
|
||||||
window.decorView.viewTreeObserver.addOnPreDrawListener(
|
window.decorView.viewTreeObserver.addOnPreDrawListener(
|
||||||
object : ViewTreeObserver.OnPreDrawListener {
|
object : ViewTreeObserver.OnPreDrawListener {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ data class DesignCanvas(
|
|||||||
val designWidth: Int = 1080,
|
val designWidth: Int = 1080,
|
||||||
val designHeight: Int = 1920,
|
val designHeight: Int = 1920,
|
||||||
/** 内容顶部安全区(设计稿坐标系内),防止内容压到状态栏 */
|
/** 内容顶部安全区(设计稿坐标系内),防止内容压到状态栏 */
|
||||||
val safeTop: Int = 132,
|
val safeTop: Int = 72,
|
||||||
/** 内容底部安全区,避让手势条 */
|
/** 内容底部安全区,避让手势条 */
|
||||||
val safeBottom: Int = 72,
|
val safeBottom: Int = 72,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ data class LauncherLayout(
|
|||||||
data class CanvasDef(
|
data class CanvasDef(
|
||||||
val designWidth: Int = 1080,
|
val designWidth: Int = 1080,
|
||||||
val designHeight: Int = 1920,
|
val designHeight: Int = 1920,
|
||||||
val safeTop: Int = 132,
|
val safeTop: Int = 72,
|
||||||
val safeBottom: Int = 72,
|
val safeBottom: Int = 72,
|
||||||
) {
|
) {
|
||||||
fun toDesignCanvas() = DesignCanvas(designWidth, designHeight, safeTop, safeBottom)
|
fun toDesignCanvas() = DesignCanvas(designWidth, designHeight, safeTop, safeBottom)
|
||||||
|
|||||||
@@ -2,8 +2,13 @@ package com.ttstd.compose.launcher.core
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.os.SystemClock
|
import android.os.SystemClock
|
||||||
|
import android.util.Log
|
||||||
import com.ttstd.compose.launcher.BuildConfig
|
import com.ttstd.compose.launcher.BuildConfig
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.channels.Channel
|
||||||
|
import kotlinx.coroutines.channels.awaitClose
|
||||||
|
import kotlinx.coroutines.flow.buffer
|
||||||
|
import kotlinx.coroutines.flow.callbackFlow
|
||||||
import kotlinx.coroutines.currentCoroutineContext
|
import kotlinx.coroutines.currentCoroutineContext
|
||||||
import kotlinx.coroutines.ensureActive
|
import kotlinx.coroutines.ensureActive
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
@@ -13,6 +18,12 @@ import java.io.ByteArrayOutputStream
|
|||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.net.HttpURLConnection
|
import java.net.HttpURLConnection
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import okhttp3.Call
|
||||||
|
import okhttp3.Callback
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.Response
|
||||||
|
|
||||||
class RemoteLayoutSync(context: Context, private val repo: LayoutRepository) {
|
class RemoteLayoutSync(context: Context, private val repo: LayoutRepository) {
|
||||||
data class Connection(val server: String, val token: String)
|
data class Connection(val server: String, val token: String)
|
||||||
@@ -21,6 +32,58 @@ class RemoteLayoutSync(context: Context, private val repo: LayoutRepository) {
|
|||||||
private val mutex = Mutex()
|
private val mutex = Mutex()
|
||||||
private var etag: String? = null
|
private var etag: String? = null
|
||||||
|
|
||||||
|
private val eventClient by lazy {
|
||||||
|
OkHttpClient.Builder()
|
||||||
|
.connectTimeout(5, TimeUnit.SECONDS)
|
||||||
|
.readTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.followRedirects(false)
|
||||||
|
.followSslRedirects(false)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun layoutEvents(config: Connection) = callbackFlow {
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(endpoint(config.server).toString() + "/events")
|
||||||
|
.header("Authorization", "Bearer " + config.token)
|
||||||
|
.header("Accept", "text/event-stream")
|
||||||
|
.build()
|
||||||
|
val call = eventClient.newCall(request)
|
||||||
|
call.enqueue(object : Callback {
|
||||||
|
override fun onFailure(call: Call, e: IOException) {
|
||||||
|
close(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResponse(call: Call, response: Response) {
|
||||||
|
try {
|
||||||
|
response.use {
|
||||||
|
val body = it.body ?: throw IOException("布局事件响应为空")
|
||||||
|
val type = body.contentType()
|
||||||
|
if (!it.isSuccessful || type?.type != "text" || type.subtype != "event-stream") {
|
||||||
|
throw IOException("布局事件服务返回 HTTP ${it.code}")
|
||||||
|
}
|
||||||
|
if (BuildConfig.DEBUG) Log.d("LayoutEvents", "SSE connected to ${request.url.host}")
|
||||||
|
trySend(Unit) // Check for releases missed before the stream opened.
|
||||||
|
val source = body.source()
|
||||||
|
var event = ""
|
||||||
|
while (!source.exhausted()) {
|
||||||
|
val line = source.readUtf8LineStrict(1024)
|
||||||
|
if (line.isEmpty()) {
|
||||||
|
if (event == "layout-changed") trySend(Unit)
|
||||||
|
event = ""
|
||||||
|
} else if (line.startsWith("event:")) {
|
||||||
|
event = line.substringAfter(':').trim()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close()
|
||||||
|
} catch (error: IOException) {
|
||||||
|
close(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
awaitClose { call.cancel() }
|
||||||
|
}.buffer(Channel.CONFLATED)
|
||||||
|
|
||||||
fun connection(): Connection {
|
fun connection(): Connection {
|
||||||
val savedServer = prefs.getString("server", null)
|
val savedServer = prefs.getString("server", null)
|
||||||
val savedToken = prefs.getString("token", null)
|
val savedToken = prefs.getString("token", null)
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ spec:
|
|||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: server
|
- name: server
|
||||||
image: docker.io/library/vibe-launcher-backend:1.1.0-deploy8
|
image: docker.io/library/vibe-launcher-backend:1.1.0-deploy11
|
||||||
imagePullPolicy: IfNotPresent
|
imagePullPolicy: IfNotPresent
|
||||||
ports:
|
ports:
|
||||||
- name: http
|
- name: http
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ package com.ttstd.launcher;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableScheduling
|
||||||
public class LauncherApplication {
|
public class LauncherApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(LauncherApplication.class, args);
|
SpringApplication.run(LauncherApplication.class, args);
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||||||
import org.springframework.web.bind.annotation.PutMapping;
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||||
import tools.jackson.core.JacksonException;
|
import tools.jackson.core.JacksonException;
|
||||||
import tools.jackson.databind.DeserializationFeature;
|
import tools.jackson.databind.DeserializationFeature;
|
||||||
import tools.jackson.databind.JsonNode;
|
import tools.jackson.databind.JsonNode;
|
||||||
@@ -39,13 +40,15 @@ class LayoutController {
|
|||||||
+ "img-src 'self' data:; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'";
|
+ "img-src 'self' data:; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'";
|
||||||
|
|
||||||
private final LayoutStore store;
|
private final LayoutStore store;
|
||||||
|
private final LayoutEvents events;
|
||||||
private final RequestPolicy policy;
|
private final RequestPolicy policy;
|
||||||
private final LayoutValidator validator;
|
private final LayoutValidator validator;
|
||||||
private final ObjectMapper mapper;
|
private final ObjectMapper mapper;
|
||||||
private final Resource editor = new ClassPathResource("launcher/web/index.html");
|
private final Resource editor = new ClassPathResource("launcher/web/index.html");
|
||||||
|
|
||||||
LayoutController(LayoutStore store, RequestPolicy policy, LayoutValidator validator, ObjectMapper mapper) {
|
LayoutController(LayoutStore store, RequestPolicy policy, LayoutValidator validator, ObjectMapper mapper, LayoutEvents events) {
|
||||||
this.store = store;
|
this.store = store;
|
||||||
|
this.events = events;
|
||||||
this.policy = policy;
|
this.policy = policy;
|
||||||
this.validator = validator;
|
this.validator = validator;
|
||||||
this.mapper = mapper;
|
this.mapper = mapper;
|
||||||
@@ -73,6 +76,13 @@ class LayoutController {
|
|||||||
return Map.of("status", "ok");
|
return Map.of("status", "ok");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping(value = "/api/layout/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||||
|
SseEmitter layoutEvents(HttpServletRequest request, HttpServletResponse response) {
|
||||||
|
policy.requireDevice(request, response);
|
||||||
|
response.setHeader("X-Accel-Buffering", "no");
|
||||||
|
return events.subscribe();
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/api/layout")
|
@GetMapping("/api/layout")
|
||||||
ResponseEntity<byte[]> deviceLayout(HttpServletRequest request, HttpServletResponse response) throws SQLException {
|
ResponseEntity<byte[]> deviceLayout(HttpServletRequest request, HttpServletResponse response) throws SQLException {
|
||||||
policy.requireDevice(request, response);
|
policy.requireDevice(request, response);
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package com.ttstd.launcher;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class LayoutEvents {
|
||||||
|
private final LayoutStore store;
|
||||||
|
private final Set<SseEmitter> subscribers = ConcurrentHashMap.newKeySet();
|
||||||
|
private int lastRevision;
|
||||||
|
private long lastHeartbeat;
|
||||||
|
|
||||||
|
LayoutEvents(LayoutStore store) {
|
||||||
|
this.store = store;
|
||||||
|
}
|
||||||
|
|
||||||
|
SseEmitter subscribe() {
|
||||||
|
// Periodic reconnects also refresh device authorization and check for missed releases.
|
||||||
|
var emitter = new SseEmitter(TimeUnit.MINUTES.toMillis(30));
|
||||||
|
emitter.onCompletion(() -> subscribers.remove(emitter));
|
||||||
|
emitter.onTimeout(() -> subscribers.remove(emitter));
|
||||||
|
emitter.onError(error -> subscribers.remove(emitter));
|
||||||
|
subscribers.add(emitter);
|
||||||
|
send(emitter, SseEmitter.event().comment("connected"));
|
||||||
|
return emitter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(fixedDelay = 1000)
|
||||||
|
void emitLatestRevision() throws SQLException {
|
||||||
|
if (subscribers.isEmpty()) return;
|
||||||
|
// Read the committed revision once across all subscribers, including after rollback.
|
||||||
|
int revision = store.latestRevision();
|
||||||
|
if (revision != lastRevision) {
|
||||||
|
lastRevision = revision;
|
||||||
|
for (var emitter : subscribers) {
|
||||||
|
send(emitter, SseEmitter.event().name("layout-changed").id(Integer.toString(revision))
|
||||||
|
.data(Integer.toString(revision)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
long now = System.nanoTime();
|
||||||
|
if (now - lastHeartbeat >= TimeUnit.SECONDS.toNanos(10) || lastHeartbeat == 0) {
|
||||||
|
lastHeartbeat = now;
|
||||||
|
for (var emitter : subscribers) send(emitter, SseEmitter.event().comment("heartbeat"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void send(SseEmitter emitter, SseEmitter.SseEventBuilder event) {
|
||||||
|
try {
|
||||||
|
emitter.send(event);
|
||||||
|
} catch (IOException | IllegalStateException error) {
|
||||||
|
subscribers.remove(emitter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -141,6 +141,13 @@ class LayoutStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int latestRevision() throws SQLException {
|
||||||
|
try (var connection = connect(); var statement = connection.createStatement();
|
||||||
|
var rows = statement.executeQuery("SELECT MAX(revision) FROM releases")) {
|
||||||
|
return rows.getInt(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Release latest() throws SQLException {
|
Release latest() throws SQLException {
|
||||||
try (var connection = connect(); var statement = connection.createStatement();
|
try (var connection = connect(); var statement = connection.createStatement();
|
||||||
var rows = statement.executeQuery(LATEST)) {
|
var rows = statement.executeQuery(LATEST)) {
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ class LayoutValidator {
|
|||||||
}
|
}
|
||||||
int width = canvas.path("designWidth").asInt(1080);
|
int width = canvas.path("designWidth").asInt(1080);
|
||||||
int height = canvas.path("designHeight").asInt(1920);
|
int height = canvas.path("designHeight").asInt(1920);
|
||||||
int safeTop = canvas.path("safeTop").asInt(132);
|
int safeTop = canvas.path("safeTop").asInt(72);
|
||||||
int safeBottom = canvas.path("safeBottom").asInt(72);
|
int safeBottom = canvas.path("safeBottom").asInt(72);
|
||||||
if (width < 320 || height < 480) error(issues, "canvas", "画布最小尺寸为 320 × 480");
|
if (width < 320 || height < 480) error(issues, "canvas", "画布最小尺寸为 320 × 480");
|
||||||
if (width > MAX_CANVAS || height > MAX_CANVAS) error(issues, "canvas", "画布尺寸不能超过 " + MAX_CANVAS);
|
if (width > MAX_CANVAS || height > MAX_CANVAS) error(issues, "canvas", "画布尺寸不能超过 " + MAX_CANVAS);
|
||||||
|
|||||||
@@ -34,6 +34,17 @@ class LayoutApiTest {
|
|||||||
|
|
||||||
@TempDir Path dir;
|
@TempDir Path dir;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void layoutEventStreamRequiresDeviceToken() throws Exception {
|
||||||
|
MockMvc mvc = mvc(dir.resolve("events.sqlite"));
|
||||||
|
mvc.perform(get("/api/layout/events")).andExpect(status().isUnauthorized());
|
||||||
|
mvc.perform(get("/api/layout/events").header("Authorization", ADMIN)).andExpect(status().isUnauthorized());
|
||||||
|
mvc.perform(get("/api/layout/events").header("Authorization", DEVICE))
|
||||||
|
.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.request().asyncStarted())
|
||||||
|
.andExpect(header().string("X-Accel-Buffering", "no"))
|
||||||
|
.andExpect(content().string(org.hamcrest.Matchers.containsString(":connected")));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void widgetCatalogRequiresAdminAndPersistsUniqueComponents() throws Exception {
|
void widgetCatalogRequiresAdminAndPersistsUniqueComponents() throws Exception {
|
||||||
String path = "/api/admin/widget-providers";
|
String path = "/api/admin/widget-providers";
|
||||||
@@ -269,7 +280,7 @@ class LayoutApiTest {
|
|||||||
var validator = new LayoutValidator();
|
var validator = new LayoutValidator();
|
||||||
var store = new LayoutStore(database.toString(), mapper, validator);
|
var store = new LayoutStore(database.toString(), mapper, validator);
|
||||||
var policy = new RequestPolicy(ADMIN.substring(7), DEVICE.substring(7));
|
var policy = new RequestPolicy(ADMIN.substring(7), DEVICE.substring(7));
|
||||||
return MockMvcBuilders.standaloneSetup(new LayoutController(store, policy, validator, mapper))
|
return MockMvcBuilders.standaloneSetup(new LayoutController(store, policy, validator, mapper, new LayoutEvents(store)))
|
||||||
.setControllerAdvice(new ApiExceptionHandler())
|
.setControllerAdvice(new ApiExceptionHandler())
|
||||||
.addFilters(policy).build();
|
.addFilters(policy).build();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ class UniformityTest {
|
|||||||
assertEquals(3, layout.path("schemaVersion").intValue());
|
assertEquals(3, layout.path("schemaVersion").intValue());
|
||||||
assertEquals(1080, layout.path("canvas").path("designWidth").intValue());
|
assertEquals(1080, layout.path("canvas").path("designWidth").intValue());
|
||||||
assertEquals(1920, layout.path("canvas").path("designHeight").intValue());
|
assertEquals(1920, layout.path("canvas").path("designHeight").intValue());
|
||||||
assertEquals(132, layout.path("canvas").path("safeTop").intValue());
|
assertEquals(72, layout.path("canvas").path("safeTop").intValue());
|
||||||
assertEquals(72, layout.path("canvas").path("safeBottom").intValue());
|
assertEquals(72, layout.path("canvas").path("safeBottom").intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -14,7 +14,7 @@
|
|||||||
"canvas": {
|
"canvas": {
|
||||||
"designWidth": 1080,
|
"designWidth": 1080,
|
||||||
"designHeight": 1920,
|
"designHeight": 1920,
|
||||||
"safeTop": 132,
|
"safeTop": 72,
|
||||||
"safeBottom": 72
|
"safeBottom": 72
|
||||||
},
|
},
|
||||||
"fit": {
|
"fit": {
|
||||||
@@ -31,7 +31,7 @@
|
|||||||
"type": "widget",
|
"type": "widget",
|
||||||
"x": 108, "y": 196, "w": 864, "h": 320,
|
"x": 108, "y": 196, "w": 864, "h": 320,
|
||||||
"z": 1,
|
"z": 1,
|
||||||
"payload": { "provider": "com.android.deskclock/.ClockWidget", "label": "时钟" }
|
"payload": { "provider": "com.android.deskclock/com.android.alarmclock.DigitalAppWidgetProvider", "label": "时钟" }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "i0",
|
"id": "i0",
|
||||||
|
|||||||
+16
-16
@@ -222,7 +222,7 @@ pre.json{font:11px/1.55 ui-monospace,Menlo,monospace;color:#9fb4d8;
|
|||||||
<option value="1080x2400">1080 × 2400</option>
|
<option value="1080x2400">1080 × 2400</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="row">safeTop <input type="number" id="safeTop" value="132" step="4"></div>
|
<div class="row">safeTop <input type="number" id="safeTop" value="72" step="4"></div>
|
||||||
<div class="row">safeBottom <input type="number" id="safeBottom" value="72" step="4"></div>
|
<div class="row">safeBottom <input type="number" id="safeBottom" value="72" step="4"></div>
|
||||||
<div class="row">吸附 12px <input type="checkbox" id="snap" checked></div>
|
<div class="row">吸附 12px <input type="checkbox" id="snap" checked></div>
|
||||||
<div class="row">参考线 <input type="checkbox" id="showGuides" checked></div>
|
<div class="row">参考线 <input type="checkbox" id="showGuides" checked></div>
|
||||||
@@ -351,10 +351,10 @@ oy = (H − Dh·s) · anchor</div>
|
|||||||
* ===========================================================================*/
|
* ===========================================================================*/
|
||||||
const LIB = [
|
const LIB = [
|
||||||
{ type:'app_icon', label:'图标', w:132, h:168, payload:{ package:'com.tencent.mm', label:'微信' } },
|
{ type:'app_icon', label:'图标', w:132, h:168, payload:{ package:'com.tencent.mm', label:'微信' } },
|
||||||
{ type:'app_icon', label:'相机', w:132, h:168, payload:{ package:'com.android.camera', label:'相机' } },
|
{ type:'app_icon', label:'相机', w:132, h:168, payload:{ package:'com.android.camera2', label:'相机' } },
|
||||||
{ type:'widget', label:'时钟组件', w:864, h:320, payload:{ provider:'com.android.deskclock/.ClockWidget', label:'时钟' } },
|
{ type:'widget', label:'时钟组件', w:864, h:320, payload:{ provider:'com.android.deskclock/com.android.alarmclock.DigitalAppWidgetProvider', label:'时钟' } },
|
||||||
{ type:'widget', label:'天气组件', w:420, h:300, payload:{ provider:'com.android.weather/.WeatherWidget', label:'天气' } },
|
{ type:'widget', label:'搜索组件', w:420, h:300, payload:{ provider:'com.android.quicksearchbox/com.android.quicksearchbox.SearchWidgetProvider', label:'搜索' } },
|
||||||
{ type:'widget', label:'音乐组件', w:420, h:300, payload:{ provider:'com.spotify/.MusicWidget', label:'音乐' } },
|
{ type:'widget', label:'音乐组件', w:420, h:300, payload:{ provider:'com.tencent.qqmusic/com.tencent.qqmusiccommon.WidgetProviderSmall', label:'音乐' } },
|
||||||
{ type:'shortcut', label:'快捷方式', w:132, h:168, payload:{ label:'扫一扫', intent:'weixin://scanqrcode' } },
|
{ type:'shortcut', label:'快捷方式', w:132, h:168, payload:{ label:'扫一扫', intent:'weixin://scanqrcode' } },
|
||||||
{ type:'dock_bg', label:'Dock 底板', w:936, h:200, payload:{ radius:56 } },
|
{ type:'dock_bg', label:'Dock 底板', w:936, h:200, payload:{ radius:56 } },
|
||||||
];
|
];
|
||||||
@@ -382,7 +382,7 @@ function newState() {
|
|||||||
return {
|
return {
|
||||||
schemaVersion: 3,
|
schemaVersion: 3,
|
||||||
revision: 1,
|
revision: 1,
|
||||||
canvas: { designWidth:1080, designHeight:1920, safeTop:132, safeBottom:72 },
|
canvas: { designWidth:1080, designHeight:1920, safeTop:72, safeBottom:72 },
|
||||||
fit: { mode:'contain', anchorY:0.5 },
|
fit: { mode:'contain', anchorY:0.5 },
|
||||||
pages: [{ index:0, background:{ type:'wallpaper' }, items: demoItems() }],
|
pages: [{ index:0, background:{ type:'wallpaper' }, items: demoItems() }],
|
||||||
};
|
};
|
||||||
@@ -393,22 +393,22 @@ function demoItems() {
|
|||||||
const colX = c => pad + c * colW + (colW - icon) / 2;
|
const colX = c => pad + c * colW + (colW - icon) / 2;
|
||||||
const out = [];
|
const out = [];
|
||||||
out.push({ id:'w_clock', type:'widget', x:pad, y:196, w:864, h:320, z:1,
|
out.push({ id:'w_clock', type:'widget', x:pad, y:196, w:864, h:320, z:1,
|
||||||
payload:{ provider:'com.android.deskclock/.ClockWidget', label:'时钟' } });
|
payload:{ provider:'com.android.deskclock/com.android.alarmclock.DigitalAppWidgetProvider', label:'时钟' } });
|
||||||
|
|
||||||
['微信','相机','相册','设置','浏览器','音乐','地图','日历'].forEach((t, i) => {
|
[['微信','com.tencent.mm'],['相机','com.android.camera2'],['相册','com.android.gallery3d'],['设置','com.android.settings'],['浏览器','com.android.chrome'],['QQ音乐','com.tencent.qqmusic'],['电话','com.android.dialer'],['日历','com.android.calendar']].forEach(([label, pkg], i) => {
|
||||||
const c = i % 4, r = Math.floor(i / 4);
|
const c = i % 4, r = Math.floor(i / 4);
|
||||||
out.push({ id:'i'+i, type:'app_icon', x:colX(c), y:604 + r*224, w:icon, h:ih, z:2,
|
out.push({ id:'i'+i, type:'app_icon', x:colX(c), y:604 + r*224, w:icon, h:ih, z:2,
|
||||||
payload:{ package:'com.demo.app'+i, label:t } });
|
payload:{ package:pkg, label } });
|
||||||
});
|
});
|
||||||
|
|
||||||
out.push({ id:'w_weather', type:'widget', x:pad, y:1076, w:420, h:300, z:1,
|
out.push({ id:'w_weather', type:'widget', x:pad, y:1076, w:420, h:300, z:1,
|
||||||
payload:{ provider:'com.android.weather/.WeatherWidget', label:'天气' } });
|
payload:{ provider:'com.android.quicksearchbox/com.android.quicksearchbox.SearchWidgetProvider', label:'搜索' } });
|
||||||
out.push({ id:'w_music', type:'widget', x:552, y:1076, w:420, h:300, z:1,
|
out.push({ id:'w_music', type:'widget', x:552, y:1076, w:420, h:300, z:1,
|
||||||
payload:{ provider:'com.spotify/.MusicWidget', label:'音乐' } });
|
payload:{ provider:'com.tencent.qqmusic/com.tencent.qqmusiccommon.WidgetProviderSmall', label:'音乐' } });
|
||||||
|
|
||||||
['扫一扫','支付','钱包','更多'].forEach((t, i) => {
|
[['联系人','com.android.contacts'],['短信','com.android.messaging'],['文件','com.android.documentsui'],['QQ','com.tencent.mobileqq']].forEach(([label, pkg], i) => {
|
||||||
out.push({ id:'s'+i, type:'shortcut', x:colX(i), y:1650, w:icon, h:ih, z:2,
|
out.push({ id:'s'+i, type:'app_icon', x:colX(i), y:1650, w:icon, h:ih, z:2,
|
||||||
payload:{ label:t, intent:'demo://'+i } });
|
payload:{ package:pkg, label } });
|
||||||
});
|
});
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
@@ -830,7 +830,7 @@ function validate() {
|
|||||||
|
|
||||||
if (c.safeTop < 0 || c.safeBottom < 0) push('err', '-', '安全区不能为负数');
|
if (c.safeTop < 0 || c.safeBottom < 0) push('err', '-', '安全区不能为负数');
|
||||||
if (!['contain', 'elastic', 'stretch'].includes(state.fit.mode.toLowerCase())) push('warn', '-', '未知映射模式');
|
if (!['contain', 'elastic', 'stretch'].includes(state.fit.mode.toLowerCase())) push('warn', '-', '未知映射模式');
|
||||||
if (c.safeTop < 118) push('warn', '-', `safeTop=${c.safeTop} 偏小,1440 屏状态栏约 176px`);
|
if (c.safeTop < 118) push('warn', '-', `safeTop=${c.safeTop} 偏小`);
|
||||||
if (c.safeBottom < 40) push('warn', '-', `safeBottom=${c.safeBottom} 偏小,可能被手势条压住`);
|
if (c.safeBottom < 40) push('warn', '-', `safeBottom=${c.safeBottom} 偏小,可能被手势条压住`);
|
||||||
|
|
||||||
const errors = issues.filter(i => i.lv === 'err').length;
|
const errors = issues.filter(i => i.lv === 'err').length;
|
||||||
@@ -1083,7 +1083,7 @@ $('download').onclick = $('exportBtn').onclick = () => {
|
|||||||
|
|
||||||
function importLayout(d) {
|
function importLayout(d) {
|
||||||
if (!d || (d.schemaVersion ?? 3) !== 3) throw new Error('仅支持布局协议 v3');
|
if (!d || (d.schemaVersion ?? 3) !== 3) throw new Error('仅支持布局协议 v3');
|
||||||
const canvas = { designWidth:1080, designHeight:1920, safeTop:132, safeBottom:72, ...d.canvas };
|
const canvas = { designWidth:1080, designHeight:1920, safeTop:72, safeBottom:72, ...d.canvas };
|
||||||
const fit = { mode:'contain', anchorY:0.5, ...d.fit };
|
const fit = { mode:'contain', anchorY:0.5, ...d.fit };
|
||||||
if (![canvas.designWidth, canvas.designHeight, canvas.safeTop, canvas.safeBottom].every(Number.isInteger) ||
|
if (![canvas.designWidth, canvas.designHeight, canvas.safeTop, canvas.safeBottom].every(Number.isInteger) ||
|
||||||
canvas.designWidth < 320 || canvas.designHeight < 480 ||
|
canvas.designWidth < 320 || canvas.designHeight < 480 ||
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, watch } from "vue";
|
import { ref, watch } from "vue";
|
||||||
import { ElButton, ElDialog, ElInput, ElMessageBox } from "element-plus";
|
import { ElButton, ElDialog, ElMessageBox } from "element-plus";
|
||||||
import { loggedIn } from "./auth";
|
import { loggedIn } from "./auth";
|
||||||
import { normalizeLayout, type Issue, type Layout } from "./layout";
|
import { builtInLayout, normalizeLayout, type Issue, type Layout } from "./layout";
|
||||||
|
|
||||||
interface HistoryEntry { revision: number; publishedAt: string; restoredFrom?: number }
|
interface HistoryEntry { revision: number; publishedAt: string; restoredFrom?: number }
|
||||||
interface History { revisions: HistoryEntry[]; nextBefore: number | null }
|
interface History { revisions: HistoryEntry[]; nextBefore: number | null }
|
||||||
@@ -118,21 +118,48 @@ async function confirmPublish(version?: number) {
|
|||||||
} catch {
|
} catch {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
return run(() => publish(
|
||||||
|
rollback ? "rollback" : "layout",
|
||||||
|
rollback ? "POST" : "PUT",
|
||||||
|
rollback ? { revision: version } : props.layout,
|
||||||
|
tag.value,
|
||||||
|
rollback,
|
||||||
|
"已发布",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
function restoreDefault() {
|
||||||
return run(async () => {
|
return run(async () => {
|
||||||
if (!tag.value) throw new Error("发布前请先读取线上版本");
|
if (!(await props.confirmReplace())) return "";
|
||||||
const { data, etag } = await request<Layout>(rollback ? "rollback" : "layout", {
|
const { data: latest, etag } = await request<Layout>("layout");
|
||||||
method: rollback ? "POST" : "PUT",
|
try {
|
||||||
headers: { "If-Match": tag.value, "Content-Type": "application/json" },
|
await ElMessageBox.confirm(
|
||||||
body: JSON.stringify(rollback ? { revision: version } : props.layout),
|
"当前线上为 v" + latest.revision + "。将内置默认布局发布为新版本,并应用到所有已连接设备?",
|
||||||
});
|
"恢复默认布局",
|
||||||
tag.value = etag;
|
{ type: "warning", confirmButtonText: "恢复并发布", cancelButtonText: "取消" },
|
||||||
revision.value = data.revision;
|
);
|
||||||
if (rollback) emit("load", normalizeLayout(data), true);
|
} catch {
|
||||||
else emit("published", data.revision);
|
return "";
|
||||||
return refreshHistory("已发布 v" + data.revision + ",设备会在下次同步时更新。");
|
}
|
||||||
|
return publish("layout", "PUT", builtInLayout(), etag, true, "已恢复默认布局,发布");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function publish(path: string, method: "POST" | "PUT", layout: Layout | { revision: number | undefined },
|
||||||
|
expectedTag: string | undefined, replaceCanvas: boolean, message: string) {
|
||||||
|
if (!expectedTag) throw new Error("未取得线上版本,请重试");
|
||||||
|
const { data, etag } = await request<Layout>(path, {
|
||||||
|
method,
|
||||||
|
headers: { "If-Match": expectedTag, "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(layout),
|
||||||
|
});
|
||||||
|
tag.value = etag;
|
||||||
|
revision.value = data.revision;
|
||||||
|
if (replaceCanvas) emit("load", normalizeLayout(data), true);
|
||||||
|
else emit("published", data.revision);
|
||||||
|
return refreshHistory(message + " v" + data.revision + ",设备会在下次同步时更新。");
|
||||||
|
}
|
||||||
|
|
||||||
function moreHistory() {
|
function moreHistory() {
|
||||||
return run(async () => {
|
return run(async () => {
|
||||||
await loadHistory(true);
|
await loadHistory(true);
|
||||||
@@ -148,6 +175,7 @@ function moreHistory() {
|
|||||||
<div class="release-actions">
|
<div class="release-actions">
|
||||||
<ElButton :disabled="busy" @click="loadOnline">读取线上布局</ElButton>
|
<ElButton :disabled="busy" @click="loadOnline">读取线上布局</ElButton>
|
||||||
<ElButton type="primary" :disabled="busy || !tag || hasErrors" @click="confirmPublish()">发布当前画布</ElButton>
|
<ElButton type="primary" :disabled="busy || !tag || hasErrors" @click="confirmPublish()">发布当前画布</ElButton>
|
||||||
|
<ElButton type="warning" plain :disabled="busy || !loggedIn" @click="restoreDefault">恢复默认</ElButton>
|
||||||
<ElButton :disabled="busy" @click="open = false">返回编辑</ElButton>
|
<ElButton :disabled="busy" @click="open = false">返回编辑</ElButton>
|
||||||
</div>
|
</div>
|
||||||
<p v-if="hasErrors" class="release-error">请先修正画布校验错误。</p>
|
<p v-if="hasErrors" class="release-error">请先修正画布校验错误。</p>
|
||||||
|
|||||||
@@ -36,10 +36,10 @@ export type Template = Pick<LayoutItem, "type" | "w" | "h" | "payload"> & { labe
|
|||||||
|
|
||||||
export const templates: Template[] = [
|
export const templates: Template[] = [
|
||||||
{ type: "app_icon", label: "应用图标", w: 132, h: 168, payload: { package: "com.tencent.mm", label: "微信" } },
|
{ type: "app_icon", label: "应用图标", w: 132, h: 168, payload: { package: "com.tencent.mm", label: "微信" } },
|
||||||
{ type: "app_icon", label: "相机", w: 132, h: 168, payload: { package: "com.android.camera", label: "相机" } },
|
{ type: "app_icon", label: "相机", w: 132, h: 168, payload: { package: "com.android.camera2", label: "相机" } },
|
||||||
{ type: "widget", label: "时钟组件", w: 864, h: 320, payload: { provider: "com.android.deskclock/.ClockWidget", label: "时钟" } },
|
{ type: "widget", label: "时钟组件", w: 864, h: 320, payload: { provider: "com.android.deskclock/com.android.alarmclock.DigitalAppWidgetProvider", label: "时钟" } },
|
||||||
{ type: "widget", label: "天气组件", w: 420, h: 300, payload: { provider: "com.android.weather/.WeatherWidget", label: "天气" } },
|
{ type: "widget", label: "搜索组件", w: 420, h: 300, payload: { provider: "com.android.quicksearchbox/com.android.quicksearchbox.SearchWidgetProvider", label: "搜索" } },
|
||||||
{ type: "widget", label: "音乐组件", w: 420, h: 300, payload: { provider: "com.spotify/.MusicWidget", label: "音乐" } },
|
{ type: "widget", label: "音乐组件", w: 420, h: 300, payload: { provider: "com.tencent.qqmusic/com.tencent.qqmusiccommon.WidgetProviderSmall", label: "音乐" } },
|
||||||
{ type: "shortcut", label: "快捷方式", w: 132, h: 168, payload: { intent: "weixin://scanqrcode", label: "扫一扫" } },
|
{ type: "shortcut", label: "快捷方式", w: 132, h: 168, payload: { intent: "weixin://scanqrcode", label: "扫一扫" } },
|
||||||
{ type: "dock_bg", label: "Dock 底板", w: 936, h: 200, payload: { radius: 56 } },
|
{ type: "dock_bg", label: "Dock 底板", w: 936, h: 200, payload: { radius: 56 } },
|
||||||
];
|
];
|
||||||
@@ -53,7 +53,7 @@ export const devices = [
|
|||||||
{ name: "平板 4:3", w: 1536, h: 2048 },
|
{ name: "平板 4:3", w: 1536, h: 2048 },
|
||||||
];
|
];
|
||||||
|
|
||||||
const canvasDefaults = { designWidth: 1080, designHeight: 1920, safeTop: 132, safeBottom: 72 };
|
const canvasDefaults = { designWidth: 1080, designHeight: 1920, safeTop: 72, safeBottom: 72 };
|
||||||
const fitDefaults = { mode: "contain", anchorY: 0.5 };
|
const fitDefaults = { mode: "contain", anchorY: 0.5 };
|
||||||
const object = (value: unknown): value is Record<string, unknown> =>
|
const object = (value: unknown): value is Record<string, unknown> =>
|
||||||
typeof value === "object" && value !== null && !Array.isArray(value);
|
typeof value === "object" && value !== null && !Array.isArray(value);
|
||||||
|
|||||||
Reference in new issue
Block a user