Skip to content

Commit ab0933b

Browse files
authored
Merge 3c0ae2f into 19d98e8
2 parents 19d98e8 + 3c0ae2f commit ab0933b

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

sentry-android-replay/src/main/java/io/sentry/android/replay/ScreenshotRecorder.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import io.sentry.SentryReplayOptions
2626
import io.sentry.android.replay.util.MainLooperHandler
2727
import io.sentry.android.replay.util.getVisibleRects
2828
import io.sentry.android.replay.util.gracefullyShutdown
29+
import io.sentry.android.replay.util.submitSafely
2930
import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode
3031
import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode.ImageViewHierarchyNode
3132
import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode.TextViewHierarchyNode
@@ -122,7 +123,7 @@ internal class ScreenshotRecorder(
122123
val viewHierarchy = ViewHierarchyNode.fromView(root, null, 0, options)
123124
root.traverse(viewHierarchy)
124125

125-
recorder.submit {
126+
recorder.submitSafely(options, "screenshot_recorder.redact") {
126127
val canvas = Canvas(bitmap)
127128
canvas.setMatrix(prescaledMatrix)
128129
viewHierarchy.traverse { node ->

sentry-android-replay/src/main/java/io/sentry/android/replay/capture/SessionCaptureStrategy.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ internal class SessionCaptureStrategy(
124124
}
125125

126126
override fun onConfigurationChanged(recorderConfig: ScreenshotRecorderConfig) {
127-
val currentSegmentTimestamp = segmentTimestamp ?: return
128127
createCurrentSegment("onConfigurationChanged") { segment ->
129128
if (segment is ReplaySegment.Created) {
130129
segment.capture(hub)

sentry-android-replay/src/main/java/io/sentry/android/replay/util/Views.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import android.os.Build.VERSION
1414
import android.os.Build.VERSION_CODES
1515
import android.text.Layout
1616
import android.view.View
17+
import android.widget.TextView
18+
import java.lang.NullPointerException
1719

1820
/**
1921
* Adapted copy of AccessibilityNodeInfo from https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/view/View.java;l=10718
@@ -26,7 +28,7 @@ internal fun View.isVisibleToUser(): Pair<Boolean, Rect?> {
2628
}
2729
// An invisible predecessor or one with alpha zero means
2830
// that this view is not visible to the user.
29-
var current: Any = this
31+
var current: Any? = this
3032
while (current is View) {
3133
val view = current
3234
val transitionAlpha = if (VERSION.SDK_INT >= VERSION_CODES.Q) view.transitionAlpha else 1f
@@ -53,7 +55,10 @@ internal fun Drawable?.isRedactable(): Boolean {
5355
// TODO: otherwise maybe check for the bitmap size and don't redact those that take a lot of height (e.g. a background of a whatsapp chat)
5456
return when (this) {
5557
is InsetDrawable, is ColorDrawable, is VectorDrawable, is GradientDrawable -> false
56-
is BitmapDrawable -> !bitmap.isRecycled && bitmap.height > 10 && bitmap.width > 10
58+
is BitmapDrawable -> {
59+
val bmp = bitmap ?: return false
60+
return !bmp.isRecycled && bmp.height > 10 && bmp.width > 10
61+
}
5762
else -> true
5863
}
5964
}
@@ -84,3 +89,15 @@ internal fun Layout?.getVisibleRects(globalRect: Rect, paddingLeft: Int, padding
8489
}
8590
return rects
8691
}
92+
93+
/**
94+
* [TextView.getVerticalOffset] which is used by [TextView.getTotalPaddingTop] may throw an NPE on
95+
* some devices (Redmi), so we try-catch it specifically for an NPE and then fallback to
96+
* [TextView.getExtendedPaddingTop]
97+
*/
98+
internal val TextView.totalPaddingTopSafe: Int
99+
get() = try {
100+
totalPaddingTop
101+
} catch (e: NullPointerException) {
102+
extendedPaddingTop
103+
}

sentry-android-replay/src/main/java/io/sentry/android/replay/video/SimpleVideoEncoder.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import android.graphics.Bitmap
3434
import android.media.MediaCodec
3535
import android.media.MediaCodecInfo
3636
import android.media.MediaFormat
37+
import android.os.Build
3738
import android.view.Surface
3839
import io.sentry.SentryLevel.DEBUG
3940
import io.sentry.SentryOptions
@@ -139,10 +140,13 @@ internal class SimpleVideoEncoder(
139140
}
140141

141142
fun encode(image: Bitmap) {
142-
// NOTE do not use `lockCanvas` like what is done in bitmap2video
143-
// This is because https://developer.android.com/reference/android/media/MediaCodec#createInputSurface()
144-
// says that, "Surface.lockCanvas(android.graphics.Rect) may fail or produce unexpected results."
145-
val canvas = surface?.lockHardwareCanvas()
143+
// it seems that Xiaomi devices have problems with hardware canvas, so we have to use
144+
// lockCanvas instead
145+
val canvas = if (Build.MANUFACTURER.contains("xiaomi", ignoreCase = true)) {
146+
surface?.lockCanvas(null)
147+
} else {
148+
surface?.lockHardwareCanvas()
149+
}
146150
canvas?.drawBitmap(image, 0f, 0f, null)
147151
surface?.unlockCanvasAndPost(canvas)
148152
drainCodec(false)

sentry-android-replay/src/main/java/io/sentry/android/replay/viewhierarchy/ViewHierarchyNode.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.widget.TextView
99
import io.sentry.SentryOptions
1010
import io.sentry.android.replay.util.isRedactable
1111
import io.sentry.android.replay.util.isVisibleToUser
12+
import io.sentry.android.replay.util.totalPaddingTopSafe
1213

1314
@TargetApi(26)
1415
sealed class ViewHierarchyNode(
@@ -245,7 +246,7 @@ sealed class ViewHierarchyNode(
245246
layout = view.layout,
246247
dominantColor = view.currentTextColor.toOpaque(),
247248
paddingLeft = view.totalPaddingLeft,
248-
paddingTop = view.totalPaddingTop,
249+
paddingTop = view.totalPaddingTopSafe,
249250
x = view.x,
250251
y = view.y,
251252
width = view.width,

0 commit comments

Comments
 (0)