-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathComposeViewHierarchyNode.kt
More file actions
222 lines (200 loc) · 9.55 KB
/
ComposeViewHierarchyNode.kt
File metadata and controls
222 lines (200 loc) · 9.55 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") // to access internal vals
package io.sentry.android.replay.viewhierarchy
import android.annotation.TargetApi
import android.view.View
import androidx.compose.ui.graphics.isUnspecified
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.findRootCoordinates
import androidx.compose.ui.node.LayoutNode
import androidx.compose.ui.node.Owner
import androidx.compose.ui.semantics.SemanticsActions
import androidx.compose.ui.semantics.SemanticsProperties
import androidx.compose.ui.semantics.getOrNull
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.unit.TextUnit
import io.sentry.SentryLevel
import io.sentry.SentryOptions
import io.sentry.SentryReplayOptions
import io.sentry.android.replay.SentryReplayModifiers
import io.sentry.android.replay.util.ComposeTextLayout
import io.sentry.android.replay.util.boundsInWindow
import io.sentry.android.replay.util.findPainter
import io.sentry.android.replay.util.findTextAttributes
import io.sentry.android.replay.util.isMaskable
import io.sentry.android.replay.util.toOpaque
import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode.GenericViewHierarchyNode
import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode.ImageViewHierarchyNode
import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode.TextViewHierarchyNode
import java.lang.ref.WeakReference
@TargetApi(26)
internal object ComposeViewHierarchyNode {
/**
* Since Compose doesn't have a concept of a View class (they are all composable functions),
* we need to map the semantics node to a corresponding old view system class.
*/
private fun LayoutNode.getProxyClassName(isImage: Boolean): String {
return when {
isImage -> SentryReplayOptions.IMAGE_VIEW_CLASS_NAME
collapsedSemantics?.contains(SemanticsProperties.Text) == true ||
collapsedSemantics?.contains(SemanticsActions.SetText) == true ||
collapsedSemantics?.contains(SemanticsProperties.EditableText) == true -> SentryReplayOptions.TEXT_VIEW_CLASS_NAME
else -> "android.view.View"
}
}
private fun LayoutNode.shouldMask(isImage: Boolean, options: SentryOptions): Boolean {
val sentryPrivacyModifier = collapsedSemantics?.getOrNull(SentryReplayModifiers.SentryPrivacy)
if (sentryPrivacyModifier == "unmask") {
return false
}
if (sentryPrivacyModifier == "mask") {
return true
}
val className = getProxyClassName(isImage)
if (options.sessionReplay.unmaskViewClasses.contains(className)) {
return false
}
return options.sessionReplay.maskViewClasses.contains(className)
}
private var _rootCoordinates: WeakReference<LayoutCoordinates>? = null
private fun fromComposeNode(
node: LayoutNode,
parent: ViewHierarchyNode?,
distance: Int,
isComposeRoot: Boolean,
options: SentryOptions
): ViewHierarchyNode? {
val isInTree = node.isPlaced && node.isAttached
if (!isInTree) {
return null
}
if (isComposeRoot) {
_rootCoordinates = WeakReference(node.coordinates.findRootCoordinates())
}
val semantics = node.collapsedSemantics
val visibleRect = node.coordinates.boundsInWindow(_rootCoordinates?.get())
val isVisible = !node.outerCoordinator.isTransparent() &&
(semantics == null || !semantics.contains(SemanticsProperties.InvisibleToUser)) &&
visibleRect.height() > 0 && visibleRect.width() > 0
val isEditable = semantics?.contains(SemanticsActions.SetText) == true ||
semantics?.contains(SemanticsProperties.EditableText) == true
return when {
semantics?.contains(SemanticsProperties.Text) == true || isEditable -> {
val shouldMask = isVisible && node.shouldMask(isImage = false, options)
parent?.setImportantForCaptureToAncestors(true)
// TODO: if we get reports that it's slow, we can drop this, and just mask
// TODO: the whole view instead of per-line
val textLayoutResults = mutableListOf<TextLayoutResult>()
semantics?.getOrNull(SemanticsActions.GetTextLayoutResult)
?.action
?.invoke(textLayoutResults)
val (color, hasFillModifier) = node.findTextAttributes()
val textLayoutResult = textLayoutResults.firstOrNull()
var textColor = textLayoutResult?.layoutInput?.style?.color
if (textColor?.isUnspecified == true) {
textColor = color
}
val isLaidOut = textLayoutResult?.layoutInput?.style?.fontSize != TextUnit.Unspecified
// TODO: support editable text (currently there's a way to get @Composable's padding only via reflection, and we can't reliably mask input fields based on TextLayout, so we mask the whole view instead)
TextViewHierarchyNode(
layout = if (textLayoutResult != null && !isEditable && isLaidOut) {
ComposeTextLayout(textLayoutResult, hasFillModifier)
} else {
null
},
dominantColor = textColor?.toArgb()?.toOpaque(),
x = visibleRect.left.toFloat(),
y = visibleRect.top.toFloat(),
width = node.width,
height = node.height,
elevation = (parent?.elevation ?: 0f),
distance = distance,
parent = parent,
shouldMask = shouldMask,
isImportantForContentCapture = true,
isVisible = isVisible,
visibleRect = visibleRect
)
}
else -> {
val painter = node.findPainter()
if (painter != null) {
val shouldMask = isVisible && node.shouldMask(isImage = true, options)
parent?.setImportantForCaptureToAncestors(true)
ImageViewHierarchyNode(
x = visibleRect.left.toFloat(),
y = visibleRect.top.toFloat(),
width = node.width,
height = node.height,
elevation = (parent?.elevation ?: 0f),
distance = distance,
parent = parent,
isVisible = isVisible,
isImportantForContentCapture = true,
shouldMask = shouldMask && painter.isMaskable(),
visibleRect = visibleRect
)
} else {
val shouldMask = isVisible && node.shouldMask(isImage = false, options)
// TODO: this currently does not support embedded AndroidViews, we'd have to
// TODO: traverse the ViewHierarchyNode here again. For now we can recommend
// TODO: using custom modifiers to obscure the entire node if it's sensitive
GenericViewHierarchyNode(
x = visibleRect.left.toFloat(),
y = visibleRect.top.toFloat(),
width = node.width,
height = node.height,
elevation = (parent?.elevation ?: 0f),
distance = distance,
parent = parent,
shouldMask = shouldMask,
isImportantForContentCapture = false, /* will be set by children */
isVisible = isVisible,
visibleRect = visibleRect
)
}
}
}
}
fun fromView(view: View, parent: ViewHierarchyNode?, options: SentryOptions): Boolean {
if (!view::class.java.name.contains("AndroidComposeView")) {
return false
}
if (parent == null) {
return false
}
try {
val rootNode = (view as? Owner)?.root ?: return false
rootNode.traverse(parent, isComposeRoot = true, options)
} catch (e: Throwable) {
options.logger.log(
SentryLevel.ERROR,
e,
"""
Error traversing Compose tree. Most likely you're using an unsupported version of
androidx.compose.ui:ui. The minimum supported version is 1.5.0. If it's a newer
version, please open a github issue with the version you're using, so we can add
support for it.
""".trimIndent()
)
return false
}
return true
}
private fun LayoutNode.traverse(parentNode: ViewHierarchyNode, isComposeRoot: Boolean, options: SentryOptions) {
val children = this.children
if (children.isEmpty()) {
return
}
val childNodes = ArrayList<ViewHierarchyNode>(children.size)
for (index in children.indices) {
val child = children[index]
val childNode = fromComposeNode(child, parentNode, index, isComposeRoot, options)
if (childNode != null) {
childNodes.add(childNode)
child.traverse(childNode, isComposeRoot = false, options)
}
}
parentNode.children = childNodes
}
}