Skip to content

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.

fix: a theme change reaches the assistive ball while it is showing

Durganshu Mishraf2f1c72dde86
diff --git a/app/src/main/java/com/gotcha/data/SettingsRepository.kt b/app/src/main/java/com/gotcha/data/SettingsRepository.kt
index 7e88af3..79cf2d5 100644
--- a/app/src/main/java/com/gotcha/data/SettingsRepository.kt
+++ b/app/src/main/java/com/gotcha/data/SettingsRepository.kt
@@ -209,6 +209,27 @@ internal fun migrateSkinId(legacyThemeMode: String?): String =
SKIN_DEEP_SPACE_DARK
}
+/** The preference file [SettingsRepository] encrypts into. */
+internal const val SETTINGS_PREFS_FILE = "gotcha_settings"
+
+/**
+ * The raw preference file underneath [SettingsRepository], for change
+ * notification and nothing else. Everything in it is encrypted; read values
+ * through the repository.
+ *
+ * This exists because [EncryptedSharedPreferences] holds its listener list on
+ * the *wrapper*, and `create` hands back a new wrapper every call — so a
+ * listener registered through one `SettingsRepository` is never told about a
+ * write made through another one, which is every write the app actually makes.
+ * The file beneath is the process-wide singleton the framework caches, and it
+ * notifies whoever wrote to it.
+ *
+ * Keys arrive encrypted, so a listener cannot match on one. Read the setting
+ * back and compare instead.
+ */
+fun settingsChangeNotifier(context: Context): SharedPreferences =
+ context.applicationContext.getSharedPreferences(SETTINGS_PREFS_FILE, Context.MODE_PRIVATE)
+
internal const val SKIN_DEEP_SPACE_DARK = "deepspace"
internal const val SKIN_DEEP_SPACE_LIGHT = "deepspace_light"
private const val LEGACY_THEME_MODE_LIGHT = "LIGHT"
@@ -222,7 +243,7 @@ class SettingsRepository(context: Context) {
.build()
EncryptedSharedPreferences.create(
context.applicationContext,
- "gotcha_settings",
+ SETTINGS_PREFS_FILE,
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
diff --git a/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt b/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt
index c39d399..f408f03 100644
--- a/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt
+++ b/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt
@@ -2,8 +2,10 @@ package com.gotcha.ui
import android.animation.ValueAnimator
import android.content.Context
+import android.content.SharedPreferences
import android.graphics.Color
import android.graphics.PixelFormat
+import android.graphics.Typeface
import android.graphics.drawable.GradientDrawable
import android.os.Handler
import android.os.Looper
@@ -22,6 +24,7 @@ import android.widget.TextView
import androidx.core.graphics.ColorUtils
import com.gotcha.R
import com.gotcha.data.SettingsRepository
+import com.gotcha.data.settingsChangeNotifier
import com.gotcha.service.EntityType
import com.gotcha.service.ProactiveSessionItem
import com.gotcha.service.SmartActionDetector
@@ -95,22 +98,75 @@ class AssistiveBallOverlay(context: Context) {
private var statusRingAnimator: ValueAnimator? = null
private var activity: BallActivity = BallActivity.IDLE
+ /** What the card is showing, so a theme change can rebuild it as it was. */
+ private var cardMessage: String? = null
+ private var cardShowClose: Boolean = true
+
private var dockSide: Int = DOCK_SIDE_END
private val ballParams: WindowManager.LayoutParams by lazy { ballLayoutParams() }
private val settingsRepository by lazy { SettingsRepository(appContext) }
/**
- * The active skin, translated for View code. Re-read on each build rather
- * than cached, so changing theme in Settings reaches the ball the next time
- * it draws anything without needing to tear the window down.
+ * The skin the chrome on screen is currently wearing.
+ *
+ * The menu and the card are rebuilt every time they open, so they pick up a
+ * new theme on their own. The ball does not: it is built once in [show] and
+ * then lives for as long as the service does, which meant a theme changed
+ * in Settings did not reach it until the ball was switched off and on
+ * again. This is what [restyleIfSkinChanged] compares against.
+ */
+ private var appliedSkinId: String? = null
+
+ /**
+ * Repaint when the theme changes underneath us.
+ *
+ * Registered on [settingsChangeNotifier] — the raw file — rather than on
+ * the repository's own encrypted view, because that view keeps its listener
+ * list per instance and Appearance saves through an instance of its own.
+ *
+ * The key is ignored: it arrives encrypted, and `save` writes every setting
+ * at once so almost anything fires this. Reading the skin back and
+ * comparing is the only thing that works, and it is what makes the listener
+ * cheap enough not to care how often it runs.
*/
- private fun palette(): OverlaySkin = overlaySkin(
- appContext,
+ private val skinWatcher = SharedPreferences.OnSharedPreferenceChangeListener { _, _ ->
+ mainHandler.post { restyleIfSkinChanged() }
+ }
+
+ private fun currentSkinId(): String =
runCatching { settingsRepository.load().skinId }.getOrDefault(Skins.DEFAULT_ID)
- )
+
+ /** The active skin, translated for View code. */
+ private fun palette(): OverlaySkin = overlaySkin(appContext, currentSkinId())
private fun accentColor(): Int = palette().accent
+ /**
+ * Rebuild whatever is on screen in the new theme.
+ *
+ * The ball is restyled in place rather than recreated: its window carries
+ * the drag position and the touch listener, and tearing it down to change a
+ * colour would drop both.
+ */
+ private fun restyleIfSkinChanged() {
+ if (ballView == null) return
+ val skinId = currentSkinId()
+ if (skinId == appliedSkinId) return
+ appliedSkinId = skinId
+
+ val colors = overlaySkin(appContext, skinId)
+ ballView?.let { applyBallSkin(it, colors) }
+ if (statusRingView != null) refreshStatusRing()
+ if (menuView != null) {
+ removeMenu()
+ // Not a fresh open, so no clipboard poke: that launches an activity,
+ // and changing theme is not a reason to take focus off whatever the
+ // user is actually looking at.
+ showMenu(requestClipboard = false)
+ }
+ cardMessage?.let { showCard(it, cardShowClose) }
+ }
+
fun canShow(): Boolean = Settings.canDrawOverlays(appContext)
/**
@@ -157,6 +213,13 @@ class AssistiveBallOverlay(context: Context) {
windowManager.addView(ball, ballParams)
ballView = ball
refreshStatusRing()
+ // SharedPreferences keeps only a weak reference to a listener;
+ // [skinWatcher] is a field of this object, which the service
+ // holds, so it survives as long as the ball does.
+ runCatching {
+ settingsChangeNotifier(appContext)
+ .registerOnSharedPreferenceChangeListener(skinWatcher)
+ }
} catch (_: Exception) {
ballView = null
}
@@ -165,6 +228,10 @@ class AssistiveBallOverlay(context: Context) {
fun dismiss() {
mainHandler.post {
+ runCatching {
+ settingsChangeNotifier(appContext)
+ .unregisterOnSharedPreferenceChangeListener(skinWatcher)
+ }
removeLongPressRing()
removeStatusRing()
removeMenu()
@@ -172,6 +239,7 @@ class AssistiveBallOverlay(context: Context) {
removeDismissTarget()
ballView?.let { safeRemove(it) }
ballView = null
+ appliedSkinId = null
}
}
@@ -212,6 +280,8 @@ class AssistiveBallOverlay(context: Context) {
fun showCard(message: String, showClose: Boolean = true) {
mainHandler.post {
removeCard()
+ cardMessage = message
+ cardShowClose = showClose
val colors = palette()
val container = LinearLayout(appContext).apply {
orientation = LinearLayout.VERTICAL
@@ -271,38 +341,46 @@ class AssistiveBallOverlay(context: Context) {
*/
private fun buildBall(): View {
val size = dp(BALL_SIZE_DP)
- val colors = palette()
- val density = appContext.resources.displayMetrics.density
- val disc = overlayCardBackground(
- density = density,
- colors = colors,
- radiusDp = BALL_SIZE_DP / 2f,
- fill = colors.ground,
- shadowRadiusDp = BALL_SHADOW_DP
- )
+ appliedSkinId = currentSkinId()
return android.widget.ImageView(appContext).apply {
contentDescription = ASSISTIVE_BALL_CONTENT_DESCRIPTION
// The in-app mark. The launcher icon is adaptive now, so it would
// draw at two-thirds size inside its own safe zone.
setImageResource(R.drawable.gotcha_logo)
scaleType = android.widget.ImageView.ScaleType.CENTER_INSIDE
- background = disc
- val inset = disc.shadowPadPx + dp(BALL_MARK_INSET_DP)
- setPadding(inset, inset, inset, inset)
+ applyBallSkin(this, palette())
alpha = PEEK_ALPHA
setOnTouchListener(ballTouchListener())
layoutParams = LinearLayout.LayoutParams(size, size)
}
}
+ /**
+ * Dress the ball in [colors]. Separate from [buildBall] because a theme
+ * change repaints the existing view rather than making a new one — the
+ * window it lives in carries the drag position and the touch listener.
+ */
+ private fun applyBallSkin(view: View, colors: OverlaySkin) {
+ val disc = overlayCardBackground(
+ density = appContext.resources.displayMetrics.density,
+ colors = colors,
+ radiusDp = BALL_SIZE_DP / 2f,
+ fill = colors.ground,
+ shadowRadiusDp = BALL_SHADOW_DP
+ )
+ view.background = disc
+ val inset = disc.shadowPadPx + dp(BALL_MARK_INSET_DP)
+ view.setPadding(inset, inset, inset, inset)
+ }
+
private fun toggleMenu() {
if (menuView != null) removeMenu() else showMenu()
}
- private fun showMenu() {
+ private fun showMenu(requestClipboard: Boolean = true) {
removeMenu()
// Trigger a fresh clipboard read if no smart action is already staged.
- if (pendingSmartAction == null) {
+ if (requestClipboard && pendingSmartAction == null) {
onRequestClipboardCheck()
}
val colors = palette()
@@ -326,19 +404,19 @@ class AssistiveBallOverlay(context: Context) {
orientation = LinearLayout.VERTICAL
}
appNavRow.addView(
- tapButton("Open App", colors, R.drawable.ic_overlay_open_app) {
+ tapButton("Open App", colors, R.drawable.ic_overlay_open_app, bold = true) {
removeMenu()
onOpenApp()
}
)
appNavRow.addView(
- tapButton("Screenshot", colors, R.drawable.ic_overlay_screenshot) {
+ tapButton("Screenshot", colors, R.drawable.ic_overlay_screenshot, bold = true) {
removeMenu()
onTakeScreenshot()
}
)
appNavRow.addView(
- tapButton("Lens", colors, R.drawable.ic_overlay_lens) {
+ tapButton("Lens", colors, R.drawable.ic_overlay_lens, bold = true) {
removeMenu()
onStartLens()
}
@@ -488,19 +566,25 @@ class AssistiveBallOverlay(context: Context) {
* chose, so they are the one element in the menu guaranteed not to match
* the app. Rows without one keep the text on the same column as rows with
* one, so the menu reads as a list rather than as two lists.
+ *
+ * [bold] is for the fixed commands — Open App, Screenshot, Lens. They are
+ * always there and always mean the same thing, and the weight separates
+ * them from the smart-action rows above, which are suggestions that come
+ * and go with whatever is on the clipboard.
*/
private fun tapButton(
label: String,
colors: OverlaySkin,
iconRes: Int? = null,
asMenuRow: Boolean = true,
+ bold: Boolean = false,
onClick: () -> Unit
): TextView {
val iconPx = dp(MENU_ICON_DP)
val iconGutter = iconPx + dp(MENU_ICON_GAP_DP)
return TextView(appContext).apply {
text = label
- typeface = colors.sans
+ typeface = if (bold) Typeface.create(colors.sans, Typeface.BOLD) else colors.sans
textSize = colors.bodySp
setTextColor(colors.buttonText)
gravity = if (asMenuRow) Gravity.CENTER_VERTICAL or Gravity.START else Gravity.CENTER
@@ -540,6 +624,7 @@ class AssistiveBallOverlay(context: Context) {
private fun removeCard() {
cardView?.let { safeRemove(it) }
cardView = null
+ cardMessage = null
}
/**
diff --git a/app/src/test/java/com/gotcha/data/SettingsChangeNotifierTest.kt b/app/src/test/java/com/gotcha/data/SettingsChangeNotifierTest.kt
new file mode 100644
index 0000000..3387d94
--- /dev/null
+++ b/app/src/test/java/com/gotcha/data/SettingsChangeNotifierTest.kt
@@ -0,0 +1,75 @@
+package com.gotcha.data
+
+import android.content.Context
+import android.content.SharedPreferences
+import androidx.test.core.app.ApplicationProvider
+import org.junit.Assert.assertSame
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+
+/**
+ * The assistive ball is built once and then lives as long as the service, so it
+ * has to be *told* when the theme changes rather than re-reading it on the next
+ * draw. [settingsChangeNotifier] is the telling.
+ *
+ * The trap it exists to avoid: `EncryptedSharedPreferences` keeps its listener
+ * list on the wrapper object, and `create` returns a new wrapper every call. A
+ * listener registered through one [SettingsRepository] therefore never hears
+ * about a write made through another — and every writer in this app makes its
+ * own. Registering on the raw file works because that one *is* shared.
+ *
+ * What is asserted here is the half that a JVM test can reach: the raw file is
+ * process-wide and it notifies. The other half — that the repository encrypts
+ * into this same file — is held by [SETTINGS_PREFS_FILE] being the one constant
+ * both use, because `EncryptedSharedPreferences` needs the AndroidKeyStore and
+ * cannot be constructed under Robolectric at all.
+ */
+@RunWith(RobolectricTestRunner::class)
+class SettingsChangeNotifierTest {
+
+ private val context = ApplicationProvider.getApplicationContext<Context>()
+
+ /** The framework holds listeners weakly; this outlives the registration. */
+ private var listener: SharedPreferences.OnSharedPreferenceChangeListener? = null
+
+ @Test
+ fun `every caller gets the same preferences object`() {
+ assertSame(
+ "a per-caller instance would mean a per-caller listener list, which " +
+ "is the exact bug this function exists to route around",
+ settingsChangeNotifier(context),
+ settingsChangeNotifier(context)
+ )
+ }
+
+ @Test
+ fun `a write by one caller reaches a listener registered by another`() {
+ val fired = intArrayOf(0)
+ val watcher = SharedPreferences.OnSharedPreferenceChangeListener { _, _ -> fired[0]++ }
+ listener = watcher
+ settingsChangeNotifier(context).registerOnSharedPreferenceChangeListener(watcher)
+
+ settingsChangeNotifier(context).edit().putString("some_encrypted_key", "x").apply()
+
+ assertTrue("the notifier must notify", fired[0] > 0)
+ settingsChangeNotifier(context).unregisterOnSharedPreferenceChangeListener(watcher)
+ }
+
+ @Test
+ fun `unregistering stops the notifications`() {
+ val fired = intArrayOf(0)
+ val watcher = SharedPreferences.OnSharedPreferenceChangeListener { _, _ -> fired[0]++ }
+ listener = watcher
+ val prefs = settingsChangeNotifier(context)
+ prefs.registerOnSharedPreferenceChangeListener(watcher)
+ prefs.unregisterOnSharedPreferenceChangeListener(watcher)
+
+ prefs.edit().putString("another_key", "y").apply()
+
+ // The ball unregisters on dismiss; a listener that kept firing would be
+ // repainting a window that is no longer on screen.
+ assertTrue("unregister must actually detach", fired[0] == 0)
+ }
+}