Skip to content

CircleToSearch with multi search engine support for all android

Kotlin

fix(ocr): removes hybrid text detection

AKS Labs31869815b3d7
diff --git a/.kotlin/errors/errors-1788557061572.log b/.kotlin/errors/errors-1788557061572.log
new file mode 100644
index 0000000..1219b50
--- /dev/null
+++ b/.kotlin/errors/errors-1788557061572.log
@@ -0,0 +1,4 @@
+kotlin version: 2.0.21
+error message: The daemon has terminated unexpectedly on startup attempt #1 with error code: 0. The daemon process output:
+ 1. Kotlin compile daemon is ready
+
diff --git a/app/src/main/java/com/akslabs/circletosearch/AssistSessionService.kt b/app/src/main/java/com/akslabs/circletosearch/AssistSessionService.kt
index e0ea762..d9a846a 100644
--- a/app/src/main/java/com/akslabs/circletosearch/AssistSessionService.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/AssistSessionService.kt
@@ -49,10 +49,6 @@ class AssistSessionService : VoiceInteractionSessionService() {
super.onShow(args, showFlags)
android.util.Log.d("AssistSessionService", "onShow called with flags: $showFlags")
- // Clear data at the absolute start of the session to prevent race conditions
- // with onHandleAssist/onHandleScreenshot delivery order.
- com.akslabs.circletosearch.data.AssistDataRepository.clear()
-
// Ensure our session window doesn't intercept target app touches
window?.window?.addFlags(android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
window?.window?.addFlags(android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
@@ -70,125 +66,6 @@ class AssistSessionService : VoiceInteractionSessionService() {
override fun onHandleAssist(data: Bundle?, structure: AssistStructure?, content: AssistContent?) {
super.onHandleAssist(data, structure, content)
android.util.Log.d("AssistSessionService", "onHandleAssist called")
-
- if (structure == null) {
- android.util.Log.w("AssistSessionService", "AssistStructure is null")
- return
- }
-
- val allNodes = mutableListOf<com.akslabs.circletosearch.ui.components.TextNode>()
- val coveredRects = mutableListOf<android.graphics.Rect>()
-
- android.util.Log.d("AssistSessionService", "Capturing AssistStructure - Window count: ${structure.windowNodeCount}")
-
- // Process windows from TOP to BOTTOM (Reverse order)
- // This allows us to track occlusion from overlays like BottomSheets.
- for (i in (structure.windowNodeCount - 1) downTo 0) {
- val windowNode = structure.getWindowNodeAt(i)
- val windowTitle = windowNode.title?.toString() ?: "No Title"
-
- android.util.Log.d("AssistSessionService", "Processing Window [$i]: \"$windowTitle\"")
-
- val windowOffsetX = windowNode.left
- val windowOffsetY = windowNode.top
-
- // Collect all text from this window, passing the occlusion mask
- collectTextNodes(windowNode.rootViewNode, windowOffsetX, windowOffsetY, allNodes, coveredRects)
-
- // After processing a window, we treat its main bounds as a "covered" area for lower windows.
- // We only do this for windows that are likely to be opaque overlays (Dialogs, BottomSheets, etc.)
- // System windows like StatusBar are handled separately or excluded if needed.
- if (windowNode.width > 0 && windowNode.height > 0) {
- coveredRects.add(android.graphics.Rect(windowOffsetX, windowOffsetY, windowOffsetX + windowNode.width, windowOffsetY + windowNode.height))
- }
- }
-
- if (allNodes.isEmpty()) {
- android.util.Log.w("AssistSessionService", "No text nodes found in assist data")
- } else {
- android.util.Log.d("AssistSessionService", "Extracted total of ${allNodes.size} text nodes from all windows")
- }
-
- com.akslabs.circletosearch.data.AssistDataRepository.setNodes(allNodes)
- }
-
- private fun collectTextNodes(
- node: AssistStructure.ViewNode,
- parentX: Int,
- parentY: Int,
- list: MutableList<com.akslabs.circletosearch.ui.components.TextNode>,
- coveredRects: List<android.graphics.Rect>
- ) {
- val nodeX = parentX + node.left
- val nodeY = parentY + node.top
- val nodeRect = android.graphics.Rect(nodeX, nodeY, nodeX + node.width, nodeY + node.height)
-
- // 1. OCCLUSION CHECK: If this node is completely covered by a higher-level window, skip it.
- // This prevents highlighting text that is "behind" a bottom sheet or dialog.
- val isOccluded = coveredRects.any { it.contains(nodeRect) }
- if (isOccluded) return
-
- // 2. PRECISION FILTERING: Avoid whole-screen highlights from root layouts.
- // We only fallback to contentDescription/hint for small elements (icons/buttons).
- val density = context.resources.displayMetrics.density
- val smallSizeThreshold = 100 * density // Approx 100dp
-
- val text = node.text?.toString() ?: run {
- // For contentDescription/hint, only capture if the element is small (likely an icon or input field)
- if (node.width < smallSizeThreshold && node.height < smallSizeThreshold) {
- node.contentDescription?.toString() ?: node.hint?.toString()
- } else {
- null
- }
- }
-
- if (!text.isNullOrBlank() &&
- node.visibility == android.view.View.VISIBLE &&
- node.width > 0 && node.height > 10) {
-
- // Only add if it's within reasonable screen bounds (optional but safer)
- // Filter out words that are clearly blank
- val wordStrings = text.split(Regex("\\s+")).filter { it.isNotBlank() }
- if (wordStrings.isNotEmpty()) {
- var currentStartIndex = 0
- val words = mutableListOf<com.akslabs.circletosearch.ui.components.Word>()
-
- wordStrings.forEachIndexed { wordIndex, wordText ->
- val startIndex = text.indexOf(wordText, currentStartIndex)
- val endIndex = startIndex + wordText.length
- if (startIndex != -1) {
- currentStartIndex = endIndex
- val wordBounds = android.graphics.RectF(nodeRect)
- words.add(
- com.akslabs.circletosearch.ui.components.Word(
- text = wordText,
- index = wordIndex,
- startIndex = startIndex,
- endIndex = endIndex,
- bounds = wordBounds
- )
- )
- }
- }
-
- list.add(
- com.akslabs.circletosearch.ui.components.TextNode(
- id = java.util.UUID.randomUUID().toString(),
- fullText = text,
- bounds = nodeRect,
- words = words
- )
- )
- }
- }
-
- // Important: For children, subtract current node's scroll position from translated coordinates
- val nextParentX = nodeX - node.scrollX
- val nextParentY = nodeY - node.scrollY
-
- for (i in 0 until node.childCount) {
- collectTextNodes(node.getChildAt(i), nextParentX, nextParentY, list, coveredRects)
- }
}
override fun onHandleScreenshot(screenshot: android.graphics.Bitmap?) {
@@ -200,8 +77,6 @@ class AssistSessionService : VoiceInteractionSessionService() {
// 2. Launch the search overlay
launchOverlayDirectly()
-
- // 3. Keep session alive for a moment to receive onHandleAssist data
}
private fun launchOverlayDirectly() {
diff --git a/app/src/main/java/com/akslabs/circletosearch/OverlayActivity.kt b/app/src/main/java/com/akslabs/circletosearch/OverlayActivity.kt
index afd5d02..e1c8cc6 100644
--- a/app/src/main/java/com/akslabs/circletosearch/OverlayActivity.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/OverlayActivity.kt
@@ -72,7 +72,6 @@ class OverlayActivity : ComponentActivity() {
searchModeOverride = searchModeOverride.value,
onClose = {
BitmapRepository.clear()
- com.akslabs.circletosearch.data.AssistDataRepository.clear()
finish()
},
copyTextManager = copyTextManager.value,
@@ -132,7 +131,6 @@ class OverlayActivity : ComponentActivity() {
copyTextManager.value = null
if (isFinishing) {
com.akslabs.circletosearch.data.BitmapRepository.clear()
- com.akslabs.circletosearch.data.AssistDataRepository.clear()
com.akslabs.circletosearch.utils.StorageUtils.clearAppCache(this)
}
}
diff --git a/app/src/main/java/com/akslabs/circletosearch/data/AssistDataRepository.kt b/app/src/main/java/com/akslabs/circletosearch/data/AssistDataRepository.kt
deleted file mode 100644
index 55867eb..0000000
--- a/app/src/main/java/com/akslabs/circletosearch/data/AssistDataRepository.kt
+++ /dev/null
@@ -1,28 +0,0 @@
-package com.akslabs.circletosearch.data
-
-import com.akslabs.circletosearch.ui.components.TextNode
-import kotlinx.coroutines.flow.MutableStateFlow
-import kotlinx.coroutines.flow.StateFlow
-import kotlinx.coroutines.flow.asStateFlow
-
-object AssistDataRepository {
- private val _assistNodes = MutableStateFlow<List<TextNode>>(emptyList())
- val assistNodes: StateFlow<List<TextNode>> = _assistNodes.asStateFlow()
-
- private val _isDataReady = MutableStateFlow(false)
- val isDataReady: StateFlow<Boolean> = _isDataReady.asStateFlow()
-
- fun setNodes(nodes: List<TextNode>) {
- _assistNodes.value = nodes
- _isDataReady.value = true
- }
-
- fun getNodes(): List<TextNode> {
- return _assistNodes.value
- }
-
- fun clear() {
- _assistNodes.value = emptyList()
- _isDataReady.value = false
- }
-}
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 a7795ac..6e8b4aa 100644
--- a/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt
@@ -1458,28 +1458,18 @@ fun CircleToSearchScreen(
}
// Circular Button: Assist Copy (Music Icon)
- val assistNodes by com.akslabs.circletosearch.data.AssistDataRepository.assistNodes.collectAsState()
- val isAssistDataReady by com.akslabs.circletosearch.data.AssistDataRepository.isDataReady.collectAsState()
-
IconButton(
onClick = {
haptic.performHapticFeedback(androidx.compose.ui.hapticfeedback.HapticFeedbackType.LongPress)
-
- if (isAssistDataReady) {
- // Use high-accuracy AssistStructure data + OCR merge
- copyTextManager?.setHybridMode(assistNodes)
- isCopyMode = true
- isCopyTextTriggered = true
- } else {
- // Data not ready (likely bubble trigger)
- android.widget.Toast.makeText(context, "Comming Soon: launch CTS as assistant to try Hybrid text detection.", android.widget.Toast.LENGTH_LONG).show()
- }
+ copyTextManager?.setOcrOnlyMode()
+ isCopyMode = true
+ isCopyTextTriggered = true
},
modifier = Modifier
.size(60.dp)
.background(MaterialTheme.colorScheme.surfaceContainer, CircleShape),
) {
- Icon(Icons.Default.MusicNote, contentDescription = "Assist Copy", tint = if (isAssistDataReady) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurface)
+ Icon(Icons.Default.MusicNote, contentDescription = "Assist Copy", tint = MaterialTheme.colorScheme.onSurface)
}
// Circular Button: Translate
@@ -1556,9 +1546,9 @@ fun CircleToSearchScreen(
}
// More Apps
- BottomBarButton("More Apps", { Icon(Icons.Default.Apps, null) }) {
- showMoreAppsSheet = true
- }
+// BottomBarButton("More Apps", { Icon(Icons.Default.Apps, null) }) {
+// showMoreAppsSheet = true
+// }
// Pin
val isPinEnabled = selectedBitmap != null
@@ -1622,9 +1612,9 @@ fun CircleToSearchScreen(
}
// Donate
- BottomBarButton("Donate", { Icon(painterResource(id = com.akslabs.circletosearch.R.drawable.donation), null, modifier = Modifier.size(22.dp)) }) {
- showDonateSheet = true
- }
+// BottomBarButton("Donate", { Icon(painterResource(id = com.akslabs.circletosearch.R.drawable.donation), null, modifier = Modifier.size(22.dp)) }) {
+// showDonateSheet = true
+// }
// Copy Text
BottomBarButton("Copy Text", { Icon(painterResource(id = com.akslabs.circletosearch.R.drawable.ocr), null, modifier = Modifier.size(20.dp)) }) {
diff --git a/app/src/main/java/com/akslabs/circletosearch/ui/components/CopyTextOverlayManager.kt b/app/src/main/java/com/akslabs/circletosearch/ui/components/CopyTextOverlayManager.kt
index 804698b..086be2a 100644
--- a/app/src/main/java/com/akslabs/circletosearch/ui/components/CopyTextOverlayManager.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/ui/components/CopyTextOverlayManager.kt
@@ -58,32 +58,16 @@ class CopyTextOverlayManager(
// Status message for the user
private val statusMessage = mutableStateOf<String?>(null)
-
- // Assistant Data support
- var isAssistMode: Boolean = false
- private set
-
- private var nativeNodes: List<TextNode> = emptyList()
- fun setHybridMode(nodes: List<TextNode>) {
- isAssistMode = true
- nativeNodes = nodes
- textNodes.clear()
- textNodes.addAll(nodes)
- updateAllWords()
- }
-
fun setOcrOnlyMode() {
- isAssistMode = false
- nativeNodes = emptyList()
textNodes.clear()
allWords = emptyList()
statusMessage.value = null
}
-
+
private fun updateAllWords() {
- allWords = textNodes.flatMap { node ->
- node.words.map { word ->
+ allWords = textNodes.flatMap { node ->
+ node.words.map { word ->
word
}
}
@@ -124,7 +108,7 @@ class CopyTextOverlayManager(
)
Spacer(Modifier.height(16.dp))
Text(
- if (isAssistMode) "Hybrid Deep Scan..." else "Scanning text...",
+ "Scanning text...",
style = MaterialTheme.typography.titleMedium,
color = ComposeColor.White,
modifier = Modifier
@@ -155,12 +139,7 @@ class CopyTextOverlayManager(
}
container.addView(topBar)
- if (isAssistMode) {
- // Trigger parallel OCR scan even if we have native nodes
- scanNodes(view, isHybrid = true)
- } else {
- scanNodes(view, isHybrid = false)
- }
+ scanNodes(view)
return container
}
@@ -262,7 +241,7 @@ class CopyTextOverlayManager(
dimView?.let { scanNodes(it) }
}
- private fun scanNodes(view: View, isHybrid: Boolean = false) {
+ private fun scanNodes(view: View) {
scanJob?.cancel()
statusMessage.value = null
scanJob = scope.launch(Dispatchers.Main) {
@@ -270,9 +249,6 @@ class CopyTextOverlayManager(
val bitmap = screenshotBitmap ?: BitmapRepository.getScreenshot()
if (bitmap == null) {
- if (isAssistMode && textNodes.isEmpty()) {
- statusMessage.value = "This app doesn't allow reading screen content."
- }
isScanning.value = false
view.invalidate()
return@launch
@@ -282,14 +258,10 @@ class CopyTextOverlayManager(
// OCR scan runs on background thread
val ocrNodes = TesseractEngine.extractText(context, bitmap)
- if (isHybrid) {
- mergeHybridNodes(ocrNodes)
- } else {
- val sortedNodes = ocrNodes.sortedWith(compareBy({ it.bounds.top }, { it.bounds.left }))
- textNodes.clear()
- textNodes.addAll(sortedNodes)
- updateAllWords()
- }
+ val sortedNodes = ocrNodes.sortedWith(compareBy({ it.bounds.top }, { it.bounds.left }))
+ textNodes.clear()
+ textNodes.addAll(sortedNodes)
+ updateAllWords()
if (textNodes.isEmpty()) {
statusMessage.value = "No text found on screen."
@@ -305,47 +277,6 @@ class CopyTextOverlayManager(
}
}
- private fun mergeHybridNodes(ocrNodes: List<TextNode>) {
- val newNodes = mutableListOf<TextNode>()
- // Start with existing native nodes
- newNodes.addAll(nativeNodes)
-
- for (ocr in ocrNodes) {
- if (!isDuplicate(ocr, nativeNodes)) {
- // Only add if OCR node is within screen bounds (roughly)
- if (ocr.bounds.left >= -50 && ocr.bounds.top >= -50) {
- newNodes.add(ocr)
- }
- }
- }
-
- val sortedNodes = newNodes.sortedWith(compareBy({ it.bounds.top }, { it.bounds.left }))
- textNodes.clear()
- textNodes.addAll(sortedNodes)
- updateAllWords()
- }
-
- private fun isDuplicate(ocr: TextNode, natives: List<TextNode>): Boolean {
- for (native in natives) {
- // 1. Coordinate Overlap Check (> 50% overlap)
- val ocrRect = ocr.bounds
- val nativeRect = native.bounds
-
- val intersect = Rect(ocrRect)
- if (intersect.intersect(nativeRect)) {
- val intersectArea = intersect.width() * intersect.height()
- val ocrArea = ocrRect.width() * ocrRect.height()
- if (intersectArea > ocrArea * 0.5) return true
- }
-
- // 2. Text Similarity Check
- val ocrT = ocr.fullText.lowercase().trim()
- val nativeT = native.fullText.lowercase().trim()
- if (nativeT.contains(ocrT) || ocrT.contains(nativeT)) return true
- }
- return false
- }
-
@SuppressLint("ClickableViewAccessibility")
inner class DimPunchOutView(context: Context) : View(context) {
private val density = resources.displayMetrics.density