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.
feat(storage): implement saveScreenshot method for improved screenshot handling
Durganshu Mishraf05ef1d920ed
diff --git a/app/src/main/java/com/gotcha/data/GotchaStorage.kt b/app/src/main/java/com/gotcha/data/GotchaStorage.ktindex cea502f..642c871 100644--- a/app/src/main/java/com/gotcha/data/GotchaStorage.kt+++ b/app/src/main/java/com/gotcha/data/GotchaStorage.kt@@ -1,9 +1,14 @@package com.gotcha.data+import android.content.ContentValuesimport android.content.Context+import android.graphics.Bitmapimport android.media.MediaScannerConnection+import android.os.Buildimport android.os.Environment+import android.provider.MediaStoreimport java.io.File+import java.io.IOException/*** Single source of truth for every path Gotcha reads or writes under shared@@ -107,4 +112,38 @@ object GotchaStorage {fun publishToGallery(context: Context, file: File) {MediaScannerConnection.scanFile(context, arrayOf(file.absolutePath), null, null)}++ /**+ * Saves [bitmap] as a PNG named [fileName] under [screenshotsRoot], returning+ * the human-readable location it landed in.+ *+ * Creating anything under the Gotcha root needs "All files access"+ * (MANAGE_EXTERNAL_STORAGE) on API 30+; without it `mkdirs()` fails and the+ * open would throw ENOENT. Since screenshot capture is a standalone feature+ * that shouldn't depend on that grant, fall back to a MediaStore insert into+ * `Pictures/Gotcha`, which needs no permission at all.+ */+ fun saveScreenshot(context: Context, fileName: String, bitmap: Bitmap): String {+ val dir = screenshotsRoot()+ if (dir.mkdirs() || dir.isDirectory) {+ val file = File(dir, fileName)+ file.outputStream().use { out -> bitmap.compress(Bitmap.CompressFormat.PNG, 100, out) }+ publishToGallery(context, file)+ return "Gotcha/Screenshots"+ }+ val values = ContentValues().apply {+ put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)+ put(MediaStore.MediaColumns.MIME_TYPE, "image/png")+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {+ put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/Gotcha")+ }+ }+ val resolver = context.contentResolver+ val uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)+ ?: throw IOException("Could not create $fileName in Pictures/Gotcha")+ resolver.openOutputStream(uri)?.use { out ->+ bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)+ } ?: throw IOException("Could not open Pictures/Gotcha for writing")+ return "Pictures/Gotcha"+ }}diff --git a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt b/app/src/main/java/com/gotcha/service/AssistiveBallService.ktindex 46532b9..e6092eb 100644--- a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt+++ b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt@@ -762,14 +762,12 @@ class AssistiveBallService : Service() {java.util.Locale.US).format(java.util.Date())val fileName = "Screenshot_$timestamp.png"- val dir = com.gotcha.data.GotchaStorage.screenshotsRoot()- dir.mkdirs()- val file = java.io.File(dir, fileName)- file.outputStream().use { out ->- bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, out)- }- com.gotcha.data.GotchaStorage.publishToGallery(applicationContext, file)- withContext(Dispatchers.Main) { overlay.showError("Screenshot saved: $fileName") }+ val location = com.gotcha.data.GotchaStorage.saveScreenshot(+ applicationContext,+ fileName,+ bitmap+ )+ withContext(Dispatchers.Main) { overlay.showError("Screenshot saved to $location") }bitmap.recycle()} catch (e: Throwable) {withContext(Dispatchers.Main) {diff --git a/app/src/main/java/com/gotcha/service/ScreenLensController.kt b/app/src/main/java/com/gotcha/service/ScreenLensController.ktindex 0ea43f6..4cdbbcd 100644--- a/app/src/main/java/com/gotcha/service/ScreenLensController.kt+++ b/app/src/main/java/com/gotcha/service/ScreenLensController.kt@@ -489,17 +489,11 @@ class ScreenLensController(scope.launch(Dispatchers.IO) {try {val filename = "Gotcha_Lens_${System.currentTimeMillis()}.png"- val dir = com.gotcha.data.GotchaStorage.screenshotsRoot()- dir.mkdirs()- val file = java.io.File(dir, filename)- file.outputStream().use { out ->- bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)- }- com.gotcha.data.GotchaStorage.publishToGallery(appContext, file)+ val location = com.gotcha.data.GotchaStorage.saveScreenshot(appContext, filename, bitmap)withContext(Dispatchers.Main) {android.widget.Toast.makeText(appContext,- "Saved image to Gotcha/Screenshots",+ "Saved image to $location",android.widget.Toast.LENGTH_SHORT).show()}diff --git a/app/src/test/java/com/gotcha/data/GotchaStorageTest.kt b/app/src/test/java/com/gotcha/data/GotchaStorageTest.ktindex daa0af9..03464fb 100644--- a/app/src/test/java/com/gotcha/data/GotchaStorageTest.kt+++ b/app/src/test/java/com/gotcha/data/GotchaStorageTest.kt@@ -161,9 +161,31 @@ class GotchaStorageTest {assertTrue(entries.any { it.name.startsWith("${dir.name}_") })}+ // ---- screenshotsRoot writability (the ENOENT regression) ----++ @Test+ fun `screenshotsRoot is creatable when the Gotcha root is writable`() {+ val dir = GotchaStorage.screenshotsRoot()+ assertFalse(dir.exists())+ assertTrue(dir.mkdirs() || dir.isDirectory)+ assertTrue(dir.isDirectory)+ }++ @Test+ fun `screenshotsRoot reports not-creatable when the root cannot be created`() {+ // Simulate the no-All-Files-Access case: the root's parent is a *file*,+ // so mkdirs() fails exactly as it does when the grant is missing. This is+ // the condition saveScreenshot uses to pick the MediaStore fallback.+ val blocker = tmp.newFile("blocked")+ GotchaStorage.rootPath = File(blocker, "Gotcha").absolutePath+ val dir = GotchaStorage.screenshotsRoot()+ assertFalse(dir.mkdirs() || dir.isDirectory)+ }+@Testfun `archiveChatDir is a no-op when the chat dir does not exist`() {GotchaStorage.archiveChatDir("a3f9c1d2-1234-5678-9abc-def012345678")- assertFalse(GotchaStorage.archiveRoot().exists() && GotchaStorage.archiveRoot().listFiles()?.isNotEmpty() == true)+ val archived = GotchaStorage.archiveRoot().listFiles().orEmpty()+ assertTrue(archived.isEmpty())}}