Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Nov 26, 2024
1 parent a7b5df0 commit 2c81d1e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.PackageManager.GET_META_DATA
import android.graphics.Bitmap
import android.graphics.Point
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Build
import android.os.Process
import android.telephony.TelephonyManager
import android.util.Base64
import android.util.DisplayMetrics
import android.view.WindowManager
import com.posthog.PostHogInternal
import com.posthog.android.PostHogAndroidConfig
import java.io.ByteArrayOutputStream

@Suppress("DEPRECATION")
internal fun getPackageInfo(
Expand Down Expand Up @@ -205,3 +208,52 @@ public fun getApplicationInfo(context: Context): ApplicationInfo =
.packageManager
.getApplicationInfo(context.packageName, GET_META_DATA)
}

private fun Bitmap.isValid(): Boolean {
return !isRecycled &&
width > 0 &&
height > 0
}

@PostHogInternal
@Suppress("DEPRECATION")
public fun Bitmap.base64(
format: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG,
quality: Int = 30,
): String? {
if (!isValid()) {
return null
}

val lossyFormat =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Bitmap.CompressFormat.WEBP_LOSSY
} else {
Bitmap.CompressFormat.WEBP
}

val losslessFormat =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Bitmap.CompressFormat.WEBP_LOSSLESS
} else {
Bitmap.CompressFormat.WEBP
}

val htmlFormat =
when (format) {
Bitmap.CompressFormat.JPEG -> "jpeg"
Bitmap.CompressFormat.PNG -> "png"
Bitmap.CompressFormat.WEBP -> "webp"
lossyFormat -> "webp"
losslessFormat -> "webp"
else -> "jpeg"
}

ByteArrayOutputStream(allocationByteCount).use {
// we can make format and type configurable
compress(format, quality, it)
val byteArray = it.toByteArray()
val encoded = Base64.encodeToString(byteArray, Base64.DEFAULT) ?: return null
return "data:image/$htmlFormat;base64,$encoded"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import android.os.Build
import android.os.Handler
import android.os.HandlerThread
import android.text.InputType
import android.util.Base64
import android.util.TypedValue
import android.view.Gravity
import android.view.MotionEvent
Expand Down Expand Up @@ -54,6 +53,7 @@ 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
Expand Down Expand Up @@ -83,7 +83,6 @@ import curtains.onDecorViewReady
import curtains.phoneWindow
import curtains.touchEventInterceptors
import curtains.windowAttachCount
import java.io.ByteArrayOutputStream
import java.lang.ref.WeakReference
import java.util.WeakHashMap
import java.util.concurrent.CountDownLatch
Expand Down Expand Up @@ -1100,26 +1099,6 @@ public class PostHogReplayIntegration(
return null
}

private fun Bitmap.isValid(): Boolean {
return !isRecycled &&
width > 0 &&
height > 0
}

private fun Bitmap.base64(): String? {
if (!isValid()) {
return null
}

ByteArrayOutputStream(allocationByteCount).use {
// we can make format and type configurable
compress(Bitmap.CompressFormat.JPEG, 30, it)
val byteArray = it.toByteArray()
val encoded = Base64.encodeToString(byteArray, Base64.DEFAULT) ?: return null
return "data:image/jpeg;base64,$encoded"
}
}

private fun Drawable.base64(
width: Int,
height: Int,
Expand Down

0 comments on commit 2c81d1e

Please sign in to comment.