Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(mobile): confirm before Android back exits the app

tonybb1e128a95cb
diff --git a/mobile/android/app/src/main/kotlin/ai/autonomous/harness/android/MainActivity.kt b/mobile/android/app/src/main/kotlin/ai/autonomous/harness/android/MainActivity.kt
index eb801adb1..821b1a428 100644
--- a/mobile/android/app/src/main/kotlin/ai/autonomous/harness/android/MainActivity.kt
+++ b/mobile/android/app/src/main/kotlin/ai/autonomous/harness/android/MainActivity.kt
@@ -56,6 +56,17 @@ class MainActivity : FlutterActivity() {
main.post { result.success(png) }
}.start()
}
+
+ // `harness/app_task` — "Exit" in the question back asks at the app's root (Dart:
+ // `lib/phone/exit_app.dart`). The task goes to the background, as the system's own back does at
+ // a launcher activity since Android 12, rather than being finished: the engine and its sockets
+ // stay up, and opening the app again lands where it was. False when the task did not move, and
+ // Dart then falls back to `SystemNavigator.pop`.
+ MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "harness/app_task")
+ .setMethodCallHandler { call, result ->
+ if (call.method != "moveToBack") { result.notImplemented(); return@setMethodCallHandler }
+ result.success(moveTaskToBack(true))
+ }
}
/** The first image on the clipboard, or null. The description is checked before any item, so a
diff --git a/mobile/lib/phone/exit_app.dart b/mobile/lib/phone/exit_app.dart
new file mode 100644
index 000000000..ec37b1aee
--- /dev/null
+++ b/mobile/lib/phone/exit_app.dart
@@ -0,0 +1,71 @@
+import 'dart:io' show Platform;
+
+import 'package:flutter/services.dart';
+import 'package:flutter/widgets.dart';
+import 'package:lucide_icons_flutter/lucide_icons.dart';
+
+import 'phone_sheet.dart' show confirmPhoneAction;
+
+/// Whether back at the root of the app asks before leaving it.
+///
+/// Android only. iOS has no back button, and its edge swipe has nothing to go back to at a root —
+/// so a root that refused to pop there would change nothing anyone could see, and it is left alone.
+///
+/// ⚠️ **A root that asks has to refuse the pop, and refusing costs the predictive-back peek.**
+/// Android 13+ animates the app shrinking towards the home screen while back is held — but only
+/// when the SYSTEM owns the press, which it does exactly when the root's `PopScope` lets it go.
+/// A root that answers with a question has to keep the press, so the peek is gone at the root; it
+/// still plays on every page above it.
+final bool confirmsExitOnBack = Platform.isAndroid;
+
+/// Android's back at the app's root — the terminal, or the first welcome screen signed out: asks
+/// first, then sends the app to the background.
+///
+/// Every other back is spent before this is reached: the keyboard (the IME takes the press before
+/// Flutter sees it), a sheet or dialog (on the root navigator, above the page that asks), Find over
+/// the terminal, and a pushed page. Back while the question is up is its Cancel — the dialog is the
+/// root navigator's top route, so the press pops it and never reaches the page that asked.
+///
+/// ⚠️ **"Exit" backgrounds the app rather than finishing it**, which is also what Android's own
+/// back does at a launcher activity since Android 12. The engine stays up, so the machines'
+/// sockets and the "agent finished" notices that ride them (`notify/system_notices.dart`) go on
+/// working until the OS reclaims the process, and opening the app again lands where it was instead
+/// of on a cold start that reconnects every machine. The agents themselves run on the computers
+/// either way.
+Future<void> confirmExitApp(BuildContext context) async {
+ // One question at a time. The dialog is pushed synchronously, so a second press lands on it
+ // rather than here — this only closes the gap should that ever stop being true.
+ if (_asking) return;
+ _asking = true;
+ final bool confirmed;
+ try {
+ confirmed = await confirmPhoneAction(
+ context,
+ icon: LucideIcons.logOut300,
+ title: 'Exit Harness?',
+ message: 'Your harnesses keep running on your computers.',
+ confirmLabel: 'Exit',
+ );
+ } finally {
+ _asking = false;
+ }
+ if (!confirmed) return;
+ await _moveToBack();
+}
+
+bool _asking = false;
+
+/// `harness/app_task` (android `MainActivity.kt`).
+const MethodChannel _appTask = MethodChannel('harness/app_task');
+
+/// The task to the background; failing that, Flutter's own way out, so "Exit" never does nothing.
+Future<void> _moveToBack() async {
+ try {
+ if (await _appTask.invokeMethod<bool>('moveToBack') ?? false) return;
+ } on MissingPluginException {
+ // No native handler — fall through.
+ } catch (_) {
+ // The channel failed — fall through.
+ }
+ await SystemNavigator.pop();
+}
diff --git a/mobile/lib/phone/phone_shell.dart b/mobile/lib/phone/phone_shell.dart
index 247a1263d..04432dcaf 100644
--- a/mobile/lib/phone/phone_shell.dart
+++ b/mobile/lib/phone/phone_shell.dart
@@ -1,9 +1,12 @@
+import 'dart:async';
+
import 'package:flutter/material.dart';
import 'package:harness_mobile/state/app_state.dart';
import '../p2p/phone_terminal_p2p.dart';
import 'agent_home.dart';
+import 'exit_app.dart';
import 'phone_shell_scope.dart';
/// The signed-in phone app: one page stack, rooted in [AgentHome] — the terminal the phone opens
@@ -149,7 +152,7 @@ class _PhoneShellState extends State<PhoneShell> with WidgetsBindingObserver {
/// below, or `canPop` would answer with whatever was true when the shell last happened to build.
bool _canPop = false;
- /// Android's back button: the stack's own pages first, then the system.
+ /// Android's back button: the stack's own pages first, then the question before leaving the app.
///
/// The notification is used only as a SIGNAL that the stack moved, and the answer is then read
/// from the navigator itself.
@@ -174,9 +177,20 @@ class _PhoneShellState extends State<PhoneShell> with WidgetsBindingObserver {
void _handleBack(bool didPop, Object? result) {
if (didPop) return;
- _navigator.currentState?.maybePop().then((_) {
- if (mounted) _syncCanPop();
- });
+ unawaited(_back());
+ }
+
+ /// The stack's own pages first; what is left is leaving the app, which Android asks about.
+ ///
+ /// ⚠️ **The stack is ASKED, rather than judged by its `canPop`.** `maybePop` answers false only
+ /// when the root has nothing to close. `canPop` is false at the root even while something there
+ /// holds the press itself — Find over the terminal, whose own [PopScope] steps out of it — and
+ /// back used to leave the app from under an open Find because of it.
+ Future<void> _back() async {
+ final handled = await _navigator.currentState?.maybePop() ?? false;
+ if (!mounted) return;
+ _syncCanPop();
+ if (!handled && confirmsExitOnBack) await confirmExitApp(context);
}
@override
@@ -187,8 +201,10 @@ class _PhoneShellState extends State<PhoneShell> with WidgetsBindingObserver {
listenable: widget.notifier,
builder: (context, _) => PopScope<Object?>(
// False while the stack has somewhere to go back to, so the gesture reaches [_handleBack]
- // instead of leaving the app. True at its root: the press then belongs to the system.
- canPop: !_canPop,
+ // instead of leaving the app. True at its root: the press then belongs to the system —
+ // except on Android, where the root asks before the app is left ([confirmExitApp]), so
+ // every press is the shell's.
+ canPop: !confirmsExitOnBack && !_canPop,
onPopInvokedWithResult: _handleBack,
child: NotificationListener<NavigationNotification>(
onNotification: _onNavigation,
diff --git a/mobile/lib/phone/welcome/phone_welcome.dart b/mobile/lib/phone/welcome/phone_welcome.dart
index 48de26e64..4a4253f89 100644
--- a/mobile/lib/phone/welcome/phone_welcome.dart
+++ b/mobile/lib/phone/welcome/phone_welcome.dart
@@ -7,6 +7,7 @@ import 'package:lucide_icons_flutter/lucide_icons.dart';
import 'package:harness_mobile/shared/theme/app_theme.dart';
import 'package:harness_mobile/state/app_state.dart';
+import '../exit_app.dart';
import '../tty.dart';
import '../tty_controls.dart';
import 'connect_code.dart';
@@ -215,9 +216,15 @@ class _PhoneWelcomeState extends State<PhoneWelcome> {
final tty = Tty.of(context);
final signingIn = widget.notifier.signingIn;
return PopScope(
- canPop: _step == _Step.hello,
+ // The first screen lets the press go to the system — except on Android, which asks before
+ // the app is left ([confirmExitApp]) and so keeps it here too.
+ canPop: !confirmsExitOnBack && _step == _Step.hello,
onPopInvokedWithResult: (didPop, _) {
if (didPop) return;
+ if (_step == _Step.hello) {
+ unawaited(confirmExitApp(context));
+ return;
+ }
_go(_step == _Step.code ? _Step.email : _Step.hello);
},
child: Scaffold(