diff --git a/android/app/src/main/java/io/github/mdhaarith/airbridge/overlay/EdgePanel.kt b/android/app/src/main/java/io/github/mdhaarith/airbridge/overlay/EdgePanel.kt index f438e2d..0ac7c1e 100644 --- a/android/app/src/main/java/io/github/mdhaarith/airbridge/overlay/EdgePanel.kt +++ b/android/app/src/main/java/io/github/mdhaarith/airbridge/overlay/EdgePanel.kt @@ -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), @@ -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() @@ -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) { @@ -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 { @@ -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)) diff --git a/android/app/src/test/java/io/github/mdhaarith/airbridge/overlay/EdgePanelTest.kt b/android/app/src/test/java/io/github/mdhaarith/airbridge/overlay/EdgePanelTest.kt new file mode 100644 index 0000000..2e2d871 --- /dev/null +++ b/android/app/src/test/java/io/github/mdhaarith/airbridge/overlay/EdgePanelTest.kt @@ -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) + } + } +}