Add real-time layout sync and refresh default layout
This commit is contained in:
1 parent
274b8538b7
commit
f79e30897b
20 files changed
+289
-77
No files matched your search
@@ -82,6 +82,7 @@ dependencies {
|
||||
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1")
|
||||
implementation("com.squareup.okhttp3:okhttp:4.12.0")
|
||||
|
||||
debugImplementation("androidx.compose.ui:ui-tooling")
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"canvas": {
|
||||
"designWidth": 1080,
|
||||
"designHeight": 1920,
|
||||
"safeTop": 132,
|
||||
"safeTop": 72,
|
||||
"safeBottom": 72
|
||||
},
|
||||
"fit": {
|
||||
@@ -27,7 +27,7 @@
|
||||
"h": 320,
|
||||
"z": 1,
|
||||
"payload": {
|
||||
"provider": "com.android.deskclock/.ClockWidget",
|
||||
"provider": "com.android.deskclock/com.android.alarmclock.DigitalAppWidgetProvider",
|
||||
"label": "时钟"
|
||||
}
|
||||
},
|
||||
@@ -40,7 +40,7 @@
|
||||
"h": 168,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.demo.app0",
|
||||
"package": "com.tencent.mm",
|
||||
"label": "微信"
|
||||
}
|
||||
},
|
||||
@@ -53,7 +53,7 @@
|
||||
"h": 168,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.demo.app1",
|
||||
"package": "com.android.camera2",
|
||||
"label": "相机"
|
||||
}
|
||||
},
|
||||
@@ -66,7 +66,7 @@
|
||||
"h": 168,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.demo.app2",
|
||||
"package": "com.android.gallery3d",
|
||||
"label": "相册"
|
||||
}
|
||||
},
|
||||
@@ -79,7 +79,7 @@
|
||||
"h": 168,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.demo.app3",
|
||||
"package": "com.android.settings",
|
||||
"label": "设置"
|
||||
}
|
||||
},
|
||||
@@ -92,7 +92,7 @@
|
||||
"h": 168,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.demo.app4",
|
||||
"package": "com.android.chrome",
|
||||
"label": "浏览器"
|
||||
}
|
||||
},
|
||||
@@ -105,8 +105,8 @@
|
||||
"h": 168,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.demo.app5",
|
||||
"label": "音乐"
|
||||
"package": "com.tencent.qqmusic",
|
||||
"label": "QQ音乐"
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -118,8 +118,8 @@
|
||||
"h": 168,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.demo.app6",
|
||||
"label": "地图"
|
||||
"package": "com.android.dialer",
|
||||
"label": "电话"
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -131,7 +131,7 @@
|
||||
"h": 168,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"package": "com.demo.app7",
|
||||
"package": "com.android.calendar",
|
||||
"label": "日历"
|
||||
}
|
||||
},
|
||||
@@ -144,8 +144,8 @@
|
||||
"h": 300,
|
||||
"z": 1,
|
||||
"payload": {
|
||||
"provider": "com.android.weather/.WeatherWidget",
|
||||
"label": "天气"
|
||||
"provider": "com.android.quicksearchbox/com.android.quicksearchbox.SearchWidgetProvider",
|
||||
"label": "搜索"
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -157,63 +157,63 @@
|
||||
"h": 300,
|
||||
"z": 1,
|
||||
"payload": {
|
||||
"provider": "com.spotify/.MusicWidget",
|
||||
"label": "音乐"
|
||||
"provider": "com.tencent.qqmusic/com.tencent.qqmusiccommon.WidgetProviderSmall",
|
||||
"label": "QQ音乐"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "s0",
|
||||
"type": "shortcut",
|
||||
"type": "app_icon",
|
||||
"x": 150,
|
||||
"y": 1650,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"label": "扫一扫",
|
||||
"intent": "demo://0"
|
||||
"package": "com.android.contacts",
|
||||
"label": "联系人"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "s1",
|
||||
"type": "shortcut",
|
||||
"type": "app_icon",
|
||||
"x": 366,
|
||||
"y": 1650,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"label": "支付",
|
||||
"intent": "demo://1"
|
||||
"package": "com.android.messaging",
|
||||
"label": "短信"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "s2",
|
||||
"type": "shortcut",
|
||||
"type": "app_icon",
|
||||
"x": 582,
|
||||
"y": 1650,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"label": "钱包",
|
||||
"intent": "demo://2"
|
||||
"package": "com.android.documentsui",
|
||||
"label": "文件"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "s3",
|
||||
"type": "shortcut",
|
||||
"type": "app_icon",
|
||||
"x": 798,
|
||||
"y": 1650,
|
||||
"w": 132,
|
||||
"h": 168,
|
||||
"z": 2,
|
||||
"payload": {
|
||||
"label": "更多",
|
||||
"intent": "demo://3"
|
||||
"package": "com.tencent.mobileqq",
|
||||
"label": "QQ"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -133,7 +133,7 @@ class LauncherViewModel(app: Application) : AndroidViewModel(app) {
|
||||
companion object {
|
||||
/** 首帧占位:一页、零元素、壁纸透出。绝不做 IO */
|
||||
fun placeholderLayout() = LauncherLayout(
|
||||
canvas = CanvasDef(1080, 1920, 132, 72),
|
||||
canvas = CanvasDef(),
|
||||
fit = FitDef("contain", 0.5f),
|
||||
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 准,它是"即将真正绘制"的时刻
|
||||
window.decorView.viewTreeObserver.addOnPreDrawListener(
|
||||
object : ViewTreeObserver.OnPreDrawListener {
|
||||
|
||||
@@ -19,7 +19,7 @@ data class DesignCanvas(
|
||||
val designWidth: Int = 1080,
|
||||
val designHeight: Int = 1920,
|
||||
/** 内容顶部安全区(设计稿坐标系内),防止内容压到状态栏 */
|
||||
val safeTop: Int = 132,
|
||||
val safeTop: Int = 72,
|
||||
/** 内容底部安全区,避让手势条 */
|
||||
val safeBottom: Int = 72,
|
||||
)
|
||||
|
||||
@@ -28,7 +28,7 @@ data class LauncherLayout(
|
||||
data class CanvasDef(
|
||||
val designWidth: Int = 1080,
|
||||
val designHeight: Int = 1920,
|
||||
val safeTop: Int = 132,
|
||||
val safeTop: Int = 72,
|
||||
val safeBottom: Int = 72,
|
||||
) {
|
||||
fun toDesignCanvas() = DesignCanvas(designWidth, designHeight, safeTop, safeBottom)
|
||||
|
||||
@@ -2,8 +2,13 @@ package com.ttstd.compose.launcher.core
|
||||
|
||||
import android.content.Context
|
||||
import android.os.SystemClock
|
||||
import android.util.Log
|
||||
import com.ttstd.compose.launcher.BuildConfig
|
||||
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.ensureActive
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
@@ -13,6 +18,12 @@ import java.io.ByteArrayOutputStream
|
||||
import java.io.IOException
|
||||
import java.net.HttpURLConnection
|
||||
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) {
|
||||
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 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 {
|
||||
val savedServer = prefs.getString("server", null)
|
||||
val savedToken = prefs.getString("token", null)
|
||||
|
||||
Reference in new issue
Block a user