Key workout_app /sdcard paths on package name
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (flutter, flutter-analyze, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,flutter-analyze) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / lock-popup-safety (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (0) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (1) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (2) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (3) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (4) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (5) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (6) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (7) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (8) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (9) (push) Waiting to run
Tests / flutter-coverage (push) Blocked by required conditions
Tests / web-coverage (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run

A slot build (com.kuhy.slotN) shared /sdcard/WorkoutTracker and
workout_result.json with the daily build. MainActivity now reports the
package name; any package but com.kuhy.workout_app gets sibling paths
and never reads RunnerUp's exports. The daily build's paths are
unchanged, so the PC side needs nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Krzysztof Rudnicki 2026-11-16 09:07:37 +01:00
parent 981fad12f0
commit 8e52688138
12 changed files with 160 additions and 4 deletions

View File

@ -17,6 +17,9 @@ class MainActivity : FlutterActivity() {
.setMethodCallHandler { call, result ->
when (call.method) {
"isSandbox" -> result.success(BuildConfig.FLAVOR == "sandbox")
// A slot build runs this code under another package name;
// Dart keys its /sdcard paths on it.
"packageName" -> result.success(applicationContext.packageName)
"log" -> {
Log.i(LOG_TAG, call.arguments as String)
result.success(null)

View File

@ -24,6 +24,7 @@ import 'package:workout_app/services/progression_sync_service.dart';
import 'package:workout_app/services/run_upload_credentials.dart';
import 'package:workout_app/services/run_uploader.dart';
import 'package:workout_app/services/runnerup_importer.dart';
import 'package:workout_app/services/shared_storage.dart';
import 'package:workout_app/services/storage_service.dart';
import 'package:workout_app/services/sync_device_id.dart';
import 'package:workout_app/services/sync_service.dart';
@ -52,6 +53,8 @@ void main(List<String> args) async {
BackupService.baseDir = Sandbox.backupDir;
SyncService.filePath = Sandbox.syncFilePath;
SandboxLog.event('startup', {'args': args});
} else {
applySharedStoragePaths(Sandbox.packageName);
}
// Linux desktop has no sqflite plugin. The FFI factory runs the same
// schema and migrations against the same SQL, so this is a transport
@ -139,7 +142,11 @@ void main(List<String> args) async {
}
// One-time RunnerUp history import (no-op in the sandbox and off-phone,
// and once it completed). Off the launch path: it is many downloads.
unawaited(RunnerUpImporter().runGuarded());
unawaited(
RunnerUpImporter(
localDir: sharedPathFor(Sandbox.packageName, kRunnerUpLocalDir),
).runGuarded(),
);
runApp(const WorkoutApp());
}
// coverage:ignore-end

View File

@ -11,6 +11,7 @@ import 'dart:io' show Platform;
import 'package:flutter/foundation.dart' show debugPrint;
import 'package:flutter/services.dart';
import 'package:workout_app/services/shared_storage.dart';
/// Build-time facts about the running flavor, resolved once at startup.
abstract final class Sandbox {
@ -33,6 +34,11 @@ abstract final class Sandbox {
/// assign it directly to exercise both flavors in one process.
static bool enabled = false;
/// The running application id, asked of MainActivity by [init]. Anything
/// but `com.kuhy.workout_app` (a phone slot build) keeps its shared-storage
/// files apart; see `shared_storage.dart`.
static String packageName = kDailyPackage;
/// Rest length (seconds) every break uses while [enabled]; persisted by
/// the Settings screen, loaded by `main`.
static int restSecs = defaultRestSecs;
@ -57,7 +63,9 @@ abstract final class Sandbox {
static Future<void> init([Map<String, String>? environment]) async {
try {
enabled = await channel.invokeMethod<bool>('isSandbox') ?? false;
packageName = await _askPackageName();
} on MissingPluginException catch (error) {
packageName = kDailyPackage;
enabled = (environment ?? Platform.environment)[envVar] == '1';
debugPrint(
'WorkoutApp: no sandbox channel on this platform ($error) — '
@ -66,6 +74,21 @@ abstract final class Sandbox {
}
}
/// The package name MainActivity reports; an older MainActivity that does
/// not answer means the daily build, whose paths it always used.
static Future<String> _askPackageName() async {
try {
final answer = await channel.invokeMethod<Object?>('packageName');
return answer is String ? answer : kDailyPackage;
} on MissingPluginException catch (error) {
debugPrint(
'WorkoutApp: MainActivity does not report its package ($error) — '
'using the daily build storage paths.',
);
return kDailyPackage;
}
}
/// [defaultSecs] for the daily build, the override for the sandbox.
static int rest(int defaultSecs) => enabled ? restSecs : defaultSecs;

View File

@ -48,7 +48,8 @@ class BackupService {
// Overridable: tests point it at a real temp directory instead of
// `/sdcard`, which doesn't exist on the test host, and the sandbox flavor
// at its own `/sdcard/WorkoutTrackerSandbox` beside the daily build's.
// at its own `/sdcard/WorkoutTrackerSandbox` beside the daily build's;
// `main` keys it on packageName for any other install (shared_storage.dart).
/// The directory backup files are written to and read from.
static String baseDir = kBackupDir;

View File

@ -77,6 +77,8 @@ class HttpServerService {
/// On startup, try to load the last saved workout JSON from disk so the
/// HTTP endpoint is populated even before the next workout is completed.
Future<void> _loadFromDisk() async {
// SyncService.filePath is already keyed on packageName, and the external
// storage dir below is this package's own.
final candidates = <String>[SyncService.filePath];
// External storage is Android-only; getExternalStorageDirectory() throws an
// UnsupportedError (not an Exception) on other platforms (desktop / test

View File

@ -1,3 +1,4 @@
// phone-slot: not-shared-storage (mentions /sdcard in docs only)
/// Where the last pace target is kept between runs (shared_preferences).
library;

View File

@ -47,6 +47,7 @@ class ProvisionOutcome {
Future<Directory?> _externalFilesDir() async {
// coverage:ignore-start
// Android-only plugin call; path_provider throws on every other platform.
// The dir is Android/data/<packageName>/files: one per package, not shared.
if (Platform.isAndroid) return await getExternalStorageDirectory();
// coverage:ignore-end
return null;

View File

@ -4,7 +4,8 @@ library;
import 'package:flutter/foundation.dart';
/// Where RunnerUp itself leaves exports on the phone.
/// Where RunnerUp itself leaves exports on the phone. `main` keys it on
/// packageName so a slot build never reads it (shared_storage.dart).
const String kRunnerUpLocalDir = '/sdcard/Documents/RunnerUp';
/// `shared_preferences` key: every remote export is in the database.

View File

@ -0,0 +1,27 @@
/// Shared-storage paths keyed by the running package name.
///
/// `/sdcard` is one directory for every install on the phone. A second
/// install of this code under another package name — a `com.kuhy.slotN`
/// test build — would otherwise read and overwrite the daily build's backup
/// and the workout file the PC pulls. So only the daily package keeps the
/// paths the PC knows; any other package gets a sibling named after itself.
/// The sandbox flavor keeps its own fixed paths (see `Sandbox`).
library;
import 'package:workout_app/services/backup_service.dart';
import 'package:workout_app/services/sync_service.dart';
/// The daily build's application id; the only package that owns the
/// `/sdcard` paths the PC reads.
const String kDailyPackage = 'com.kuhy.workout_app';
/// [dailyPath] for the daily build, `<dailyPath>.<packageName>` otherwise.
String sharedPathFor(String packageName, String dailyPath) =>
packageName == kDailyPackage ? dailyPath : '$dailyPath.$packageName';
/// Points the backup dir and the workout file at [packageName]'s own paths.
/// Must run before the first read or write of either.
void applySharedStoragePaths(String packageName) {
BackupService.baseDir = sharedPathFor(packageName, kBackupDir);
SyncService.filePath = sharedPathFor(packageName, kSyncFilePath);
}

View File

@ -1,3 +1,4 @@
// phone-slot: not-shared-storage (mentions /sdcard in docs only)
/// Persistent storage for exercise progression state using SQLite.
library;

View File

@ -13,7 +13,8 @@ const String kSyncFilePath = '/sdcard/workout_result.json';
/// Handles writing completed workout sessions to disk and the HTTP server.
class SyncService {
/// Where the result file is written; the sandbox flavor points this at
/// its own file so the PC's ADB pull never sees a sandbox workout.
/// its own file so the PC's ADB pull never sees a sandbox workout, and
/// `main` keys it on packageName for any other install (shared_storage.dart).
static String filePath = kSyncFilePath;
/// Writes [session] as JSON to external storage and updates the HTTP server.

View File

@ -0,0 +1,88 @@
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:workout_app/sandbox/sandbox.dart';
import 'package:workout_app/services/backup_service.dart';
import 'package:workout_app/services/runnerup_import_state.dart';
import 'package:workout_app/services/shared_storage.dart';
import 'package:workout_app/services/sync_service.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
void answer(Object? Function(MethodCall call)? handler) {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(
Sandbox.channel,
handler == null ? null : (call) async => handler(call),
);
}
tearDown(() {
answer(null);
Sandbox.enabled = false;
Sandbox.packageName = kDailyPackage;
});
test('the daily build keeps the paths the PC reads', () {
expect(sharedPathFor(kDailyPackage, kBackupDir), '/sdcard/WorkoutTracker');
expect(
sharedPathFor(kDailyPackage, kSyncFilePath),
'/sdcard/workout_result.json',
);
});
test('any other package gets its own sibling path', () {
expect(
sharedPathFor('com.kuhy.slot3', kBackupDir),
'/sdcard/WorkoutTracker.com.kuhy.slot3',
);
expect(
sharedPathFor('com.kuhy.slot3', kSyncFilePath),
isNot(kSyncFilePath),
);
expect(
sharedPathFor('com.kuhy.slot3', kRunnerUpLocalDir),
isNot(kRunnerUpLocalDir),
);
});
test('init asks MainActivity for the package name', () async {
answer((call) => call.method == 'packageName' ? 'com.kuhy.slot3' : false);
await Sandbox.init();
expect(Sandbox.packageName, 'com.kuhy.slot3');
});
test('an older MainActivity without packageName means the daily build',
() async {
answer((call) {
if (call.method == 'packageName') {
throw MissingPluginException();
}
return false;
});
await Sandbox.init();
expect(Sandbox.packageName, kDailyPackage);
});
test('no channel at all means the daily build', () async {
answer(null);
await Sandbox.init({});
expect(Sandbox.packageName, kDailyPackage);
});
test('applySharedStoragePaths moves a slot off the daily files', () {
final savedBase = BackupService.baseDir;
final savedSync = SyncService.filePath;
addTearDown(() {
BackupService.baseDir = savedBase;
SyncService.filePath = savedSync;
});
applySharedStoragePaths('com.kuhy.slot3');
expect(BackupService.baseDir, '/sdcard/WorkoutTracker.com.kuhy.slot3');
expect(SyncService.filePath, '/sdcard/workout_result.json.com.kuhy.slot3');
applySharedStoragePaths(kDailyPackage);
expect(BackupService.baseDir, kBackupDir);
expect(SyncService.filePath, kSyncFilePath);
});
}