-
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(screenshots): Enable screenshots for Hybrid SDKs (#2360)
- Loading branch information
Showing
10 changed files
with
192 additions
and
85 deletions.
There are no files selected for viewing
This file contains 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 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 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
41 changes: 41 additions & 0 deletions
41
sentry-android-core/src/main/java/io/sentry/android/core/CurrentActivityHolder.java
This file contains 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,41 @@ | ||
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 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 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 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 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 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 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.