Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ class EdgePanel(private val context: Context) {
fun show() {
if (handle != null || !Settings.canDrawOverlays(context)) return
val rightSide = engine.settings.value.edgeSide != "left"
val view = HandleRoot(context, rightSide)
val view = runCatching { HandleRoot(context, rightSide) }.getOrElse {
Log.w(TAG, "could not build the edge handle", it)
return
}
val metrics = context.resources.displayMetrics
val params = WindowManager.LayoutParams(
dp(HANDLE_WIDTH), dp(HANDLE_HEIGHT),
Expand Down Expand Up @@ -159,8 +162,6 @@ class EdgePanel(private val context: Context) {
loadPhoto()
val owner = OverlayOwner().also { this.owner = it }
val compose = ComposeView(context).apply {
setViewTreeLifecycleOwner(owner)
setViewTreeSavedStateRegistryOwner(owner)
setContent {
AirBridgeTheme {
val peers by engine.peers.collectAsState()
Expand Down Expand Up @@ -188,6 +189,9 @@ class EdgePanel(private val context: Context) {
if (hasWindowFocus) readClipboard()
}
}
// Compose resolves its window recomposer from the overlay root.
root.setViewTreeLifecycleOwner(owner)
root.setViewTreeSavedStateRegistryOwner(owner)
root.addView(compose)
var receivingTarget: String? = null
if (Build.VERSION.SDK_INT >= 31) {
Expand Down Expand Up @@ -474,8 +478,6 @@ class EdgePanel(private val context: Context) {
private val pill = HandleView(context, rightSide, this)
private val owner = OverlayOwner()
private val tab = ComposeView(context).apply {
setViewTreeLifecycleOwner(owner)
setViewTreeSavedStateRegistryOwner(owner)
visibility = GONE
setContent {
AirBridgeTheme {
Expand All @@ -489,6 +491,8 @@ class EdgePanel(private val context: Context) {
private var receivingTarget: String? = null

init {
setViewTreeLifecycleOwner(owner)
setViewTreeSavedStateRegistryOwner(owner)
contentDescription = "AirBridge edge panel"
addView(pill, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
addView(tab, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.github.mdhaarith.airbridge.overlay

import android.content.Context
import android.view.View
import androidx.lifecycle.findViewTreeLifecycleOwner
import androidx.savedstate.findViewTreeSavedStateRegistryOwner
import app.cash.paparazzi.HtmlReportWriter
import app.cash.paparazzi.Paparazzi
import io.github.mdhaarith.airbridge.AirBridgeApp
import io.github.mdhaarith.airbridge.core.Engine
import io.github.mdhaarith.airbridge.core.FakePlatform
import org.junit.Assert.assertNotNull
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder

class EdgePanelTest {
@get:Rule
val paparazzi = Paparazzi(snapshotHandler = HtmlReportWriter())

@get:Rule
val tmp = TemporaryFolder()

@Test
fun handleRootOwnsTheWindowRecomposerLifecycle() {
// Exercise the real private handle without starting a service or window.
val app = AirBridgeApp()
val engine = Engine(FakePlatform(tmp.newFolder(), "Pixel"))
AirBridgeApp::class.java.getDeclaredField("engine\$delegate").apply {
isAccessible = true
}.set(app, lazy { engine })
val instance = AirBridgeApp::class.java.getDeclaredField("instance").apply { isAccessible = true }
val previous = instance.get(null)
instance.set(null, app)
try {
val panel = EdgePanel(paparazzi.context)
try {
val handle = Class.forName("${EdgePanel::class.java.name}\$HandleRoot")
.getDeclaredConstructor(EdgePanel::class.java, Context::class.java, Boolean::class.javaPrimitiveType)
.apply { isAccessible = true }
.newInstance(panel, paparazzi.context, true) as View
// Owners on the nested ComposeView cannot be found from the root.
assertNotNull(handle.findViewTreeLifecycleOwner())
assertNotNull(handle.findViewTreeSavedStateRegistryOwner())
} finally {
panel.destroy()
}
} finally {
instance.set(null, previous)
}
}
}