Skip to content

CircleToSearch with multi search engine support for all android

Kotlin

fix: launch as assistant using home button long press

AKS Labsef909f8698f0
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index e199529..4bbb6b9 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -58,6 +58,34 @@
android:label="Wobble Home"
android:theme="@style/Theme.CircleToSearch" />
+ <!-- Assist entry point: lets assist intents (long-press home on builds that
+ route assist through an activity, the ASSIST key, voice-assist
+ entrypoints) resolve to this app directly instead of the system
+ disambiguation dialog. Triggers capture and finishes immediately. -->
+ <activity
+ android:name=".AssistEntryActivity"
+ android:exported="true"
+ android:excludeFromRecents="true"
+ android:launchMode="singleTask"
+ android:theme="@style/Theme.CircleToSearch.Transparent">
+ <intent-filter>
+ <action android:name="android.intent.action.ASSIST" />
+ <category android:name="android.intent.category.DEFAULT" />
+ <category android:name="android.intent.category.ASSIST" />
+ </intent-filter>
+ <intent-filter>
+ <action android:name="android.intent.action.VOICE_ASSIST" />
+ <category android:name="android.intent.category.DEFAULT" />
+ </intent-filter>
+ <!-- Launcher home-button long-press runs contextual search ("omni"),
+ dispatched via LAUNCH_CONTEXTUAL_SEARCH; paired with the
+ secure contextual_search_package setting pointing at this app. -->
+ <intent-filter>
+ <action android:name="android.app.contextualsearch.action.LAUNCH_CONTEXTUAL_SEARCH" />
+ <category android:name="android.intent.category.DEFAULT" />
+ </intent-filter>
+ </activity>
+
<activity
android:name=".ui.wobblepulldown.WobbleDeviceInfoActivity"
android:exported="false"
diff --git a/app/src/main/java/com/akslabs/circletosearch/AssistEntryActivity.kt b/app/src/main/java/com/akslabs/circletosearch/AssistEntryActivity.kt
new file mode 100644
index 0000000..985c9b4
--- /dev/null
+++ b/app/src/main/java/com/akslabs/circletosearch/AssistEntryActivity.kt
@@ -0,0 +1,127 @@
+/*
+ *
+ * * Copyright (C) 2025 AKS-Labs (original author)
+ *
+ * * This program is free software: you can redistribute it and/or modify
+ * * it under the terms of the GNU General Public License as published by
+ * * the Free Software Foundation, either version 3 of the License, or
+ * * (at your option) any later version.
+ *
+ * * This program is distributed in the hope that it will be useful,
+ * * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * * GNU General Public License for more details.
+ *
+ * * You should have received a copy of the GNU General Public License
+ * * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package com.akslabs.circletosearch
+
+import android.app.Activity
+import android.content.Intent
+import android.graphics.Bitmap
+import android.os.Build
+import android.os.Bundle
+import android.service.voice.VoiceInteractionSession
+import com.akslabs.circletosearch.data.BitmapRepository
+
+/**
+ * Entry point for system assist intents (ACTION_ASSIST / ACTION_VOICE_ASSIST) and
+ * the contextual search launch (ACTION_LAUNCH_CONTEXTUAL_SEARCH) that the system
+ * server targets at this package while it is the default assistant.
+ *
+ * The trigger is handed to our VoiceInteractionSession — the assistant path — with
+ * the invocation-time screenshot the system server captured with assistant
+ * privileges. The session launches the overlay from that screenshot, so the flow
+ * does not depend on the accessibility service. Accessibility capture remains as
+ * a fallback for invocations where no screenshot was delivered.
+ */
+class AssistEntryActivity : Activity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
+ overrideActivityTransition(OVERRIDE_TRANSITION_OPEN, 0, 0)
+ overrideActivityTransition(OVERRIDE_TRANSITION_CLOSE, 0, 0)
+ } else {
+ @Suppress("DEPRECATION")
+ overridePendingTransition(0, 0)
+ }
+
+ android.util.Log.d("CircleToSearchAssist", "Assist intent received: ${intent?.action}")
+ val screenshot = extractScreenshot(intent)
+
+ val voiceService = CircleToSearchVoiceService.instance
+ if (voiceService != null) {
+ try {
+ val args = Bundle()
+ if (screenshot != null) {
+ args.putParcelable(AssistSessionService.EXTRA_SCREENSHOT_BITMAP, screenshot)
+ }
+ // No screenshot attached? Ask the system for one through the
+ // assistant pipeline instead; the session falls back if it
+ // never arrives.
+ val flags = if (screenshot == null) {
+ VoiceInteractionSession.SHOW_WITH_SCREENSHOT
+ } else {
+ 0
+ }
+ voiceService.showSession(args, flags)
+ android.util.Log.d(
+ "CircleToSearchAssist",
+ "Handed trigger to voice interaction session (screenshot=${screenshot != null})"
+ )
+ finish()
+ return
+ } catch (e: Exception) {
+ android.util.Log.e("CircleToSearchAssist", "showSession failed; using direct path", e)
+ }
+ }
+
+ // Assistant session unavailable: still launch without accessibility when
+ // the system handed us its own screenshot.
+ if (screenshot != null) {
+ BitmapRepository.setScreenshot(screenshot)
+ try {
+ startActivity(Intent(this, OverlayActivity::class.java).apply {
+ addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
+ putExtra("triggered_by", "assistant")
+ })
+ } catch (e: Exception) {
+ android.util.Log.e("CircleToSearchAssist", "Failed to launch OverlayActivity", e)
+ }
+ } else {
+ CircleToSearchAccessibilityService.triggerCapture()
+ }
+ finish()
+ }
+
+ private fun extractScreenshot(intent: Intent?): Bitmap? {
+ val raw = try {
+ @Suppress("DEPRECATION")
+ intent?.extras?.get("android.app.contextualsearch.extra.SCREENSHOT")
+ } catch (e: Exception) {
+ null
+ }
+ if (raw == null) return null
+ if (raw !is Bitmap) {
+ android.util.Log.w("CircleToSearchAssist", "Unexpected screenshot extra type: ${raw.javaClass.name}")
+ return null
+ }
+ return try {
+ // Match the accessibility capture path: OCR/native pixel access expects a
+ // software ARGB_8888 bitmap; the assist screenshot may arrive as HARDWARE
+ // or a wide-color config.
+ if (raw.config != Bitmap.Config.ARGB_8888) {
+ raw.copy(Bitmap.Config.ARGB_8888, false)
+ } else {
+ raw
+ }
+ } catch (e: Exception) {
+ raw
+ }
+ }
+}
diff --git a/app/src/main/java/com/akslabs/circletosearch/AssistSessionService.kt b/app/src/main/java/com/akslabs/circletosearch/AssistSessionService.kt
index d9a846a..c936599 100644
--- a/app/src/main/java/com/akslabs/circletosearch/AssistSessionService.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/AssistSessionService.kt
@@ -33,6 +33,11 @@ import com.akslabs.circletosearch.data.BitmapRepository
class AssistSessionService : VoiceInteractionSessionService() {
+ companion object {
+ /** Screenshot handed from AssistEntryActivity (captured by the system's assist pipeline). */
+ const val EXTRA_SCREENSHOT_BITMAP = "com.akslabs.circletosearch.extra.SCREENSHOT_BITMAP"
+ }
+
override fun onCreate() {
super.onCreate()
android.util.Log.d("AssistSessionService", "Service onCreate")
@@ -45,6 +50,9 @@ class AssistSessionService : VoiceInteractionSessionService() {
inner class CircleToSearchSession(context: Context) : VoiceInteractionSession(context) {
+ private var captureLaunched = false
+ private val sessionHandler = android.os.Handler(android.os.Looper.getMainLooper())
+
override fun onShow(args: Bundle?, showFlags: Int) {
super.onShow(args, showFlags)
android.util.Log.d("AssistSessionService", "onShow called with flags: $showFlags")
@@ -52,7 +60,7 @@ class AssistSessionService : VoiceInteractionSessionService() {
// 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)
-
+
// Haptic feedback to acknowledge the trigger
val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@@ -61,6 +69,48 @@ class AssistSessionService : VoiceInteractionSessionService() {
@Suppress("DEPRECATION")
vibrator.vibrate(50)
}
+
+ // Screenshot captured by the system's assist pipeline (assistant privileges)
+ // and passed through the entry activity. Using it means the whole flow works
+ // without the accessibility service.
+ val systemScreenshot: android.graphics.Bitmap? = args?.let {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+ it.getParcelable(EXTRA_SCREENSHOT_BITMAP, android.graphics.Bitmap::class.java)
+ } else {
+ @Suppress("DEPRECATION")
+ it.getParcelable(EXTRA_SCREENSHOT_BITMAP)
+ }
+ }
+ if (systemScreenshot != null && !systemScreenshot.isRecycled) {
+ android.util.Log.d(
+ "AssistSessionService",
+ "Using system-provided screenshot ${systemScreenshot.width}x${systemScreenshot.height}"
+ )
+ BitmapRepository.setScreenshot(systemScreenshot)
+ captureLaunched = true
+ launchOverlayDirectly()
+ return
+ }
+
+ // No screenshot in args (e.g. plain assist invocation). Modern Android versions
+ // may never deliver onHandleScreenshot, so kick off a capture shortly after
+ // showing; whichever source (the fallback or the system screenshot) arrives
+ // first wins. Without the accessibility service wait a little longer for the
+ // system screenshot requested via SHOW_WITH_SCREENSHOT.
+ captureLaunched = false
+ val a11yAvailable = CircleToSearchAccessibilityService.instance != null
+ val fallbackDelayMs = if (a11yAvailable) 250L else 1200L
+ sessionHandler.postDelayed({
+ if (captureLaunched) return@postDelayed
+ captureLaunched = true
+ if (CircleToSearchAccessibilityService.instance != null) {
+ android.util.Log.d("AssistSessionService", "Using accessibility capture fallback")
+ CircleToSearchAccessibilityService.triggerCapture()
+ } else {
+ android.util.Log.d("AssistSessionService", "No capture source available; launching overlay directly")
+ launchOverlayDirectly()
+ }
+ }, fallbackDelayMs)
}
override fun onHandleAssist(data: Bundle?, structure: AssistStructure?, content: AssistContent?) {
@@ -71,10 +121,16 @@ class AssistSessionService : VoiceInteractionSessionService() {
override fun onHandleScreenshot(screenshot: android.graphics.Bitmap?) {
super.onHandleScreenshot(screenshot)
android.util.Log.d("AssistSessionService", "onHandleScreenshot received, bitmap null? ${screenshot == null}")
-
+
+ if (captureLaunched) {
+ android.util.Log.d("AssistSessionService", "Screenshot arrived after fallback already launched; ignoring")
+ return
+ }
+ captureLaunched = true
+
// 1. Save screenshot to repository
BitmapRepository.setScreenshot(screenshot)
-
+
// 2. Launch the search overlay
launchOverlayDirectly()
}
diff --git a/app/src/main/java/com/akslabs/circletosearch/CircleToSearchAccessibilityService.kt b/app/src/main/java/com/akslabs/circletosearch/CircleToSearchAccessibilityService.kt
index b81aa9f..6a57d3d 100644
--- a/app/src/main/java/com/akslabs/circletosearch/CircleToSearchAccessibilityService.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/CircleToSearchAccessibilityService.kt
@@ -1681,13 +1681,26 @@ class CircleToSearchAccessibilityService : AccessibilityService() {
// --- Hardware button triggers ---
// With FLAG_REQUEST_FILTER_KEY_EVENTS every volume/power press flows through
- // onKeyEvent. We NEVER consume (always return false), so volume still adjusts
- // and power still sleeps — presses are only observed to launch the overlay.
+ // onKeyEvent. Volume/power are NEVER consumed (always return false), so volume
+ // still adjusts and power still sleeps — presses are only observed to launch the
+ // overlay. HOME is the one exception: a long-press is claimed by us (the release
+ // is consumed) because this ROM routes long-press-home into the launcher's
+ // contextual search, which fails silently, instead of dispatching assist — so
+ // we detect the hold ourselves and a consumed UP prevents the stray "go home".
private var lastVolUpDown = 0L
private var lastVolDownDown = 0L
private var lastPowerDown = 0L
private var lastTriggerFiredAt = 0L
private val keysCurrentlyDown = mutableSetOf<Int>()
+ private val homeKeyHandler = android.os.Handler(android.os.Looper.getMainLooper())
+ private var homeDownEventTime = 0L
+ private var homeLongPressTriggered = false
+ private val homeLongPressRunnable = Runnable {
+ if (homeDownEventTime == 0L) return@Runnable
+ homeLongPressTriggered = true
+ android.util.Log.d("CircleToSearch", "Home long-press fired (threshold while held)")
+ fireHardwareTrigger()
+ }
override fun onKeyEvent(event: android.view.KeyEvent): Boolean {
val code = event.keyCode
@@ -1695,6 +1708,17 @@ class CircleToSearchAccessibilityService : AccessibilityService() {
android.view.KeyEvent.ACTION_DOWN -> {
// Long-press repeats must not count as new presses
if (event.repeatCount != 0) return false
+ if (code == android.view.KeyEvent.KEYCODE_HOME) {
+ homeDownEventTime = event.eventTime
+ homeLongPressTriggered = false
+ homeKeyHandler.removeCallbacks(homeLongPressRunnable)
+ homeKeyHandler.postDelayed(
+ homeLongPressRunnable,
+ android.view.ViewConfiguration.getLongPressTimeout().toLong(),
+ )
+ android.util.Log.d("CircleToSearch", "HOME down observed by a11y filter")
+ return false // short tap must still behave like the Home button
+ }
if (code != android.view.KeyEvent.KEYCODE_VOLUME_UP &&
code != android.view.KeyEvent.KEYCODE_VOLUME_DOWN &&
code != android.view.KeyEvent.KEYCODE_POWER
@@ -1746,6 +1770,23 @@ class CircleToSearchAccessibilityService : AccessibilityService() {
if (fire) fireHardwareTrigger()
}
android.view.KeyEvent.ACTION_UP -> {
+ if (code == android.view.KeyEvent.KEYCODE_HOME) {
+ homeKeyHandler.removeCallbacks(homeLongPressRunnable)
+ val sawDown = homeDownEventTime != 0L
+ val held = if (sawDown) event.eventTime - homeDownEventTime else 0L
+ val wasLongPress = sawDown &&
+ (homeLongPressTriggered ||
+ held >= android.view.ViewConfiguration.getLongPressTimeout())
+ homeDownEventTime = 0L
+ if (wasLongPress) {
+ if (!homeLongPressTriggered) fireHardwareTrigger()
+ // Consume the release: we handled this press, so the system
+ // must not also run its "go home" on key-up.
+ android.util.Log.d("CircleToSearch", "HOME long-press release consumed")
+ return true
+ }
+ return false // normal short tap → Home behaves as usual
+ }
keysCurrentlyDown.remove(code)
}
}
diff --git a/app/src/main/java/com/akslabs/circletosearch/CircleToSearchVoiceService.kt b/app/src/main/java/com/akslabs/circletosearch/CircleToSearchVoiceService.kt
index b9cb06c..364ce8e 100644
--- a/app/src/main/java/com/akslabs/circletosearch/CircleToSearchVoiceService.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/CircleToSearchVoiceService.kt
@@ -27,8 +27,21 @@ import android.service.voice.VoiceInteractionService
* The actual assist handling is done by AssistSessionService.
*/
class CircleToSearchVoiceService : VoiceInteractionService() {
+
+ companion object {
+ /** Live instance while the system has this service ready as the active assistant. */
+ var instance: CircleToSearchVoiceService? = null
+ private set
+ }
+
+ override fun onCreate() {
+ super.onCreate()
+ android.util.Log.d("CircleToSearchVoiceService", "VoiceService onCreate")
+ }
+
override fun onReady() {
super.onReady()
+ instance = this
android.util.Log.d("CircleToSearchVoiceService", "VoiceService Ready")
}
@@ -36,4 +49,9 @@ class CircleToSearchVoiceService : VoiceInteractionService() {
super.onShutdown()
android.util.Log.d("CircleToSearchVoiceService", "VoiceService Shutdown")
}
+
+ override fun onDestroy() {
+ instance = null
+ super.onDestroy()
+ }
}
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 09bfba4..88b1fdc 100644
--- a/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt
@@ -220,7 +220,16 @@ private suspend fun runInstantScreenshotOcr(
): ImageWordSelection? {
val app = context.applicationContext as? TranslatorApplication ?: return null
val coordinator = runCatching { app.translationCoordinator }.getOrNull() ?: return null
- val target = CircleToSearchAccessibilityService.instance?.defaultTargetLanguage() ?: return null
+ // The accessibility service exposes the configured target language when it is
+ // running, but the assistant launch path must also work with accessibility
+ // disabled — resolve the same setting from the translator application directly.
+ val target = CircleToSearchAccessibilityService.instance?.defaultTargetLanguage()
+ ?: runCatching {
+ app.languageCatalog?.languageByCode(
+ app.settingsManager.settings.value.defaultTargetLanguageCode
+ )
+ }.getOrNull()
+ ?: return null
var ocrMissing = false
var sourceDetectFailed = false
val result = coordinator.ocrWordsOnly(