feat(6.2): Android screen recording backend (MediaProjection + gallery save)

This commit is contained in:
Jonas Bark
2026-06-24 16:51:58 +02:00
parent 64e015313b
commit d1ecbe2cbc
3 changed files with 55 additions and 1 deletions

View File

@@ -28,6 +28,8 @@
<!-- Required for trainer overlay display -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<!-- Strip flutter_screen_recording library permissions not needed by this app -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" tools:node="remove" />
<!-- Android 14+: foreground service type declaration for overlay window service -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />

View File

@@ -0,0 +1,51 @@
import 'dart:io';
import 'package:bike_control/services/screen_recording/screen_recording_service.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_screen_recording/flutter_screen_recording.dart';
import 'package:gal/gal.dart';
/// Android backend: system-wide MediaProjection capture via flutter_screen_recording,
/// then the resulting mp4 is copied into the device gallery via `gal`.
class AndroidScreenRecorder implements ScreenRecorderBackend {
@override
Future<bool> isAvailable() async => true;
@override
Future<bool> ensurePermission() async {
// MediaProjection consent is requested by startRecordScreen itself.
// Gallery save needs gallery access on older Androids; request up front.
try {
if (await Gal.hasAccess(toAlbum: true)) return true;
return await Gal.requestAccess(toAlbum: true);
} catch (e, s) {
debugPrintStack(label: 'gal access: $e', stackTrace: s);
return true; // don't block recording if the gallery check itself failed
}
}
@override
Future<bool> start() async {
try {
// Video only — use startRecordScreen (no audio variant).
final name = 'BikeControl_${DateTime.now().millisecondsSinceEpoch}';
return await FlutterScreenRecording.startRecordScreen(name);
} catch (e, s) {
debugPrintStack(label: 'android startRecordScreen: $e', stackTrace: s);
return false;
}
}
@override
Future<String?> stop() async {
try {
final path = await FlutterScreenRecording.stopRecordScreen;
if (path.isEmpty || !File(path).existsSync()) return null;
await Gal.putVideo(path, album: 'BikeControl');
return path;
} catch (e, s) {
debugPrintStack(label: 'android stopRecordScreen/gal: $e', stackTrace: s);
return null;
}
}
}

View File

@@ -1,5 +1,6 @@
import 'dart:io';
import 'package:bike_control/services/screen_recording/backends/android_screen_recorder.dart';
import 'package:bike_control/services/screen_recording/backends/native_channel_screen_recorder.dart';
import 'package:bike_control/services/screen_recording/backends/unsupported_screen_recorder.dart';
import 'package:flutter/foundation.dart';
@@ -104,6 +105,6 @@ ScreenRecorderBackend createScreenRecorderBackend() {
if (Platform.isIOS || Platform.isMacOS || Platform.isWindows) {
return NativeChannelScreenRecorder();
}
// Android backend added later; Linux unsupported.
if (Platform.isAndroid) return AndroidScreenRecorder();
return UnsupportedScreenRecorder();
}