|
| 1 | +package io.sentry.android.core.internal.util; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.graphics.Bitmap; |
| 5 | +import android.view.View; |
| 6 | +import io.sentry.ILogger; |
| 7 | +import io.sentry.SentryLevel; |
| 8 | +import io.sentry.android.core.SentryScreenshotOptions; |
| 9 | +import io.sentry.android.replay.util.MaskRenderer; |
| 10 | +import io.sentry.android.replay.util.ViewsKt; |
| 11 | +import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode; |
| 12 | +import io.sentry.util.ExceptionUtils; |
| 13 | +import io.sentry.util.LoadClass; |
| 14 | +import io.sentry.util.thread.IThreadChecker; |
| 15 | +import java.util.concurrent.CountDownLatch; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import java.util.concurrent.atomic.AtomicReference; |
| 18 | +import org.jetbrains.annotations.ApiStatus; |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | +import org.jetbrains.annotations.Nullable; |
| 21 | + |
| 22 | +/** |
| 23 | + * Applies screenshot masking using the Session Replay view hierarchy approach. Requires the |
| 24 | + * `sentry-android-replay` module at runtime, callers must check {@link #isAvailable} first. |
| 25 | + */ |
| 26 | +@ApiStatus.Internal |
| 27 | +public final class ScreenshotMasking { |
| 28 | + |
| 29 | + private static final long MASKING_TIMEOUT_MS = 2000; |
| 30 | + |
| 31 | + // Checked instead of the masking classes, since it's the only replay class whose name is kept |
| 32 | + // by the consumer proguard rules |
| 33 | + private static final String REPLAY_INTEGRATION_CLASS_NAME = |
| 34 | + "io.sentry.android.replay.ReplayIntegration"; |
| 35 | + |
| 36 | + private ScreenshotMasking() {} |
| 37 | + |
| 38 | + /** Whether the `sentry-android-replay` module, required for masking, is available at runtime. */ |
| 39 | + public static boolean isAvailable(final @NotNull ILogger logger) { |
| 40 | + return new LoadClass().isClassAvailable(REPLAY_INTEGRATION_CLASS_NAME, logger); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Masks the given screenshot according to the screenshot options. |
| 45 | + * |
| 46 | + * @return the masked bitmap, or null if masking failed. The passed screenshot is recycled on |
| 47 | + * failure, so a raw screenshot is never returned in place of a masked one. |
| 48 | + */ |
| 49 | + public static @Nullable Bitmap mask( |
| 50 | + final @NotNull Activity activity, |
| 51 | + final @NotNull Bitmap screenshot, |
| 52 | + final @NotNull SentryScreenshotOptions screenshotOptions, |
| 53 | + final @NotNull IThreadChecker threadChecker, |
| 54 | + final @NotNull ILogger logger) { |
| 55 | + final @Nullable ViewHierarchyNode rootNode = |
| 56 | + captureViewHierarchy(activity, screenshotOptions, threadChecker, logger); |
| 57 | + if (rootNode == null) { |
| 58 | + screenshot.recycle(); |
| 59 | + return null; |
| 60 | + } |
| 61 | + // applyMasking already recycles its bitmaps on failure |
| 62 | + return applyMasking(screenshot, rootNode, logger); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Captures the view hierarchy on the main thread, since view traversal requires it. If already on |
| 67 | + * the main thread, captures directly; otherwise posts to the main thread and waits. |
| 68 | + */ |
| 69 | + private static @Nullable ViewHierarchyNode captureViewHierarchy( |
| 70 | + final @NotNull Activity activity, |
| 71 | + final @NotNull SentryScreenshotOptions screenshotOptions, |
| 72 | + final @NotNull IThreadChecker threadChecker, |
| 73 | + final @NotNull ILogger logger) { |
| 74 | + if (threadChecker.isMainThread()) { |
| 75 | + return buildViewHierarchy(activity, screenshotOptions, logger); |
| 76 | + } |
| 77 | + |
| 78 | + final AtomicReference<ViewHierarchyNode> result = new AtomicReference<>(null); |
| 79 | + final CountDownLatch latch = new CountDownLatch(1); |
| 80 | + |
| 81 | + try { |
| 82 | + activity.runOnUiThread( |
| 83 | + () -> { |
| 84 | + try { |
| 85 | + result.set(buildViewHierarchy(activity, screenshotOptions, logger)); |
| 86 | + } finally { |
| 87 | + latch.countDown(); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + if (!latch.await(MASKING_TIMEOUT_MS, TimeUnit.MILLISECONDS)) { |
| 92 | + logger.log( |
| 93 | + SentryLevel.WARNING, "Timed out waiting for view hierarchy capture on main thread"); |
| 94 | + return null; |
| 95 | + } |
| 96 | + } catch (InterruptedException e) { |
| 97 | + Thread.currentThread().interrupt(); |
| 98 | + logger.log(SentryLevel.ERROR, "Interrupted while capturing view hierarchy", e); |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + return result.get(); |
| 103 | + } |
| 104 | + |
| 105 | + private static @Nullable ViewHierarchyNode buildViewHierarchy( |
| 106 | + final @NotNull Activity activity, |
| 107 | + final @NotNull SentryScreenshotOptions screenshotOptions, |
| 108 | + final @NotNull ILogger logger) { |
| 109 | + try { |
| 110 | + final @Nullable View rootView = |
| 111 | + activity.getWindow() != null |
| 112 | + && activity.getWindow().peekDecorView() != null |
| 113 | + && activity.getWindow().peekDecorView().getRootView() != null |
| 114 | + ? activity.getWindow().peekDecorView().getRootView() |
| 115 | + : null; |
| 116 | + if (rootView == null) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + final ViewHierarchyNode rootNode = |
| 121 | + ViewHierarchyNode.Companion.fromView(rootView, null, 0, screenshotOptions); |
| 122 | + ViewsKt.traverse(rootView, rootNode, screenshotOptions, logger, null); |
| 123 | + return rootNode; |
| 124 | + } catch (Throwable e) { |
| 125 | + // Traversal calls into arbitrary (custom) View code, which can throw anything |
| 126 | + ExceptionUtils.rethrowIfFatal(e); |
| 127 | + logger.log(SentryLevel.ERROR, "Failed to build view hierarchy", e); |
| 128 | + return null; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private static @Nullable Bitmap applyMasking( |
| 133 | + final @NotNull Bitmap screenshot, |
| 134 | + final @NotNull ViewHierarchyNode rootNode, |
| 135 | + final @NotNull ILogger logger) { |
| 136 | + Bitmap mutableBitmap = screenshot; |
| 137 | + boolean createdCopy = false; |
| 138 | + try (final MaskRenderer maskRenderer = new MaskRenderer()) { |
| 139 | + // Make bitmap mutable if needed |
| 140 | + if (!screenshot.isMutable()) { |
| 141 | + mutableBitmap = screenshot.copy(Bitmap.Config.RGB_565, true); |
| 142 | + if (mutableBitmap == null) { |
| 143 | + screenshot.recycle(); |
| 144 | + return null; |
| 145 | + } |
| 146 | + createdCopy = true; |
| 147 | + } |
| 148 | + |
| 149 | + maskRenderer.renderMasks(mutableBitmap, rootNode, null); |
| 150 | + |
| 151 | + // Recycle original if we created a copy |
| 152 | + if (createdCopy && !screenshot.isRecycled()) { |
| 153 | + screenshot.recycle(); |
| 154 | + } |
| 155 | + |
| 156 | + return mutableBitmap; |
| 157 | + } catch (RuntimeException e) { |
| 158 | + logger.log(SentryLevel.ERROR, "Failed to mask screenshot", e); |
| 159 | + if (createdCopy) { |
| 160 | + if (!mutableBitmap.isRecycled()) { |
| 161 | + mutableBitmap.recycle(); |
| 162 | + } |
| 163 | + } |
| 164 | + if (!screenshot.isRecycled()) { |
| 165 | + screenshot.recycle(); |
| 166 | + } |
| 167 | + return null; |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments