Hide surface holder from EngineWrapper interface
EarlyInitDetails now consists of only WatchFaceService-generated things (complications, schema, flavors).
It allows to create it earlier and to decouple init sequence a bit.
Test: existing tests + menually. checked preview generation in the system.
Bug: 258210649
Change-Id: I5db6abb266ccab510fa8cf0792162bd70e0ed283
diff --git a/wear/watchface/watchface/src/main/java/androidx/wear/watchface/WatchFaceService.kt b/wear/watchface/watchface/src/main/java/androidx/wear/watchface/WatchFaceService.kt
index 0e94efe..9453e7b 100644
--- a/wear/watchface/watchface/src/main/java/androidx/wear/watchface/WatchFaceService.kt
+++ b/wear/watchface/watchface/src/main/java/androidx/wear/watchface/WatchFaceService.kt
@@ -1137,8 +1137,7 @@
internal class EarlyInitDetails(
val complicationSlotsManager: ComplicationSlotsManager,
val userStyleRepository: CurrentUserStyleRepository,
- val userStyleFlavors: UserStyleFlavors,
- val surfaceHolder: SurfaceHolder
+ val userStyleFlavors: UserStyleFlavors
)
/** @hide */
@@ -1182,7 +1181,7 @@
* [deferredSurfaceHolder] will complete after [onSurfaceChanged], before then it's not
* safe to create a UiThread OpenGL context.
*/
- internal var deferredSurfaceHolder = CompletableDeferred<SurfaceHolder>()
+ private var deferredSurfaceHolder = CompletableDeferred<SurfaceHolder>()
internal val mutableWatchState = getMutableWatchState().apply {
isVisible.value = this@EngineWrapper.isVisible || forceIsVisibleForTesting()
@@ -2028,6 +2027,15 @@
asyncWatchFaceConstructionPending = true
createdBy = _createdBy
+ // In case of overrideSurfaceHolder provided (tests) return its size instead of real
+ // metrics.
+ screenBounds = if (overrideSurfaceHolder != null) {
+ overrideSurfaceHolder.surfaceFrame
+ } else {
+ val displayMetrics = resources.displayMetrics
+ Rect(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels)
+ }
+
backgroundThreadCoroutineScope.launch {
val timeBefore = System.currentTimeMillis()
val currentUserStyleRepository =
@@ -2049,6 +2057,14 @@
createUserStyleFlavors(currentUserStyleRepository, complicationSlotsManager)
}
+ deferredEarlyInitDetails.complete(
+ EarlyInitDetails(
+ complicationSlotsManager,
+ currentUserStyleRepository,
+ userStyleFlavors
+ )
+ )
+
val deferredWatchFace = CompletableDeferred<WatchFace>()
val initComplicationsDone = CompletableDeferred<Unit>()
@@ -2067,14 +2083,6 @@
try {
val surfaceHolder = overrideSurfaceHolder ?: deferredSurfaceHolder.await()
- deferredEarlyInitDetails.complete(
- EarlyInitDetails(
- complicationSlotsManager,
- currentUserStyleRepository,
- userStyleFlavors,
- surfaceHolder
- )
- )
val watchFace = TraceEvent("WatchFaceService.createWatchFace").use {
// Note by awaiting deferredSurfaceHolder we ensure onSurfaceChanged has
@@ -2659,6 +2667,9 @@
}
}
+ internal lateinit var screenBounds: Rect
+ private set
+
@UiThread
internal fun dump(writer: IndentingPrintWriter) {
require(uiThreadHandler.looper.isCurrentThread) {
diff --git a/wear/watchface/watchface/src/main/java/androidx/wear/watchface/control/HeadlessWatchFaceImpl.kt b/wear/watchface/watchface/src/main/java/androidx/wear/watchface/control/HeadlessWatchFaceImpl.kt
index badd1a8..584f4d4 100644
--- a/wear/watchface/watchface/src/main/java/androidx/wear/watchface/control/HeadlessWatchFaceImpl.kt
+++ b/wear/watchface/watchface/src/main/java/androidx/wear/watchface/control/HeadlessWatchFaceImpl.kt
@@ -85,11 +85,13 @@
"HeadlessWatchFaceImpl.getPreviewReferenceTimeMillis"
) { it.previewReferenceInstant.toEpochMilli() } ?: 0
- override fun getComplicationState() =
+ override fun getComplicationState() = run {
+ val engineCopy = engine
WatchFaceService.awaitDeferredEarlyInitDetailsThenRunOnBinderThread(
- engine,
+ engineCopy,
"HeadlessWatchFaceImpl.getComplicationState"
- ) { it.complicationSlotsManager.getComplicationsState(it.surfaceHolder.surfaceFrame) }
+ ) { it.complicationSlotsManager.getComplicationsState(engineCopy!!.screenBounds) }
+ }
override fun renderComplicationToBitmap(params: ComplicationRenderParams) =
WatchFaceService.awaitDeferredWatchFaceImplThenRunOnUiThreadBlocking(
diff --git a/wear/watchface/watchface/src/main/java/androidx/wear/watchface/control/InteractiveWatchFaceImpl.kt b/wear/watchface/watchface/src/main/java/androidx/wear/watchface/control/InteractiveWatchFaceImpl.kt
index 3134a78..3a82120 100644
--- a/wear/watchface/watchface/src/main/java/androidx/wear/watchface/control/InteractiveWatchFaceImpl.kt
+++ b/wear/watchface/watchface/src/main/java/androidx/wear/watchface/control/InteractiveWatchFaceImpl.kt
@@ -189,10 +189,11 @@
}
override fun getComplicationDetails(): List<IdAndComplicationStateWireFormat>? {
+ val engineCopy = engine
return WatchFaceService.awaitDeferredEarlyInitDetailsThenRunOnBinderThread(
- engine,
+ engineCopy,
"InteractiveWatchFaceImpl.getComplicationDetails"
- ) { it.complicationSlotsManager.getComplicationsState(it.surfaceHolder.surfaceFrame) }
+ ) { it.complicationSlotsManager.getComplicationsState(engineCopy!!.screenBounds) }
}
override fun getUserStyleSchema(): UserStyleSchemaWireFormat? {