Skip to content

Use PixelCopy API for capturing screenshots on API level 24+ #3008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 31, 2023
Merged
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Use PixelCopy API for capturing screenshots on API level 24+ ([#3008](https://github.com/getsentry/sentry-java/pull/3008))

### Fixes

- Fix crash when HTTP connection error message contains formatting symbols ([#3002](https://github.com/getsentry/sentry-java/pull/3002))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.view.PixelCopy;
import android.view.View;
import android.view.Window;
import androidx.annotation.Nullable;
import io.sentry.ILogger;
import io.sentry.SentryLevel;
Expand All @@ -14,6 +18,7 @@
import java.io.ByteArrayOutputStream;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

Expand All @@ -38,13 +43,14 @@ public class ScreenshotUtils {

if (!isActivityValid(activity, buildInfoProvider)
|| activity.getWindow() == null
|| activity.getWindow().getDecorView() == null
|| activity.getWindow().getDecorView().getRootView() == null) {
|| activity.getWindow().peekDecorView() == null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: I'm wondering if we should rather memoize this into a variable, because the next time we call it, it might've already changed? especially if we are not on the main thread

|| activity.getWindow().peekDecorView().getRootView() == null) {
logger.log(SentryLevel.DEBUG, "Activity isn't valid, not taking screenshot.");
return null;
}

final View view = activity.getWindow().getDecorView().getRootView();
final Window window = activity.getWindow();
final View view = window.peekDecorView().getRootView();
if (view.getWidth() <= 0 || view.getHeight() <= 0) {
logger.log(SentryLevel.DEBUG, "View's width and height is zeroed, not taking screenshot.");
return null;
Expand All @@ -55,20 +61,57 @@ public class ScreenshotUtils {
final Bitmap bitmap =
Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);

final Canvas canvas = new Canvas(bitmap);
if (mainThreadChecker.isMainThread()) {
view.draw(canvas);
} else {
final @NotNull CountDownLatch latch = new CountDownLatch(1);
activity.runOnUiThread(
() -> {
try {
view.draw(canvas);
final @NotNull CountDownLatch latch = new CountDownLatch(1);

// Use Pixel Copy API on new devices, fallback to canvas rendering on older ones
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: We could use BuildInfoProvider here and test it. You could take a look at SentryGestureListenerClickTest where we completely mock windows and decor views, and there's also ShadowPixelCopy of robolectric that would be helpful

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely forget about that, I've added some test using ShadowPixelCopy


final HandlerThread thread = new HandlerThread("screenshot");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final HandlerThread thread = new HandlerThread("screenshot");
final HandlerThread thread = new HandlerThread("SentryScreenshot");

to make it more specific to our sdk

thread.start();

boolean success = false;
try {
final Handler handler = new Handler(thread.getLooper());
final AtomicBoolean copyResultSuccess = new AtomicBoolean(false);

PixelCopy.request(
window,
bitmap,
copyResult -> {
copyResultSuccess.set(copyResult == PixelCopy.SUCCESS);
latch.countDown();
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Taking screenshot failed (view.draw).", e);
}
});
},
handler);

success =
latch.await(CAPTURE_TIMEOUT_MS, TimeUnit.MILLISECONDS) && copyResultSuccess.get();
} catch (InterruptedException e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch (InterruptedException e) {
} catch (Throwable e) {

I'd catch any throwable just to make sure

// ignored
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// ignored
logger.log(SentryLevel.ERROR, "Taking screenshot failed (view.draw).", e);

} finally {
thread.quit();
}

if (!success) {
return null;
}
} else {
final Canvas canvas = new Canvas(bitmap);
if (mainThreadChecker.isMainThread()) {
view.draw(canvas);
latch.countDown();
} else {
activity.runOnUiThread(
() -> {
try {
view.draw(canvas);
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Taking screenshot failed (view.draw).", e);
} finally {
latch.countDown();
}
});
}

if (!latch.await(CAPTURE_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ScreenshotEventProcessorTest {
whenever(rootView.height).thenReturn(1)
whenever(view.rootView).thenReturn(rootView)
whenever(window.decorView).thenReturn(view)
whenever(window.peekDecorView()).thenReturn(view)
whenever(activity.window).thenReturn(window)
whenever(activity.runOnUiThread(any())).then {
it.getArgument<Runnable>(0).run()
Expand Down