Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

refactor: centralize bitmap compression logic in ScreenPerception and update usages across services and view models.

Rishabh Bajpaie96bf22069b2
diff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
index aaca200..d453368 100644
--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
@@ -36,7 +36,6 @@ import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonPrimitive
-import java.io.ByteArrayOutputStream
@kotlinx.serialization.Serializable
data class UiMessage(
@@ -326,7 +325,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
return
}
val msg = if (imageBase64 != null) {
- visionUserMessage(trimmed, imageBase64)
+ visionUserMessage(trimmed, imageBase64, "jpeg")
} else {
ChatMessage(role = "user", content = JsonPrimitive(trimmed))
}
@@ -405,20 +404,13 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) ?: return null
- val (w, h) = if (bitmap.width > maxDimension || bitmap.height > maxDimension) {
- val ratio = minOf(maxDimension.toFloat() / bitmap.width, maxDimension.toFloat() / bitmap.height)
- (bitmap.width * ratio).toInt() to (bitmap.height * ratio).toInt()
- } else {
- bitmap.width to bitmap.height
- }
-
- val scaled = Bitmap.createScaledBitmap(bitmap, w, h, true)
- if (scaled != bitmap) bitmap.recycle()
-
- val output = ByteArrayOutputStream()
- scaled.compress(android.graphics.Bitmap.CompressFormat.JPEG, 85, output)
- scaled.recycle()
- android.util.Base64.encodeToString(output.toByteArray(), android.util.Base64.NO_WRAP)
+ com.gotcha.tools.ScreenPerception.compressBitmap(
+ bitmap = bitmap,
+ maxDimension = maxDimension,
+ quality = 85,
+ format = Bitmap.CompressFormat.JPEG,
+ recycleInput = true
+ )
} catch (_: Exception) {
null
}
diff --git a/app/src/main/java/com/gotcha/agent/ScreenSnapshot.kt b/app/src/main/java/com/gotcha/agent/ScreenSnapshot.kt
index 980cdcc..3799aa1 100644
--- a/app/src/main/java/com/gotcha/agent/ScreenSnapshot.kt
+++ b/app/src/main/java/com/gotcha/agent/ScreenSnapshot.kt
@@ -6,7 +6,6 @@ import com.gotcha.service.GotchaAccessibilityService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
-import java.io.ByteArrayOutputStream
/**
* Accessibility-based screen capture for overlay features (voice calls).
@@ -41,25 +40,13 @@ object ScreenSnapshot {
}
if (bitmap == null) return null
return withContext(Dispatchers.Default) {
- try {
- val (w, h) = if (bitmap.width > MAX_DIMENSION || bitmap.height > MAX_DIMENSION) {
- val ratio = minOf(
- MAX_DIMENSION.toFloat() / bitmap.width,
- MAX_DIMENSION.toFloat() / bitmap.height
- )
- (bitmap.width * ratio).toInt() to (bitmap.height * ratio).toInt()
- } else {
- bitmap.width to bitmap.height
- }
- val scaled = Bitmap.createScaledBitmap(bitmap, w, h, true)
- if (scaled != bitmap) bitmap.recycle()
- val output = ByteArrayOutputStream()
- scaled.compress(Bitmap.CompressFormat.JPEG, JPEG_QUALITY, output)
- scaled.recycle()
- android.util.Base64.encodeToString(output.toByteArray(), android.util.Base64.NO_WRAP)
- } catch (_: Exception) {
- null
- }
+ com.gotcha.tools.ScreenPerception.compressBitmap(
+ bitmap = bitmap,
+ maxDimension = MAX_DIMENSION,
+ quality = JPEG_QUALITY,
+ format = Bitmap.CompressFormat.JPEG,
+ recycleInput = true
+ )
}
}
diff --git a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
index 1b5f961..216b5f4 100644
--- a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
+++ b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
@@ -180,34 +180,14 @@ class AssistiveBallService : Service() {
overlay.isPanelOpen = true
scope.launch {
try {
- var base64: String? = null
- if (attachScreenshot && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
- val service = com.gotcha.service.GotchaAccessibilityService.instance
- if (service != null) {
- var bitmap = service.takeScreenshotBitmap()
- if (bitmap == null) {
- kotlinx.coroutines.delay(500)
- bitmap = service.takeScreenshotBitmap()
- }
- if (bitmap != null) {
- val (w, h) = if (bitmap.width > 1024 || bitmap.height > 1024) {
- val ratio = minOf(1024f / bitmap.width, 1024f / bitmap.height)
- (bitmap.width * ratio).toInt() to (bitmap.height * ratio).toInt()
- } else {
- bitmap.width to bitmap.height
- }
- val scaled = android.graphics.Bitmap.createScaledBitmap(bitmap, w, h, true)
- if (scaled != bitmap) bitmap.recycle()
- val output = java.io.ByteArrayOutputStream()
- scaled.compress(android.graphics.Bitmap.CompressFormat.JPEG, 85, output)
- scaled.recycle()
- base64 = android.util.Base64.encodeToString(output.toByteArray(), android.util.Base64.NO_WRAP)
- }
- }
+ val compressed = if (attachScreenshot) {
+ com.gotcha.tools.ScreenPerception.compressScreenshot(maxDimension = 1024, quality = 85)
+ } else {
+ null
}
- val userMsg = if (base64 != null) {
- com.gotcha.llm.visionUserMessage(prompt, base64)
+ val userMsg = if (compressed != null) {
+ com.gotcha.llm.visionUserMessage(prompt, compressed.base64, compressed.format)
} else {
com.gotcha.llm.ChatMessage("user", kotlinx.serialization.json.JsonPrimitive(prompt))
}
diff --git a/app/src/main/java/com/gotcha/service/ScreenCompanionController.kt b/app/src/main/java/com/gotcha/service/ScreenCompanionController.kt
index f336d3b..73a48d1 100644
--- a/app/src/main/java/com/gotcha/service/ScreenCompanionController.kt
+++ b/app/src/main/java/com/gotcha/service/ScreenCompanionController.kt
@@ -63,11 +63,12 @@ class ScreenCompanionController(
addAction(ACTION_APP_CHANGED)
addAction(ACTION_CLIPBOARD_CHANGED)
}
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
- context.registerReceiver(appChangeReceiver, filter, Context.RECEIVER_NOT_EXPORTED)
- } else {
- context.registerReceiver(appChangeReceiver, filter)
- }
+ androidx.core.content.ContextCompat.registerReceiver(
+ context,
+ appChangeReceiver,
+ filter,
+ androidx.core.content.ContextCompat.RECEIVER_NOT_EXPORTED
+ )
}
fun stop() {
diff --git a/app/src/main/java/com/gotcha/service/ScreenLensController.kt b/app/src/main/java/com/gotcha/service/ScreenLensController.kt
index 94a6851..74926e0 100644
--- a/app/src/main/java/com/gotcha/service/ScreenLensController.kt
+++ b/app/src/main/java/com/gotcha/service/ScreenLensController.kt
@@ -120,6 +120,7 @@ class ScreenLensController(
}
}
+ @SuppressLint("NewApi")
private fun onRegionSelected(rectInView: Rect) {
val overlay = cropOverlay as? ScreenCropOverlayView
val viewW = overlay?.width ?: 0
@@ -562,22 +563,13 @@ class ScreenLensController(
}
private fun encodeJpeg(bitmap: Bitmap): String? {
- return try {
- val (w, h) = if (bitmap.width > 1024 || bitmap.height > 1024) {
- val ratio = minOf(1024f / bitmap.width, 1024f / bitmap.height)
- (bitmap.width * ratio).toInt().coerceAtLeast(1) to
- (bitmap.height * ratio).toInt().coerceAtLeast(1)
- } else {
- bitmap.width to bitmap.height
- }
- val scaled = Bitmap.createScaledBitmap(bitmap, w, h, true)
- val output = java.io.ByteArrayOutputStream()
- scaled.compress(Bitmap.CompressFormat.JPEG, 85, output)
- if (scaled != bitmap) scaled.recycle()
- android.util.Base64.encodeToString(output.toByteArray(), android.util.Base64.NO_WRAP)
- } catch (_: Exception) {
- null
- }
+ return com.gotcha.tools.ScreenPerception.compressBitmap(
+ bitmap = bitmap,
+ maxDimension = 1024,
+ quality = 85,
+ format = Bitmap.CompressFormat.JPEG,
+ recycleInput = false
+ )
}
private fun recyclePendingCrop() {
diff --git a/app/src/main/java/com/gotcha/tools/ScreenPerception.kt b/app/src/main/java/com/gotcha/tools/ScreenPerception.kt
index aeb7582..5c3ea1d 100644
--- a/app/src/main/java/com/gotcha/tools/ScreenPerception.kt
+++ b/app/src/main/java/com/gotcha/tools/ScreenPerception.kt
@@ -368,6 +368,32 @@ object ScreenPerception {
}
}
+ fun compressBitmap(
+ bitmap: Bitmap,
+ maxDimension: Int = 1024,
+ quality: Int = 85,
+ format: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG,
+ recycleInput: Boolean = false
+ ): String? {
+ return try {
+ val forEncoding = if (maxDimension > 0) downscale(bitmap, maxDimension) else bitmap
+ val output = ByteArrayOutputStream()
+ forEncoding.compress(format, quality, output)
+ if (forEncoding !== bitmap) {
+ forEncoding.recycle()
+ }
+ if (recycleInput) {
+ bitmap.recycle()
+ }
+ Base64.encodeToString(output.toByteArray(), Base64.NO_WRAP)
+ } catch (e: kotlinx.coroutines.CancellationException) {
+ throw e
+ } catch (e: Throwable) {
+ Log.e("ScreenCapture", "compressBitmap failed: ${e.message}", e)
+ null
+ }
+ }
+
private fun downscale(bitmap: Bitmap, maxDim: Int): Bitmap {
val (w, h) = if (bitmap.width > maxDim || bitmap.height > maxDim) {
val ratio = minOf(maxDim.toFloat() / bitmap.width, maxDim.toFloat() / bitmap.height)