-
Notifications
You must be signed in to change notification settings - Fork 947
/
AbstractComposeViewDescriptor.kt
114 lines (99 loc) · 3.85 KB
/
AbstractComposeViewDescriptor.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.flipper.plugins.jetpackcompose.descriptors
import android.os.Build
import android.os.Debug
import android.util.Log
import android.view.View
import androidx.annotation.RequiresApi
import androidx.compose.ui.platform.AbstractComposeView
import androidx.inspection.DefaultArtTooling
import com.facebook.flipper.plugins.jetpackcompose.model.ComposeNode
import com.facebook.flipper.plugins.uidebugger.descriptors.ChainedDescriptor
import com.facebook.flipper.plugins.uidebugger.descriptors.WarningMessage
import facebook.internal.androidx.compose.ui.inspection.RecompositionHandler
import facebook.internal.androidx.compose.ui.inspection.inspector.InspectorNode
import facebook.internal.androidx.compose.ui.inspection.inspector.LayoutInspectorTree
import java.io.IOException
@RequiresApi(Build.VERSION_CODES.Q)
object AbstractComposeViewDescriptor : ChainedDescriptor<AbstractComposeView>() {
internal var hideSystemNodes: Boolean = true
private val recompositionHandler by lazy {
RecompositionHandler(DefaultArtTooling("Flipper")).apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
attachJvmtiAgent()
startTrackingRecompositions(this)
}
}
}
override fun onGetName(node: AbstractComposeView): String = node.javaClass.simpleName
private fun transform(
view: View,
nodes: List<InspectorNode>,
layoutInspectorTree: LayoutInspectorTree
): List<ComposeNode> {
val positionOnScreen = IntArray(2)
view.getLocationInWindow(positionOnScreen)
val xOffset = positionOnScreen[0]
val yOffset = positionOnScreen[1]
return nodes.map { node ->
ComposeNode(view, layoutInspectorTree, recompositionHandler, node, xOffset, yOffset)
}
}
override fun onGetChildren(node: AbstractComposeView): List<Any> {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
return listOf(
WarningMessage(
"Flipper Compose Plugin works only on devices with Android Q (API 29) and above.",
getBounds(node)))
}
val children = mutableListOf<Any>()
val count = node.childCount - 1
for (i in 0..count) {
val child: View = node.getChildAt(i)
children.add(child)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val layoutInspector = LayoutInspectorTree()
layoutInspector.hideSystemNodes = hideSystemNodes
val composeNodes =
try {
transform(child, layoutInspector.convert(child), layoutInspector)
} catch (t: Throwable) {
listOf(
WarningMessage(
"Unknown error occurred while trying to inspect compose node: ${t.message}",
getBounds(node)))
}
return if (composeNodes.isNullOrEmpty()) {
listOf(
WarningMessage(
"Go to developer options and make sure that \"Enable view attribute inspection\" option is enabled.",
getBounds(node)))
} else {
composeNodes
}
}
}
return children
}
@RequiresApi(Build.VERSION_CODES.Q)
private fun attachJvmtiAgent() {
try {
Debug.attachJvmtiAgent("nonexistent.so", null, null)
} catch (e: IOException) {
// expected: "nonexistent.so" doesn't exist, however attachJvmtiAgent call is enough
// to make art to load JVMTI plugin.
}
}
private fun startTrackingRecompositions(recompositionHandler: RecompositionHandler) {
try {
recompositionHandler.changeCollectionMode(startCollecting = true, keepCounts = true)
} catch (t: Throwable) {
Log.e("ComposeViewDescriptor", "Failed to start tracking recompositions", t)
}
}
}