Skip to content

Commit 3402c85

Browse files
Merge pull request #2545 from DataDog/aleksandr-gringauz/fix-npe-drawable-current-release
Fix NPE when Drawable.getCurrent returns null
2 parents 19edaea + 8caa3a4 commit 3402c85

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/utils/LegacyDrawableToColorMapper.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ open class LegacyDrawableToColorMapper(
200200
* @return the color to map to or null if not applicable
201201
*/
202202
private fun resolveStateListDrawable(drawable: StateListDrawable, internalLogger: InternalLogger): Int? {
203-
return mapDrawableToColor(drawable = drawable.current, internalLogger = internalLogger)
203+
// Drawable.getCurrent() can return null in case <selector> doesn't have an item for the default case.
204+
@Suppress("UNNECESSARY_SAFE_CALL")
205+
return drawable.current?.let { mapDrawableToColor(drawable = it, internalLogger = internalLogger) }
204206
}
205207

206208
companion object {

features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/utils/LegacyDrawableToColorMapperTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,17 @@ open class LegacyDrawableToColorMapperTest {
212212
// Then
213213
assertThat(result).isEqualTo(drawableColor)
214214
}
215+
216+
@Test
217+
fun `M map StateListDrawable to null W mapDrawableToColor() { Drawable getCurrent returns null }`() {
218+
val stateListDrawable = mock<StateListDrawable>().apply {
219+
whenever<Drawable?>(this.current) doReturn null
220+
}
221+
222+
// When
223+
val result = testedMapper.mapDrawableToColor(stateListDrawable, mockInternalLogger)
224+
225+
// Then
226+
assertThat(result).isNull()
227+
}
215228
}

sample/kotlin/src/main/kotlin/com/datadog/android/sample/sessionreplay/ImageComponentsFragment.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ internal class ImageComponentsFragment : Fragment() {
6464
backgroundId = R.drawable.selector_statelist_images
6565
)
6666

67+
setupStateListButton(
68+
rootView = rootView,
69+
buttonId = R.id.button_statelist_images_no_default,
70+
backgroundId = R.drawable.selector_statelist_images_no_default
71+
)
72+
6773
return rootView
6874
}
6975

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:drawable="@drawable/ic_dd_icon_red" android:state_pressed="true"/>
4+
<item android:drawable="@drawable/ic_dd_icon_green" android:state_selected="true" />
5+
</selector>

sample/kotlin/src/main/res/layout/fragment_image_components.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,17 @@
382382
android:contentDescription="@null"
383383
/>
384384

385+
<Button
386+
android:id="@+id/button_statelist_images_no_default"
387+
style="?android:attr/buttonBarButtonStyle"
388+
android:layout_marginStart="48dp"
389+
android:layout_width="48dp"
390+
android:layout_height="48dp"
391+
android:clickable="true"
392+
android:focusable="true"
393+
android:contentDescription="@null"
394+
/>
395+
385396
</LinearLayout>
386397

387398
</LinearLayout>

0 commit comments

Comments
 (0)