workout_app: drop geolocator for LocationManager

geolocator_android links non-free play-services-location (plus -base,
-basement, -tasks). LocationManagerPositionSource reads fixes through
kuhyx/utils platform_location (AOSP LocationManager, also registers in
the run-service isolate). Release APK: 0 com/google/android/gms
classes (was 15). Emulator: 2 s GPS fixes with the screen off,
location-off and denied-forever states.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Krzysztof Rudnicki 2026-11-20 23:33:40 +01:00
parent 9c0af63547
commit 2e58e734ef
12 changed files with 183 additions and 272 deletions

View File

@ -10,7 +10,7 @@ import 'dart:math' as math;
import 'package:workout_app/sandbox/sandbox.dart';
import 'package:workout_app/services/gps_filter.dart';
import 'package:workout_app/services/position_source.dart';
import 'package:workout_app/services/position_source_geolocator.dart';
import 'package:workout_app/services/position_source_location_manager.dart';
import 'package:workout_app/services/position_source_scripted.dart';
/// Length of the synthetic loop in metres.
@ -60,11 +60,11 @@ PositionSource sandboxRunSource({DateTime? now}) => ScriptedPositionSource(
/// The source a recording uses: the replay in the sandbox, the phone's GPS
/// otherwise.
PositionSource positionSourceFor() =>
Sandbox.enabled ? sandboxRunSource() : GeolocatorPositionSource();
Sandbox.enabled ? sandboxRunSource() : LocationManagerPositionSource();
/// The source the idle map watches: in the sandbox just the replay's first
/// fix (so the marker sits at the loop's start and does not walk the lap
/// before the run), the phone's GPS otherwise.
PositionSource idlePositionSourceFor({DateTime? now}) => Sandbox.enabled
? ScriptedPositionSource([sandboxLoopFixes(now ?? DateTime.now()).first])
: GeolocatorPositionSource();
: LocationManagerPositionSource();

View File

@ -2,7 +2,7 @@
///
/// Everything above this interface (recorder, map, sandbox replay) is pure
/// Dart and runs on the Linux test host and the desktop target; only
/// `GeolocatorPositionSource` touches the geolocator plugin.
/// `LocationManagerPositionSource` touches the platform_location plugin.
library;
import 'package:workout_app/services/gps_filter.dart';

View File

@ -1,68 +0,0 @@
/// The real [PositionSource], backed by the geolocator plugin.
library;
import 'package:geolocator/geolocator.dart';
import 'package:workout_app/services/gps_filter.dart';
import 'package:workout_app/services/position_source.dart';
/// Reads fixes through [GeolocatorPlatform.instance], so a test can swap the
/// platform and drive this class without a device.
class GeolocatorPositionSource implements PositionSource {
/// Creates the source; [forceLocationManager] skips Google Play services.
GeolocatorPositionSource({this.forceLocationManager = false});
/// Android: use the legacy LocationManager instead of the fused provider.
final bool forceLocationManager;
GeolocatorPlatform get _platform => GeolocatorPlatform.instance;
@override
Future<LocationAccess> requestAccess() async {
if (!await _platform.isLocationServiceEnabled()) {
return LocationAccess.serviceOff;
}
var permission = await _platform.checkPermission();
if (permission == LocationPermission.denied) {
permission = await _platform.requestPermission();
}
return _access(permission);
}
@override
Future<LocationAccess> checkAccess() async {
if (!await _platform.isLocationServiceEnabled()) {
return LocationAccess.serviceOff;
}
return _access(await _platform.checkPermission());
}
static LocationAccess _access(LocationPermission permission) =>
switch (permission) {
LocationPermission.always ||
LocationPermission.whileInUse => LocationAccess.granted,
LocationPermission.deniedForever => LocationAccess.deniedForever,
LocationPermission.denied ||
LocationPermission.unableToDetermine => LocationAccess.denied,
};
@override
Stream<GpsFix> fixes({Duration interval = const Duration(seconds: 2)}) =>
_platform
.getPositionStream(
locationSettings: AndroidSettings(
accuracy: LocationAccuracy.bestForNavigation,
intervalDuration: interval,
forceLocationManager: forceLocationManager,
),
)
.map(
(p) => GpsFix(
time: p.timestamp,
latitude: p.latitude,
longitude: p.longitude,
altitudeM: p.altitude,
accuracyM: p.accuracy,
isMocked: p.isMocked,
),
);
}

View File

@ -0,0 +1,57 @@
/// The real [PositionSource], backed by the platform_location plugin
/// (AOSP LocationManager; no Google Play services in the APK).
library;
import 'package:platform_location/platform_location.dart';
import 'package:workout_app/services/gps_filter.dart';
import 'package:workout_app/services/position_source.dart';
/// Reads fixes through [PlatformLocation.instance], so a test can swap the
/// platform and drive this class without a device.
class LocationManagerPositionSource implements PositionSource {
PlatformLocation get _platform => PlatformLocation.instance;
@override
Future<LocationAccess> requestAccess() async {
if (!await _platform.isLocationEnabled()) {
return LocationAccess.serviceOff;
}
var permission = await _platform.checkPermission();
if (permission != LocationPermissionState.granted) {
// Asked again even after a refusal: only the request itself can tell
// Android's "don't ask again" (deniedForever) apart from "denied".
permission = await _platform.requestPermission();
}
return _access(permission);
}
@override
Future<LocationAccess> checkAccess() async {
if (!await _platform.isLocationEnabled()) {
return LocationAccess.serviceOff;
}
return _access(await _platform.checkPermission());
}
static LocationAccess _access(LocationPermissionState permission) =>
switch (permission) {
LocationPermissionState.granted => LocationAccess.granted,
LocationPermissionState.deniedForever => LocationAccess.deniedForever,
LocationPermissionState.denied => LocationAccess.denied,
};
@override
Stream<GpsFix> fixes({Duration interval = const Duration(seconds: 2)}) =>
_platform
.fixes(interval: interval)
.map(
(p) => GpsFix(
time: p.time,
latitude: p.latitude,
longitude: p.longitude,
altitudeM: p.altitude,
accuracyM: p.accuracy,
isMocked: p.isMock,
),
);
}

View File

@ -18,7 +18,7 @@ import 'package:workout_app/sandbox/sandbox_run_replay.dart';
import 'package:workout_app/services/feedback_cues.dart';
import 'package:workout_app/services/gps_filter.dart';
import 'package:workout_app/services/position_source.dart';
import 'package:workout_app/services/position_source_geolocator.dart';
import 'package:workout_app/services/position_source_location_manager.dart';
import 'package:workout_app/services/run_checkpoint_location.dart';
import 'package:workout_app/services/run_engine_recorder.dart';
import 'package:workout_app/services/run_host.dart';
@ -116,7 +116,7 @@ class _RunServiceTaskHandler extends TaskHandler {
engineFactory: recorderEngineFactory(
() => Sandbox.enabled
? sandboxRunSource()
: NoPromptSource(GeolocatorPositionSource()),
: NoPromptSource(LocationManagerPositionSource()),
),
port: port,
store: await openRunCheckpointStore(),

View File

@ -551,70 +551,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
geoclue:
dependency: transitive
description:
name: geoclue
sha256: c2a998c77474fc57aa00c6baa2928e58f4b267649057a1c76738656e9dbd2a7f
url: "https://pub.dev"
source: hosted
version: "0.1.1"
geolocator:
dependency: "direct main"
description:
name: geolocator
sha256: "2b3bc0f7320d5a5fa02f8209ffcf1aaa7c2cabb7b2bedbc0cfeb5ff2778b8b09"
url: "https://pub.dev"
source: hosted
version: "14.1.2"
geolocator_android:
dependency: transitive
description:
name: geolocator_android
sha256: e67583809a1fab5f4a2255902978e2bc095ace9451fde0d9624e54facd0a5c22
url: "https://pub.dev"
source: hosted
version: "5.2.1"
geolocator_apple:
dependency: transitive
description:
name: geolocator_apple
sha256: a1e177f3081089724f2095856aeb96eb8d7d1c59e425aed8dd4de7aa2c11ab66
url: "https://pub.dev"
source: hosted
version: "2.4.3"
geolocator_linux:
dependency: transitive
description:
name: geolocator_linux
sha256: "3da7420f11c3496511a5bd3c18fd67b88e5659f12e46b7ce00a788f6996e850a"
url: "https://pub.dev"
source: hosted
version: "0.2.6"
geolocator_platform_interface:
dependency: transitive
description:
name: geolocator_platform_interface
sha256: e012af6b7e2674e7500237ab11e44ac9119e87fd0105e7bb55cde1750e8e5d80
url: "https://pub.dev"
source: hosted
version: "4.4.1"
geolocator_web:
dependency: transitive
description:
name: geolocator_web
sha256: "19e485a0f8d6a88abcf9c53cba3a4105e14b7435ed8ac1c108c067b938fe8429"
url: "https://pub.dev"
source: hosted
version: "4.1.4"
geolocator_windows:
dependency: transitive
description:
name: geolocator_windows
sha256: "175435404d20278ffd220de83c2ca293b73db95eafbdc8131fe8609be1421eb6"
url: "https://pub.dev"
source: hosted
version: "0.2.5"
glob:
dependency: transitive
description:
@ -632,14 +568,6 @@ packages:
url: "https://github.com/kuhyx/utils"
source: git
version: "0.1.0"
gsettings:
dependency: transitive
description:
name: gsettings
sha256: "1b0ce661f5436d2db1e51f3c4295a49849f03d304003a7ba177d01e3a858249c"
url: "https://pub.dev"
source: hosted
version: "0.2.8"
hooks:
dependency: transitive
description:
@ -856,22 +784,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.0"
package_info_plus:
dependency: transitive
description:
name: package_info_plus
sha256: "73ef2cd5c11a7548f9b7f6fc2a4f6113de2e64aa2a70de63feede95548822908"
url: "https://pub.dev"
source: hosted
version: "10.2.2"
package_info_plus_platform_interface:
dependency: transitive
description:
name: package_info_plus_platform_interface
sha256: db762cb2f4f25ee60fb6359773861b0f199e00b90d237bd85a76a1e806b46ef4
url: "https://pub.dev"
source: hosted
version: "4.1.0"
path:
dependency: "direct main"
description:
@ -992,6 +904,15 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.1.6"
platform_location:
dependency: "direct main"
description:
path: platform_location
ref: "platform_location-v0.1.0"
resolved-ref: b8e2bf88132716bc3b52c0ae13fb169e1e411eaa
url: "https://github.com/kuhyx/utils"
source: git
version: "0.1.0"
plugin_platform_interface:
dependency: transitive
description:

View File

@ -39,8 +39,6 @@ dependencies:
flutter_map: 8.3.2
flutter_map_cache: 2.1.2
flutter_secure_storage: 11.2.0
# GPS fixes, behind the PositionSource seam (lib/services/position_source).
geolocator: 14.1.2
# One-tap sign-in for the sync account (AccountManager ID token; no Play
# Services SDK in the APK). App-side only: crdt_sync stays pure Dart so it
# keeps working on Linux desktop and headless.
@ -56,6 +54,14 @@ dependencies:
path: 1.9.1
path_provider: 2.1.6
permission_handler: 13.0.2
# GPS fixes over AOSP LocationManager, behind the PositionSource seam
# (lib/services/position_source). No Google Play services: the previous
# plugin linked non-free play-services-location on Android.
platform_location:
git:
url: https://github.com/kuhyx/utils
ref: platform_location-v0.1.0
path: platform_location
shared_preferences: 2.5.6
sqflite: 2.4.6
# Linux desktop has no sqflite plugin; the FFI factory drives the same

View File

@ -1,27 +0,0 @@
/// A geolocator platform that answers from fields, for tests that build the
/// real run controller (and so the real position source) without a device.
library;
import 'package:geolocator/geolocator.dart';
/// Location on, permission granted, no positions.
class FakeGeolocator extends GeolocatorPlatform {
/// Whether the device's location switch is on.
bool serviceOn = true;
/// The permission [checkPermission] reports.
LocationPermission permission = LocationPermission.whileInUse;
@override
Future<bool> isLocationServiceEnabled() async => serviceOn;
@override
Future<LocationPermission> checkPermission() async => permission;
@override
Future<LocationPermission> requestPermission() async => permission;
@override
Stream<Position> getPositionStream({LocationSettings? locationSettings}) =>
const Stream.empty();
}

View File

@ -0,0 +1,54 @@
/// A platform_location fake that answers from fields, for tests that build
/// the real run controller (and so the real position source) without a
/// device.
library;
import 'package:platform_location/platform_location.dart';
/// Location on, permission granted, no fixes unless [positions] says so.
class FakePlatformLocation extends PlatformLocation {
/// Whether the device's location switch is on.
bool serviceOn = true;
/// The permission [checkPermission] reports.
LocationPermissionState current = LocationPermissionState.granted;
/// What a [requestPermission] call turns [current] into.
LocationPermissionState? afterRequest;
/// How many times [requestPermission] ran.
int requests = 0;
/// The `precise` flag of the last request.
bool? lastPrecise;
/// The interval the last [fixes] call asked for.
Duration? interval;
/// What [fixes] emits.
List<LocationFix> positions = [];
@override
Future<bool> isLocationEnabled() async => serviceOn;
@override
Future<LocationPermissionState> checkPermission() async => current;
@override
Future<LocationPermissionState> requestPermission({
bool precise = true,
}) async {
requests++;
lastPrecise = precise;
return current = afterRequest ?? current;
}
@override
Future<LocationFix> currentLocation() async => positions.first;
@override
Stream<LocationFix> fixes({Duration interval = const Duration(seconds: 2)}) {
this.interval = interval;
return Stream.fromIterable(positions);
}
}

View File

@ -3,7 +3,7 @@ import 'package:workout_app/models/run_track.dart';
import 'package:workout_app/sandbox/sandbox.dart';
import 'package:workout_app/sandbox/sandbox_run_replay.dart';
import 'package:workout_app/services/gps_filter.dart';
import 'package:workout_app/services/position_source_geolocator.dart';
import 'package:workout_app/services/position_source_location_manager.dart';
import 'package:workout_app/services/position_source_scripted.dart';
import 'package:workout_app/services/run_math.dart';
@ -70,7 +70,7 @@ void main() {
Sandbox.enabled = true;
expect(positionSourceFor(), isA<ScriptedPositionSource>());
Sandbox.enabled = false;
expect(positionSourceFor(), isA<GeolocatorPositionSource>());
expect(positionSourceFor(), isA<LocationManagerPositionSource>());
});
test('the idle position is the replay first fix, and only that', () async {
@ -83,6 +83,6 @@ void main() {
expect(fixes.single.longitude, first.longitude);
expect((await source.requestAccess()).isGranted, isTrue);
Sandbox.enabled = false;
expect(idlePositionSourceFor(), isA<GeolocatorPositionSource>());
expect(idlePositionSourceFor(), isA<LocationManagerPositionSource>());
});
}

View File

@ -1,7 +1,7 @@
// The run icon on the home screen opens the run screen.
import 'package:material_ui/material_ui.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:geolocator/geolocator.dart';
import 'package:platform_location/platform_location.dart';
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
@ -9,7 +9,7 @@ import 'package:workout_app/screens/run_screen.dart';
import 'package:workout_app/services/storage_service.dart';
import '../fake_audio_platform.dart';
import '../fake_geolocator.dart';
import '../fake_platform_location.dart';
import '../fake_secure_storage.dart';
import '../run_persist_rig.dart';
import '_home_test_fixtures.dart';
@ -29,9 +29,9 @@ void main() {
installFakeSecureStorage();
installFakeAudioPlatform();
// The real run controller asks the real source where the user is.
final original = GeolocatorPlatform.instance;
GeolocatorPlatform.instance = FakeGeolocator();
addTearDown(() => GeolocatorPlatform.instance = original);
final original = PlatformLocation.instance;
PlatformLocation.instance = FakePlatformLocation();
addTearDown(() => PlatformLocation.instance = original);
// `main` installs the real wiring; here the run lives in a fake host.
RunScreen.defaultControllerFactory = PersistRig().build;
addTearDown(() => RunScreen.defaultControllerFactory = null);

View File

@ -1,50 +1,19 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:geolocator/geolocator.dart';
import 'package:platform_location/platform_location.dart';
import 'package:workout_app/services/gps_filter.dart';
import 'package:workout_app/services/position_source.dart';
import 'package:workout_app/services/position_source_geolocator.dart';
import 'package:workout_app/services/position_source_location_manager.dart';
import 'package:workout_app/services/position_source_scripted.dart';
/// A platform that answers from fields instead of a method channel.
class _FakeGeolocator extends GeolocatorPlatform {
bool serviceOn = true;
LocationPermission current = LocationPermission.denied;
LocationPermission afterRequest = LocationPermission.whileInUse;
int requests = 0;
LocationSettings? settings;
List<Position> positions = [];
import '../fake_platform_location.dart';
@override
Future<bool> isLocationServiceEnabled() async => serviceOn;
@override
Future<LocationPermission> checkPermission() async => current;
@override
Future<LocationPermission> requestPermission() async {
requests++;
return current = afterRequest;
}
@override
Stream<Position> getPositionStream({LocationSettings? locationSettings}) {
settings = locationSettings;
return Stream.fromIterable(positions);
}
}
Position _position({bool mocked = false}) => Position(
LocationFix _position({bool mocked = false}) => LocationFix(
latitude: 48.8584,
longitude: 2.2945,
timestamp: DateTime.utc(2026, 11, 9, 12),
time: DateTime.utc(2026, 11, 9, 12),
accuracy: 4.5,
altitude: 33,
altitudeAccuracy: 1,
heading: 0,
headingAccuracy: 1,
speed: 2,
speedAccuracy: 1,
isMocked: mocked,
isMock: mocked,
);
void main() {
@ -54,88 +23,87 @@ void main() {
]);
});
group('GeolocatorPositionSource', () {
late _FakeGeolocator platform;
final original = GeolocatorPlatform.instance;
group('LocationManagerPositionSource', () {
late FakePlatformLocation platform;
final original = PlatformLocation.instance;
setUp(() {
platform = _FakeGeolocator();
GeolocatorPlatform.instance = platform;
platform = FakePlatformLocation()
..current = LocationPermissionState.denied
..afterRequest = LocationPermissionState.granted;
PlatformLocation.instance = platform;
});
tearDown(() => GeolocatorPlatform.instance = original);
tearDown(() => PlatformLocation.instance = original);
test('service off wins over any permission', () async {
platform.serviceOn = false;
expect(
await GeolocatorPositionSource().requestAccess(),
await LocationManagerPositionSource().requestAccess(),
LocationAccess.serviceOff,
);
expect(platform.requests, 0);
});
test('a denied permission is requested once and then granted', () async {
expect(
await GeolocatorPositionSource().requestAccess(),
LocationAccess.granted,
);
expect(platform.requests, 1);
});
test(
'a denied permission is requested once, precise, then granted',
() async {
expect(
await LocationManagerPositionSource().requestAccess(),
LocationAccess.granted,
);
expect(platform.requests, 1);
expect(platform.lastPrecise, isTrue);
},
);
test('an already granted permission is not requested again', () async {
platform.current = LocationPermission.always;
platform.current = LocationPermissionState.granted;
expect(
await GeolocatorPositionSource().requestAccess(),
await LocationManagerPositionSource().requestAccess(),
LocationAccess.granted,
);
expect(platform.requests, 0);
});
test('refusals map to denied / deniedForever', () async {
platform.afterRequest = LocationPermission.denied;
platform.afterRequest = LocationPermissionState.denied;
expect(
await GeolocatorPositionSource().requestAccess(),
await LocationManagerPositionSource().requestAccess(),
LocationAccess.denied,
);
platform.current = LocationPermission.deniedForever;
platform.afterRequest = LocationPermissionState.deniedForever;
expect(
await GeolocatorPositionSource().requestAccess(),
await LocationManagerPositionSource().requestAccess(),
LocationAccess.deniedForever,
);
platform.current = LocationPermission.unableToDetermine;
expect(
await GeolocatorPositionSource().requestAccess(),
LocationAccess.denied,
);
});
test('checkAccess reads the state and never prompts', () async {
final source = GeolocatorPositionSource();
final source = LocationManagerPositionSource();
expect(await source.checkAccess(), LocationAccess.denied);
platform.current = LocationPermission.whileInUse;
platform.current = LocationPermissionState.granted;
expect(await source.checkAccess(), LocationAccess.granted);
platform.current = LocationPermission.deniedForever;
platform.current = LocationPermissionState.deniedForever;
expect(await source.checkAccess(), LocationAccess.deniedForever);
platform.serviceOn = false;
expect(await source.checkAccess(), LocationAccess.serviceOff);
expect(platform.requests, 0);
});
test('fixes maps positions and asks for navigation accuracy', () async {
test('fixes maps platform fixes at the asked interval', () async {
platform.positions = [_position(), _position(mocked: true)];
final fixes = await GeolocatorPositionSource(
forceLocationManager: true,
).fixes(interval: const Duration(seconds: 3)).toList();
final fixes = await LocationManagerPositionSource()
.fixes(interval: const Duration(seconds: 3))
.toList();
expect(fixes, hasLength(2));
expect(fixes.first.latitude, 48.8584);
expect(fixes.first.longitude, 2.2945);
expect(fixes.first.altitudeM, 33);
expect(fixes.first.accuracyM, 4.5);
expect(fixes.first.time, DateTime.utc(2026, 11, 9, 12));
expect(fixes.first.isMocked, isFalse);
expect(fixes.last.isMocked, isTrue);
final android = platform.settings! as AndroidSettings;
expect(android.accuracy, LocationAccuracy.bestForNavigation);
expect(android.intervalDuration, const Duration(seconds: 3));
expect(android.forceLocationManager, isTrue);
expect(platform.interval, const Duration(seconds: 3));
});
});