Skip to content

CircleToSearch with multi search engine support for all android

Kotlin

circle to search ui improvements++

AKS Labsb228dc218bb4
diff --git a/app/src/main/java/com/akslabs/circletosearch/CircleToSearchService.kt b/app/src/main/java/com/akslabs/circletosearch/CircleToSearchService.kt
index 0747ca9..e83612b 100644
--- a/app/src/main/java/com/akslabs/circletosearch/CircleToSearchService.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/CircleToSearchService.kt
@@ -115,16 +115,6 @@ class CircleToSearchService : AccessibilityService() {
triggerView?.setOnTouchListener { _, event ->
gestureDetector.onTouchEvent(event)
- // Pass touch through so we don't block status bar interactions completely
- // But we need to return true to consume events for gesture detector to work?
- // Actually, if we return false, the gesture detector might not see the full sequence.
- // But if we return true, we block clicks.
- // For a transparent overlay, usually we want to pass through.
- // However, GestureDetector needs the stream.
- // A common trick is to use FLAG_NOT_TOUCH_MODAL and let touches pass through *if* we don't consume them?
- // But we are just an overlay.
- // Let's stick to the previous logic which was 'true' but maybe refine it if user complains about blocked status bar.
- // For now, keep it simple.
true
}
@@ -137,56 +127,54 @@ class CircleToSearchService : AccessibilityService() {
private fun performCapture() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
- // Add a slight delay to ensure UI is stable/ripple finished
- android.os.Handler(android.os.Looper.getMainLooper()).postDelayed({
- takeScreenshot(
- Display.DEFAULT_DISPLAY,
- executor,
- object : TakeScreenshotCallback {
- override fun onSuccess(screenshot: ScreenshotResult) {
- try {
- val hardwareBuffer = screenshot.hardwareBuffer
- val colorSpace = screenshot.colorSpace
-
- android.util.Log.d("CircleToSearch", "Screenshot captured. Size: ${hardwareBuffer.width}x${hardwareBuffer.height}")
-
- val bitmap = Bitmap.wrapHardwareBuffer(hardwareBuffer, colorSpace)
- if (bitmap == null) {
- android.util.Log.e("CircleToSearch", "Failed to wrap hardware buffer")
- hardwareBuffer.close()
- return
- }
-
- // Copy to software bitmap
- val copy = bitmap.copy(Bitmap.Config.ARGB_8888, false)
- hardwareBuffer.close() // Close buffer after copy
-
- if (copy == null) {
- android.util.Log.e("CircleToSearch", "Failed to copy bitmap")
- return
- }
-
- // Check center pixel
- val centerX = copy.width / 2
- val centerY = copy.height / 2
- val pixel = copy.getPixel(centerX, centerY)
- android.util.Log.d("CircleToSearch", "Center pixel color: ${Integer.toHexString(pixel)}")
-
- val path = ImageUtils.saveBitmap(this@CircleToSearchService, copy)
- android.util.Log.d("CircleToSearch", "Screenshot saved to $path")
- launchOverlay(path)
-
- } catch (e: Exception) {
- android.util.Log.e("CircleToSearch", "Error processing screenshot", e)
+ // Execute immediately for instant trigger
+ takeScreenshot(
+ Display.DEFAULT_DISPLAY,
+ executor,
+ object : TakeScreenshotCallback {
+ override fun onSuccess(screenshot: ScreenshotResult) {
+ try {
+ val hardwareBuffer = screenshot.hardwareBuffer
+ val colorSpace = screenshot.colorSpace
+
+ android.util.Log.d("CircleToSearch", "Screenshot captured. Size: ${hardwareBuffer.width}x${hardwareBuffer.height}")
+
+ val bitmap = Bitmap.wrapHardwareBuffer(hardwareBuffer, colorSpace)
+ if (bitmap == null) {
+ android.util.Log.e("CircleToSearch", "Failed to wrap hardware buffer")
+ hardwareBuffer.close()
+ return
}
- }
- override fun onFailure(errorCode: Int) {
- android.util.Log.e("CircleToSearch", "Screenshot failed with error code: $errorCode")
+ // Copy to software bitmap
+ val copy = bitmap.copy(Bitmap.Config.ARGB_8888, false)
+ hardwareBuffer.close() // Close buffer after copy
+
+ if (copy == null) {
+ android.util.Log.e("CircleToSearch", "Failed to copy bitmap")
+ return
+ }
+
+ // Check center pixel
+ val centerX = copy.width / 2
+ val centerY = copy.height / 2
+ val pixel = copy.getPixel(centerX, centerY)
+ android.util.Log.d("CircleToSearch", "Center pixel color: ${Integer.toHexString(pixel)}")
+
+ val path = ImageUtils.saveBitmap(this@CircleToSearchService, copy)
+ android.util.Log.d("CircleToSearch", "Screenshot saved to $path")
+ launchOverlay(path)
+
+ } catch (e: Exception) {
+ android.util.Log.e("CircleToSearch", "Error processing screenshot", e)
}
}
- )
- }, 500)
+
+ override fun onFailure(errorCode: Int) {
+ android.util.Log.e("CircleToSearch", "Screenshot failed with error code: $errorCode")
+ }
+ }
+ )
}
}
diff --git a/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt b/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt
index 6eee054..ade1e58 100644
--- a/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt
@@ -2,6 +2,7 @@ package com.akslabs.circletosearch.ui
import android.graphics.Bitmap
import android.graphics.Rect
+import android.util.Base64
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.animation.core.animateFloatAsState
@@ -72,6 +73,7 @@ import com.akslabs.circletosearch.data.SearchEngine
import com.akslabs.circletosearch.ui.theme.OverlayGradientColors
import com.akslabs.circletosearch.utils.ImageUtils
import kotlinx.coroutines.launch
+import java.io.ByteArrayOutputStream
import kotlin.math.max
import kotlin.math.min
@@ -161,23 +163,44 @@ fun CircleToSearchScreen(
WebView(ctx).apply {
webViewClient = WebViewClient()
settings.javaScriptEnabled = true
+ settings.domStorageEnabled = true
}
},
update = { view ->
- val url = selectedEngine.queryUrl
- if (view.url != url) {
- view.loadUrl(url)
+ if (selectedEngine == SearchEngine.Google && selectedBitmap != null) {
+ // Convert bitmap to base64
+ val byteArrayOutputStream = ByteArrayOutputStream()
+ selectedBitmap!!.compress(Bitmap.CompressFormat.JPEG, 70, byteArrayOutputStream)
+ val byteArray = byteArrayOutputStream.toByteArray()
+ val base64Image = Base64.encodeToString(byteArray, Base64.DEFAULT)
+
+ // Construct HTML form to auto-submit image to Google
+ val html = """
+ <html>
+ <body onload="document.getElementById('f').submit()">
+ <form id="f" method="POST" action="https://www.google.com/searchbyimage/upload" enctype="multipart/form-data">
+ <input type="hidden" name="encoded_image" value="$base64Image">
+ </form>
+ </body>
+ </html>
+ """.trimIndent()
+
+ // Only load if content changed to avoid loop (simple check)
+ // In a real app we'd use a more robust state check
+ if (view.tag != selectedBitmap.hashCode()) {
+ view.loadData(html, "text/html", "UTF-8")
+ view.tag = selectedBitmap.hashCode()
+ }
+ } else {
+ val url = selectedEngine.queryUrl
+ if (view.url != url && view.tag != url) {
+ view.loadUrl(url)
+ view.tag = url
+ }
}
},
modifier = Modifier.fillMaxSize()
)
-
- // Trigger load on selection change
- LaunchedEffect(selectedEngine, selectionRect) {
- // This effect ensures that if the selection changes (new search), we might want to reload
- // even if the URL string is the same (e.g. to refresh the "demo" page).
- // However, AndroidView update block handles the main URL load.
- }
}
}
) { paddingValues ->
@@ -316,7 +339,7 @@ fun CircleToSearchScreen(
val width = right - left
val height = bottom - top
- val cornerRadius = 32f // Radius for the corner curve
+ val cornerRadius = 64f // Increased radius for rounder look
val armLength = min(width, height) * 0.2f // Length of the straight part
// Top Left
@@ -382,7 +405,7 @@ fun CircleToSearchScreen(
color = Color.White,
topLeft = Offset(left, top),
size = Size(width, height),
- cornerRadius = CornerRadius(16f),
+ cornerRadius = CornerRadius(32f),
style = Stroke(width = 4f),
alpha = (1f - progress) * 0.5f
)