Skip to content
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

fix(screenshots): Run take screenshot on ui thread always #2743

Merged
merged 11 commits into from
Jan 23, 2023
Merged
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Take screenshot runs on UI thread on Android
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved

## 4.13.0

### Fixes
Expand Down
24 changes: 20 additions & 4 deletions android/src/main/java/io/sentry/react/RNSentryModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
Expand All @@ -36,6 +37,7 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;

import io.sentry.Breadcrumb;
import io.sentry.HubAdapter;
Expand All @@ -45,14 +47,14 @@
import io.sentry.SentryEvent;
import io.sentry.SentryLevel;
import io.sentry.UncaughtExceptionHandlerIntegration;
import io.sentry.android.core.AndroidLogger;
import io.sentry.android.core.AnrIntegration;
import io.sentry.android.core.AppStartState;
import io.sentry.android.core.BuildInfoProvider;
import io.sentry.android.core.CurrentActivityHolder;
import io.sentry.android.core.NdkIntegration;
import io.sentry.android.core.ScreenshotEventProcessor;
import io.sentry.android.core.SentryAndroid;
import io.sentry.android.core.AndroidLogger;
import io.sentry.protocol.SdkVersion;
import io.sentry.protocol.SentryException;
import io.sentry.protocol.SentryPackage;
Expand Down Expand Up @@ -343,15 +345,29 @@ public void captureScreenshot(Promise promise) {
return;
}

final byte[] raw = takeScreenshot(activity, logger, buildInfo);
if (raw == null) {
CountDownLatch doneSignal = new CountDownLatch(1);
final byte[][] bytesWrapper = {{}};
UiThreadUtil.runOnUiThread(() -> {
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved
bytesWrapper[0] = takeScreenshot(activity, logger, buildInfo);
doneSignal.countDown();
});

try {
doneSignal.await();
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved
} catch (InterruptedException e) {
logger.log(SentryLevel.ERROR, "Screenshot process was interrupted.");
promise.resolve(null);
return;
}

if (bytesWrapper[0] == null) {
logger.log(SentryLevel.WARNING, "Screenshot is null, screen was not captured.");
promise.resolve(null);
return;
}

final WritableNativeArray data = new WritableNativeArray();
for (final byte b : raw) {
for (final byte b : bytesWrapper[0]) {
data.pushInt(b);
}
final WritableMap screenshot = new WritableNativeMap();
Expand Down