CircleToSearch with multi search engine support for all android
Kotlin
filtered out large root layouts in text textection
AKS Labs95921c0929af
diff --git a/.idea/deviceManager.xml b/.idea/deviceManager.xmlnew file mode 100644index 0000000..91f9558--- /dev/null+++ b/.idea/deviceManager.xml@@ -0,0 +1,13 @@+<?xml version="1.0" encoding="UTF-8"?>+<project version="4">+ <component name="DeviceTable">+ <option name="columnSorters">+ <list>+ <ColumnSorterState>+ <option name="column" value="Name" />+ <option name="order" value="ASCENDING" />+ </ColumnSorterState>+ </list>+ </option>+ </component>+</project>\ No newline at end of filediff --git a/app/src/main/java/com/akslabs/circletosearch/AssistSessionService.kt b/app/src/main/java/com/akslabs/circletosearch/AssistSessionService.ktindex f4f8920..e0ea762 100644--- a/app/src/main/java/com/akslabs/circletosearch/AssistSessionService.kt+++ b/app/src/main/java/com/akslabs/circletosearch/AssistSessionService.kt@@ -77,9 +77,13 @@ class AssistSessionService : VoiceInteractionSessionService() {}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}")- for (i in 0 until 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"@@ -88,8 +92,15 @@ class AssistSessionService : VoiceInteractionSessionService() {val windowOffsetX = windowNode.leftval windowOffsetY = windowNode.top- // Collect all text from this window- collectTextNodes(windowNode.rootViewNode, windowOffsetX, windowOffsetY, allNodes)+ // 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()) {@@ -105,21 +116,35 @@ class AssistSessionService : VoiceInteractionSessionService() {node: AssistStructure.ViewNode,parentX: Int,parentY: Int,- list: MutableList<com.akslabs.circletosearch.ui.components.TextNode>+ list: MutableList<com.akslabs.circletosearch.ui.components.TextNode>,+ coveredRects: List<android.graphics.Rect>) {val nodeX = parentX + node.leftval 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- // Extract text with fallback priority: text > contentDescription > hint- val text = node.text?.toString()- ?: node.contentDescription?.toString()- ?: node.hint?.toString()+ 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 > 0) {-- val nodeRect = android.graphics.Rect(nodeX, nodeY, nodeX + node.width, nodeY + node.height)+ 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@@ -162,7 +187,7 @@ class AssistSessionService : VoiceInteractionSessionService() {val nextParentY = nodeY - node.scrollYfor (i in 0 until node.childCount) {- collectTextNodes(node.getChildAt(i), nextParentX, nextParentY, list)+ collectTextNodes(node.getChildAt(i), nextParentX, nextParentY, list, coveredRects)}}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.ktindex 6fcee03..f6336ef 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@@ -54,15 +54,25 @@ class CopyTextOverlayManager(private val textNodes = mutableStateListOf<TextNode>()private var allWords: List<Word> = emptyList()+ // Status message for the user+ private val statusMessage = mutableStateOf<String?>(null)+// Assistant Data supportvar isAssistMode: Boolean = falseprivate set++ private var nativeNodes: List<TextNode> = emptyList()fun setAssistNodes(nodes: List<TextNode>) {isAssistMode = true+ nativeNodes = nodestextNodes.clear()textNodes.addAll(nodes)- allWords = nodes.flatMap { node ->+ updateAllWords()+ }++ private fun updateAllWords() {+ allWords = textNodes.flatMap { node ->node.words.map { word ->word}@@ -109,7 +119,7 @@ class CopyTextOverlayManager()Spacer(Modifier.height(16.dp))Text(- if (isAssistMode) "Applying Assist Data..." else "Scanning text...",+ if (isAssistMode) "Hybrid Deep Scan..." else "Scanning text...",style = MaterialTheme.typography.titleMedium,color = ComposeColor.White,modifier = Modifier@@ -118,14 +128,33 @@ class CopyTextOverlayManager()}}++ statusMessage.value?.let { msg ->+ Box(+ modifier = Modifier+ .align(Alignment.BottomCenter)+ .padding(bottom = 100.dp)+ .background(ComposeColor.Black.copy(alpha = 0.7f), RoundedCornerShape(16.dp))+ .padding(horizontal = 20.dp, vertical = 10.dp)+ ) {+ Text(+ msg,+ color = ComposeColor.White,+ style = MaterialTheme.typography.bodyMedium+ )+ }+ }}}}}container.addView(topBar)- if (!isAssistMode) {- scanNodes(view)+ if (isAssistMode) {+ // Trigger parallel OCR scan even if we have native nodes+ scanNodes(view, isHybrid = true)+ } else {+ scanNodes(view, isHybrid = false)}return container}@@ -226,35 +255,42 @@ class CopyTextOverlayManager(dimView?.let { scanNodes(it) }}- private fun scanNodes(view: View) {+ private fun scanNodes(view: View, isHybrid: Boolean = false) {scanJob?.cancel()+ statusMessage.value = nullscanJob = scope.launch(Dispatchers.Main) {isScanning.value = trueval bitmap = screenshotBitmap ?: BitmapRepository.getScreenshot()+if (bitmap == null) {- textNodes.clear()+ if (isAssistMode && textNodes.isEmpty()) {+ statusMessage.value = "This app doesn't allow reading screen content."+ }isScanning.value = falseview.invalidate()return@launch}try {- val nodes = TesseractEngine.extractText(context, bitmap)- val sortedNodes = nodes.sortedWith(compareBy({ it.bounds.top }, { it.bounds.left }))+ // OCR scan runs on background thread+ val ocrNodes = TesseractEngine.extractText(context, bitmap)- textNodes.clear()- textNodes.addAll(sortedNodes)+ if (isHybrid) {+ mergeHybridNodes(ocrNodes)+ } else {+ val sortedNodes = ocrNodes.sortedWith(compareBy({ it.bounds.top }, { it.bounds.left }))+ textNodes.clear()+ textNodes.addAll(sortedNodes)+ updateAllWords()+ }- val tempAllWords = mutableListOf<Word>()- sortedNodes.forEach { node ->- node.words.forEach { word ->- tempAllWords.add(word)- }+ if (textNodes.isEmpty()) {+ statusMessage.value = "No text found on screen."}- allWords = tempAllWords- Log.d("CopyTextOverlay", "OCR complete: ${textNodes.size} nodes, ${allWords.size} words")++ Log.d("CopyTextOverlay", "Capture complete: ${textNodes.size} total nodes")} catch (e: Exception) {- Log.e("CopyTextOverlay", "OCR failed: ${e.message}")+ Log.e("CopyTextOverlay", "Extraction failed: ${e.message}")} finally {isScanning.value = falseview.invalidate()@@ -262,6 +298,47 @@ 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