diff --git a/stronglift_replacement/workout_app/android/app/src/main/kotlin/com/kuhy/workout_app/MainActivity.kt b/stronglift_replacement/workout_app/android/app/src/main/kotlin/com/kuhy/workout_app/MainActivity.kt index 0a54581..6b9dd45 100644 --- a/stronglift_replacement/workout_app/android/app/src/main/kotlin/com/kuhy/workout_app/MainActivity.kt +++ b/stronglift_replacement/workout_app/android/app/src/main/kotlin/com/kuhy/workout_app/MainActivity.kt @@ -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) diff --git a/stronglift_replacement/workout_app/lib/main.dart b/stronglift_replacement/workout_app/lib/main.dart index 7f98505..a0d75b1 100644 --- a/stronglift_replacement/workout_app/lib/main.dart +++ b/stronglift_replacement/workout_app/lib/main.dart @@ -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 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 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 diff --git a/stronglift_replacement/workout_app/lib/sandbox/sandbox.dart b/stronglift_replacement/workout_app/lib/sandbox/sandbox.dart index e93be91..59d0b2c 100644 --- a/stronglift_replacement/workout_app/lib/sandbox/sandbox.dart +++ b/stronglift_replacement/workout_app/lib/sandbox/sandbox.dart @@ -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 init([Map? environment]) async { try { enabled = await channel.invokeMethod('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 _askPackageName() async { + try { + final answer = await channel.invokeMethod('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; diff --git a/stronglift_replacement/workout_app/lib/services/backup_service.dart b/stronglift_replacement/workout_app/lib/services/backup_service.dart index 7c0d16a..7d93b6e 100644 --- a/stronglift_replacement/workout_app/lib/services/backup_service.dart +++ b/stronglift_replacement/workout_app/lib/services/backup_service.dart @@ -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; diff --git a/stronglift_replacement/workout_app/lib/services/http_server_service.dart b/stronglift_replacement/workout_app/lib/services/http_server_service.dart index 5a2470b..2c4d329 100644 --- a/stronglift_replacement/workout_app/lib/services/http_server_service.dart +++ b/stronglift_replacement/workout_app/lib/services/http_server_service.dart @@ -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 _loadFromDisk() async { + // SyncService.filePath is already keyed on packageName, and the external + // storage dir below is this package's own. final candidates = [SyncService.filePath]; // External storage is Android-only; getExternalStorageDirectory() throws an // UnsupportedError (not an Exception) on other platforms (desktop / test diff --git a/stronglift_replacement/workout_app/lib/services/pace_target_store.dart b/stronglift_replacement/workout_app/lib/services/pace_target_store.dart index cb49f47..a99ca3f 100644 --- a/stronglift_replacement/workout_app/lib/services/pace_target_store.dart +++ b/stronglift_replacement/workout_app/lib/services/pace_target_store.dart @@ -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; diff --git a/stronglift_replacement/workout_app/lib/services/run_upload_credentials.dart b/stronglift_replacement/workout_app/lib/services/run_upload_credentials.dart index 76a8a48..f830096 100644 --- a/stronglift_replacement/workout_app/lib/services/run_upload_credentials.dart +++ b/stronglift_replacement/workout_app/lib/services/run_upload_credentials.dart @@ -47,6 +47,7 @@ class ProvisionOutcome { Future _externalFilesDir() async { // coverage:ignore-start // Android-only plugin call; path_provider throws on every other platform. + // The dir is Android/data//files: one per package, not shared. if (Platform.isAndroid) return await getExternalStorageDirectory(); // coverage:ignore-end return null; diff --git a/stronglift_replacement/workout_app/lib/services/runnerup_import_state.dart b/stronglift_replacement/workout_app/lib/services/runnerup_import_state.dart index fcd20ee..6e9d68a 100644 --- a/stronglift_replacement/workout_app/lib/services/runnerup_import_state.dart +++ b/stronglift_replacement/workout_app/lib/services/runnerup_import_state.dart @@ -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. diff --git a/stronglift_replacement/workout_app/lib/services/shared_storage.dart b/stronglift_replacement/workout_app/lib/services/shared_storage.dart new file mode 100644 index 0000000..da6bfc7 --- /dev/null +++ b/stronglift_replacement/workout_app/lib/services/shared_storage.dart @@ -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, `.` 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); +} diff --git a/stronglift_replacement/workout_app/lib/services/storage_service.dart b/stronglift_replacement/workout_app/lib/services/storage_service.dart index 634f03f..3fc8654 100644 --- a/stronglift_replacement/workout_app/lib/services/storage_service.dart +++ b/stronglift_replacement/workout_app/lib/services/storage_service.dart @@ -1,3 +1,4 @@ +// phone-slot: not-shared-storage (mentions /sdcard in docs only) /// Persistent storage for exercise progression state using SQLite. library; diff --git a/stronglift_replacement/workout_app/lib/services/sync_service.dart b/stronglift_replacement/workout_app/lib/services/sync_service.dart index 97da854..7c22a72 100644 --- a/stronglift_replacement/workout_app/lib/services/sync_service.dart +++ b/stronglift_replacement/workout_app/lib/services/sync_service.dart @@ -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. diff --git a/stronglift_replacement/workout_app/test/services/shared_storage_test.dart b/stronglift_replacement/workout_app/test/services/shared_storage_test.dart new file mode 100644 index 0000000..6c59936 --- /dev/null +++ b/stronglift_replacement/workout_app/test/services/shared_storage_test.dart @@ -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); + }); +}