184 lines
6.9 KiB
Markdown
184 lines
6.9 KiB
Markdown
# Android 端接入说明
|
||
|
||
> 网页端产出 `launcher_layout.json`(schemaVersion 3),Android 端按「设计稿坐标 → 等比居中」渲染。
|
||
> 手机端只读,不带任何拖拽逻辑。
|
||
|
||
---
|
||
|
||
## 一、文件清单
|
||
|
||
| 文件 | 职责 | 依赖 |
|
||
|---|---|---|
|
||
| `CanvasMapper.kt` | **映射核心**:`contain` / `elastic` / `stretch` 三种策略 + 安全区修正 | 纯 Kotlin,零 Android 依赖,可放 `:shared` 模块 |
|
||
| `LayoutSpec.kt` | 协议数据类 + JSON 解析 + 校验(与网页端规则逐条对齐) | kotlinx-serialization |
|
||
| `LayoutRepository.kt` | 三级兜底加载、原子写入、坏包隔离 | coroutines |
|
||
| `IconLoader.kt` | 图标加载 + LruCache + 后台解码 | — |
|
||
| `DesignCanvasRenderer.kt` | Compose 渲染:壁纸层 + Pager + 逐元素像素定位 | Compose |
|
||
| `AppWidgetHostController.kt` | AppWidget 绑定与尺寸下发 | — |
|
||
| `LauncherActivity.kt` | 冷启动分层、首帧埋点、ViewModel | activity-compose |
|
||
| `assets/default_layout.json` | 首启内置布局(已校验无越界无重叠) | — |
|
||
|
||
旧的 `GridWorkspace.kt`、`LayoutEngine.kt` 是**网格编辑版**的实现,本方案不再需要(没有拖拽就没有碰撞与压缩)。留着只作对照。
|
||
|
||
---
|
||
|
||
## 二、依赖
|
||
|
||
`build.gradle.kts`(module):
|
||
|
||
```kotlin
|
||
plugins {
|
||
id("org.jetbrains.kotlin.plugin.serialization") version "2.0.21"
|
||
}
|
||
|
||
dependencies {
|
||
implementation("androidx.core:core-ktx:1.13.1")
|
||
implementation("androidx.activity:activity-compose:1.9.3")
|
||
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.7")
|
||
implementation(platform("androidx.compose:compose-bom:2024.10.01"))
|
||
implementation("androidx.compose.ui:ui")
|
||
implementation("androidx.compose.foundation:foundation") // HorizontalPager 在 foundation 里
|
||
implementation("androidx.compose.material3:material3")
|
||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
|
||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.9.0")
|
||
}
|
||
```
|
||
|
||
> `HorizontalPager` 在 `androidx.compose.foundation.pager`,从 Compose 1.4 起已稳定,不需要额外库。
|
||
|
||
---
|
||
|
||
## 三、Manifest
|
||
|
||
```xml
|
||
<activity
|
||
android:name=".canvas.LauncherActivity"
|
||
android:exported="true"
|
||
android:launchMode="singleTask"
|
||
android:stateNotNeeded="true"
|
||
android:theme="@style/Theme.Launcher">
|
||
|
||
<intent-filter>
|
||
<action android:name="android.intent.action.MAIN" />
|
||
<category android:name="android.intent.category.HOME" />
|
||
<category android:name="android.intent.category.DEFAULT" />
|
||
</intent-filter>
|
||
</activity>
|
||
```
|
||
|
||
主题里给窗口一个深色背景,避免首帧白闪:
|
||
|
||
```xml
|
||
<style name="Theme.Launcher" parent="android:Theme.Material.NoActionBar">
|
||
<item name="android:windowBackground">#11151C</item>
|
||
<item name="android:statusBarColor">@android:color/transparent</item>
|
||
<item name="android:navigationBarColor">@android:color/transparent</item>
|
||
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
|
||
</style>
|
||
```
|
||
|
||
`windowLayoutInDisplayCutoutMode=shortEdges` 必须有,否则刘海屏上壁纸铺不满、内容被压到刘海下方。
|
||
|
||
---
|
||
|
||
## 四、接入五步
|
||
|
||
**1. 把 `launcher_layout.json` 放进 `app/src/main/assets/default_layout.json`**
|
||
|
||
**2. 启动时加载**
|
||
|
||
```kotlin
|
||
val repo = LayoutRepository(context)
|
||
val layout = repo.load() // suspend,内部已做三级兜底
|
||
```
|
||
|
||
**3. 渲染**
|
||
|
||
```kotlin
|
||
LauncherCanvas(
|
||
layout = layout,
|
||
icons = IconLoader(context),
|
||
statusBarPx = statusBarHeightPx(),
|
||
widgetHost = AppWidgetHostController(context),
|
||
)
|
||
```
|
||
|
||
`statusBarPx` 一定要传真实值。渲染端会用它做 `avoidSystemInsets`,否则在 1440 屏上 `safeTop=132` 会被状态栏压掉 44px。
|
||
|
||
**4. 热更新布局**
|
||
|
||
```kotlin
|
||
lifecycleScope.launch {
|
||
repo.save(jsonFromServer).onFailure { Log.w("Layout", "坏包已拒绝:${it.message}") }
|
||
}
|
||
```
|
||
|
||
`save()` 会先解析校验,坏包直接返回失败、不落盘。落盘前把当前可用版本备份成 `last_good_layout.json`。
|
||
|
||
**5. 多页翻页**已内置在 `HorizontalPager` 里,`beyondViewportPageCount = 1` 预组合相邻页,翻页不白屏。
|
||
|
||
---
|
||
|
||
## 五、四个必须知道的边界
|
||
|
||
### 1)AppWidget 需要系统权限
|
||
|
||
`BIND_APPWIDGET` 是 `signature|privileged` 级。普通 App 只能在用户点「添加小组件」时走系统授权弹窗:
|
||
|
||
```kotlin
|
||
val intent = Intent(AppWidgetManager.ACTION_APPWIDGET_BIND).apply {
|
||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id)
|
||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider)
|
||
}
|
||
startActivityForResult(intent, REQ_BIND)
|
||
```
|
||
|
||
系统签名 Launcher 直接有权限,不需要这步。**如果你们是预装系统应用,这条不构成障碍;如果是应用商店分发,第三方 widget 基本加不进来。**
|
||
|
||
### 2)widget 必须下发尺寸
|
||
|
||
`updateAppWidgetOptions` 不给的后果是 widget 内部按 0 尺寸排版,表现为「组件一片空白」。代码里每次尺寸变化都会重发一次,别优化掉。
|
||
|
||
### 3)`hostId` 必须持久化
|
||
|
||
每次启动换 `hostId`,系统认为你放弃了旧实例,用户已加的 widget 全被回收。代码里用 SharedPreferences 存。
|
||
|
||
### 4)跨设备一致性有物理上限
|
||
|
||
设计稿 16:9 放到 19.5:9 上,等比居中后上下各留 210px。这个留白**无法消除**——消除它只有两条路:裁切(切掉内容)或拉伸(图标变形)。当前实现选了第三条:**留白交给壁纸**(画布背景透明),用户感知不到。
|
||
|
||
如果某些设备上留白确实太难看,把协议里的 `fit.mode` 改成 `elastic`,留白会转成行间距,代价是元素间距变化 5%~50%。
|
||
|
||
---
|
||
|
||
## 六、验证
|
||
|
||
`CanvasMapper` 是纯 Kotlin,可以在 JVM 上直接跑,不需要真机:
|
||
|
||
```bash
|
||
kotlinc launcher-kt/canvas/CanvasMapper.kt test/CanvasTest.kt -include-runtime -d t.jar
|
||
java -cp t.jar CanvasTestKt
|
||
```
|
||
|
||
当前 **49 项断言全过**,覆盖:
|
||
|
||
- 三种策略在 16:9 / 16:8 / 19.5:9 / 20:9 / 21:9 / 16:10 / 平板 4:3 上的缩放、留白、变形
|
||
- 双向居中不变式:高屏左右留白必须为 0 且上下对称、宽屏上下留白必须为 0 且左右对称
|
||
- 等比缩放下**不会同时出现横纵留白**
|
||
- 平板 4:3 上 `elastic` 自动退回 `contain`(否则纵向溢出)
|
||
- 状态栏修正:内容不得压到真机状态栏
|
||
- 协议校验:越界、负坐标、重复 id、缺 package / provider、进安全区
|
||
- 空列表、零尺寸不崩
|
||
|
||
真机验收清单:
|
||
|
||
| 项 | 期望 |
|
||
|---|---|
|
||
| 首帧 | < 300ms 看到壁纸 + 布局轮廓 |
|
||
| 图标 | 加载完成后替换首字母占位,无闪烁跳动 |
|
||
| 19.5:9 | 上下各留 210px,壁纸铺满,看不出"空了一块" |
|
||
| 平板 4:3 | 左右各留 80px,内容完整不裁切 |
|
||
| 翻页 | 相邻页预组合,无白屏 |
|
||
| 坏包 | 推送非法 JSON → 桌面不变,logcat 有拒绝原因 |
|
||
| widget | 绑定后内容正常,不空白、不重叠 |
|