Skip to content

Commit 3b6fa83

Browse files
romtsnclaude
andauthored
feat(replay): Add pauseTouchTracking and resumeTouchTracking (#6546)
Apps can stop recording touches on sensitive screens, such as a custom PIN pad, while the masked screen recording continues. Masking hides the content, but recorded touch positions could reveal the input. The flag is independent of pause()/resume() and is cleared only by resumeTouchTracking(), also across new replays. It is set directly instead of being queued on the main thread, so it applies to touch events that are already pending. Also mark debugMaskingEnabled as volatile, it is written and read on different threads. Fixes JAVA-658 Fixes #5787 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 13ec6c7 commit 3b6fa83

7 files changed

Lines changed: 135 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
## Unreleased
99

10+
### Features
11+
12+
- Session Replay: Add `Sentry.replay().pauseTouchTracking()` and `resumeTouchTracking()` to stop recording touches on sensitive screens, such as a custom PIN pad, while the screen recording continues. Touch tracking stays paused until `resumeTouchTracking()` is called, also for new replays and after `resume()` ([#6546](https://github.com/getsentry/sentry-java/pull/6546))
13+
1014
### Performance
1115

1216
- Most SDK log calls no longer allocate when `debug` is off ([#6600](https://github.com/getsentry/sentry-java/pull/6600))

‎sentry-android-replay/api/sentry-android-replay.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ public final class io/sentry/android/replay/ReplayIntegration : io/sentry/IConne
7979
public fun onTouchEvent (Landroid/view/MotionEvent;)V
8080
public fun onWindowSizeChanged (II)V
8181
public fun pause ()V
82+
public fun pauseTouchTracking ()V
8283
public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V
8384
public fun registerSegmentName (Ljava/lang/String;)V
8485
public fun registerTraceId (Lio/sentry/protocol/SentryId;)V
8586
public fun resume ()V
87+
public fun resumeTouchTracking ()V
8688
public fun setBreadcrumbConverter (Lio/sentry/ReplayBreadcrumbConverter;)V
8789
public fun start ()V
8890
public fun startBuffering ()V

‎sentry-android-replay/src/main/java/io/sentry/android/replay/ReplayIntegration.kt‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public class ReplayIntegration(
114114
}
115115

116116
private var lastKnownConnectionStatus: ConnectionStatus = ConnectionStatus.UNKNOWN
117-
private var debugMaskingEnabled: Boolean = false
117+
@Volatile private var debugMaskingEnabled: Boolean = false
118118
private lateinit var options: SentryOptions
119119
private var scopes: IScopes? = null
120120
private var recorder: Recorder? = null
@@ -135,6 +135,11 @@ public class ReplayIntegration(
135135

136136
internal val isEnabled = AtomicBoolean(false)
137137
internal var isManualPause = false
138+
// Not reset by start()/stop(), only resumeTouchTracking() clears it, so touches cannot leak
139+
// by accident. Set directly instead of enqueueOnMainThread like lifecycle commands: it does not
140+
// touch ReplayState, so ordering does not matter, and a queued set could let already-pending
141+
// touch events through.
142+
@Volatile private var isTouchTrackingPaused = false
138143
public val replayCacheDir: File?
139144
get() = state.get().captureStrategy?.replayCacheDir
140145

@@ -397,6 +402,14 @@ public class ReplayIntegration(
397402
}
398403
}
399404

405+
override fun pauseTouchTracking() {
406+
isTouchTrackingPaused = true
407+
}
408+
409+
override fun resumeTouchTracking() {
410+
isTouchTrackingPaused = false
411+
}
412+
400413
override fun enableDebugMaskingOverlay() {
401414
debugMaskingEnabled = true
402415
}
@@ -606,7 +619,7 @@ public class ReplayIntegration(
606619

607620
override fun onTouchEvent(event: MotionEvent) {
608621
val current = state.get()
609-
if (!isEnabled.get() || !current.isTouchRecordingAllowed) {
622+
if (!isEnabled.get() || isTouchTrackingPaused || !current.isTouchRecordingAllowed) {
610623
return
611624
}
612625
current.captureStrategy?.onTouchEvent(event)

‎sentry-android-replay/src/test/java/io/sentry/android/replay/ReplayIntegrationTest.kt‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,93 @@ class ReplayIntegrationTest {
15981598
verify(captureStrategy, never()).onTouchEvent(any())
15991599
}
16001600

1601+
@Test
1602+
fun `pauseTouchTracking drops touches but keeps recording screenshots`() {
1603+
val captureStrategy = mock<CaptureStrategy>()
1604+
val recorder = mock<Recorder>()
1605+
val replay =
1606+
fixture.getSut(
1607+
context,
1608+
recorderProvider = { recorder },
1609+
replayCaptureStrategyProvider = { captureStrategy },
1610+
)
1611+
1612+
replay.register(fixture.scopes, fixture.options)
1613+
replay.start()
1614+
replay.pauseTouchTracking()
1615+
replay.onTouchEvent(mock())
1616+
1617+
verify(captureStrategy, never()).onTouchEvent(any())
1618+
verify(recorder, never()).pause()
1619+
verify(captureStrategy, never()).pause()
1620+
assertTrue(replay.isRecording)
1621+
1622+
replay.resumeTouchTracking()
1623+
replay.onTouchEvent(mock())
1624+
1625+
verify(captureStrategy).onTouchEvent(any())
1626+
}
1627+
1628+
@Test
1629+
fun `resume does not resume paused touch tracking`() {
1630+
val captureStrategy = mock<CaptureStrategy>()
1631+
val replay = fixture.getSut(context, replayCaptureStrategyProvider = { captureStrategy })
1632+
1633+
replay.register(fixture.scopes, fixture.options)
1634+
replay.start()
1635+
replay.pauseTouchTracking()
1636+
replay.pause()
1637+
replay.resume()
1638+
replay.onTouchEvent(mock())
1639+
1640+
verify(captureStrategy, never()).onTouchEvent(any())
1641+
}
1642+
1643+
@Test
1644+
fun `resumeTouchTracking does not record touches while replay is paused`() {
1645+
val captureStrategy = mock<CaptureStrategy>()
1646+
val replay = fixture.getSut(context, replayCaptureStrategyProvider = { captureStrategy })
1647+
1648+
replay.register(fixture.scopes, fixture.options)
1649+
replay.start()
1650+
replay.pauseTouchTracking()
1651+
replay.pause()
1652+
replay.resumeTouchTracking()
1653+
replay.onTouchEvent(mock())
1654+
1655+
verify(captureStrategy, never()).onTouchEvent(any())
1656+
1657+
replay.resume()
1658+
replay.onTouchEvent(mock())
1659+
1660+
verify(captureStrategy).onTouchEvent(any())
1661+
}
1662+
1663+
@Test
1664+
fun `paused touch tracking stays paused for new replays`() {
1665+
val firstStrategy = mock<CaptureStrategy>()
1666+
val secondStrategy = mock<CaptureStrategy>()
1667+
val thirdStrategy = mock<CaptureStrategy>()
1668+
val strategies = ArrayDeque(listOf(firstStrategy, secondStrategy, thirdStrategy))
1669+
val replay =
1670+
fixture.getSut(context, replayCaptureStrategyProvider = { strategies.removeFirst() })
1671+
1672+
replay.register(fixture.scopes, fixture.options)
1673+
// called before any replay is started
1674+
replay.pauseTouchTracking()
1675+
replay.onAppForegrounded(true)
1676+
replay.onTouchEvent(mock())
1677+
replay.onAppForegrounded(true)
1678+
replay.onTouchEvent(mock())
1679+
replay.stop()
1680+
replay.start()
1681+
replay.onTouchEvent(mock())
1682+
1683+
verify(firstStrategy, never()).onTouchEvent(any())
1684+
verify(secondStrategy, never()).onTouchEvent(any())
1685+
verify(thirdStrategy, never()).onTouchEvent(any())
1686+
}
1687+
16011688
@Test
16021689
fun `when paused manually onConnectionStatusChanged does not resume`() {
16031690
val captureStrategy = getSessionCaptureStrategy(fixture.options)

‎sentry/api/sentry.api‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,9 @@ public abstract interface class io/sentry/IReplayApi {
988988
public abstract fun enableDebugMaskingOverlay ()V
989989
public abstract fun flush ()V
990990
public abstract fun pause ()V
991+
public abstract fun pauseTouchTracking ()V
991992
public abstract fun resume ()V
993+
public abstract fun resumeTouchTracking ()V
992994
public abstract fun start ()V
993995
public abstract fun startBuffering ()V
994996
public abstract fun stop ()V
@@ -1842,9 +1844,11 @@ public final class io/sentry/NoOpReplayController : io/sentry/ReplayController {
18421844
public fun onAppForegrounded (Z)V
18431845
public fun onAppSessionEnded ()V
18441846
public fun pause ()V
1847+
public fun pauseTouchTracking ()V
18451848
public fun registerSegmentName (Ljava/lang/String;)V
18461849
public fun registerTraceId (Lio/sentry/protocol/SentryId;)V
18471850
public fun resume ()V
1851+
public fun resumeTouchTracking ()V
18481852
public fun setBreadcrumbConverter (Lio/sentry/ReplayBreadcrumbConverter;)V
18491853
public fun start ()V
18501854
public fun startBuffering ()V

‎sentry/src/main/java/io/sentry/IReplayApi.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ public interface IReplayApi {
3232
/** Resumes a replay paused with {@link #pause()}. */
3333
void resume();
3434

35+
/**
36+
* Stops recording touch events until {@link #resumeTouchTracking()} is called. Screenshots are
37+
* still recorded. This can be used on sensitive screens, such as a custom PIN pad, where the
38+
* screen is masked but the touch positions could reveal the input.
39+
*
40+
* <p>Touch tracking stays paused for all replays in the same process, also if the replay is
41+
* stopped and started again. {@link #resume()} does not resume touch tracking. Has effect
42+
* immediately, also before a replay is started.
43+
*/
44+
void pauseTouchTracking();
45+
46+
/**
47+
* Resumes touch tracking paused with {@link #pauseTouchTracking()}. Touch events are recorded
48+
* again only while the replay is not paused with {@link #pause()}.
49+
*/
50+
void resumeTouchTracking();
51+
3552
/**
3653
* Immediately sends the current replay data to Sentry in either session or buffer mode. A
3754
* buffering replay continues in session mode after the buffer is sent. If replay is not

‎sentry/src/main/java/io/sentry/NoOpReplayController.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public void pause() {}
3131
@Override
3232
public void resume() {}
3333

34+
@Override
35+
public void pauseTouchTracking() {}
36+
37+
@Override
38+
public void resumeTouchTracking() {}
39+
3440
@Override
3541
public void flush() {}
3642

0 commit comments

Comments
 (0)