screen-locker/stronglift_replacement/workout_app/lib/sandbox/sandbox.dart
Krzysztof kuhy Rudnicki 8e52688138
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
Key workout_app /sdcard paths on package name
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>
2026-11-16 09:47:49 +01:00

100 lines
4.2 KiB
Dart

/// The sandbox flavor: a second install that can be poked at without fear.
///
/// `com.kuhy.workout_app.sandbox` is a separate package, so Android already
/// keeps its database, secure storage and notifications apart from the daily
/// build. This class carries the rest of the isolation into Dart: no network,
/// no LAN server for the PC to pull from, its own `/sdcard` paths, and a
/// rest length short enough to watch a break start and end in one sitting.
library;
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 {
/// Channel MainActivity answers on; also carries the logcat trace.
static const MethodChannel channel = MethodChannel(
'com.kuhy.workout_app/sandbox',
);
/// Where the sandbox mirrors its backups, beside — never inside — the
/// daily build's `/sdcard/WorkoutTracker`.
static const String backupDir = '/sdcard/WorkoutTrackerSandbox';
/// Where the sandbox writes finished workouts; the PC never reads this.
static const String syncFilePath = '/sdcard/workout_result_sandbox.json';
/// Rest length used when no override has been saved yet.
static const int defaultRestSecs = 5;
/// Whether this process is the sandbox flavor. Set once by [init]; tests
/// 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;
/// Replay speed-ups the Settings control offers.
static const List<int> replayFactors = [1, 10, 60];
/// Replay speed-up used when no override has been saved yet.
static const int defaultReplayFactor = 60;
/// How many times faster than real time the sandbox replays a GPS run;
/// persisted by the Settings screen, loaded by `main`.
static int runReplayFactor = defaultReplayFactor;
/// Environment variable that turns the sandbox on where there is no
/// flavor to ask — the Linux desktop build, run under Xvfb to check a
/// change on the PC before it goes anywhere near the phone.
static const String envVar = 'WORKOUT_SANDBOX';
/// Asks the platform which flavor built this process, or, where there is
/// no MainActivity to ask (Linux, tests), reads [envVar].
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) — '
'$envVar=${enabled ? '1: sandbox' : 'unset: daily build'}.',
);
}
}
/// 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;
/// [defaultLabel] for the daily build; the sandbox names its real length
/// so a 5 s countdown is never captioned "3 min".
static String restLabel(String defaultLabel) =>
enabled ? 'Rest ($restSecs s — sandbox)' : defaultLabel;
}