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): link to app Settings when camera access is off

tonyd88777d72882
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 821b1a428..e914c5456 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
@@ -1,8 +1,10 @@
package ai.autonomous.harness.android
+import android.content.ActivityNotFoundException
import android.content.ClipboardManager
import android.content.ContentResolver
import android.content.Context
+import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
@@ -57,15 +59,36 @@ class MainActivity : FlutterActivity() {
}.start()
}
- // `harness/app_task` — "Exit" in the question back asks at the app's root (Dart:
+ // `harness/app_task` — what the app asks of its own activity.
+ //
+ // `moveToBack`: "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`.
+ //
+ // `openSettings`: this app's App info screen, where a camera permission refused for good is
+ // turned back on (Dart: `lib/core/app_settings.dart`). False when no activity takes the intent.
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "harness/app_task")
.setMethodCallHandler { call, result ->
- if (call.method != "moveToBack") { result.notImplemented(); return@setMethodCallHandler }
- result.success(moveTaskToBack(true))
+ when (call.method) {
+ "moveToBack" -> result.success(moveTaskToBack(true))
+ "openSettings" -> {
+ val intent = Intent(
+ Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
+ Uri.fromParts("package", packageName, null),
+ )
+ result.success(
+ try {
+ startActivity(intent)
+ true
+ } catch (e: ActivityNotFoundException) {
+ false
+ }
+ )
+ }
+ else -> result.notImplemented()
+ }
}
}
diff --git a/mobile/lib/core/app_settings.dart b/mobile/lib/core/app_settings.dart
new file mode 100644
index 000000000..7b0f00ddd
--- /dev/null
+++ b/mobile/lib/core/app_settings.dart
@@ -0,0 +1,30 @@
+import 'dart:io' show Platform;
+
+import 'package:flutter/services.dart';
+import 'package:url_launcher/url_launcher.dart';
+
+/// Opens this app's own page in the phone's Settings — where a permission refused once is turned
+/// back on, since the app may not ask again.
+///
+/// iOS: `app-settings:` (`UIApplication.openSettingsURLString`) through url_launcher, which lands
+/// on Settings ▸ Harness with its switches. Android: App info, over `harness/app_task` (android
+/// `MainActivity.kt`) — no URL reaches that screen, so url_launcher cannot; the permissions are
+/// one tap further in, under Permissions.
+///
+/// False when nothing opened: another platform, or the OS refused.
+Future<bool> openAppSettings() async {
+ try {
+ if (Platform.isIOS) return await launchUrl(Uri.parse('app-settings:'));
+ if (Platform.isAndroid) {
+ return await _appTask.invokeMethod<bool>('openSettings') ?? false;
+ }
+ } on MissingPluginException {
+ return false;
+ } catch (_) {
+ return false;
+ }
+ return false;
+}
+
+/// The same channel `phone/exit_app.dart` sends the app to the background over.
+const MethodChannel _appTask = MethodChannel('harness/app_task');
diff --git a/mobile/lib/phone/welcome/scan_to_connect.dart b/mobile/lib/phone/welcome/scan_to_connect.dart
index 11cf9e785..5eb410f00 100644
--- a/mobile/lib/phone/welcome/scan_to_connect.dart
+++ b/mobile/lib/phone/welcome/scan_to_connect.dart
@@ -1,8 +1,12 @@
+import 'dart:async';
+import 'dart:io' show Platform;
+
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:lucide_icons_flutter/lucide_icons.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
+import 'package:harness_mobile/core/app_settings.dart';
import 'package:harness_mobile/shared/theme/app_theme.dart';
import '../phone_navigation.dart' show phoneRoute;
@@ -26,7 +30,9 @@ import 'connect_code.dart';
/// ```
///
/// The camera runs only while this page is up. A code that is not ours is ignored, so a stray QR
-/// in frame does nothing; no camera (refused, or none) says so and leaves email as the way in.
+/// in frame does nothing. A camera refused says where to turn it on, with a link to this app's
+/// Settings, and the camera comes up on the way back; no camera at all says so. Either way the
+/// button under the square is the way in without one.
class ScanToConnectPage extends StatefulWidget {
const ScanToConnectPage({
super.key,
@@ -56,10 +62,93 @@ class ScanToConnectPage extends StatefulWidget {
State<ScanToConnectPage> createState() => _ScanToConnectPageState();
}
-class _ScanToConnectPageState extends State<ScanToConnectPage> {
+class _ScanToConnectPageState extends State<ScanToConnectPage>
+ with WidgetsBindingObserver {
/// Set once a code of ours is read: the camera keeps reporting it every frame.
bool _done = false;
+ /// The camera, held here rather than left to [MobileScanner] — null when [ScanToConnectPage.camera]
+ /// stands in for it.
+ ///
+ /// ⚠️ **Held so that a refused camera can be asked again.** The widget's own controller follows
+ /// the app's lifecycle only while it HAS the permission (`_MobileScannerState
+ /// .didChangeAppLifecycleState`), so someone who turned the camera on in Settings came back to the
+ /// same "access is off" and had to leave the page and return. A start on this same controller
+ /// retries instead, and a start that succeeds clears the error it replaces.
+ ///
+ /// Holding it makes the lifecycle this page's to follow — see [didChangeAppLifecycleState].
+ MobileScannerController? _scanner;
+
+ /// "Open Settings" took the person out of the app: the camera is asked again when they return.
+ ///
+ /// ⚠️ **Only then, never on every return.** A start on Android asks for the permission again if
+ /// the OS still lets it, and the first refusal's own dialog brings the app back to the foreground
+ /// — a retry on that would put the dialog straight back in front of someone who just said no.
+ bool _awaitingSettings = false;
+
+ /// The camera was running when the app went inactive, and was stopped for it.
+ bool _stoppedForLifecycle = false;
+
+ @override
+ void initState() {
+ super.initState();
+ if (widget.camera == null) {
+ _scanner = MobileScannerController();
+ WidgetsBinding.instance.addObserver(this);
+ }
+ }
+
+ @override
+ void dispose() {
+ if (_scanner case final scanner?) {
+ WidgetsBinding.instance.removeObserver(this);
+ unawaited(scanner.dispose());
+ }
+ super.dispose();
+ }
+
+ /// What [MobileScanner] does for a controller of its own: stop while the app is not in front,
+ /// start again when it is — and here, also after a trip to Settings.
+ @override
+ void didChangeAppLifecycleState(AppLifecycleState state) {
+ final scanner = _scanner;
+ if (scanner == null) return;
+ switch (state) {
+ case AppLifecycleState.inactive:
+ if (!scanner.value.isRunning) return;
+ _stoppedForLifecycle = true;
+ unawaited(scanner.stop());
+ case AppLifecycleState.resumed:
+ if (!_stoppedForLifecycle && !_awaitingSettings) return;
+ _stoppedForLifecycle = false;
+ _awaitingSettings = false;
+ unawaited(_start(scanner));
+ case AppLifecycleState.detached:
+ case AppLifecycleState.hidden:
+ case AppLifecycleState.paused:
+ return;
+ }
+ }
+
+ Future<void> _start(MobileScannerController scanner) async {
+ // Still starting — the permission prompt is up — or already running: nothing to ask.
+ if (scanner.value.isStarting || scanner.value.isRunning) return;
+ try {
+ await scanner.start();
+ } on MobileScannerException {
+ // A start that fails on the camera itself lands in the controller's state, and the error
+ // below draws it; this is a start refused before reaching the camera (disposed, not attached)
+ // — the page is going away.
+ }
+ }
+
+ /// Raised BEFORE Settings opens, not on its answer: iOS may answer only once the app is back in
+ /// front, after the return this flag exists to catch.
+ Future<void> _openSettings() async {
+ _awaitingSettings = true;
+ if (!await openAppSettings()) _awaitingSettings = false;
+ }
+
void _onDetect(BarcodeCapture capture) {
if (_done) return;
for (final barcode in capture.barcodes) {
@@ -98,16 +187,11 @@ class _ScanToConnectPageState extends State<ScanToConnectPage> {
child:
widget.camera ??
MobileScanner(
+ controller: _scanner,
onDetect: _onDetect,
- errorBuilder: (context, error) => Center(
- child: Padding(
- padding: const EdgeInsets.all(24),
- child: TtyText(
- 'No camera. Allow it in Settings, or use your email.',
- color: tty.faint,
- size: TtySize.meta,
- ),
- ),
+ errorBuilder: (context, error) => _CameraProblem(
+ code: error.errorCode,
+ onOpenSettings: () => unawaited(_openSettings()),
),
),
),
@@ -167,6 +251,79 @@ class _ScanToConnectPageState extends State<ScanToConnectPage> {
}
}
+/// What stands in the camera's square when it cannot run: what happened, and what fixes it.
+///
+/// ⚠️ **A refused permission and a missing camera are told apart**, because only one of them is
+/// fixed in Settings: a link to Settings on a simulator or a camera-less tablet leads nowhere. The
+/// way round either — email, the computer's password, "Not now" — is the page's own button under
+/// the square, so none of these names it: the same square stands on all three pages.
+class _CameraProblem extends StatelessWidget {
+ const _CameraProblem({required this.code, required this.onOpenSettings});
+
+ final MobileScannerErrorCode code;
+ final VoidCallback onOpenSettings;
+
+ @override
+ Widget build(BuildContext context) {
+ final tty = Tty.of(context);
+ final refused = code == MobileScannerErrorCode.permissionDenied;
+ final (title, detail) = switch (code) {
+ MobileScannerErrorCode.permissionDenied => (
+ 'Camera access is off',
+ // Where the switch is, in each OS's words: iOS opens straight onto it; Android opens App
+ // info, one screen short of it.
+ Platform.isAndroid
+ ? 'Allow Camera for Harness in Settings, under Permissions › Camera.'
+ : 'Turn on Camera for Harness in Settings to scan the code.',
+ ),
+ MobileScannerErrorCode.unsupported => (
+ 'No camera on this device',
+ 'Scanning needs a camera. Use the option below instead.',
+ ),
+ _ => (
+ 'The camera didn’t start',
+ 'Go back and try again, or use the option below.',
+ ),
+ };
+ // Scrolls rather than overflows: the square shrinks with a small phone, the words grow with the
+ // UI size.
+ return Center(
+ child: SingleChildScrollView(
+ padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 20),
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Icon(LucideIcons.cameraOff300, size: 28, color: tty.faint),
+ const SizedBox(height: 14),
+ Text(
+ title,
+ textAlign: TextAlign.center,
+ style: tty.style(size: TtySize.row, weight: FontWeight.w600),
+ ),
+ const SizedBox(height: 6),
+ Text(
+ detail,
+ textAlign: TextAlign.center,
+ style: tty
+ .style(size: TtySize.meta, color: tty.faint)
+ .copyWith(height: 1.5),
+ ),
+ if (refused) ...[
+ const SizedBox(height: 6),
+ TtyTextButton(
+ label: 'Open Settings ›',
+ color: tty.green,
+ weight: FontWeight.w600,
+ onPressed: onOpenSettings,
+ ),
+ ],
+ ],
+ ),
+ ),
+ );
+ }
+}
+
/// The camera over the current page, and the code it read — null when the person went back or
/// took the other way ([fallbackLabel]). For a phone that is already signed in and wants a
/// computer: unlocking one, or pairing with one just set up.