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.

feat: enhance tour functionality with new actions and anchors for improved user guidance

Durganshu Mishra157cb9bdec87
diff --git a/app/src/main/java/com/gotcha/ui/PermissionsSection.kt b/app/src/main/java/com/gotcha/ui/PermissionsSection.kt
index 893918f..2a124b0 100644
--- a/app/src/main/java/com/gotcha/ui/PermissionsSection.kt
+++ b/app/src/main/java/com/gotcha/ui/PermissionsSection.kt
@@ -151,7 +151,13 @@ private fun PermissionRow(
}
}
-private fun openSpecialAccess(context: android.content.Context, marker: String, packageName: String) {
+/**
+ * Opens the system screen that grants [marker]. Shared with the feature tour,
+ * whose permission steps offer the same journey from their coach card — two
+ * copies of this intent table would be two places for an OEM quirk to be fixed
+ * in only one.
+ */
+internal fun openSpecialAccess(context: android.content.Context, marker: String, packageName: String) {
val intent: Intent? = when (marker) {
ToolResult.WRITE_SETTINGS -> Intent(
AndroidSettings.ACTION_MANAGE_WRITE_SETTINGS,
diff --git a/app/src/main/java/com/gotcha/ui/PersonalInfoScreen.kt b/app/src/main/java/com/gotcha/ui/PersonalInfoScreen.kt
index 04f5e81..2bf692b 100644
--- a/app/src/main/java/com/gotcha/ui/PersonalInfoScreen.kt
+++ b/app/src/main/java/com/gotcha/ui/PersonalInfoScreen.kt
@@ -27,6 +27,8 @@ import com.gotcha.data.Settings
import com.gotcha.i18n.Language
import com.gotcha.ui.theme.SkinAlertDialog
import com.gotcha.ui.theme.SkinExposedDropdownMenu
+import com.gotcha.ui.tour.TourAnchor
+import com.gotcha.ui.tour.tourAnchor
import kotlinx.coroutines.launch
/** Currencies offered for [Settings.preferredCurrency]. */
@@ -103,6 +105,7 @@ fun PersonalInfoScreen(
modifier = Modifier
.fillMaxWidth()
.testTag("settings_user_name")
+ .tourAnchor(TourAnchor.PERSONAL_NAME)
)
OutlinedTextField(
value = userLocation,
@@ -283,6 +286,7 @@ fun PersonalInfoScreen(
modifier = Modifier
.fillMaxWidth()
.testTag("settings_save_personal_info")
+ .tourAnchor(TourAnchor.PERSONAL_SAVE)
) { Text("Save Personal Info") }
}
}
diff --git a/app/src/main/java/com/gotcha/ui/tour/TourAnchors.kt b/app/src/main/java/com/gotcha/ui/tour/TourAnchors.kt
index 60d892f..7ea6ffe 100644
--- a/app/src/main/java/com/gotcha/ui/tour/TourAnchors.kt
+++ b/app/src/main/java/com/gotcha/ui/tour/TourAnchors.kt
@@ -1,9 +1,13 @@
package com.gotcha.ui.tour
+import androidx.compose.foundation.ExperimentalFoundationApi
+import androidx.compose.foundation.relocation.BringIntoViewRequester
+import androidx.compose.foundation.relocation.bringIntoViewRequester
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.mutableStateMapOf
+import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
@@ -25,9 +29,18 @@ enum class TourAnchor {
SETTINGS_PERMISSIONS,
AI_PROVIDER,
AI_SAMOSA_SIGN_IN,
- AI_SAVE
+ AI_SAVE,
+ PERSONAL_NAME,
+ PERSONAL_SAVE
}
+/** Where one anchored control is, and how to scroll it back into view. */
+@OptIn(ExperimentalFoundationApi::class)
+private class AnchorHandle(
+ val bounds: Rect,
+ val requester: BringIntoViewRequester
+)
+
/**
* Where each registered [TourAnchor] currently is, in root coordinates.
*
@@ -37,16 +50,34 @@ enum class TourAnchor {
*/
@Stable
class TourAnchors {
- private val bounds = mutableStateMapOf<TourAnchor, Rect>()
+ private val handles = mutableStateMapOf<TourAnchor, AnchorHandle>()
+
+ /**
+ * The visible bounds of [anchor], or null when it is not on screen.
+ *
+ * Deliberately the *clipped* bounds. A control scrolled out of its list
+ * still exists and still reports a position, and treating that as a
+ * spotlight punches the hole somewhere the user cannot see while blocking
+ * every part of the screen they can — which is precisely the trap the tour
+ * must never spring. Null here means "not visible", and the overlay asks
+ * [bringIntoView] to fix that rather than drawing a hole into nothing.
+ */
+ operator fun get(anchor: TourAnchor): Rect? =
+ handles[anchor]?.bounds?.takeIf { !it.isEmpty }
- operator fun get(anchor: TourAnchor): Rect? = bounds[anchor]
+ /** Scrolls [anchor] back into view, if it is registered and scrollable. */
+ @OptIn(ExperimentalFoundationApi::class)
+ suspend fun bringIntoView(anchor: TourAnchor) {
+ handles[anchor]?.requester?.bringIntoView()
+ }
- internal fun record(anchor: TourAnchor, rect: Rect) {
- bounds[anchor] = rect
+ @OptIn(ExperimentalFoundationApi::class)
+ internal fun record(anchor: TourAnchor, bounds: Rect, requester: BringIntoViewRequester) {
+ handles[anchor] = AnchorHandle(bounds, requester)
}
internal fun forget(anchor: TourAnchor) {
- bounds.remove(anchor)
+ handles.remove(anchor)
}
}
@@ -64,13 +95,20 @@ val LocalTourAnchors = staticCompositionLocalOf { TourAnchors() }
* reads the result, and a build with the tour disabled writes into the default
* registry above and is otherwise unaffected.
*/
+@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Modifier.tourAnchor(anchor: TourAnchor): Modifier {
val anchors = LocalTourAnchors.current
+ // Half the controls worth pointing at — a Save button at the foot of a long
+ // settings page — are off screen when their step opens. Registering a
+ // requester here is what lets the tour scroll them back to the user.
+ val requester = remember { BringIntoViewRequester() }
// Rows scroll away and pages are left; a stale rect would spotlight whatever
// has since been laid out in the same place.
DisposableEffect(anchors, anchor) {
onDispose { anchors.forget(anchor) }
}
- return onGloballyPositioned { anchors.record(anchor, it.boundsInRoot()) }
+ return this
+ .bringIntoViewRequester(requester)
+ .onGloballyPositioned { anchors.record(anchor, it.boundsInRoot(), requester) }
}
diff --git a/app/src/main/java/com/gotcha/ui/tour/TourOverlay.kt b/app/src/main/java/com/gotcha/ui/tour/TourOverlay.kt
index 5bb14a5..8667a06 100644
--- a/app/src/main/java/com/gotcha/ui/tour/TourOverlay.kt
+++ b/app/src/main/java/com/gotcha/ui/tour/TourOverlay.kt
@@ -1,5 +1,6 @@
package com.gotcha.ui.tour
+import android.content.Context
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
@@ -17,6 +18,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
+import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
@@ -51,11 +53,17 @@ import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
+import com.gotcha.ui.openSpecialAccess
import com.gotcha.ui.theme.GotchaMono
import kotlinx.coroutines.delay
import kotlin.math.roundToInt
import android.provider.Settings as AndroidSettings
+/** Runs a coach card's primary action — always a trip to a system settings screen. */
+private fun openTourAction(context: Context, action: TourAction) {
+ openSpecialAccess(context, action.marker, context.packageName)
+}
+
/** Gap between the spotlit control and the card explaining it. */
private val CARD_GAP = 12.dp
@@ -84,9 +92,16 @@ fun TourOverlay(
val anchors = LocalTourAnchors.current
val hole = step.anchor?.let { anchors[it] }
+ // The control this step is about is often below the fold — a Save button at
+ // the foot of a long page. Scroll it up before asking the user to press it.
+ LaunchedEffect(step.id, step.anchor) {
+ step.anchor?.let { anchors.bringIntoView(it) }
+ }
+
// A step that belongs to a branch this user isn't on — signing in to Samosa
// when they have chosen their own API key. Wait a beat for the page to
- // finish laying out before concluding the control is genuinely not there.
+ // finish laying out, and for the scroll above to land, before concluding the
+ // control is genuinely not there.
LaunchedEffect(step.id) {
if (step.requiresAnchor) {
delay(ANCHOR_GRACE_MS)
@@ -299,6 +314,20 @@ private fun CoachCard(
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
+ // The way out of the app, for the steps whose toggle lives in
+ // Android's own settings. Telling someone where to go is not the
+ // same as taking them, and the path is three screens deep on some
+ // phones — see the hint above, which is only there because of that.
+ step.action?.let { action ->
+ val context = LocalContext.current
+ Button(
+ onClick = { openTourAction(context, action) },
+ modifier = Modifier
+ .fillMaxWidth()
+ .padding(top = 4.dp)
+ .testTag("tour_action")
+ ) { Text(action.label) }
+ }
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
diff --git a/app/src/main/java/com/gotcha/ui/tour/TourStep.kt b/app/src/main/java/com/gotcha/ui/tour/TourStep.kt
index de73380..2159cfe 100644
--- a/app/src/main/java/com/gotcha/ui/tour/TourStep.kt
+++ b/app/src/main/java/com/gotcha/ui/tour/TourStep.kt
@@ -2,6 +2,7 @@ package com.gotcha.ui.tour
import android.content.Context
import com.gotcha.data.Settings
+import com.gotcha.tools.ToolResult
import com.gotcha.ui.isAccessibilityGranted
import com.gotcha.ui.isOverlayGranted
@@ -24,6 +25,13 @@ enum class TourPlace {
PERSONAL_INFO
}
+/**
+ * A coach card's primary button, for the steps whose real work happens outside
+ * the app. [marker] is one of [com.gotcha.tools.ToolResult]'s special-access
+ * markers, resolved through the same intent table the Permissions screen uses.
+ */
+data class TourAction(val label: String, val marker: String)
+
/**
* One instruction in the guided setup.
*
@@ -46,6 +54,8 @@ enum class TourPlace {
* not on screen. Used for branches that only exist under some configurations.
* @param ackLabel label for the button that dismisses the step without doing the
* thing. Null means the step can only be finished by acting on it.
+ * @param action a primary button that leaves for a system settings screen, for
+ * the steps whose toggle is not ours to draw.
* @param isDone the state that ends the step, re-read on every resume.
*/
data class TourStep(
@@ -58,6 +68,7 @@ data class TourStep(
val hint: String? = null,
val requiresAnchor: Boolean = false,
val ackLabel: String? = null,
+ val action: TourAction? = null,
val isDone: (Settings, Context) -> Boolean = { _, _ -> false }
)
@@ -69,7 +80,11 @@ data class TourStep(
* merely nice to know belongs in the screen it lives on, not in a queue the user
* has to clear before they can type their first message.
*/
-fun defaultTourSteps(): List<TourStep> = listOf(
+fun defaultTourSteps(): List<TourStep> =
+ brainSteps() + permissionSteps() + profileSteps()
+
+/** Finding Settings, and giving the assistant something to think with. */
+private fun brainSteps(): List<TourStep> = listOf(
TourStep(
id = "open_settings",
place = TourPlace.CHAT_DRAWER,
@@ -114,7 +129,11 @@ fun defaultTourSteps(): List<TourStep> = listOf(
body = "Nothing on this page takes effect until you do.",
ackLabel = "Skip",
isDone = { settings, _ -> settings.hasUsableModel }
- ),
+ )
+)
+
+/** The two grants the assistant is crippled without. Both live in Android's own settings. */
+private fun permissionSteps(): List<TourStep> = listOf(
TourStep(
id = "open_permissions",
place = TourPlace.SETTINGS_HOME,
@@ -127,21 +146,27 @@ fun defaultTourSteps(): List<TourStep> = listOf(
place = TourPlace.PERMISSIONS,
title = "Accessibility is the important one",
body = "It's what lets Gotcha read the screen and tap for you. Without it the assistant " +
- "can answer questions but cannot do anything. Turn on Accessibility under System Access.",
- hint = "On some phones this lives under Settings › Additional settings › Accessibility › " +
- "Installed apps.",
+ "can answer questions but cannot do anything. The switch is Android's, not ours.",
+ hint = "Look for Gotcha in the list. On some phones it lives under Settings › " +
+ "Additional settings › Accessibility › Installed apps.",
ackLabel = "Skip for now",
+ action = TourAction("Open Android settings", ToolResult.ACCESSIBILITY_ACCESS),
isDone = { _, context -> isAccessibilityGranted(context) }
),
TourStep(
id = "grant_overlay",
place = TourPlace.PERMISSIONS,
title = "And one for the floating ball",
- body = "\"Display Over Apps\" lets the assistive ball and Screen Lens appear on top of " +
- "other apps.",
+ body = "\"Display over other apps\" lets the assistive ball and Screen Lens appear on " +
+ "top of whatever you're using.",
ackLabel = "Skip for now",
+ action = TourAction("Open Android settings", ToolResult.OVERLAY_ACCESS),
isDone = { _, context -> isOverlayGranted(context) }
- ),
+ )
+)
+
+/** Who the user is, and the way back out into the chat. */
+private fun profileSteps(): List<TourStep> = listOf(
TourStep(
id = "open_personal_info",
place = TourPlace.SETTINGS_HOME,
@@ -153,9 +178,18 @@ fun defaultTourSteps(): List<TourStep> = listOf(
TourStep(
id = "fill_personal_info",
place = TourPlace.PERSONAL_INFO,
+ anchor = TourAnchor.PERSONAL_NAME,
title = "Tell it your name",
- body = "All of this is optional, and all of it makes the answers fit you better. Fill in " +
- "what you like, then save.",
+ body = "Type whatever you'd like to be called. Everything on this page is optional, and " +
+ "all of it makes the answers fit you better.",
+ ackLabel = "Skip"
+ ),
+ TourStep(
+ id = "save_personal_info",
+ place = TourPlace.PERSONAL_INFO,
+ anchor = TourAnchor.PERSONAL_SAVE,
+ title = "Save it",
+ body = "Same as before — nothing on the page counts until you do.",
ackLabel = "Skip",
isDone = { settings, _ -> settings.userName.isNotBlank() }
),
diff --git a/app/src/test/java/com/gotcha/ui/tour/TourControllerTest.kt b/app/src/test/java/com/gotcha/ui/tour/TourControllerTest.kt
index acfc63c..45541f9 100644
--- a/app/src/test/java/com/gotcha/ui/tour/TourControllerTest.kt
+++ b/app/src/test/java/com/gotcha/ui/tour/TourControllerTest.kt
@@ -237,6 +237,41 @@ class TourControllerTest {
assertTrue(defaultTourSteps().isNotEmpty())
}
+ @Test
+ fun `a step that asks the user to type points at the field they type into`() {
+ // A coach mark with no anchor blocks the entire screen, so an
+ // instruction to fill something in is one the user cannot follow.
+ val typing = defaultTourSteps().single { it.id == "fill_personal_info" }
+
+ assertEquals(TourAnchor.PERSONAL_NAME, typing.anchor)
+ }
+
+ @Test
+ fun `the steps that leave for Android settings offer a way to get there`() {
+ val leaving = defaultTourSteps().filter {
+ it.id == "grant_accessibility" || it.id == "grant_overlay"
+ }
+
+ assertEquals(2, leaving.size)
+ leaving.forEach { step ->
+ assertTrue("Step '${step.id}' has no action button", step.action != null)
+ }
+ }
+
+ @Test
+ fun `anything the user must act on is anchored to the control they act on`() {
+ // The rule the whole coach layer rests on: everything but the spotlit
+ // control is covered, so a step demanding an in-app action without an
+ // anchor is a step the scrim makes impossible. Steps that only inform,
+ // or that hand off to another app through `action`, are exempt.
+ val informational = setOf("grant_accessibility", "grant_overlay", "finish")
+ defaultTourSteps()
+ .filterNot { it.id in informational }
+ .forEach { step ->
+ assertTrue("Step '${step.id}' asks for an action it covers up", step.anchor != null)
+ }
+ }
+
@Test
fun `no shipped step can strand the user with no way out`() {
// A step is escapable if it offers a button of its own, or if the next