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

chore: change screenshot image type from JPEG to WEBP #211

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- chore: change screenshot image type from JPEG to WEBP ([#211](https://github.com/PostHog/posthog-android/pull/211))

## 3.9.3 - 2024-11-26

- no user facing changes
Expand Down
2 changes: 2 additions & 0 deletions posthog-android/api/posthog-android.api
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public final class com/posthog/android/internal/PostHogAndroidUtilsKt {
public static final fun base64 (Landroid/graphics/Bitmap;Landroid/graphics/Bitmap$CompressFormat;I)Ljava/lang/String;
public static synthetic fun base64$default (Landroid/graphics/Bitmap;Landroid/graphics/Bitmap$CompressFormat;IILjava/lang/Object;)Ljava/lang/String;
public static final fun getApplicationInfo (Landroid/content/Context;)Landroid/content/pm/ApplicationInfo;
public static final fun webpBase64 (Landroid/graphics/Bitmap;I)Ljava/lang/String;
public static synthetic fun webpBase64$default (Landroid/graphics/Bitmap;IILjava/lang/Object;)Ljava/lang/String;
}

public abstract interface class com/posthog/android/replay/PostHogDrawableConverter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ private fun Bitmap.isValid(): Boolean {
height > 0
}

@PostHogInternal
@Suppress("DEPRECATION")
public fun Bitmap.webpBase64(quality: Int = 30): String? {
val format =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Bitmap.CompressFormat.WEBP_LOSSY
} else {
Bitmap.CompressFormat.WEBP
}

return base64(format, quality)
}

@PostHogInternal
@Suppress("DEPRECATION")
public fun Bitmap.base64(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ import com.posthog.PostHog
import com.posthog.PostHogIntegration
import com.posthog.android.PostHogAndroidConfig
import com.posthog.android.internal.MainHandler
import com.posthog.android.internal.base64
import com.posthog.android.internal.densityValue
import com.posthog.android.internal.displayMetrics
import com.posthog.android.internal.screenSize
import com.posthog.android.internal.webpBase64
import com.posthog.android.replay.PostHogMaskModifier.PostHogReplayMask
import com.posthog.android.replay.internal.NextDrawListener.Companion.onNextDraw
import com.posthog.android.replay.internal.ViewTreeSnapshotStatus
Expand Down Expand Up @@ -733,7 +733,7 @@ public class PostHogReplayIntegration(
canvas.drawRoundRect(RectF(it), 10f, 10f, paint)
}

base64 = bitmap.base64()
base64 = bitmap.webpBase64()
}
} catch (e: Throwable) {
config.logger.log("Session Replay PixelCopy failed: $e.")
Expand Down Expand Up @@ -1106,7 +1106,7 @@ public class PostHogReplayIntegration(
): String? {
val convertedBitmap = runDrawableConverter(this)
if (convertedBitmap != null) {
return convertedBitmap.base64()
return convertedBitmap.webpBase64()
}

var clonedDrawable = this
Expand All @@ -1117,7 +1117,7 @@ public class PostHogReplayIntegration(
when (clonedDrawable) {
is BitmapDrawable -> {
try {
return clonedDrawable.bitmap.base64()
return clonedDrawable.bitmap.webpBase64()
} catch (_: Throwable) {
// ignore
}
Expand All @@ -1138,7 +1138,7 @@ public class PostHogReplayIntegration(

try {
val bitmap = clonedDrawable.toBitmap(width, height)
val base64 = bitmap.base64()
val base64 = bitmap.webpBase64()
if (!bitmap.isRecycled) {
bitmap.recycle()
}
Expand Down
Loading