-
-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 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
42 changes: 42 additions & 0 deletions
42
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,42 @@ | ||
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; | ||
|
||
@ApiStatus.Internal | ||
public class CurrentActivityHolder { | ||
|
||
private static @Nullable CurrentActivityHolder instance; | ||
|
||
private @Nullable WeakReference<Activity> currentActivity; | ||
|
||
public static CurrentActivityHolder getInstance() { | ||
if (instance != null) { | ||
return instance; | ||
} | ||
instance = new CurrentActivityHolder(); | ||
return instance; | ||
} | ||
|
||
public @Nullable Activity getActivity() { | ||
if (currentActivity != null) { | ||
return currentActivity.get(); | ||
} | ||
return null; | ||
} | ||
|
||
public void setActivity(@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
20 changes: 20 additions & 0 deletions
20
sentry-android-core/src/main/java/io/sentry/android/core/internal/util/ActivityUtils.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,20 @@ | ||
package io.sentry.android.core.internal.util; | ||
|
||
import android.app.Activity; | ||
import android.os.Build; | ||
import androidx.annotation.Nullable; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
@ApiStatus.Internal | ||
public class ActivityUtils { | ||
public static boolean isActivityValid(@Nullable Activity activity) { | ||
if (activity == null) { | ||
return false; | ||
} | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | ||
return !activity.isFinishing() && !activity.isDestroyed(); | ||
} else { | ||
return !activity.isFinishing(); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
package io.sentry.android.core.internal.util; | ||
|
||
import static io.sentry.android.core.internal.util.ActivityUtils.isActivityValid; | ||
|
||
import android.app.Activity; | ||
import android.graphics.Bitmap; | ||
import android.graphics.Canvas; | ||
import android.view.View; | ||
import androidx.annotation.Nullable; | ||
import io.sentry.ILogger; | ||
import io.sentry.SentryLevel; | ||
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 @Nullable Activity activity, final @NotNull ILogger logger) { | ||
if (activity == null) { | ||
return null; | ||
} | ||
|
||
if (!isActivityValid(activity) | ||
|| 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 { | ||
// 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); | ||
|
||
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
|
||
// 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; | ||
} | ||
} |
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
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.