T12-T14: platform liveness via geolocator; drop flutter_foreground_task
geolocator_android's ForegroundNotificationConfig raises a service with foregroundServiceType=location and holds a wake lock for as long as the position stream is subscribed, and the plugin declares that service in its own manifest. That covers everything the native TrackingService used a foreground service and a PARTIAL_WAKE_LOCK for, so flutter_foreground_task is gone -- and with it both deprecation warnings that were recorded under T01. Known parity gap for T27: geolocator's notification cannot carry actions, so the native Pause/Resume buttons in the shade are not reproduced. iOS: UIBackgroundModes plus allowBackgroundLocationUpdates keep the isolate and the writer timer alive while updates flow. Two settings matter more than they look -- pauseLocationUpdatesAutomatically is false because CoreLocation does not reliably restart, and activityType is otherNavigation rather than automotiveNavigation, which would snap fixes to the road network and silently falsify the recorded path. Usage strings are specific, not generic. Android deliberately does not request ACCESS_BACKGROUND_LOCATION: a foreground service with a visible notification is exactly the supported case, and asking would trigger the harder "Allow all the time" flow for no benefit. T14 process-death resume is implemented and called at startup, covered by four tests including the 111 km crash-gap guard. Verified by building both platforms and launching on the iOS simulator: Drift opened, Riverpod resolved, restore found no active trip, controls correct. lib/main.dart is a harness so the pipeline can be exercised on hardware before Phase 4 builds screens; T15 replaces it. Phase 3 done: 145 tests passing, analyze clean. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
164
lib/src/recording/geolocator_location_source.dart
Normal file
164
lib/src/recording/geolocator_location_source.dart
Normal file
@@ -0,0 +1,164 @@
|
||||
/// The real [LocationSource], backed by `geolocator`.
|
||||
///
|
||||
/// This is the one file in the app that knows Android and iOS keep a process alive by
|
||||
/// different mechanisms. Everything above it — the engine, the repository, the UI — is
|
||||
/// platform-agnostic.
|
||||
///
|
||||
/// ## Android: geolocator raises the foreground service
|
||||
///
|
||||
/// `ForegroundNotificationConfig` starts a service with
|
||||
/// `foregroundServiceType="location"` for as long as the position stream is subscribed,
|
||||
/// and holds a wake lock. That is everything the native `TrackingService` used a
|
||||
/// foreground service and a `PARTIAL_WAKE_LOCK` for, which is why
|
||||
/// `flutter_foreground_task` was dropped — along with its two deprecation warnings (no
|
||||
/// Swift Package Manager support, and it applies KGP).
|
||||
///
|
||||
/// **Known parity gap:** this notification cannot carry *actions*, so the native app's
|
||||
/// Pause/Resume buttons in the shade are not reproduced. Tapping the notification opens
|
||||
/// the app instead. Recorded in `docs/port/PROGRESS.md` for the T27 parity audit.
|
||||
///
|
||||
/// ## iOS: the app stays awake only while location flows
|
||||
///
|
||||
/// There is no foreground service. `allowBackgroundLocationUpdates` plus
|
||||
/// `UIBackgroundModes: [location]` keeps the process — and therefore the Dart isolate and
|
||||
/// the writer timer — running while updates are being delivered.
|
||||
///
|
||||
/// The danger is the OS suspending a *stationary* app. Two settings push back:
|
||||
///
|
||||
/// - `pauseLocationUpdatesAutomatically: false` — otherwise CoreLocation decides on its
|
||||
/// own that the ride has ended, and it does not reliably restart.
|
||||
/// - `activityType: otherNavigation` — **not** `automotiveNavigation`, which makes
|
||||
/// CoreLocation snap fixes to the road network. This app records where the rider
|
||||
/// actually went; a snapped path is a different (and wrong) artefact.
|
||||
///
|
||||
/// Whether this is sufficient can only be settled by a real ride — see T13 in
|
||||
/// `docs/port/PLAN.md` and the checklist in the native repo's `docs/TESTING.md`.
|
||||
library;
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io' show Platform;
|
||||
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
|
||||
import 'location_source.dart';
|
||||
|
||||
/// Matches the native `LocationRequest`: high accuracy, 1 s nominal interval, no
|
||||
/// distance filter (a stationary bike must still produce fixes so elapsed time and the
|
||||
/// noise floor behave).
|
||||
const Duration _interval = Duration(seconds: 1);
|
||||
|
||||
class GeolocatorLocationSource implements LocationSource {
|
||||
GeolocatorLocationSource({
|
||||
this.notificationTitle = 'Rippr is recording',
|
||||
this.notificationText = 'Tracking your ride',
|
||||
});
|
||||
|
||||
final String notificationTitle;
|
||||
final String notificationText;
|
||||
|
||||
final _controller = StreamController<LocationFix>.broadcast();
|
||||
StreamSubscription<Position>? _subscription;
|
||||
|
||||
@override
|
||||
Stream<LocationFix> get fixes => _controller.stream;
|
||||
|
||||
@override
|
||||
Future<void> start() async {
|
||||
if (_subscription != null) return; // idempotent
|
||||
|
||||
await _ensurePermission();
|
||||
|
||||
_subscription = Geolocator.getPositionStream(
|
||||
locationSettings: _settings(),
|
||||
).listen(
|
||||
(p) => _controller.add(_toFix(p)),
|
||||
// A transient platform error must not close the stream: the engine treats a closed
|
||||
// stream as "recording stopped", which is a user-visible event.
|
||||
onError: (Object _) {},
|
||||
cancelOnError: false,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> stop() async {
|
||||
await _subscription?.cancel();
|
||||
_subscription = null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> dispose() async {
|
||||
await stop();
|
||||
await _controller.close();
|
||||
}
|
||||
|
||||
LocationSettings _settings() {
|
||||
if (Platform.isAndroid) {
|
||||
return AndroidSettings(
|
||||
accuracy: LocationAccuracy.best,
|
||||
distanceFilter: 0,
|
||||
intervalDuration: _interval,
|
||||
foregroundNotificationConfig: ForegroundNotificationConfig(
|
||||
notificationTitle: notificationTitle,
|
||||
notificationText: notificationText,
|
||||
notificationChannelName: 'Ride recording',
|
||||
enableWakeLock: true,
|
||||
setOngoing: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (Platform.isIOS) {
|
||||
return AppleSettings(
|
||||
accuracy: LocationAccuracy.best,
|
||||
distanceFilter: 0,
|
||||
allowBackgroundLocationUpdates: true,
|
||||
// Never let CoreLocation decide the ride is over. It does not reliably restart.
|
||||
pauseLocationUpdatesAutomatically: false,
|
||||
// NOT automotiveNavigation: that snaps fixes to roads, and this app records
|
||||
// where the rider actually went.
|
||||
activityType: ActivityType.otherNavigation,
|
||||
// The blue status bar while recording in the background. Honest, and reviewers
|
||||
// look for it.
|
||||
showBackgroundLocationIndicator: true,
|
||||
);
|
||||
}
|
||||
return const LocationSettings(
|
||||
accuracy: LocationAccuracy.best,
|
||||
distanceFilter: 0,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _ensurePermission() async {
|
||||
if (!await Geolocator.isLocationServiceEnabled()) {
|
||||
throw const LocationException(
|
||||
LocationFailure.serviceDisabled,
|
||||
'Location services are switched off',
|
||||
);
|
||||
}
|
||||
|
||||
var permission = await Geolocator.checkPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
permission = await Geolocator.requestPermission();
|
||||
}
|
||||
|
||||
switch (permission) {
|
||||
case LocationPermission.denied:
|
||||
throw const LocationException(LocationFailure.permissionDenied);
|
||||
case LocationPermission.deniedForever:
|
||||
throw const LocationException(LocationFailure.permissionDeniedForever);
|
||||
case LocationPermission.whileInUse:
|
||||
case LocationPermission.always:
|
||||
case LocationPermission.unableToDetermine:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
LocationFix _toFix(Position p) => LocationFix(
|
||||
timestamp: p.timestamp.millisecondsSinceEpoch,
|
||||
latitude: p.latitude,
|
||||
longitude: p.longitude,
|
||||
speedMps: p.speed,
|
||||
altitudeM: p.altitude,
|
||||
accuracyM: p.accuracy,
|
||||
bearingDeg: p.heading,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user