-
-
Notifications
You must be signed in to change notification settings - Fork 465
Make possible screenshots on hybrid sdks (react-native) #2360
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
Changes from 26 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
fc20d26
Fix do not add screenshots if should not apply scope data, make setCu…
krystofwoldrich d9a2e69
Format code
getsentry-bot 6615b75
Expose ScreenshotEventProcessor to allow HybridSDKs to use it
krystofwoldrich 0dddd51
Merge remote-tracking branch 'origin/feat-screenshots-for-hybrid-sdks…
krystofwoldrich d7b8109
Revert gradle.properties
krystofwoldrich 33eb7e2
Format code
getsentry-bot 98459b9
Return correctly when invalid activity
krystofwoldrich ba66365
Update api
krystofwoldrich 465bbf3
Extract screenshot logic to static method, create activity holder sin…
krystofwoldrich 97c3233
Merge branch 'main' into feat-screenshots-for-hybrid-sdks
krystofwoldrich 706dc8e
Format code
getsentry-bot 8c89da7
Refactor
krystofwoldrich 2cce844
Make logger public
krystofwoldrich 66b4e9e
Add configurable tag to android logger
krystofwoldrich fde7981
Format code
getsentry-bot ec05044
Update sentry-android-core/src/main/java/io/sentry/android/core/Andro…
krystofwoldrich 2e19e51
Fix activity holder access, return build info
krystofwoldrich 42e606b
Format code
getsentry-bot 5aee06d
Merge branch 'main' into feat-screenshots-for-hybrid-sdks
krystofwoldrich 0299512
Fix lint
krystofwoldrich 6d57ab3
Merge branch 'main' into feat-screenshots-for-hybrid-sdks
krystofwoldrich 03e03b9
Apply suggestions from code review
krystofwoldrich 47e66be
Small refactor and add more hybrid sdks
krystofwoldrich ce5d2ce
Format code
getsentry-bot 867a810
More refactoring annotations, finals
krystofwoldrich 3e9008d
Add auto close steam
krystofwoldrich ce00db0
Merge branch 'main' into feat-screenshots-for-hybrid-sdks
krystofwoldrich 0ce3f0e
HintUtils add only class annotation internal
krystofwoldrich baa0a64
CurrentActivityHolder make constructor private
krystofwoldrich 8ebcaa2
Add changelog (don't attach screenshots to hybridSDKs)
krystofwoldrich 8b49d10
Format code
getsentry-bot 3bb055f
Update changelog
krystofwoldrich 6a469aa
Revert "Update changelog"
krystofwoldrich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
sentry-android-core/src/main/java/io/sentry/android/core/CurrentActivityHolder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package io.sentry.android.core; | ||
|
|
||
| import android.app.Activity; | ||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
| import java.lang.ref.WeakReference; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| @ApiStatus.Internal | ||
| public class CurrentActivityHolder { | ||
|
|
||
| private static final @NotNull CurrentActivityHolder instance = new CurrentActivityHolder(); | ||
|
|
||
| private @Nullable WeakReference<Activity> currentActivity; | ||
|
|
||
| public static @NonNull CurrentActivityHolder getInstance() { | ||
| return instance; | ||
| } | ||
|
|
||
| public @Nullable Activity getActivity() { | ||
| if (currentActivity != null) { | ||
| return currentActivity.get(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public void setActivity(final @NonNull Activity activity) { | ||
| if (currentActivity != null && currentActivity.get() == activity) { | ||
| return; | ||
| } | ||
|
|
||
| currentActivity = new WeakReference<>(activity); | ||
| } | ||
|
|
||
| public void clearActivity() { | ||
| currentActivity = null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
sentry-android-core/src/main/java/io/sentry/android/core/internal/util/ScreenshotUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package io.sentry.android.core.internal.util; | ||
|
|
||
| import android.annotation.SuppressLint; | ||
| import android.app.Activity; | ||
| import android.graphics.Bitmap; | ||
| import android.graphics.Canvas; | ||
| import android.os.Build; | ||
| import android.view.View; | ||
| import androidx.annotation.Nullable; | ||
| import io.sentry.ILogger; | ||
| import io.sentry.SentryLevel; | ||
| import io.sentry.android.core.BuildInfoProvider; | ||
| import java.io.ByteArrayOutputStream; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| @ApiStatus.Internal | ||
| public class ScreenshotUtils { | ||
| public static @Nullable byte[] takeScreenshot( | ||
| final @NotNull Activity activity, | ||
| final @NotNull ILogger logger, | ||
| final @NotNull BuildInfoProvider buildInfoProvider) { | ||
| if (!isActivityValid(activity, buildInfoProvider) | ||
| || activity.getWindow() == null | ||
| || activity.getWindow().getDecorView() == null | ||
| || activity.getWindow().getDecorView().getRootView() == null) { | ||
| logger.log(SentryLevel.DEBUG, "Activity isn't valid, not taking screenshot."); | ||
| return null; | ||
| } | ||
|
|
||
| final View view = activity.getWindow().getDecorView().getRootView(); | ||
| if (view.getWidth() <= 0 || view.getHeight() <= 0) { | ||
| logger.log(SentryLevel.DEBUG, "View's width and height is zeroed, not taking screenshot."); | ||
| return null; | ||
| } | ||
|
|
||
| try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { | ||
| // ARGB_8888 -> This configuration is very flexible and offers the best quality | ||
| final Bitmap bitmap = | ||
| Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); | ||
|
|
||
| final Canvas canvas = new Canvas(bitmap); | ||
| view.draw(canvas); | ||
|
|
||
| // 0 meaning compress for small size, 100 meaning compress for max quality. | ||
| // Some formats, like PNG which is lossless, will ignore the quality setting. | ||
| bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteArrayOutputStream); | ||
|
|
||
| if (byteArrayOutputStream.size() <= 0) { | ||
| logger.log(SentryLevel.DEBUG, "Screenshot is 0 bytes, not attaching the image."); | ||
| return null; | ||
| } | ||
|
|
||
| // screenshot png is around ~100-150 kb | ||
| return byteArrayOutputStream.toByteArray(); | ||
| } catch (Throwable e) { | ||
| logger.log(SentryLevel.ERROR, "Taking screenshot failed.", e); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @SuppressLint("NewApi") | ||
| private static boolean isActivityValid( | ||
| final @NotNull Activity activity, final @NotNull BuildInfoProvider buildInfoProvider) { | ||
| if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | ||
| return !activity.isFinishing() && !activity.isDestroyed(); | ||
| } else { | ||
| return !activity.isFinishing(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.