Files
swiftcontrol/lib/services/overlay/android_overlay_controller.dart
Jonas Bark abf52e4d7a overlay: re-resolve live FitnessBikeDefinition + fix macOS close + recordError
Fixes the user-reported bugs after the auto-show landed:

1. Auto-opened overlay buttons did nothing
   The trainer emulator rebinds a fresh FitnessBikeDefinition each time
   its transport restarts (e.g. when a trainer app connects), so the
   `def` captured at show() time can already be stale by the time the
   user taps + / −. Action handlers now re-resolve through a new
   `LiveDefinitionLookup` callback on every dispatch. Navigation's
   auto-show and OverlaySettingsSection's manual show both pass a
   lookup that reads `device.emulator.activeDefinition` live.

2. Toggling "Show overlay during ride" off didn't close the panel
   The vendored macOS plugin's `closeWindow` handler ignored its args
   and called `closeWindow(NSApp.mainWindow)`, which either terminated
   the app or no-op'd silently. Patched it to honor
   `isMainWindow: false` and close the (single) secondary window.

3. "Error in overlay.position.decode: Unexpected character: light"
   The vendored macOS plugin packs the Dart entrypoint args in the
   order [routeName, theme, argsJson]; the Windows runner uses
   `std::map` iteration order. The sub-window was indexing `args[1]`
   blindly. Now it scans `args[1..]` for the first JSON-looking
   payload with `x`/`y`.

Also:
- TrainerOverlayController.show now takes optional `liveDef`; all
  three implementations (desktop, android, ios) thread it through.
- Navigation initState no longer early-`return`s after auto-show, so
  the post-frame system-UI / changelog logic still runs.
- All silent `catch (_) {}` blocks in the overlay code now forward
  via `recordError(e, s, context: '...')`. New project memory
  entry [feedback_record_error.md] documents the rule.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 12:48:53 +02:00

246 lines
8.5 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:async';
import 'dart:convert';
import 'package:bike_control/main.dart' show recordError;
// Importing the entry point ensures it is compiled into the app binary.
// `@pragma('vm:entry-point')` only prevents tree-shaking once a file is
// compiled; without an import the secondary engine cannot find `overlayMain`
// and Android logs `Width is zero` followed by `Destroying the overlay
// window service`.
// ignore: unused_import
import 'package:bike_control/services/overlay/overlay_entry_point.dart';
import 'package:bike_control/services/overlay/overlay_state.dart';
import 'package:bike_control/services/overlay/trainer_overlay_controller.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart' show MethodChannel;
import 'package:flutter/widgets.dart' show WidgetsBinding;
import 'package:flutter_overlay_window/flutter_overlay_window.dart';
import 'package:prop/emulators/definitions/fitness_bike_definition.dart';
/// Custom MethodChannel that bridges overlay→main on Android. Set up by a
/// Kotlin singleton (OverlayActionBridge) because flutter_overlay_window
/// 0.5.0's own overlay→main path doesn't deliver.
const _overlayActionsChannel = MethodChannel('bike_control/overlay_actions');
class AndroidOverlayController implements TrainerOverlayController {
static const _minPushIntervalMs = 100; // ~10 Hz
final ValueNotifier<bool> _showing = ValueNotifier(false);
FitnessBikeDefinition? _def;
LiveDefinitionLookup? _liveDef;
Listenable? _bound;
Set<OverlayField> _fields = {OverlayField.power, OverlayField.cadence};
Timer? _pushDebounce;
TrainerOverlayState? _lastPushed;
DateTime _lastPushAt = DateTime.fromMillisecondsSinceEpoch(0);
/// `true` once we've registered our MethodCallHandler on the main-side
/// MethodChannel that receives forwarded actions from the overlay engine.
/// One-shot: handler is set and then never replaced; it gates dispatch on
/// `_def != null` so messages received while the overlay is hidden are
/// silently dropped.
bool _mainHandlerInstalled = false;
@override
ValueListenable<bool> get isShowing => _showing;
@override
Future<OverlayShowResult> show(
FitnessBikeDefinition def,
Set<OverlayField> fields, {
LiveDefinitionLookup? liveDef,
}) async {
final granted = await FlutterOverlayWindow.isPermissionGranted();
if (!granted) {
final ok = await FlutterOverlayWindow.requestPermission();
if (ok != true) {
return const OverlayShowResult.fail(OverlayShowFailure.permissionDenied);
}
}
_def = def;
_liveDef = liveDef;
_fields = fields;
_bind();
_installMainHandler();
// flutter_overlay_window 0.5.0 passes width/height through to
// WindowManager.LayoutParams as RAW PIXELS (not dp). Compute the surface
// size from the platform's device pixel ratio so the overlay always
// matches our widget's dp footprint — otherwise the +/- buttons get
// clipped on high-density phones (510 px needed for a 170 dp widget on
// a 3× display).
final dpr = WidgetsBinding.instance.platformDispatcher.views.first.devicePixelRatio;
int dpToPx(double dp) => (dp * dpr).round();
// Android requires a foreground-service notification for SYSTEM_ALERT_WINDOW
// overlays — we can't suppress it entirely. Use visibilitySecret so it
// doesn't appear on the lock screen / heads-up, an empty content line, and
// a minimal title so it sits unobtrusively in the notification shade.
await FlutterOverlayWindow.showOverlay(
enableDrag: true,
overlayTitle: 'BikeControl',
overlayContent: '',
flag: OverlayFlag.defaultFlag,
visibility: NotificationVisibility.visibilitySecret,
positionGravity: PositionGravity.none,
width: dpToPx(270),
height: dpToPx(110),
startPosition: const OverlayPosition(20, 80),
);
// The Kotlin bridge needs to register a handler on the overlay engine,
// which only becomes available in FlutterEngineCache after showOverlay
// creates it. Retry a few times because engine attach is async on some
// OEMs (Oppo / OnePlus) and the cache lookup can race the show call.
unawaited(_installOverlayHandlerWithRetry());
_showing.value = true;
// Send an initial state immediately.
_push(force: true);
return const OverlayShowResult.ok();
}
Future<void> _installOverlayHandlerWithRetry() async {
Object? lastError;
StackTrace? lastStack;
for (var i = 0; i < 10; i++) {
try {
final ok = await _overlayActionsChannel
.invokeMethod<bool>('installOverlayHandler');
if (ok == true) return;
} catch (e, s) {
// Expected to fail until the overlay engine is in the FlutterEngineCache;
// only surface the final attempt's failure.
lastError = e;
lastStack = s;
}
await Future.delayed(const Duration(milliseconds: 100));
}
if (lastError != null) {
recordError(lastError, lastStack,
context: 'overlay.android.installHandler');
}
}
@override
Future<void> hide() async {
_bound?.removeListener(_onChange);
_bound = null;
_def = null;
_liveDef = null;
_pushDebounce?.cancel();
_pushDebounce = null;
_lastPushed = null;
if (await FlutterOverlayWindow.isActive()) {
await FlutterOverlayWindow.closeOverlay();
}
// Tear the overlay-side Kotlin handler down too; the engine itself is
// disposed by the package, but our handler reference can leak if we
// forget to clear it.
try {
await _overlayActionsChannel.invokeMethod('uninstallOverlayHandler');
} catch (e, s) {
recordError(e, s, context: 'overlay.android.uninstallHandler');
}
_showing.value = false;
}
/// Set up the main-side MethodCallHandler (idempotent). The Kotlin bridge
/// invokes "action" on this channel whenever the overlay isolate calls
/// `MethodChannel('bike_control/overlay_actions').invokeMethod('push', ...)`.
void _installMainHandler() {
if (_mainHandlerInstalled) return;
_mainHandlerInstalled = true;
_overlayActionsChannel.setMethodCallHandler((call) async {
if (call.method != 'action') return null;
// Re-resolve the live FitnessBikeDefinition each call — see comment
// on LiveDefinitionLookup; the trainer emulator rebinds a fresh def
// on every transport restart.
final live = _liveDef?.call() ?? _def;
if (live == null) return null; // overlay hidden — drop
final action = call.arguments;
if (action is! String) return null;
switch (action) {
case 'primaryDecrement':
_adjustPrimary(live, increment: false);
break;
case 'primaryIncrement':
_adjustPrimary(live, increment: true);
break;
}
return null;
});
}
void _adjustPrimary(FitnessBikeDefinition def, {required bool increment}) {
if (def.trainerMode.value == TrainerMode.ergMode) {
final current = def.ergTargetPower.value ?? 150;
final next = (current + (increment ? 5 : -5)).clamp(0, 500);
def.setManualErgPower(next);
} else {
if (increment) {
def.shiftUp();
} else {
def.shiftDown();
}
}
}
@override
void updateFields(Set<OverlayField> fields) {
_fields = fields;
_push(force: true);
}
void _bind() {
final def = _def;
if (def == null) return;
_bound?.removeListener(_onChange);
_bound = Listenable.merge([
def.currentGear,
def.gearRatio,
def.trainerMode,
def.powerW,
def.cadenceRpm,
def.ergTargetPower,
]);
_bound!.addListener(_onChange);
}
void _onChange() {
final now = DateTime.now();
final elapsed = now.difference(_lastPushAt).inMilliseconds;
if (elapsed >= _minPushIntervalMs) {
_push();
return;
}
_pushDebounce ??= Timer(
Duration(milliseconds: _minPushIntervalMs - elapsed),
() {
_pushDebounce = null;
_push();
},
);
}
Future<void> _push({bool force = false}) async {
final def = _def;
if (def == null) return;
final s = TrainerOverlayState(
gear: def.currentGear.value,
maxGear: def.maxGear,
gearRatio: def.gearRatio.value,
mode: def.trainerMode.value,
powerW: def.powerW.value,
cadenceRpm: def.cadenceRpm.value,
ergTargetW: def.ergTargetPower.value,
fields: _fields,
);
if (!force && s == _lastPushed) return;
_lastPushed = s;
_lastPushAt = DateTime.now();
await FlutterOverlayWindow.shareData(jsonEncode(s.toJson()));
}
}