Skip to content

Commit 3ebb2b5

Browse files
authored
Merge pull request #2784 from DataDog/yl/fix-session-replay-crash
RUM-10703: Fix Session Replay NPE when getting TextView padding
2 parents dc9cc0b + 5984380 commit 3ebb2b5

File tree

2 files changed

+46
-7
lines changed
  • features/dd-sdk-android-session-replay/src

2 files changed

+46
-7
lines changed

features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/recorder/mapper/TextViewMapper.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,21 @@ open class TextViewMapper<in T : TextView>(
188188
}
189189

190190
private fun resolvePadding(textView: TextView, pixelsDensity: Float): MobileSegment.Padding {
191-
return MobileSegment.Padding(
192-
top = textView.totalPaddingTop.densityNormalized(pixelsDensity).toLong(),
193-
bottom = textView.totalPaddingBottom.densityNormalized(pixelsDensity).toLong(),
194-
left = textView.totalPaddingStart.densityNormalized(pixelsDensity).toLong(),
195-
right = textView.totalPaddingEnd.densityNormalized(pixelsDensity).toLong()
196-
)
191+
return if (textView.layout != null) {
192+
MobileSegment.Padding(
193+
top = textView.totalPaddingTop.densityNormalized(pixelsDensity).toLong(),
194+
bottom = textView.totalPaddingBottom.densityNormalized(pixelsDensity).toLong(),
195+
left = textView.totalPaddingStart.densityNormalized(pixelsDensity).toLong(),
196+
right = textView.totalPaddingEnd.densityNormalized(pixelsDensity).toLong()
197+
)
198+
} else {
199+
MobileSegment.Padding(
200+
top = textView.paddingTop.densityNormalized(pixelsDensity).toLong(),
201+
bottom = textView.paddingBottom.densityNormalized(pixelsDensity).toLong(),
202+
left = textView.paddingStart.densityNormalized(pixelsDensity).toLong(),
203+
right = textView.paddingEnd.densityNormalized(pixelsDensity).toLong()
204+
)
205+
}
197206
}
198207

199208
private fun resolveAlignment(textView: TextView): MobileSegment.Alignment {

features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/recorder/mapper/TextViewMapperTest.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.graphics.Typeface
44
import android.text.Layout
55
import android.view.Gravity
66
import android.widget.TextView
7+
import com.datadog.android.internal.utils.densityNormalized
78
import com.datadog.android.sessionreplay.TextAndInputPrivacy
89
import com.datadog.android.sessionreplay.model.MobileSegment
910
import com.datadog.android.sessionreplay.model.MobileSegment.Horizontal.LEFT
@@ -47,6 +48,18 @@ internal abstract class TextViewMapperTest :
4748
@IntForgery(min = 0, max = 0xffffff)
4849
var fakeTextColor: Int = 0
4950

51+
@IntForgery(min = 0, max = 100)
52+
var fakeTotalBottomPadding: Int = 0
53+
54+
@IntForgery(min = 0, max = 100)
55+
var fakeTotalTopPadding: Int = 0
56+
57+
@IntForgery
58+
var fakeTotalStartPadding: Int = 0
59+
60+
@IntForgery
61+
var fakeTotalEndPadding: Int = 0
62+
5063
@StringForgery(regex = "#[0-9A-F]{8}")
5164
lateinit var fakeTextColorHexString: String
5265

@@ -93,6 +106,10 @@ internal abstract class TextViewMapperTest :
93106
whenever(mockView.textAlignment) doReturn fakeTextAlignment
94107
whenever(mockView.gravity) doReturn fakeTextGravity
95108
whenever(mockView.text) doReturn fakeText
109+
whenever(mockView.totalPaddingBottom) doReturn fakeTotalBottomPadding
110+
whenever(mockView.totalPaddingTop) doReturn fakeTotalTopPadding
111+
whenever(mockView.totalPaddingStart) doReturn fakeTotalStartPadding
112+
whenever(mockView.totalPaddingEnd) doReturn fakeTotalEndPadding
96113
}
97114
val expectedFontSize = (fakeFontSize / fakeMappingContext.systemInformation.screenDensity).toLong()
98115

@@ -127,7 +144,20 @@ internal abstract class TextViewMapperTest :
127144
color = fakeTextColorHexString
128145
),
129146
textPosition = MobileSegment.TextPosition(
130-
padding = MobileSegment.Padding(0L, 0L, 0L, 0L),
147+
padding = MobileSegment.Padding(
148+
top = fakeTotalTopPadding
149+
.densityNormalized(fakeMappingContext.systemInformation.screenDensity)
150+
.toLong(),
151+
fakeTotalBottomPadding
152+
.densityNormalized(fakeMappingContext.systemInformation.screenDensity)
153+
.toLong(),
154+
fakeTotalStartPadding
155+
.densityNormalized(fakeMappingContext.systemInformation.screenDensity)
156+
.toLong(),
157+
fakeTotalEndPadding
158+
.densityNormalized(fakeMappingContext.systemInformation.screenDensity)
159+
.toLong()
160+
),
131161
alignment = MobileSegment.Alignment(
132162
horizontal = expectedHorizontal,
133163
vertical = expectedVertical

0 commit comments

Comments
 (0)